Skip to content

Commit

Permalink
♻️ refactor: improve cli
Browse files Browse the repository at this point in the history
  • Loading branch information
zrr1999 committed Jul 23, 2024
1 parent f92df8e commit 88ba1c7
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 31 deletions.
3 changes: 2 additions & 1 deletion pdm_build.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
20 changes: 10 additions & 10 deletions pyfuture/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()

Expand All @@ -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)

Expand All @@ -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"):
Expand All @@ -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)

Expand Down
21 changes: 1 addition & 20 deletions pyfuture/hooks/pdm.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
from __future__ import annotations

import os
import sys
from pathlib import Path

from pdm.backend.hooks.base import Context
Expand All @@ -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", {})

Expand Down
19 changes: 19 additions & 0 deletions pyfuture/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import contextlib
import io
import sys
from pathlib import Path

import libcst as cst
Expand All @@ -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,
Expand Down

0 comments on commit 88ba1c7

Please sign in to comment.