cuemsutils 0.0.1__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,177 @@
1
+ import xmlschema
2
+ from xml.etree.ElementTree import Element
3
+ from xml.etree.ElementTree import register_namespace as etree_register_namespace
4
+ from lxml.etree import Element as lxml_etree_element
5
+ from lxml.etree import register_namespace as lxml_etree_register_namespace
6
+ from xmlschema.exceptions import XMLSchemaTypeError, XMLSchemaValueError
7
+ from collections import namedtuple
8
+
9
+ ElementData = namedtuple('ElementData', ['tag', 'text', 'content', 'attributes'])
10
+
11
+ class CMLCuemsConverter(xmlschema.XMLSchemaConverter):
12
+
13
+ def __init__(self, namespaces=None, dict_class=None, list_class=None,
14
+ etree_element_class=None, text_key='&', attr_prefix='',
15
+ cdata_prefix=None, indent=4, strip_namespaces=True,
16
+ preserve_root=False, force_dict=False, force_list=False, **kwargs):
17
+
18
+ if etree_element_class is None or etree_element_class is Element:
19
+ register_namespace = etree_register_namespace
20
+ elif etree_element_class is lxml_etree_element:
21
+ register_namespace = lxml_etree_register_namespace
22
+ else:
23
+ raise XMLSchemaTypeError("unsupported element class {!r}".format(etree_element_class))
24
+
25
+ super(CMLCuemsConverter, self).__init__(namespaces=None, register_namespace=register_namespace, strip_namespaces=strip_namespaces)
26
+
27
+ self.dict = dict_class or dict
28
+ self.list = list_class or list
29
+ self.etree_element_class = etree_element_class or Element
30
+ self.text_key = text_key
31
+ self.attr_prefix = attr_prefix
32
+ self.cdata_prefix = cdata_prefix
33
+ self.indent = indent
34
+ self.preserve_root = preserve_root
35
+ self.force_dict = force_dict
36
+ self.force_list = force_list
37
+
38
+
39
+ def element_decode(self, data, xsd_element, xsd_type=None, level=0):
40
+ """
41
+ Converts a decoded element data to a data structure.
42
+ :param data: ElementData instance decoded from an Element node.
43
+ :param xsd_element: the `XsdElement` associated to decoded the data.
44
+ :param xsd_type: optional `XsdType` for supporting dynamic type through \
45
+ xsi:type or xs:alternative.
46
+ :param level: the level related to the decoding process (0 means the root).
47
+ :return: a data structure containing the decoded data.
48
+ """
49
+ xsd_type = xsd_type or xsd_element.type
50
+ result_dict = self.dict()
51
+ if level == 0 and xsd_element.is_global() and not self.strip_namespaces and self:
52
+ schema_namespaces = set(xsd_element.namespaces.values())
53
+ result_dict.update(
54
+ ('%s:%s' % (self.ns_prefix, k) if k else self.ns_prefix, v)
55
+ for k, v in self._namespaces.items()
56
+ if v in schema_namespaces
57
+ )
58
+
59
+ if xsd_type.is_simple() or xsd_type.has_simple_content():
60
+ if data.attributes or self.force_dict and not xsd_type.is_simple():
61
+ result_dict.update(t for t in self.map_attributes(data.attributes))
62
+ if data.text is not None and data.text != '':
63
+ result_dict[self.text_key] = data.text
64
+ return result_dict
65
+ else:
66
+ return data.text if data.text != '' else None
67
+ else:
68
+ if data.attributes:
69
+ result_dict.update(t for t in self.map_attributes(data.attributes))
70
+
71
+ # has_single_group = xsd_type.content_type.is_single()
72
+ list_types = list if self.list is list else (self.list, list)
73
+ dict_types = dict if self.dict is dict else (self.dict, dict)
74
+ if data.content:
75
+ for name, value, xsd_child in self.map_content(data.content):
76
+ try:
77
+ if isinstance(result_dict, list_types):
78
+ result = result_dict
79
+ else:
80
+ result = result_dict[name]
81
+ except KeyError:
82
+ if xsd_child is not None and not xsd_child.is_single():
83
+ result_dict = [{name:value}]
84
+ else:
85
+ result_dict[name] = self.list([value]) if self.force_list else value
86
+ else:
87
+ if isinstance(result, dict_types):
88
+ result_dict[name] = self.list([result, value])
89
+ elif isinstance(result, list_types) or not result:
90
+ result_dict.append({name:value})
91
+ else:
92
+ result.append(value)
93
+
94
+
95
+ elif data.text is not None and data.text != '':
96
+ result_dict[self.text_key] = data.text
97
+
98
+ if level == 0 and self.preserve_root:
99
+ return self.dict(
100
+ [(self.map_qname(data.tag), result_dict if result_dict else None)]
101
+ )
102
+ return result_dict if result_dict else None
103
+
104
+ def element_encode(self, obj, xsd_element, level=0):
105
+ """
106
+ Extracts XML decoded data from a data structure for encoding into an ElementTree.
107
+ :param obj: the decoded object.
108
+ :param xsd_element: the `XsdElement` associated to the decoded data structure.
109
+ :param level: the level related to the encoding process (0 means the root).
110
+ :return: an ElementData instance.
111
+ """
112
+ if level != 0:
113
+ tag = xsd_element.name
114
+ elif not self.preserve_root:
115
+ tag = xsd_element.qualified_name
116
+ else:
117
+ tag = xsd_element.qualified_name
118
+ try:
119
+ obj = obj.get(tag, xsd_element.local_name)
120
+ except (KeyError, AttributeError, TypeError):
121
+ pass
122
+
123
+ if not isinstance(obj, (self.dict, dict)):
124
+ if xsd_element.type.is_simple() or xsd_element.type.has_simple_content():
125
+ return ElementData(tag, obj, None, {})
126
+ elif xsd_element.type.mixed and not isinstance(obj, list):
127
+ return ElementData(tag, obj, None, {})
128
+ else:
129
+ return ElementData(tag, None, obj, {})
130
+
131
+ text = None
132
+ content = []
133
+ attributes = {}
134
+
135
+ for name, value in obj.items():
136
+ if name == self.text_key and self.text_key:
137
+ text = obj[self.text_key]
138
+ elif (self.cdata_prefix and name.startswith(self.cdata_prefix)) or \
139
+ name[0].isdigit() and self.cdata_prefix == '':
140
+ index = int(name[len(self.cdata_prefix):])
141
+ content.append((index, value))
142
+ elif name == self.ns_prefix:
143
+ self[''] = value
144
+ elif name.startswith('%s:' % self.ns_prefix):
145
+ if not self.strip_namespaces:
146
+ self[name[len(self.ns_prefix) + 1:]] = value
147
+ elif self.attr_prefix and name.startswith(self.attr_prefix):
148
+ attr_name = name[len(self.attr_prefix):]
149
+ ns_name = self.unmap_qname(attr_name, xsd_element.attributes)
150
+ attributes[ns_name] = value
151
+ elif not isinstance(value, (self.list, list)) or not value:
152
+ content.append((self.unmap_qname(name), value))
153
+ elif isinstance(value[0], (self.dict, dict, self.list, list)):
154
+ ns_name = self.unmap_qname(name)
155
+ content.extend((ns_name, item) for item in value)
156
+ else:
157
+ ns_name = self.unmap_qname(name)
158
+ for xsd_child in xsd_element.type.content_type.iter_elements():
159
+ matched_element = xsd_child.match(ns_name, resolve=True)
160
+ if matched_element is not None:
161
+ if matched_element.type.is_list():
162
+ content.append((ns_name, value))
163
+ else:
164
+ content.extend((ns_name, item) for item in value)
165
+ break
166
+ else:
167
+ if self.attr_prefix == '' and ns_name not in attributes:
168
+ for key, xsd_attribute in xsd_element.attributes.items():
169
+ if xsd_attribute.is_matching(ns_name):
170
+ attributes[key] = value
171
+ break
172
+ else:
173
+ content.append((ns_name, value))
174
+ else:
175
+ content.append((ns_name, value))
176
+
177
+ return ElementData(tag, text, content, attributes)
@@ -0,0 +1,258 @@
1
+ # DEV: Move to cuems-utils
2
+ import distutils.util
3
+
4
+ from ..CuemsScript import CuemsScript
5
+ from ..cues.CueList import CueList
6
+ from ..cues.Cue import Cue
7
+ from ....dev.Media import Media, region
8
+ from ..UI_properties import UI_properties
9
+ from ..cues.CueOutput import CueOutput, AudioCueOutput, VideoCueOutput, DmxCueOutput
10
+ from ..cues.AudioCue import AudioCue
11
+ from ..cues.VideoCue import VideoCue
12
+ from ..cues.ActionCue import ActionCue
13
+ from ..cues.DmxCue import DmxCue, DmxScene, DmxUniverse, DmxChannel
14
+ from ..cues.ActionCue import ActionCue
15
+ from ..CTimecode import CTimecode
16
+ from ..log import logger
17
+ from ..cuems_nodeconf.CuemsNode import CuemsNodeDict, CuemsNode
18
+
19
+ PARSER_SUFFIX = 'Parser'
20
+ GENERIC_PARSER = 'GenericParser'
21
+
22
+ class GenericDict(dict):
23
+ pass
24
+
25
+ class CuemsParser():
26
+ def __init__(self, init_dict):
27
+ self.init_dict=init_dict
28
+
29
+ def get_parser_class(self, class_string):
30
+ parser_name = class_string + PARSER_SUFFIX
31
+ try:
32
+ parser_class = (globals()[parser_name], class_string)
33
+ except KeyError as err:
34
+ # logger.debug("Could not find class {0}, reverting to generic parser class".format(err))
35
+ parser_class = (globals()[GENERIC_PARSER], class_string)
36
+ return parser_class
37
+
38
+ def get_class(self, class_string):
39
+
40
+ try:
41
+ _class = globals()[class_string]
42
+ except KeyError as err:
43
+ # logger.debug("Could not find class {0}".format(err))
44
+ _class = GenericDict
45
+ return _class
46
+
47
+ def get_first_key(self, _dict):
48
+ return list(_dict.keys())[0]
49
+
50
+
51
+ def get_contained_dict(self, _dict):
52
+ return list(_dict.values())[0]
53
+
54
+ def convert_string_to_value(self, _string):
55
+ bool_strings = ['true', 'false']
56
+ null_strings = ['none', 'null']
57
+ if isinstance(_string, str):
58
+ if (_string.lower() in bool_strings):
59
+ return bool(distutils.util.strtobool(_string.lower()))
60
+ elif (_string.lower() in null_strings):
61
+ return None
62
+ elif (_string.isdigit()):
63
+ return int(_string)
64
+ else:
65
+ try:
66
+ return float(_string)
67
+ except ValueError:
68
+ return _string
69
+ else:
70
+ return _string
71
+
72
+ def parse(self):
73
+ parser_class, class_string = self.get_parser_class(self.get_first_key(self.init_dict))
74
+ item_obj = parser_class(init_dict=self.get_contained_dict(self.init_dict), class_string=class_string).parse()
75
+ return item_obj
76
+
77
+ class CuemsScriptParser(CuemsParser):
78
+ def __init__(self, init_dict, class_string):
79
+ self.init_dict = init_dict
80
+ self.class_string = class_string
81
+ self._class = self.get_class(class_string)
82
+ self.item_csp = self._class()
83
+
84
+ def parse(self):
85
+ for dict_key, dict_value in self.init_dict.items():
86
+ if type(dict_value) is dict:
87
+ if (len(list(dict_value))> 0):
88
+ parser_class, class_string = self.get_parser_class(dict_key)
89
+ self.item_csp[dict_key.lower()] = parser_class(init_dict=dict_value, class_string=class_string).parse()
90
+
91
+ else:
92
+ dict_value = self.convert_string_to_value(dict_value)
93
+ self.item_csp[dict_key] = dict_value
94
+
95
+ return self.item_csp
96
+
97
+ class CueListParser(CuemsScriptParser):
98
+ def __init__(self, init_dict, class_string):
99
+ self.init_dict = init_dict
100
+ self.class_string = class_string
101
+ self._class = self.get_class(class_string)
102
+ self.item_clp = self._class()
103
+
104
+ def parse(self):
105
+ for dict_key, dict_value in self.init_dict.items():
106
+ if isinstance(dict_value, list):
107
+ local_list = []
108
+ for cue in dict_value:
109
+ parser_class, unused_class_string = self.get_parser_class(self.get_first_key(cue))
110
+ item_obj = parser_class(init_dict=self.get_contained_dict(cue), class_string=self.get_first_key(cue)).parse()
111
+ local_list.append(item_obj)
112
+
113
+ self.item_clp['contents'] = local_list
114
+ elif isinstance(dict_value, dict):
115
+ key_parser_class, key_class_string = self.get_parser_class(dict_key)
116
+ if key_parser_class == GenericParser:
117
+ value_parser_class, value_class_string = self.get_parser_class(self.get_first_key(dict_value))
118
+
119
+ if value_parser_class == GenericParser:
120
+ self.item_clp[dict_key] = key_parser_class(init_dict=dict_value, class_string=key_class_string).parse()
121
+ else:
122
+ self.item_clp[dict_key] = value_parser_class(init_dict=dict_value, class_string=value_class_string).parse()
123
+
124
+ else:
125
+ dict_value = self.convert_string_to_value(dict_value)
126
+ self.item_clp[dict_key] = dict_value
127
+
128
+ return self.item_clp
129
+
130
+ class GenericParser(CuemsScriptParser):
131
+ def __init__(self, init_dict, class_string):
132
+ self.init_dict = init_dict
133
+ self.class_string = class_string
134
+ self._class = self.get_class(class_string)
135
+ self.item_gp = self._class()
136
+
137
+ def parse(self):
138
+ if self._class == GenericDict:
139
+ self.item_gp = self.init_dict
140
+
141
+ elif isinstance(self.init_dict, dict):
142
+ for dict_key, dict_value in self.init_dict.items():
143
+ if isinstance (dict_value, dict):
144
+ key_parser_class, key_class_string = self.get_parser_class(dict_key)
145
+ if key_parser_class == GenericParser:
146
+ value_parser_class, value_class_string = self.get_parser_class(self.get_first_key(dict_value))
147
+
148
+ if value_parser_class == GenericParser:
149
+ self.item_gp[dict_key] = key_parser_class(init_dict=dict_value, class_string=key_class_string).parse()
150
+ else:
151
+ self.item_gp[dict_key] = value_parser_class(init_dict=dict_value, class_string=value_class_string).parse()
152
+ elif isinstance(dict_value, list):
153
+ local_list = []
154
+ parser_class, class_string = self.get_parser_class(dict_key)
155
+ for list_item in dict_value:
156
+
157
+ item_obj = parser_class(init_dict=list_item, class_string=class_string).parse()
158
+ local_list.append(item_obj)
159
+ self.item_gp[dict_key] = local_list
160
+ else:
161
+ dict_value = self.convert_string_to_value(dict_value)
162
+ self.item_gp[dict_key] = dict_value
163
+
164
+ return self.item_gp
165
+
166
+ class DmxSceneParser(GenericParser):
167
+ pass
168
+
169
+ def parse(self):
170
+ for class_string, class_item_list in self.init_dict.items():
171
+ for class_item in class_item_list:
172
+ parser_class, class_string = self.get_parser_class(class_string)
173
+ item_obj = parser_class(init_dict=class_item, class_string=class_string).parse()
174
+ self.item_gp.set_universe(item_obj, class_item['id'])
175
+ return self.item_gp
176
+
177
+ class DmxUniverseParser(GenericParser):
178
+
179
+ def parse(self):
180
+ for class_string, class_item_list in self.init_dict.items():
181
+ if class_string != 'id':
182
+ for class_item in class_item_list:
183
+ parser_class, class_string = self.get_parser_class(class_string)
184
+ item_obj = parser_class(init_dict=class_item, class_string=class_string).parse()
185
+ self.item_gp.set_channel(class_item['id'], item_obj)
186
+ return self.item_gp
187
+
188
+ class DmxChannelParser(GenericParser):
189
+
190
+ def parse(self):
191
+ self.item_gp.value = self.init_dict['&']
192
+ return self.item_gp
193
+
194
+ class GenericSubObjectParser(GenericParser):
195
+
196
+ def parse(self):
197
+ self.item_gp = self._class(self.init_dict)
198
+ return self.item_gp
199
+
200
+ class CTimecodeParser(GenericSubObjectParser):
201
+
202
+ def parse(self):
203
+ self.item_gp = self.init_dict
204
+ return self.item_gp
205
+
206
+
207
+ class OutputsParser(GenericParser):
208
+ def __init__(self, init_dict, class_string, parent_class=None):
209
+ self.init_dict = init_dict
210
+
211
+ def parse(self):
212
+ for dict_key, dict_value in self.init_dict.items():
213
+ self._class = self.get_class(dict_key)
214
+ self.item_op = self._class(dict_value)
215
+
216
+ return self.item_op
217
+
218
+ class regionsParser(GenericParser):
219
+ def __init__(self, init_dict, class_string, parent_class=None):
220
+ self.init_dict = init_dict
221
+ self.class_string = class_string
222
+ self._class = self.get_class(class_string)
223
+ self.item_rp = self._class()
224
+
225
+ def parse(self):
226
+ for dict_key, dict_value in self.init_dict.items():
227
+ key_parser_class, key_class_string = self.get_parser_class(dict_key)
228
+ self.item_rp = key_parser_class(init_dict=dict_value, class_string=key_class_string).parse()
229
+
230
+ return self.item_rp
231
+
232
+ class AudioCueOutputParser(OutputsParser):
233
+ pass
234
+
235
+ class VideoCueOutputParser(OutputsParser):
236
+ pass
237
+ class DmxCueOutputParser(OutputsParser):
238
+ pass
239
+
240
+ class CuemsNodeDictParser(OutputsParser):
241
+ def parse(self):
242
+ self.item_rp = list()
243
+ for item in self.init_dict:
244
+ for dict_key, dict_value in item.items():
245
+ key_parser_class, key_class_string = self.get_parser_class(dict_key)
246
+ self.item_rp.append(key_parser_class(init_dict=dict_value, class_string=key_class_string).parse())
247
+
248
+ return self.item_rp
249
+
250
+ class CuemsNodeParser(GenericParser):
251
+ pass
252
+
253
+ class NoneTypeParser():
254
+ def __init__(self, init_dict, class_string):
255
+ pass
256
+
257
+ def parse(self):
258
+ return None