Show More
@@ -1,217 +1,220 b'' | |||||
1 | # encoding: utf-8 |
|
1 | # encoding: utf-8 | |
2 | """ |
|
2 | """ | |
3 | An application for managing IPython profiles. |
|
3 | An application for managing IPython profiles. | |
4 |
|
4 | |||
5 | To be invoked as the `ipython profile` subcommand. |
|
5 | To be invoked as the `ipython profile` subcommand. | |
6 |
|
6 | |||
7 | Authors: |
|
7 | Authors: | |
8 |
|
8 | |||
9 | * Min RK |
|
9 | * Min RK | |
10 |
|
10 | |||
11 | """ |
|
11 | """ | |
12 |
|
12 | |||
13 | #----------------------------------------------------------------------------- |
|
13 | #----------------------------------------------------------------------------- | |
14 | # Copyright (C) 2008-2011 The IPython Development Team |
|
14 | # Copyright (C) 2008-2011 The IPython Development Team | |
15 | # |
|
15 | # | |
16 | # Distributed under the terms of the BSD License. The full license is in |
|
16 | # Distributed under the terms of the BSD License. The full license is in | |
17 | # the file COPYING, distributed as part of this software. |
|
17 | # the file COPYING, distributed as part of this software. | |
18 | #----------------------------------------------------------------------------- |
|
18 | #----------------------------------------------------------------------------- | |
19 |
|
19 | |||
20 | #----------------------------------------------------------------------------- |
|
20 | #----------------------------------------------------------------------------- | |
21 | # Imports |
|
21 | # Imports | |
22 | #----------------------------------------------------------------------------- |
|
22 | #----------------------------------------------------------------------------- | |
23 |
|
23 | |||
24 | import logging |
|
24 | import logging | |
25 | import os |
|
25 | import os | |
26 |
|
26 | |||
27 | from IPython.config.application import Application, boolean_flag |
|
27 | from IPython.config.application import Application, boolean_flag | |
28 | from IPython.core.application import ( |
|
28 | from IPython.core.application import ( | |
29 | BaseIPythonApplication, base_flags, base_aliases |
|
29 | BaseIPythonApplication, base_flags, base_aliases | |
30 | ) |
|
30 | ) | |
31 | from IPython.core.profiledir import ProfileDir |
|
31 | from IPython.core.profiledir import ProfileDir | |
32 | from IPython.utils.path import get_ipython_dir |
|
32 | from IPython.utils.path import get_ipython_dir | |
33 | from IPython.utils.traitlets import Unicode, Bool, Dict |
|
33 | from IPython.utils.traitlets import Unicode, Bool, Dict | |
34 |
|
34 | |||
35 | #----------------------------------------------------------------------------- |
|
35 | #----------------------------------------------------------------------------- | |
36 | # Constants |
|
36 | # Constants | |
37 | #----------------------------------------------------------------------------- |
|
37 | #----------------------------------------------------------------------------- | |
38 |
|
38 | |||
39 | create_help = """Create an IPython profile by name |
|
39 | create_help = """Create an IPython profile by name | |
40 |
|
40 | |||
41 | Create an ipython profile directory by its name or |
|
41 | Create an ipython profile directory by its name or | |
42 | profile directory path. Profile directories contain |
|
42 | profile directory path. Profile directories contain | |
43 | configuration, log and security related files and are named |
|
43 | configuration, log and security related files and are named | |
44 | using the convention 'profile_<name>'. By default they are |
|
44 | using the convention 'profile_<name>'. By default they are | |
45 | located in your ipython directory. Once created, you will |
|
45 | located in your ipython directory. Once created, you will | |
46 | can edit the configuration files in the profile |
|
46 | can edit the configuration files in the profile | |
47 | directory to configure IPython. Most users will create a |
|
47 | directory to configure IPython. Most users will create a | |
48 | profile directory by name, |
|
48 | profile directory by name, | |
49 | `ipython profile create myprofile`, which will put the directory |
|
49 | `ipython profile create myprofile`, which will put the directory | |
50 | in `<ipython_dir>/profile_myprofile`. |
|
50 | in `<ipython_dir>/profile_myprofile`. | |
51 | """ |
|
51 | """ | |
52 | list_help = """List available IPython profiles |
|
52 | list_help = """List available IPython profiles | |
53 |
|
53 | |||
54 | List all available profiles, by profile location, that can |
|
54 | List all available profiles, by profile location, that can | |
55 | be found in the current working directly or in the ipython |
|
55 | be found in the current working directly or in the ipython | |
56 | directory. Profile directories are named using the convention |
|
56 | directory. Profile directories are named using the convention | |
57 | 'profile_<profile>'. |
|
57 | 'profile_<profile>'. | |
58 | """ |
|
58 | """ | |
59 | profile_help = """Manage IPython profiles |
|
59 | profile_help = """Manage IPython profiles | |
60 |
|
60 | |||
61 | Profile directories contain |
|
61 | Profile directories contain | |
62 | configuration, log and security related files and are named |
|
62 | configuration, log and security related files and are named | |
63 | using the convention 'profile_<name>'. By default they are |
|
63 | using the convention 'profile_<name>'. By default they are | |
64 | located in your ipython directory. You can create profiles |
|
64 | located in your ipython directory. You can create profiles | |
65 | with `ipython profile create <name>`, or see the profiles you |
|
65 | with `ipython profile create <name>`, or see the profiles you | |
66 | already have with `ipython profile list` |
|
66 | already have with `ipython profile list` | |
67 |
|
67 | |||
68 | To get started configuring IPython, simply do: |
|
68 | To get started configuring IPython, simply do: | |
69 |
|
69 | |||
70 | $> ipython profile create |
|
70 | $> ipython profile create | |
71 |
|
71 | |||
72 | and IPython will create the default profile in <ipython_dir>/profile_default, |
|
72 | and IPython will create the default profile in <ipython_dir>/profile_default, | |
73 | where you can edit ipython_config.py to start configuring IPython. |
|
73 | where you can edit ipython_config.py to start configuring IPython. | |
74 |
|
74 | |||
75 | """ |
|
75 | """ | |
76 |
|
76 | |||
77 | #----------------------------------------------------------------------------- |
|
77 | #----------------------------------------------------------------------------- | |
78 | # Profile Application Class (for `ipython profile` subcommand) |
|
78 | # Profile Application Class (for `ipython profile` subcommand) | |
79 | #----------------------------------------------------------------------------- |
|
79 | #----------------------------------------------------------------------------- | |
80 |
|
80 | |||
81 |
|
81 | |||
82 |
|
82 | |||
83 | class ProfileList(Application): |
|
83 | class ProfileList(Application): | |
84 | name = u'ipython-profile' |
|
84 | name = u'ipython-profile' | |
85 | description = list_help |
|
85 | description = list_help | |
86 |
|
86 | |||
87 | aliases = Dict(dict( |
|
87 | aliases = Dict(dict( | |
88 | ipython_dir = 'ProfileList.ipython_dir', |
|
88 | ipython_dir = 'ProfileList.ipython_dir', | |
89 | log_level = 'Application.log_level', |
|
89 | log_level = 'Application.log_level', | |
90 | )) |
|
90 | )) | |
91 | flags = Dict(dict( |
|
91 | flags = Dict(dict( | |
92 | debug = ({'Application' : {'log_level' : 0}}, |
|
92 | debug = ({'Application' : {'log_level' : 0}}, | |
93 | "Set log_level to 0, maximizing log output." |
|
93 | "Set log_level to 0, maximizing log output." | |
94 | ) |
|
94 | ) | |
95 | )) |
|
95 | )) | |
96 | ipython_dir = Unicode(get_ipython_dir(), config=True, |
|
96 | ipython_dir = Unicode(get_ipython_dir(), config=True, | |
97 | help=""" |
|
97 | help=""" | |
98 | The name of the IPython directory. This directory is used for logging |
|
98 | The name of the IPython directory. This directory is used for logging | |
99 | configuration (through profiles), history storage, etc. The default |
|
99 | configuration (through profiles), history storage, etc. The default | |
100 | is usually $HOME/.ipython. This options can also be specified through |
|
100 | is usually $HOME/.ipython. This options can also be specified through | |
101 | the environment variable IPYTHON_DIR. |
|
101 | the environment variable IPYTHON_DIR. | |
102 | """ |
|
102 | """ | |
103 | ) |
|
103 | ) | |
104 |
|
104 | |||
105 | def list_profile_dirs(self): |
|
105 | def list_profile_dirs(self): | |
106 | # Find the search paths |
|
106 | # Find the search paths | |
107 | paths = [os.getcwdu(), self.ipython_dir] |
|
107 | paths = [os.getcwdu(), self.ipython_dir] | |
108 |
|
108 | |||
109 | self.log.warn('Searching for IPython profiles in paths: %r' % paths) |
|
109 | self.log.warn('Searching for IPython profiles in paths: %r' % paths) | |
110 | for path in paths: |
|
110 | for path in paths: | |
111 | files = os.listdir(path) |
|
111 | files = os.listdir(path) | |
112 | for f in files: |
|
112 | for f in files: | |
113 | full_path = os.path.join(path, f) |
|
113 | full_path = os.path.join(path, f) | |
114 | if os.path.isdir(full_path) and f.startswith('profile_'): |
|
114 | if os.path.isdir(full_path) and f.startswith('profile_'): | |
115 | profile = f.split('_',1)[-1] |
|
115 | profile = f.split('_',1)[-1] | |
116 | start_cmd = 'ipython profile=%s' % profile |
|
116 | start_cmd = 'ipython profile=%s' % profile | |
117 | print start_cmd + " ==> " + full_path |
|
117 | print start_cmd + " ==> " + full_path | |
118 |
|
118 | |||
119 | def start(self): |
|
119 | def start(self): | |
120 | self.list_profile_dirs() |
|
120 | self.list_profile_dirs() | |
121 |
|
121 | |||
122 |
|
122 | |||
123 | create_flags = {} |
|
123 | create_flags = {} | |
124 | create_flags.update(base_flags) |
|
124 | create_flags.update(base_flags) | |
125 | create_flags.update(boolean_flag('reset', 'ProfileCreate.overwrite', |
|
125 | create_flags.update(boolean_flag('reset', 'ProfileCreate.overwrite', | |
126 | "reset config files to defaults", "leave existing config files")) |
|
126 | "reset config files to defaults", "leave existing config files")) | |
127 | create_flags.update(boolean_flag('parallel', 'ProfileCreate.parallel', |
|
127 | create_flags.update(boolean_flag('parallel', 'ProfileCreate.parallel', | |
128 | "Include parallel computing config files", |
|
128 | "Include parallel computing config files", | |
129 | "Don't include parallel computing config files")) |
|
129 | "Don't include parallel computing config files")) | |
130 |
|
130 | |||
131 | class ProfileCreate(BaseIPythonApplication): |
|
131 | class ProfileCreate(BaseIPythonApplication): | |
132 | name = u'ipython-profile' |
|
132 | name = u'ipython-profile' | |
133 | description = create_help |
|
133 | description = create_help | |
134 | auto_create = Bool(True, config=False) |
|
134 | auto_create = Bool(True, config=False) | |
135 |
|
135 | |||
136 | def _copy_config_files_default(self): |
|
136 | def _copy_config_files_default(self): | |
137 | return True |
|
137 | return True | |
138 |
|
138 | |||
139 | parallel = Bool(False, config=True, |
|
139 | parallel = Bool(False, config=True, | |
140 | help="whether to include parallel computing config files") |
|
140 | help="whether to include parallel computing config files") | |
141 | def _parallel_changed(self, name, old, new): |
|
141 | def _parallel_changed(self, name, old, new): | |
142 | parallel_files = [ 'ipcontroller_config.py', |
|
142 | parallel_files = [ 'ipcontroller_config.py', | |
143 | 'ipengine_config.py', |
|
143 | 'ipengine_config.py', | |
144 | 'ipcluster_config.py' |
|
144 | 'ipcluster_config.py' | |
145 | ] |
|
145 | ] | |
146 | if new: |
|
146 | if new: | |
147 | for cf in parallel_files: |
|
147 | for cf in parallel_files: | |
148 | self.config_files.append(cf) |
|
148 | self.config_files.append(cf) | |
149 | else: |
|
149 | else: | |
150 | for cf in parallel_files: |
|
150 | for cf in parallel_files: | |
151 | if cf in self.config_files: |
|
151 | if cf in self.config_files: | |
152 | self.config_files.remove(cf) |
|
152 | self.config_files.remove(cf) | |
153 |
|
153 | |||
154 | def parse_command_line(self, argv): |
|
154 | def parse_command_line(self, argv): | |
155 | super(ProfileCreate, self).parse_command_line(argv) |
|
155 | super(ProfileCreate, self).parse_command_line(argv) | |
156 | # accept positional arg as profile name |
|
156 | # accept positional arg as profile name | |
157 | if self.extra_args: |
|
157 | if self.extra_args: | |
158 | self.profile = self.extra_args[0] |
|
158 | self.profile = self.extra_args[0] | |
159 |
|
159 | |||
160 | flags = Dict(create_flags) |
|
160 | flags = Dict(create_flags) | |
161 |
|
161 | |||
162 | classes = [ProfileDir] |
|
162 | classes = [ProfileDir] | |
163 |
|
163 | |||
164 | def init_config_files(self): |
|
164 | def init_config_files(self): | |
165 | super(ProfileCreate, self).init_config_files() |
|
165 | super(ProfileCreate, self).init_config_files() | |
166 | # use local imports, since these classes may import from here |
|
166 | # use local imports, since these classes may import from here | |
167 | from IPython.frontend.terminal.ipapp import TerminalIPythonApp |
|
167 | from IPython.frontend.terminal.ipapp import TerminalIPythonApp | |
168 | apps = [TerminalIPythonApp] |
|
168 | apps = [TerminalIPythonApp] | |
169 | try: |
|
169 | try: | |
170 | from IPython.frontend.qt.console.qtconsoleapp import IPythonQtConsoleApp |
|
170 | from IPython.frontend.qt.console.qtconsoleapp import IPythonQtConsoleApp | |
171 |
except |
|
171 | except Exception: | |
|
172 | # this should be ImportError, but under weird circumstances | |||
|
173 | # this might be an AttributeError, or possibly others | |||
|
174 | # in any case, nothing should cause the profile creation to crash. | |||
172 | pass |
|
175 | pass | |
173 | else: |
|
176 | else: | |
174 | apps.append(IPythonQtConsoleApp) |
|
177 | apps.append(IPythonQtConsoleApp) | |
175 | if self.parallel: |
|
178 | if self.parallel: | |
176 | from IPython.parallel.apps.ipcontrollerapp import IPControllerApp |
|
179 | from IPython.parallel.apps.ipcontrollerapp import IPControllerApp | |
177 | from IPython.parallel.apps.ipengineapp import IPEngineApp |
|
180 | from IPython.parallel.apps.ipengineapp import IPEngineApp | |
178 | from IPython.parallel.apps.ipclusterapp import IPClusterStart |
|
181 | from IPython.parallel.apps.ipclusterapp import IPClusterStart | |
179 | from IPython.parallel.apps.iploggerapp import IPLoggerApp |
|
182 | from IPython.parallel.apps.iploggerapp import IPLoggerApp | |
180 | apps.extend([ |
|
183 | apps.extend([ | |
181 | IPControllerApp, |
|
184 | IPControllerApp, | |
182 | IPEngineApp, |
|
185 | IPEngineApp, | |
183 | IPClusterStart, |
|
186 | IPClusterStart, | |
184 | IPLoggerApp, |
|
187 | IPLoggerApp, | |
185 | ]) |
|
188 | ]) | |
186 | for App in apps: |
|
189 | for App in apps: | |
187 | app = App() |
|
190 | app = App() | |
188 | app.config.update(self.config) |
|
191 | app.config.update(self.config) | |
189 | app.log = self.log |
|
192 | app.log = self.log | |
190 | app.overwrite = self.overwrite |
|
193 | app.overwrite = self.overwrite | |
191 | app.copy_config_files=True |
|
194 | app.copy_config_files=True | |
192 | app.profile = self.profile |
|
195 | app.profile = self.profile | |
193 | app.init_profile_dir() |
|
196 | app.init_profile_dir() | |
194 | app.init_config_files() |
|
197 | app.init_config_files() | |
195 |
|
198 | |||
196 | def stage_default_config_file(self): |
|
199 | def stage_default_config_file(self): | |
197 | pass |
|
200 | pass | |
198 |
|
201 | |||
199 | class ProfileApp(Application): |
|
202 | class ProfileApp(Application): | |
200 | name = u'ipython-profile' |
|
203 | name = u'ipython-profile' | |
201 | description = profile_help |
|
204 | description = profile_help | |
202 |
|
205 | |||
203 | subcommands = Dict(dict( |
|
206 | subcommands = Dict(dict( | |
204 | create = (ProfileCreate, "Create a new profile dir with default config files"), |
|
207 | create = (ProfileCreate, "Create a new profile dir with default config files"), | |
205 | list = (ProfileList, "List existing profiles") |
|
208 | list = (ProfileList, "List existing profiles") | |
206 | )) |
|
209 | )) | |
207 |
|
210 | |||
208 | def start(self): |
|
211 | def start(self): | |
209 | if self.subapp is None: |
|
212 | if self.subapp is None: | |
210 | print "No subcommand specified. Must specify one of: %s"%(self.subcommands.keys()) |
|
213 | print "No subcommand specified. Must specify one of: %s"%(self.subcommands.keys()) | |
211 |
|
214 | |||
212 | self.print_description() |
|
215 | self.print_description() | |
213 | self.print_subcommands() |
|
216 | self.print_subcommands() | |
214 | self.exit(1) |
|
217 | self.exit(1) | |
215 | else: |
|
218 | else: | |
216 | return self.subapp.start() |
|
219 | return self.subapp.start() | |
217 |
|
220 |
General Comments 0
You need to be logged in to leave comments.
Login now