]> git.0d.be Git - jack_mixer.git/blob - serialization.py
Update README & INSTALL file with current info
[jack_mixer.git] / serialization.py
1 #!/usr/bin/env python
2 #
3 # This file is part of jack_mixer
4 #
5 # Copyright (C) 2006 Nedko Arnaudov <nedko@arnaudov.name>
6 #  
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; version 2 of the License
10 #
11 # This program is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 # GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License
17 # along with this program; if not, write to the Free Software
18 # Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
19
20 class serialization_backend:
21     '''Base class for serialization backends'''
22     def get_root_serialization_object(self, name):
23         '''Returns serialization object where properties of root object will be serialized to'''
24         return None                     # this method should never be called for the base class
25
26     def get_child_serialization_object(self, name, backend_object):
27         return None                     # this method should never be called for the base class
28
29 class serialization_object_backend:
30     '''Base class for serialization backend objects where real object properties will be serialized to or unserialized from.'''
31     def add_property(self, name, value):
32         '''Serialize particular property'''
33         pass
34
35     def get_childs(self):
36         pass
37
38     def get_properties(self):
39         pass
40
41     def serialization_name(self):
42         return None
43
44 class serialized_object:
45     '''Base class for object supporting serialization'''
46     def serialization_name(self):
47         return None
48
49     def serialize(self, object_backend):
50         '''Serialize properties of called object into supplied serialization_object_backend'''
51         pass
52
53     def serialization_get_childs(self):
54         '''Get child objects tha required and support serialization'''
55         return []
56
57     def unserialize_property(self, name, value):
58         pass
59
60     def unserialize_child(self, name):
61         return None
62
63 class serializator:
64     def __init__(self):
65         pass
66
67     def serialize(self, root, backend):
68         self.serialize_one(backend, root, backend.get_root_serialization_object(root.serialization_name()))
69
70     def unserialize(self, root, backend):
71         backend_object = backend.get_root_unserialization_object(root.serialization_name())
72         if backend_object == None:
73             return False
74
75         return self.unserialize_one(backend, root, backend_object)
76
77     def unserialize_one(self, backend, object, backend_object):
78         #print "Unserializing " + repr(object)
79         properties = backend_object.get_properties()
80         for name, value in properties.iteritems():
81             #print "%s = %s" % (name, value)
82             if not object.unserialize_property(name, value):
83                 return False
84
85         backend_childs = backend_object.get_childs()
86         for backend_child in backend_childs:
87             name = backend_child.serialization_name()
88             child = object.unserialize_child(name)
89             if not child:
90                 return False
91             if not self.unserialize_one(backend, child, backend_child):
92                 return False
93
94         return True
95
96     def serialize_one(self, backend, object, backend_object):
97         object.serialize(backend_object)
98         childs = object.serialization_get_childs()
99         for child in childs:
100             #print "serializing child " + repr(child)
101             self.serialize_one(backend, child, backend.get_child_serialization_object(child.serialization_name(), backend_object))