File: serialization.py

package info (click to toggle)
jack-mixer 10-1
  • links: PTS, VCS
  • area: main
  • in suites: buster, jessie, jessie-kfreebsd, stretch
  • size: 1,968 kB
  • ctags: 803
  • sloc: sh: 11,219; ansic: 3,251; python: 2,238; makefile: 110
file content (105 lines) | stat: -rw-r--r-- 3,836 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# This file is part of jack_mixer
#
# Copyright (C) 2006 Nedko Arnaudov <nedko@arnaudov.name>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.

class SerializationBackend:
    '''Base class for serialization backends'''
    def get_root_serialization_object(self, name):
        '''Returns serialization object where properties of root object
           will be serialized to'''
        # this method should never be called for the base class
        raise NotImplementedError

    def get_child_serialization_object(self, name, backend_object):
        # this method should never be called for the base class
        raise NotImplementedError

class SerializationObjectBackend:
    '''Base class for serialization backend objects where real object
       properties will be serialized to or unserialized from.'''
    def add_property(self, name, value):
        '''Serialize particular property'''
        pass

    def get_childs(self):
        pass

    def get_properties(self):
        pass

    def serialization_name(self):
        return None

class SerializedObject:
    '''Base class for object supporting serialization'''
    def serialization_name(self):
        return None

    def serialize(self, object_backend):
        '''Serialize properties of called object into supplied serialization_object_backend'''
        pass

    def serialization_get_childs(self):
        '''Get child objects tha required and support serialization'''
        return []

    def unserialize_property(self, name, value):
        pass

    def unserialize_child(self, name):
        return None

class Serializator:
    def __init__(self):
        pass

    def serialize(self, root, backend):
        self.serialize_one(backend, root, backend.get_root_serialization_object(root.serialization_name()))

    def unserialize(self, root, backend):
        backend_object = backend.get_root_unserialization_object(root.serialization_name())
        if backend_object == None:
            return False

        return self.unserialize_one(backend, root, backend_object)

    def unserialize_one(self, backend, object, backend_object):
        #print "Unserializing " + repr(object)
        properties = backend_object.get_properties()
        for name, value in properties.iteritems():
            #print "%s = %s" % (name, value)
            if not object.unserialize_property(name, value):
                return False

        backend_childs = backend_object.get_childs()
        for backend_child in backend_childs:
            name = backend_child.serialization_name()
            child = object.unserialize_child(name)
            if not child:
                return False
            if not self.unserialize_one(backend, child, backend_child):
                return False

        return True

    def serialize_one(self, backend, object, backend_object):
        object.serialize(backend_object)
        childs = object.serialization_get_childs()
        for child in childs:
            #print "serializing child " + repr(child)
            self.serialize_one(backend, child,
                               backend.get_child_serialization_object(
                                       child.serialization_name(), backend_object))