32 except ImportError: |
32 except ImportError: |
33 pass |
33 pass |
34 |
34 |
35 _serializers = {} |
35 _serializers = {} |
36 |
36 |
37 def register_serializer(format, serializer_module): |
37 def register_serializer(format, serializer_module, serializers=None): |
38 """Register a new serializer by passing in a module name.""" |
38 """"Register a new serializer. |
|
39 |
|
40 ``serializer_module`` should be the fully qualified module name |
|
41 for the serializer. |
|
42 |
|
43 If ``serializers`` is provided, the registration will be added |
|
44 to the provided dictionary. |
|
45 |
|
46 If ``serializers`` is not provided, the registration will be made |
|
47 directly into the global register of serializers. Adding serializers |
|
48 directly is not a thread-safe operation. |
|
49 """ |
39 module = __import__(serializer_module, {}, {}, ['']) |
50 module = __import__(serializer_module, {}, {}, ['']) |
40 _serializers[format] = module |
51 if serializers is None: |
41 |
52 _serializers[format] = module |
|
53 else: |
|
54 serializers[format] = module |
|
55 |
42 def unregister_serializer(format): |
56 def unregister_serializer(format): |
43 """Unregister a given serializer""" |
57 "Unregister a given serializer. This is not a thread-safe operation." |
44 del _serializers[format] |
58 del _serializers[format] |
45 |
59 |
46 def get_serializer(format): |
60 def get_serializer(format): |
47 if not _serializers: |
61 if not _serializers: |
48 _load_serializers() |
62 _load_serializers() |
86 """ |
100 """ |
87 Register built-in and settings-defined serializers. This is done lazily so |
101 Register built-in and settings-defined serializers. This is done lazily so |
88 that user code has a chance to (e.g.) set up custom settings without |
102 that user code has a chance to (e.g.) set up custom settings without |
89 needing to be careful of import order. |
103 needing to be careful of import order. |
90 """ |
104 """ |
|
105 global _serializers |
|
106 serializers = {} |
91 for format in BUILTIN_SERIALIZERS: |
107 for format in BUILTIN_SERIALIZERS: |
92 register_serializer(format, BUILTIN_SERIALIZERS[format]) |
108 register_serializer(format, BUILTIN_SERIALIZERS[format], serializers) |
93 if hasattr(settings, "SERIALIZATION_MODULES"): |
109 if hasattr(settings, "SERIALIZATION_MODULES"): |
94 for format in settings.SERIALIZATION_MODULES: |
110 for format in settings.SERIALIZATION_MODULES: |
95 register_serializer(format, settings.SERIALIZATION_MODULES[format]) |
111 register_serializer(format, settings.SERIALIZATION_MODULES[format], serializers) |
|
112 _serializers = serializers |