Mercurial > hg > martINI
comparison martini/tests/test_config.txt @ 0:3c3522ce6e3a
initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
author | k0s <k0scist@gmail.com> |
---|---|
date | Tue, 08 Dec 2009 15:13:28 -0500 |
parents | |
children | 09bed87f7fa4 |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:3c3522ce6e3a |
---|---|
1 Test martini.config | |
2 ===================== | |
3 | |
4 The obligatory imports: | |
5 | |
6 >>> import sys | |
7 >>> from pprint import pprint | |
8 >>> from martini.config import ConfigMunger | |
9 >>> from ConfigParser import InterpolationMissingOptionError | |
10 >>> from StringIO import StringIO | |
11 | |
12 Make the munger and some .ini files: | |
13 | |
14 >>> munger = ConfigMunger() | |
15 >>> foo = StringIO("[foo]\nbar = fleem") | |
16 | |
17 Test basic reading + writing functionality: | |
18 | |
19 >>> munger.read(foo) | |
20 >>> munger.write(sys.stdout) | |
21 [foo] | |
22 bar = fleem | |
23 | |
24 Test overriding: | |
25 | |
26 >>> munger.read("[foo]\nblah = florg\nbar=thorg") | |
27 >>> munger.write(sys.stdout) | |
28 [foo] | |
29 bar = thorg | |
30 blah = florg | |
31 | |
32 Test variable interpolation: | |
33 | |
34 >>> munger.read("[variable]\nvar=%(value)s") | |
35 >>> buffer = StringIO() | |
36 >>> try: | |
37 ... munger.write(buffer) | |
38 ... except InterpolationMissingOptionError, e: | |
39 ... print e.reference | |
40 value | |
41 >>> munger.write(sys.stdout, vars=dict(value='interpolated')) | |
42 [foo] | |
43 bar = thorg | |
44 blah = florg | |
45 <BLANKLINE> | |
46 [variable] | |
47 var = interpolated | |
48 | |
49 Testing dict-like functionality: | |
50 | |
51 >>> munger = ConfigMunger() | |
52 >>> munger.read("[foo]\nblah = florg\nbar=thorg") | |
53 >>> munger['foo']['bar'] | |
54 'thorg' | |
55 >>> pprint(munger.dict()) | |
56 {'foo': {'bar': 'thorg', 'blah': 'florg'}} | |
57 >>> pprint(munger['foo']) | |
58 {'bar': 'thorg', 'blah': 'florg'} | |
59 | |
60 You can read from a dictionary too: | |
61 | |
62 >>> munger = ConfigMunger() | |
63 >>> munger.read({'foo': {'bar': 'baz'}}) | |
64 >>> munger['foo']['bar'] | |
65 'baz' | |
66 >>> munger.write(sys.stdout) | |
67 [foo] | |
68 bar = baz | |
69 | |
70 Test the ability to parse sectionless .ini files: | |
71 | |
72 >>> munger = ConfigMunger() | |
73 >>> munger.read("foo = bar\nbaz=fleem") | |
74 >>> munger.sections() | |
75 ['DEFAULTS'] | |
76 >>> munger['DEFAULTS'] | |
77 {'foo': 'bar', 'baz': 'fleem'} | |
78 >>> munger.write(sys.stdout) | |
79 [DEFAULTS] | |
80 baz = fleem | |
81 foo = bar | |
82 >>> munger = ConfigMunger() | |
83 >>> munger.read("foo = bar\n\n[foo]\nbaz=fleem") | |
84 >>> sorted(munger.sections()) | |
85 ['DEFAULTS', 'foo'] | |
86 >>> munger['DEFAULTS']['foo'] | |
87 'bar' | |
88 >>> munger['foo']['baz'] | |
89 'fleem' | |
90 | |
91 Move a secton around: | |
92 | |
93 >>> munger.rename_section('foo', 'oof') | |
94 >>> sorted(munger.sections()) | |
95 ['DEFAULTS', 'oof'] | |
96 >>> munger['oof'] | |
97 {'baz': 'fleem'} | |
98 | |
99 Test the ability to parse multi-line .ini files [TODO]: | |
100 | |
101 >>> munger = ConfigMunger() | |
102 >>> # munger.read("[Jeff Hammel]\naddress = 639 W. 173 St.\nApt. 11D\nNew York, NY 10032") |