##// END OF EJS Templates
Improve pylab support, find profiles in IPython's own directory....
Fernando Perez -
Show More
@@ -34,7 +34,7 b' import os'
34 import sys
34 import sys
35
35
36 from IPython.core import release
36 from IPython.core import release
37 from IPython.utils.genutils import get_ipython_dir
37 from IPython.utils.genutils import get_ipython_dir, get_ipython_package_dir
38 from IPython.config.loader import (
38 from IPython.config.loader import (
39 PyFileConfigLoader,
39 PyFileConfigLoader,
40 ArgParseConfigLoader,
40 ArgParseConfigLoader,
@@ -82,15 +82,22 b' class Application(object):'
82
82
83 name = u'ipython'
83 name = u'ipython'
84 description = 'IPython: an enhanced interactive Python shell.'
84 description = 'IPython: an enhanced interactive Python shell.'
85
85 config_file_name = u'ipython_config.py'
86 config_file_name = u'ipython_config.py'
87 # Track the default and actual separately because some messages are
88 # only printed if we aren't using the default.
89 default_config_file_name = config_file_name
86 default_log_level = logging.WARN
90 default_log_level = logging.WARN
91 # Set by --profile option
92 profile_name = None
93 # User's ipython directory, typically ~/.ipython/
94 ipython_dir = None
95
96 # Private attributes
97 _exiting = False
87
98
88 def __init__(self):
99 def __init__(self):
89 self._exiting = False
90 self.init_logger()
100 self.init_logger()
91 # Track the default and actual separately because some messages are
92 # only printed if we aren't using the default.
93 self.default_config_file_name = self.config_file_name
94
101
95 def init_logger(self):
102 def init_logger(self):
96 self.log = logging.getLogger(self.__class__.__name__)
103 self.log = logging.getLogger(self.__class__.__name__)
@@ -197,10 +204,10 b' class Application(object):'
197 def find_ipython_dir(self):
204 def find_ipython_dir(self):
198 """Set the IPython directory.
205 """Set the IPython directory.
199
206
200 This sets ``self.ipython_dir``, but the actual value that is passed
207 This sets ``self.ipython_dir``, but the actual value that is passed to
201 to the application is kept in either ``self.default_config`` or
208 the application is kept in either ``self.default_config`` or
202 ``self.command_line_config``. This also adds ``self.ipython_dir`` to
209 ``self.command_line_config``. This also adds ``self.ipython_dir`` to
203 ``sys.path`` so config files there can be references by other config
210 ``sys.path`` so config files there can be referenced by other config
204 files.
211 files.
205 """
212 """
206
213
@@ -230,8 +237,7 b' class Application(object):'
230 config file are set in :meth:`find_config_file_paths` and then passed
237 config file are set in :meth:`find_config_file_paths` and then passed
231 to the config file loader where they are resolved to an absolute path.
238 to the config file loader where they are resolved to an absolute path.
232
239
233 If a profile has been set at the command line, this will resolve
240 If a profile has been set at the command line, this will resolve it.
234 it.
235 """
241 """
236
242
237 try:
243 try:
@@ -241,11 +247,12 b' class Application(object):'
241
247
242 try:
248 try:
243 self.profile_name = self.command_line_config.Global.profile
249 self.profile_name = self.command_line_config.Global.profile
250 except AttributeError:
251 pass
252 else:
244 name_parts = self.config_file_name.split('.')
253 name_parts = self.config_file_name.split('.')
245 name_parts.insert(1, u'_' + self.profile_name + u'.')
254 name_parts.insert(1, u'_' + self.profile_name + u'.')
246 self.config_file_name = ''.join(name_parts)
255 self.config_file_name = ''.join(name_parts)
247 except AttributeError:
248 pass
249
256
250 def find_config_file_paths(self):
257 def find_config_file_paths(self):
251 """Set the search paths for resolving the config file.
258 """Set the search paths for resolving the config file.
@@ -253,7 +260,11 b' class Application(object):'
253 This must set ``self.config_file_paths`` to a sequence of search
260 This must set ``self.config_file_paths`` to a sequence of search
254 paths to pass to the config file loader.
261 paths to pass to the config file loader.
255 """
262 """
256 self.config_file_paths = (os.getcwd(), self.ipython_dir)
263 # Include our own profiles directory last, so that users can still find
264 # our shipped copies of builtin profiles even if they don't have them
265 # in their local ipython directory.
266 prof_dir = os.path.join(get_ipython_package_dir(), 'config', 'profile')
267 self.config_file_paths = (os.getcwd(), self.ipython_dir,prof_dir)
257
268
258 def pre_load_file_config(self):
269 def pre_load_file_config(self):
259 """Do actions before the config file is loaded."""
270 """Do actions before the config file is loaded."""
@@ -266,7 +277,8 b' class Application(object):'
266 ``CONFIG_FILE`` config variable is set to the resolved config file
277 ``CONFIG_FILE`` config variable is set to the resolved config file
267 location. If not successful, an empty config is used.
278 location. If not successful, an empty config is used.
268 """
279 """
269 self.log.debug("Attempting to load config file: %s" % self.config_file_name)
280 self.log.debug("Attempting to load config file: %s" %
281 self.config_file_name)
270 loader = PyFileConfigLoader(self.config_file_name,
282 loader = PyFileConfigLoader(self.config_file_name,
271 path=self.config_file_paths)
283 path=self.config_file_paths)
272 try:
284 try:
@@ -275,11 +287,11 b' class Application(object):'
275 except IOError:
287 except IOError:
276 # Only warn if the default config file was NOT being used.
288 # Only warn if the default config file was NOT being used.
277 if not self.config_file_name==self.default_config_file_name:
289 if not self.config_file_name==self.default_config_file_name:
278 self.log.warn("Config file not found, skipping: %s" % \
290 self.log.warn("Config file not found, skipping: %s" %
279 self.config_file_name, exc_info=True)
291 self.config_file_name, exc_info=True)
280 self.file_config = Config()
292 self.file_config = Config()
281 except:
293 except:
282 self.log.warn("Error loading config file: %s" % \
294 self.log.warn("Error loading config file: %s" %
283 self.config_file_name, exc_info=True)
295 self.config_file_name, exc_info=True)
284 self.file_config = Config()
296 self.file_config = Config()
285
297
@@ -299,7 +311,8 b' class Application(object):'
299
311
300 def log_file_config(self):
312 def log_file_config(self):
301 if hasattr(self.file_config.Global, 'config_file'):
313 if hasattr(self.file_config.Global, 'config_file'):
302 self.log.debug("Config file loaded: %s" % self.file_config.Global.config_file)
314 self.log.debug("Config file loaded: %s" %
315 self.file_config.Global.config_file)
303 self.log.debug(repr(self.file_config))
316 self.log.debug(repr(self.file_config))
304
317
305 def merge_configs(self):
318 def merge_configs(self):
@@ -292,6 +292,24 b' class IPythonAppCLConfigLoader(BaseAppArgParseConfigLoader):'
292
292
293 arguments = cl_args
293 arguments = cl_args
294
294
295 def load_config(self):
296 """Do actions just before loading the command line config."""
297
298 # Special hack: there are countless uses of 'ipython -pylab' (with one
299 # dash) in the wild, including in printed books. Since argparse does
300 # will interpret -pylab as '-p ylab', sending us in a search for a
301 # profile named 'ylab', instead we special-case here -pylab as the
302 # first or second option only (this is how old ipython used to work)
303 # and convert this use to --pylab. Ugly, but needed for this one
304 # very widely used case.
305 firstargs = sys.argv[:3]
306 try:
307 idx = firstargs.index('-pylab')
308 except ValueError:
309 pass
310 else:
311 sys.argv[idx] = '--pylab'
312 return super(IPythonAppCLConfigLoader, self).load_config()
295
313
296 default_config_file_name = u'ipython_config.py'
314 default_config_file_name = u'ipython_config.py'
297
315
@@ -303,22 +321,25 b' class IPythonApp(Application):'
303
321
304 def create_default_config(self):
322 def create_default_config(self):
305 super(IPythonApp, self).create_default_config()
323 super(IPythonApp, self).create_default_config()
306 self.default_config.Global.display_banner = True
324 # Eliminate multiple lookups
325 Global = self.default_config.Global
326 # Set all default values
327 Global.display_banner = True
307
328
308 # If the -c flag is given or a file is given to run at the cmd line
329 # If the -c flag is given or a file is given to run at the cmd line
309 # like "ipython foo.py", normally we exit without starting the main
330 # like "ipython foo.py", normally we exit without starting the main
310 # loop. The force_interact config variable allows a user to override
331 # loop. The force_interact config variable allows a user to override
311 # this and interact. It is also set by the -i cmd line flag, just
332 # this and interact. It is also set by the -i cmd line flag, just
312 # like Python.
333 # like Python.
313 self.default_config.Global.force_interact = False
334 Global.force_interact = False
314
335
315 # By default always interact by starting the IPython mainloop.
336 # By default always interact by starting the IPython mainloop.
316 self.default_config.Global.interact = True
337 Global.interact = True
317
338
318 # No GUI integration by default
339 # No GUI integration by default
319 self.default_config.Global.wthread = False
340 Global.wthread = False
320 self.default_config.Global.q4thread = False
341 Global.q4thread = False
321 self.default_config.Global.gthread = False
342 Global.gthread = False
322
343
323 def create_command_line_config(self):
344 def create_command_line_config(self):
324 """Create and return a command line config loader."""
345 """Create and return a command line config loader."""
@@ -46,11 +46,6 b' from IPython.utils import platutils'
46 from IPython.utils.generics import result_display
46 from IPython.utils.generics import result_display
47 from IPython.external.path import path
47 from IPython.external.path import path
48
48
49 try:
50 set
51 except:
52 from sets import Set as set
53
54
49
55 #****************************************************************************
50 #****************************************************************************
56 # Exceptions
51 # Exceptions
@@ -824,6 +819,12 b' def get_ipython_dir():'
824 return ipdir.decode(sys.getfilesystemencoding())
819 return ipdir.decode(sys.getfilesystemencoding())
825
820
826
821
822 def get_ipython_package_dir():
823 """Get the base directory where IPython itself is installed."""
824 ipdir = os.path.dirname(IPython.__file__)
825 return ipdir.decode(sys.getfilesystemencoding())
826
827
827 #****************************************************************************
828 #****************************************************************************
828 # strings and text
829 # strings and text
829
830
@@ -304,3 +304,8 b' def test_filefind():'
304 alt_dirs = genutils.get_ipython_dir()
304 alt_dirs = genutils.get_ipython_dir()
305 t = genutils.filefind(f.name,alt_dirs)
305 t = genutils.filefind(f.name,alt_dirs)
306 print 'found:',t
306 print 'found:',t
307
308
309 def test_get_ipython_package_dir():
310 ipdir = genutils.get_ipython_package_dir()
311 nt.assert_true(os.path.isdir(ipdir))
General Comments 0
You need to be logged in to leave comments. Login now