Show More
@@ -28,8 +28,9 b' from IPython.config.loader import (' | |||
|
28 | 28 | ) |
|
29 | 29 | |
|
30 | 30 | from IPython.utils.traitlets import ( |
|
31 | Unicode, List, Int, Enum, Dict | |
|
31 | Unicode, List, Int, Enum, Dict, Instance | |
|
32 | 32 | ) |
|
33 | from IPython.utils.importstring import import_item | |
|
33 | 34 | from IPython.utils.text import indent |
|
34 | 35 | |
|
35 | 36 | #----------------------------------------------------------------------------- |
@@ -102,6 +103,14 b' class Application(SingletonConfigurable):' | |||
|
102 | 103 | # and the second being the help string for the flag |
|
103 | 104 | flags = Dict() |
|
104 | 105 | |
|
106 | # subcommands for launching other applications | |
|
107 | # if this is not empty, this will be a parent Application | |
|
108 | # this must be a dict of two-tuples, the first element being the application class/import string | |
|
109 | # and the second being the help string for the subcommand | |
|
110 | subcommands = Dict() | |
|
111 | # parse_command_line will initialize a subapp, if requested | |
|
112 | subapp = Instance('IPython.config.application.Application', allow_none=True) | |
|
113 | ||
|
105 | 114 | |
|
106 | 115 | def __init__(self, **kwargs): |
|
107 | 116 | SingletonConfigurable.__init__(self, **kwargs) |
@@ -134,7 +143,23 b' class Application(SingletonConfigurable):' | |||
|
134 | 143 | self._log_formatter = logging.Formatter("[%(name)s] %(message)s") |
|
135 | 144 | self._log_handler.setFormatter(self._log_formatter) |
|
136 | 145 | self.log.addHandler(self._log_handler) |
|
137 | ||
|
146 | ||
|
147 | def initialize(self, argv=None): | |
|
148 | """Do the basic steps to configure me. | |
|
149 | ||
|
150 | Override in subclasses. | |
|
151 | """ | |
|
152 | self.parse_command_line(argv) | |
|
153 | ||
|
154 | ||
|
155 | def start(self): | |
|
156 | """Start the app mainloop. | |
|
157 | ||
|
158 | Override in subclasses. | |
|
159 | """ | |
|
160 | if self.subapp is not None: | |
|
161 | return self.subapp.start() | |
|
162 | ||
|
138 | 163 | def _log_level_changed(self, name, old, new): |
|
139 | 164 | """Adjust the log level when log_level is set.""" |
|
140 | 165 | self.log.setLevel(new) |
@@ -145,7 +170,7 b' class Application(SingletonConfigurable):' | |||
|
145 | 170 | return |
|
146 | 171 | |
|
147 | 172 | lines = ['Aliases'] |
|
148 |
lines.append(' |
|
|
173 | lines.append('-'*len(lines[0])) | |
|
149 | 174 | lines.append(self.alias_description) |
|
150 | 175 | lines.append('') |
|
151 | 176 | |
@@ -161,11 +186,6 b' class Application(SingletonConfigurable):' | |||
|
161 | 186 | help = cls.class_get_trait_help(trait) |
|
162 | 187 | help = help.replace(longname, "%s (%s)"%(alias, longname), 1) |
|
163 | 188 | lines.append(help) |
|
164 | # header = "%s (%s) : %s"%(alias, longname, trait.__class__.__name__) | |
|
165 | # lines.append(header) | |
|
166 | # help = cls.class_get_trait_help(trait) | |
|
167 | # if help: | |
|
168 | # lines.append(indent(help, flatten=True)) | |
|
169 | 189 | lines.append('') |
|
170 | 190 | print '\n'.join(lines) |
|
171 | 191 | |
@@ -175,7 +195,7 b' class Application(SingletonConfigurable):' | |||
|
175 | 195 | return |
|
176 | 196 | |
|
177 | 197 | lines = ['Flags'] |
|
178 |
lines.append(' |
|
|
198 | lines.append('-'*len(lines[0])) | |
|
179 | 199 | lines.append(self.flag_description) |
|
180 | 200 | lines.append('') |
|
181 | 201 | |
@@ -185,6 +205,20 b' class Application(SingletonConfigurable):' | |||
|
185 | 205 | lines.append('') |
|
186 | 206 | print '\n'.join(lines) |
|
187 | 207 | |
|
208 | def print_subcommands(self): | |
|
209 | """print the subcommand part of the help""" | |
|
210 | if not self.subcommands: | |
|
211 | return | |
|
212 | ||
|
213 | lines = ["Subcommands"] | |
|
214 | lines.append('-'*len(lines[0])) | |
|
215 | for subc, cls,help in self.subcommands: | |
|
216 | lines.append("%s : %s"%(subc, cls)) | |
|
217 | if help: | |
|
218 | lines.append(indent(help, flatten=True)) | |
|
219 | lines.append('') | |
|
220 | print '\n'.join(lines) | |
|
221 | ||
|
188 | 222 | def print_help(self, classes=False): |
|
189 | 223 | """Print the help for each Configurable class in self.classes. |
|
190 | 224 | |
@@ -225,11 +259,44 b' class Application(SingletonConfigurable):' | |||
|
225 | 259 | # Save the combined config as self.config, which triggers the traits |
|
226 | 260 | # events. |
|
227 | 261 | self.config = newconfig |
|
228 | ||
|
262 | ||
|
263 | def initialize_subcommand(self, subc, argv=None): | |
|
264 | """Initialize a subcommand with argv""" | |
|
265 | if '-h' in subc: | |
|
266 | # requested help | |
|
267 | self.print_description() | |
|
268 | self.print_subcommands() | |
|
269 | self.exit(0) | |
|
270 | subapp = self.subcommands.get(subc, None) | |
|
271 | if subapp is None: | |
|
272 | self.print_description() | |
|
273 | print "No such subcommand: %r"%subc | |
|
274 | ||
|
275 | self.print_subcommands() | |
|
276 | self.exit(1) | |
|
277 | ||
|
278 | if isinstance(subapp, basestring): | |
|
279 | subapp = import_item(subapp) | |
|
280 | ||
|
281 | # instantiate | |
|
282 | self.subapp = subapp() | |
|
283 | # and initialize subapp | |
|
284 | self.subapp.initialize(argv) | |
|
285 | ||
|
229 | 286 | def parse_command_line(self, argv=None): |
|
230 | 287 | """Parse the command line arguments.""" |
|
231 | 288 | argv = sys.argv[1:] if argv is None else argv |
|
232 | 289 | |
|
290 | if self.subcommands: | |
|
291 | # we have subcommands | |
|
292 | if len(argv) == 0: | |
|
293 | # none specified | |
|
294 | self.print_description() | |
|
295 | self.print_subcommands() | |
|
296 | self.exit(1) | |
|
297 | ||
|
298 | return self.initialize_subcommand(argv[0], argv[1:]) | |
|
299 | ||
|
233 | 300 | if '-h' in argv or '--help' in argv or '--help-all' in argv: |
|
234 | 301 | self.print_description() |
|
235 | 302 | self.print_help('--help-all' in argv) |
@@ -79,17 +79,22 b' class MyApp(Application):' | |||
|
79 | 79 | # Pass config to other classes for them to inherit the config. |
|
80 | 80 | self.bar = Bar(config=self.config) |
|
81 | 81 | |
|
82 | def initialize(self, argv=None): | |
|
83 | self.parse_command_line(argv) | |
|
84 | if self.config_file: | |
|
85 | self.load_config_file(self.config_file) | |
|
86 | self.init_foo() | |
|
87 | self.init_bar() | |
|
88 | ||
|
89 | def start(self): | |
|
90 | print "app.config:" | |
|
91 | print self.config | |
|
82 | 92 | |
|
83 | 93 | |
|
84 | 94 | def main(): |
|
85 | 95 | app = MyApp() |
|
86 | app.parse_command_line() | |
|
87 | if app.config_file: | |
|
88 | app.load_config_file(app.config_file) | |
|
89 | app.init_foo() | |
|
90 | app.init_bar() | |
|
91 | print "app.config:" | |
|
92 | print app.config | |
|
96 | app.initialize() | |
|
97 | app.start() | |
|
93 | 98 | |
|
94 | 99 | |
|
95 | 100 | if __name__ == "__main__": |
General Comments 0
You need to be logged in to leave comments.
Login now