summaryrefslogtreecommitdiff
path: root/morphlib/blobs.py
blob: 1c8686b04678e34eeb9e255b91bc36f1861ad7c3 (plain)
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
# Copyright (C) 2012  Codethink Limited
# 
# 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 Street, Fifth Floor, Boston, MA 02110-1301 USA.


class Blob(object):

    @staticmethod
    def create_blob(morph):
        if morph.kind == 'stratum':
            return Stratum(morph)
        elif morph.kind == 'chunk':
            return Chunk(morph)
        elif morph.kind == 'system':
            return System(morph)
        else:
            raise TypeError('Morphology %s has an unknown type: %s' % 
                            (morph.filename, morph.kind))

    def __init__(self, morph):
        self.parents = []
        self.morph = morph
        self.dependencies = []
        self.dependents = []

    def add_parent(self, parent):
        if not parent in self.parents:
            self.parents.append(parent)

    def remove_parent(self, parent):
        if parent in self.parents:
            self.parents.remove(parent)

    def add_dependency(self, other):
        if not other in self.dependencies:
            self.dependencies.append(other)
        if not self in other.dependents:
            other.dependents.append(self)

    def remove_dependency(self, other):
        self.dependencies.remove(other)
        other.dependents.remove(self)

    def depends_on(self, other):
        return other in self.dependencies

    @property
    def chunks(self):
        if self.morph.chunks:
            return self.morph.chunks
        else:
            return { self.morph.name: ['.'] }

    def __eq__(self, other):
        return self.morph == other.morph

    def __hash__(self):
        return hash(self.morph)

    def __str__(self): # pragma: no cover
        return str(self.morph)


class Chunk(Blob):

    pass


class Stratum(Blob):

    pass


class System(Blob):

    pass