Show More
@@ -152,27 +152,56 b' def CannedBuffer(CannedBytes):' | |||||
152 | # Functions |
|
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 | if Application.initialized(): |
|
160 | if Application.initialized(): | |
157 | logger = Application.instance().log |
|
161 | logger = Application.instance().log | |
158 | else: |
|
162 | else: | |
159 | logger = logging.getLogger() |
|
163 | logger = logging.getLogger() | |
160 | if not logger.handlers: |
|
164 | if not logger.handlers: | |
161 | logging.basicConfig() |
|
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 | def can(obj): |
|
187 | def can(obj): | |
165 | """prepare an object for pickling""" |
|
188 | """prepare an object for pickling""" | |
|
189 | ||||
|
190 | import_needed = False | |||
|
191 | ||||
166 | for cls,canner in can_map.iteritems(): |
|
192 | for cls,canner in can_map.iteritems(): | |
167 | if isinstance(cls, basestring): |
|
193 | if isinstance(cls, basestring): | |
168 | try: |
|
194 | import_needed = True | |
169 | cls = import_item(cls) |
|
195 | break | |
170 | except Exception: |
|
196 | elif isinstance(obj, cls): | |
171 | _error("cannning class not importable: %r", cls, exc_info=True) |
|
|||
172 | cls = None |
|
|||
173 | continue |
|
|||
174 | if isinstance(obj, cls): |
|
|||
175 | return canner(obj) |
|
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 | return obj |
|
205 | return obj | |
177 |
|
206 | |||
178 | def can_dict(obj): |
|
207 | def can_dict(obj): | |
@@ -195,16 +224,21 b' def can_sequence(obj):' | |||||
195 |
|
224 | |||
196 | def uncan(obj, g=None): |
|
225 | def uncan(obj, g=None): | |
197 | """invert canning""" |
|
226 | """invert canning""" | |
|
227 | ||||
|
228 | import_needed = False | |||
198 | for cls,uncanner in uncan_map.iteritems(): |
|
229 | for cls,uncanner in uncan_map.iteritems(): | |
199 | if isinstance(cls, basestring): |
|
230 | if isinstance(cls, basestring): | |
200 | try: |
|
231 | import_needed = True | |
201 | cls = import_item(cls) |
|
232 | break | |
202 | except Exception: |
|
233 | elif isinstance(obj, cls): | |
203 | _error("uncanning class not importable: %r", cls, exc_info=True) |
|
|||
204 | cls = None |
|
|||
205 | continue |
|
|||
206 | if isinstance(obj, cls): |
|
|||
207 | return uncanner(obj, g) |
|
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 | return obj |
|
242 | return obj | |
209 |
|
243 | |||
210 | def uncan_dict(obj, g=None): |
|
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 | # These dicts can be extended for custom serialization of new objects |
|
265 | # These dicts can be extended for custom serialization of new objects | |
@@ -242,3 +276,6 b' uncan_map = {' | |||||
242 | CannedObject : lambda obj, g: obj.get_object(g), |
|
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