From 605614c3188bdbd4e22183aef5211028bfaaabe4 2011-04-24 00:42:02 From: Brian Granger Date: 2011-04-24 00:42:02 Subject: [PATCH] Added tests for IPython.config.application.Application --- diff --git a/IPython/config/application.py b/IPython/config/application.py index 814f111..48e5aa2 100644 --- a/IPython/config/application.py +++ b/IPython/config/application.py @@ -24,7 +24,7 @@ import sys from IPython.config.configurable import SingletonConfigurable from IPython.utils.traitlets import ( - Unicode, List, Int + Unicode, List, Int, Enum ) from IPython.config.loader import ( KeyValueConfigLoader, PyFileConfigLoader @@ -52,7 +52,10 @@ class Application(SingletonConfigurable): # The version string of this application. version = Unicode(u'0.0') - log_level = Int(logging.WARN, config=True, shortname="log_level") + # The log level for the application + log_level = Enum((0,10,20,30,40,50), default_value=logging.WARN, + config=True, shortname="log_level", + help="Set the log level (0,10,20,30,40,50).") def __init__(self, **kwargs): SingletonConfigurable.__init__(self, **kwargs) @@ -105,8 +108,7 @@ class Application(SingletonConfigurable): def parse_command_line(self, argv=None): """Parse the command line arguments.""" - if argv is None: - argv = sys.argv[1:] + argv = sys.argv[1:] if argv is None else argv if '-h' in argv or '--h' in argv: self.print_description() diff --git a/docs/examples/core/appconfig.py b/docs/examples/core/appconfig.py index 7c8954f..f682717 100644 --- a/docs/examples/core/appconfig.py +++ b/docs/examples/core/appconfig.py @@ -1,3 +1,14 @@ +"""A simple example of how to use IPython.config.application.Application. + +This should serve as a simple example that shows how the IPython config +system works. The main classes are: + +* IPython.config.configurable.Configurable +* IPython.config.configurable.SingletonConfigurable +* IPython.config.loader.Config +* IPython.config.application.Application +""" + import sys from IPython.config.configurable import Configurable @@ -15,15 +26,24 @@ class Foo(Configurable): class Bar(Configurable): - enabled = Bool(True, config=True, shortname="bar-enabled", help="Enable bar.") + enabled = Bool(True, config=True, shortname="enabled", help="Enable bar.") class MyApp(Application): app_name = Unicode(u'myapp') - running = Bool(False, config=True, shortname="running", help="Is the app running?") + running = Bool(False, config=True, shortname="running", + help="Is the app running?") classes = List([Bar, Foo]) - config_file = Unicode(u'', config=True, shortname="config-file", help="Load this config file") + config_file = Unicode(u'', config=True, shortname="config_file", + help="Load this config file") + + def init_foo(self): + self.foo = Foo(config=self.config) + + def init_bar(self): + self.bar = Bar(config=self.config) + def main(): @@ -31,6 +51,8 @@ def main(): app.parse_command_line() if app.config_file: app.load_config_file(app.config_file) + app.init_foo() + app.init_bar() print "app.config:" print app.config