[PATCH v4 1/4] Move configuration parsing to config.py

The configuration is also needed for other code except starting kimchi server. So it should be moved to a separate module, config.py. Then the configuration can be accessed directly by importing config module. Signed-off-by: Mark Wu <wudxw@linux.vnet.ibm.com> --- src/kimchi/config.py.in | 25 +++++++++++++++++++++++++ src/kimchid.in | 20 +------------------- 2 files changed, 26 insertions(+), 19 deletions(-) diff --git a/src/kimchi/config.py.in b/src/kimchi/config.py.in index f3c408a..a3973ce 100644 --- a/src/kimchi/config.py.in +++ b/src/kimchi/config.py.in @@ -27,11 +27,14 @@ import os import platform +from ConfigParser import SafeConfigParser from glob import iglob from kimchi.xmlutils import xpath_get_text +DEFAULT_LOG_LEVEL = "debug" + def get_prefix(): if __file__[0] == '/': @@ -166,5 +169,27 @@ def get_plugin_tab_xml(name): return os.path.join(_get_plugin_ui_dir(name), 'config/tab-ext.xml') +def _get_config(): + config = SafeConfigParser() + config.add_section("server") + config.set("server", "host", "0.0.0.0") + config.set("server", "port", "8000") + config.set("server", "ssl_port", "8001") + config.set("server", "ssl_cert", "") + config.set("server", "ssl_key", "") + config.set("server", "environment", "development") + config.add_section("logging") + config.set("logging", "log_dir", get_default_log_dir()) + config.set("logging", "log_level", DEFAULT_LOG_LEVEL) + + config_file = os.path.join(get_config_dir(), 'kimchi.conf') + if os.path.exists(config_file): + config.read(config_file) + return config + + +config = _get_config() + + if __name__ == '__main__': print get_prefix() diff --git a/src/kimchid.in b/src/kimchid.in index 7865713..548fa52 100644 --- a/src/kimchid.in +++ b/src/kimchid.in @@ -33,31 +33,13 @@ import kimchi.config if kimchi.config.without_installation(): sys.path.append(kimchi.config.get_prefix()) -from ConfigParser import SafeConfigParser +from kimchi.config import config from optparse import OptionParser ACCESS_LOG = "kimchi-access.log" ERROR_LOG = "kimchi-error.log" -CONFIG_FILE = "%s/kimchi.conf" % kimchi.config.get_config_dir() -DEFAULT_LOG_DIR = kimchi.config.get_default_log_dir() -DEFAULT_LOG_LEVEL = "debug" def main(options): - config = SafeConfigParser() - config.add_section("server") - config.set("server", "host", "0.0.0.0") - config.set("server", "port", "8000") - config.set("server", "ssl_port", "8001") - config.set("server", "ssl_cert", "") - config.set("server", "ssl_key", "") - config.set("server", "environment", "development") - config.add_section("logging") - config.set("logging", "log_dir", DEFAULT_LOG_DIR) - config.set("logging", "log_level", DEFAULT_LOG_LEVEL) - - if os.path.exists(CONFIG_FILE): - config.read(CONFIG_FILE) - host = config.get("server", "host") port = config.get("server", "port") ssl_port = config.get("server", "ssl_port") -- 1.8.4.2

Reviewed-by: Royce Lv<lvroyce@linux.vnet.ibm.com> On 2014年01月08日 18:04, Mark Wu wrote:
The configuration is also needed for other code except starting kimchi server. So it should be moved to a separate module, config.py. Then the configuration can be accessed directly by importing config module.
Signed-off-by: Mark Wu <wudxw@linux.vnet.ibm.com> --- src/kimchi/config.py.in | 25 +++++++++++++++++++++++++ src/kimchid.in | 20 +------------------- 2 files changed, 26 insertions(+), 19 deletions(-)
diff --git a/src/kimchi/config.py.in b/src/kimchi/config.py.in index f3c408a..a3973ce 100644 --- a/src/kimchi/config.py.in +++ b/src/kimchi/config.py.in @@ -27,11 +27,14 @@ import os import platform
+from ConfigParser import SafeConfigParser from glob import iglob
from kimchi.xmlutils import xpath_get_text
+DEFAULT_LOG_LEVEL = "debug" +
def get_prefix(): if __file__[0] == '/': @@ -166,5 +169,27 @@ def get_plugin_tab_xml(name): return os.path.join(_get_plugin_ui_dir(name), 'config/tab-ext.xml')
+def _get_config(): + config = SafeConfigParser() + config.add_section("server") + config.set("server", "host", "0.0.0.0") + config.set("server", "port", "8000") + config.set("server", "ssl_port", "8001") + config.set("server", "ssl_cert", "") + config.set("server", "ssl_key", "") + config.set("server", "environment", "development") + config.add_section("logging") + config.set("logging", "log_dir", get_default_log_dir()) + config.set("logging", "log_level", DEFAULT_LOG_LEVEL) + + config_file = os.path.join(get_config_dir(), 'kimchi.conf') + if os.path.exists(config_file): + config.read(config_file) + return config + + +config = _get_config() + + if __name__ == '__main__': print get_prefix() diff --git a/src/kimchid.in b/src/kimchid.in index 7865713..548fa52 100644 --- a/src/kimchid.in +++ b/src/kimchid.in @@ -33,31 +33,13 @@ import kimchi.config if kimchi.config.without_installation(): sys.path.append(kimchi.config.get_prefix())
-from ConfigParser import SafeConfigParser +from kimchi.config import config from optparse import OptionParser
ACCESS_LOG = "kimchi-access.log" ERROR_LOG = "kimchi-error.log" -CONFIG_FILE = "%s/kimchi.conf" % kimchi.config.get_config_dir() -DEFAULT_LOG_DIR = kimchi.config.get_default_log_dir() -DEFAULT_LOG_LEVEL = "debug"
def main(options): - config = SafeConfigParser() - config.add_section("server") - config.set("server", "host", "0.0.0.0") - config.set("server", "port", "8000") - config.set("server", "ssl_port", "8001") - config.set("server", "ssl_cert", "") - config.set("server", "ssl_key", "") - config.set("server", "environment", "development") - config.add_section("logging") - config.set("logging", "log_dir", DEFAULT_LOG_DIR) - config.set("logging", "log_level", DEFAULT_LOG_LEVEL) - - if os.path.exists(CONFIG_FILE): - config.read(CONFIG_FILE) - host = config.get("server", "host") port = config.get("server", "port") ssl_port = config.get("server", "ssl_port")
participants (3)
-
Aline Manera
-
Mark Wu
-
Royce Lv