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.
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"""
49 def get_properties(self
):
52 def serialization_name(self
):
56 class SerializedObject
:
57 """Base class for object supporting serialization"""
59 def serialization_name(self
):
62 def serialize(self
, object_backend
):
63 """Serialize properties of called object into supplied serialization_object_backend"""
66 def serialization_get_childs(self
):
67 """Get child objects tha required and support serialization"""
70 def unserialize_property(self
, name
, value
):
73 def unserialize_child(self
, name
):
81 def serialize(self
, root
, backend
):
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:
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
):
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
)
107 if not self
.unserialize_one(backend
, child
, backend_child
):
112 def serialize_one(self
, backend
, object, backend_object
):
113 object.serialize(backend_object
)
114 childs
= object.serialization_get_childs()
116 log
.debug("Serializing child %r.", child
)
120 backend
.get_child_serialization_object(child
.serialization_name(), backend_object
),