Mercurial > hg > martINI
annotate martini/config.py @ 7:7c8f23eae311
discriminate on case
author | Jeff Hammel <jhammel@mozilla.com> |
---|---|
date | Wed, 24 Nov 2010 10:55:59 -0800 |
parents | 09bed87f7fa4 |
children | 81aed4352851 |
rev | line source |
---|---|
0
3c3522ce6e3a
initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
1 #!/usr/bin/env python |
3c3522ce6e3a
initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
2 |
3c3522ce6e3a
initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
3 import os |
3c3522ce6e3a
initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
4 import sys |
3c3522ce6e3a
initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
5 import urllib2 |
3c3522ce6e3a
initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
6 |
3c3522ce6e3a
initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
7 from ConfigParser import ConfigParser |
3c3522ce6e3a
initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
8 from ConfigParser import InterpolationMissingOptionError |
3c3522ce6e3a
initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
9 from ConfigParser import MissingSectionHeaderError |
3c3522ce6e3a
initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
10 from ConfigParser import NoOptionError |
3c3522ce6e3a
initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
11 from StringIO import StringIO |
3c3522ce6e3a
initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
12 |
3c3522ce6e3a
initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
13 def file_pointer(resource): |
3c3522ce6e3a
initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
14 """returns a file-like object given a string""" |
3c3522ce6e3a
initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
15 # XXX could go in utils.py |
3c3522ce6e3a
initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
16 |
3c3522ce6e3a
initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
17 if not isinstance(resource, basestring): |
3c3522ce6e3a
initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
18 # assume resource is already a file-like object |
3c3522ce6e3a
initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
19 return resource |
3c3522ce6e3a
initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
20 |
3c3522ce6e3a
initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
21 if os.path.exists(resource): |
3c3522ce6e3a
initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
22 return file(resource) |
3c3522ce6e3a
initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
23 if sum([resource.startswith(http) for http in 'http://', 'https://']): |
3c3522ce6e3a
initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
24 return urllib2.urlopen(resource) |
3c3522ce6e3a
initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
25 return StringIO(resource) |
3c3522ce6e3a
initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
26 |
3c3522ce6e3a
initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
27 |
3c3522ce6e3a
initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
28 class ConfigMunger(ConfigParser): |
3c3522ce6e3a
initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
29 """combine configuration from .ini files""" |
3c3522ce6e3a
initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
30 |
3c3522ce6e3a
initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
31 def __init__(self, *conf, **kw): |
3c3522ce6e3a
initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
32 ConfigParser.__init__(self, kw.get('defaults',{})) |
7 | 33 self.optionxform = str |
0
3c3522ce6e3a
initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
34 self.read(*conf) |
3c3522ce6e3a
initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
35 |
3c3522ce6e3a
initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
36 def __getitem__(self, section): |
3c3522ce6e3a
initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
37 """ |
3c3522ce6e3a
initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
38 return an object with __getitem__ defined appropriately |
3c3522ce6e3a
initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
39 to allow referencing like self['foo']['bar'] |
3c3522ce6e3a
initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
40 """ |
3c3522ce6e3a
initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
41 return dict(self.items(section)) |
3c3522ce6e3a
initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
42 |
3c3522ce6e3a
initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
43 def get(self, section, option, default=None, raw=False, vars=None): |
3c3522ce6e3a
initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
44 try: |
3c3522ce6e3a
initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
45 value = ConfigParser.get(self, section, option, raw, vars) |
3c3522ce6e3a
initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
46 except NoOptionError: |
3c3522ce6e3a
initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
47 return default |
3c3522ce6e3a
initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
48 return value |
3c3522ce6e3a
initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
49 |
3c3522ce6e3a
initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
50 def set(self, section, option, value): |
3c3522ce6e3a
initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
51 if section not in self.sections(): |
3c3522ce6e3a
initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
52 self.add_section(section) |
3c3522ce6e3a
initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
53 ConfigParser.set(self, section, option, value) |
3c3522ce6e3a
initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
54 |
3c3522ce6e3a
initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
55 def move_section(self, section, newname): |
3c3522ce6e3a
initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
56 if self.has_section(section): |
3c3522ce6e3a
initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
57 _section = self[section] |
3c3522ce6e3a
initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
58 self.remove_section(section) |
3c3522ce6e3a
initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
59 else: |
3c3522ce6e3a
initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
60 _section = {} |
3c3522ce6e3a
initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
61 self.read({newname: _section}) |
3c3522ce6e3a
initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
62 |
3c3522ce6e3a
initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
63 def dict(self): |
3c3522ce6e3a
initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
64 """return a dictionary of dictionaries; |
3c3522ce6e3a
initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
65 the outer with keys of section names; |
3c3522ce6e3a
initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
66 the inner with keys, values of the section""" |
3c3522ce6e3a
initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
67 return dict([(section, self[section]) |
3c3522ce6e3a
initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
68 for section in self.sections()]) |
3c3522ce6e3a
initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
69 |
3c3522ce6e3a
initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
70 def read(self, *ini): |
3c3522ce6e3a
initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
71 for _ini in ini: |
3c3522ce6e3a
initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
72 if isinstance(_ini, dict): |
3c3522ce6e3a
initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
73 for section, contents in _ini.items(): |
3c3522ce6e3a
initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
74 for option, value in contents.items(): |
3c3522ce6e3a
initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
75 self.set(section, option, value) |
3c3522ce6e3a
initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
76 elif isinstance(_ini, list) or isinstance(_ini, tuple): |
3c3522ce6e3a
initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
77 |
3c3522ce6e3a
initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
78 # ensure list or tuple of 3-tuples |
3c3522ce6e3a
initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
79 assert len([option for option in _ini |
3c3522ce6e3a
initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
80 if isinstance(option, tuple) |
3c3522ce6e3a
initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
81 and len(option) == 3]) |
3c3522ce6e3a
initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
82 |
3c3522ce6e3a
initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
83 for section, option, value in _ini: |
3c3522ce6e3a
initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
84 self.set(section, option, value) |
3c3522ce6e3a
initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
85 else: |
3c3522ce6e3a
initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
86 fp = file_pointer(_ini) |
3c3522ce6e3a
initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
87 try: |
3c3522ce6e3a
initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
88 self.readfp(fp) |
3c3522ce6e3a
initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
89 except MissingSectionHeaderError: |
3c3522ce6e3a
initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
90 fp.seek(0) |
3c3522ce6e3a
initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
91 fp = StringIO("[DEFAULTS]\n" + fp.read()) |
3c3522ce6e3a
initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
92 self.readfp(fp) |
3c3522ce6e3a
initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
93 |
3c3522ce6e3a
initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
94 def missing(self): |
3c3522ce6e3a
initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
95 """returns missing variable names""" |
3c3522ce6e3a
initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
96 missing = set() |
3c3522ce6e3a
initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
97 |
3c3522ce6e3a
initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
98 for section in self.sections(): |
3c3522ce6e3a
initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
99 for key, val in self.items(section, raw=True): |
3c3522ce6e3a
initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
100 try: |
3c3522ce6e3a
initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
101 self.get(section, key) |
3c3522ce6e3a
initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
102 except InterpolationMissingOptionError, e: |
3c3522ce6e3a
initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
103 missing.add(e.reference) |
3c3522ce6e3a
initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
104 return missing |
3c3522ce6e3a
initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
105 |
3c3522ce6e3a
initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
106 def tuples(self): |
3c3522ce6e3a
initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
107 """ |
3c3522ce6e3a
initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
108 return options in format appropriate to trac: |
3c3522ce6e3a
initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
109 [ (section, option, value) ] |
3c3522ce6e3a
initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
110 """ |
3c3522ce6e3a
initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
111 options = [] |
3c3522ce6e3a
initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
112 for section in self.sections(): |
3c3522ce6e3a
initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
113 options.extend([(section,) + item |
3c3522ce6e3a
initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
114 for item in self.items(section)]) |
3c3522ce6e3a
initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
115 return options |
3c3522ce6e3a
initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
116 |
3c3522ce6e3a
initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
117 def write(self, fp=sys.stdout, raw=False, sorted=True, vars=None): |
3c3522ce6e3a
initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
118 sections = self.sections() |
3c3522ce6e3a
initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
119 if sorted: |
3c3522ce6e3a
initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
120 sections.sort() |
3c3522ce6e3a
initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
121 |
3c3522ce6e3a
initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
122 for section in sections: |
3c3522ce6e3a
initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
123 print >> fp, '[%s]' % section |
3c3522ce6e3a
initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
124 options = self.options(section) |
3c3522ce6e3a
initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
125 if sorted: |
3c3522ce6e3a
initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
126 options.sort() |
3c3522ce6e3a
initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
127 for option in options: |
6 | 128 print >> fp, "%s = %s" % (option, self.get(section, option, raw=raw, vars=vars)) |
0
3c3522ce6e3a
initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
129 if section != sections[-1]: |
3c3522ce6e3a
initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
130 print >> fp |
3c3522ce6e3a
initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
131 |
3c3522ce6e3a
initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
132 if __name__ == '__main__': |
3c3522ce6e3a
initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
133 import sys |
3c3522ce6e3a
initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
134 from optparse import OptionParser |
3c3522ce6e3a
initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
135 parser = OptionParser() |
3c3522ce6e3a
initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
136 parser.add_option('--missing', action="store_true", default=False, |
3c3522ce6e3a
initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
137 help="list missing template variables") |
3c3522ce6e3a
initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
138 munger = ConfigMunger() |
3c3522ce6e3a
initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
139 options, args = parser.parse_args() |
3c3522ce6e3a
initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
140 munger.read(*args) |
3c3522ce6e3a
initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
141 if options.missing: |
3c3522ce6e3a
initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
142 for missing in munger.missing(): |
3c3522ce6e3a
initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
143 print missing |
3c3522ce6e3a
initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
144 else: |
3c3522ce6e3a
initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff
changeset
|
145 munger.write(sys.stdout) |