Show More
@@ -1,115 +1,146 b'' | |||||
1 | #----------------------------------------------------------------------------- |
|
1 | #----------------------------------------------------------------------------- | |
2 | # Global options |
|
2 | # Global options | |
3 | #----------------------------------------------------------------------------- |
|
3 | #----------------------------------------------------------------------------- | |
4 |
|
4 | |||
5 | Global.classic = False |
|
5 | # Global.display_banner = True | |
6 | Global.nosep = False |
|
6 | ||
|
7 | # Global.classic = False | |||
|
8 | ||||
|
9 | # Global.nosep = True | |||
|
10 | ||||
|
11 | # Set this to determine the detail of what is logged at startup. | |||
|
12 | # The default is 30 and possible values are 0,10,20,30,40,50. | |||
|
13 | # Global.log_level = 30 | |||
|
14 | ||||
|
15 | # This should be a list of importable Python modules that have an | |||
|
16 | # load_in_ipython(ip) method. This method gets called when the extension | |||
|
17 | # is loaded. You can put your extensions anywhere they can be imported | |||
|
18 | # but we add the extensions subdir of the ipython directory to sys.path | |||
|
19 | # during extension loading, so you can put them there as well. | |||
|
20 | # Global.extensions = [ | |||
|
21 | # 'myextension' | |||
|
22 | # ] | |||
|
23 | ||||
|
24 | # These lines are run in IPython in the user's namespace after extensions | |||
|
25 | # are loaded. They can contain full IPython syntax with magics etc. | |||
|
26 | # Global.exec_lines = [ | |||
|
27 | # 'import numpy', | |||
|
28 | # 'a = 10; b = 20', | |||
|
29 | # '1/0' | |||
|
30 | # ] | |||
|
31 | ||||
|
32 | # These files are run in IPython in the user's namespace. Files with a .py | |||
|
33 | # extension need to be pure Python. Files with a .ipy extension can have | |||
|
34 | # custom IPython syntax (like magics, etc.). | |||
|
35 | # These files need to be in the cwd, the ipythondir or be absolute paths. | |||
|
36 | # Global.exec_files = [ | |||
|
37 | # 'mycode.py', | |||
|
38 | # 'fancy.ipy' | |||
|
39 | # ] | |||
7 |
|
40 | |||
8 | #----------------------------------------------------------------------------- |
|
41 | #----------------------------------------------------------------------------- | |
9 | # InteractiveShell options |
|
42 | # InteractiveShell options | |
10 | #----------------------------------------------------------------------------- |
|
43 | #----------------------------------------------------------------------------- | |
11 |
|
44 | |||
12 |
|
45 | |||
13 | InteractiveShell.autocall = 1 |
|
46 | # InteractiveShell.autocall = 1 | |
14 |
|
47 | |||
15 | InteractiveShell.autoedit_syntax = False |
|
48 | # InteractiveShell.autoedit_syntax = False | |
16 |
|
49 | |||
17 | InteractiveShell.autoindent = True |
|
50 | # InteractiveShell.autoindent = True | |
18 |
|
51 | |||
19 | InteractiveShell.automagic = False |
|
52 | # InteractiveShell.automagic = False | |
20 |
|
53 | |||
21 | InteractiveShell.banner1 = 'This if for overriding the default IPython banner' |
|
54 | # InteractiveShell.banner1 = 'This if for overriding the default IPython banner' | |
22 |
|
55 | |||
23 | InteractiveShell.banner2 = "This is for extra banner text" |
|
56 | # InteractiveShell.banner2 = "This is for extra banner text" | |
24 |
|
57 | |||
25 | InteractiveShell.cache_size = 1000 |
|
58 | # InteractiveShell.cache_size = 1000 | |
26 |
|
59 | |||
27 | InteractiveShell.colors = 'LightBG' |
|
60 | # InteractiveShell.colors = 'LightBG' | |
28 |
|
61 | |||
29 | InteractiveShell.color_info = True |
|
62 | # InteractiveShell.color_info = True | |
30 |
|
63 | |||
31 | InteractiveShell.confirm_exit = True |
|
64 | # InteractiveShell.confirm_exit = True | |
32 |
|
65 | |||
33 | InteractiveShell.deep_reload = False |
|
66 | # InteractiveShell.deep_reload = False | |
34 |
|
67 | |||
35 |
InteractiveShell. |
|
68 | # InteractiveShell.editor = 'nano' | |
36 |
|
69 | |||
37 |
InteractiveShell. |
|
70 | # InteractiveShell.logstart = True | |
38 |
|
71 | |||
39 | InteractiveShell.logstart = True |
|
72 | # InteractiveShell.logfile = 'ipython_log.py' | |
40 |
|
73 | |||
41 |
InteractiveShell.log |
|
74 | # InteractiveShell.logplay = 'mylog.py' | |
42 |
|
75 | |||
43 | InteractiveShell.logplay = 'mylog.py' |
|
76 | # InteractiveShell.object_info_string_level = 0 | |
44 |
|
77 | |||
45 | InteractiveShell.object_info_string_level = 0 |
|
78 | # InteractiveShell.pager = 'less' | |
46 |
|
79 | |||
47 |
InteractiveShell.p |
|
80 | # InteractiveShell.pdb = False | |
48 |
|
81 | |||
49 |
InteractiveShell.p |
|
82 | # InteractiveShell.pprint = True | |
50 |
|
83 | |||
51 |
InteractiveShell.p |
|
84 | # InteractiveShell.prompt_in1 = 'In [\#]: ' | |
|
85 | # InteractiveShell.prompt_in2 = ' .\D.: ' | |||
|
86 | # InteractiveShell.prompt_out = 'Out[\#]: ' | |||
|
87 | # InteractiveShell.prompts_pad_left = True | |||
52 |
|
88 | |||
53 | InteractiveShell.prompt_in1 = 'In [\#]: ' |
|
89 | # InteractiveShell.quiet = False | |
54 | InteractiveShell.prompt_in2 = ' .\D.: ' |
|
|||
55 | InteractiveShell.prompt_out = 'Out[\#]: ' |
|
|||
56 | InteractiveShell.prompts_pad_left = True |
|
|||
57 |
|
||||
58 | InteractiveShell.quiet = False |
|
|||
59 |
|
90 | |||
60 | # Readline |
|
91 | # Readline | |
61 |
InteractiveShell.readline_use = |
|
92 | # InteractiveShell.readline_use = True | |
62 |
|
93 | |||
63 | InteractiveShell.readline_parse_and_bind = [ |
|
94 | # InteractiveShell.readline_parse_and_bind = [ | |
64 | 'tab: complete', |
|
95 | # 'tab: complete', | |
65 | '"\C-l": possible-completions', |
|
96 | # '"\C-l": possible-completions', | |
66 | 'set show-all-if-ambiguous on', |
|
97 | # 'set show-all-if-ambiguous on', | |
67 | '"\C-o": tab-insert', |
|
98 | # '"\C-o": tab-insert', | |
68 | '"\M-i": " "', |
|
99 | # '"\M-i": " "', | |
69 | '"\M-o": "\d\d\d\d"', |
|
100 | # '"\M-o": "\d\d\d\d"', | |
70 | '"\M-I": "\d\d\d\d"', |
|
101 | # '"\M-I": "\d\d\d\d"', | |
71 | '"\C-r": reverse-search-history', |
|
102 | # '"\C-r": reverse-search-history', | |
72 | '"\C-s": forward-search-history', |
|
103 | # '"\C-s": forward-search-history', | |
73 | '"\C-p": history-search-backward', |
|
104 | # '"\C-p": history-search-backward', | |
74 | '"\C-n": history-search-forward', |
|
105 | # '"\C-n": history-search-forward', | |
75 | '"\e[A": history-search-backward', |
|
106 | # '"\e[A": history-search-backward', | |
76 | '"\e[B": history-search-forward', |
|
107 | # '"\e[B": history-search-forward', | |
77 | '"\C-k": kill-line', |
|
108 | # '"\C-k": kill-line', | |
78 | '"\C-u": unix-line-discard', |
|
109 | # '"\C-u": unix-line-discard', | |
79 | ] |
|
110 | # ] | |
80 | InteractiveShell.readline_remove_delims = '-/~' |
|
111 | # InteractiveShell.readline_remove_delims = '-/~' | |
81 | InteractiveShell.readline_merge_completions = True |
|
112 | # InteractiveShell.readline_merge_completions = True | |
82 | InteractiveShell.readline_omit_names = 0 |
|
113 | # InteractiveShell.readline_omit_names = 0 | |
83 |
|
114 | |||
84 | InteractiveShell.screen_length = 0 |
|
115 | # InteractiveShell.screen_length = 0 | |
85 |
|
116 | |||
86 | InteractiveShell.separate_in = '\n' |
|
117 | # InteractiveShell.separate_in = '\n' | |
87 | InteractiveShell.separate_out = '' |
|
118 | # InteractiveShell.separate_out = '' | |
88 | InteractiveShell.separate_out2 = '' |
|
119 | # InteractiveShell.separate_out2 = '' | |
89 |
|
120 | |||
90 | InteractiveShell.system_header = "IPython system call: " |
|
121 | # InteractiveShell.system_header = "IPython system call: " | |
91 |
|
122 | |||
92 | InteractiveShell.system_verbose = True |
|
123 | # InteractiveShell.system_verbose = True | |
93 |
|
124 | |||
94 | InteractiveShell.term_title = False |
|
125 | # InteractiveShell.term_title = False | |
95 |
|
126 | |||
96 | InteractiveShell.wildcards_case_sensitive = True |
|
127 | # InteractiveShell.wildcards_case_sensitive = True | |
97 |
|
128 | |||
98 | InteractiveShell.xmode = 'Context' |
|
129 | # InteractiveShell.xmode = 'Context' | |
99 |
|
130 | |||
100 | #----------------------------------------------------------------------------- |
|
131 | #----------------------------------------------------------------------------- | |
101 | # PrefilterManager options |
|
132 | # PrefilterManager options | |
102 | #----------------------------------------------------------------------------- |
|
133 | #----------------------------------------------------------------------------- | |
103 |
|
134 | |||
104 | PrefilterManager.multi_line_specials = True |
|
135 | # PrefilterManager.multi_line_specials = True | |
105 |
|
136 | |||
106 | #----------------------------------------------------------------------------- |
|
137 | #----------------------------------------------------------------------------- | |
107 | # AliasManager options |
|
138 | # AliasManager options | |
108 | #----------------------------------------------------------------------------- |
|
139 | #----------------------------------------------------------------------------- | |
109 |
|
140 | |||
110 | # Do this to enable all defaults |
|
141 | # Do this to enable all defaults | |
111 | # AliasManager.default_aliases = [] |
|
142 | # AliasManager.default_aliases = [] | |
112 |
|
143 | |||
113 | AliasManger.user_aliases = [ |
|
144 | # AliasManger.user_aliases = [ | |
114 | ('foo', 'echo Hi') |
|
145 | # ('foo', 'echo Hi') | |
115 | ] No newline at end of file |
|
146 | # ] No newline at end of file |
@@ -1,311 +1,328 b'' | |||||
1 | #!/usr/bin/env python |
|
1 | #!/usr/bin/env python | |
2 | # encoding: utf-8 |
|
2 | # encoding: utf-8 | |
3 | """A simple configuration system. |
|
3 | """A simple configuration system. | |
4 |
|
4 | |||
5 | Authors: |
|
5 | Authors: | |
6 |
|
6 | |||
7 | * Brian Granger |
|
7 | * Brian Granger | |
8 | """ |
|
8 | """ | |
9 |
|
9 | |||
10 | #----------------------------------------------------------------------------- |
|
10 | #----------------------------------------------------------------------------- | |
11 | # Copyright (C) 2008-2009 The IPython Development Team |
|
11 | # Copyright (C) 2008-2009 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 | import __builtin__ |
|
21 | import __builtin__ | |
22 | import os |
|
22 | import os | |
23 | import sys |
|
23 | import sys | |
24 |
|
24 | |||
25 | from IPython.external import argparse |
|
25 | from IPython.external import argparse | |
26 | from IPython.utils.genutils import filefind |
|
26 | from IPython.utils.genutils import filefind | |
27 |
|
27 | |||
28 | #----------------------------------------------------------------------------- |
|
28 | #----------------------------------------------------------------------------- | |
29 | # Exceptions |
|
29 | # Exceptions | |
30 | #----------------------------------------------------------------------------- |
|
30 | #----------------------------------------------------------------------------- | |
31 |
|
31 | |||
32 |
|
32 | |||
33 | class ConfigError(Exception): |
|
33 | class ConfigError(Exception): | |
34 | pass |
|
34 | pass | |
35 |
|
35 | |||
36 |
|
36 | |||
37 | class ConfigLoaderError(ConfigError): |
|
37 | class ConfigLoaderError(ConfigError): | |
38 | pass |
|
38 | pass | |
39 |
|
39 | |||
40 |
|
40 | |||
41 | #----------------------------------------------------------------------------- |
|
41 | #----------------------------------------------------------------------------- | |
42 | # Config class for holding config information |
|
42 | # Config class for holding config information | |
43 | #----------------------------------------------------------------------------- |
|
43 | #----------------------------------------------------------------------------- | |
44 |
|
44 | |||
45 |
|
45 | |||
46 | class Config(dict): |
|
46 | class Config(dict): | |
47 | """An attribute based dict that can do smart merges.""" |
|
47 | """An attribute based dict that can do smart merges.""" | |
48 |
|
48 | |||
49 | def __init__(self, *args, **kwds): |
|
49 | def __init__(self, *args, **kwds): | |
50 | dict.__init__(self, *args, **kwds) |
|
50 | dict.__init__(self, *args, **kwds) | |
51 | # This sets self.__dict__ = self, but it has to be done this way |
|
51 | # This sets self.__dict__ = self, but it has to be done this way | |
52 | # because we are also overriding __setattr__. |
|
52 | # because we are also overriding __setattr__. | |
53 | dict.__setattr__(self, '__dict__', self) |
|
53 | dict.__setattr__(self, '__dict__', self) | |
54 |
|
54 | |||
55 | def _merge(self, other): |
|
55 | def _merge(self, other): | |
56 | to_update = {} |
|
56 | to_update = {} | |
57 | for k, v in other.items(): |
|
57 | for k, v in other.items(): | |
58 | if not self.has_key(k): |
|
58 | if not self.has_key(k): | |
59 | to_update[k] = v |
|
59 | to_update[k] = v | |
60 | else: # I have this key |
|
60 | else: # I have this key | |
61 | if isinstance(v, Config): |
|
61 | if isinstance(v, Config): | |
62 | # Recursively merge common sub Configs |
|
62 | # Recursively merge common sub Configs | |
63 | self[k]._merge(v) |
|
63 | self[k]._merge(v) | |
64 | else: |
|
64 | else: | |
65 | # Plain updates for non-Configs |
|
65 | # Plain updates for non-Configs | |
66 | to_update[k] = v |
|
66 | to_update[k] = v | |
67 |
|
67 | |||
68 | self.update(to_update) |
|
68 | self.update(to_update) | |
69 |
|
69 | |||
70 | def _is_section_key(self, key): |
|
70 | def _is_section_key(self, key): | |
71 | if key[0].upper()==key[0] and not key.startswith('_'): |
|
71 | if key[0].upper()==key[0] and not key.startswith('_'): | |
72 | return True |
|
72 | return True | |
73 | else: |
|
73 | else: | |
74 | return False |
|
74 | return False | |
75 |
|
75 | |||
76 | def has_key(self, key): |
|
76 | def has_key(self, key): | |
77 | if self._is_section_key(key): |
|
77 | if self._is_section_key(key): | |
78 | return True |
|
78 | return True | |
79 | else: |
|
79 | else: | |
80 | return dict.has_key(self, key) |
|
80 | return dict.has_key(self, key) | |
81 |
|
81 | |||
82 | def _has_section(self, key): |
|
82 | def _has_section(self, key): | |
83 | if self._is_section_key(key): |
|
83 | if self._is_section_key(key): | |
84 | if dict.has_key(self, key): |
|
84 | if dict.has_key(self, key): | |
85 | return True |
|
85 | return True | |
86 | return False |
|
86 | return False | |
87 |
|
87 | |||
88 | def copy(self): |
|
88 | def copy(self): | |
89 | return type(self)(dict.copy(self)) |
|
89 | return type(self)(dict.copy(self)) | |
90 |
|
90 | |||
91 | def __copy__(self): |
|
91 | def __copy__(self): | |
92 | return self.copy() |
|
92 | return self.copy() | |
93 |
|
93 | |||
94 | def __deepcopy__(self, memo): |
|
94 | def __deepcopy__(self, memo): | |
95 | import copy |
|
95 | import copy | |
96 | return type(self)(copy.deepcopy(self.items())) |
|
96 | return type(self)(copy.deepcopy(self.items())) | |
97 |
|
97 | |||
98 | def __getitem__(self, key): |
|
98 | def __getitem__(self, key): | |
99 | # Because we use this for an exec namespace, we need to delegate |
|
99 | # Because we use this for an exec namespace, we need to delegate | |
100 | # the lookup of names in __builtin__ to itself. This means |
|
100 | # the lookup of names in __builtin__ to itself. This means | |
101 | # that you can't have section or attribute names that are |
|
101 | # that you can't have section or attribute names that are | |
102 | # builtins. |
|
102 | # builtins. | |
103 | try: |
|
103 | try: | |
104 | return getattr(__builtin__, key) |
|
104 | return getattr(__builtin__, key) | |
105 | except AttributeError: |
|
105 | except AttributeError: | |
106 | pass |
|
106 | pass | |
107 | if self._is_section_key(key): |
|
107 | if self._is_section_key(key): | |
108 | try: |
|
108 | try: | |
109 | return dict.__getitem__(self, key) |
|
109 | return dict.__getitem__(self, key) | |
110 | except KeyError: |
|
110 | except KeyError: | |
111 | c = Config() |
|
111 | c = Config() | |
112 | dict.__setitem__(self, key, c) |
|
112 | dict.__setitem__(self, key, c) | |
113 | return c |
|
113 | return c | |
114 | else: |
|
114 | else: | |
115 | return dict.__getitem__(self, key) |
|
115 | return dict.__getitem__(self, key) | |
116 |
|
116 | |||
117 | def __setitem__(self, key, value): |
|
117 | def __setitem__(self, key, value): | |
118 | # Don't allow names in __builtin__ to be modified. |
|
118 | # Don't allow names in __builtin__ to be modified. | |
119 | if hasattr(__builtin__, key): |
|
119 | if hasattr(__builtin__, key): | |
120 | raise ConfigError('Config variable names cannot have the same name ' |
|
120 | raise ConfigError('Config variable names cannot have the same name ' | |
121 | 'as a Python builtin: %s' % key) |
|
121 | 'as a Python builtin: %s' % key) | |
122 | if self._is_section_key(key): |
|
122 | if self._is_section_key(key): | |
123 | if not isinstance(value, Config): |
|
123 | if not isinstance(value, Config): | |
124 | raise ValueError('values whose keys begin with an uppercase ' |
|
124 | raise ValueError('values whose keys begin with an uppercase ' | |
125 | 'char must be Config instances: %r, %r' % (key, value)) |
|
125 | 'char must be Config instances: %r, %r' % (key, value)) | |
126 | else: |
|
126 | else: | |
127 | dict.__setitem__(self, key, value) |
|
127 | dict.__setitem__(self, key, value) | |
128 |
|
128 | |||
129 | def __getattr__(self, key): |
|
129 | def __getattr__(self, key): | |
130 | try: |
|
130 | try: | |
131 | return self.__getitem__(key) |
|
131 | return self.__getitem__(key) | |
132 | except KeyError, e: |
|
132 | except KeyError, e: | |
133 | raise AttributeError(e) |
|
133 | raise AttributeError(e) | |
134 |
|
134 | |||
135 | def __setattr__(self, key, value): |
|
135 | def __setattr__(self, key, value): | |
136 | try: |
|
136 | try: | |
137 | self.__setitem__(key, value) |
|
137 | self.__setitem__(key, value) | |
138 | except KeyError, e: |
|
138 | except KeyError, e: | |
139 | raise AttributeError(e) |
|
139 | raise AttributeError(e) | |
140 |
|
140 | |||
141 | def __delattr__(self, key): |
|
141 | def __delattr__(self, key): | |
142 | try: |
|
142 | try: | |
143 | dict.__delitem__(self, key) |
|
143 | dict.__delitem__(self, key) | |
144 | except KeyError, e: |
|
144 | except KeyError, e: | |
145 | raise AttributeError(e) |
|
145 | raise AttributeError(e) | |
146 |
|
146 | |||
147 |
|
147 | |||
148 | #----------------------------------------------------------------------------- |
|
148 | #----------------------------------------------------------------------------- | |
149 | # Config loading classes |
|
149 | # Config loading classes | |
150 | #----------------------------------------------------------------------------- |
|
150 | #----------------------------------------------------------------------------- | |
151 |
|
151 | |||
152 |
|
152 | |||
153 | class ConfigLoader(object): |
|
153 | class ConfigLoader(object): | |
154 | """A object for loading configurations from just about anywhere. |
|
154 | """A object for loading configurations from just about anywhere. | |
155 |
|
155 | |||
156 | The resulting configuration is packaged as a :class:`Struct`. |
|
156 | The resulting configuration is packaged as a :class:`Struct`. | |
157 |
|
157 | |||
158 | Notes |
|
158 | Notes | |
159 | ----- |
|
159 | ----- | |
160 | A :class:`ConfigLoader` does one thing: load a config from a source |
|
160 | A :class:`ConfigLoader` does one thing: load a config from a source | |
161 | (file, command line arguments) and returns the data as a :class:`Struct`. |
|
161 | (file, command line arguments) and returns the data as a :class:`Struct`. | |
162 | There are lots of things that :class:`ConfigLoader` does not do. It does |
|
162 | There are lots of things that :class:`ConfigLoader` does not do. It does | |
163 | not implement complex logic for finding config files. It does not handle |
|
163 | not implement complex logic for finding config files. It does not handle | |
164 | default values or merge multiple configs. These things need to be |
|
164 | default values or merge multiple configs. These things need to be | |
165 | handled elsewhere. |
|
165 | handled elsewhere. | |
166 | """ |
|
166 | """ | |
167 |
|
167 | |||
168 | def __init__(self): |
|
168 | def __init__(self): | |
169 | """A base class for config loaders. |
|
169 | """A base class for config loaders. | |
170 |
|
170 | |||
171 | Examples |
|
171 | Examples | |
172 | -------- |
|
172 | -------- | |
173 |
|
173 | |||
174 | >>> cl = ConfigLoader() |
|
174 | >>> cl = ConfigLoader() | |
175 | >>> config = cl.load_config() |
|
175 | >>> config = cl.load_config() | |
176 | >>> config |
|
176 | >>> config | |
177 | {} |
|
177 | {} | |
178 | """ |
|
178 | """ | |
179 | self.clear() |
|
179 | self.clear() | |
180 |
|
180 | |||
181 | def clear(self): |
|
181 | def clear(self): | |
182 | self.config = Config() |
|
182 | self.config = Config() | |
183 |
|
183 | |||
184 | def load_config(self): |
|
184 | def load_config(self): | |
185 | """Load a config from somewhere, return a Struct. |
|
185 | """Load a config from somewhere, return a Struct. | |
186 |
|
186 | |||
187 | Usually, this will cause self.config to be set and then returned. |
|
187 | Usually, this will cause self.config to be set and then returned. | |
188 | """ |
|
188 | """ | |
189 | return self.config |
|
189 | return self.config | |
190 |
|
190 | |||
191 |
|
191 | |||
192 | class FileConfigLoader(ConfigLoader): |
|
192 | class FileConfigLoader(ConfigLoader): | |
193 | """A base class for file based configurations. |
|
193 | """A base class for file based configurations. | |
194 |
|
194 | |||
195 | As we add more file based config loaders, the common logic should go |
|
195 | As we add more file based config loaders, the common logic should go | |
196 | here. |
|
196 | here. | |
197 | """ |
|
197 | """ | |
198 | pass |
|
198 | pass | |
199 |
|
199 | |||
200 |
|
200 | |||
201 | class PyFileConfigLoader(FileConfigLoader): |
|
201 | class PyFileConfigLoader(FileConfigLoader): | |
202 | """A config loader for pure python files. |
|
202 | """A config loader for pure python files. | |
203 |
|
203 | |||
204 | This calls execfile on a plain python file and looks for attributes |
|
204 | This calls execfile on a plain python file and looks for attributes | |
205 | that are all caps. These attribute are added to the config Struct. |
|
205 | that are all caps. These attribute are added to the config Struct. | |
206 | """ |
|
206 | """ | |
207 |
|
207 | |||
208 | def __init__(self, filename, path=None): |
|
208 | def __init__(self, filename, path=None): | |
209 | """Build a config loader for a filename and path. |
|
209 | """Build a config loader for a filename and path. | |
210 |
|
210 | |||
211 | Parameters |
|
211 | Parameters | |
212 | ---------- |
|
212 | ---------- | |
213 | filename : str |
|
213 | filename : str | |
214 | The file name of the config file. |
|
214 | The file name of the config file. | |
215 | path : str, list, tuple |
|
215 | path : str, list, tuple | |
216 | The path to search for the config file on, or a sequence of |
|
216 | The path to search for the config file on, or a sequence of | |
217 | paths to try in order. |
|
217 | paths to try in order. | |
218 | """ |
|
218 | """ | |
219 | super(PyFileConfigLoader, self).__init__() |
|
219 | super(PyFileConfigLoader, self).__init__() | |
220 | self.filename = filename |
|
220 | self.filename = filename | |
221 | self.path = path |
|
221 | self.path = path | |
222 | self.full_filename = '' |
|
222 | self.full_filename = '' | |
223 | self.data = None |
|
223 | self.data = None | |
224 |
|
224 | |||
225 | def load_config(self): |
|
225 | def load_config(self): | |
226 | """Load the config from a file and return it as a Struct.""" |
|
226 | """Load the config from a file and return it as a Struct.""" | |
227 | self._find_file() |
|
227 | self._find_file() | |
228 | self._read_file_as_dict() |
|
228 | self._read_file_as_dict() | |
229 | self._convert_to_config() |
|
229 | self._convert_to_config() | |
230 | return self.config |
|
230 | return self.config | |
231 |
|
231 | |||
232 | def _find_file(self): |
|
232 | def _find_file(self): | |
233 | """Try to find the file by searching the paths.""" |
|
233 | """Try to find the file by searching the paths.""" | |
234 | self.full_filename = filefind(self.filename, self.path) |
|
234 | self.full_filename = filefind(self.filename, self.path) | |
235 |
|
235 | |||
236 | def _read_file_as_dict(self): |
|
236 | def _read_file_as_dict(self): | |
237 | execfile(self.full_filename, self.config) |
|
237 | """Load the config file into self.config, with recursive loading.""" | |
|
238 | # This closure is made available in the namespace that is used | |||
|
239 | # to exec the config file. This allows users to call | |||
|
240 | # load_subconfig('myconfig.py') to load config files recursively. | |||
|
241 | # It needs to be a closure because it has references to self.path | |||
|
242 | # and self.config. The sub-config is loaded with the same path | |||
|
243 | # as the parent, but it uses an empty config which is then merged | |||
|
244 | # with the parents. | |||
|
245 | def load_subconfig(fname): | |||
|
246 | loader = PyFileConfigLoader(fname, self.path) | |||
|
247 | sub_config = loader.load_config() | |||
|
248 | self.config._merge(sub_config) | |||
|
249 | ||||
|
250 | self.config.load_subconfig = load_subconfig | |||
|
251 | try: | |||
|
252 | execfile(self.full_filename, self.config) | |||
|
253 | finally: | |||
|
254 | del self.config.load_subconfig | |||
|
255 | del self.config['__builtins__'] | |||
238 |
|
256 | |||
239 | def _convert_to_config(self): |
|
257 | def _convert_to_config(self): | |
240 | if self.data is None: |
|
258 | if self.data is None: | |
241 | ConfigLoaderError('self.data does not exist') |
|
259 | ConfigLoaderError('self.data does not exist') | |
242 | del self.config['__builtins__'] |
|
|||
243 |
|
260 | |||
244 |
|
261 | |||
245 | class CommandLineConfigLoader(ConfigLoader): |
|
262 | class CommandLineConfigLoader(ConfigLoader): | |
246 | """A config loader for command line arguments. |
|
263 | """A config loader for command line arguments. | |
247 |
|
264 | |||
248 | As we add more command line based loaders, the common logic should go |
|
265 | As we add more command line based loaders, the common logic should go | |
249 | here. |
|
266 | here. | |
250 | """ |
|
267 | """ | |
251 |
|
268 | |||
252 |
|
269 | |||
253 | class NoConfigDefault(object): pass |
|
270 | class NoConfigDefault(object): pass | |
254 | NoConfigDefault = NoConfigDefault() |
|
271 | NoConfigDefault = NoConfigDefault() | |
255 |
|
272 | |||
256 | class ArgParseConfigLoader(CommandLineConfigLoader): |
|
273 | class ArgParseConfigLoader(CommandLineConfigLoader): | |
257 |
|
274 | |||
258 | # arguments = [(('-f','--file'),dict(type=str,dest='file'))] |
|
275 | # arguments = [(('-f','--file'),dict(type=str,dest='file'))] | |
259 | arguments = () |
|
276 | arguments = () | |
260 |
|
277 | |||
261 | def __init__(self, *args, **kw): |
|
278 | def __init__(self, *args, **kw): | |
262 | """Create a config loader for use with argparse. |
|
279 | """Create a config loader for use with argparse. | |
263 |
|
280 | |||
264 | The args and kwargs arguments here are passed onto the constructor |
|
281 | The args and kwargs arguments here are passed onto the constructor | |
265 | of :class:`argparse.ArgumentParser`. |
|
282 | of :class:`argparse.ArgumentParser`. | |
266 | """ |
|
283 | """ | |
267 | super(CommandLineConfigLoader, self).__init__() |
|
284 | super(CommandLineConfigLoader, self).__init__() | |
268 | self.args = args |
|
285 | self.args = args | |
269 | self.kw = kw |
|
286 | self.kw = kw | |
270 |
|
287 | |||
271 | def load_config(self, args=None): |
|
288 | def load_config(self, args=None): | |
272 | """Parse command line arguments and return as a Struct.""" |
|
289 | """Parse command line arguments and return as a Struct.""" | |
273 | self._create_parser() |
|
290 | self._create_parser() | |
274 | self._parse_args(args) |
|
291 | self._parse_args(args) | |
275 | self._convert_to_config() |
|
292 | self._convert_to_config() | |
276 | return self.config |
|
293 | return self.config | |
277 |
|
294 | |||
278 | def get_extra_args(self): |
|
295 | def get_extra_args(self): | |
279 | if hasattr(self, 'extra_args'): |
|
296 | if hasattr(self, 'extra_args'): | |
280 | return self.extra_args |
|
297 | return self.extra_args | |
281 | else: |
|
298 | else: | |
282 | return [] |
|
299 | return [] | |
283 |
|
300 | |||
284 | def _create_parser(self): |
|
301 | def _create_parser(self): | |
285 | self.parser = argparse.ArgumentParser(*self.args, **self.kw) |
|
302 | self.parser = argparse.ArgumentParser(*self.args, **self.kw) | |
286 | self._add_arguments() |
|
303 | self._add_arguments() | |
287 | self._add_other_arguments() |
|
304 | self._add_other_arguments() | |
288 |
|
305 | |||
289 | def _add_other_arguments(self): |
|
306 | def _add_other_arguments(self): | |
290 | pass |
|
307 | pass | |
291 |
|
308 | |||
292 | def _add_arguments(self): |
|
309 | def _add_arguments(self): | |
293 | for argument in self.arguments: |
|
310 | for argument in self.arguments: | |
294 | if not argument[1].has_key('default'): |
|
311 | if not argument[1].has_key('default'): | |
295 | argument[1]['default'] = NoConfigDefault |
|
312 | argument[1]['default'] = NoConfigDefault | |
296 | self.parser.add_argument(*argument[0],**argument[1]) |
|
313 | self.parser.add_argument(*argument[0],**argument[1]) | |
297 |
|
314 | |||
298 | def _parse_args(self, args=None): |
|
315 | def _parse_args(self, args=None): | |
299 | """self.parser->self.parsed_data""" |
|
316 | """self.parser->self.parsed_data""" | |
300 | if args is None: |
|
317 | if args is None: | |
301 | self.parsed_data, self.extra_args = self.parser.parse_known_args() |
|
318 | self.parsed_data, self.extra_args = self.parser.parse_known_args() | |
302 | else: |
|
319 | else: | |
303 | self.parsed_data, self.extra_args = self.parser.parse_known_args(args) |
|
320 | self.parsed_data, self.extra_args = self.parser.parse_known_args(args) | |
304 |
|
321 | |||
305 | def _convert_to_config(self): |
|
322 | def _convert_to_config(self): | |
306 | """self.parsed_data->self.config""" |
|
323 | """self.parsed_data->self.config""" | |
307 | for k, v in vars(self.parsed_data).items(): |
|
324 | for k, v in vars(self.parsed_data).items(): | |
308 | if v is not NoConfigDefault: |
|
325 | if v is not NoConfigDefault: | |
309 | exec_str = 'self.config.' + k + '= v' |
|
326 | exec_str = 'self.config.' + k + '= v' | |
310 | exec exec_str in locals(), globals() |
|
327 | exec exec_str in locals(), globals() | |
311 |
|
328 |
@@ -1,8 +1,17 b'' | |||||
1 | from ipython_config import * |
|
1 | # This can be used at any point in a config file to load a sub config | |
|
2 | # and merge it into the current one. | |||
|
3 | load_subconfig('ipython_config.py') | |||
2 |
|
4 | |||
3 | Global.exec_lines.extend([ |
|
5 | lines = """ | |
4 |
|
|
6 | import cmath | |
5 |
|
|
7 | from math import * | |
6 | 'print "*** math functions available globally, cmath as a module"' |
|
8 | """ | |
7 | ]) |
|
9 | ||
|
10 | # You have to make sure that attributes that are containers already | |||
|
11 | # exist before using them. Simple assigning a new list will override | |||
|
12 | # all previous values. | |||
|
13 | if hasattr(Global, 'exec_lines'): | |||
|
14 | Global.exec_lines.append(lines) | |||
|
15 | else: | |||
|
16 | Global.exec_lines = [lines] | |||
8 |
|
17 |
@@ -1,8 +1,18 b'' | |||||
1 | from ipython_config import * |
|
1 | # This can be used at any point in a config file to load a sub config | |
|
2 | # and merge it into the current one. | |||
|
3 | load_subconfig('ipython_config.py') | |||
2 |
|
4 | |||
3 | Global.exec_lines.extend([ |
|
5 | lines = """ | |
4 |
|
|
6 | import numpy | |
5 |
|
|
7 | import scipy | |
6 |
|
|
8 | import numpy as np | |
7 |
|
|
9 | import scipy as sp | |
8 | ]) No newline at end of file |
|
10 | """ | |
|
11 | ||||
|
12 | # You have to make sure that attributes that are containers already | |||
|
13 | # exist before using them. Simple assigning a new list will override | |||
|
14 | # all previous values. | |||
|
15 | if hasattr(Global, 'exec_lines'): | |||
|
16 | Global.exec_lines.append(lines) | |||
|
17 | else: | |||
|
18 | Global.exec_lines = [lines] No newline at end of file |
@@ -1,8 +1,20 b'' | |||||
1 | from ipython_config_numeric import * |
|
1 | # This can be used at any point in a config file to load a sub config | |
|
2 | # and merge it into the current one. | |||
|
3 | load_subconfig('ipython_config.py') | |||
2 |
|
4 | |||
3 | Global.exec_lines.extend([ |
|
5 | lines = """ | |
4 |
|
|
6 | import matplotlib | |
5 | 'from matplotlib import pyplot as plt', |
|
7 | %gui -a wx | |
6 | 'from matplotlib.pyplot import *' |
|
8 | matplotlib.use('wxagg') | |
7 | ]) |
|
9 | matplotlib.interactive(True) | |
|
10 | from matplotlib import pyplot as plt | |||
|
11 | from matplotlib.pyplot import * | |||
|
12 | """ | |||
8 |
|
13 | |||
|
14 | # You have to make sure that attributes that are containers already | |||
|
15 | # exist before using them. Simple assigning a new list will override | |||
|
16 | # all previous values. | |||
|
17 | if hasattr(Global, 'exec_lines'): | |||
|
18 | Global.exec_lines.append(lines) | |||
|
19 | else: | |||
|
20 | Global.exec_lines = [lines] No newline at end of file |
@@ -1,13 +1,27 b'' | |||||
1 | from ipython_config import * |
|
1 | # This can be used at any point in a config file to load a sub config | |
|
2 | # and merge it into the current one. | |||
|
3 | load_subconfig('ipython_config.py') | |||
2 |
|
4 | |||
3 |
InteractiveShell.prompt_in |
|
5 | InteractiveShell.prompt_in1 = '\C_LightGreen\u@\h\C_LightBlue[\C_LightCyan\Y1\C_LightBlue]\C_Green|\#> ' | |
4 | InteractiveShell.prompt_in2 = '\C_Green|\C_LightGreen\D\C_Green> ' |
|
6 | InteractiveShell.prompt_in2 = '\C_Green|\C_LightGreen\D\C_Green> ' | |
5 | InteractiveShell.prompt_out = '<\#> ' |
|
7 | InteractiveShell.prompt_out = '<\#> ' | |
6 |
|
8 | |||
7 | InteractiveShell.prompts_pad_left = True |
|
9 | InteractiveShell.prompts_pad_left = True | |
8 |
|
10 | |||
9 | InteractiveShell.separate_in = '' |
|
11 | InteractiveShell.separate_in = '' | |
10 | InteractiveShell.separate_out = '' |
|
12 | InteractiveShell.separate_out = '' | |
11 | InteractiveShell.separate_out2 = '' |
|
13 | InteractiveShell.separate_out2 = '' | |
12 |
|
14 | |||
13 | PrefilterManager.multi_line_specials = True No newline at end of file |
|
15 | PrefilterManager.multi_line_specials = True | |
|
16 | ||||
|
17 | lines = """ | |||
|
18 | %rehashx | |||
|
19 | """ | |||
|
20 | ||||
|
21 | # You have to make sure that attributes that are containers already | |||
|
22 | # exist before using them. Simple assigning a new list will override | |||
|
23 | # all previous values. | |||
|
24 | if hasattr(Global, 'exec_lines'): | |||
|
25 | Global.exec_lines.append(lines) | |||
|
26 | else: | |||
|
27 | Global.exec_lines = [lines] No newline at end of file |
@@ -1,9 +1,19 b'' | |||||
1 | from ipython_config import * |
|
1 | # This can be used at any point in a config file to load a sub config | |
|
2 | # and merge it into the current one. | |||
|
3 | load_subconfig('ipython_config.py') | |||
2 |
|
4 | |||
3 | Global.exec_lines.extend([ |
|
5 | lines = """ | |
4 |
|
|
6 | from __future__ import division | |
5 |
|
|
7 | from sympy import * | |
6 |
|
|
8 | x, y, z = symbols('xyz') | |
7 |
|
|
9 | k, m, n = symbols('kmn', integer=True) | |
8 |
|
|
10 | f, g, h = map(Function, 'fgh') | |
9 | ]) |
|
11 | """ | |
|
12 | ||||
|
13 | # You have to make sure that attributes that are containers already | |||
|
14 | # exist before using them. Simple assigning a new list will override | |||
|
15 | # all previous values. | |||
|
16 | if hasattr(Global, 'exec_lines'): | |||
|
17 | Global.exec_lines.append(lines) | |||
|
18 | else: | |||
|
19 | Global.exec_lines = [lines] |
General Comments 0
You need to be logged in to leave comments.
Login now