forked from ipython/ipython
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_config.py
135 lines (112 loc) · 2.47 KB
/
test_config.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
"""
# Test utilities
>>> import os
>>> def dict_as_sorted_list(d):
... for k in d:
... if isinstance(d[k],dict):
... d[k] = dict_as_sorted_list(d[k])
... return sorted(d.items())
>>> def pprint(d,level=0):
... if isinstance(d,dict):
... d = dict_as_sorted_list(d)
... for item,value in d:
... if isinstance(value,list):
... print "%s%s" % (' '*level, item)
... pprint(value,level+2)
... else:
... print "%s%s: %s" % (' '*level, item, value)
# Tests
>>> from IPython.config.api import *
>>> from sample_config import *
>>> s = Sample()
>>> print s.my_float
3.0
>>> s.my_float = 4
>>> print s.my_float
4.0
>>> print type(s.my_float)
<type 'float'>
>>> s.SubSample.SubSubSample.my_int = 5.0
>>> print s.SubSample.SubSubSample.my_int
5
>>> i = ConfigInspector(s)
>>> print i.properties
[('my_choice', 'a'), ('my_float', 4.0)]
>>> print tuple(s for s,v in i.subconfigs)
('MiddleSection', 'SubSample')
>>> print s
my_float = 4.0
<BLANKLINE>
[SubSample]
<BLANKLINE>
[[SubSubSample]]
my_int = 5
<BLANKLINE>
>>> import tempfile
>>> fn = tempfile.mktemp()
>>> f = open(fn,'w')
>>> f.write(str(s))
>>> f.close()
>>> s += fn
>>> from IPython.external.configobj import ConfigObj
>>> c = ConfigObj(fn)
>>> c['SubSample']['subsample_uri'] = 'http://ipython.scipy.org'
>>> s += c
>>> print s
my_float = 4.0
<BLANKLINE>
[SubSample]
subsample_uri = 'http://ipython.scipy.org'
<BLANKLINE>
[[SubSubSample]]
my_int = 5
<BLANKLINE>
>>> pprint(dict_from_config(s,only_modified=False))
MiddleSection
left_alone: '1'
unknown_mod: 'asd'
SubSample
SubSubSample
my_int: 5
subsample_uri: 'http://ipython.scipy.org'
my_choice: 'a'
my_float: 4.0
>>> pprint(dict_from_config(s))
SubSample
SubSubSample
my_int: 5
subsample_uri: 'http://ipython.scipy.org'
my_float: 4.0
Test roundtripping:
>>> fn = tempfile.mktemp()
>>> f = open(fn, 'w')
>>> f.write('''
... [MiddleSection]
... # some comment here
... left_alone = 'c'
... ''')
>>> f.close()
>>> s += fn
>>> pprint(dict_from_config(s))
MiddleSection
left_alone: 'c'
SubSample
SubSubSample
my_int: 5
subsample_uri: 'http://ipython.scipy.org'
my_float: 4.0
>>> write(s, fn)
>>> f = file(fn,'r')
>>> ConfigInspector(s).reset()
>>> pprint(dict_from_config(s))
>>> s += fn
>>> os.unlink(fn)
>>> pprint(dict_from_config(s))
MiddleSection
left_alone: 'c'
SubSample
SubSubSample
my_int: 5
subsample_uri: 'http://ipython.scipy.org'
my_float: 4.0
"""