From 38b6d77561c87cb427b21b811ce10208f2eb5b1a Mon Sep 17 00:00:00 2001 From: Roberto Hueso Gómez Date: Wed, 13 May 2020 14:10:01 +0200 Subject: Switch config file to json This patch makes configuration parsing easier as well as making the full configuration available in many subclasses. --- src/ogClient.py | 25 +++++++++++++++---------- src/ogRest.py | 9 +++++---- 2 files changed, 20 insertions(+), 14 deletions(-) (limited to 'src') diff --git a/src/ogClient.py b/src/ogClient.py index ff2c704..fec8eb9 100644 --- a/src/ogClient.py +++ b/src/ogClient.py @@ -11,6 +11,7 @@ import select import socket import time import email +import platform from io import StringIO from src.restRequest import * @@ -23,19 +24,23 @@ class State(Enum): FORCE_DISCONNECTED = 2 class ogClient: - def __init__(self, ip, port, mode, samba_config=None): - if mode not in {'virtual', 'linux'}: + def __init__(self, config): + self.CONFIG = config + + self.mode = self.CONFIG['opengnsys']['mode'] + if self.mode not in {'virtual', 'linux'}: raise ValueError('Mode not supported.') + if self.mode == 'linux' and platform.system() != 'Linux': + raise ValueError('Linux mode not supported on ' + 'non-Linux platform.') - if samba_config: - assert('user' in samba_config) - assert('pass' in samba_config) + if self.CONFIG['samba']['activate']: + assert('user' in self.CONFIG['samba']) + assert('pass' in self.CONFIG['samba']) - self.ip = ip - self.port = port - self.mode = mode - self.samba_config = samba_config - self.ogrest = ogRest(self.mode, self.samba_config) + self.ip = self.CONFIG['opengnsys']['ip'] + self.port = self.CONFIG['opengnsys']['port'] + self.ogrest = ogRest(self.CONFIG) def get_socket(self): return self.sock diff --git a/src/ogRest.py b/src/ogRest.py index abe05c1..14b9d33 100644 --- a/src/ogRest.py +++ b/src/ogRest.py @@ -242,14 +242,15 @@ class ogResponses(Enum): SERVICE_UNAVAILABLE=5 class ogRest(): - def __init__(self, mode, samba_config): + def __init__(self, config): self.proc = None self.terminated = False self.state = ThreadState.IDLE - self.mode = mode - self.samba_config = samba_config + self.CONFIG = config + self.mode = self.CONFIG['opengnsys']['mode'] + self.samba_config = self.CONFIG['samba'] - if self.mode == 'linux' and platform.system() == 'Linux': + if self.mode == 'linux': self.operations = OgLinuxOperations() elif self.mode == 'virtual': self.operations = OgVirtualOperations() -- cgit v1.2.3-18-g5258