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

feat: ✨ Default path to "." #37

Merged
merged 4 commits into from
Aug 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,4 @@ To show your project uses Code Limit place this badge in the README markdown:

[GPL-3.0-or-later](LICENSE) © 2022 Rob van der Leek <[email protected]>
(https://twitter.com/robvanderleek)

4 changes: 2 additions & 2 deletions codelimit/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ def check(
def scan(
path: Annotated[
Path, typer.Argument(exists=True, file_okay=False, help="Codebase root")
]
] = Path(".")
):
scan_command(path)

Expand All @@ -46,7 +46,7 @@ def scan(
def report(
path: Annotated[
Path, typer.Argument(exists=True, file_okay=False, help="Codebase root")
],
] = Path("."),
full: Annotated[bool, typer.Option("--full", help="Show full report")] = False,
):
report_command(path, full)
Expand Down
38 changes: 21 additions & 17 deletions codelimit/commands/app.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,22 +28,26 @@ def logout():

@app.command(help="Upload report to Code Limit GitHub App")
def upload(
repository: Annotated[str, typer.Argument(show_default=False, help="GitHub repository")],
branch: Annotated[str, typer.Argument(show_default=False, help="GitHub branch")],
report_file: Path = typer.Option(
None,
"--report",
show_default=False,
exists=True,
dir_okay=False,
file_okay=True,
help="JSON report file",
),
token: str = typer.Option(None, "--token", show_default=False, help="GitHub access token"),
url: str = typer.Option(
"https://codelimit.vercel.app/api/upload",
"--url",
help="Upload JSON report to this URL.",
),
repository: Annotated[
str, typer.Argument(show_default=False, help="GitHub repository")
],
branch: Annotated[str, typer.Argument(show_default=False, help="GitHub branch")],
report_file: Path = typer.Option(
None,
"--report",
show_default=False,
exists=True,
dir_okay=False,
file_okay=True,
help="JSON report file",
),
token: str = typer.Option(
None, "--token", show_default=False, help="GitHub access token"
),
url: str = typer.Option(
"https://codelimit.vercel.app/api/upload",
"--url",
help="Upload JSON report to this URL.",
),
):
upload_command(repository, branch, report_file, token, url)
6 changes: 3 additions & 3 deletions codelimit/commands/check.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@ def check_command(paths: list[Path], quiet: bool):
check_file(abs_path, check_result)
exit_code = 1 if check_result.unmaintainable > 0 else 0
if (
not quiet
or check_result.hard_to_maintain > 0
or check_result.unmaintainable > 0
not quiet
or check_result.hard_to_maintain > 0
or check_result.unmaintainable > 0
):
check_result.report()
raise typer.Exit(code=exit_code)
Expand Down
2 changes: 1 addition & 1 deletion codelimit/common/Language.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ def extract_headers(self, tokens: list[Token]) -> list[Header]:

@abstractmethod
def extract_blocks(
self, tokens: list[Token], headers: list[Header]
self, tokens: list[Token], headers: list[Header]
) -> list[TokenRange]:
pass
22 changes: 14 additions & 8 deletions codelimit/common/ScanResultTable.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,23 @@
class ScanResultTable(Table):
def __init__(self, scan_totals: ScanTotals):
super().__init__(
expand=True,
box=box.SIMPLE,
show_footer=len(scan_totals.languages()) > 1
expand=True, box=box.SIMPLE, show_footer=len(scan_totals.languages()) > 1
)
self.scan_totals = scan_totals
self.add_column("Language")
self.add_column("Files", f'{scan_totals.total_files():n}', justify="right")
self.add_column("Lines of Code", f'{scan_totals.total_loc():n}', justify="right")
self.add_column("Functions", f'{scan_totals.total_functions():n}', justify="right")
self.add_column("\u26A0", f'{scan_totals.total_hard_to_maintain():n}', justify="right")
self.add_column("\u2716", f'{scan_totals.total_unmaintainable():n}', justify="right")
self.add_column("Files", f"{scan_totals.total_files():n}", justify="right")
self.add_column(
"Lines of Code", f"{scan_totals.total_loc():n}", justify="right"
)
self.add_column(
"Functions", f"{scan_totals.total_functions():n}", justify="right"
)
self.add_column(
"\u26A0", f"{scan_totals.total_hard_to_maintain():n}", justify="right"
)
self.add_column(
"\u2716", f"{scan_totals.total_unmaintainable():n}", justify="right"
)
self._populate()

def _populate(self):
Expand Down
21 changes: 11 additions & 10 deletions codelimit/common/Scanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ def scan_codebase(path: Path, cached_report: Union[Report, None] = None) -> Code
print_header(cached_report, path)
scan_totals = ScanTotals()
with Live(refresh_per_second=2) as live:

def add_file_entry(entry: SourceFileEntry):
scan_totals.add(entry)
table = ScanResultTable(scan_totals)
Expand Down Expand Up @@ -80,10 +81,10 @@ def print_refactor_candidates(scan_totals: ScanTotals):


def _scan_folder(
codebase: Codebase,
folder: Path,
cached_report: Union[Report, None] = None,
add_file_entry: Union[Callable[[SourceFileEntry], None], None] = None,
codebase: Codebase,
folder: Path,
cached_report: Union[Report, None] = None,
add_file_entry: Union[Callable[[SourceFileEntry], None], None] = None,
):
gitignore = _read_gitignore(folder)
for root, dirs, files in os.walk(folder.absolute()):
Expand All @@ -92,7 +93,7 @@ def _scan_folder(
for file in files:
rel_path = Path(os.path.join(root, file)).relative_to(folder.absolute())
if is_excluded(rel_path) or (
gitignore is not None and is_excluded_by_gitignore(rel_path, gitignore)
gitignore is not None and is_excluded_by_gitignore(rel_path, gitignore)
):
continue
try:
Expand All @@ -111,11 +112,11 @@ def _scan_folder(


def _scan_file(
codebase: Codebase,
lexer: Lexer,
root: Path,
path: str,
cached_report: Union[Report, None] = None,
codebase: Codebase,
lexer: Lexer,
root: Path,
path: str,
cached_report: Union[Report, None] = None,
) -> SourceFileEntry:
checksum = calculate_checksum(path)
rel_path = relpath(path, root)
Expand Down
2 changes: 1 addition & 1 deletion codelimit/common/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def render_quality_profile(profile: list[int]) -> Text:
def path_has_extension(path: str, suffixes: Union[str, list[str]]):
dot_index = path.rfind(".")
if dot_index >= 0:
suffix = path[dot_index + 1:]
suffix = path[dot_index + 1 :]
if isinstance(suffixes, list):
return suffix in suffixes
else:
Expand Down
4 changes: 2 additions & 2 deletions codelimit/languages/C.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,12 @@

class C(Language):
def __init__(self):
super().__init__('C', False)
super().__init__("C", False)

def extract_headers(self, tokens: list[Token]) -> list[Header]:
return get_headers(tokens, [Name(), OneOrMore(Balanced("(", ")"))], Symbol("{"))

def extract_blocks(
self, tokens: list[Token], headers: list[Header]
self, tokens: list[Token], headers: list[Header]
) -> list[TokenRange]:
return get_blocks(tokens, "{", "}")
4 changes: 2 additions & 2 deletions codelimit/languages/Cpp.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@

class Cpp(Language):
def __init__(self):
super().__init__('C++')
super().__init__("C++")

def extract_headers(self, tokens: list[Token]) -> list[Header]:
return get_headers(tokens, [Name(), OneOrMore(Balanced("(", ")"))], Symbol("{"))

def extract_blocks(
self, tokens: list[Token], headers: list[Header]
self, tokens: list[Token], headers: list[Header]
) -> list[TokenRange]:
return get_blocks(tokens, "{", "}")
2 changes: 1 addition & 1 deletion codelimit/languages/Java.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

class Java(Language):
def __init__(self):
super().__init__('Java')
super().__init__("Java")

def extract_headers(self, tokens: list) -> list:
return get_headers(
Expand Down
4 changes: 2 additions & 2 deletions codelimit/languages/JavaScript.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

class JavaScript(Language):
def __init__(self):
super().__init__('JavaScript')
super().__init__("JavaScript")

def extract_headers(self, tokens: list[Token]) -> list[Header]:
return get_headers(
Expand All @@ -26,6 +26,6 @@ def extract_headers(self, tokens: list[Token]) -> list[Header]:
)

def extract_blocks(
self, tokens: list[Token], headers: list[Header]
self, tokens: list[Token], headers: list[Header]
) -> list[TokenRange]:
return get_blocks(tokens, "{", "}")
4 changes: 2 additions & 2 deletions codelimit/languages/Python.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,15 @@

class Python(Language):
def __init__(self):
super().__init__('Python')
super().__init__("Python")

def extract_headers(self, tokens: list[Token]) -> list[Header]:
return get_headers(
tokens, [Keyword("def"), Name(), OneOrMore(Balanced("(", ")"))]
)

def extract_blocks(
self, tokens: list[Token], headers: list[Header]
self, tokens: list[Token], headers: list[Header]
) -> list[TokenRange]:
lines = _get_token_lines(tokens)
result = []
Expand Down
4 changes: 2 additions & 2 deletions codelimit/languages/TypeScript.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

class TypeScript(Language):
def __init__(self):
super().__init__('TypeScript')
super().__init__("TypeScript")

def extract_headers(self, tokens: list[Token]) -> list[Header]:
functions = get_headers(
Expand All @@ -38,6 +38,6 @@ def extract_headers(self, tokens: list[Token]) -> list[Header]:
return functions + arrow_functions

def extract_blocks(
self, tokens: list[Token], headers: list[Header]
self, tokens: list[Token], headers: list[Header]
) -> list[TokenRange]:
return get_blocks(tokens, "{", "}")
2 changes: 1 addition & 1 deletion codelimit/languages/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,4 +21,4 @@ class Languages:
JavaScript.name: JavaScript,
Python.name: Python,
TypeScript.name: TypeScript,
}
}
6 changes: 3 additions & 3 deletions codelimit/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ def api_post_report(report, branch, repository, url, token):
f'{{{{"repository": "{repository}", "branch": "{branch}", "report":{{}}}}}}'
)
with Progress(
SpinnerColumn(),
TextColumn("[progress.description]{task.description}"),
transient=True,
SpinnerColumn(),
TextColumn("[progress.description]{task.description}"),
transient=True,
) as progress:
progress.add_task(description=f"Uploading report to {url}", total=None)
result = requests.post(
Expand Down
4 changes: 2 additions & 2 deletions main.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env python3
from codelimit import __main__

if __name__ == '__main__':
__main__.cli()
if __name__ == "__main__":
__main__.cli()
10 changes: 5 additions & 5 deletions tests/commands/test_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@


def test_check_file():
code = ''
code += 'def foo():\n'
code = ""
code += "def foo():\n"
code += ' print("Hello, world!")\n'
tmp = tempfile.NamedTemporaryFile(suffix='.py')
tmp = tempfile.NamedTemporaryFile(suffix=".py")
tmp.write(code.encode())
check_result = CheckResult()

Expand All @@ -19,8 +19,8 @@ def test_check_file():


def test_check_unsupported_file():
tmp = tempfile.NamedTemporaryFile(suffix='.gitignore')
tmp.write(''.encode())
tmp = tempfile.NamedTemporaryFile(suffix=".gitignore")
tmp.write("".encode())
check_result = CheckResult()

check_file(Path(tmp.name), check_result)
Expand Down
2 changes: 1 addition & 1 deletion tests/common/test_Scanner.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def test_scan_single_file():
def test_scan_unsupported_file():
tmp_root = tempfile.TemporaryDirectory()
with open(os.path.join(tmp_root.name, "foo.not-supported"), "w") as pythonFile:
pythonFile.write('')
pythonFile.write("")

result = scan_codebase(Path(tmp_root.name))

Expand Down
Loading