comparison pyloader/factory.py @ 20:057ccfe310b2

finish basic form of .ini factory - that was easy
author Jeff Hammel <jhammel@mozilla.com>
date Mon, 06 Jun 2011 07:33:37 -0700
parents 8987867698ee
children 4f7c05630f36
comparison
equal deleted inserted replaced
19:8987867698ee 20:057ccfe310b2
1 """ 1 """
2 abstract factories 2 abstract factories
3 """ 3 """
4 4
5 import cast
5 import loader 6 import loader
6 import os 7 import os
7 from ConfigParser import InterpolationMissingOptionError 8 from ConfigParser import InterpolationMissingOptionError
8 from ConfigParser import InterpolationSyntaxError 9 from ConfigParser import InterpolationSyntaxError
9 from ConfigParser import SafeConfigParser as ConfigParser 10 from ConfigParser import SafeConfigParser as ConfigParser
105 config = {} 106 config = {}
106 for section in parser.sections(): 107 for section in parser.sections():
107 108
108 # sanity check 109 # sanity check
109 assert ':' in section, "No : in section: %s" % section 110 assert ':' in section, "No : in section: %s" % section
111
112 # make a dict for the section
110 name, path = section.split(':', 1) 113 name, path = section.split(':', 1)
111 sect = config[name] = dict(path=path) 114 sect = config[name] = dict(path=path)
115
116 # read the options
112 for option in parser.options(section): 117 for option in parser.options(section):
113 118
114 if option in parser.defaults(): 119 if option in parser.defaults():
115 # don't include the defaults 120 # don't include the defaults
116 continue 121 continue
122
123 # try to interpolate the option
124 # otherwise, use the raw value
117 try: 125 try:
118 value = parser.get(section, option) 126 value = parser.get(section, option)
119 except (InterpolationMissingOptionError, InterpolationSyntaxError): 127 except (InterpolationMissingOptionError, InterpolationSyntaxError):
120 value = parser.get(section, option, raw=True) 128 value = parser.get(section, option, raw=True)
121 129
122 130 if option = '.': # positional arguments
131 sect['args'] = cast.str2list(value)
132 else:
133 sect.setdefault('kwargs', {})[option] = value
123 134