annotate martini/config.py @ 17:a42a02bb46ed

py3
author Jeff Hammel <k0scist@gmail.com>
date Mon, 20 Feb 2017 15:11:21 -0800
parents 8ae3a7fd466a
children 81e0743dbe53
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
16
Jeff Hammel <k0scist@gmail.com>
parents: 15
diff changeset
6
9
77c7556fa8e0 well, dammit
Jeff Hammel <k0scist@gmail.com>
parents: 8
diff changeset
7 try:
77c7556fa8e0 well, dammit
Jeff Hammel <k0scist@gmail.com>
parents: 8
diff changeset
8 from collections import OrderedDict
77c7556fa8e0 well, dammit
Jeff Hammel <k0scist@gmail.com>
parents: 8
diff changeset
9 except ImportError:
77c7556fa8e0 well, dammit
Jeff Hammel <k0scist@gmail.com>
parents: 8
diff changeset
10 from odict import OrderedDict
77c7556fa8e0 well, dammit
Jeff Hammel <k0scist@gmail.com>
parents: 8
diff changeset
11
16
Jeff Hammel <k0scist@gmail.com>
parents: 15
diff changeset
12 try:
Jeff Hammel <k0scist@gmail.com>
parents: 15
diff changeset
13 # python 2
Jeff Hammel <k0scist@gmail.com>
parents: 15
diff changeset
14 from urllib2 import urlopen
Jeff Hammel <k0scist@gmail.com>
parents: 15
diff changeset
15 from ConfigParser import ConfigParser
Jeff Hammel <k0scist@gmail.com>
parents: 15
diff changeset
16 from ConfigParser import InterpolationMissingOptionError
Jeff Hammel <k0scist@gmail.com>
parents: 15
diff changeset
17 from ConfigParser import MissingSectionHeaderError
Jeff Hammel <k0scist@gmail.com>
parents: 15
diff changeset
18 from ConfigParser import NoOptionError
Jeff Hammel <k0scist@gmail.com>
parents: 15
diff changeset
19 from StringIO import StringIO
Jeff Hammel <k0scist@gmail.com>
parents: 15
diff changeset
20
Jeff Hammel <k0scist@gmail.com>
parents: 15
diff changeset
21 except ImportError:
Jeff Hammel <k0scist@gmail.com>
parents: 15
diff changeset
22 # python 3
Jeff Hammel <k0scist@gmail.com>
parents: 15
diff changeset
23 from urllib.request import urlopen
Jeff Hammel <k0scist@gmail.com>
parents: 15
diff changeset
24 from configparser import ConfigParser
Jeff Hammel <k0scist@gmail.com>
parents: 15
diff changeset
25 from configparser import InterpolationMissingOptionError
Jeff Hammel <k0scist@gmail.com>
parents: 15
diff changeset
26 from configparser import MissingSectionHeaderError
Jeff Hammel <k0scist@gmail.com>
parents: 15
diff changeset
27 from configparser import NoOptionError
Jeff Hammel <k0scist@gmail.com>
parents: 15
diff changeset
28 from io import StringIO
Jeff Hammel <k0scist@gmail.com>
parents: 15
diff changeset
29
9
77c7556fa8e0 well, dammit
Jeff Hammel <k0scist@gmail.com>
parents: 8
diff changeset
30
17
Jeff Hammel <k0scist@gmail.com>
parents: 16
diff changeset
31 try:
Jeff Hammel <k0scist@gmail.com>
parents: 16
diff changeset
32 # python 2
Jeff Hammel <k0scist@gmail.com>
parents: 16
diff changeset
33 string = (str, unicode)
Jeff Hammel <k0scist@gmail.com>
parents: 16
diff changeset
34 except:
Jeff Hammel <k0scist@gmail.com>
parents: 16
diff changeset
35 # python 3
Jeff Hammel <k0scist@gmail.com>
parents: 16
diff changeset
36 string = (str,)
Jeff Hammel <k0scist@gmail.com>
parents: 16
diff changeset
37
Jeff Hammel <k0scist@gmail.com>
parents: 16
diff changeset
38
0
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
39 def file_pointer(resource):
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
40 """returns a file-like object given a string"""
9
77c7556fa8e0 well, dammit
Jeff Hammel <k0scist@gmail.com>
parents: 8
diff changeset
41 # XXX could go in utils.py
0
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
42
17
Jeff Hammel <k0scist@gmail.com>
parents: 16
diff changeset
43 if not isinstance(resource, string):
0
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
44 # 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
45 return resource
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
46
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
47 if os.path.exists(resource):
17
Jeff Hammel <k0scist@gmail.com>
parents: 16
diff changeset
48 return open(resource)
11
c77259f467a3 hopefully OrderedDict preservation actually works now
Jeff Hammel <k0scist@gmail.com>
parents: 10
diff changeset
49 if sum([resource.startswith(http)
12
Jeff Hammel <k0scist@gmail.com>
parents: 11
diff changeset
50 for http in ('http://', 'https://')]):
16
Jeff Hammel <k0scist@gmail.com>
parents: 15
diff changeset
51 return urlopen(resource)
0
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
52 return StringIO(resource)
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
53
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 class ConfigMunger(ConfigParser):
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
56 """combine configuration from .ini files"""
10
66d11212175d this should be ordered too
Jeff Hammel <k0scist@gmail.com>
parents: 9
diff changeset
57
0
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
58 def __init__(self, *conf, **kw):
8
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents: 7
diff changeset
59 ConfigParser.__init__(self, defaults=kw.get('defaults',{}),
81aed4352851 make martini work with an ordered dictionary
Jeff Hammel <jhammel@mozilla.com>
parents: 7
diff changeset
60 dict_type=OrderedDict)
7
7c8f23eae311 discriminate on case
Jeff Hammel <jhammel@mozilla.com>
parents: 6
diff changeset
61 self.optionxform = str
0
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
62 self.read(*conf)
10
66d11212175d this should be ordered too
Jeff Hammel <k0scist@gmail.com>
parents: 9
diff changeset
63
0
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
64 def __getitem__(self, section):
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
65 """
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
66 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
67 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
68 """
10
66d11212175d this should be ordered too
Jeff Hammel <k0scist@gmail.com>
parents: 9
diff changeset
69 return OrderedDict(self.items(section))
0
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
70
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
71 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
72 try:
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
73 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
74 except NoOptionError:
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
75 return default
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
76 return value
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 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
79 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
80 self.add_section(section)
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
81 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
82
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
83 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
84 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
85 _section = self[section]
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
86 self.remove_section(section)
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
87 else:
11
c77259f467a3 hopefully OrderedDict preservation actually works now
Jeff Hammel <k0scist@gmail.com>
parents: 10
diff changeset
88 _section = OrderedDict()
c77259f467a3 hopefully OrderedDict preservation actually works now
Jeff Hammel <k0scist@gmail.com>
parents: 10
diff changeset
89 self.read(OrderedDict(newname=_section))
9
77c7556fa8e0 well, dammit
Jeff Hammel <k0scist@gmail.com>
parents: 8
diff changeset
90
0
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
91 def dict(self):
11
c77259f467a3 hopefully OrderedDict preservation actually works now
Jeff Hammel <k0scist@gmail.com>
parents: 10
diff changeset
92 """
c77259f467a3 hopefully OrderedDict preservation actually works now
Jeff Hammel <k0scist@gmail.com>
parents: 10
diff changeset
93 return a dictionary of dictionaries:
0
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
94 the outer with keys of section names;
11
c77259f467a3 hopefully OrderedDict preservation actually works now
Jeff Hammel <k0scist@gmail.com>
parents: 10
diff changeset
95 the inner with keys, values of the section
c77259f467a3 hopefully OrderedDict preservation actually works now
Jeff Hammel <k0scist@gmail.com>
parents: 10
diff changeset
96 """
9
77c7556fa8e0 well, dammit
Jeff Hammel <k0scist@gmail.com>
parents: 8
diff changeset
97 return OrderedDict([(section, self[section])
77c7556fa8e0 well, dammit
Jeff Hammel <k0scist@gmail.com>
parents: 8
diff changeset
98 for section in self.sections()])
0
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
99
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
100 def read(self, *ini):
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
101 for _ini in ini:
9
77c7556fa8e0 well, dammit
Jeff Hammel <k0scist@gmail.com>
parents: 8
diff changeset
102 if isinstance(_ini, (dict, OrderedDict)):
0
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
103 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
104 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
105 self.set(section, option, value)
9
77c7556fa8e0 well, dammit
Jeff Hammel <k0scist@gmail.com>
parents: 8
diff changeset
106 elif isinstance(_ini, (list, tuple)):
0
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 # 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
109 assert len([option for option in _ini
9
77c7556fa8e0 well, dammit
Jeff Hammel <k0scist@gmail.com>
parents: 8
diff changeset
110 if isinstance(option, tuple)
0
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
111 and len(option) == 3])
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
112
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
113 for section, option, value in _ini:
9
77c7556fa8e0 well, dammit
Jeff Hammel <k0scist@gmail.com>
parents: 8
diff changeset
114 self.set(section, option, value)
0
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
115 else:
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
116 fp = file_pointer(_ini)
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
117 try:
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
118 self.readfp(fp)
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
119 except MissingSectionHeaderError:
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
120 fp.seek(0)
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
121 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
122 self.readfp(fp)
9
77c7556fa8e0 well, dammit
Jeff Hammel <k0scist@gmail.com>
parents: 8
diff changeset
123
0
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
124 def missing(self):
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
125 """returns missing variable names"""
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
126
9
77c7556fa8e0 well, dammit
Jeff Hammel <k0scist@gmail.com>
parents: 8
diff changeset
127 missing = set()
0
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
128 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
129 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
130 try:
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
131 self.get(section, key)
14
Jeff Hammel <k0scist@gmail.com>
parents: 12
diff changeset
132 except InterpolationMissingOptionError as e:
0
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
133 missing.add(e.reference)
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
134 return missing
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
135
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
136 def tuples(self):
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
137 """
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
138 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
139 [ (section, option, value) ]
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
140 """
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
141 options = []
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
142 for section in self.sections():
9
77c7556fa8e0 well, dammit
Jeff Hammel <k0scist@gmail.com>
parents: 8
diff changeset
143 options.extend([(section,) + item
0
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
144 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
145 return options
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
146
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
147 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
148 sections = self.sections()
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
149 if sorted:
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
150 sections.sort()
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
151
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
152 for section in sections:
15
Jeff Hammel <k0scist@gmail.com>
parents: 14
diff changeset
153 fp.write('[%s]\n' % section)
0
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
154 options = self.options(section)
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
155 if sorted:
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
156 options.sort()
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
157 for option in options:
15
Jeff Hammel <k0scist@gmail.com>
parents: 14
diff changeset
158 fp.write("%s = %s\n" % (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
159 if section != sections[-1]:
15
Jeff Hammel <k0scist@gmail.com>
parents: 14
diff changeset
160 fp.write('\n')
0
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
161
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
162 if __name__ == '__main__':
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
163 import sys
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
164 from optparse import OptionParser
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
165 parser = OptionParser()
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
166 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
167 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
168 munger = ConfigMunger()
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
169 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
170 munger.read(*args)
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
171 if options.missing:
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
172 for missing in munger.missing():
15
Jeff Hammel <k0scist@gmail.com>
parents: 14
diff changeset
173 print(missing)
0
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
174 else:
3c3522ce6e3a initial import of martINI from https://svn.openplans.org/svn/standalone/martINI/
k0s <k0scist@gmail.com>
parents:
diff changeset
175 munger.write(sys.stdout)