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

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

View file

@ -13,12 +13,11 @@ import logging
import pathlib2 as pl
import pycalver.version as v1version
import pycalver.patterns as v1patterns
from pycalver import parse
from pycalver import config
from . import version
from . import patterns
logger = logging.getLogger("pycalver.rewrite")
@ -78,12 +77,12 @@ def iter_file_paths(
def rewrite_lines(
pattern_strs: typ.List[str],
new_vinfo : version.VersionInfo,
new_vinfo : v1version.VersionInfo,
old_lines : typ.List[str],
) -> typ.List[str]:
"""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}"']
>>> rewrite_lines(pattern_strs, new_vinfo, ['__version__ = "v201809.0002-beta"'])
['__version__ = "v201811.0123-beta"']
@ -97,7 +96,7 @@ def rewrite_lines(
for match in parse.iter_matches(old_lines, pattern_strs):
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
new_line = match.line[:span_l] + replacement + match.line[span_r:]
new_lines[match.lineno] = new_line
@ -106,7 +105,7 @@ def rewrite_lines(
if non_matched_patterns:
for non_matched_pattern in non_matched_patterns:
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}'")
raise NoPatternMatch("Invalid pattern(s)")
else:
@ -115,19 +114,19 @@ def rewrite_lines(
def rfd_from_content(
pattern_strs: typ.List[str],
new_vinfo : version.VersionInfo,
new_vinfo : v1version.VersionInfo,
content : str,
) -> RewrittenFileData:
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}"']
>>> content = '__version__ = "v201809.0001-alpha"'
>>> rfd = rfd_from_content(pattern_strs, new_vinfo, content)
>>> rfd.new_lines
['__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}"']
>>> content = '__version__ = "v1.2.2"'
>>> rfd = rfd_from_content(pattern_strs, new_vinfo, content)
@ -142,12 +141,12 @@ def rfd_from_content(
def iter_rewritten(
file_patterns: config.PatternsByGlob,
new_vinfo : version.VersionInfo,
new_vinfo : v1version.VersionInfo,
) -> typ.Iterable[RewrittenFileData]:
r'''Iterate over files with version string replaced.
>>> 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)
>>> rfd = list(rewritten_datas)[0]
>>> expected = [
@ -192,10 +191,10 @@ def diff_lines(rfd: RewrittenFileData) -> typ.List[str]:
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.
>>> new_vinfo = version.parse_version_info("v201809.0123")
>>> new_vinfo = v1version.parse_version_info("v201809.0123")
>>> file_patterns = {"src/pycalver/__init__.py": ['__version__ = "{pycalver}"']}
>>> diff_str = diff(new_vinfo, file_patterns)
>>> lines = diff_str.split("\n")
@ -233,7 +232,7 @@ def diff(new_vinfo: version.VersionInfo, file_patterns: config.PatternsByGlob) -
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."""
fobj: typ.IO[str]

View file

@ -12,7 +12,7 @@ import datetime as dt
import lexid
import pkg_resources
from . import patterns
import pycalver.patterns as v1patterns
logger = logging.getLogger("pycalver.version")
@ -263,7 +263,7 @@ class PatternError(Exception):
def _parse_pattern_groups(pattern_groups: PatternGroups) -> FieldValues:
for part_name in pattern_groups.keys():
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:
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}")
>>> 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)
if match is None:
err_msg = (
@ -414,7 +414,7 @@ def format_version(vinfo: VersionInfo, pattern: str) -> str:
'v1.02.034'
"""
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)
kwargs: typ.Dict[str, typ.Union[str, int, None]] = vinfo._asdict()