From 7b26e27c9dd244a47db2f867467ff23250b5eba5 Mon Sep 17 00:00:00 2001 From: Sven Czarnian Date: Sun, 15 Aug 2021 08:59:32 +0200 Subject: [PATCH] introduce a configuration module --- aman/config/Configuration.py | 23 ++++++++++++++++++++ aman/config/Server.py | 41 ++++++++++++++++++++++++++++++++++++ aman/config/__init__.py | 0 setup.py | 2 ++ 4 files changed, 66 insertions(+) create mode 100644 aman/config/Configuration.py create mode 100644 aman/config/Server.py create mode 100644 aman/config/__init__.py diff --git a/aman/config/Configuration.py b/aman/config/Configuration.py new file mode 100644 index 0000000..6ec85db --- /dev/null +++ b/aman/config/Configuration.py @@ -0,0 +1,23 @@ +#!/usr/bin/env python + +import configparser +import sys + +from aman.config.Server import Server + +class Configuration: + def __init__(self, filepath : str): + config = configparser.ConfigParser() + config.read(filepath) + + # search the required sections + serverSectionAvailable = False + for key in config: + if 'SERVER' == key: + serverSectionAvailable = True + + if not serverSectionAvailable: + sys.stderr.write('No server-configuration section found!') + sys.exit(-1) + + self.Server = Server(config['SERVER']) diff --git a/aman/config/Server.py b/aman/config/Server.py new file mode 100644 index 0000000..b3216d0 --- /dev/null +++ b/aman/config/Server.py @@ -0,0 +1,41 @@ +#!/usr/bin/env python + +import configparser; +import sys + +class Server(): + def __init__(self, config : configparser.ConfigParser): + self.Address = None + self.PortReceiver = None + self.PortNotification = None + self.ServerKeyPath = None + self.ClientKeyPath = None + + # search the required sections + for key in config: + if 'address' == key: + self.Address = config['address'] + elif 'portreceiver' == key: + self.PortReceiver = int(config['portreceiver']) + elif 'portnotification' == key: + self.PortNotification = int(config['portnotification']) + elif 'serverkeypath' == key: + self.ServerKeyPath = config['serverkeypath'] + elif 'clientkeypath' == key: + self.ClientKeyPath = config['clientkeypath'] + + if self.Address is None: + sys.stderr.write('No server-address configuration found!') + sys.exit(-1) + if self.PortReceiver is None: + sys.stderr.write('No server-port-receiver configuration found!') + sys.exit(-1) + if self.PortNotification is None: + sys.stderr.write('No server-port-notification configuration found!') + sys.exit(-1) + if self.ServerKeyPath is None: + sys.stderr.write('No server-key-path configuration found!') + sys.exit(-1) + if self.ClientKeyPath is None: + sys.stderr.write('No client-key-path configuration found!') + sys.exit(-1) diff --git a/aman/config/__init__.py b/aman/config/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/setup.py b/setup.py index 4d11cbb..9bc8328 100644 --- a/setup.py +++ b/setup.py @@ -61,6 +61,7 @@ setup( packages = [ 'aman', 'aman.com', + 'aman.config', 'aman.tools' ], namespace_packages = [ 'aman' ], @@ -73,6 +74,7 @@ setup( install_requires=[ 'argparse', 'setuptools', + 'configparser', 'pyzmq', 'protobuf' ]