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