Author: tmckay
Date: 2011-01-17 14:55:41 +0000 (Mon, 17 Jan 2011)
New Revision: 4469
Modified:
branches/multiples/cumin/bin/cumin-data
branches/multiples/cumin/bin/cumin-web
branches/multiples/cumin/python/cumin/config.py
Log:
Move preamble logic in cumin-web and cumin-data into classes and a common method
Modified: branches/multiples/cumin/bin/cumin-data
===================================================================
--- branches/multiples/cumin/bin/cumin-data 2011-01-17 13:12:20 UTC (rev 4468)
+++ branches/multiples/cumin/bin/cumin-data 2011-01-17 14:55:41 UTC (rev 4469)
@@ -11,35 +11,8 @@
from mint import *
def main():
- # Configuration boot-strap issue. We need the value of
- # the --section option first to set up defaults for further
- # option parsing. This is complicated by the fact that
- # OptionParser has no way to handle options that it doesn't
- # recognize without throwing an exception. We have to create all options,
- # parse, set defaults based on section, and parse again.
+ config, values, opts = get_configuration(CuminDataOptionParser(), CuminDataConfig)
- # Get the section value first. That's all we care about right now.
- parser = CuminDataOptionParser(default_section="data")
- opts, args = parser.parse_args()
-
- # Now that we have a section name, we have to dynamically create
- # a CuminConfigSection associated with that name to guide the parse.
- config = CuminConfig()
- config.create_data_section(opts.section)
- values = config.parse()
-
- # We need to have the requested section defined in the config files
- if not hasattr(values, opts.section):
- print "Section not defined in configuration files: " + opts.section
- return
- values = getattr(values, opts.section)
-
- # Set defaults for options from the file values for requested section...
- parser.set_defaults_from_section(values)
-
- # And finally parse the options
- opts, args = parser.parse_args()
-
setup_logging(opts)
model_dir = os.path.join(config.home, "model")
Modified: branches/multiples/cumin/bin/cumin-web
===================================================================
--- branches/multiples/cumin/bin/cumin-web 2011-01-17 13:12:20 UTC (rev 4468)
+++ branches/multiples/cumin/bin/cumin-web 2011-01-17 14:55:41 UTC (rev 4469)
@@ -11,40 +11,13 @@
from cumin.util import *
def main():
- # Configuration boot-strap issue. We need the value of
- # the --section option first to set up defaults for further
- # option parsing. This is complicated by the fact that
- # OptionParser has no way to handle options that it doesn't
- # recognize without throwing an exception. We have to create all options,
- # parse, set defaults based on section, and parse again.
+ config, values, opts = get_configuration(CuminWebOptionParser(), CuminWebConfig)
- # Get the section value first. That's all we care about right now.
- parser = CuminWebOptionParser(default_section="web")
- opts, args = parser.parse_args()
-
- # Now that we have a section name, we have to dynamically create
- # a CuminConfigSection associated with that name to guide the parse.
- config = CuminConfig()
- config.create_web_section(opts.section)
- values = config.parse()
-
- # We need to have the requested section defined in the config files
- if not hasattr(values, opts.section):
- print "Section not defined in configuration files: " + opts.section
- return
- values = getattr(values, opts.section)
-
- # Set defaults for options from the file values for requested section...
- parser.set_defaults_from_section(values)
-
- # And finally parse the options
- opts, args = parser.parse_args()
-
setup_logging(opts)
broker_uris = [x.strip() for x in opts.brokers.split(",")]
- cumin = Cumin(config.home, broker_uris, opts.database,
+ cumin = Cumin(config.get_home(), broker_uris, opts.database,
opts.host, opts.port)
cumin.debug = opts.debug
Modified: branches/multiples/cumin/python/cumin/config.py
===================================================================
--- branches/multiples/cumin/python/cumin/config.py 2011-01-17 13:12:20 UTC (rev 4468)
+++ branches/multiples/cumin/python/cumin/config.py 2011-01-17 14:55:41 UTC (rev 4469)
@@ -17,8 +17,25 @@
if not os.path.isdir(self.home):
raise Exception("Home path '%s' is not a directory")
- def create_web_section(self, name):
+ def get_home(self):
+ return self.home
+ def parse(self):
+ paths = list()
+
+ paths.append(os.path.join(os.sep, "etc", "cumin",
"cumin.conf"))
+ paths.append(os.path.join(self.home, "etc", "cumin.conf"))
+ paths.append(os.path.join(os.path.expanduser("~"),
".cumin.conf"))
+
+ return self.parse_files(paths)
+
+class CuminWebConfig(CuminConfig):
+ def __init__(self, section_name):
+ super(CuminWebConfig, self).__init__()
+
+ self.create_section(section_name)
+
+ def create_section(self, name):
web = CuminConfigSection(self, name)
web.log_file.default = os.path.join(self.home, "log", name +
".log")
@@ -44,8 +61,13 @@
param = ConfigParameter(web, "request-disk-vm", int)
param.default = 5 * 1024 #MB
- def create_data_section(self, name):
+class CuminDataConfig(CuminConfig):
+ def __init__(self, section_name):
+ super(CuminDataConfig, self).__init__()
+ self.create_section(section_name)
+
+ def create_section(self, name):
data = CuminConfigSection(self, name)
data.log_file.default = os.path.join(self.home, "log", name +
".log")
@@ -60,15 +82,6 @@
param = ConfigParameter(data, "vacuum-interval", int)
param.default = 60 * 60 # 1 hour
- def parse(self):
- paths = list()
-
- paths.append(os.path.join(os.sep, "etc", "cumin",
"cumin.conf"))
- paths.append(os.path.join(self.home, "etc", "cumin.conf"))
- paths.append(os.path.join(os.path.expanduser("~"),
".cumin.conf"))
-
- return self.parse_files(paths)
-
class CuminConfigSection(ConfigSection):
def __init__(self, config, name):
super(CuminConfigSection, self).__init__(config, name)
@@ -121,7 +134,7 @@
self.set_default("debug", section.debug)
class CuminWebOptionParser(CuminMultiOptionParser):
- def __init__(self, default_section):
+ def __init__(self, default_section="web"):
super(CuminWebOptionParser, self).__init__(default_section)
# Add additional parameters for web
@@ -136,7 +149,7 @@
self.set_default("port", section.port)
class CuminDataOptionParser(CuminMultiOptionParser):
- def __init__(self, default_section):
+ def __init__(self, default_section="data"):
super(CuminDataOptionParser, self).__init__(default_section)
# Add additional parameters for data
@@ -152,3 +165,17 @@
if values.debug:
for name in modules:
enable_logging(name, "debug", sys.stderr)
+
+def get_configuration(parser, ConfigType):
+ # First parse should return a "section" value
+ opts, args = parser.parse_args()
+ if hasattr(opts, "section"):
+ # Create section and parse config files
+ config = ConfigType(opts.section)
+ values = config.parse()
+ if hasattr(values, opts.section):
+ # Reset defaults and parse again
+ values = getattr(values, opts.section)
+ parser.set_defaults_from_section(values)
+ opts, args = parser.parse_args()
+ return config, values, opts