Selaa lähdekoodia

introduce a configuration module

Sven Czarnian 3 vuotta sitten
vanhempi
commit
7b26e27c9d
4 muutettua tiedostoa jossa 66 lisäystä ja 0 poistoa
  1. 23 0
      aman/config/Configuration.py
  2. 41 0
      aman/config/Server.py
  3. 0 0
      aman/config/__init__.py
  4. 2 0
      setup.py

+ 23 - 0
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'])

+ 41 - 0
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)

+ 0 - 0
aman/config/__init__.py


+ 2 - 0
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'
     ]