-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblacklistparser.py
99 lines (91 loc) · 3.16 KB
/
blacklistparser.py
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
import re
from transformations import *
from blacklistitem import BlacklistItem, BlacklistItemDirect
from constants import MAX_COORD
from helper import calc_coord
# Parses tipp blacklist patterns from a visual ascii representation. For
# example:
#
# """
# name pyramid-a
# transform R, L, F
# missing 1
# ..x
# .xxx
# x...x
# """
#
# Dots are ignored, x marks numbers.
#
# misses: Amount of coords that don't have to match
# transformations: List of transformations that should be applied to the
# pattern before testing. L, R - Rotate, F - Flip, M - Mirror, LM - Left then
# Mirror , RM - Right then Mirror
class BlacklistParser:
transformations = {
'L90': L90(),
'L180': L180(),
'R90': R90(),
'R180': R180(),
'M': Mirror(),
'ML90': ML90(),
'ML180': ML180(),
'MR90': MR90(),
'MR180': MR180(),
}
def parse(pattern):
coords = []
lines = pattern.splitlines()
misses = 0
name = ''
transform = []
direct_numbers = []
y = 0
min_x = MAX_COORD
for line in lines:
line = line.strip()
if not line:
continue
result = re.match(r'\s*numbers\s+(.+)', line)
if result:
direct_numbers = result.group(1).split(',')
direct_numbers = [int(number) for number in direct_numbers]
continue
result = re.match(r'\s*miss(?:es|ing)\s+(\d+)', line)
if result:
misses = int(result.group(1))
continue
result = re.match(r'\s*name\s+(.+)', line)
if result:
name = result.group(1)
continue
result = re.match(r'\s*transform\s+(.*)', line)
# Note: ignore transform config, apply them all now and filter
# duplicates later
if result:
transform.extend(BlacklistParser.transformations.values())
continue
# transform_suffixes = result.group(1).split(',')
# for transformation_suffix in transform_suffixes:
# transformation_suffix = transformation_suffix.strip()
# transform.append(BlacklistParser.transformations[transformation_suffix])
# continue
x = 0
for char in line:
if char == " " or char == "\t":
continue
elif char == 'x' or char == 'X':
min_x = min(min_x, x)
coords.append([x, y])
x += 1
elif char == '.':
x += 1
else:
raise ValueError("Unexpected chars in line: '%s' (%s)" % (line, name))
y += 1
if x > 7 or y > 7:
raise ValueError("Coords out of bounds: %dx%d (%s)" % (max(0, x - 1), y - 1, name))
if direct_numbers:
return BlacklistItemDirect(name, direct_numbers, misses)
else:
return BlacklistItem(name, coords, misses, transform)