Skip to content

Commit

Permalink
Test for rename cmd (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
lalmei authored Dec 2, 2022
1 parent 36b446c commit 9d9fd48
Show file tree
Hide file tree
Showing 27 changed files with 188 additions and 82 deletions.
1 change: 1 addition & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ end_of_line = lf

[*.md]
trim_trailing_whitespace = false
indent_size = 2

[Makefile]
indent_style = tab
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ jobs:
env:
CC_TEST_REPORTER_ID: ${{secrets.REPORTER_ID}}
with:
prefix: src/
prefix: ${{github.workspace}}
coverageLocations: |
${{github.workspace}}/.coverage/:coverage.py
${{github.workspace}}/coverage.xml/:coverage.py
6 changes: 1 addition & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1 @@
[![Maintainability](https://api.codeclimate.com/v1/badges/67d0f7c0715d81bef5f9/maintainability)](https://codeclimate.com/github/lalmei/asunder/maintainability)

[![Test Coverage](https://api.codeclimate.com/v1/badges/67d0f7c0715d81bef5f9/test_coverage)](https://codeclimate.com/github/lalmei/asunder/test_coverage)


[![asunder](https://github.com/lalmei/asunder/actions/workflows/main.yml/badge.svg?branch=main)](https://github.com/lalmei/asunder/actions/workflows/main.yml) [![Maintainability](https://api.codeclimate.com/v1/badges/67d0f7c0715d81bef5f9/maintainability)](https://codeclimate.com/github/lalmei/asunder/maintainability) [![Test Coverage](https://api.codeclimate.com/v1/badges/67d0f7c0715d81bef5f9/test_coverage)](https://codeclimate.com/github/lalmei/asunder/test_coverage)
5 changes: 3 additions & 2 deletions config/.isort.cfg
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
[settings]
line_length = 80
[tool.isort]
line_length = 79
profile = "black"
multi_line_output = 3
balanced_wrapping = true
default_section = "THIRDPARTY"
Expand Down
2 changes: 1 addition & 1 deletion config/flake8.ini
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ classmethod-decorators =
validator
exclude = fixtures,docs,site
show-source = true
max-line-length = 80
max-line-length = 100
statistics = True
docstring-convention = google
ban-relative-imports = true
Expand Down
2 changes: 1 addition & 1 deletion config/pytest.ini
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,6 @@ python_files =
test_*.py
testpaths =
./tests/*
addopts = -v --cov-config=./config/coverage.ini --cov=./src --cov-report=xml --cov-report=term-missing
addopts = -v --cov-config=./config/coverage.ini --cov=./src --cov-report=xml --cov-report=html --cov-report=term-missing


14 changes: 7 additions & 7 deletions docs/nav.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
* [Overview](index.md)
* Initial Setup
* [Get Started](get_started.md)
* Basic Usage
* [Refactoring](basic_usage/refactoring.md)
* [Help & Get Help](help.md)
* [Release Notes](changelog.md)
* [Code Reference](reference/)
* Initial Setup
* [Get Started](get_started.md)
* Basic Usage
* [Refactoring](basic_usage/refactoring.md)
* [Help & Get Help](help.md)
* [Release Notes](changelog.md)
* [Code Reference](reference/)
40 changes: 20 additions & 20 deletions poetry.lock

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

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,4 @@ requires = ["poetry-core"]
build-backend = "poetry.core.masonry.api"

[tool.black]
line-length = 80
line-length = 79
1 change: 0 additions & 1 deletion src/asunder/command/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import typer
from rich import box
from rich.console import Console
from rich.logging import RichHandler
from rich.panel import Panel

from asunder._version import version_info
Expand Down
21 changes: 9 additions & 12 deletions src/asunder/command/rename_cmd.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import logging
import os
from pathlib import Path
from typing import Optional

Expand All @@ -18,33 +17,31 @@
@app.command(no_args_is_help=True)
def rename(
ctx: Context,
path: Path = typer.Option(
Path.cwd() / "src", help="path to package source code"
),
module: str = typer.Argument(
"", help='module where renaming will take placed, e.g. "package.module"'
path: Path = typer.Option(Path.cwd(), help="path to package source code"),
module: Path = typer.Option(
"",
help='module where renaming will take placed, e.g. "package.module"',
),
old_name: Optional[str] = typer.Option(
"", help="old name of module/class/attribute"
),
name: str = typer.Argument("", help="new module/class/attribute name"),
new_name: str = typer.Option("", help="new module/class/attribute name"),
) -> None:

if not old_name:
old_name = module
old_name = str(module)
dry_run = ctx.obj.get("dry_run", True)

logger, console = get_logger_console()

project = Project(path=Path.cwd(), console=console)
project = Project(path=path, console=console)

# module to folder
module = os.path.join(*module.split("."))
name = os.path.join(*name.split("."))
# module = os.path.join(*module.split("."))

logger.info("Calculating Changes")
# compute changes needed
changes = rename_changes(project.rope_project, module, old_name, name)
changes = rename_changes(project.rope_project, module, old_name, new_name)

if not dry_run:
logger.info("Perfoming Changes")
Expand Down
2 changes: 1 addition & 1 deletion src/asunder/project/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ def __init__(
self, path: Path = Path.cwd(), console: Console = Console()
) -> None:

self.rope_project = RopeProject(str(path / "src"))
self.rope_project = RopeProject(str(path))
self.console = console

def perform_changes(self, changes: ChangeSet, dry_run: bool) -> None:
Expand Down
2 changes: 1 addition & 1 deletion src/asunder/rope_sdk/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
from asunder.rope_sdk.refactor.rename.rename import rename, rename_module

__all__: list[str] = ["rename_module, rename"]
__all__: list[str] = ["rename_module", "rename"]
22 changes: 14 additions & 8 deletions src/asunder/rope_sdk/find/find.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
from functools import partial
from typing import Optional

from rope.base.project import Project as RopeProject
from rope.refactor.occurrences import Finder
from rope.refactor.occurrences import Finder, Occurrence

from asunder.utils.logging import get_logger_console


def find_definition_in_resource(
repo_project: RopeProject, name: str, resource: str
):
repo_project: RopeProject, name: Optional[str], resource: Optional[str]
) -> Occurrence:
FINDER = partial(Finder, repo_project)
finder = FINDER(name)
return next(
occ
for occ in finder.find_occurrences(resource=resource)
if occ.is_defined() or occ.is_written()
)
logger, console = get_logger_console()
console.print(finder.find_occurrences(resource=resource))
for occ in finder.find_occurrences(resource=resource):
console.print(occ)
if occ.is_defined():
return occ
Empty file.
35 changes: 26 additions & 9 deletions src/asunder/rope_sdk/refactor/rename/rename.py
Original file line number Diff line number Diff line change
@@ -1,28 +1,45 @@
import logging
from pathlib import Path
from typing import Optional

from rope.base.change import ChangeSet
from rope.base.project import Project as RopeProject
from rope.refactor.rename import Rename

from asunder.rope_sdk.find import find_definition_in_resource
from asunder.utils.logging import get_logger_console

logger = logging.getLogger("asunder")


def rename_module(
rope_project: RopeProject, module: str, to_name: str
rope_project: RopeProject, module: Optional[str], to_name: Optional[str]
) -> ChangeSet:
module_resource = rope_project.get_resource(module)
return Rename(rope_project, module_resource).get_changes(to_name)


def rename(
rope_project: RopeProject, module: str, from_name: str, to_name: str
rope_project: RopeProject,
resource: Path,
from_name: Optional[str],
to_name: Optional[str],
) -> ChangeSet:
module_resource = rope_project.get_resource(module)
definition_occurrence = find_definition_in_resource(
rope_project, from_name, module_resource
)
return Rename(
rope_project, module_resource, definition_occurrence.offset
).get_changes(to_name)
module_resource = rope_project.get_resource(str(resource))
logger, console = get_logger_console()
# console.print(dir(module_resource))
if module_resource.is_folder():
if from_name in [
Path(x._path).stem for x in module_resource.get_files()
]:
new_resource = resource / Path(from_name + ".py")
changes = rename_module(rope_project, str(new_resource), to_name)
return changes

else:
definition_occurrence = find_definition_in_resource(
rope_project, from_name, module_resource
)
return Rename(
rope_project, module_resource, definition_occurrence.offset
).get_changes(to_name)
20 changes: 11 additions & 9 deletions src/asunder/utils/logging.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,10 @@ def _set_up_logger(console: Optional[Console] = None) -> Logger:
if not console:
console = Console()
module_logger = getLogger("asunder")
module_logger.addHandler(RichHandler(rich_tracebacks=True, console=console))

rich_handler = RichHandler(rich_tracebacks=True, console=console)
rich_handler.set_name("rich")
module_logger.addHandler(rich_handler)
module_logger.setLevel(level=WARNING)

return module_logger
Expand All @@ -39,13 +42,12 @@ def get_logger_console(
logger = getLogger("asunder")

if len(logger.handlers) > 0:
handler: RichHandler = cast(RichHandler, logger.handlers[0])
console = handler.console
print(
"handler names = ", [handle.__dict__ for handle in logger.handlers]
)
else:
logger = _set_up_logger(console)
logger.debug("Setting up rich log handler")
if logger.handlers[0].get_name() == "rich":
handler: RichHandler = cast(RichHandler, logger.handlers[0])
console = handler.console
return logger, console

logger = _set_up_logger(console)
logger.debug("Setting up rich log handler")

return logger, console
Empty file.
Empty file.
4 changes: 4 additions & 0 deletions test_data/test_package/module1/subMOM.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
def my_function():
myresult = 1 + 1

return myresult
Loading

0 comments on commit 9d9fd48

Please sign in to comment.