release: 13
[jack_mixer.git] / serialization.py
blob61e3985ed6c3bec30114ae12dda2a8950e500405
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'''
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'''
42 pass
44 def get_childs(self):
45 pass
47 def get_properties(self):
48 pass
50 def serialization_name(self):
51 return None
54 class SerializedObject:
55 '''Base class for object supporting serialization'''
56 def serialization_name(self):
57 return None
59 def serialize(self, object_backend):
60 '''Serialize properties of called object into supplied serialization_object_backend'''
61 pass
63 def serialization_get_childs(self):
64 '''Get child objects tha required and support serialization'''
65 return []
67 def unserialize_property(self, name, value):
68 pass
70 def unserialize_child(self, name):
71 return None
74 class Serializator:
75 def __init__(self):
76 pass
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:
84 return False
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):
94 return False
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)
100 if not child:
101 return False
102 if not self.unserialize_one(backend, child, backend_child):
103 return False
105 return True
107 def serialize_one(self, backend, object, backend_object):
108 object.serialize(backend_object)
109 childs = object.serialization_get_childs()
110 for child in 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))