Completed
Push — master ( c473c3...c82ddc )
by Luiz
01:01
created

src.tam.add_transition()   A

Complexity

Conditions 1

Size

Total Lines 2

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 1
dl 0
loc 2
rs 10
1
from enum import Enum
2
3
from pyrsistent import field, PRecord, pmap_field
4
import toolz
5
6
try:
7
    basestring
8
except NameError:
9
    basestring = str
10
11
__all__ = ["Direction", "Glue",
12
           "add_transition", "join",
13
           "Tile", "format_tile", "new_tile",
14
           "dirbind", "eastbind", "westbind", "northbind", "southbind",
15
           "dirlabel", "eastlabel", "westlabel", "northlabel", "southlabel"]
16
17
18
class Direction(Enum):
19
    North = 1
20
    South = 2
21
    East = 3
22
    West = 4
23
24
    def opposite(self):
25
        if self == Direction.North:
26
            return Direction.South
27
        elif self == Direction.South:
28
            return Direction.North
29
        elif self == Direction.West:
30
            return Direction.East
31
        elif self == Direction.East:
32
            return Direction.West
33
        else:
34
            raise TypeError("Invalid direction")
35
36
37
class Glue(PRecord):
38
    label = field(basestring, initial="")
39
    strength = field(int, initial=0,
40
                     invariant=lambda x: (x in (0, 1, 2), "invalid strength"))
41
42
43
class Tile(PRecord):
44
    name = field(basestring)
45
    label = field(basestring)
46
    tilecolor = field(basestring)  # TODO: enum for colors?
47
    textcolor = field(basestring)
48
    concentration = field(int)
49
    glues = pmap_field(key_type=(Direction, type(Direction.North)),
50
                       value_type=Glue)  # TODO: invariant?
51
52
53
def join(tt1, tt2):
54
    pass
55
56
57
def add_transition():
58
    pass
59
60
61
def dirbind(tile, direction=Direction.North):
62
    return tile.glues[direction].strength
63
64
65
def dirlabel(tile, direction=Direction.North):
66
    return tile.glues[direction].label
67
68
69
northbind = toolz.partial(dirbind, direction=Direction.North)
70
southbind = toolz.partial(dirbind, direction=Direction.South)
71
eastbind = toolz.partial(dirbind, direction=Direction.East)
72
westbind = toolz.partial(dirbind, direction=Direction.West)
73
74
75
northlabel = toolz.partial(dirlabel, direction=Direction.North)
76
southlabel = toolz.partial(dirlabel, direction=Direction.South)
77
eastlabel = toolz.partial(dirlabel, direction=Direction.East)
78
westlabel = toolz.partial(dirlabel, direction=Direction.West)
79
80
81
def new_tile(name, label="", tilecolor="white", textcolor="black", concentration=1, glues=None):
82
    if glues is None:
83
        glues = {
84
            Direction.North: Glue(label="", strength=0),
85
            Direction.South: Glue(label="", strength=0),
86
            Direction.West: Glue(label="", strength=0),
87
            Direction.East: Glue(label="", strength=0),
88
        }
89
    # TODO: validate
90
    return Tile(name=name,
91
                label=label,
92
                tilecolor=tilecolor,
93
                textcolor=textcolor,
94
                concentration=concentration,
95
                glues=glues)
96
97
98
def format_tile(tile):
99
    return ("TILENAME {t.name}\n"
100
            "LABEL {t.label}\n"
101
            "TILECOLOR {t.tilecolor}\n"
102
            "TEXTCOLOR {t.textcolor}\n"
103
            "CONCENTRATION {t.concentration}\n"
104
            "NORTHBIND {northbind}\n"
105
            "SOUTHBIND {southbind}\n"
106
            "WESTBIND {westbind}\n"
107
            "EASTBIND {eastbind}\n"
108
            "NORTHLABEL {northlabel}\n"
109
            "SOUTHLABEL {southlabel}\n"
110
            "WESTLABEL {westlabel}\n"
111
            "EASTLABEL {eastlabel}\n"
112
            "CREATE".format(t=tile,
113
                            northbind=northbind(tile),
114
                            southbind=southbind(tile),
115
                            eastbind=eastbind(tile),
116
                            westbind=westbind(tile),
117
                            northlabel=northlabel(tile),
118
                            southlabel=southlabel(tile),
119
                            eastlabel=eastlabel(tile),
120
                            westlabel=westlabel(tile),
121
                            ))
122