Mercurial > hg > martINI
comparison martini/config.py @ 9:77c7556fa8e0
well, dammit
author | Jeff Hammel <k0scist@gmail.com> |
---|---|
date | Tue, 23 Aug 2016 09:11:49 -0700 |
parents | 81aed4352851 |
children | 66d11212175d |
comparison
equal
deleted
inserted
replaced
8:81aed4352851 | 9:77c7556fa8e0 |
---|---|
2 | 2 |
3 import os | 3 import os |
4 import sys | 4 import sys |
5 import urllib2 | 5 import urllib2 |
6 | 6 |
7 from odict import OrderedDict | |
8 from ConfigParser import ConfigParser | 7 from ConfigParser import ConfigParser |
9 from ConfigParser import InterpolationMissingOptionError | 8 from ConfigParser import InterpolationMissingOptionError |
10 from ConfigParser import MissingSectionHeaderError | 9 from ConfigParser import MissingSectionHeaderError |
11 from ConfigParser import NoOptionError | 10 from ConfigParser import NoOptionError |
12 from StringIO import StringIO | 11 from StringIO import StringIO |
13 | 12 |
13 try: | |
14 from collections import OrderedDict | |
15 except ImportError: | |
16 from odict import OrderedDict | |
17 | |
18 | |
14 def file_pointer(resource): | 19 def file_pointer(resource): |
15 """returns a file-like object given a string""" | 20 """returns a file-like object given a string""" |
16 # XXX could go in utils.py | 21 # XXX could go in utils.py |
17 | 22 |
18 if not isinstance(resource, basestring): | 23 if not isinstance(resource, basestring): |
19 # assume resource is already a file-like object | 24 # assume resource is already a file-like object |
20 return resource | 25 return resource |
21 | 26 |
59 _section = self[section] | 64 _section = self[section] |
60 self.remove_section(section) | 65 self.remove_section(section) |
61 else: | 66 else: |
62 _section = {} | 67 _section = {} |
63 self.read({newname: _section}) | 68 self.read({newname: _section}) |
64 | 69 |
65 def dict(self): | 70 def dict(self): |
66 """return a dictionary of dictionaries; | 71 """return a dictionary of dictionaries; |
67 the outer with keys of section names; | 72 the outer with keys of section names; |
68 the inner with keys, values of the section""" | 73 the inner with keys, values of the section""" |
69 return dict([(section, self[section]) | 74 return OrderedDict([(section, self[section]) |
70 for section in self.sections()]) | 75 for section in self.sections()]) |
71 | 76 |
72 def read(self, *ini): | 77 def read(self, *ini): |
73 for _ini in ini: | 78 for _ini in ini: |
74 if isinstance(_ini, dict): | 79 if isinstance(_ini, (dict, OrderedDict)): |
75 for section, contents in _ini.items(): | 80 for section, contents in _ini.items(): |
76 for option, value in contents.items(): | 81 for option, value in contents.items(): |
77 self.set(section, option, value) | 82 self.set(section, option, value) |
78 elif isinstance(_ini, list) or isinstance(_ini, tuple): | 83 elif isinstance(_ini, (list, tuple)): |
79 | 84 |
80 # ensure list or tuple of 3-tuples | 85 # ensure list or tuple of 3-tuples |
81 assert len([option for option in _ini | 86 assert len([option for option in _ini |
82 if isinstance(option, tuple) | 87 if isinstance(option, tuple) |
83 and len(option) == 3]) | 88 and len(option) == 3]) |
84 | 89 |
85 for section, option, value in _ini: | 90 for section, option, value in _ini: |
86 self.set(section, option, value) | 91 self.set(section, option, value) |
87 else: | 92 else: |
88 fp = file_pointer(_ini) | 93 fp = file_pointer(_ini) |
89 try: | 94 try: |
90 self.readfp(fp) | 95 self.readfp(fp) |
91 except MissingSectionHeaderError: | 96 except MissingSectionHeaderError: |
92 fp.seek(0) | 97 fp.seek(0) |
93 fp = StringIO("[DEFAULTS]\n" + fp.read()) | 98 fp = StringIO("[DEFAULTS]\n" + fp.read()) |
94 self.readfp(fp) | 99 self.readfp(fp) |
95 | 100 |
96 def missing(self): | 101 def missing(self): |
97 """returns missing variable names""" | 102 """returns missing variable names""" |
98 missing = set() | |
99 | 103 |
104 missing = set() | |
100 for section in self.sections(): | 105 for section in self.sections(): |
101 for key, val in self.items(section, raw=True): | 106 for key, val in self.items(section, raw=True): |
102 try: | 107 try: |
103 self.get(section, key) | 108 self.get(section, key) |
104 except InterpolationMissingOptionError, e: | 109 except InterpolationMissingOptionError, e: |
110 return options in format appropriate to trac: | 115 return options in format appropriate to trac: |
111 [ (section, option, value) ] | 116 [ (section, option, value) ] |
112 """ | 117 """ |
113 options = [] | 118 options = [] |
114 for section in self.sections(): | 119 for section in self.sections(): |
115 options.extend([(section,) + item | 120 options.extend([(section,) + item |
116 for item in self.items(section)]) | 121 for item in self.items(section)]) |
117 return options | 122 return options |
118 | 123 |
119 def write(self, fp=sys.stdout, raw=False, sorted=True, vars=None): | 124 def write(self, fp=sys.stdout, raw=False, sorted=True, vars=None): |
120 sections = self.sections() | 125 sections = self.sections() |