Completed
Push — master ( 64c77b...88c52a )
by Luiz
01:10
created

src.tam.Glue   A

Complexity

Total Complexity 1

Size/Duplication

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