diff --git a/pdm_build.py b/pdm_build.py index 32ad434..063fcad 100644 --- a/pdm_build.py +++ b/pdm_build.py @@ -29,8 +29,9 @@ def pdm_build_initialize(context: Context) -> None: def pdm_build_update_files(context: Context, files: dict[str, Path]) -> None: from pyfuture.hooks import pdm as pyfuture_pdm_hooks + from pyfuture.utils import get_target hook_config = pyfuture_pdm_hooks.get_hook_config(context) target_str = pyfuture_pdm_hooks.get_target_str(hook_config) - target = pyfuture_pdm_hooks.get_target(target_str) + target = get_target(target_str) pyfuture_pdm_hooks.pdm_build_update_files(context, files, target) diff --git a/pyfuture/__main__.py b/pyfuture/__main__.py index 4ee6288..783fb6b 100644 --- a/pyfuture/__main__.py +++ b/pyfuture/__main__.py @@ -9,7 +9,7 @@ from rich.logging import RichHandler from rich.style import Style -from pyfuture.utils import transfer_file +from pyfuture.utils import get_target, transfer_file app = typer.Typer() @@ -21,21 +21,21 @@ def init_logger(log_level: str): @app.command() -def transfer(src_file: Path, tgt_file: Path, *, target: str = "3.9", log_level: str = "INFO"): +def transfer(src_file: Path, tgt_file: Path, *, target: str = "py39", log_level: str = "INFO"): """ Transfer code from src_file and write to tgt_file. """ - assert target == "3.9", "PyFuture is very early stage, not support target argument yet" + init_logger(log_level) transfer_file(src_file, tgt_file) @app.command() -def watch(src_file: Path, tgt_file: Path, *, target: str = "3.9", log_level: str = "INFO"): # pragma: no cover +def watch(src_file: Path, tgt_file: Path, *, target: str = "py39", log_level: str = "INFO"): # pragma: no cover """ Transfer all python files in src_dir to build_dir, and watch for changes. """ - assert target == "3.9", "PyFuture is very early stage, not support target argument yet" + init_logger(log_level) transfer_file(src_file, tgt_file) @@ -46,18 +46,18 @@ def watch(src_file: Path, tgt_file: Path, *, target: str = "3.9", log_level: str match mode: case Change.modified: logger.info("Source file has been modified") - transfer_file(Path(path), tgt_file) + transfer_file(Path(path), tgt_file, target=get_target(target)) case Change.deleted: logger.info("Source file has been deleted") break @app.command() -def transfer_dir(src_dir: Path, build_dir: Path, *, target: str = "3.9", log_level: str = "INFO"): +def transfer_dir(src_dir: Path, build_dir: Path, *, target: str = "py39", log_level: str = "INFO"): """ Transfer all python files in src_dir to build_dir. """ - assert target == "3.9", "PyFuture is very early stage, not support target argument yet" + init_logger(log_level) for src_file in src_dir.glob("**/*.py"): @@ -66,11 +66,11 @@ def transfer_dir(src_dir: Path, build_dir: Path, *, target: str = "3.9", log_lev @app.command() -def watch_dir(src_dir: Path, build_dir: Path, *, target: str = "3.9", log_level: str = "INFO"): # pragma: no cover +def watch_dir(src_dir: Path, build_dir: Path, *, target: str = "py39", log_level: str = "INFO"): # pragma: no cover """ Transfer all python files in src_dir to build_dir, and watch for changes. """ - assert target == "3.9", "PyFuture is very early stage, not support target argument yet" + init_logger(log_level) transfer_dir(src_dir, build_dir, target=target) diff --git a/pyfuture/hooks/pdm.py b/pyfuture/hooks/pdm.py index 518cb77..1d976b9 100644 --- a/pyfuture/hooks/pdm.py +++ b/pyfuture/hooks/pdm.py @@ -1,7 +1,6 @@ from __future__ import annotations import os -import sys from pathlib import Path from pdm.backend.hooks.base import Context @@ -26,28 +25,10 @@ def get_target_str(hook_config: dict) -> str | None: """ target_str = os.environ.get("PYFUTURE_TARGET", None) if target_str is None: - target_str = hook_config.get("target", None) + target_str = hook_config.get("target") return target_str -def get_target(target_str: str | None) -> tuple[int, int]: - """ - Get target version from target string. - - Example: - >>> get_target(None) == sys.version_info[:2] - True - >>> get_target("py39") - (3, 9) - >>> get_target("py310") - (3, 10) - """ - if target_str is None: - return sys.version_info[:2] - else: - return (int(target_str[2:3]), int(target_str[3:])) - - def get_hook_config(context: Context) -> dict: # pragma: no cover return context.config.data.get("tool", {}).get("pdm", {}).get("build", {}).get("hooks", {}).get("pyfuture", {}) diff --git a/pyfuture/utils.py b/pyfuture/utils.py index 7eb921c..8cd0c88 100644 --- a/pyfuture/utils.py +++ b/pyfuture/utils.py @@ -2,6 +2,7 @@ import contextlib import io +import sys from pathlib import Path import libcst as cst @@ -10,6 +11,24 @@ from .codemod.utils import RuleSet, get_transformers +def get_target(target_str: str | None) -> tuple[int, int]: + """ + Get target version from target string. + + Example: + >>> get_target(None) == sys.version_info[:2] + True + >>> get_target("py39") + (3, 9) + >>> get_target("py310") + (3, 10) + """ + if target_str is None: + return sys.version_info[:2] + else: + return (int(target_str[2:3]), int(target_str[3:])) + + def apply_transformer( transformers: list[type[Codemod]], code: str,