Bladeren bron

[update] GetConfig

jhao104 9 jaren geleden
bovenliggende
commit
1837cd63e0
1 gewijzigde bestanden met toevoegingen van 53 en 0 verwijderingen
  1. 53 0
      Util/GetConfig.py

+ 53 - 0
Util/GetConfig.py

@@ -0,0 +1,53 @@
+# -*- coding: utf-8 -*-
+"""
+-------------------------------------------------
+   File Name:     GetConfig.py  
+   Description :  fetch config from config.ini
+   Author :       JHao
+   date:          2016/12/3
+-------------------------------------------------
+   Change Activity:
+                   2016/12/3: get db property func
+-------------------------------------------------
+"""
+__author__ = 'JHao'
+
+import os
+import ConfigParser
+from Util.utilClass import LazyProperty
+
+
+class GetConfig(object):
+    """
+    to get config from config.ini
+    """
+
+    def __init__(self):
+        self.pwd = os.path.split(os.path.realpath(__file__))[0]
+        self.config_path = os.path.join(os.path.split(self.pwd)[0], 'config.ini')
+        self.config_file = ConfigParser.ConfigParser()
+        self.config_file.read(self.config_path)
+
+    @LazyProperty
+    def db_type(self):
+        return self.config_file.get('DB', 'type')
+
+    @LazyProperty
+    def db_name(self):
+        return self.config_file.get('DB', 'name')
+
+    @LazyProperty
+    def db_host(self):
+        return self.config_file.get('DB', 'host')
+
+    @LazyProperty
+    def db_port(self):
+        return self.config_file.get('DB', 'port')
+
+
+if __name__ == '__main__':
+    gg = GetConfig()
+    print gg.db_type
+    print gg.db_name
+    print gg.db_host
+    print gg.db_port