comparison pyloader/factory.py @ 26:e547679d4bfd

this now works
author Jeff Hammel <jhammel@mozilla.com>
date Mon, 06 Jun 2011 10:54:33 -0700
parents ce8fa574324d
children 4b757f73e8ca
comparison
equal deleted inserted replaced
25:8aabb54129a7 26:e547679d4bfd
7 import cast 7 import cast
8 import loader 8 import loader
9 import os 9 import os
10 import sys 10 import sys
11 from optparse import OptionParser 11 from optparse import OptionParser
12 from ConfigParser import InterpolationDepthError
12 from ConfigParser import InterpolationMissingOptionError 13 from ConfigParser import InterpolationMissingOptionError
13 from ConfigParser import InterpolationSyntaxError 14 from ConfigParser import InterpolationSyntaxError
14 from ConfigParser import SafeConfigParser as ConfigParser 15 from ConfigParser import SafeConfigParser as ConfigParser
15 16
16 __all__ = ['CircularReferenceError', 'PyFactory', 'IniFactory'] 17 __all__ = ['CircularReferenceError', 'PyFactory', 'IniFactory']
117 # sanity check 118 # sanity check
118 assert ':' in section, "No : in section: %s" % section 119 assert ':' in section, "No : in section: %s" % section
119 120
120 # make a dict for the section 121 # make a dict for the section
121 name, path = section.split(':', 1) 122 name, path = section.split(':', 1)
123 path = path % parser.defaults()
122 sect = config[name] = dict(path=path) 124 sect = config[name] = dict(path=path)
123 125
124 # read the options 126 # read the options
125 for option in parser.options(section): 127 for option in parser.options(section):
126 128
130 132
131 # try to interpolate the option 133 # try to interpolate the option
132 # otherwise, use the raw value 134 # otherwise, use the raw value
133 try: 135 try:
134 value = parser.get(section, option) 136 value = parser.get(section, option)
135 except (InterpolationMissingOptionError, InterpolationSyntaxError): 137 except (InterpolationMissingOptionError, InterpolationSyntaxError, InterpolationDepthError):
136 value = parser.get(section, option, raw=True) 138 value = parser.get(section, option, raw=True)
137 139
138 if option == '.': # positional arguments 140 if option == '.': # positional arguments
139 sect['args'] = cast.str2list(value) 141 sect['args'] = cast.str2list(value)
140 else: 142 else:
142 144
143 return config 145 return config
144 146
145 def main(args=sys.argv[1:]): 147 def main(args=sys.argv[1:]):
146 """command line entry point""" 148 """command line entry point"""
147 parser = OptionParser() 149 usage = '%prog file1.ini -arg1 -arg2 --key1=value1 --key2=value2'
150 parser = OptionParser(usage=usage)
148 options, args = parser.parse_args(args) 151 options, args = parser.parse_args(args)
149 152
150 if len(args) != 1: 153 if len(args) != 1:
151 parser.print_usage() 154 parser.print_usage()
152 parser.exit() 155 parser.exit()
153 156
154 factory = IniFactory(args[0]) 157 factory = IniFactory(args[0])
155 print factory.load() 158 obj = factory.load()
159 print obj
156 160
157 if __name__ == '__main__': 161 if __name__ == '__main__':
158 main() 162 main()