diff --git a/src/diana/sgfParser/property.py b/src/diana/sgfParser/property.py --- a/src/diana/sgfParser/property.py +++ b/src/diana/sgfParser/property.py @@ -2,8 +2,8 @@ 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 +from .propValues import choose, singleton, list_of, compose, number, real, double, color, text, empty, anything, point, move, stone +from . import skip_whitespace, ParserError GAME_INFO = 1 UNKNOWN = 99 @@ -14,7 +14,7 @@ class DateException(Exception): class Property: - identRegexp = re.compile(r"[A-Z]+") + ident_regexp = re.compile(r"[A-Z]+") def __init__(self): self.name = "" @@ -29,38 +29,38 @@ class Property: assert Property.fits(s, start) res = Property() (i, res.name) = Property.ident(s, start) - i = skipWhitespace(s, i) + i = skip_whitespace(s, i) try: - (i, x) = Property.createValue(s, i, res.name) + (i, x) = Property.create_value(s, i, res.name) except ParserError as e: # a malformed value log.warning(e) - (i, x) = choose(listOf(anything), singleton(anything))(s, i) + (i, x) = choose(list_of(anything), singleton(anything))(s, i) res.name = "_"+res.name res.value = x if res.name == "DT": res = DateProperty(x) - i = skipWhitespace(s, i) + i = skip_whitespace(s, i) return (i, res) @staticmethod def ident(s, start): - m = Property.identRegexp.match(s, start) + m = Property.ident_regexp.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): + def create_value(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) + return choose(list_of(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: + game_info = {"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 game_info: return GAME_INFO else: return UNKNOWN @@ -83,9 +83,9 @@ class Property: "KO": singleton(empty), "MN": singleton(number), "W": singleton(move), - "AB": listOf(stone), # - "AE": listOf(point), # - "AW": listOf(stone), # + "AB": list_of(stone), # + "AE": list_of(point), # + "AW": list_of(stone), # "PL": singleton(color), "C": singleton(text(simple=False)), "DM": singleton(double), @@ -99,15 +99,15 @@ class Property: "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), # + "AR": list_of(compose(point, point)), # + "CR": list_of(point), # + "DD": list_of(point, allow_empty=True), # + "LB": list_of(compose(point, text())), # + "LN": list_of(compose(point, point)), # + "MA": list_of(point), # + "SL": list_of(point), # + "SQ": list_of(point), # + "TR": list_of(point), # "AP": singleton(compose(text(composed=True), text())), # "CA": singleton(text()), "FF": singleton(number), @@ -141,13 +141,13 @@ class Property: "WL": singleton(real), "FG": choose(singleton(empty), singleton(compose(number, text()))), # "PM": singleton(number), - "VW": listOf(point, allowEmpty=True), # + "VW": list_of(point, allow_empty=True), # # go specific "HA": singleton(number), "KM": singleton(real), - "TB": listOf(point, allowEmpty=True), - "TW": listOf(point, allowEmpty=True) + "TB": list_of(point, allow_empty=True), + "TW": list_of(point, allow_empty=True) } @@ -156,7 +156,7 @@ class DateProperty(Property): super().__init__() self.name = "DT" self.value = [] - self.rawValue = value + self.raw_value = value self.parse(value) def parse(self, s): @@ -165,21 +165,21 @@ class DateProperty(Property): if not match: raise DateException('Could not parse a DT value: "{0}"'.format(s)) substr = match.group(0) - dateStrs = substr.split(",") + date_strs = substr.split(",") dates = [] - prevFormat = None + prev_format = None - for s in dateStrs: + for s in date_strs: try: - (prevFormat, d) = DateProperty.parseSingle(s, prevFormat, dates[-1] if dates else None) + (prev_format, d) = DateProperty.parse_single(s, prev_format, 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("-") + def parse_single(date_str, prev_format, prev=None): + tokens = date_str.split("-") num_tokens = list(map(int, tokens)) if len(tokens) == 3: return ("YMD", date(*num_tokens)) @@ -191,7 +191,7 @@ class DateProperty(Property): else: if len(tokens[0]) == 4: return ("Y", date(*num_tokens, 1, 1)) - elif prevFormat in ("YM","M"): + elif prev_format in ("YM", "M"): return ("M", date(prev.year, *num_tokens, 1)) else: return ("D", date(prev.year, prev.month, *num_tokens))