annotate martini/config.py @ 16:8ae3a7fd466a

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