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 class SerializationBackend
:
19 '''Base class for serialization backends'''
20 def get_root_serialization_object(self
, name
):
21 '''Returns serialization object where properties of root object
22 will be serialized to'''
23 # this method should never be called for the base class
24 raise NotImplementedError
26 def get_child_serialization_object(self
, name
, backend_object
):
27 # this method should never be called for the base class
28 raise NotImplementedError
30 class SerializationObjectBackend
:
31 '''Base class for serialization backend objects where real object
32 properties will be serialized to or unserialized from.'''
33 def add_property(self
, name
, value
):
34 '''Serialize particular property'''
40 def get_properties(self
):
43 def serialization_name(self
):
46 class SerializedObject
:
47 '''Base class for object supporting serialization'''
48 def serialization_name(self
):
51 def serialize(self
, object_backend
):
52 '''Serialize properties of called object into supplied serialization_object_backend'''
55 def serialization_get_childs(self
):
56 '''Get child objects tha required and support serialization'''
59 def unserialize_property(self
, name
, value
):
62 def unserialize_child(self
, name
):
69 def serialize(self
, root
, backend
):
70 self
.serialize_one(backend
, root
, backend
.get_root_serialization_object(root
.serialization_name()))
72 def unserialize(self
, root
, backend
):
73 backend_object
= backend
.get_root_unserialization_object(root
.serialization_name())
74 if backend_object
== None:
77 return self
.unserialize_one(backend
, root
, backend_object
)
79 def unserialize_one(self
, backend
, object, backend_object
):
80 #print "Unserializing " + repr(object)
81 properties
= backend_object
.get_properties()
82 for name
, value
in properties
.iteritems():
83 #print "%s = %s" % (name, value)
84 if not object.unserialize_property(name
, value
):
87 backend_childs
= backend_object
.get_childs()
88 for backend_child
in backend_childs
:
89 name
= backend_child
.serialization_name()
90 child
= object.unserialize_child(name
)
93 if not self
.unserialize_one(backend
, child
, backend_child
):
98 def serialize_one(self
, backend
, object, backend_object
):
99 object.serialize(backend_object
)
100 childs
= object.serialization_get_childs()
102 #print "serializing child " + repr(child)
103 self
.serialize_one(backend
, child
,
104 backend
.get_child_serialization_object(
105 child
.serialization_name(), backend_object
))