comparison pyloader/factory.py @ 41:2c228e3cd6d8

split reading and interpreting the .ini into different functions
author Jeff Hammel <jhammel@mozilla.com>
date Wed, 08 Jun 2011 17:50:05 -0700
parents 850d31be0fb8
children a45743b31c6f
comparison
equal deleted inserted replaced
40:0b2019d0af98 41:2c228e3cd6d8
96 def __init__(self, inifile, main=''): 96 def __init__(self, inifile, main=''):
97 assert os.path.exists(inifile), "File not found: %s" % inifile 97 assert os.path.exists(inifile), "File not found: %s" % inifile
98 self.inifile = inifile 98 self.inifile = inifile
99 config = self.read(inifile) 99 config = self.read(inifile)
100 PyFactory.__init__(self, config, main) 100 PyFactory.__init__(self, config, main)
101
102 @classmethod
103 def configuration(cls, iniconfig, **defaults):
104 """interpret configuration from raw .ini syntax"""
105 config = {}
106 for section, options in iniconfig.items():
107
108 # sanity check
109 assert ':' in section, "No : in section: %s" % section
110
111 # make a dict for the section
112 name, path = section.split(':', 1)
113 path = path % defaults
114 sect = config[name] = dict(path=path)
115
116 for option, value in options.items():
117
118 if option == '.': # positional arguments
119 sect['args'] = cast.str2list(value)
120 else:
121 sect.setdefault('kwargs', {})[option] = value
122
123 return config
101 124
102 @classmethod 125 @classmethod
103 def read(cls, inifile): 126 def read(cls, inifile):
104 """reads configuration from an .ini file""" 127 """reads configuration from an .ini file"""
105 128
110 'this': os.path.abspath(inifile)} 133 'this': os.path.abspath(inifile)}
111 parser = ConfigParser(defaults=defaults) 134 parser = ConfigParser(defaults=defaults)
112 parser.optionxform = str # use whole case 135 parser.optionxform = str # use whole case
113 parser.read(inifile) 136 parser.read(inifile)
114 137
115 decorators = {}
116
117 # parse configuration 138 # parse configuration
118 config = {} 139 config = {}
119 for section in parser.sections(): 140 for section in parser.sections():
120 141
121 # sanity check 142 config[section] = {}
122 assert ':' in section, "No : in section: %s" % section
123
124 # make a dict for the section
125 name, path = section.split(':', 1)
126 path = path % parser.defaults()
127 sect = config[name] = dict(path=path)
128 143
129 # read the options 144 # read the options
130 for option in parser.options(section): 145 for option in parser.options(section):
131 146
132 if option in parser.defaults(): 147 if option in parser.defaults():
138 try: 153 try:
139 value = parser.get(section, option) 154 value = parser.get(section, option)
140 except (InterpolationMissingOptionError, InterpolationSyntaxError, InterpolationDepthError): 155 except (InterpolationMissingOptionError, InterpolationSyntaxError, InterpolationDepthError):
141 value = parser.get(section, option, raw=True) 156 value = parser.get(section, option, raw=True)
142 157
143 if option == '.': # positional arguments 158 config[section][option] = value
144 sect['args'] = cast.str2list(value)
145 else:
146 sect.setdefault('kwargs', {})[option] = value
147 159
148 return config 160 return cls.configuration(config, **parser.defaults())
149 161
150 def main(args=sys.argv[1:]): 162 def main(args=sys.argv[1:]):
151 """command line entry point""" 163 """command line entry point"""
152 usage = '%prog file1.ini -arg1 -arg2 --key1=value1 --key2=value2' 164 usage = '%prog file1.ini -arg1 -arg2 --key1=value1 --key2=value2'
153 parser = OptionParser(usage=usage, description=IniFactory.__doc__) 165 parser = OptionParser(usage=usage, description=IniFactory.__doc__)