wip refactoring

This commit is contained in:
Manuel Barkhau 2020-09-07 21:43:55 +00:00
parent 31f9d51bf3
commit e053af589e
11 changed files with 39 additions and 34 deletions

View file

@ -19,7 +19,8 @@ warn_redundant_casts = True
[tool:isort] [tool:isort]
known_third_party = click,pathlib2 known_first_party = pycalver,pycalver2
known_third_party = click,pathlib2,lexid
force_single_line = True force_single_line = True
length_sort = True length_sort = True
@ -161,3 +162,8 @@ disable =
missing-class-docstring, missing-class-docstring,
missing-function-docstring, missing-function-docstring,
raise-missing-from, raise-missing-from,
duplicate-code,
generated-members =
# members of typing.NamedTuple
"(_replace|_asdict|_fields)",

View file

@ -15,10 +15,10 @@ import logging
import subprocess as sp import subprocess as sp
import click import click
import pycalver2.version as v2version
import pycalver.cli as v1cli import pycalver.cli as v1cli
import pycalver.version as v1version import pycalver.version as v1version
import pycalver2.version as v2version
from pycalver import vcs from pycalver import vcs
from pycalver import config from pycalver import config

View file

@ -13,12 +13,11 @@ import logging
import pathlib2 as pl import pathlib2 as pl
import pycalver.version as v1version
import pycalver.patterns as v1patterns
from pycalver import parse from pycalver import parse
from pycalver import config from pycalver import config
from . import version
from . import patterns
logger = logging.getLogger("pycalver.rewrite") logger = logging.getLogger("pycalver.rewrite")
@ -78,12 +77,12 @@ def iter_file_paths(
def rewrite_lines( def rewrite_lines(
pattern_strs: typ.List[str], pattern_strs: typ.List[str],
new_vinfo : version.VersionInfo, new_vinfo : v1version.VersionInfo,
old_lines : typ.List[str], old_lines : typ.List[str],
) -> typ.List[str]: ) -> typ.List[str]:
"""Replace occurances of pattern_strs in old_lines with new_vinfo. """Replace occurances of pattern_strs in old_lines with new_vinfo.
>>> new_vinfo = version.parse_version_info("v201811.0123-beta") >>> new_vinfo = v1version.parse_version_info("v201811.0123-beta")
>>> pattern_strs = ['__version__ = "{pycalver}"'] >>> pattern_strs = ['__version__ = "{pycalver}"']
>>> rewrite_lines(pattern_strs, new_vinfo, ['__version__ = "v201809.0002-beta"']) >>> rewrite_lines(pattern_strs, new_vinfo, ['__version__ = "v201809.0002-beta"'])
['__version__ = "v201811.0123-beta"'] ['__version__ = "v201811.0123-beta"']
@ -97,7 +96,7 @@ def rewrite_lines(
for match in parse.iter_matches(old_lines, pattern_strs): for match in parse.iter_matches(old_lines, pattern_strs):
found_patterns.add(match.pattern) found_patterns.add(match.pattern)
replacement = version.format_version(new_vinfo, match.pattern) replacement = v1version.format_version(new_vinfo, match.pattern)
span_l, span_r = match.span span_l, span_r = match.span
new_line = match.line[:span_l] + replacement + match.line[span_r:] new_line = match.line[:span_l] + replacement + match.line[span_r:]
new_lines[match.lineno] = new_line new_lines[match.lineno] = new_line
@ -106,7 +105,7 @@ def rewrite_lines(
if non_matched_patterns: if non_matched_patterns:
for non_matched_pattern in non_matched_patterns: for non_matched_pattern in non_matched_patterns:
logger.error(f"No match for pattern '{non_matched_pattern}'") logger.error(f"No match for pattern '{non_matched_pattern}'")
compiled_pattern_str = patterns.compile_pattern_str(non_matched_pattern) compiled_pattern_str = v1patterns.compile_pattern_str(non_matched_pattern)
logger.error(f"Pattern compiles to regex '{compiled_pattern_str}'") logger.error(f"Pattern compiles to regex '{compiled_pattern_str}'")
raise NoPatternMatch("Invalid pattern(s)") raise NoPatternMatch("Invalid pattern(s)")
else: else:
@ -115,19 +114,19 @@ def rewrite_lines(
def rfd_from_content( def rfd_from_content(
pattern_strs: typ.List[str], pattern_strs: typ.List[str],
new_vinfo : version.VersionInfo, new_vinfo : v1version.VersionInfo,
content : str, content : str,
) -> RewrittenFileData: ) -> RewrittenFileData:
r"""Rewrite pattern occurrences with version string. r"""Rewrite pattern occurrences with version string.
>>> new_vinfo = version.parse_version_info("v201809.0123") >>> new_vinfo = v1version.parse_version_info("v201809.0123")
>>> pattern_strs = ['__version__ = "{pycalver}"'] >>> pattern_strs = ['__version__ = "{pycalver}"']
>>> content = '__version__ = "v201809.0001-alpha"' >>> content = '__version__ = "v201809.0001-alpha"'
>>> rfd = rfd_from_content(pattern_strs, new_vinfo, content) >>> rfd = rfd_from_content(pattern_strs, new_vinfo, content)
>>> rfd.new_lines >>> rfd.new_lines
['__version__ = "v201809.0123"'] ['__version__ = "v201809.0123"']
>>> >>>
>>> new_vinfo = version.parse_version_info("v1.2.3", "v{semver}") >>> new_vinfo = v1version.parse_version_info("v1.2.3", "v{semver}")
>>> pattern_strs = ['__version__ = "v{semver}"'] >>> pattern_strs = ['__version__ = "v{semver}"']
>>> content = '__version__ = "v1.2.2"' >>> content = '__version__ = "v1.2.2"'
>>> rfd = rfd_from_content(pattern_strs, new_vinfo, content) >>> rfd = rfd_from_content(pattern_strs, new_vinfo, content)
@ -142,12 +141,12 @@ def rfd_from_content(
def iter_rewritten( def iter_rewritten(
file_patterns: config.PatternsByGlob, file_patterns: config.PatternsByGlob,
new_vinfo : version.VersionInfo, new_vinfo : v1version.VersionInfo,
) -> typ.Iterable[RewrittenFileData]: ) -> typ.Iterable[RewrittenFileData]:
r'''Iterate over files with version string replaced. r'''Iterate over files with version string replaced.
>>> file_patterns = {"src/pycalver/__init__.py": ['__version__ = "{pycalver}"']} >>> file_patterns = {"src/pycalver/__init__.py": ['__version__ = "{pycalver}"']}
>>> new_vinfo = version.parse_version_info("v201809.0123") >>> new_vinfo = v1version.parse_version_info("v201809.0123")
>>> rewritten_datas = iter_rewritten(file_patterns, new_vinfo) >>> rewritten_datas = iter_rewritten(file_patterns, new_vinfo)
>>> rfd = list(rewritten_datas)[0] >>> rfd = list(rewritten_datas)[0]
>>> expected = [ >>> expected = [
@ -192,10 +191,10 @@ def diff_lines(rfd: RewrittenFileData) -> typ.List[str]:
return list(lines) return list(lines)
def diff(new_vinfo: version.VersionInfo, file_patterns: config.PatternsByGlob) -> str: def diff(new_vinfo: v1version.VersionInfo, file_patterns: config.PatternsByGlob) -> str:
r"""Generate diffs of rewritten files. r"""Generate diffs of rewritten files.
>>> new_vinfo = version.parse_version_info("v201809.0123") >>> new_vinfo = v1version.parse_version_info("v201809.0123")
>>> file_patterns = {"src/pycalver/__init__.py": ['__version__ = "{pycalver}"']} >>> file_patterns = {"src/pycalver/__init__.py": ['__version__ = "{pycalver}"']}
>>> diff_str = diff(new_vinfo, file_patterns) >>> diff_str = diff(new_vinfo, file_patterns)
>>> lines = diff_str.split("\n") >>> lines = diff_str.split("\n")
@ -233,7 +232,7 @@ def diff(new_vinfo: version.VersionInfo, file_patterns: config.PatternsByGlob) -
return full_diff return full_diff
def rewrite(file_patterns: config.PatternsByGlob, new_vinfo: version.VersionInfo) -> None: def rewrite(file_patterns: config.PatternsByGlob, new_vinfo: v1version.VersionInfo) -> None:
"""Rewrite project files, updating each with the new version.""" """Rewrite project files, updating each with the new version."""
fobj: typ.IO[str] fobj: typ.IO[str]

View file

@ -12,7 +12,7 @@ import datetime as dt
import lexid import lexid
import pkg_resources import pkg_resources
from . import patterns import pycalver.patterns as v1patterns
logger = logging.getLogger("pycalver.version") logger = logging.getLogger("pycalver.version")
@ -263,7 +263,7 @@ class PatternError(Exception):
def _parse_pattern_groups(pattern_groups: PatternGroups) -> FieldValues: def _parse_pattern_groups(pattern_groups: PatternGroups) -> FieldValues:
for part_name in pattern_groups.keys(): for part_name in pattern_groups.keys():
is_valid_part_name = ( is_valid_part_name = (
part_name in patterns.COMPOSITE_PART_PATTERNS or part_name in PATTERN_PART_FIELDS part_name in v1patterns.COMPOSITE_PART_PATTERNS or part_name in PATTERN_PART_FIELDS
) )
if not is_valid_part_name: if not is_valid_part_name:
err_msg = f"Invalid part '{part_name}'" err_msg = f"Invalid part '{part_name}'"
@ -318,7 +318,7 @@ def parse_version_info(version_str: str, pattern: str = "{pycalver}") -> Version
>>> vnfo = parse_version_info("1.23.456", pattern="{semver}") >>> vnfo = parse_version_info("1.23.456", pattern="{semver}")
>>> assert vnfo == _parse_version_info({'MAJOR': "1", 'MINOR': "23", 'PATCH': "456"}) >>> assert vnfo == _parse_version_info({'MAJOR': "1", 'MINOR': "23", 'PATCH': "456"})
""" """
regex = patterns.compile_pattern(pattern) regex = v1patterns.compile_pattern(pattern)
match = regex.match(version_str) match = regex.match(version_str)
if match is None: if match is None:
err_msg = ( err_msg = (
@ -414,7 +414,7 @@ def format_version(vinfo: VersionInfo, pattern: str) -> str:
'v1.02.034' 'v1.02.034'
""" """
full_pattern = pattern full_pattern = pattern
for part_name, full_part_format in patterns.FULL_PART_FORMATS.items(): for part_name, full_part_format in v1patterns.FULL_PART_FORMATS.items():
full_pattern = full_pattern.replace("{" + part_name + "}", full_part_format) full_pattern = full_pattern.replace("{" + part_name + "}", full_part_format)
kwargs: typ.Dict[str, typ.Union[str, int, None]] = vinfo._asdict() kwargs: typ.Dict[str, typ.Union[str, int, None]] = vinfo._asdict()

View file

@ -14,7 +14,6 @@ import logging
import pycalver2.rewrite as v2rewrite import pycalver2.rewrite as v2rewrite
import pycalver2.version as v2version import pycalver2.version as v2version
from pycalver import config from pycalver import config
logger = logging.getLogger("pycalver2.cli") logger = logging.getLogger("pycalver2.cli")

View file

@ -176,6 +176,8 @@ FULL_PART_FORMATS = {
def _replace_pattern_parts(pattern: str) -> str: def _replace_pattern_parts(pattern: str) -> str:
# The pattern is escaped, so that everything besides the format
# string variables is treated literally.
for part_name, part_pattern in PART_PATTERNS.items(): for part_name, part_pattern in PART_PATTERNS.items():
named_part_pattern = f"(?P<{part_name}>{part_pattern})" named_part_pattern = f"(?P<{part_name}>{part_pattern})"
placeholder = "\u005c{" + part_name + "\u005c}" placeholder = "\u005c{" + part_name + "\u005c}"

View file

@ -9,12 +9,11 @@ import io
import typing as typ import typing as typ
import logging import logging
from pycalver2 import version
from pycalver2 import patterns
from pycalver import parse from pycalver import parse
from pycalver import config from pycalver import config
from pycalver import rewrite as v1rewrite from pycalver import rewrite as v1rewrite
from pycalver2 import version
from pycalver2 import patterns
logger = logging.getLogger("pycalver2.rewrite") logger = logging.getLogger("pycalver2.rewrite")

View file

@ -12,7 +12,8 @@ import datetime as dt
import lexid import lexid
import pkg_resources import pkg_resources
from . import patterns # import pycalver.patterns as v1patterns
import pycalver2.patterns as v2patterns
logger = logging.getLogger("pycalver.version") logger = logging.getLogger("pycalver.version")
@ -300,7 +301,7 @@ class PatternError(Exception):
def _parse_pattern_groups(pattern_groups: PatternGroups) -> FieldValues: def _parse_pattern_groups(pattern_groups: PatternGroups) -> FieldValues:
for part_name in pattern_groups.keys(): for part_name in pattern_groups.keys():
is_valid_part_name = ( is_valid_part_name = (
part_name in patterns.COMPOSITE_PART_PATTERNS or part_name in PATTERN_PART_FIELDS part_name in v2patterns.COMPOSITE_PART_PATTERNS or part_name in PATTERN_PART_FIELDS
) )
if not is_valid_part_name: if not is_valid_part_name:
err_msg = f"Invalid part '{part_name}'" err_msg = f"Invalid part '{part_name}'"
@ -361,7 +362,7 @@ def parse_version_info(version_str: str, pattern: str = "{pycalver}") -> Version
>>> vnfo = parse_version_info("1.23.456", pattern="{semver}") >>> vnfo = parse_version_info("1.23.456", pattern="{semver}")
>>> assert vnfo == _parse_version_info({'MAJOR': "1", 'MINOR': "23", 'PATCH': "456"}) >>> assert vnfo == _parse_version_info({'MAJOR': "1", 'MINOR': "23", 'PATCH': "456"})
""" """
regex = patterns.compile_pattern(pattern) regex = v2patterns.compile_pattern(pattern)
match = regex.match(version_str) match = regex.match(version_str)
if match is None: if match is None:
err_msg = ( err_msg = (
@ -447,7 +448,7 @@ def _compile_format_template(pattern: str, kwargs: TemplateKwargs) -> str:
# NOTE (mb 2020-09-04): Some parts are optional, we need the kwargs to # NOTE (mb 2020-09-04): Some parts are optional, we need the kwargs to
# determine if part is set to its zero value # determine if part is set to its zero value
format_tmpl = pattern format_tmpl = pattern
for part_name, full_part_format in patterns.FULL_PART_FORMATS.items(): for part_name, full_part_format in v2patterns.FULL_PART_FORMATS.items():
format_tmpl = format_tmpl.replace("{" + part_name + "}", full_part_format) format_tmpl = format_tmpl.replace("{" + part_name + "}", full_part_format)
return format_tmpl return format_tmpl

View file

@ -8,10 +8,10 @@ import subprocess as sp
import pytest import pytest
import pathlib2 as pl import pathlib2 as pl
import pycalver2.patterns as patterns
from click.testing import CliRunner from click.testing import CliRunner
import pycalver.config as config import pycalver.config as config
import pycalver2.patterns as patterns
from pycalver.__main__ import cli from pycalver.__main__ import cli
SETUP_CFG_FIXTURE = """ SETUP_CFG_FIXTURE = """

View file

@ -1,9 +1,9 @@
import re import re
import pytest import pytest
import pycalver2.patterns as v2patterns
import pycalver.patterns as v1patterns import pycalver.patterns as v1patterns
import pycalver2.patterns as v2patterns
# TODO (mb 2020-09-06): test for v2patterns # TODO (mb 2020-09-06): test for v2patterns

View file

@ -3,12 +3,11 @@
import copy import copy
from test import util from test import util
from pycalver2 import rewrite as v2rewrite
from pycalver2 import version as v2version
from pycalver import config from pycalver import config
from pycalver import rewrite as v1rewrite from pycalver import rewrite as v1rewrite
from pycalver import version as v1version from pycalver import version as v1version
from pycalver2 import rewrite as v2rewrite
from pycalver2 import version as v2version
REWRITE_FIXTURE = """ REWRITE_FIXTURE = """
# SPDX-License-Identifier: MIT # SPDX-License-Identifier: MIT