##// END OF EJS Templates
profile list prints just profile names
MinRK -
Show More
@@ -1,283 +1,290 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, get_ipython_package_dir
32 from IPython.utils.path import get_ipython_dir, get_ipython_package_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 _list_examples = "ipython profile list # list all profiles"
77 _list_examples = "ipython profile list # list all profiles"
78
78
79 _create_examples = """
79 _create_examples = """
80 ipython profile create foo # create profile foo w/ default config files
80 ipython profile create foo # create profile foo w/ default config files
81 ipython profile create foo --reset # restage default config files over current
81 ipython profile create foo --reset # restage default config files over current
82 ipython profile create foo --parallel # also stage parallel config files
82 ipython profile create foo --parallel # also stage parallel config files
83 """
83 """
84
84
85 _main_examples = """
85 _main_examples = """
86 ipython profile create -h # show the help string for the create subcommand
86 ipython profile create -h # show the help string for the create subcommand
87 ipython profile list -h # show the help string for the list subcommand
87 ipython profile list -h # show the help string for the list subcommand
88 """
88 """
89
89
90 #-----------------------------------------------------------------------------
90 #-----------------------------------------------------------------------------
91 # Profile Application Class (for `ipython profile` subcommand)
91 # Profile Application Class (for `ipython profile` subcommand)
92 #-----------------------------------------------------------------------------
92 #-----------------------------------------------------------------------------
93
93
94
94
95 class ProfileList(Application):
95 class ProfileList(Application):
96 name = u'ipython-profile'
96 name = u'ipython-profile'
97 description = list_help
97 description = list_help
98 examples = _list_examples
98 examples = _list_examples
99
99
100 aliases = Dict({
100 aliases = Dict({
101 'ipython-dir' : 'ProfileList.ipython_dir',
101 'ipython-dir' : 'ProfileList.ipython_dir',
102 'log-level' : 'Application.log_level',
102 'log-level' : 'Application.log_level',
103 })
103 })
104 flags = Dict(dict(
104 flags = Dict(dict(
105 debug = ({'Application' : {'log_level' : 0}},
105 debug = ({'Application' : {'log_level' : 0}},
106 "Set Application.log_level to 0, maximizing log output."
106 "Set Application.log_level to 0, maximizing log output."
107 )
107 )
108 ))
108 ))
109
109
110 ipython_dir = Unicode(get_ipython_dir(), config=True,
110 ipython_dir = Unicode(get_ipython_dir(), config=True,
111 help="""
111 help="""
112 The name of the IPython directory. This directory is used for logging
112 The name of the IPython directory. This directory is used for logging
113 configuration (through profiles), history storage, etc. The default
113 configuration (through profiles), history storage, etc. The default
114 is usually $HOME/.ipython. This options can also be specified through
114 is usually $HOME/.ipython. This options can also be specified through
115 the environment variable IPYTHON_DIR.
115 the environment variable IPYTHON_DIR.
116 """
116 """
117 )
117 )
118
118
119 def _list_profiles_in(self, path):
119 def _list_profiles_in(self, path):
120 """list profiles in a given root directory"""
120 """list profiles in a given root directory"""
121 files = os.listdir(path)
121 files = os.listdir(path)
122 profiles = []
122 profiles = []
123 for f in files:
123 for f in files:
124 full_path = os.path.join(path, f)
124 full_path = os.path.join(path, f)
125 if os.path.isdir(full_path) and f.startswith('profile_'):
125 if os.path.isdir(full_path) and f.startswith('profile_'):
126 profiles.append(f.split('_',1)[-1])
126 profiles.append(f.split('_',1)[-1])
127 return profiles
127 return profiles
128
128
129 def _list_bundled_profiles(self):
129 def _list_bundled_profiles(self):
130 """list profiles in a given root directory"""
130 """list profiles in a given root directory"""
131 path = os.path.join(get_ipython_package_dir(), u'config', u'profile')
131 path = os.path.join(get_ipython_package_dir(), u'config', u'profile')
132 files = os.listdir(path)
132 files = os.listdir(path)
133 profiles = []
133 profiles = []
134 for profile in files:
134 for profile in files:
135 full_path = os.path.join(path, profile)
135 full_path = os.path.join(path, profile)
136 if os.path.isdir(full_path):
136 if os.path.isdir(full_path):
137 profiles.append(profile)
137 profiles.append(profile)
138 return profiles
138 return profiles
139
139
140 def _print_profiles(self, profiles):
141 """print list of profiles, indented."""
142 for profile in profiles:
143 print ' %s' % profile
144
140 def list_profile_dirs(self):
145 def list_profile_dirs(self):
141 profiles = self._list_bundled_profiles()
146 profiles = self._list_bundled_profiles()
142 if profiles:
147 if profiles:
143 print
148 print
144 print "Available profiles in IPython:"
149 print "Available profiles in IPython:"
145 for profile in profiles:
150 self._print_profiles(profiles)
146 print ' ipython --profile=%s' % profile
147 print
151 print
148 print " The first request for a bundled profile will copy it"
152 print " The first request for a bundled profile will copy it"
149 print " into your IPython directory (%s)," % self.ipython_dir
153 print " into your IPython directory (%s)," % self.ipython_dir
150 print " where you can customize it to your needs."
154 print " where you can customize it."
151
155
152 profiles = self._list_profiles_in(self.ipython_dir)
156 profiles = self._list_profiles_in(self.ipython_dir)
153 if profiles:
157 if profiles:
154 print
158 print
155 print "Available profiles in %s:" % self.ipython_dir
159 print "Available profiles in %s:" % self.ipython_dir
156 for profile in profiles:
160 self._print_profiles(profiles)
157 print ' ipython --profile=%s' % profile
158
161
159 profiles = self._list_profiles_in(os.getcwdu())
162 profiles = self._list_profiles_in(os.getcwdu())
160 if profiles:
163 if profiles:
161 print
164 print
162 print "Available profiles in current directory (%s):" % os.getcwdu()
165 print "Available profiles in current directory (%s):" % os.getcwdu()
163 for profile in profiles:
166 self._print_profiles(profiles)
164 print ' ipython --profile=%s' % profile
167
168 print
169 print "To use any of the above profiles, start IPython with:"
170 print " ipython --profile=<name>"
171 print
165
172
166 def start(self):
173 def start(self):
167 self.list_profile_dirs()
174 self.list_profile_dirs()
168
175
169
176
170 create_flags = {}
177 create_flags = {}
171 create_flags.update(base_flags)
178 create_flags.update(base_flags)
172 # don't include '--init' flag, which implies running profile create in other apps
179 # don't include '--init' flag, which implies running profile create in other apps
173 create_flags.pop('init')
180 create_flags.pop('init')
174 create_flags['reset'] = ({'ProfileCreate': {'overwrite' : True}},
181 create_flags['reset'] = ({'ProfileCreate': {'overwrite' : True}},
175 "reset config files in this profile to the defaults.")
182 "reset config files in this profile to the defaults.")
176 create_flags['parallel'] = ({'ProfileCreate': {'parallel' : True}},
183 create_flags['parallel'] = ({'ProfileCreate': {'parallel' : True}},
177 "Include the config files for parallel "
184 "Include the config files for parallel "
178 "computing apps (ipengine, ipcontroller, etc.)")
185 "computing apps (ipengine, ipcontroller, etc.)")
179
186
180
187
181 class ProfileCreate(BaseIPythonApplication):
188 class ProfileCreate(BaseIPythonApplication):
182 name = u'ipython-profile'
189 name = u'ipython-profile'
183 description = create_help
190 description = create_help
184 examples = _create_examples
191 examples = _create_examples
185 auto_create = Bool(True, config=False)
192 auto_create = Bool(True, config=False)
186
193
187 def _copy_config_files_default(self):
194 def _copy_config_files_default(self):
188 return True
195 return True
189
196
190 parallel = Bool(False, config=True,
197 parallel = Bool(False, config=True,
191 help="whether to include parallel computing config files")
198 help="whether to include parallel computing config files")
192 def _parallel_changed(self, name, old, new):
199 def _parallel_changed(self, name, old, new):
193 parallel_files = [ 'ipcontroller_config.py',
200 parallel_files = [ 'ipcontroller_config.py',
194 'ipengine_config.py',
201 'ipengine_config.py',
195 'ipcluster_config.py'
202 'ipcluster_config.py'
196 ]
203 ]
197 if new:
204 if new:
198 for cf in parallel_files:
205 for cf in parallel_files:
199 self.config_files.append(cf)
206 self.config_files.append(cf)
200 else:
207 else:
201 for cf in parallel_files:
208 for cf in parallel_files:
202 if cf in self.config_files:
209 if cf in self.config_files:
203 self.config_files.remove(cf)
210 self.config_files.remove(cf)
204
211
205 def parse_command_line(self, argv):
212 def parse_command_line(self, argv):
206 super(ProfileCreate, self).parse_command_line(argv)
213 super(ProfileCreate, self).parse_command_line(argv)
207 # accept positional arg as profile name
214 # accept positional arg as profile name
208 if self.extra_args:
215 if self.extra_args:
209 self.profile = self.extra_args[0]
216 self.profile = self.extra_args[0]
210
217
211 flags = Dict(create_flags)
218 flags = Dict(create_flags)
212
219
213 classes = [ProfileDir]
220 classes = [ProfileDir]
214
221
215 def init_config_files(self):
222 def init_config_files(self):
216 super(ProfileCreate, self).init_config_files()
223 super(ProfileCreate, self).init_config_files()
217 # use local imports, since these classes may import from here
224 # use local imports, since these classes may import from here
218 from IPython.frontend.terminal.ipapp import TerminalIPythonApp
225 from IPython.frontend.terminal.ipapp import TerminalIPythonApp
219 apps = [TerminalIPythonApp]
226 apps = [TerminalIPythonApp]
220 try:
227 try:
221 from IPython.frontend.qt.console.qtconsoleapp import IPythonQtConsoleApp
228 from IPython.frontend.qt.console.qtconsoleapp import IPythonQtConsoleApp
222 except Exception:
229 except Exception:
223 # this should be ImportError, but under weird circumstances
230 # this should be ImportError, but under weird circumstances
224 # this might be an AttributeError, or possibly others
231 # this might be an AttributeError, or possibly others
225 # in any case, nothing should cause the profile creation to crash.
232 # in any case, nothing should cause the profile creation to crash.
226 pass
233 pass
227 else:
234 else:
228 apps.append(IPythonQtConsoleApp)
235 apps.append(IPythonQtConsoleApp)
229 try:
236 try:
230 from IPython.frontend.html.notebook.notebookapp import NotebookApp
237 from IPython.frontend.html.notebook.notebookapp import NotebookApp
231 except ImportError:
238 except ImportError:
232 pass
239 pass
233 except Exception:
240 except Exception:
234 self.log.debug('Unexpected error when importing NotebookApp',
241 self.log.debug('Unexpected error when importing NotebookApp',
235 exc_info=True
242 exc_info=True
236 )
243 )
237 else:
244 else:
238 apps.append(NotebookApp)
245 apps.append(NotebookApp)
239 if self.parallel:
246 if self.parallel:
240 from IPython.parallel.apps.ipcontrollerapp import IPControllerApp
247 from IPython.parallel.apps.ipcontrollerapp import IPControllerApp
241 from IPython.parallel.apps.ipengineapp import IPEngineApp
248 from IPython.parallel.apps.ipengineapp import IPEngineApp
242 from IPython.parallel.apps.ipclusterapp import IPClusterStart
249 from IPython.parallel.apps.ipclusterapp import IPClusterStart
243 from IPython.parallel.apps.iploggerapp import IPLoggerApp
250 from IPython.parallel.apps.iploggerapp import IPLoggerApp
244 apps.extend([
251 apps.extend([
245 IPControllerApp,
252 IPControllerApp,
246 IPEngineApp,
253 IPEngineApp,
247 IPClusterStart,
254 IPClusterStart,
248 IPLoggerApp,
255 IPLoggerApp,
249 ])
256 ])
250 for App in apps:
257 for App in apps:
251 app = App()
258 app = App()
252 app.config.update(self.config)
259 app.config.update(self.config)
253 app.log = self.log
260 app.log = self.log
254 app.overwrite = self.overwrite
261 app.overwrite = self.overwrite
255 app.copy_config_files=True
262 app.copy_config_files=True
256 app.profile = self.profile
263 app.profile = self.profile
257 app.init_profile_dir()
264 app.init_profile_dir()
258 app.init_config_files()
265 app.init_config_files()
259
266
260 def stage_default_config_file(self):
267 def stage_default_config_file(self):
261 pass
268 pass
262
269
263
270
264 class ProfileApp(Application):
271 class ProfileApp(Application):
265 name = u'ipython-profile'
272 name = u'ipython-profile'
266 description = profile_help
273 description = profile_help
267 examples = _main_examples
274 examples = _main_examples
268
275
269 subcommands = Dict(dict(
276 subcommands = Dict(dict(
270 create = (ProfileCreate, "Create a new profile dir with default config files"),
277 create = (ProfileCreate, "Create a new profile dir with default config files"),
271 list = (ProfileList, "List existing profiles")
278 list = (ProfileList, "List existing profiles")
272 ))
279 ))
273
280
274 def start(self):
281 def start(self):
275 if self.subapp is None:
282 if self.subapp is None:
276 print "No subcommand specified. Must specify one of: %s"%(self.subcommands.keys())
283 print "No subcommand specified. Must specify one of: %s"%(self.subcommands.keys())
277 print
284 print
278 self.print_description()
285 self.print_description()
279 self.print_subcommands()
286 self.print_subcommands()
280 self.exit(1)
287 self.exit(1)
281 else:
288 else:
282 return self.subapp.start()
289 return self.subapp.start()
283
290
General Comments 0
You need to be logged in to leave comments. Login now