##// END OF EJS Templates
include unminified prettify.js
include unminified prettify.js

File last commit:

r3949:5753a650
r6537:60b2da9c
Show More
appconfig.py
101 lines | 3.2 KiB | text/x-python | PythonLexer
Brian Granger
Added tests for IPython.config.application.Application
r3795 """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
Brian Granger
Adding more documentation to config.application related files.
r3796
To see the command line option help, run this program from the command line::
$ python appconfig.py -h
To make one of your classes configurable (from the command line and config
files) inherit from Configurable and declare class attributes as traits (see
classes Foo and Bar below). To make the traits configurable, you will need
to set the following options:
* ``config``: set to ``True`` to make the attribute configurable.
* ``shortname``: by default, configurable attributes are set using the syntax
"Classname.attributename". At the command line, this is a bit verbose, so
we allow "shortnames" to be declared. Setting a shortname is optional, but
when you do this, you can set the option at the command line using the
syntax: "shortname=value".
* ``help``: set the help string to display a help message when the ``-h``
option is given at the command line. The help string should be valid ReST.
When the config attribute of an Application is updated, it will fire all of
the trait's events for all of the config=True attributes.
Brian Granger
Added tests for IPython.config.application.Application
r3795 """
Brian Granger
Ongoing work on the config system....
r3789 import sys
from IPython.config.configurable import Configurable
Brian Granger
Created config.application and updated Configurable.
r3790 from IPython.config.application import Application
Brian Granger
Ongoing work on the config system....
r3789 from IPython.utils.traitlets import (
MinRK
rename macros/shortnames to flags/aliases...
r3861 Bool, Unicode, Int, Float, List, Dict
Brian Granger
Ongoing work on the config system....
r3789 )
Brian Granger
Adding more documentation to config.application related files.
r3796
Brian Granger
Ongoing work on the config system....
r3789 class Foo(Configurable):
Brian Granger
Adding more documentation to config.application related files.
r3796 """A class that has configurable, typed attributes.
"""
Brian Granger
Ongoing work on the config system....
r3789
MinRK
disable string macros
r3854 i = Int(0, config=True, help="The integer i.")
j = Int(1, config=True, help="The integer j.")
name = Unicode(u'Brian', config=True, help="First name.")
Brian Granger
Ongoing work on the config system....
r3789
class Bar(Configurable):
MinRK
disable string macros
r3854 enabled = Bool(True, config=True, help="Enable bar.")
Brian Granger
Ongoing work on the config system....
r3789
Brian Granger
Created config.application and updated Configurable.
r3790 class MyApp(Application):
Brian Granger
Ongoing work on the config system....
r3789
Brian Granger
Fixing bugs in BaseIPythonApplication....
r3942 name = Unicode(u'myapp')
MinRK
disable string macros
r3854 running = Bool(False, config=True,
Brian Granger
Added tests for IPython.config.application.Application
r3795 help="Is the app running?")
Brian Granger
Ongoing work on the config system....
r3789 classes = List([Bar, Foo])
MinRK
disable string macros
r3854 config_file = Unicode(u'', config=True,
Brian Granger
Added tests for IPython.config.application.Application
r3795 help="Load this config file")
MinRK
move shortname to Application...
r3852
MinRK
rename macros/shortnames to flags/aliases...
r3861 aliases = Dict(dict(i='Foo.i',j='Foo.j',name='Foo.name', running='MyApp.running',
enabled='Bar.enabled', log_level='MyApp.log_level'))
flags = Dict(dict(enable=({'Bar': {'enabled' : True}}, "Enable Bar"),
disable=({'Bar': {'enabled' : False}}, "Disable Bar"),
debug=({'MyApp':{'log_level':10}}, "Set loglevel to DEBUG")
))
MinRK
move shortname to Application...
r3852
Brian Granger
Added tests for IPython.config.application.Application
r3795 def init_foo(self):
Brian Granger
Adding more documentation to config.application related files.
r3796 # Pass config to other classes for them to inherit the config.
Brian Granger
Added tests for IPython.config.application.Application
r3795 self.foo = Foo(config=self.config)
def init_bar(self):
Brian Granger
Adding more documentation to config.application related files.
r3796 # Pass config to other classes for them to inherit the config.
Brian Granger
Added tests for IPython.config.application.Application
r3795 self.bar = Bar(config=self.config)
MinRK
add subcommand support
r3949 def initialize(self, argv=None):
self.parse_command_line(argv)
if self.config_file:
self.load_config_file(self.config_file)
self.init_foo()
self.init_bar()
def start(self):
print "app.config:"
print self.config
Brian Granger
Ongoing work on the config system....
r3789
def main():
app = MyApp()
MinRK
add subcommand support
r3949 app.initialize()
app.start()
Brian Granger
Ongoing work on the config system....
r3789
if __name__ == "__main__":
main()