Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remove 'decorator' dependency #1513

Merged
merged 13 commits into from
Feb 19, 2024
74 changes: 40 additions & 34 deletions copier/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,9 @@
from os import PathLike
from pathlib import Path
from textwrap import dedent
from typing import Callable

import yaml
from decorator import decorator
from plumbum import cli, colors

from .errors import UnsafeTemplateError, UserMessageError
Expand All @@ -63,12 +63,11 @@
from .types import AnyByStrDict, OptStr, StrSeq


@decorator
def handle_exceptions(method, *args, **kwargs):
def _handle_exceptions(method: Callable[[], None]) -> int:
"""Handle keyboard interruption while running a method."""
try:
try:
return method(*args, **kwargs)
method()
except KeyboardInterrupt:
raise UserMessageError("Execution stopped by user")
except UserMessageError as error:
Expand All @@ -78,6 +77,7 @@ def handle_exceptions(method, *args, **kwargs):
print(colors.red | "\n".join(error.args), file=sys.stderr)
# DOCS https://github.com/copier-org/copier/issues/1328#issuecomment-1723214165
return 0b100
return 0


class CopierApp(cli.Application):
Expand Down Expand Up @@ -247,7 +247,6 @@ class CopierCopySubApp(_Subcommand):
help="Overwrite files that already exist, without asking.",
)

@handle_exceptions
def main(self, template_src: str, destination_path: str) -> int:
"""Call [run_copy][copier.main.Worker.run_copy].

Expand All @@ -260,15 +259,18 @@ def main(self, template_src: str, destination_path: str) -> int:
destination_path:
Where to generate the new subproject. It must not exist or be empty.
"""
with self._worker(
template_src,
destination_path,
cleanup_on_error=self.cleanup_on_error,
defaults=self.force or self.defaults,
overwrite=self.force or self.overwrite,
) as worker:
worker.run_copy()
return 0

def inner() -> None:
with self._worker(
template_src,
destination_path,
cleanup_on_error=self.cleanup_on_error,
defaults=self.force or self.defaults,
overwrite=self.force or self.overwrite,
) as worker:
worker.run_copy()

return _handle_exceptions(inner)


@CopierApp.subcommand("recopy")
Expand Down Expand Up @@ -315,7 +317,6 @@ class CopierRecopySubApp(_Subcommand):
help="Skip questions that have already been answered",
)

@handle_exceptions
def main(self, destination_path: cli.ExistingDirectory = ".") -> int:
"""Call [run_recopy][copier.main.Worker.run_recopy].

Expand All @@ -327,14 +328,17 @@ def main(self, destination_path: cli.ExistingDirectory = ".") -> int:
The subproject must exist. If not specified, the currently
working directory is used.
"""
with self._worker(
dst_path=destination_path,
defaults=self.force or self.defaults,
overwrite=self.force or self.overwrite,
skip_answered=self.skip_answered,
) as worker:
worker.run_recopy()
return 0

def inner() -> None:
with self._worker(
dst_path=destination_path,
defaults=self.force or self.defaults,
overwrite=self.force or self.overwrite,
skip_answered=self.skip_answered,
) as worker:
worker.run_recopy()

return _handle_exceptions(inner)


@CopierApp.subcommand("update")
Expand Down Expand Up @@ -387,7 +391,6 @@ class CopierUpdateSubApp(_Subcommand):
help="Skip questions that have already been answered",
)

@handle_exceptions
def main(self, destination_path: cli.ExistingDirectory = ".") -> int:
"""Call [run_update][copier.main.Worker.run_update].

Expand All @@ -399,13 +402,16 @@ def main(self, destination_path: cli.ExistingDirectory = ".") -> int:
The subproject must exist. If not specified, the currently
working directory is used.
"""
with self._worker(
dst_path=destination_path,
conflict=self.conflict,
context_lines=self.context_lines,
defaults=self.defaults,
skip_answered=self.skip_answered,
overwrite=True,
) as worker:
worker.run_update()
return 0

def inner() -> None:
with self._worker(
dst_path=destination_path,
conflict=self.conflict,
context_lines=self.context_lines,
defaults=self.defaults,
skip_answered=self.skip_answered,
overwrite=True,
) as worker:
worker.run_update()

return _handle_exceptions(inner)
24 changes: 1 addition & 23 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 0 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ copier = "copier.__main__:copier_app_run"
[tool.poetry.dependencies]
python = ">=3.8"
colorama = ">=0.4.6"
decorator = ">=5.1.1"
# HACK Remove markers when https://github.com/mtkennerly/dunamai/issues/74 is fixed
dunamai = { version = ">=1.7.0", markers = "python_version < '4'" }
funcy = ">=1.17"
Expand Down Expand Up @@ -58,7 +57,6 @@ pytest-cov = ">=3.0.0"
pytest-gitconfig = ">=0.6.0"
pytest-xdist = ">=2.5.0"
types-backports = ">=0.1.3"
types-decorator = ">=5.1.1"
types-pyyaml = ">=6.0.4"
types-psutil = "*"

Expand Down
Loading