python-lottie  0.6.11+deved4e6c7
A framework to work with lottie files and telegram animated stickers (tgs)
wrappers.py
Go to the documentation of this file.
1 from .utils import *
2 from .core_nodes import XmlDescriptor, ObjectRegistry
3 
4 
6  def __init__(self, name, child_node, default_ctor=None):
7  super().__init__(name)
8  self.child_nodechild_node = child_node
9  self.default_ctordefault_ctor = default_ctor or child_node
10 
11  def from_xml(self, obj, parent: minidom.Element, registry: ObjectRegistry):
12  for cn in xml_child_elements(parent, "param"):
13  if cn.getAttribute("name") == self.namename:
14  value = self.child_nodechild_node.from_dom(xml_first_element_child(cn), registry)
15  break
16  else:
17  value = self.defaultdefaultdefault()
18 
19  setattr(obj, self.att_nameatt_name, value)
20 
21  def to_xml(self, obj, parent: minidom.Element, dom: minidom.Document):
22  param = parent.appendChild(dom.createElement("param"))
23  param.setAttribute("name", self.namename)
24  param.appendChild(getattr(obj, self.att_nameatt_name).to_dom(dom))
25  return param
26 
27  def clean(self, value):
28  if not isinstance(value, self.child_nodechild_node):
29  raise ValueError("%s isn't a valid value for %s" % (value, self.namename))
30  return value
31 
32  def default(self):
33  return self.default_ctordefault_ctor()
34 
35 
37  def __init__(self, type):
38  self._items_items = []
39  self._type_type = type
40 
41  def __len__(self):
42  return len(self._items_items)
43 
44  def __iter__(self):
45  return iter(self._items_items)
46 
47  def __getitem__(self, name):
48  return self._items_items[name]
49 
50  def __getslice__(self, i, j):
51  return self._items_items[i:j]
52 
53  def __setitem__(self, key, value: "Layer"):
54  self.validatevalidate(value)
55  self._items_items[key] = value
56 
57  def append(self, value: "Layer"):
58  self.validatevalidate(value)
59  self._items_items.append(value)
60 
61  def __str__(self):
62  return str(self._items_items)
63 
64  def __repr__(self):
65  return "<SifNodeList %s>" % self._items_items
66 
67  def validate(self, value):
68  if not isinstance(value, self._type_type):
69  raise ValueError("Not a valid object: %s" % value)
70 
71 
73  def __init__(self, name, child_node, nested=True):
74  super().__init__(name)
75  self.child_nodechild_node = child_node
76  self.nestednested = nested
77 
78  def default(self):
79  return self.child_nodechild_node()
80 
81  def from_python(self, value):
82  if not isinstance(value, self.child_nodechild_node):
83  raise ValueError("Invalid value for %s: %s" % (self.namename, value))
84  return value
85 
86  def from_xml(self, obj, parent: minidom.Element, registry: ObjectRegistry):
87  cn = xml_first_element_child(parent, self.namename, allow_none=True)
88  if cn:
89  if self.nestednested:
90  element = xml_first_element_child(cn)
91  else:
92  element = cn
93  value = self.child_nodechild_node.from_dom(element, registry)
94  else:
95  value = self.defaultdefaultdefault()
96 
97  setattr(obj, self.att_nameatt_name, value)
98 
99  def to_xml(self, obj, parent: minidom.Element, dom: minidom.Document):
100  value = getattr(obj, self.att_nameatt_name)
101  if self.nestednested:
102  node = dom.createElement(self.namename)
103  parent.appendChild(node)
104  else:
105  node = parent
106  node.appendChild(value.to_dom(dom))
107 
108 
110  def __init__(self, child_node, name=None, wrapper_tag=None, tags=None):
111  super().__init__(wrapper_tag or child_node._tag)
112  self.child_nodechild_node = child_node
113  self.att_nameatt_nameatt_name = self.att_nameatt_nameatt_name + "s" if name is None else name
114  self.wrapper_tagwrapper_tag = wrapper_tag
115  if tags is None:
116  self.tagstags = {self.namename}
117  else:
118  self.tagstags = tags
119 
120  def default(self):
121  return SifNodeList(self.child_nodechild_node)
122 
123  def clean(self, value):
124  return value
125 
126  def from_xml(self, obj, parent: minidom.Element, registry: ObjectRegistry):
127  values = self.defaultdefaultdefault()
128  for cn in xml_child_elements(parent):
129  if cn.tagName in self.tagstags:
130  value_node = cn
131  if self.wrapper_tagwrapper_tag:
132  value_node = xml_first_element_child(cn)
133  values.append(self.child_nodechild_node.from_dom(value_node, registry))
134 
135  setattr(obj, self.att_nameatt_nameatt_name, values)
136 
137  def to_xml(self, obj, parent: minidom.Element, dom: minidom.Document):
138  for value in getattr(obj, self.att_nameatt_nameatt_name):
139  value_node = value.to_dom(dom)
140  if self.wrapper_tagwrapper_tag:
141  wrapper = dom.createElement(self.wrapper_tagwrapper_tag)
142  wrapper.appendChild(value_node)
143  value_node = wrapper
144  parent.appendChild(value_node)
145 
146 
148  def __init__(self, name, wrapped: XmlDescriptor):
149  super().__init__(name)
150  self.wrappedwrapped = wrapped
151 
152  def to_xml(self, obj, parent: minidom.Element, dom: minidom.Document):
153  wrapper = parent.appendChild(dom.createElement(self.namename))
154  self.wrappedwrapped.to_xml(obj, wrapper, dom)
155 
156  def from_xml(self, obj, parent: minidom.Element, registry: ObjectRegistry):
157  wrapper = xml_first_element_child(parent, self.namename, True)
158  if wrapper:
159  return self.wrappedwrapped.from_xml(obj, wrapper, registry)
160  return self.defaultdefaultdefault()
161 
162  def from_python(self, value):
163  return self.wrappedwrapped.from_python(value)
164 
165  def initialize_object(self, dict, obj):
166  return self.wrappedwrapped.initialize_object(dict, obj)
167 
168  def clean(self, value):
169  return self.wrappedwrapped.clean(value)
170 
171  def default(self):
172  return self.wrappedwrapped.default()
173 
174 
176  def to_xml(self, obj, parent: minidom.Element, dom: minidom.Document):
177  wrapper = parent.appendChild(dom.createElement("param"))
178  wrapper.setAttribute("name", self.namename)
179  self.wrappedwrapped.to_xml(obj, wrapper, dom)
180 
181  def from_xml(self, obj, parent: minidom.Element, registry: ObjectRegistry):
182  for wrapper in xml_child_elements(parent, "param"):
183  if wrapper.getAttribute("name") == self.namename:
184  return self.wrappedwrapped.from_xml(obj, wrapper, registry)
185  return self.defaultdefaultdefault()
186 
187 
189  def __init__(self, name):
190  super().__init__(name)
191 
192  def to_xml(self, obj, parent: minidom.Element, dom: minidom.Document):
193  value = getattr(obj, self.namename, None)
194  if not value:
195  return
196 
197  node = parent.appendChild(dom.createElement(self.namename))
198  value_node = node.appendChild(dom.createElement("bone_valuenode"))
199  value_node.setAttribute("type", value.type)
200  value_node.setAttribute("guid", value.guid)
201 
202  return node
203 
204  def from_xml(self, obj, parent: minidom.Element, registry: ObjectRegistry):
205  node = xml_first_element_child(parent, self.namename, True)
206  value = None
207  if node:
208  value_node = xml_first_element_child(node, "bone_valuenode", True)
209  if value_node:
210  value = registry.get_object(value_node.getAttribute("guid"))
211  if value.type != value_node.getAttribute("type"):
212  raise ValueError("Bone type %s is not %s" % (value.type, value_node.getAttribute("type")))
213 
214  setattr(obj, self.att_nameatt_name, value)
215 
216  def from_python(self, value):
217  return value
def __setitem__(self, key, "Layer" value)
Definition: wrappers.py:53
def append(self, "Layer" value)
Definition: wrappers.py:57
def from_xml(self, obj, minidom.Element parent, ObjectRegistry registry)
Definition: wrappers.py:204
def to_xml(self, obj, minidom.Element parent, minidom.Document dom)
Definition: wrappers.py:192
def to_xml(self, obj, minidom.Element parent, minidom.Document dom)
Definition: wrappers.py:137
def from_xml(self, obj, minidom.Element parent, ObjectRegistry registry)
Definition: wrappers.py:126
def __init__(self, child_node, name=None, wrapper_tag=None, tags=None)
Definition: wrappers.py:110
def __init__(self, name, child_node, default_ctor=None)
Definition: wrappers.py:6
def to_xml(self, obj, minidom.Element parent, minidom.Document dom)
Definition: wrappers.py:21
def from_xml(self, obj, minidom.Element parent, ObjectRegistry registry)
Definition: wrappers.py:11
def from_xml(self, obj, minidom.Element parent, ObjectRegistry registry)
Definition: wrappers.py:86
def __init__(self, name, child_node, nested=True)
Definition: wrappers.py:73
def to_xml(self, obj, minidom.Element parent, minidom.Document dom)
Definition: wrappers.py:99
def from_xml(self, obj, minidom.Element parent, ObjectRegistry registry)
Definition: wrappers.py:181
def to_xml(self, obj, minidom.Element parent, minidom.Document dom)
Definition: wrappers.py:176
def from_xml(self, obj, minidom.Element parent, ObjectRegistry registry)
Definition: wrappers.py:156
def to_xml(self, obj, minidom.Element parent, minidom.Document dom)
Definition: wrappers.py:152
def initialize_object(self, dict, obj)
Definition: wrappers.py:165
def __init__(self, name, XmlDescriptor wrapped)
Definition: wrappers.py:148
def xml_first_element_child(minidom.Node xml, tagname=None, allow_none=False)
Definition: utils.py:79
def xml_child_elements(minidom.Node xml, tagname=None)
Definition: utils.py:73