reorg to avoid circular import

This commit is contained in:
Manuel Barkhau 2018-12-09 14:49:13 +01:00
parent aafcc459f1
commit 9c2883d8ef
7 changed files with 172 additions and 171 deletions

View file

@ -3,63 +3,18 @@
#
# Copyright (c) 2018 Manuel Barkhau (@mbarkhau) - MIT License
# SPDX-License-Identifier: MIT
"""Parse PyCalVer strings.
>>> version_info = PYCALVER_RE.match("v201712.0123-alpha").groupdict()
>>> assert version_info == {
... "version" : "v201712.0123-alpha",
... "calver" : "v201712",
... "year" : "2017",
... "month" : "12",
... "build" : ".0123",
... "release" : "-alpha",
... }
>>>
>>> version_info = PYCALVER_RE.match("v201712.0033").groupdict()
>>> assert version_info == {
... "version" : "v201712.0033",
... "calver" : "v201712",
... "year" : "2017",
... "month" : "12",
... "build" : ".0033",
... "release" : None,
... }
"""
"""Parse PyCalVer strings from files."""
import re
import logging
import typing as typ
from . import version
log = logging.getLogger("pycalver.parse")
VALID_RELESE_VALUES = ("alpha", "beta", "dev", "rc", "post")
VALID_RELEASE_VALUES = ("alpha", "beta", "dev", "rc", "post")
# https://regex101.com/r/fnj60p/10
PYCALVER_PATTERN = r"""
\b
(?P<version>
(?P<calver>
v # "v" version prefix
(?P<year>\d{4})
(?P<month>\d{2})
)
(?P<build>
\. # "." build nr prefix
\d{4,}
)
(?P<release>
\- # "-" release prefix
(?:alpha|beta|dev|rc|post)
)?
)(?:\s|$)
"""
PYCALVER_RE: typ.Pattern[str] = re.compile(PYCALVER_PATTERN, flags=re.VERBOSE)
PATTERN_ESCAPES = [
("\u005c", "\u005c\u005c"),
("-" , "\u005c-"),
@ -86,41 +41,6 @@ RE_PATTERN_PARTS = {
}
class VersionInfo(typ.NamedTuple):
"""Container for parsed version string."""
version : str
pep440_version: str
calver : str
year : str
month : str
build : str
release : typ.Optional[str]
def parse_version_info(version_str: str) -> VersionInfo:
"""Parse a PyCalVer string.
>>> vnfo = parse_version_info("v201712.0033-beta")
>>> assert vnfo == VersionInfo(
... version="v201712.0033-beta",
... pep440_version="201712.33b0",
... calver="v201712",
... year="2017",
... month="12",
... build=".0033",
... release="-beta",
... )
"""
match = PYCALVER_RE.match(version_str)
if match is None:
raise ValueError(f"Invalid PyCalVer string: {version_str}")
kwargs = match.groupdict()
kwargs['pep440_version'] = version.pycalver_to_pep440(kwargs['version'])
return VersionInfo(**kwargs)
class PatternMatch(typ.NamedTuple):
"""Container to mark a version string in a file."""