From 79161341ebf581ca31ea4cbd6ac1645fd4123bd8 Mon Sep 17 00:00:00 2001 From: VioletsOleander <1377232072@qq.com> Date: Tue, 26 May 2026 19:48:27 +0800 Subject: [PATCH 1/2] refactor: random non-important change --- src/vanillian/log/canonicalizer.py | 52 ++++++++++++++------------- src/vanillian/main.py | 4 +-- src/vanillian/{parse.py => parser.py} | 2 +- 3 files changed, 31 insertions(+), 27 deletions(-) rename src/vanillian/{parse.py => parser.py} (90%) diff --git a/src/vanillian/log/canonicalizer.py b/src/vanillian/log/canonicalizer.py index 0cf4b38..3d0cf1f 100644 --- a/src/vanillian/log/canonicalizer.py +++ b/src/vanillian/log/canonicalizer.py @@ -9,59 +9,46 @@ logger = logging.getLogger(__name__) -class _LogSection(StrEnum): - DOC = "Doc" - PAPER = "Paper" - WIKI = "Wiki" - Other = "Other" - - -def _match_section( - line: str, section_patterns: dict[_LogSection, re.Pattern] -) -> _LogSection | None: - """Return the log section type if the line matches any section pattern, else None.""" - for section_type, pattern in section_patterns.items(): - if pattern.fullmatch(line): - return section_type - return None - - class LogCanonicalizer: def __init__(self) -> None: self.section_patterns = { _LogSection.DOC: re.compile(r"\\\[Doc\\\]\s"), _LogSection.PAPER: re.compile(r"\\\[Paper\\\]\s"), _LogSection.WIKI: re.compile(r"\\\[Wiki\\\]\s"), - _LogSection.Other: re.compile(r"\\\[.+\\\]\s"), + _LogSection.OTHER: re.compile(r"\\\[.+\\\]\s"), } self.log_item_pattern = re.compile(r"^- \[\[.+\]\]:?") self.paper_file_name_pattern = re.compile( r"^(?P.+)-(?P<year>\d{4})(-(?P<publisher>.+))?$" ) - def canonicalize(self, lines: list[str], *, force: bool = False) -> None: + def canonicalize(self, lines: list[str], *, force: bool = False) -> list[str]: """Canonicalize log items in the provided lines. - Modifies the input lines in place. - Args: lines (list[str]): The lines of the log file. force (bool, optional): If True, canonicalize without asking for confirmation. Defaults to False. + + Returns: + list[str]: The canonicalized log lines. """ logger.info("Starting log canonicalization.") - current_section = _LogSection.Other + + current_section = _LogSection.OTHER for idx, line in enumerate(lines): if (match := _match_section(line, self.section_patterns)) is not None: - level = logger.info if current_section != _LogSection.Other else logger.debug + level = logger.info if current_section != _LogSection.OTHER else logger.debug level("Line: %d: Entering section: '%s'", idx, match.value) current_section = match - elif self.log_item_pattern.match(line) and current_section != _LogSection.Other: + elif self.log_item_pattern.match(line) and current_section != _LogSection.OTHER: logger.info("Line: %d: Processing...", idx) lines[idx] = self._canonicalize_line(current_section, line, force=force) logger.info("Line: %d: Finished processing.", idx) logger.info("Log canonicalization completed.") + return lines + def _canonicalize_line( self, log_section: _LogSection, @@ -152,3 +139,20 @@ def _canonicalize_item(self, log_section: _LogSection, log_item: str, sep: str = raise ValueError(f"Unhandled log section: {log_section}") return f"{path}{sep}{new_title}" + + +class _LogSection(StrEnum): + DOC = "Doc" + PAPER = "Paper" + WIKI = "Wiki" + OTHER = "Other" + + +def _match_section( + line: str, section_patterns: dict[_LogSection, re.Pattern] +) -> _LogSection | None: + """Return the log section type if the line matches any section pattern, else None.""" + for section_type, pattern in section_patterns.items(): + if pattern.fullmatch(line): + return section_type + return None diff --git a/src/vanillian/main.py b/src/vanillian/main.py index 9e03f12..00e11da 100644 --- a/src/vanillian/main.py +++ b/src/vanillian/main.py @@ -5,7 +5,7 @@ from pathlib import Path from typing import TYPE_CHECKING -from .parse import parse_args +from .parser import parse_args if TYPE_CHECKING: from collections.abc import Callable @@ -47,7 +47,7 @@ def main() -> int: lines = read_lines(log_path) canonicalizer = LogCanonicalizer() - canonicalizer.canonicalize(lines, force=args.force) + lines = canonicalizer.canonicalize(lines, force=args.force) dest_path = log_path if args.overwrite else log_path.with_suffix(".canonicalized.md") write_lines_atomic(dest_path, lines) diff --git a/src/vanillian/parse.py b/src/vanillian/parser.py similarity index 90% rename from src/vanillian/parse.py rename to src/vanillian/parser.py index a6da937..ad894a9 100644 --- a/src/vanillian/parse.py +++ b/src/vanillian/parser.py @@ -12,7 +12,7 @@ def parse_args() -> argparse.Namespace: prog = Path(sys.argv[0]).name parser = argparse.ArgumentParser( prog=prog, - description="The dedicated steward for vault-vanilla.", + description="The toolbox for vault-vanilla.", formatter_class=argparse.ArgumentDefaultsHelpFormatter, ) subparsers = parser.add_subparsers(dest="command", required=True) From 73dc589a94f41af9e83efbbaef9f6f783defb151 Mon Sep 17 00:00:00 2001 From: VioletsOleander <1377232072@qq.com> Date: Tue, 26 May 2026 20:38:51 +0800 Subject: [PATCH 2/2] update --- src/vanillian/log/canonicalizer.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/vanillian/log/canonicalizer.py b/src/vanillian/log/canonicalizer.py index 3d0cf1f..85fa98d 100644 --- a/src/vanillian/log/canonicalizer.py +++ b/src/vanillian/log/canonicalizer.py @@ -25,6 +25,8 @@ def __init__(self) -> None: def canonicalize(self, lines: list[str], *, force: bool = False) -> list[str]: """Canonicalize log items in the provided lines. + Modify the input lines in place. + Args: lines (list[str]): The lines of the log file. force (bool, optional): If True, canonicalize without asking for confirmation. Defaults to False.