formatting updates

This commit is contained in:
Manuel Barkhau 2018-11-06 21:45:33 +01:00
parent 3385f2d675
commit 1129de0beb
11 changed files with 153 additions and 128 deletions

View file

@ -1,7 +1,7 @@
# This file is part of the pycalver project
# https://github.com/mbarkhau/pycalver
#
# (C) 2018 Manuel Barkhau (@mbarkhau)
# Copyright (c) 2018 Manuel Barkhau (@mbarkhau) - MIT License
# SPDX-License-Identifier: MIT
import re
@ -17,7 +17,7 @@ VALID_RELESE_VALUES = ("alpha", "beta", "dev", "rc", "post")
# https://regex101.com/r/fnj60p/10
PYCALVER_RE: typ.Pattern[str] = re.compile(r"""
PYCALVER_PATTERN = r"""
\b
(?P<version>
(?P<calver>
@ -34,8 +34,19 @@ PYCALVER_RE: typ.Pattern[str] = re.compile(r"""
(?:alpha|beta|dev|rc|post)
)?
)(?:\s|$)
""", flags=re.VERBOSE)
"""
PYCALVER_RE: typ.Pattern[str] = re.compile(PYCALVER_PATTERN, flags=re.VERBOSE)
PATTERN_ESCAPES = [
("\u005c", "\u005c\u005c"),
("-" , "\u005c-"),
("." , "\u005c."),
("+" , "\u005c+"),
("*" , "\u005c*"),
("[" , "\u005c["),
("(" , "\u005c("),
]
# NOTE (mb 2018-09-03): These are matchers for parts, which are
# used in the patterns, they're not for validation. This means
@ -45,32 +56,32 @@ PYCALVER_RE: typ.Pattern[str] = re.compile(r"""
RE_PATTERN_PARTS = {
"pep440_version" : r"\d{6}\.[1-9]\d*(a|b|dev|rc|post)?\d*",
"version" : r"v\d{6}\.\d{4,}(\-(alpha|beta|dev|rc|post))?",
"calver" : r"v\d{6}",
"build" : r"\.\d{4,}",
"release" : r"(\-(alpha|beta|dev|rc|post))?",
'pep440_version': r"\d{6}\.[1-9]\d*(a|b|dev|rc|post)?\d*",
'version' : r"v\d{6}\.\d{4,}(\-(alpha|beta|dev|rc|post))?",
'calver' : r"v\d{6}",
'build' : r"\.\d{4,}",
'release' : r"(\-(alpha|beta|dev|rc|post))?",
}
class PatternMatch(typ.NamedTuple):
lineno : int # zero based
line : str
pattern : str
span : typ.Tuple[int, int]
match : str
lineno : int # zero based
line : str
pattern: str
span : typ.Tuple[int, int]
match : str
class VersionInfo(typ.NamedTuple):
pep440_version : str
version : str
calver : str
year : str
month : str
build : str
release : typ.Optional[str]
pep440_version: str
version : str
calver : str
year : str
month : str
build : str
release : typ.Optional[str]
def parse_version_info(version: str) -> VersionInfo:
@ -81,20 +92,17 @@ def parse_version_info(version: str) -> VersionInfo:
return VersionInfo(pep440_version=pep440_version, **match.groupdict())
def iter_pattern_matches(lines: typ.List[str], pattern: str) -> typ.Iterable[PatternMatch]:
def _iter_pattern_matches(lines: typ.List[str], pattern: str) -> typ.Iterable[PatternMatch]:
# The pattern is escaped, so that everything besides the format
# string variables is treated literally.
pattern_re = re.compile(
pattern
.replace("\\", "\\\\")
.replace("-", "\\-")
.replace(".", "\\.")
.replace("+", "\\+")
.replace("*", "\\*")
.replace("[", "\\[")
.replace("(", "\\(")
.format(**RE_PATTERN_PARTS)
)
pattern_tmpl = pattern
for char, escaped in PATTERN_ESCAPES:
pattern_tmpl = pattern_tmpl.replace(char, escaped)
pattern_str = pattern_tmpl.format(**RE_PATTERN_PARTS)
pattern_re = re.compile(pattern_str)
for lineno, line in enumerate(lines):
match = pattern_re.search(line)
if match:
@ -104,5 +112,5 @@ def iter_pattern_matches(lines: typ.List[str], pattern: str) -> typ.Iterable[Pat
def parse_patterns(lines: typ.List[str], patterns: typ.List[str]) -> typ.List[PatternMatch]:
all_matches: typ.List[PatternMatch] = []
for pattern in patterns:
all_matches.extend(iter_pattern_matches(lines, pattern))
all_matches.extend(_iter_pattern_matches(lines, pattern))
return all_matches