Moving view into classes
This commit is contained in:
75
src/model/data.py
Normal file
75
src/model/data.py
Normal file
@@ -0,0 +1,75 @@
|
||||
empty_doc = __new__(DOMParser()).parseFromString("<empty />", "text/xml").firstChild
|
||||
|
||||
|
||||
def copy(parent):
|
||||
if type(parent) is None:
|
||||
console.log("Copying non-transcrypt object, I think, this could go fairly wrong")
|
||||
return parent
|
||||
|
||||
elif type(parent) is dict:
|
||||
result = {}
|
||||
for k, v in parent.items():
|
||||
result[k] = copy(v)
|
||||
return result
|
||||
|
||||
elif type(parent) is list:
|
||||
return [copy(i) for i in parent]
|
||||
|
||||
elif type(parent) is int or type(parent) is float or type(parent) is str or parent is None:
|
||||
return parent
|
||||
|
||||
else:
|
||||
# some fancy object it looks like
|
||||
result = __pragma__ ('js', "{{}};")
|
||||
|
||||
proto = Object.getPrototypeOf(parent)
|
||||
Object.setPrototypeOf(result, proto)
|
||||
|
||||
result["__class__"] = parent.__class__
|
||||
|
||||
for key in Object.getOwnPropertyNames(parent):
|
||||
if key == "__class__":
|
||||
continue
|
||||
|
||||
val = getattr(parent, key)
|
||||
result[key] = copy(val)
|
||||
|
||||
return result
|
||||
|
||||
|
||||
class Data:
|
||||
def make_copy(self):
|
||||
# makes an internal copy of self
|
||||
# delete old copy
|
||||
if hasattr(self, "_copy"):
|
||||
if self._copy is not None:
|
||||
self._copy = None
|
||||
|
||||
# make new data only copy
|
||||
self._copy = copy(self)
|
||||
|
||||
|
||||
def copy(self):
|
||||
# return internal copy of self
|
||||
# check if even initialized
|
||||
if hasattr(self, "_copy"):
|
||||
return self._copy
|
||||
else:
|
||||
return None
|
||||
|
||||
def overwrite_with_copy(self):
|
||||
for key, value in dict(self._copy).items():
|
||||
# skip functions and _copy
|
||||
if key == "_copy" or type(value) is None:
|
||||
continue
|
||||
setattr(self, key, value)
|
||||
|
||||
self._copy = None
|
||||
|
||||
def view(self, model, *args):
|
||||
raise NotImplementedError()
|
||||
|
||||
@classmethod
|
||||
def import_xml(cls, xml, *args):
|
||||
raise NotImplementedError()
|
||||
|
||||
Reference in New Issue
Block a user