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'''
26 def get_root_serialization_object(self
, name
):
27 '''Returns serialization object where properties of root object
28 will be serialized to'''
29 # this method should never be called for the base class
30 raise NotImplementedError
32 def get_child_serialization_object(self
, name
, backend_object
):
33 # this method should never be called for the base class
34 raise NotImplementedError
37 class SerializationObjectBackend
:
38 '''Base class for serialization backend objects where real object
39 properties will be serialized to or unserialized from.'''
40 def add_property(self
, name
, value
):
41 '''Serialize particular property'''
47 def get_properties(self
):
50 def serialization_name(self
):
54 class SerializedObject
:
55 '''Base class for object supporting serialization'''
56 def serialization_name(self
):
59 def serialize(self
, object_backend
):
60 '''Serialize properties of called object into supplied serialization_object_backend'''
63 def serialization_get_childs(self
):
64 '''Get child objects tha required and support serialization'''
67 def unserialize_property(self
, name
, value
):
70 def unserialize_child(self
, name
):
78 def serialize(self
, root
, backend
):
79 self
.serialize_one(backend
, root
, backend
.get_root_serialization_object(root
.serialization_name()))
81 def unserialize(self
, root
, backend
):
82 backend_object
= backend
.get_root_unserialization_object(root
.serialization_name())
83 if backend_object
== None:
86 return self
.unserialize_one(backend
, root
, backend_object
)
88 def unserialize_one(self
, backend
, object, backend_object
):
89 log
.debug("Unserializing %r.", object)
90 properties
= backend_object
.get_properties()
91 for name
, value
in properties
.items():
92 log
.debug("%s = %s", name
, value
)
93 if not object.unserialize_property(name
, value
):
96 backend_childs
= backend_object
.get_childs()
97 for backend_child
in backend_childs
:
98 name
= backend_child
.serialization_name()
99 child
= object.unserialize_child(name
)
102 if not self
.unserialize_one(backend
, child
, backend_child
):
107 def serialize_one(self
, backend
, object, backend_object
):
108 object.serialize(backend_object
)
109 childs
= object.serialization_get_childs()
111 log
.debug("Serializing child %r.", child
)
112 self
.serialize_one(backend
, child
,
113 backend
.get_child_serialization_object(
114 child
.serialization_name(), backend_object
))