src.tam.Direction.opposite()   B
last analyzed

Complexity

Conditions 5

Size

Total Lines 11

Duplication

Lines 0
Ratio 0 %
Metric Value
cc 5
dl 0
loc 11
rs 8.5454
1
from enum import Enum
2
3
import toolz
4
from pyrsistent import PRecord, field, pmap_field
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,), value_type=Glue)  # TODO: invariant?
50
51
52
def join(tt1, tt2):
53
    pass
54
55
56
def add_transition():
57
    pass
58
59
60
def dirbind(tile, direction=Direction.North):
61
    return tile.glues[direction].strength
62
63
64
def dirlabel(tile, direction=Direction.North):
65
    return tile.glues[direction].label
66
67
68
northbind = toolz.partial(dirbind, direction=Direction.North)
69
southbind = toolz.partial(dirbind, direction=Direction.South)
70
eastbind = toolz.partial(dirbind, direction=Direction.East)
71
westbind = toolz.partial(dirbind, direction=Direction.West)
72
73
74
northlabel = toolz.partial(dirlabel, direction=Direction.North)
75
southlabel = toolz.partial(dirlabel, direction=Direction.South)
76
eastlabel = toolz.partial(dirlabel, direction=Direction.East)
77
westlabel = toolz.partial(dirlabel, direction=Direction.West)
78
79
80
def new_tile(name, label="", tilecolor="white", textcolor="black", concentration=1, glues=None):
81
    if glues is None:
82
        glues = {
83
            Direction.North: Glue(label="", strength=0),
84
            Direction.South: Glue(label="", strength=0),
85
            Direction.West: Glue(label="", strength=0),
86
            Direction.East: Glue(label="", strength=0),
87
        }
88
    # TODO: validate
89
    return Tile(name=name,
90
                label=label,
91
                tilecolor=tilecolor,
92
                textcolor=textcolor,
93
                concentration=concentration,
94
                glues=glues)
95
96
97
def format_tile(tile):
98
    return ("TILENAME {t.name}\n"
99
            "LABEL {t.label}\n"
100
            "TILECOLOR {t.tilecolor}\n"
101
            "TEXTCOLOR {t.textcolor}\n"
102
            "CONCENTRATION {t.concentration}\n"
103
            "NORTHBIND {northbind}\n"
104
            "SOUTHBIND {southbind}\n"
105
            "WESTBIND {westbind}\n"
106
            "EASTBIND {eastbind}\n"
107
            "NORTHLABEL {northlabel}\n"
108
            "SOUTHLABEL {southlabel}\n"
109
            "WESTLABEL {westlabel}\n"
110
            "EASTLABEL {eastlabel}\n"
111
            "CREATE".format(t=tile,
112
                            northbind=northbind(tile),
113
                            southbind=southbind(tile),
114
                            eastbind=eastbind(tile),
115
                            westbind=westbind(tile),
116
                            northlabel=northlabel(tile),
117
                            southlabel=southlabel(tile),
118
                            eastlabel=eastlabel(tile),
119
                            westlabel=westlabel(tile),
120
                            ))
121