diff --git a/src/diana/sgfParser/propValues.py b/src/diana/sgfParser/propValues.py --- a/src/diana/sgfParser/propValues.py +++ b/src/diana/sgfParser/propValues.py @@ -1,6 +1,6 @@ import re -from . import ParserError, skipWhitespace +from . import ParserError, skip_whitespace class Regexp: @@ -8,14 +8,14 @@ class Regexp: real = re.compile(r"(\+|-|)\d+(\.\d+)?") point = re.compile(r"[a-zA-Z]{2}|") text = re.compile(r"(?:.*?[^\\])??(?:\\\\)*(?=])", re.DOTALL) - composedText = re.compile(r"(?:.*?[^\\])??(?:\\\\)*(?=]|:)", re.DOTALL) + composed_text = re.compile(r"(?:.*?[^\\])??(?:\\\\)*(?=]|:)", re.DOTALL) class Text: - softBreaks = re.compile(r"(^|[^\\])((\\\\)*)\\((\n\r)|(\r\n)|\r|\n)") + soft_breaks = re.compile(r"(^|[^\\])((\\\\)*)\\((\n\r)|(\r\n)|\r|\n)") whitespace = re.compile(r"[\t\f\v]") - simpleWhitespace = re.compile(r"[\t\f\v\n\r]") - removeSlashes = re.compile(r"(^|[^\\])((\\\\)*)\\($|[^\\])") - unescapeSlashes = re.compile(r"\\\\") + simple_whitespace = re.compile(r"[\t\f\v\n\r]") + remove_slashes = re.compile(r"(^|[^\\])((\\\\)*)\\($|[^\\])") + unescape_slashes = re.compile(r"\\\\") class Composed: @@ -44,9 +44,9 @@ class Point: ## Metatype matching one of the provided types. # # Returns the first match, so the order is important. -def choose(*vTypes): +def choose(*v_types): def f(s, start): - for vType in vTypes: + for vType in v_types: try: (i, x) = vType(s, start) return (i, x) @@ -56,50 +56,50 @@ def choose(*vTypes): return f -def singletonFits(s, i): +def singleton_fits(s, i): return i < len(s) and s[i] == "[" -def singletonEnds(s, i): +def singleton_ends(s, i): return i < len(s) and s[i] == "]" -def singleton(vType): +def singleton(v_type): def f(s, start): - if not singletonFits(s, start): + if not singleton_fits(s, start): raise ParserError("expected a property value starting with '['", s, start) - (i, x) = vType(s, start+1) - if not singletonEnds(s, i): + (i, x) = v_type(s, start + 1) + if not singleton_ends(s, i): raise ParserError("expected a property value ending with ']'", s, i) - i = skipWhitespace(s, i+1) + i = skip_whitespace(s, i + 1) return (i, x) return f -def listOf(vType, allowEmpty=False): +def list_of(v_type, allow_empty=False): def f(s, start): i = start - if not singletonFits(s, i): + if not singleton_fits(s, i): raise ParserError("expected a property value starting with '['", s, i) - if singletonEnds(s, i+1) and allowEmpty: - i = skipWhitespace(s, i+2) + if singleton_ends(s, i + 1) and allow_empty: + i = skip_whitespace(s, i + 2) return (i, []) - single = singleton(vType) + single = singleton(v_type) (i, x) = single(s, i) res = [x] - while singletonFits(s, i): + while singleton_fits(s, i): (i, x) = single(s, i) res.append(x) return (i, res) return f -def compose(vTypeA, vTypeB): +def compose(v_type_a, v_type_b): def f(s, start): - (i, a) = vTypeA(s, start) + (i, a) = v_type_a(s, start) if i >= len(s) or s[i] != ":": raise ParserError("expected a composed property value separated by ':'", s, i) - (i, b) = vTypeB(s, i+1) + (i, b) = v_type_b(s, i + 1) return (i, Composed(a, b)) return f @@ -137,15 +137,15 @@ def color(s,start): def text(simple=True, composed=False): def f(s, start): regexps = Regexp.Text - m = Regexp.composedText.match(s, start) if composed else Regexp.text.match(s, start) + m = Regexp.composed_text.match(s, start) if composed else Regexp.text.match(s, start) res = m.group(0) - res = regexps.softBreaks.sub(r"\1\2", res) # remove soft line breaks + res = regexps.soft_breaks.sub(r"\1\2", res) # remove soft line breaks if simple: - res = regexps.simpleWhitespace.sub(" ", res) # convert whitespace to spaces, no escapes + res = regexps.simple_whitespace.sub(" ", res) # convert whitespace to spaces, no escapes else: res = regexps.whitespace.sub(" ", res) # convert whitespace to spaces, no escapes - res = regexps.removeSlashes.sub(r"\1\2\4", res) - res = regexps.unescapeSlashes.sub(r"\\", res) # unescape slashes + res = regexps.remove_slashes.sub(r"\1\2\4", res) + res = regexps.unescape_slashes.sub(r"\\", res) # unescape slashes return (m.end(), res) return f