Show More
@@ -1,108 +1,130 b'' | |||||
1 | # encoding: utf-8 |
|
1 | # encoding: utf-8 | |
2 | """ |
|
2 | """ | |
3 | A base class for a configurable application. |
|
3 | A base class for a configurable application. | |
4 |
|
4 | |||
5 | Authors: |
|
5 | Authors: | |
6 |
|
6 | |||
7 | * Brian Granger |
|
7 | * Brian Granger | |
8 | """ |
|
8 | """ | |
9 |
|
9 | |||
10 | #----------------------------------------------------------------------------- |
|
10 | #----------------------------------------------------------------------------- | |
11 | # Copyright (C) 2008-2011 The IPython Development Team |
|
11 | # Copyright (C) 2008-2011 The IPython Development Team | |
12 | # |
|
12 | # | |
13 | # Distributed under the terms of the BSD License. The full license is in |
|
13 | # Distributed under the terms of the BSD License. The full license is in | |
14 | # the file COPYING, distributed as part of this software. |
|
14 | # the file COPYING, distributed as part of this software. | |
15 | #----------------------------------------------------------------------------- |
|
15 | #----------------------------------------------------------------------------- | |
16 |
|
16 | |||
17 | #----------------------------------------------------------------------------- |
|
17 | #----------------------------------------------------------------------------- | |
18 | # Imports |
|
18 | # Imports | |
19 | #----------------------------------------------------------------------------- |
|
19 | #----------------------------------------------------------------------------- | |
20 |
|
20 | |||
21 | from copy import deepcopy |
|
21 | from copy import deepcopy | |
|
22 | import logging | |||
22 | import sys |
|
23 | import sys | |
23 |
|
24 | |||
24 | from IPython.config.configurable import Configurable |
|
25 | from IPython.config.configurable import SingletonConfigurable | |
25 | from IPython.utils.traitlets import ( |
|
26 | from IPython.utils.traitlets import ( | |
26 | Unicode, List |
|
27 | Unicode, List, Int | |
27 | ) |
|
28 | ) | |
28 | from IPython.config.loader import ( |
|
29 | from IPython.config.loader import ( | |
29 | KeyValueConfigLoader, PyFileConfigLoader |
|
30 | KeyValueConfigLoader, PyFileConfigLoader | |
30 | ) |
|
31 | ) | |
31 |
|
32 | |||
32 | #----------------------------------------------------------------------------- |
|
33 | #----------------------------------------------------------------------------- | |
33 | # Application class |
|
34 | # Application class | |
34 | #----------------------------------------------------------------------------- |
|
35 | #----------------------------------------------------------------------------- | |
35 |
|
36 | |||
36 |
|
37 | |||
37 | class Application(Configurable): |
|
38 | class Application(SingletonConfigurable): | |
38 |
|
39 | |||
39 | # The name of the application, will usually match the name of the command |
|
40 | # The name of the application, will usually match the name of the command | |
40 | # line application |
|
41 | # line application | |
41 | app_name = Unicode(u'application') |
|
42 | app_name = Unicode(u'application') | |
42 |
|
43 | |||
43 | # The description of the application that is printed at the beginning |
|
44 | # The description of the application that is printed at the beginning | |
44 | # of the help. |
|
45 | # of the help. | |
45 | description = Unicode(u'This is an application.') |
|
46 | description = Unicode(u'This is an application.') | |
46 |
|
47 | |||
47 | # A sequence of Configurable subclasses whose config=True attributes will |
|
48 | # A sequence of Configurable subclasses whose config=True attributes will | |
48 | # be exposed at the command line (shortnames and help). |
|
49 | # be exposed at the command line (shortnames and help). | |
49 | classes = List([]) |
|
50 | classes = List([]) | |
50 |
|
51 | |||
51 | # The version string of this application. |
|
52 | # The version string of this application. | |
52 | version = Unicode(u'0.0') |
|
53 | version = Unicode(u'0.0') | |
53 |
|
54 | |||
|
55 | log_level = Int(logging.WARN, config=True, shortname="log_level") | |||
|
56 | ||||
54 | def __init__(self, **kwargs): |
|
57 | def __init__(self, **kwargs): | |
55 | Configurable.__init__(self, **kwargs) |
|
58 | SingletonConfigurable.__init__(self, **kwargs) | |
56 | # Add my class to self.classes so my attributes appear in command line |
|
59 | # Add my class to self.classes so my attributes appear in command line | |
57 | # options. |
|
60 | # options. | |
58 | self.classes.insert(0, self.__class__) |
|
61 | self.classes.insert(0, self.__class__) | |
|
62 | self.init_logging() | |||
|
63 | ||||
|
64 | def init_logging(self): | |||
|
65 | """Start logging for this application. | |||
|
66 | ||||
|
67 | The default is to log to stdout using a StreaHandler. The log level | |||
|
68 | starts at loggin.WARN, but this can be adjusted by setting the | |||
|
69 | ``log_level`` attribute. | |||
|
70 | """ | |||
|
71 | self.log = logging.getLogger(self.__class__.__name__) | |||
|
72 | self.log.setLevel(self.log_level) | |||
|
73 | self._log_handler = logging.StreamHandler() | |||
|
74 | self._log_formatter = logging.Formatter("[%(name)s] %(message)s") | |||
|
75 | self._log_handler.setFormatter(self._log_formatter) | |||
|
76 | self.log.addHandler(self._log_handler) | |||
|
77 | ||||
|
78 | def _log_level_changed(self, name, old, new): | |||
|
79 | """Adjust the log level when log_level is set.""" | |||
|
80 | self.log.setLevel(new) | |||
59 |
|
81 | |||
60 | def print_help(self): |
|
82 | def print_help(self): | |
61 | """Print the help for each Configurable class in self.classes.""" |
|
83 | """Print the help for each Configurable class in self.classes.""" | |
62 | for cls in self.classes: |
|
84 | for cls in self.classes: | |
63 | cls.class_print_help() |
|
85 | cls.class_print_help() | |
64 |
|
86 | |||
65 |
|
87 | |||
66 | def print_description(self): |
|
88 | def print_description(self): | |
67 | """Print the application description.""" |
|
89 | """Print the application description.""" | |
68 | print self.description |
|
90 | print self.description | |
69 |
|
91 | |||
70 |
|
92 | |||
71 | def print_version(self): |
|
93 | def print_version(self): | |
72 | """Print the version string.""" |
|
94 | """Print the version string.""" | |
73 | print self.version |
|
95 | print self.version | |
74 |
|
96 | |||
75 | def update_config(self, config): |
|
97 | def update_config(self, config): | |
76 | # Save a copy of the current config. |
|
98 | # Save a copy of the current config. | |
77 | newconfig = deepcopy(self.config) |
|
99 | newconfig = deepcopy(self.config) | |
78 | # Merge the new config into the current one. |
|
100 | # Merge the new config into the current one. | |
79 | newconfig._merge(config) |
|
101 | newconfig._merge(config) | |
80 | # Save the combined config as self.config, which triggers the traits |
|
102 | # Save the combined config as self.config, which triggers the traits | |
81 | # events. |
|
103 | # events. | |
82 | self.config = config |
|
104 | self.config = config | |
83 |
|
105 | |||
84 | def parse_command_line(self, argv=None): |
|
106 | def parse_command_line(self, argv=None): | |
85 | """Parse the command line arguments.""" |
|
107 | """Parse the command line arguments.""" | |
86 | if argv is None: |
|
108 | if argv is None: | |
87 | argv = sys.argv[1:] |
|
109 | argv = sys.argv[1:] | |
88 |
|
110 | |||
89 | if '-h' in argv or '--h' in argv: |
|
111 | if '-h' in argv or '--h' in argv: | |
90 | self.print_description() |
|
112 | self.print_description() | |
91 | self.print_help() |
|
113 | self.print_help() | |
92 | sys.exit(1) |
|
114 | sys.exit(1) | |
93 |
|
115 | |||
94 | if '--version' in argv: |
|
116 | if '--version' in argv: | |
95 | self.print_version() |
|
117 | self.print_version() | |
96 | sys.exit(1) |
|
118 | sys.exit(1) | |
97 |
|
119 | |||
98 | loader = KeyValueConfigLoader(argv=argv, classes=self.classes) |
|
120 | loader = KeyValueConfigLoader(argv=argv, classes=self.classes) | |
99 | config = loader.load_config() |
|
121 | config = loader.load_config() | |
100 | self.update_config(config) |
|
122 | self.update_config(config) | |
101 |
|
123 | |||
102 | def load_config_file(self, filename, path=None): |
|
124 | def load_config_file(self, filename, path=None): | |
103 | """Load a .py based config file by filename and path.""" |
|
125 | """Load a .py based config file by filename and path.""" | |
104 | # TODO: this raises IOError if filename does not exist. |
|
126 | # TODO: this raises IOError if filename does not exist. | |
105 | loader = PyFileConfigLoader(filename, path=path) |
|
127 | loader = PyFileConfigLoader(filename, path=path) | |
106 | config = loader.load_config() |
|
128 | config = loader.load_config() | |
107 | self.update_config(config) |
|
129 | self.update_config(config) | |
108 |
|
130 |
General Comments 0
You need to be logged in to leave comments.
Login now