1 """
2 Cinelab serializer implementation.
3 """
4 from bisect import insort
5 from xml.etree.cElementTree import Element, ElementTree, SubElement
6
7 from advene.model.cam.consts import CAM_XML, CAMSYS_NS_PREFIX
8 from advene.model.serializers.advene_xml import _indent
9 from advene.model.serializers.advene_xml import _Serializer as _AdveneSerializer
10
11 NAME = "Cinelab Advene XML"
12
13 EXTENSION = ".cxp"
14
15 MIMETYPE = "application/x-cinelab-package+xml"
16
18 """Return a serializer that will serialize `package` to `file_`.
19
20 `file_` is a writable file-like object. It is the responsibility of the
21 caller to close it.
22
23 The returned object must implement the interface for which
24 :class:`_Serializer` is the reference implementation.
25 """
26 return _Serializer(package, file_)
27
29 """A shortcut for ``make_serializer(package, file_).serialize()``.
30
31 See also `make_serializer`.
32 """
33 return _Serializer(package, file_).serialize()
34
35
37
38
39
40
41
43 """Perform the actual serialization."""
44 namespaces = self.namespaces = {}
45 root = self.root = Element("package", xmlns=self.default_ns)
46 package = self.package
47 namespaces = package._get_namespaces_as_dict()
48 for uri, prefix in namespaces.iteritems():
49 root.set("xmlns:%s" % prefix, uri)
50 if package.uri:
51 root.set("uri", package.uri)
52
53 self._serialize_meta(package, self.root)
54
55 ximports = SubElement(self.root, "imports")
56 for i in package.own.imports:
57 self._serialize_import(i, ximports)
58 if len(ximports) == 0:
59 self.root.remove(ximports)
60
61 xannotation_types = SubElement(self.root, "annotation-types")
62 for t in package.own.annotation_types:
63 self._serialize_tag(t, xannotation_types, "annotation-type")
64 if len(xannotation_types) == 0:
65 self.root.remove(xannotation_types)
66
67 xrelation_types = SubElement(self.root, "relation-types")
68 for t in package.own.relation_types:
69 self._serialize_tag(t, xrelation_types, "relation-type")
70 if len(xrelation_types) == 0:
71 self.root.remove(xrelation_types)
72
73 xtags = SubElement(self.root, "tags")
74 for t in package.own.user_tags:
75 self._serialize_tag(t, xtags)
76 if len(xtags) == 0:
77 self.root.remove(xtags)
78
79 xmedias = SubElement(self.root, "medias")
80 for m in package.own.medias:
81 self._serialize_media(m, xmedias)
82 if len(xmedias) == 0:
83 self.root.remove(xmedias)
84
85 xresources = SubElement(self.root, "resources")
86 for r in package.own.resources:
87 self._serialize_resource(r, xresources)
88 if len(xresources) == 0:
89 self.root.remove(xresources)
90
91 xannotations = SubElement(self.root, "annotations")
92 for a in package.own.annotations:
93 self._serialize_annotation(a, xannotations)
94 if len(xannotations) == 0:
95 self.root.remove(xannotations)
96
97 xrelations = SubElement(self.root, "relations")
98 for r in package.own.relations:
99 self._serialize_relation(r, xrelations)
100 if len(xrelations) == 0:
101 self.root.remove(xrelations)
102
103 xviews = SubElement(self.root, "views")
104 for v in package.own.views:
105 self._serialize_view(v, xviews)
106 if len(xviews) == 0:
107 self.root.remove(xviews)
108
109 xqueries = SubElement(self.root, "queries")
110 for q in package.own.queries:
111 self._serialize_query(q, xqueries)
112 if len(xqueries) == 0:
113 self.root.remove(xqueries)
114
115 xschemas = SubElement(self.root, "schemas")
116 for L in package.own.schemas:
117 self._serialize_list(L, xschemas, "schema")
118 if len(xschemas) == 0:
119 self.root.remove(xschemas)
120
121 xlists = SubElement(self.root, "lists")
122 for L in package.own.user_lists:
123 self._serialize_list(L, xlists)
124 if len(xlists) == 0:
125 self.root.remove(xlists)
126
127 self._serialize_external_tagging(self.root)
128
129 _indent(self.root)
130 ElementTree(self.root).write(self.file)
131
132
133
138
139
140
149