OLD | NEW |
1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 """Defines the PersistentMixIn utility class to easily convert classes to and | 5 """Defines the PersistentMixIn utility class to easily convert classes to and |
6 from dict for serialization. | 6 from dict for serialization. |
7 | 7 |
8 This class is aimed at json-compatible serialization, so it supports the limited | 8 This class is aimed at json-compatible serialization, so it supports the limited |
9 set of structures supported by json; strings, numbers as int or float, list and | 9 set of structures supported by json; strings, numbers as int or float, list and |
10 dictionaries. | 10 dictionaries. |
(...skipping 222 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
233 for item in dir(cls): | 233 for item in dir(cls): |
234 if item.startswith('_'): | 234 if item.startswith('_'): |
235 continue | 235 continue |
236 item_value = getattr(cls, item) | 236 item_value = getattr(cls, item) |
237 if isinstance(item_value, type): | 237 if isinstance(item_value, type): |
238 item_value = (item_value,) | 238 item_value = (item_value,) |
239 if not isinstance(item_value, tuple): | 239 if not isinstance(item_value, tuple): |
240 continue | 240 continue |
241 if not all(i is None or i.__class__ == type for i in item_value): | 241 if not all(i is None or i.__class__ == type for i in item_value): |
242 continue | 242 continue |
243 if any(i is str for i in item_value): | |
244 raise TypeError( | |
245 '%s is type \'str\' which is currently not supported' % item) | |
246 item_value = tuple( | 243 item_value = tuple( |
247 f if f is not None else None.__class__ for f in item_value) | 244 f if f is not None else None.__class__ for f in item_value) |
248 persistent_members_cache[item] = item_value | 245 persistent_members_cache[item] = item_value |
249 return persistent_members_cache | 246 return persistent_members_cache |
250 | 247 |
251 @staticmethod | 248 @staticmethod |
252 def _get_subclass(typename): | 249 def _get_subclass(typename): |
253 """Returns the PersistentMixIn subclass with the name |typename|.""" | 250 """Returns the PersistentMixIn subclass with the name |typename|.""" |
254 subclass = None | 251 subclass = None |
255 if PersistentMixIn.__persistent_classes_cache is not None: | 252 if PersistentMixIn.__persistent_classes_cache is not None: |
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
397 def save_to_json_file(filename, obj): | 394 def save_to_json_file(filename, obj): |
398 """Save one object in a JSON file.""" | 395 """Save one object in a JSON file.""" |
399 try: | 396 try: |
400 old = filename + '.old' | 397 old = filename + '.old' |
401 if os.path.exists(filename): | 398 if os.path.exists(filename): |
402 os.rename(filename, old) | 399 os.rename(filename, old) |
403 finally: | 400 finally: |
404 with open(filename, 'wb') as f: | 401 with open(filename, 'wb') as f: |
405 json.dump(obj.as_dict(), f, sort_keys=True, indent=2) | 402 json.dump(obj.as_dict(), f, sort_keys=True, indent=2) |
406 f.write('\n') | 403 f.write('\n') |
OLD | NEW |