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