|
@@
-1,106
+1,102
b''
|
|
1
|
1
|
# encoding: utf-8
|
|
2
|
2
|
|
|
3
|
3
|
"""This is the official entry point to IPython's configuration system. """
|
|
4
|
4
|
|
|
5
|
5
|
__docformat__ = "restructuredtext en"
|
|
6
|
6
|
|
|
7
|
7
|
#-------------------------------------------------------------------------------
|
|
8
|
8
|
# Copyright (C) 2008 The IPython Development Team
|
|
9
|
9
|
#
|
|
10
|
10
|
# Distributed under the terms of the BSD License. The full license is in
|
|
11
|
11
|
# the file COPYING, distributed as part of this software.
|
|
12
|
12
|
#-------------------------------------------------------------------------------
|
|
13
|
13
|
|
|
14
|
14
|
#-------------------------------------------------------------------------------
|
|
15
|
15
|
# Imports
|
|
16
|
16
|
#-------------------------------------------------------------------------------
|
|
17
|
17
|
|
|
18
|
18
|
import os
|
|
19
|
19
|
from os.path import join as pjoin
|
|
20
|
20
|
|
|
21
|
21
|
from IPython.genutils import get_home_dir, get_ipython_dir
|
|
22
|
22
|
from IPython.external.configobj import ConfigObj
|
|
23
|
23
|
|
|
24
|
|
# Traitlets config imports
|
|
25
|
|
# from IPython.config import traitlets
|
|
26
|
|
# from IPython.config.config import *
|
|
27
|
|
# from traitlets import *
|
|
28
|
24
|
|
|
29
|
25
|
class ConfigObjManager(object):
|
|
30
|
26
|
|
|
31
|
27
|
def __init__(self, configObj, filename):
|
|
32
|
28
|
self.current = configObj
|
|
33
|
29
|
self.current.indent_type = ' '
|
|
34
|
30
|
self.filename = filename
|
|
35
|
31
|
# self.write_default_config_file()
|
|
36
|
32
|
|
|
37
|
33
|
def get_config_obj(self):
|
|
38
|
34
|
return self.current
|
|
39
|
35
|
|
|
40
|
36
|
def update_config_obj(self, newConfig):
|
|
41
|
37
|
self.current.merge(newConfig)
|
|
42
|
38
|
|
|
43
|
39
|
def update_config_obj_from_file(self, filename):
|
|
44
|
40
|
newConfig = ConfigObj(filename, file_error=False)
|
|
45
|
41
|
self.current.merge(newConfig)
|
|
46
|
42
|
|
|
47
|
43
|
def update_config_obj_from_default_file(self, ipythondir=None):
|
|
48
|
44
|
fname = self.resolve_file_path(self.filename, ipythondir)
|
|
49
|
45
|
self.update_config_obj_from_file(fname)
|
|
50
|
46
|
|
|
51
|
47
|
def write_config_obj_to_file(self, filename):
|
|
52
|
48
|
f = open(filename, 'w')
|
|
53
|
49
|
self.current.write(f)
|
|
54
|
50
|
f.close()
|
|
55
|
51
|
|
|
56
|
52
|
def write_default_config_file(self):
|
|
57
|
53
|
ipdir = get_ipython_dir()
|
|
58
|
54
|
fname = pjoin(ipdir, self.filename)
|
|
59
|
55
|
if not os.path.isfile(fname):
|
|
60
|
56
|
print "Writing the configuration file to: " + fname
|
|
61
|
57
|
self.write_config_obj_to_file(fname)
|
|
62
|
58
|
|
|
63
|
59
|
def _import(self, key):
|
|
64
|
60
|
package = '.'.join(key.split('.')[0:-1])
|
|
65
|
61
|
obj = key.split('.')[-1]
|
|
66
|
62
|
execString = 'from %s import %s' % (package, obj)
|
|
67
|
63
|
exec execString
|
|
68
|
64
|
exec 'temp = %s' % obj
|
|
69
|
65
|
return temp
|
|
70
|
66
|
|
|
71
|
67
|
def resolve_file_path(self, filename, ipythondir = None):
|
|
72
|
68
|
"""Resolve filenames into absolute paths.
|
|
73
|
69
|
|
|
74
|
70
|
This function looks in the following directories in order:
|
|
75
|
71
|
|
|
76
|
72
|
1. In the current working directory or by absolute path with ~ expanded
|
|
77
|
73
|
2. In ipythondir if that is set
|
|
78
|
74
|
3. In the IPYTHONDIR environment variable if it exists
|
|
79
|
75
|
4. In the ~/.ipython directory
|
|
80
|
76
|
|
|
81
|
77
|
Note: The IPYTHONDIR is also used by the trunk version of IPython so
|
|
82
|
78
|
changing it will also affect it was well.
|
|
83
|
79
|
"""
|
|
84
|
80
|
|
|
85
|
81
|
# In cwd or by absolute path with ~ expanded
|
|
86
|
82
|
trythis = os.path.expanduser(filename)
|
|
87
|
83
|
if os.path.isfile(trythis):
|
|
88
|
84
|
return trythis
|
|
89
|
85
|
|
|
90
|
86
|
# In ipythondir if it is set
|
|
91
|
87
|
if ipythondir is not None:
|
|
92
|
88
|
trythis = pjoin(ipythondir, filename)
|
|
93
|
89
|
if os.path.isfile(trythis):
|
|
94
|
90
|
return trythis
|
|
95
|
91
|
|
|
96
|
92
|
trythis = pjoin(get_ipython_dir(), filename)
|
|
97
|
93
|
if os.path.isfile(trythis):
|
|
98
|
94
|
return trythis
|
|
99
|
95
|
|
|
100
|
96
|
return None
|
|
101
|
97
|
|
|
102
|
98
|
|
|
103
|
99
|
|
|
104
|
100
|
|
|
105
|
101
|
|
|
106
|
102
|
|