]> git.0d.be Git - jack_mixer.git/blob - serialization.py
Merge pull request #7 from synthnassizer/mergebranch
[jack_mixer.git] / serialization.py
1 # This file is part of jack_mixer
2 #
3 # Copyright (C) 2006 Nedko Arnaudov <nedko@arnaudov.name>
4 #
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
8 #
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.
13 #
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.
17
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
25
26     def get_child_serialization_object(self, name, backend_object):
27         # this method should never be called for the base class
28         raise NotImplementedError
29
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'''
35         pass
36
37     def get_childs(self):
38         pass
39
40     def get_properties(self):
41         pass
42
43     def serialization_name(self):
44         return None
45
46 class SerializedObject:
47     '''Base class for object supporting serialization'''
48     def serialization_name(self):
49         return None
50
51     def serialize(self, object_backend):
52         '''Serialize properties of called object into supplied serialization_object_backend'''
53         pass
54
55     def serialization_get_childs(self):
56         '''Get child objects tha required and support serialization'''
57         return []
58
59     def unserialize_property(self, name, value):
60         pass
61
62     def unserialize_child(self, name):
63         return None
64
65 class Serializator:
66     def __init__(self):
67         pass
68
69     def serialize(self, root, backend):
70         self.serialize_one(backend, root, backend.get_root_serialization_object(root.serialization_name()))
71
72     def unserialize(self, root, backend):
73         backend_object = backend.get_root_unserialization_object(root.serialization_name())
74         if backend_object == None:
75             return False
76
77         return self.unserialize_one(backend, root, backend_object)
78
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.items():
83             #print "%s = %s" % (name, value)
84             if not object.unserialize_property(name, value):
85                 return False
86
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)
91             if not child:
92                 return False
93             if not self.unserialize_one(backend, child, backend_child):
94                 return False
95
96         return True
97
98     def serialize_one(self, backend, object, backend_object):
99         object.serialize(backend_object)
100         childs = object.serialization_get_childs()
101         for child in 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))