summaryrefslogtreecommitdiffstats
path: root/testapi/opnfv_testapi/common/config.py
diff options
context:
space:
mode:
authorSerenaFeng <feng.xiaowei@zte.com.cn>2017-07-13 20:09:10 +0800
committerSerenaFeng <feng.xiaowei@zte.com.cn>2017-07-20 15:18:08 +0800
commit8ec6f1e7d682a56c122f930febb28da1575b8ff8 (patch)
tree5881548d80d23caf66bab8e6ea810cd1884dbc54 /testapi/opnfv_testapi/common/config.py
parentcf402a2a6888ade5c57165dc978a59d2330307a7 (diff)
decouple the mutual-dependence of config.py and server.py
Currently server.py relies on CONF while starting the service, and config.py's config_fie is set in server.py, which is wrongly bi-depended this patch aims to let Config parse the sys.argv personally, just as oslo.config do, so that decouple the mutual-dependency Change-Id: I46887d122a98d478caebe9eeb7ab038941ce1f6b Signed-off-by: SerenaFeng <feng.xiaowei@zte.com.cn>
Diffstat (limited to 'testapi/opnfv_testapi/common/config.py')
-rw-r--r--testapi/opnfv_testapi/common/config.py36
1 files changed, 27 insertions, 9 deletions
diff --git a/testapi/opnfv_testapi/common/config.py b/testapi/opnfv_testapi/common/config.py
index f73c0ab..4cd53c6 100644
--- a/testapi/opnfv_testapi/common/config.py
+++ b/testapi/opnfv_testapi/common/config.py
@@ -8,14 +8,16 @@
# feng.xiaowei@zte.com.cn remove prepare_put_request 5-30-2016
##############################################################################
import ConfigParser
+import argparse
import os
+import sys
class Config(object):
- CONFIG = None
def __init__(self):
- self.file = self.CONFIG if self.CONFIG else self._default_config()
+ self.config_file = None
+ self._set_config_file()
self._parse()
self._parse_per_page()
self.static_path = os.path.join(
@@ -24,11 +26,11 @@ class Config(object):
'static')
def _parse(self):
- if not os.path.exists(self.file):
- raise Exception("%s not found" % self.file)
+ if not os.path.exists(self.config_file):
+ raise Exception("%s not found" % self.config_file)
config = ConfigParser.RawConfigParser()
- config.read(self.file)
+ config.read(self.config_file)
self._parse_section(config)
def _parse_section(self, config):
@@ -53,8 +55,24 @@ class Config(object):
value = False
return value
- @staticmethod
- def _default_config():
+ def _set_config_file(self):
+ if not self._set_sys_config_file():
+ self._set_default_config_file()
+
+ def _set_sys_config_file(self):
+ parser = argparse.ArgumentParser()
+ parser.add_argument("-c", "--config-file", dest='config_file',
+ help="Config file location", metavar="FILE")
+ args, _ = parser.parse_known_args(sys.argv)
+ try:
+ self.config_file = args.config_file
+ finally:
+ return self.config_file is not None
+
+ def _set_default_config_file(self):
is_venv = os.getenv('VIRTUAL_ENV')
- return os.path.join('/' if not is_venv else is_venv,
- 'etc/opnfv_testapi/config.ini')
+ self.config_file = os.path.join('/' if not is_venv else is_venv,
+ 'etc/opnfv_testapi/config.ini')
+
+
+CONF = Config()