annotate pyloader/factory.py @ 81:9203ca3a5182

comment
author Jeff Hammel <jhammel@mozilla.com>
date Sat, 25 Jun 2011 11:05:06 -0700
parents 20bdb8125817
children 58eed691dca7
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
23
9b2ca32e7a36 wire up command line entry point
Jeff Hammel <jhammel@mozilla.com>
parents: 22
diff changeset
1 #!/usr/bin/env python
9b2ca32e7a36 wire up command line entry point
Jeff Hammel <jhammel@mozilla.com>
parents: 22
diff changeset
2
9
ff634cc2e62b unfinished sketch of an abstract factory
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
3 """
ff634cc2e62b unfinished sketch of an abstract factory
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
4 abstract factories
ff634cc2e62b unfinished sketch of an abstract factory
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
5 """
ff634cc2e62b unfinished sketch of an abstract factory
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
6
20
057ccfe310b2 finish basic form of .ini factory - that was easy
Jeff Hammel <jhammel@mozilla.com>
parents: 19
diff changeset
7 import cast
9
ff634cc2e62b unfinished sketch of an abstract factory
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
8 import loader
15
0bea5297c156 introduce a more betterer test
Jeff Hammel <jhammel@mozilla.com>
parents: 14
diff changeset
9 import os
22
b16d6a204ac1 stub a command line entry point
Jeff Hammel <jhammel@mozilla.com>
parents: 21
diff changeset
10 import sys
69
b9d9a94bfa19 override section now works
Jeff Hammel <jhammel@mozilla.com>
parents: 68
diff changeset
11 from copy import deepcopy
23
9b2ca32e7a36 wire up command line entry point
Jeff Hammel <jhammel@mozilla.com>
parents: 22
diff changeset
12 from optparse import OptionParser
26
e547679d4bfd this now works
Jeff Hammel <jhammel@mozilla.com>
parents: 24
diff changeset
13 from ConfigParser import InterpolationDepthError
19
8987867698ee a bit more stubbing of .ini parsing
Jeff Hammel <jhammel@mozilla.com>
parents: 18
diff changeset
14 from ConfigParser import InterpolationMissingOptionError
8987867698ee a bit more stubbing of .ini parsing
Jeff Hammel <jhammel@mozilla.com>
parents: 18
diff changeset
15 from ConfigParser import InterpolationSyntaxError
16
edecb6fbd5a7 further stubbing of ini factory
Jeff Hammel <jhammel@mozilla.com>
parents: 15
diff changeset
16 from ConfigParser import SafeConfigParser as ConfigParser
15
0bea5297c156 introduce a more betterer test
Jeff Hammel <jhammel@mozilla.com>
parents: 14
diff changeset
17
0bea5297c156 introduce a more betterer test
Jeff Hammel <jhammel@mozilla.com>
parents: 14
diff changeset
18 __all__ = ['CircularReferenceError', 'PyFactory', 'IniFactory']
9
ff634cc2e62b unfinished sketch of an abstract factory
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
19
ff634cc2e62b unfinished sketch of an abstract factory
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
20 class CircularReferenceError(Exception):
ff634cc2e62b unfinished sketch of an abstract factory
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
21 """factory has detected a circular reference"""
ff634cc2e62b unfinished sketch of an abstract factory
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
22
ff634cc2e62b unfinished sketch of an abstract factory
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
23 class PyFactory(object):
ff634cc2e62b unfinished sketch of an abstract factory
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
24
81
9203ca3a5182 comment
Jeff Hammel <jhammel@mozilla.com>
parents: 75
diff changeset
25 # to evaluate arguments as objects
9
ff634cc2e62b unfinished sketch of an abstract factory
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
26 delimeters = ('%(', ')s')
ff634cc2e62b unfinished sketch of an abstract factory
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
27
ff634cc2e62b unfinished sketch of an abstract factory
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
28 def __init__(self, config=None, main=''):
ff634cc2e62b unfinished sketch of an abstract factory
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
29 self.main = main # main section
10
ba2355d57998 finish baseline abstract factory implementation
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
30 self.configure(config or {})
ba2355d57998 finish baseline abstract factory implementation
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
31
ba2355d57998 finish baseline abstract factory implementation
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
32 def configure(self, config):
ba2355d57998 finish baseline abstract factory implementation
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
33 """load a new configuration"""
ba2355d57998 finish baseline abstract factory implementation
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
34 # TODO: this should really be a configuration update. If you keep
ba2355d57998 finish baseline abstract factory implementation
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
35 # track of all "apps" and their parents (i.e. as a ADG)
ba2355d57998 finish baseline abstract factory implementation
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
36 # you should be able to update only relevent apps
ba2355d57998 finish baseline abstract factory implementation
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
37 self.config = config
9
ff634cc2e62b unfinished sketch of an abstract factory
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
38 self.seen = set() # already seen apps to note cyclic dependencies
ff634cc2e62b unfinished sketch of an abstract factory
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
39 self.parsed = {} # instantiated apps
ff634cc2e62b unfinished sketch of an abstract factory
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
40
10
ba2355d57998 finish baseline abstract factory implementation
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
41 def load(self, name=None):
ba2355d57998 finish baseline abstract factory implementation
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
42 """load an object"""
ba2355d57998 finish baseline abstract factory implementation
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
43
9
ff634cc2e62b unfinished sketch of an abstract factory
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
44 name = name or self.main # load main section by default
10
ba2355d57998 finish baseline abstract factory implementation
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
45 assert name in self.config, "'%s' not found in configuration"
9
ff634cc2e62b unfinished sketch of an abstract factory
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
46 if name in self.parsed:
ff634cc2e62b unfinished sketch of an abstract factory
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
47 return self.parsed[name]
ff634cc2e62b unfinished sketch of an abstract factory
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
48 if name in self.seen:
ff634cc2e62b unfinished sketch of an abstract factory
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
49 raise CircularReferenceError('Circular reference! : %s' % name)
ff634cc2e62b unfinished sketch of an abstract factory
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
50 self.seen.add(name)
ff634cc2e62b unfinished sketch of an abstract factory
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
51
ff634cc2e62b unfinished sketch of an abstract factory
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
52 # get section
11
ff272dcd5cd8 we have a passing test
Jeff Hammel <jhammel@mozilla.com>
parents: 10
diff changeset
53 section = self.config[name]
9
ff634cc2e62b unfinished sketch of an abstract factory
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
54 assert 'path' in section
ff634cc2e62b unfinished sketch of an abstract factory
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
55
ff634cc2e62b unfinished sketch of an abstract factory
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
56 # load object
ff634cc2e62b unfinished sketch of an abstract factory
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
57 obj = loader.load(section['path'])
ff634cc2e62b unfinished sketch of an abstract factory
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
58
10
ba2355d57998 finish baseline abstract factory implementation
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
59 # get the object's arguments (if any)
9
ff634cc2e62b unfinished sketch of an abstract factory
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
60 args = section.get('args', None)
ff634cc2e62b unfinished sketch of an abstract factory
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
61 kwargs = section.get('kwargs', None)
10
ba2355d57998 finish baseline abstract factory implementation
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
62
ba2355d57998 finish baseline abstract factory implementation
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
63 # if args and kwargs aren't there, you're done!
ba2355d57998 finish baseline abstract factory implementation
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
64 if args is None and kwargs is None:
ba2355d57998 finish baseline abstract factory implementation
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
65 self.parsed[name] = obj
ba2355d57998 finish baseline abstract factory implementation
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
66 return obj
ba2355d57998 finish baseline abstract factory implementation
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
67
ba2355d57998 finish baseline abstract factory implementation
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
68 # interpolate arguments
9
ff634cc2e62b unfinished sketch of an abstract factory
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
69 if args:
11
ff272dcd5cd8 we have a passing test
Jeff Hammel <jhammel@mozilla.com>
parents: 10
diff changeset
70 args = [self.interpolate(arg) for arg in args]
ff272dcd5cd8 we have a passing test
Jeff Hammel <jhammel@mozilla.com>
parents: 10
diff changeset
71 else:
ff272dcd5cd8 we have a passing test
Jeff Hammel <jhammel@mozilla.com>
parents: 10
diff changeset
72 args = []
9
ff634cc2e62b unfinished sketch of an abstract factory
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
73 if kwargs:
ff634cc2e62b unfinished sketch of an abstract factory
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
74 kwargs = dict([(key, self.interpolate(value))
ff634cc2e62b unfinished sketch of an abstract factory
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
75 for key, value in kwargs.items()])
11
ff272dcd5cd8 we have a passing test
Jeff Hammel <jhammel@mozilla.com>
parents: 10
diff changeset
76 else:
ff272dcd5cd8 we have a passing test
Jeff Hammel <jhammel@mozilla.com>
parents: 10
diff changeset
77 kwargs = {}
10
ba2355d57998 finish baseline abstract factory implementation
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
78
ba2355d57998 finish baseline abstract factory implementation
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
79 # invoke
ba2355d57998 finish baseline abstract factory implementation
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
80 self.parsed[name] = obj(*args, **kwargs)
ba2355d57998 finish baseline abstract factory implementation
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
81 return self.parsed[name]
9
ff634cc2e62b unfinished sketch of an abstract factory
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
82
ff634cc2e62b unfinished sketch of an abstract factory
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
83 def interpolate(self, value):
ff634cc2e62b unfinished sketch of an abstract factory
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
84
ff634cc2e62b unfinished sketch of an abstract factory
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
85 # only interpolate strings
ff634cc2e62b unfinished sketch of an abstract factory
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
86 if not isinstance(value, basestring):
ff634cc2e62b unfinished sketch of an abstract factory
Jeff Hammel <jhammel@mozilla.com>
parents:
diff changeset
87 return value
10
ba2355d57998 finish baseline abstract factory implementation
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
88
ba2355d57998 finish baseline abstract factory implementation
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
89 if value.startswith(self.delimeters[0]) and value.endswith(self.delimeters[1]):
ba2355d57998 finish baseline abstract factory implementation
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
90 value = value[len(self.delimeters[0]):-len(self.delimeters[1])]
ba2355d57998 finish baseline abstract factory implementation
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
91 if value in self.config:
ba2355d57998 finish baseline abstract factory implementation
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
92 return self.load(value)
ba2355d57998 finish baseline abstract factory implementation
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
93 return value
ba2355d57998 finish baseline abstract factory implementation
Jeff Hammel <jhammel@mozilla.com>
parents: 9
diff changeset
94
15
0bea5297c156 introduce a more betterer test
Jeff Hammel <jhammel@mozilla.com>
parents: 14
diff changeset
95 class IniFactory(PyFactory):
27
4b757f73e8ca give IniFactory
Jeff Hammel <jhammel@mozilla.com>
parents: 26
diff changeset
96 """load a python object from an .ini file"""
15
0bea5297c156 introduce a more betterer test
Jeff Hammel <jhammel@mozilla.com>
parents: 14
diff changeset
97
0bea5297c156 introduce a more betterer test
Jeff Hammel <jhammel@mozilla.com>
parents: 14
diff changeset
98 def __init__(self, inifile, main=''):
0bea5297c156 introduce a more betterer test
Jeff Hammel <jhammel@mozilla.com>
parents: 14
diff changeset
99 assert os.path.exists(inifile), "File not found: %s" % inifile
21
4f7c05630f36 tie up some factory loose ends and sweeten the API with syntactic sugar
Jeff Hammel <jhammel@mozilla.com>
parents: 20
diff changeset
100 self.inifile = inifile
4f7c05630f36 tie up some factory loose ends and sweeten the API with syntactic sugar
Jeff Hammel <jhammel@mozilla.com>
parents: 20
diff changeset
101 config = self.read(inifile)
4f7c05630f36 tie up some factory loose ends and sweeten the API with syntactic sugar
Jeff Hammel <jhammel@mozilla.com>
parents: 20
diff changeset
102 PyFactory.__init__(self, config, main)
41
2c228e3cd6d8 split reading and interpreting the .ini into different functions
Jeff Hammel <jhammel@mozilla.com>
parents: 35
diff changeset
103
2c228e3cd6d8 split reading and interpreting the .ini into different functions
Jeff Hammel <jhammel@mozilla.com>
parents: 35
diff changeset
104 @classmethod
2c228e3cd6d8 split reading and interpreting the .ini into different functions
Jeff Hammel <jhammel@mozilla.com>
parents: 35
diff changeset
105 def configuration(cls, iniconfig, **defaults):
2c228e3cd6d8 split reading and interpreting the .ini into different functions
Jeff Hammel <jhammel@mozilla.com>
parents: 35
diff changeset
106 """interpret configuration from raw .ini syntax"""
2c228e3cd6d8 split reading and interpreting the .ini into different functions
Jeff Hammel <jhammel@mozilla.com>
parents: 35
diff changeset
107 config = {}
43
a45743b31c6f set ourselves up to detect circular reference errors
Jeff Hammel <jhammel@mozilla.com>
parents: 41
diff changeset
108 interpolated = set()
a45743b31c6f set ourselves up to detect circular reference errors
Jeff Hammel <jhammel@mozilla.com>
parents: 41
diff changeset
109 seen = set()
50
12de7b1bf303 stub interpolation of wrapper options
Jeff Hammel <jhammel@mozilla.com>
parents: 49
diff changeset
110 object_string = '%(object)s'
43
a45743b31c6f set ourselves up to detect circular reference errors
Jeff Hammel <jhammel@mozilla.com>
parents: 41
diff changeset
111
44
be4fbf390e36 a bit of restructure
Jeff Hammel <jhammel@mozilla.com>
parents: 43
diff changeset
112 # create a hash of section names
be4fbf390e36 a bit of restructure
Jeff Hammel <jhammel@mozilla.com>
parents: 43
diff changeset
113 names = {}
be4fbf390e36 a bit of restructure
Jeff Hammel <jhammel@mozilla.com>
parents: 43
diff changeset
114 for section in iniconfig:
41
2c228e3cd6d8 split reading and interpreting the .ini into different functions
Jeff Hammel <jhammel@mozilla.com>
parents: 35
diff changeset
115
2c228e3cd6d8 split reading and interpreting the .ini into different functions
Jeff Hammel <jhammel@mozilla.com>
parents: 35
diff changeset
116 # sanity check
2c228e3cd6d8 split reading and interpreting the .ini into different functions
Jeff Hammel <jhammel@mozilla.com>
parents: 35
diff changeset
117 assert ':' in section, "No : in section: %s" % section
45
7b145c3d2a16 finish hashing the names
Jeff Hammel <jhammel@mozilla.com>
parents: 44
diff changeset
118
7b145c3d2a16 finish hashing the names
Jeff Hammel <jhammel@mozilla.com>
parents: 44
diff changeset
119 name = section.split(':',1)[0]
7b145c3d2a16 finish hashing the names
Jeff Hammel <jhammel@mozilla.com>
parents: 44
diff changeset
120 names[name] = section
41
2c228e3cd6d8 split reading and interpreting the .ini into different functions
Jeff Hammel <jhammel@mozilla.com>
parents: 35
diff changeset
121
44
be4fbf390e36 a bit of restructure
Jeff Hammel <jhammel@mozilla.com>
parents: 43
diff changeset
122 def create_section(section, options):
be4fbf390e36 a bit of restructure
Jeff Hammel <jhammel@mozilla.com>
parents: 43
diff changeset
123
be4fbf390e36 a bit of restructure
Jeff Hammel <jhammel@mozilla.com>
parents: 43
diff changeset
124 # split up the section identifier
41
2c228e3cd6d8 split reading and interpreting the .ini into different functions
Jeff Hammel <jhammel@mozilla.com>
parents: 35
diff changeset
125 name, path = section.split(':', 1)
43
a45743b31c6f set ourselves up to detect circular reference errors
Jeff Hammel <jhammel@mozilla.com>
parents: 41
diff changeset
126
68
65609d7ba63d move around when we create the (local) section dict so that we can override it as desired
Jeff Hammel <jhammel@mozilla.com>
parents: 67
diff changeset
127 # make a dict for the section
65609d7ba63d move around when we create the (local) section dict so that we can override it as desired
Jeff Hammel <jhammel@mozilla.com>
parents: 67
diff changeset
128 sect = {}
65609d7ba63d move around when we create the (local) section dict so that we can override it as desired
Jeff Hammel <jhammel@mozilla.com>
parents: 67
diff changeset
129
43
a45743b31c6f set ourselves up to detect circular reference errors
Jeff Hammel <jhammel@mozilla.com>
parents: 41
diff changeset
130 # interpret decorators
a45743b31c6f set ourselves up to detect circular reference errors
Jeff Hammel <jhammel@mozilla.com>
parents: 41
diff changeset
131 if ':' in path:
a45743b31c6f set ourselves up to detect circular reference errors
Jeff Hammel <jhammel@mozilla.com>
parents: 41
diff changeset
132 wrapper, _path = path.split(':', 1)
47
2384ab3999b7 get wrapper section
Jeff Hammel <jhammel@mozilla.com>
parents: 46
diff changeset
133 # TODO: could interpolate wrapper
46
f7d1238d1bd1 stub using the names....we are only beginning
Jeff Hammel <jhammel@mozilla.com>
parents: 45
diff changeset
134 if wrapper in names:
50
12de7b1bf303 stub interpolation of wrapper options
Jeff Hammel <jhammel@mozilla.com>
parents: 49
diff changeset
135
74
1f76705df520 interpolate inline wrapper options
Jeff Hammel <jhammel@mozilla.com>
parents: 73
diff changeset
136 # inline wrapper arguments:
60
cc361ad8a692 stub out future directions for decorators and their testing a bit
Jeff Hammel <jhammel@mozilla.com>
parents: 54
diff changeset
137 # [extended-fibonacci:@:four=4,five=5:fibonacci]
74
1f76705df520 interpolate inline wrapper options
Jeff Hammel <jhammel@mozilla.com>
parents: 73
diff changeset
138 _wrapper_args = None
1f76705df520 interpolate inline wrapper options
Jeff Hammel <jhammel@mozilla.com>
parents: 73
diff changeset
139 _wrapper_kwargs = None
70
7ec0ca0134ff stub getting wrapper options: [foo:@:bar=fleem:baz]
Jeff Hammel <jhammel@mozilla.com>
parents: 69
diff changeset
140 if ':' in _path:
75
20bdb8125817 inline wrapper arguments now seem to work....thats....uncanny
Jeff Hammel <jhammel@mozilla.com>
parents: 74
diff changeset
141 _wrapper_options, __path = _path.split(':', 1)
70
7ec0ca0134ff stub getting wrapper options: [foo:@:bar=fleem:baz]
Jeff Hammel <jhammel@mozilla.com>
parents: 69
diff changeset
142 if ',' in _wrapper_options or '=' in _wrapper_options:
7ec0ca0134ff stub getting wrapper options: [foo:@:bar=fleem:baz]
Jeff Hammel <jhammel@mozilla.com>
parents: 69
diff changeset
143 # ,= : tokens to ensure these are wrapper options
7ec0ca0134ff stub getting wrapper options: [foo:@:bar=fleem:baz]
Jeff Hammel <jhammel@mozilla.com>
parents: 69
diff changeset
144 # as these shouldn't be found in a real path (dotted path or file path)
75
20bdb8125817 inline wrapper arguments now seem to work....thats....uncanny
Jeff Hammel <jhammel@mozilla.com>
parents: 74
diff changeset
145 _wrapper_args, _wrapper_kwargs = cast.str2args(_wrapper_options)
73
65c95094dbb3 replace path
Jeff Hammel <jhammel@mozilla.com>
parents: 72
diff changeset
146 _path = __path
60
cc361ad8a692 stub out future directions for decorators and their testing a bit
Jeff Hammel <jhammel@mozilla.com>
parents: 54
diff changeset
147
cc361ad8a692 stub out future directions for decorators and their testing a bit
Jeff Hammel <jhammel@mozilla.com>
parents: 54
diff changeset
148 if _path in names:
72
6f384c989a29 modernize comment; this morning is SOOO out of date
Jeff Hammel <jhammel@mozilla.com>
parents: 71
diff changeset
149 # [foo:bar:fleem]
60
cc361ad8a692 stub out future directions for decorators and their testing a bit
Jeff Hammel <jhammel@mozilla.com>
parents: 54
diff changeset
150 wrapped_name = _path
cc361ad8a692 stub out future directions for decorators and their testing a bit
Jeff Hammel <jhammel@mozilla.com>
parents: 54
diff changeset
151 else:
72
6f384c989a29 modernize comment; this morning is SOOO out of date
Jeff Hammel <jhammel@mozilla.com>
parents: 71
diff changeset
152 # stub value for "anonymous" name
6f384c989a29 modernize comment; this morning is SOOO out of date
Jeff Hammel <jhammel@mozilla.com>
parents: 71
diff changeset
153 # [foo:bar:%(here)s/objects.py:MyClass]
60
cc361ad8a692 stub out future directions for decorators and their testing a bit
Jeff Hammel <jhammel@mozilla.com>
parents: 54
diff changeset
154 wrapped_name = section
63
995b831041be stub: override section: [foo:bar]
Jeff Hammel <jhammel@mozilla.com>
parents: 62
diff changeset
155
995b831041be stub: override section: [foo:bar]
Jeff Hammel <jhammel@mozilla.com>
parents: 62
diff changeset
156 # get wrapper options
48
03e03668f546 start recursively fetching the wrapper; tests fail
Jeff Hammel <jhammel@mozilla.com>
parents: 47
diff changeset
157 if wrapper not in config:
63
995b831041be stub: override section: [foo:bar]
Jeff Hammel <jhammel@mozilla.com>
parents: 62
diff changeset
158 # load wrapper configuration
48
03e03668f546 start recursively fetching the wrapper; tests fail
Jeff Hammel <jhammel@mozilla.com>
parents: 47
diff changeset
159 wrapper_section = names[wrapper]
03e03668f546 start recursively fetching the wrapper; tests fail
Jeff Hammel <jhammel@mozilla.com>
parents: 47
diff changeset
160 if wrapper_section in seen:
03e03668f546 start recursively fetching the wrapper; tests fail
Jeff Hammel <jhammel@mozilla.com>
parents: 47
diff changeset
161 pass # TODO
03e03668f546 start recursively fetching the wrapper; tests fail
Jeff Hammel <jhammel@mozilla.com>
parents: 47
diff changeset
162 create_section(wrapper_section, iniconfig[wrapper_section])
74
1f76705df520 interpolate inline wrapper options
Jeff Hammel <jhammel@mozilla.com>
parents: 73
diff changeset
163 wrapper_options = deepcopy(config[wrapper])
1f76705df520 interpolate inline wrapper options
Jeff Hammel <jhammel@mozilla.com>
parents: 73
diff changeset
164
1f76705df520 interpolate inline wrapper options
Jeff Hammel <jhammel@mozilla.com>
parents: 73
diff changeset
165 # add inline wrapper args, kwargs
1f76705df520 interpolate inline wrapper options
Jeff Hammel <jhammel@mozilla.com>
parents: 73
diff changeset
166 if _wrapper_args is not None:
1f76705df520 interpolate inline wrapper options
Jeff Hammel <jhammel@mozilla.com>
parents: 73
diff changeset
167 if 'args' in wrapper_options:
1f76705df520 interpolate inline wrapper options
Jeff Hammel <jhammel@mozilla.com>
parents: 73
diff changeset
168 wrapper_options['args'].extend(_wrapper_args)
1f76705df520 interpolate inline wrapper options
Jeff Hammel <jhammel@mozilla.com>
parents: 73
diff changeset
169 else:
1f76705df520 interpolate inline wrapper options
Jeff Hammel <jhammel@mozilla.com>
parents: 73
diff changeset
170 wrapper_options['args'] = _wrapper_args
1f76705df520 interpolate inline wrapper options
Jeff Hammel <jhammel@mozilla.com>
parents: 73
diff changeset
171 if _wrapper_kwargs is not None:
1f76705df520 interpolate inline wrapper options
Jeff Hammel <jhammel@mozilla.com>
parents: 73
diff changeset
172 if 'kwargs' in wrapper_options:
1f76705df520 interpolate inline wrapper options
Jeff Hammel <jhammel@mozilla.com>
parents: 73
diff changeset
173 wrapper_options['kwargs'].update(_wrapper_kwargs)
1f76705df520 interpolate inline wrapper options
Jeff Hammel <jhammel@mozilla.com>
parents: 73
diff changeset
174 else:
1f76705df520 interpolate inline wrapper options
Jeff Hammel <jhammel@mozilla.com>
parents: 73
diff changeset
175 wrapper_options['kwargs'] = _wrapper_kwargs
50
12de7b1bf303 stub interpolation of wrapper options
Jeff Hammel <jhammel@mozilla.com>
parents: 49
diff changeset
176
63
995b831041be stub: override section: [foo:bar]
Jeff Hammel <jhammel@mozilla.com>
parents: 62
diff changeset
177 # interpolate wrapper_options
51
d44f3e6dcffa interpolate options
Jeff Hammel <jhammel@mozilla.com>
parents: 50
diff changeset
178 def interpolate(option):
54
f724db086125 houston, we have a pyloader
Jeff Hammel <jhammel@mozilla.com>
parents: 53
diff changeset
179 if option == object_string:
f724db086125 houston, we have a pyloader
Jeff Hammel <jhammel@mozilla.com>
parents: 53
diff changeset
180 return '%(' + wrapped_name + ')s'
f724db086125 houston, we have a pyloader
Jeff Hammel <jhammel@mozilla.com>
parents: 53
diff changeset
181 return option
50
12de7b1bf303 stub interpolation of wrapper options
Jeff Hammel <jhammel@mozilla.com>
parents: 49
diff changeset
182 if 'args' in wrapper_options:
12de7b1bf303 stub interpolation of wrapper options
Jeff Hammel <jhammel@mozilla.com>
parents: 49
diff changeset
183 args = wrapper_options['args'][:]
51
d44f3e6dcffa interpolate options
Jeff Hammel <jhammel@mozilla.com>
parents: 50
diff changeset
184 args = [interpolate(i) for i in args]
50
12de7b1bf303 stub interpolation of wrapper options
Jeff Hammel <jhammel@mozilla.com>
parents: 49
diff changeset
185 wrapper_options['args'] = args
12de7b1bf303 stub interpolation of wrapper options
Jeff Hammel <jhammel@mozilla.com>
parents: 49
diff changeset
186 if 'kwargs' in wrapper_options:
12de7b1bf303 stub interpolation of wrapper options
Jeff Hammel <jhammel@mozilla.com>
parents: 49
diff changeset
187 kwargs = wrapper_options['kwargs'].copy()
51
d44f3e6dcffa interpolate options
Jeff Hammel <jhammel@mozilla.com>
parents: 50
diff changeset
188 kwargs = dict([(i,interpolate(j)) for i, j in kwargs.items()])
50
12de7b1bf303 stub interpolation of wrapper options
Jeff Hammel <jhammel@mozilla.com>
parents: 49
diff changeset
189 wrapper_options['kwargs'] = kwargs
53
e4a1d7eecd3b maybe the wrapper works now; to write a test
Jeff Hammel <jhammel@mozilla.com>
parents: 52
diff changeset
190
e4a1d7eecd3b maybe the wrapper works now; to write a test
Jeff Hammel <jhammel@mozilla.com>
parents: 52
diff changeset
191 # create wrapper
e4a1d7eecd3b maybe the wrapper works now; to write a test
Jeff Hammel <jhammel@mozilla.com>
parents: 52
diff changeset
192 config[name] = wrapper_options
62
97800b02f636 shouldnt need to go through the whole sequence when youre wrapping a known section
Jeff Hammel <jhammel@mozilla.com>
parents: 60
diff changeset
193 if _path == wrapped_name:
97800b02f636 shouldnt need to go through the whole sequence when youre wrapping a known section
Jeff Hammel <jhammel@mozilla.com>
parents: 60
diff changeset
194 return
53
e4a1d7eecd3b maybe the wrapper works now; to write a test
Jeff Hammel <jhammel@mozilla.com>
parents: 52
diff changeset
195 name = wrapped_name
43
a45743b31c6f set ourselves up to detect circular reference errors
Jeff Hammel <jhammel@mozilla.com>
parents: 41
diff changeset
196 path = _path
44
be4fbf390e36 a bit of restructure
Jeff Hammel <jhammel@mozilla.com>
parents: 43
diff changeset
197
63
995b831041be stub: override section: [foo:bar]
Jeff Hammel <jhammel@mozilla.com>
parents: 62
diff changeset
198 elif path in names:
995b831041be stub: override section: [foo:bar]
Jeff Hammel <jhammel@mozilla.com>
parents: 62
diff changeset
199 # override section: [foo:bar]
65
6bb431e41e0b stub getting the override options
Jeff Hammel <jhammel@mozilla.com>
parents: 63
diff changeset
200 if path not in config:
6bb431e41e0b stub getting the override options
Jeff Hammel <jhammel@mozilla.com>
parents: 63
diff changeset
201 # load overridden section
6bb431e41e0b stub getting the override options
Jeff Hammel <jhammel@mozilla.com>
parents: 63
diff changeset
202 overridden_section = names[path]
6bb431e41e0b stub getting the override options
Jeff Hammel <jhammel@mozilla.com>
parents: 63
diff changeset
203 if overridden_section in seen:
6bb431e41e0b stub getting the override options
Jeff Hammel <jhammel@mozilla.com>
parents: 63
diff changeset
204 pass # TODO
6bb431e41e0b stub getting the override options
Jeff Hammel <jhammel@mozilla.com>
parents: 63
diff changeset
205 create_section(overridden_section, iniconfig[overridden_section])
69
b9d9a94bfa19 override section now works
Jeff Hammel <jhammel@mozilla.com>
parents: 68
diff changeset
206 sect = deepcopy(config[path])
60
cc361ad8a692 stub out future directions for decorators and their testing a bit
Jeff Hammel <jhammel@mozilla.com>
parents: 54
diff changeset
207
68
65609d7ba63d move around when we create the (local) section dict so that we can override it as desired
Jeff Hammel <jhammel@mozilla.com>
parents: 67
diff changeset
208 if 'path' not in sect:
65609d7ba63d move around when we create the (local) section dict so that we can override it as desired
Jeff Hammel <jhammel@mozilla.com>
parents: 67
diff changeset
209 # add the path to section dict
65609d7ba63d move around when we create the (local) section dict so that we can override it as desired
Jeff Hammel <jhammel@mozilla.com>
parents: 67
diff changeset
210 path = path % defaults
65609d7ba63d move around when we create the (local) section dict so that we can override it as desired
Jeff Hammel <jhammel@mozilla.com>
parents: 67
diff changeset
211 sect['path'] = path
41
2c228e3cd6d8 split reading and interpreting the .ini into different functions
Jeff Hammel <jhammel@mozilla.com>
parents: 35
diff changeset
212
44
be4fbf390e36 a bit of restructure
Jeff Hammel <jhammel@mozilla.com>
parents: 43
diff changeset
213 # get arguments from .ini options
41
2c228e3cd6d8 split reading and interpreting the .ini into different functions
Jeff Hammel <jhammel@mozilla.com>
parents: 35
diff changeset
214 for option, value in options.items():
2c228e3cd6d8 split reading and interpreting the .ini into different functions
Jeff Hammel <jhammel@mozilla.com>
parents: 35
diff changeset
215 if option == '.': # positional arguments
2c228e3cd6d8 split reading and interpreting the .ini into different functions
Jeff Hammel <jhammel@mozilla.com>
parents: 35
diff changeset
216 sect['args'] = cast.str2list(value)
66
2a9274608af3 comment + whitespace
Jeff Hammel <jhammel@mozilla.com>
parents: 65
diff changeset
217 else: # keyword arguments
41
2c228e3cd6d8 split reading and interpreting the .ini into different functions
Jeff Hammel <jhammel@mozilla.com>
parents: 35
diff changeset
218 sect.setdefault('kwargs', {})[option] = value
43
a45743b31c6f set ourselves up to detect circular reference errors
Jeff Hammel <jhammel@mozilla.com>
parents: 41
diff changeset
219
67
236af713bbc0 move setting the configuration to later so that we can easily move around sect
Jeff Hammel <jhammel@mozilla.com>
parents: 66
diff changeset
220 # set the configuration
236af713bbc0 move setting the configuration to later so that we can easily move around sect
Jeff Hammel <jhammel@mozilla.com>
parents: 66
diff changeset
221 config[name] = sect
236af713bbc0 move setting the configuration to later so that we can easily move around sect
Jeff Hammel <jhammel@mozilla.com>
parents: 66
diff changeset
222
65
6bb431e41e0b stub getting the override options
Jeff Hammel <jhammel@mozilla.com>
parents: 63
diff changeset
223 # get the object definitions
43
a45743b31c6f set ourselves up to detect circular reference errors
Jeff Hammel <jhammel@mozilla.com>
parents: 41
diff changeset
224 for section, options in iniconfig.items():
a45743b31c6f set ourselves up to detect circular reference errors
Jeff Hammel <jhammel@mozilla.com>
parents: 41
diff changeset
225 seen.add(section)
a45743b31c6f set ourselves up to detect circular reference errors
Jeff Hammel <jhammel@mozilla.com>
parents: 41
diff changeset
226 if section not in interpolated:
a45743b31c6f set ourselves up to detect circular reference errors
Jeff Hammel <jhammel@mozilla.com>
parents: 41
diff changeset
227 create_section(section, options)
a45743b31c6f set ourselves up to detect circular reference errors
Jeff Hammel <jhammel@mozilla.com>
parents: 41
diff changeset
228 interpolated.add(section)
41
2c228e3cd6d8 split reading and interpreting the .ini into different functions
Jeff Hammel <jhammel@mozilla.com>
parents: 35
diff changeset
229
2c228e3cd6d8 split reading and interpreting the .ini into different functions
Jeff Hammel <jhammel@mozilla.com>
parents: 35
diff changeset
230 return config
21
4f7c05630f36 tie up some factory loose ends and sweeten the API with syntactic sugar
Jeff Hammel <jhammel@mozilla.com>
parents: 20
diff changeset
231
4f7c05630f36 tie up some factory loose ends and sweeten the API with syntactic sugar
Jeff Hammel <jhammel@mozilla.com>
parents: 20
diff changeset
232 @classmethod
4f7c05630f36 tie up some factory loose ends and sweeten the API with syntactic sugar
Jeff Hammel <jhammel@mozilla.com>
parents: 20
diff changeset
233 def read(cls, inifile):
15
0bea5297c156 introduce a more betterer test
Jeff Hammel <jhammel@mozilla.com>
parents: 14
diff changeset
234 """reads configuration from an .ini file"""
16
edecb6fbd5a7 further stubbing of ini factory
Jeff Hammel <jhammel@mozilla.com>
parents: 15
diff changeset
235
21
4f7c05630f36 tie up some factory loose ends and sweeten the API with syntactic sugar
Jeff Hammel <jhammel@mozilla.com>
parents: 20
diff changeset
236 here = os.path.dirname(os.path.abspath(inifile))
16
edecb6fbd5a7 further stubbing of ini factory
Jeff Hammel <jhammel@mozilla.com>
parents: 15
diff changeset
237
edecb6fbd5a7 further stubbing of ini factory
Jeff Hammel <jhammel@mozilla.com>
parents: 15
diff changeset
238 # read configuration
edecb6fbd5a7 further stubbing of ini factory
Jeff Hammel <jhammel@mozilla.com>
parents: 15
diff changeset
239 defaults={'here': here,
21
4f7c05630f36 tie up some factory loose ends and sweeten the API with syntactic sugar
Jeff Hammel <jhammel@mozilla.com>
parents: 20
diff changeset
240 'this': os.path.abspath(inifile)}
16
edecb6fbd5a7 further stubbing of ini factory
Jeff Hammel <jhammel@mozilla.com>
parents: 15
diff changeset
241 parser = ConfigParser(defaults=defaults)
edecb6fbd5a7 further stubbing of ini factory
Jeff Hammel <jhammel@mozilla.com>
parents: 15
diff changeset
242 parser.optionxform = str # use whole case
21
4f7c05630f36 tie up some factory loose ends and sweeten the API with syntactic sugar
Jeff Hammel <jhammel@mozilla.com>
parents: 20
diff changeset
243 parser.read(inifile)
18
d303a5883991 even more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 16
diff changeset
244
d303a5883991 even more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 16
diff changeset
245 # parse configuration
d303a5883991 even more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 16
diff changeset
246 config = {}
d303a5883991 even more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 16
diff changeset
247 for section in parser.sections():
d303a5883991 even more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 16
diff changeset
248
41
2c228e3cd6d8 split reading and interpreting the .ini into different functions
Jeff Hammel <jhammel@mozilla.com>
parents: 35
diff changeset
249 config[section] = {}
20
057ccfe310b2 finish basic form of .ini factory - that was easy
Jeff Hammel <jhammel@mozilla.com>
parents: 19
diff changeset
250
057ccfe310b2 finish basic form of .ini factory - that was easy
Jeff Hammel <jhammel@mozilla.com>
parents: 19
diff changeset
251 # read the options
18
d303a5883991 even more stubbing
Jeff Hammel <jhammel@mozilla.com>
parents: 16
diff changeset
252 for option in parser.options(section):
19
8987867698ee a bit more stubbing of .ini parsing
Jeff Hammel <jhammel@mozilla.com>
parents: 18
diff changeset
253
8987867698ee a bit more stubbing of .ini parsing
Jeff Hammel <jhammel@mozilla.com>
parents: 18
diff changeset
254 if option in parser.defaults():
8987867698ee a bit more stubbing of .ini parsing
Jeff Hammel <jhammel@mozilla.com>
parents: 18
diff changeset
255 # don't include the defaults
8987867698ee a bit more stubbing of .ini parsing
Jeff Hammel <jhammel@mozilla.com>
parents: 18
diff changeset
256 continue
20
057ccfe310b2 finish basic form of .ini factory - that was easy
Jeff Hammel <jhammel@mozilla.com>
parents: 19
diff changeset
257
057ccfe310b2 finish basic form of .ini factory - that was easy
Jeff Hammel <jhammel@mozilla.com>
parents: 19
diff changeset
258 # try to interpolate the option
057ccfe310b2 finish basic form of .ini factory - that was easy
Jeff Hammel <jhammel@mozilla.com>
parents: 19
diff changeset
259 # otherwise, use the raw value
19
8987867698ee a bit more stubbing of .ini parsing
Jeff Hammel <jhammel@mozilla.com>
parents: 18
diff changeset
260 try:
8987867698ee a bit more stubbing of .ini parsing
Jeff Hammel <jhammel@mozilla.com>
parents: 18
diff changeset
261 value = parser.get(section, option)
26
e547679d4bfd this now works
Jeff Hammel <jhammel@mozilla.com>
parents: 24
diff changeset
262 except (InterpolationMissingOptionError, InterpolationSyntaxError, InterpolationDepthError):
19
8987867698ee a bit more stubbing of .ini parsing
Jeff Hammel <jhammel@mozilla.com>
parents: 18
diff changeset
263 value = parser.get(section, option, raw=True)
20
057ccfe310b2 finish basic form of .ini factory - that was easy
Jeff Hammel <jhammel@mozilla.com>
parents: 19
diff changeset
264
41
2c228e3cd6d8 split reading and interpreting the .ini into different functions
Jeff Hammel <jhammel@mozilla.com>
parents: 35
diff changeset
265 config[section][option] = value
21
4f7c05630f36 tie up some factory loose ends and sweeten the API with syntactic sugar
Jeff Hammel <jhammel@mozilla.com>
parents: 20
diff changeset
266
41
2c228e3cd6d8 split reading and interpreting the .ini into different functions
Jeff Hammel <jhammel@mozilla.com>
parents: 35
diff changeset
267 return cls.configuration(config, **parser.defaults())
21
4f7c05630f36 tie up some factory loose ends and sweeten the API with syntactic sugar
Jeff Hammel <jhammel@mozilla.com>
parents: 20
diff changeset
268
22
b16d6a204ac1 stub a command line entry point
Jeff Hammel <jhammel@mozilla.com>
parents: 21
diff changeset
269 def main(args=sys.argv[1:]):
b16d6a204ac1 stub a command line entry point
Jeff Hammel <jhammel@mozilla.com>
parents: 21
diff changeset
270 """command line entry point"""
26
e547679d4bfd this now works
Jeff Hammel <jhammel@mozilla.com>
parents: 24
diff changeset
271 usage = '%prog file1.ini -arg1 -arg2 --key1=value1 --key2=value2'
29
000e175169c2 use IniFactory.__doc__ as parser description
Jeff Hammel <jhammel@mozilla.com>
parents: 27
diff changeset
272 parser = OptionParser(usage=usage, description=IniFactory.__doc__)
23
9b2ca32e7a36 wire up command line entry point
Jeff Hammel <jhammel@mozilla.com>
parents: 22
diff changeset
273 options, args = parser.parse_args(args)
9b2ca32e7a36 wire up command line entry point
Jeff Hammel <jhammel@mozilla.com>
parents: 22
diff changeset
274
9b2ca32e7a36 wire up command line entry point
Jeff Hammel <jhammel@mozilla.com>
parents: 22
diff changeset
275 if len(args) != 1:
9b2ca32e7a36 wire up command line entry point
Jeff Hammel <jhammel@mozilla.com>
parents: 22
diff changeset
276 parser.print_usage()
9b2ca32e7a36 wire up command line entry point
Jeff Hammel <jhammel@mozilla.com>
parents: 22
diff changeset
277 parser.exit()
9b2ca32e7a36 wire up command line entry point
Jeff Hammel <jhammel@mozilla.com>
parents: 22
diff changeset
278
9b2ca32e7a36 wire up command line entry point
Jeff Hammel <jhammel@mozilla.com>
parents: 22
diff changeset
279 factory = IniFactory(args[0])
26
e547679d4bfd this now works
Jeff Hammel <jhammel@mozilla.com>
parents: 24
diff changeset
280 obj = factory.load()
e547679d4bfd this now works
Jeff Hammel <jhammel@mozilla.com>
parents: 24
diff changeset
281 print obj
22
b16d6a204ac1 stub a command line entry point
Jeff Hammel <jhammel@mozilla.com>
parents: 21
diff changeset
282
21
4f7c05630f36 tie up some factory loose ends and sweeten the API with syntactic sugar
Jeff Hammel <jhammel@mozilla.com>
parents: 20
diff changeset
283 if __name__ == '__main__':
22
b16d6a204ac1 stub a command line entry point
Jeff Hammel <jhammel@mozilla.com>
parents: 21
diff changeset
284 main()