Files
@ cbabe0bae003
Branch filter:
Location: Diana/src/diana/sgfParser/property.py
cbabe0bae003
5.3 KiB
text/x-python
changed tests imports
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 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 | import re
from datetime import date
import logging as log
from .propValues import choose, singleton, listOf, compose, number, real, double, color, text, empty, anything, point, move, stone
from . import skipWhitespace, ParserError
GAME_INFO = 1
UNKNOWN = 99
class DateException(Exception):
pass
class Property:
identRegexp = re.compile(r"[A-Z]+")
def __init__(self):
self.name = ""
self.value = ""
@staticmethod
def fits(s, i):
return i < len(s) and s[i].isupper()
@staticmethod
def create(s, start):
assert Property.fits(s, start)
res = Property()
(i, res.name) = Property.ident(s, start)
i = skipWhitespace(s, i)
try:
(i, x) = Property.createValue(s, i, res.name)
except ParserError as e: # a malformed value
log.warning(e)
(i, x) = choose(listOf(anything), singleton(anything))(s, i)
res.name = "_"+res.name
res.value = x
if res.name == "DT":
res = DateProperty(x)
i = skipWhitespace(s, i)
return (i, res)
@staticmethod
def ident(s, start):
m = Property.identRegexp.match(s, start)
if m is None:
raise ParserError("expected a property identifier matching '[A-Z]+'", s, start)
return (m.end(), m.group())
@staticmethod
def createValue(s, start, name):
if name in Property.patterns:
return Property.patterns[name](s, start)
else:
log.info("unknown property %s at position %d", name, start)
return choose(listOf(anything), singleton(anything))(s, start)
@property
def type(self):
gameInfo = {"AN", "BR", "BT", "CP", "DT", "EV", "GN", "GC", "ON", "OT", "PB", "PC", "PW", "RE", "RO", "RU", "SO", "TM", "US", "WR", "WT"}
if self.name in gameInfo:
return GAME_INFO
else:
return UNKNOWN
def copy(self):
res = Property()
res.name = self.name
res.value = self.value if not isinstance(self.value, list) else self.value[:]
return res
def __str__(self):
name = self.name.lstrip("_")
val = "[{0}]".format(self.value) \
if not isinstance(self.value, list) \
else "".join("[{0}]".format(x) for x in self.value)
return "{0}{1}".format(name, val)
patterns = {
"B": singleton(move),
"KO": singleton(empty),
"MN": singleton(number),
"W": singleton(move),
"AB": listOf(stone), #
"AE": listOf(point), #
"AW": listOf(stone), #
"PL": singleton(color),
"C": singleton(text(simple=False)),
"DM": singleton(double),
"GB": singleton(double),
"GW": singleton(double),
"HO": singleton(double),
"N": singleton(text()),
"UC": singleton(double),
"V": singleton(real),
"BM": singleton(double),
"DO": singleton(empty),
"IT": singleton(empty),
"TE": singleton(double),
"AR": listOf(compose(point, point)), #
"CR": listOf(point), #
"DD": listOf(point, allowEmpty=True), #
"LB": listOf(compose(point, text())), #
"LN": listOf(compose(point, point)), #
"MA": listOf(point), #
"SL": listOf(point), #
"SQ": listOf(point), #
"TR": listOf(point), #
"AP": singleton(compose(text(composed=True), text())), #
"CA": singleton(text()),
"FF": singleton(number),
"GM": singleton(number),
"ST": singleton(number),
"SZ": choose(singleton(number), singleton(compose(number, number))), #
"AN": singleton(text()),
"BR": singleton(text()),
"BT": singleton(text()),
"CP": singleton(text()),
"DT": singleton(text()),
"EV": singleton(text()),
"GN": singleton(text()),
"GC": singleton(text(simple=False)),
"ON": singleton(text()),
"OT": singleton(text()),
"PB": singleton(text()),
"PC": singleton(text()),
"PW": singleton(text()),
"RE": singleton(text()),
"RO": singleton(text()),
"RU": singleton(text()),
"SO": singleton(text()),
"TM": singleton(real),
"US": singleton(text()),
"WR": singleton(text()),
"WT": singleton(text()),
"BL": singleton(real),
"OB": singleton(number),
"OW": singleton(number),
"WL": singleton(real),
"FG": choose(singleton(empty), singleton(compose(number, text()))), #
"PM": singleton(number),
"VW": listOf(point, allowEmpty=True), #
# go specific
"HA": singleton(number),
"KM": singleton(real),
"TB": listOf(point, allowEmpty=True),
"TW": listOf(point, allowEmpty=True)
}
class DateProperty(Property):
def __init__(self, value):
super().__init__()
self.name = "DT"
self.value = []
self.rawValue = value
self.parse(value)
def parse(self, s):
regexp = re.compile(r"\d{4}(-\d\d){0,2}(,(\d{4}(-\d\d){0,2}|\d\d(-\d\d)?))*")
match = re.search(regexp, s)
if not match:
raise DateException('Could not parse a DT value: "{0}"'.format(s))
substr = match.group(0)
dateStrs = substr.split(",")
dates = []
prevFormat = None
for s in dateStrs:
try:
(prevFormat, d) = DateProperty.parseSingle(s, prevFormat, dates[-1] if dates else None)
except ValueError:
raise DateException('Could not parse a DT value: "{0}"'.format(s))
dates.append(d)
self.value = dates
@staticmethod
def parseSingle(dateStr, prevFormat, prev=None):
tokens = dateStr.split("-")
num_tokens = list(map(int, tokens))
if len(tokens) == 3:
return ("YMD", date(*num_tokens))
elif len(tokens) == 2:
if len(tokens[0]) == 4:
return ("YM", date(*num_tokens, 1))
else:
return ("MD", date(prev.year, *num_tokens))
else:
if len(tokens[0]) == 4:
return ("Y", date(*num_tokens, 1, 1))
elif prevFormat in ("YM","M"):
return ("M", date(prev.year, *num_tokens, 1))
else:
return ("D", date(prev.year, prev.month, *num_tokens))
|