Show More
@@ -152,27 +152,56 b' def CannedBuffer(CannedBytes):' | |||
|
152 | 152 | # Functions |
|
153 | 153 | #------------------------------------------------------------------------------- |
|
154 | 154 | |
|
155 | def _error(*args, **kwargs): | |
|
155 | def _logger(): | |
|
156 | """get the logger for the current Application | |
|
157 | ||
|
158 | the root logger will be used if no Application is running | |
|
159 | """ | |
|
156 | 160 | if Application.initialized(): |
|
157 | 161 | logger = Application.instance().log |
|
158 | 162 | else: |
|
159 | 163 | logger = logging.getLogger() |
|
160 | 164 | if not logger.handlers: |
|
161 | 165 | logging.basicConfig() |
|
162 | logger.error(*args, **kwargs) | |
|
166 | ||
|
167 | return logger | |
|
168 | ||
|
169 | def _import_mapping(mapping, original=None): | |
|
170 | """import any string-keys in a type mapping | |
|
171 | ||
|
172 | """ | |
|
173 | log = _logger() | |
|
174 | log.debug("Importing canning map") | |
|
175 | for key,value in mapping.items(): | |
|
176 | if isinstance(key, basestring): | |
|
177 | try: | |
|
178 | cls = import_item(key) | |
|
179 | except Exception: | |
|
180 | if original and key not in original: | |
|
181 | # only message on user-added classes | |
|
182 | log.error("cannning class not importable: %r", key, exc_info=True) | |
|
183 | mapping.pop(key) | |
|
184 | else: | |
|
185 | mapping[cls] = mapping.pop(key) | |
|
163 | 186 | |
|
164 | 187 | def can(obj): |
|
165 | 188 | """prepare an object for pickling""" |
|
189 | ||
|
190 | import_needed = False | |
|
191 | ||
|
166 | 192 | for cls,canner in can_map.iteritems(): |
|
167 | 193 | if isinstance(cls, basestring): |
|
168 | try: | |
|
169 | cls = import_item(cls) | |
|
170 | except Exception: | |
|
171 | _error("cannning class not importable: %r", cls, exc_info=True) | |
|
172 | cls = None | |
|
173 | continue | |
|
174 | if isinstance(obj, cls): | |
|
194 | import_needed = True | |
|
195 | break | |
|
196 | elif isinstance(obj, cls): | |
|
175 | 197 | return canner(obj) |
|
198 | ||
|
199 | if import_needed: | |
|
200 | # perform can_map imports, then try again | |
|
201 | # this will usually only happen once | |
|
202 | _import_mapping(can_map, _original_can_map) | |
|
203 | return can(obj) | |
|
204 | ||
|
176 | 205 | return obj |
|
177 | 206 | |
|
178 | 207 | def can_dict(obj): |
@@ -195,16 +224,21 b' def can_sequence(obj):' | |||
|
195 | 224 | |
|
196 | 225 | def uncan(obj, g=None): |
|
197 | 226 | """invert canning""" |
|
227 | ||
|
228 | import_needed = False | |
|
198 | 229 | for cls,uncanner in uncan_map.iteritems(): |
|
199 | 230 | if isinstance(cls, basestring): |
|
200 | try: | |
|
201 | cls = import_item(cls) | |
|
202 | except Exception: | |
|
203 | _error("uncanning class not importable: %r", cls, exc_info=True) | |
|
204 | cls = None | |
|
205 | continue | |
|
206 | if isinstance(obj, cls): | |
|
231 | import_needed = True | |
|
232 | break | |
|
233 | elif isinstance(obj, cls): | |
|
207 | 234 | return uncanner(obj, g) |
|
235 | ||
|
236 | if import_needed: | |
|
237 | # perform uncan_map imports, then try again | |
|
238 | # this will usually only happen once | |
|
239 | _import_mapping(uncan_map, _original_uncan_map) | |
|
240 | return uncan(obj, g) | |
|
241 | ||
|
208 | 242 | return obj |
|
209 | 243 | |
|
210 | 244 | def uncan_dict(obj, g=None): |
@@ -225,7 +259,7 b' def uncan_sequence(obj, g=None):' | |||
|
225 | 259 | |
|
226 | 260 | |
|
227 | 261 | #------------------------------------------------------------------------------- |
|
228 |
# API dictionar |
|
|
262 | # API dictionaries | |
|
229 | 263 | #------------------------------------------------------------------------------- |
|
230 | 264 | |
|
231 | 265 | # These dicts can be extended for custom serialization of new objects |
@@ -242,3 +276,6 b' uncan_map = {' | |||
|
242 | 276 | CannedObject : lambda obj, g: obj.get_object(g), |
|
243 | 277 | } |
|
244 | 278 | |
|
279 | # for use in _import_mapping: | |
|
280 | _original_can_map = can_map.copy() | |
|
281 | _original_uncan_map = uncan_map.copy() |
General Comments 0
You need to be logged in to leave comments.
Login now