Show More
@@ -0,0 +1,90 b'' | |||
|
1 | """ | |
|
2 | Tests for IPython.config.application.Application | |
|
3 | ||
|
4 | Authors: | |
|
5 | ||
|
6 | * Brian Granger | |
|
7 | """ | |
|
8 | ||
|
9 | #----------------------------------------------------------------------------- | |
|
10 | # Copyright (C) 2008-2011 The IPython Development Team | |
|
11 | # | |
|
12 | # Distributed under the terms of the BSD License. The full license is in | |
|
13 | # the file COPYING, distributed as part of this software. | |
|
14 | #----------------------------------------------------------------------------- | |
|
15 | ||
|
16 | #----------------------------------------------------------------------------- | |
|
17 | # Imports | |
|
18 | #----------------------------------------------------------------------------- | |
|
19 | ||
|
20 | from unittest import TestCase | |
|
21 | ||
|
22 | from IPython.config.configurable import Configurable | |
|
23 | ||
|
24 | from IPython.config.application import ( | |
|
25 | Application | |
|
26 | ) | |
|
27 | ||
|
28 | from IPython.utils.traitlets import ( | |
|
29 | Bool, Unicode, Int, Float, List | |
|
30 | ) | |
|
31 | ||
|
32 | #----------------------------------------------------------------------------- | |
|
33 | # Code | |
|
34 | #----------------------------------------------------------------------------- | |
|
35 | ||
|
36 | class Foo(Configurable): | |
|
37 | ||
|
38 | i = Int(0, config=True, shortname='i', help="The integer i.") | |
|
39 | j = Int(1, config=True, shortname='j', help="The integer j.") | |
|
40 | name = Unicode(u'Brian', config=True, shortname='name', help="First name.") | |
|
41 | ||
|
42 | ||
|
43 | class Bar(Configurable): | |
|
44 | ||
|
45 | enabled = Bool(True, config=True, shortname="enabled", help="Enable bar.") | |
|
46 | ||
|
47 | ||
|
48 | class MyApp(Application): | |
|
49 | ||
|
50 | app_name = Unicode(u'myapp') | |
|
51 | running = Bool(False, config=True, shortname="running", | |
|
52 | help="Is the app running?") | |
|
53 | classes = List([Bar, Foo]) | |
|
54 | config_file = Unicode(u'', config=True, shortname="config_file", | |
|
55 | help="Load this config file") | |
|
56 | ||
|
57 | def init_foo(self): | |
|
58 | self.foo = Foo(config=self.config) | |
|
59 | ||
|
60 | def init_bar(self): | |
|
61 | self.bar = Bar(config=self.config) | |
|
62 | ||
|
63 | ||
|
64 | class TestApplication(TestCase): | |
|
65 | ||
|
66 | def test_basic(self): | |
|
67 | app = MyApp() | |
|
68 | self.assertEquals(app.app_name, u'myapp') | |
|
69 | self.assertEquals(app.running, False) | |
|
70 | self.assertEquals(app.classes, [MyApp,Bar,Foo]) | |
|
71 | self.assertEquals(app.config_file, u'') | |
|
72 | ||
|
73 | def test_config(self): | |
|
74 | app = MyApp() | |
|
75 | app.parse_command_line(["i=10","Foo.j=10","enabled=False","log_level=0"]) | |
|
76 | config = app.config | |
|
77 | self.assertEquals(config.Foo.i, 10) | |
|
78 | self.assertEquals(config.Foo.j, 10) | |
|
79 | self.assertEquals(config.Bar.enabled, False) | |
|
80 | self.assertEquals(config.MyApp.log_level,0) | |
|
81 | ||
|
82 | def test_config_propagation(self): | |
|
83 | app = MyApp() | |
|
84 | app.parse_command_line(["i=10","Foo.j=10","enabled=False","log_level=0"]) | |
|
85 | app.init_foo() | |
|
86 | app.init_bar() | |
|
87 | self.assertEquals(app.foo.i, 10) | |
|
88 | self.assertEquals(app.foo.j, 10) | |
|
89 | self.assertEquals(app.bar.enabled, False) | |
|
90 |
@@ -36,6 +36,7 b' from IPython.config.loader import (' | |||
|
36 | 36 | |
|
37 | 37 | |
|
38 | 38 | class Application(SingletonConfigurable): |
|
39 | """A singleton application with full configuration support.""" | |
|
39 | 40 | |
|
40 | 41 | # The name of the application, will usually match the name of the command |
|
41 | 42 | # line application |
@@ -98,6 +99,7 b' class Application(SingletonConfigurable):' | |||
|
98 | 99 | print self.version |
|
99 | 100 | |
|
100 | 101 | def update_config(self, config): |
|
102 | """Fire the traits events when the config is updated.""" | |
|
101 | 103 | # Save a copy of the current config. |
|
102 | 104 | newconfig = deepcopy(self.config) |
|
103 | 105 | # Merge the new config into the current one. |
@@ -7,6 +7,27 b' system works. The main classes are:' | |||
|
7 | 7 | * IPython.config.configurable.SingletonConfigurable |
|
8 | 8 | * IPython.config.loader.Config |
|
9 | 9 | * IPython.config.application.Application |
|
10 | ||
|
11 | To see the command line option help, run this program from the command line:: | |
|
12 | ||
|
13 | $ python appconfig.py -h | |
|
14 | ||
|
15 | To make one of your classes configurable (from the command line and config | |
|
16 | files) inherit from Configurable and declare class attributes as traits (see | |
|
17 | classes Foo and Bar below). To make the traits configurable, you will need | |
|
18 | to set the following options: | |
|
19 | ||
|
20 | * ``config``: set to ``True`` to make the attribute configurable. | |
|
21 | * ``shortname``: by default, configurable attributes are set using the syntax | |
|
22 | "Classname.attributename". At the command line, this is a bit verbose, so | |
|
23 | we allow "shortnames" to be declared. Setting a shortname is optional, but | |
|
24 | when you do this, you can set the option at the command line using the | |
|
25 | syntax: "shortname=value". | |
|
26 | * ``help``: set the help string to display a help message when the ``-h`` | |
|
27 | option is given at the command line. The help string should be valid ReST. | |
|
28 | ||
|
29 | When the config attribute of an Application is updated, it will fire all of | |
|
30 | the trait's events for all of the config=True attributes. | |
|
10 | 31 | """ |
|
11 | 32 | |
|
12 | 33 | import sys |
@@ -17,7 +38,11 b' from IPython.utils.traitlets import (' | |||
|
17 | 38 | Bool, Unicode, Int, Float, List |
|
18 | 39 | ) |
|
19 | 40 | |
|
41 | ||
|
20 | 42 | class Foo(Configurable): |
|
43 | """A class that has configurable, typed attributes. | |
|
44 | ||
|
45 | """ | |
|
21 | 46 | |
|
22 | 47 | i = Int(0, config=True, shortname='i', help="The integer i.") |
|
23 | 48 | j = Int(1, config=True, shortname='j', help="The integer j.") |
@@ -39,9 +64,11 b' class MyApp(Application):' | |||
|
39 | 64 | help="Load this config file") |
|
40 | 65 | |
|
41 | 66 | def init_foo(self): |
|
67 | # Pass config to other classes for them to inherit the config. | |
|
42 | 68 | self.foo = Foo(config=self.config) |
|
43 | 69 | |
|
44 | 70 | def init_bar(self): |
|
71 | # Pass config to other classes for them to inherit the config. | |
|
45 | 72 | self.bar = Bar(config=self.config) |
|
46 | 73 | |
|
47 | 74 |
General Comments 0
You need to be logged in to leave comments.
Login now