Show More
@@ -1,133 +1,133 | |||||
1 | # encoding: utf-8 |
|
1 | # encoding: utf-8 | |
2 | """ |
|
2 | """ | |
3 | An application for IPython. |
|
3 | An application for IPython. | |
4 |
|
4 | |||
5 | All top-level applications should use the classes in this module for |
|
5 | All top-level applications should use the classes in this module for | |
6 | handling configuration and creating componenets. |
|
6 | handling configuration and creating componenets. | |
7 |
|
7 | |||
8 | The job of an :class:`Application` is to create the master configuration |
|
8 | The job of an :class:`Application` is to create the master configuration | |
9 | object and then create the configurable objects, passing the config to them. |
|
9 | object and then create the configurable objects, passing the config to them. | |
10 |
|
10 | |||
11 | Authors: |
|
11 | Authors: | |
12 |
|
12 | |||
13 | * Brian Granger |
|
13 | * Brian Granger | |
14 | * Fernando Perez |
|
14 | * Fernando Perez | |
15 |
|
15 | |||
16 | Notes |
|
16 | Notes | |
17 | ----- |
|
17 | ----- | |
18 | """ |
|
18 | """ | |
19 |
|
19 | |||
20 | #----------------------------------------------------------------------------- |
|
20 | #----------------------------------------------------------------------------- | |
21 | # Copyright (C) 2008-2009 The IPython Development Team |
|
21 | # Copyright (C) 2008-2009 The IPython Development Team | |
22 | # |
|
22 | # | |
23 | # Distributed under the terms of the BSD License. The full license is in |
|
23 | # Distributed under the terms of the BSD License. The full license is in | |
24 | # the file COPYING, distributed as part of this software. |
|
24 | # the file COPYING, distributed as part of this software. | |
25 | #----------------------------------------------------------------------------- |
|
25 | #----------------------------------------------------------------------------- | |
26 |
|
26 | |||
27 | #----------------------------------------------------------------------------- |
|
27 | #----------------------------------------------------------------------------- | |
28 | # Imports |
|
28 | # Imports | |
29 | #----------------------------------------------------------------------------- |
|
29 | #----------------------------------------------------------------------------- | |
30 |
|
30 | |||
31 | import os |
|
31 | import os | |
32 | import sys |
|
32 | import sys | |
33 |
|
33 | |||
34 | from IPython.config.application import Application |
|
34 | from IPython.config.application import Application | |
35 | from IPython.core import release, crashhandler |
|
35 | from IPython.core import release, crashhandler | |
36 | from IPython.utils.path import get_ipython_dir, get_ipython_package_dir |
|
36 | from IPython.utils.path import get_ipython_dir, get_ipython_package_dir | |
37 |
from IPython.utils.traitlets import |
|
37 | from IPython.utils.traitlets import List, Unicode, Type | |
38 |
|
38 | |||
39 | #----------------------------------------------------------------------------- |
|
39 | #----------------------------------------------------------------------------- | |
40 | # Classes and functions |
|
40 | # Classes and functions | |
41 | #----------------------------------------------------------------------------- |
|
41 | #----------------------------------------------------------------------------- | |
42 |
|
42 | |||
43 |
|
43 | |||
44 | class BaseIPythonApplication(Application): |
|
44 | class BaseIPythonApplication(Application): | |
45 |
|
45 | |||
46 | name = Unicode(u'ipython') |
|
46 | name = Unicode(u'ipython') | |
47 | description = Unicode(u'IPython: an enhanced interactive Python shell.') |
|
47 | description = Unicode(u'IPython: an enhanced interactive Python shell.') | |
48 | version = Unicode(release.version) |
|
48 | version = Unicode(release.version) | |
49 |
|
49 | |||
50 | # The name of the default config file. Track separately from the actual |
|
50 | # The name of the default config file. Track separately from the actual | |
51 | # name because some logic happens only if we aren't using the default. |
|
51 | # name because some logic happens only if we aren't using the default. | |
52 | default_config_file_name = Unicode(u'ipython_config.py') |
|
52 | default_config_file_name = Unicode(u'ipython_config.py') | |
53 |
|
53 | |||
54 | # The directory that contains IPython's builtin profiles. |
|
54 | # The directory that contains IPython's builtin profiles. | |
55 | builtin_profile_dir = Unicode( |
|
55 | builtin_profile_dir = Unicode( | |
56 | os.path.join(get_ipython_package_dir(), u'config', u'profile') |
|
56 | os.path.join(get_ipython_package_dir(), u'config', u'profile') | |
57 | ) |
|
57 | ) | |
58 |
|
58 | |||
59 |
config_file_paths = |
|
59 | config_file_paths = List(Unicode) | |
60 | def _config_file_paths_default(self): |
|
60 | def _config_file_paths_default(self): | |
61 |
return |
|
61 | return [os.getcwdu(), self.ipython_dir, self.builtin_profile_dir] | |
62 |
|
62 | |||
63 | profile_name = Unicode(u'', config=True, |
|
63 | profile_name = Unicode(u'', config=True, | |
64 | help="""The IPython profile to use.""" |
|
64 | help="""The IPython profile to use.""" | |
65 | ) |
|
65 | ) | |
66 |
|
66 | |||
67 | ipython_dir = Unicode(get_ipython_dir(), config=True, help= |
|
67 | ipython_dir = Unicode(get_ipython_dir(), config=True, help= | |
68 | """ |
|
68 | """ | |
69 | The name of the IPython directory. This directory is used for logging |
|
69 | The name of the IPython directory. This directory is used for logging | |
70 | configuration (through profiles), history storage, etc. The default |
|
70 | configuration (through profiles), history storage, etc. The default | |
71 | is usually $HOME/.ipython. This options can also be specified through |
|
71 | is usually $HOME/.ipython. This options can also be specified through | |
72 | the environment variable IPYTHON_DIR. |
|
72 | the environment variable IPYTHON_DIR. | |
73 | """ |
|
73 | """ | |
74 | ) |
|
74 | ) | |
75 |
|
75 | |||
76 | # The class to use as the crash handler. |
|
76 | # The class to use as the crash handler. | |
77 | crash_handler_class = Type(crashhandler.CrashHandler) |
|
77 | crash_handler_class = Type(crashhandler.CrashHandler) | |
78 |
|
78 | |||
79 | #------------------------------------------------------------------------- |
|
79 | #------------------------------------------------------------------------- | |
80 | # Various stages of Application creation |
|
80 | # Various stages of Application creation | |
81 | #------------------------------------------------------------------------- |
|
81 | #------------------------------------------------------------------------- | |
82 |
|
82 | |||
83 | def init_crash_handler(self): |
|
83 | def init_crash_handler(self): | |
84 | """Create a crash handler, typically setting sys.excepthook to it.""" |
|
84 | """Create a crash handler, typically setting sys.excepthook to it.""" | |
85 | self.crash_handler = self.crash_handler_class(self) |
|
85 | self.crash_handler = self.crash_handler_class(self) | |
86 | sys.excepthook = self.crash_handler |
|
86 | sys.excepthook = self.crash_handler | |
87 |
|
87 | |||
88 | def _ipython_dir_changed(self, name, old, new): |
|
88 | def _ipython_dir_changed(self, name, old, new): | |
89 | if old in sys.path: |
|
89 | if old in sys.path: | |
90 | sys.path.remove(old) |
|
90 | sys.path.remove(old) | |
91 | sys.path.append(os.path.abspath(new)) |
|
91 | sys.path.append(os.path.abspath(new)) | |
92 | if not os.path.isdir(new): |
|
92 | if not os.path.isdir(new): | |
93 | os.makedirs(new, mode=0777) |
|
93 | os.makedirs(new, mode=0777) | |
94 | self.config_file_paths = (os.getcwdu(), new, self.builtin_profile_dir) |
|
94 | self.config_file_paths = (os.getcwdu(), new, self.builtin_profile_dir) | |
95 | self.log.debug("IPYTHON_DIR set to: %s" % new) |
|
95 | self.log.debug("IPYTHON_DIR set to: %s" % new) | |
96 |
|
96 | |||
97 | @property |
|
97 | @property | |
98 | def config_file_name(self): |
|
98 | def config_file_name(self): | |
99 | """Find the config file name for this application.""" |
|
99 | """Find the config file name for this application.""" | |
100 | if self.profile_name: |
|
100 | if self.profile_name: | |
101 | name_parts = self.default_config_file_name.split('.') |
|
101 | name_parts = self.default_config_file_name.split('.') | |
102 | name_parts.insert(1, u'_' + self.profile_name + u'.') |
|
102 | name_parts.insert(1, u'_' + self.profile_name + u'.') | |
103 | return ''.join(name_parts) |
|
103 | return ''.join(name_parts) | |
104 | else: |
|
104 | else: | |
105 | return self.default_config_file_name |
|
105 | return self.default_config_file_name | |
106 |
|
106 | |||
107 | def load_config_file(self, suppress_errors=True): |
|
107 | def load_config_file(self, suppress_errors=True): | |
108 | """Load the config file. |
|
108 | """Load the config file. | |
109 |
|
109 | |||
110 | By default, errors in loading config are handled, and a warning |
|
110 | By default, errors in loading config are handled, and a warning | |
111 | printed on screen. For testing, the suppress_errors option is set |
|
111 | printed on screen. For testing, the suppress_errors option is set | |
112 | to False, so errors will make tests fail. |
|
112 | to False, so errors will make tests fail. | |
113 | """ |
|
113 | """ | |
114 | self.log.debug("Attempting to load config file: %s" % |
|
114 | self.log.debug("Attempting to load config file: %s" % | |
115 | self.config_file_name) |
|
115 | self.config_file_name) | |
116 | try: |
|
116 | try: | |
117 | Application.load_config_file( |
|
117 | Application.load_config_file( | |
118 | self, |
|
118 | self, | |
119 | self.config_file_name, |
|
119 | self.config_file_name, | |
120 | path=self.config_file_paths |
|
120 | path=self.config_file_paths | |
121 | ) |
|
121 | ) | |
122 | except IOError: |
|
122 | except IOError: | |
123 | # Only warn if the default config file was NOT being used. |
|
123 | # Only warn if the default config file was NOT being used. | |
124 | if not self.config_file_name == self.default_config_file_name: |
|
124 | if not self.config_file_name == self.default_config_file_name: | |
125 | self.log.warn("Config file not found, skipping: %s" % |
|
125 | self.log.warn("Config file not found, skipping: %s" % | |
126 | self.config_file_name, exc_info=True) |
|
126 | self.config_file_name, exc_info=True) | |
127 | except: |
|
127 | except: | |
128 | # For testing purposes. |
|
128 | # For testing purposes. | |
129 | if not suppress_errors: |
|
129 | if not suppress_errors: | |
130 | raise |
|
130 | raise | |
131 | self.log.warn("Error loading config file: %s" % |
|
131 | self.log.warn("Error loading config file: %s" % | |
132 | self.config_file_name, exc_info=True) |
|
132 | self.config_file_name, exc_info=True) | |
133 |
|
133 |
General Comments 0
You need to be logged in to leave comments.
Login now