-
Notifications
You must be signed in to change notification settings - Fork 36
/
Copy pathgeneric_serializer.py
68 lines (56 loc) · 2.68 KB
/
generic_serializer.py
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
from django.core.serializers.base import DeserializationError
from . import base_serializer, xliff1_serializer, xliff2_serializer
class Serializer(xliff2_serializer.Serializer):
"""
For serialization, just fall back to the serializer for XLIFF version 2.0
"""
class Deserializer(base_serializer.Deserializer):
"""
For deserialization, inspect the data and choose the correct version
"""
def __init__(self, *args, **kwargs):
# Initialize the base xliff deserializer
super().__init__(*args, **kwargs)
# Get XLIFF version and initialize deserializer of correct version
for event, node in self.event_stream:
if event == "START_ELEMENT" and node.nodeName == "xliff":
if not (version := node.getAttribute("version")):
raise DeserializationError(
"The <xliff>-block does not contain a version attribute."
)
if version == "1.2":
self.xliff_deserializer = xliff1_serializer.Deserializer(
*args, **kwargs
)
return
if version == "2.0":
self.xliff_deserializer = xliff2_serializer.Deserializer(
*args, **kwargs
)
return
raise DeserializationError(
f"This serializer cannot process XLIFF version {version}."
)
raise DeserializationError("The data does not contain an <xliff>-block.")
def get_object(self, node):
"""
Retrieve an object from the serialized unit node.
Handled by either :func:`~integreat_cms.xliff.xliff1_serializer.Deserializer.get_object` or
:func:`~integreat_cms.xliff.xliff2_serializer.Deserializer.get_object`
:param node: The current xml node of the object
:type node: xml.dom.minidom.Element
:return: The original page translation
:rtype: ~integreat_cms.cms.models.pages.page_translation.PageTranslation
"""
return self.xliff_deserializer.get_object(node)
def handle_object(self, node):
"""
Convert a ``<file>``-node to a DeserializedObject.
Handled by either :func:`~integreat_cms.xliff.xliff1_serializer.Deserializer.handle_object` or
:func:`~integreat_cms.xliff.xliff2_serializer.Deserializer.handle_object`
:param node: The current xml node of the object
:type node: xml.dom.minidom.Element
:return: The deserialized page translation
:rtype: django.core.serializers.base.DeserializedObject
"""
return self.xliff_deserializer.handle_object(node)