Update Language-Team tag in German .po file
[jack_mixer.git] / jack_mixer / serialization.py
blobf2cc1757c492ef7bbb126a38f8c32b9bdf68ec13
1 # This file is part of jack_mixer
3 # Copyright (C) 2006 Nedko Arnaudov <nedko@arnaudov.name>
5 # This program is free software; you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; version 2 of the License
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 import logging
21 log = logging.getLogger(__name__)
24 class SerializationBackend:
25 """Base class for serialization backends"""
27 def get_root_serialization_object(self, name):
28 """Returns serialization object where properties of root object
29 will be serialized to"""
30 # this method should never be called for the base class
31 raise NotImplementedError
33 def get_child_serialization_object(self, name, backend_object):
34 # this method should never be called for the base class
35 raise NotImplementedError
38 class SerializationObjectBackend:
39 """Base class for serialization backend objects where real object
40 properties will be serialized to or unserialized from."""
42 def add_property(self, name, value):
43 """Serialize particular property"""
44 pass
46 def get_childs(self):
47 pass
49 def get_properties(self):
50 pass
52 def serialization_name(self):
53 return None
56 class SerializedObject:
57 """Base class for object supporting serialization"""
59 def serialization_name(self):
60 return None
62 def serialize(self, object_backend):
63 """Serialize properties of called object into supplied serialization_object_backend"""
64 pass
66 def serialization_get_childs(self):
67 """Get child objects tha required and support serialization"""
68 return []
70 def unserialize_property(self, name, value):
71 pass
73 def unserialize_child(self, name):
74 return None
77 class Serializator:
78 def __init__(self):
79 pass
81 def serialize(self, root, backend):
82 self.serialize_one(
83 backend, root, backend.get_root_serialization_object(root.serialization_name())
86 def unserialize(self, root, backend):
87 backend_object = backend.get_root_unserialization_object(root.serialization_name())
88 if backend_object is None:
89 return False
91 return self.unserialize_one(backend, root, backend_object)
93 def unserialize_one(self, backend, object, backend_object):
94 log.debug("Unserializing %r.", object)
95 properties = backend_object.get_properties()
96 for name, value in properties.items():
97 log.debug("Property %s = %s", name, value)
98 if not object.unserialize_property(name, value):
99 return False
101 backend_childs = backend_object.get_childs()
102 for backend_child in backend_childs:
103 name = backend_child.serialization_name()
104 child = object.unserialize_child(name)
105 if not child:
106 return False
107 if not self.unserialize_one(backend, child, backend_child):
108 return False
110 return True
112 def serialize_one(self, backend, object, backend_object):
113 object.serialize(backend_object)
114 childs = object.serialization_get_childs()
115 for child in childs:
116 log.debug("Serializing child %r.", child)
117 self.serialize_one(
118 backend,
119 child,
120 backend.get_child_serialization_object(child.serialization_name(), backend_object),