##// END OF EJS Templates
remove redundant `ipython profile locate` entry point
MinRK -
Show More
@@ -1,308 +1,307 b''
1 1 # encoding: utf-8
2 2 """
3 3 An application for managing IPython profiles.
4 4
5 5 To be invoked as the `ipython profile` subcommand.
6 6
7 7 Authors:
8 8
9 9 * Min RK
10 10
11 11 """
12 12
13 13 #-----------------------------------------------------------------------------
14 14 # Copyright (C) 2008-2011 The IPython Development Team
15 15 #
16 16 # Distributed under the terms of the BSD License. The full license is in
17 17 # the file COPYING, distributed as part of this software.
18 18 #-----------------------------------------------------------------------------
19 19
20 20 #-----------------------------------------------------------------------------
21 21 # Imports
22 22 #-----------------------------------------------------------------------------
23 23
24 24 import logging
25 25 import os
26 26
27 27 from IPython.config.application import Application, boolean_flag
28 28 from IPython.core.application import (
29 29 BaseIPythonApplication, base_flags, base_aliases
30 30 )
31 31 from IPython.core.profiledir import ProfileDir
32 32 from IPython.utils.path import get_ipython_dir, get_ipython_package_dir
33 33 from IPython.utils.traitlets import Unicode, Bool, Dict
34 34
35 35 #-----------------------------------------------------------------------------
36 36 # Constants
37 37 #-----------------------------------------------------------------------------
38 38
39 39 create_help = """Create an IPython profile by name
40 40
41 41 Create an ipython profile directory by its name or
42 42 profile directory path. Profile directories contain
43 43 configuration, log and security related files and are named
44 44 using the convention 'profile_<name>'. By default they are
45 45 located in your ipython directory. Once created, you will
46 46 can edit the configuration files in the profile
47 47 directory to configure IPython. Most users will create a
48 48 profile directory by name,
49 49 `ipython profile create myprofile`, which will put the directory
50 50 in `<ipython_dir>/profile_myprofile`.
51 51 """
52 52 list_help = """List available IPython profiles
53 53
54 54 List all available profiles, by profile location, that can
55 55 be found in the current working directly or in the ipython
56 56 directory. Profile directories are named using the convention
57 57 'profile_<profile>'.
58 58 """
59 59 profile_help = """Manage IPython profiles
60 60
61 61 Profile directories contain
62 62 configuration, log and security related files and are named
63 63 using the convention 'profile_<name>'. By default they are
64 64 located in your ipython directory. You can create profiles
65 65 with `ipython profile create <name>`, or see the profiles you
66 66 already have with `ipython profile list`
67 67
68 68 To get started configuring IPython, simply do:
69 69
70 70 $> ipython profile create
71 71
72 72 and IPython will create the default profile in <ipython_dir>/profile_default,
73 73 where you can edit ipython_config.py to start configuring IPython.
74 74
75 75 """
76 76
77 77 _list_examples = "ipython profile list # list all profiles"
78 78
79 79 _create_examples = """
80 80 ipython profile create foo # create profile foo w/ default config files
81 81 ipython profile create foo --reset # restage default config files over current
82 82 ipython profile create foo --parallel # also stage parallel config files
83 83 """
84 84
85 85 _main_examples = """
86 86 ipython profile create -h # show the help string for the create subcommand
87 87 ipython profile list -h # show the help string for the list subcommand
88 88
89 ipython profile locate foo # print the path to the directory for profile 'foo'
89 ipython locate profile foo # print the path to the directory for profile 'foo'
90 90 """
91 91
92 92 #-----------------------------------------------------------------------------
93 93 # Profile Application Class (for `ipython profile` subcommand)
94 94 #-----------------------------------------------------------------------------
95 95
96 96
97 97 def list_profiles_in(path):
98 98 """list profiles in a given root directory"""
99 99 files = os.listdir(path)
100 100 profiles = []
101 101 for f in files:
102 102 full_path = os.path.join(path, f)
103 103 if os.path.isdir(full_path) and f.startswith('profile_'):
104 104 profiles.append(f.split('_',1)[-1])
105 105 return profiles
106 106
107 107
108 108 def list_bundled_profiles():
109 109 """list profiles that are bundled with IPython."""
110 110 path = os.path.join(get_ipython_package_dir(), u'config', u'profile')
111 111 files = os.listdir(path)
112 112 profiles = []
113 113 for profile in files:
114 114 full_path = os.path.join(path, profile)
115 115 if os.path.isdir(full_path) and profile != "__pycache__":
116 116 profiles.append(profile)
117 117 return profiles
118 118
119 119
120 120 class ProfileLocate(BaseIPythonApplication):
121 121 description = """print the path an IPython profile dir"""
122 122
123 123 def parse_command_line(self, argv=None):
124 124 super(ProfileLocate, self).parse_command_line(argv)
125 125 if self.extra_args:
126 126 self.profile = self.extra_args[0]
127 127
128 128 def start(self):
129 129 print self.profile_dir.location
130 130
131 131
132 132 class ProfileList(Application):
133 133 name = u'ipython-profile'
134 134 description = list_help
135 135 examples = _list_examples
136 136
137 137 aliases = Dict({
138 138 'ipython-dir' : 'ProfileList.ipython_dir',
139 139 'log-level' : 'Application.log_level',
140 140 })
141 141 flags = Dict(dict(
142 142 debug = ({'Application' : {'log_level' : 0}},
143 143 "Set Application.log_level to 0, maximizing log output."
144 144 )
145 145 ))
146 146
147 147 ipython_dir = Unicode(get_ipython_dir(), config=True,
148 148 help="""
149 149 The name of the IPython directory. This directory is used for logging
150 150 configuration (through profiles), history storage, etc. The default
151 151 is usually $HOME/.ipython. This options can also be specified through
152 152 the environment variable IPYTHONDIR.
153 153 """
154 154 )
155 155
156 156
157 157 def _print_profiles(self, profiles):
158 158 """print list of profiles, indented."""
159 159 for profile in profiles:
160 160 print ' %s' % profile
161 161
162 162 def list_profile_dirs(self):
163 163 profiles = list_bundled_profiles()
164 164 if profiles:
165 165 print
166 166 print "Available profiles in IPython:"
167 167 self._print_profiles(profiles)
168 168 print
169 169 print " The first request for a bundled profile will copy it"
170 170 print " into your IPython directory (%s)," % self.ipython_dir
171 171 print " where you can customize it."
172 172
173 173 profiles = list_profiles_in(self.ipython_dir)
174 174 if profiles:
175 175 print
176 176 print "Available profiles in %s:" % self.ipython_dir
177 177 self._print_profiles(profiles)
178 178
179 179 profiles = list_profiles_in(os.getcwdu())
180 180 if profiles:
181 181 print
182 182 print "Available profiles in current directory (%s):" % os.getcwdu()
183 183 self._print_profiles(profiles)
184 184
185 185 print
186 186 print "To use any of the above profiles, start IPython with:"
187 187 print " ipython --profile=<name>"
188 188 print
189 189
190 190 def start(self):
191 191 self.list_profile_dirs()
192 192
193 193
194 194 create_flags = {}
195 195 create_flags.update(base_flags)
196 196 # don't include '--init' flag, which implies running profile create in other apps
197 197 create_flags.pop('init')
198 198 create_flags['reset'] = ({'ProfileCreate': {'overwrite' : True}},
199 199 "reset config files in this profile to the defaults.")
200 200 create_flags['parallel'] = ({'ProfileCreate': {'parallel' : True}},
201 201 "Include the config files for parallel "
202 202 "computing apps (ipengine, ipcontroller, etc.)")
203 203
204 204
205 205 class ProfileCreate(BaseIPythonApplication):
206 206 name = u'ipython-profile'
207 207 description = create_help
208 208 examples = _create_examples
209 209 auto_create = Bool(True, config=False)
210 210
211 211 def _copy_config_files_default(self):
212 212 return True
213 213
214 214 parallel = Bool(False, config=True,
215 215 help="whether to include parallel computing config files")
216 216 def _parallel_changed(self, name, old, new):
217 217 parallel_files = [ 'ipcontroller_config.py',
218 218 'ipengine_config.py',
219 219 'ipcluster_config.py'
220 220 ]
221 221 if new:
222 222 for cf in parallel_files:
223 223 self.config_files.append(cf)
224 224 else:
225 225 for cf in parallel_files:
226 226 if cf in self.config_files:
227 227 self.config_files.remove(cf)
228 228
229 229 def parse_command_line(self, argv):
230 230 super(ProfileCreate, self).parse_command_line(argv)
231 231 # accept positional arg as profile name
232 232 if self.extra_args:
233 233 self.profile = self.extra_args[0]
234 234
235 235 flags = Dict(create_flags)
236 236
237 237 classes = [ProfileDir]
238 238
239 239 def init_config_files(self):
240 240 super(ProfileCreate, self).init_config_files()
241 241 # use local imports, since these classes may import from here
242 242 from IPython.frontend.terminal.ipapp import TerminalIPythonApp
243 243 apps = [TerminalIPythonApp]
244 244 try:
245 245 from IPython.frontend.qt.console.qtconsoleapp import IPythonQtConsoleApp
246 246 except Exception:
247 247 # this should be ImportError, but under weird circumstances
248 248 # this might be an AttributeError, or possibly others
249 249 # in any case, nothing should cause the profile creation to crash.
250 250 pass
251 251 else:
252 252 apps.append(IPythonQtConsoleApp)
253 253 try:
254 254 from IPython.frontend.html.notebook.notebookapp import NotebookApp
255 255 except ImportError:
256 256 pass
257 257 except Exception:
258 258 self.log.debug('Unexpected error when importing NotebookApp',
259 259 exc_info=True
260 260 )
261 261 else:
262 262 apps.append(NotebookApp)
263 263 if self.parallel:
264 264 from IPython.parallel.apps.ipcontrollerapp import IPControllerApp
265 265 from IPython.parallel.apps.ipengineapp import IPEngineApp
266 266 from IPython.parallel.apps.ipclusterapp import IPClusterStart
267 267 from IPython.parallel.apps.iploggerapp import IPLoggerApp
268 268 apps.extend([
269 269 IPControllerApp,
270 270 IPEngineApp,
271 271 IPClusterStart,
272 272 IPLoggerApp,
273 273 ])
274 274 for App in apps:
275 275 app = App()
276 276 app.config.update(self.config)
277 277 app.log = self.log
278 278 app.overwrite = self.overwrite
279 279 app.copy_config_files=True
280 280 app.profile = self.profile
281 281 app.init_profile_dir()
282 282 app.init_config_files()
283 283
284 284 def stage_default_config_file(self):
285 285 pass
286 286
287 287
288 288 class ProfileApp(Application):
289 289 name = u'ipython-profile'
290 290 description = profile_help
291 291 examples = _main_examples
292 292
293 293 subcommands = Dict(dict(
294 294 create = (ProfileCreate, ProfileCreate.description.splitlines()[0]),
295 295 list = (ProfileList, ProfileList.description.splitlines()[0]),
296 locate = (ProfileLocate, ProfileLocate.description.splitlines()[0]),
297 296 ))
298 297
299 298 def start(self):
300 299 if self.subapp is None:
301 300 print "No subcommand specified. Must specify one of: %s"%(self.subcommands.keys())
302 301 print
303 302 self.print_description()
304 303 self.print_subcommands()
305 304 self.exit(1)
306 305 else:
307 306 return self.subapp.start()
308 307
General Comments 0
You need to be logged in to leave comments. Login now