1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52 """
53 This module defines a number of miscellaneous helper functions and classes.
54 """
55
56 import sys
57 from types import *
58 from string import *
59 import imp
60 import __builtin__
61 python_import = __builtin__.__import__
62
63 _replacement_modules = {}
64
72
74 """
75 This is a nifty function that allows your modules to inherit from each
76 other maintaining upward compatibility with regard as (for instance)
77 orm2 based data models.
78
79 Example:
80
81 >>> import model2
82 >>> controllers1 = import_with_new_modules('mytest.sub.controllers1',
83 {'model1': model2})
84
85 >>> email = controllers1.make_email()
86 >>> print email.__module__
87 model2
88
89 The controllers1.py module was written for a datamodel stored in the
90 model1.py module in the same package. So it imports it. Now, the function
91 calls above will import controllers1 with model2 as its datamodel. Of
92 course the new module must be a super set of the old for this to work.
93 (In this example the email class has been extended by an attribute.)
94
95 @param module_name: The name of the module to import (including
96 package name(s))
97 @param replacement_modules: A dictionary mapping module names (<b>as
98 they are imported by the module references by module_name</b>) and the
99 module object it is supposed to be replaces with.
100
101 """
102 global _replacement_modules
103
104 imp.acquire_lock()
105 parts = split(module_name, ".")
106
107 path = None
108 for package in parts[:-1]:
109 file, filename, description = imp.find_module(package, path)
110 module = imp.load_module(package, file, filename, description)
111 path = module.__path__
112
113 file, filename, description = imp.find_module(parts[-1], module.__path__)
114 module = imp.load_module(module_name, file, filename, description)
115
116 globs = module.__dict__.copy()
117 _replacement_modules = replacement_modules
118
119 filename = module.__file__
120 if filename.endswith("pyc"): filename = filename[:-1]
121
122 __builtin__.__import__ = _my_import
123 execfile(filename, globs)
124 __builtin__.__import__ = python_import
125 imp.release_lock()
126
127 module = imp.new_module("imported with new modules: %s %s" % \
128 (module.__name__, repr(replacement_modules),))
129 module.__dict__.update(globs)
130 return module
131
132
133
134
135
136
137
138
139
141 """
142 This class implements the mapping (dict) interface. It uses a
143 simple list to store its data and sequential search to access
144 it. It does not depend on __hash__() to manage contained
145 objects. (See Python Reference Manual Chapter 3.3)
146
147 The actual data is stored in self.data as a list of tuples like
148 (key, value).
149
150 See the docstring of orm2.sql._part for details on why this is here.
151 """
153 if type(initdata) in (ListType, TupleType,):
154 self.data = []
155 for tpl in initdata:
156 if type(tpl) not in (ListType, TupleType) or len(tpl) != 2:
157 raise ValueError("Cannot inittiate stupid_dict from "+\
158 "that data")
159 else:
160 self.data.append(tpl)
161 elif type(initdata) == DictType:
162 self.data = initdata.items()
163 else:
164 raise ValueError("A stupid_dict must be initialized either by "+\
165 "a list of pairs or a regular dictionary.")
166
168 return len(self.data)
169
171 for key, value in self.data:
172 if key == which:
173 return value
174
175 if hasattr(self, "default"):
176 return self.default
177 else:
178 raise KeyError(what)
179
185
187 if self.has_key(which):
188 idx = self.keys().index(which)
189 del self.data[idx]
190 else:
191 raise KeyError(which)
192
193
196
197
199 return which in self.keys()
200
202 raise NotImplementedError("I have no idea on how to do this...")
203
205 self.data.sort()
206 other.data.sort()
207
208 if self.data == other.data:
209 return True
210 else:
211 return False
212
214 return "stupid_dict(%s)" % repr(self.data)
215
217 self.data = []
218
221
222 - def get(self, which, default=None):
223 if self.has_key(which):
224 return self[which]
225 else:
226 return default
227
229 if which in self.keys():
230 return True
231 else:
232 return False
233
235 return self.data[:]
236
238 for tpl in self.data: yield tpl
239
240 iterkeys = __iter__
241
244
246 return list(self.iterkeys())
247
250
252 raise NotImplementedError("This doesn't make sense in a stupid_dict,"+\
253 " or does it? No, seriously...")
254
255 popitem = pop
256
258 self.default = default
259
261 """
262 Other must implement the mapping (i.e. dict) interface.
263 """
264 for key, value in other.items():
265 self[key] = value
266
267
269 """
270 Property class the will return the module object of the module the
271 owning class was loaded from.
272 """
273 - def __get__(self, dbobj, owner=None):
274 return sys.modules[dbobj.__class__.__module__]
275
276
277
278
279
280