diff --git a/README.md b/README.md index 5792ed9..e914812 100644 --- a/README.md +++ b/README.md @@ -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 (https://twitter.com/robvanderleek) + diff --git a/codelimit/__main__.py b/codelimit/__main__.py index cb22aab..a650232 100755 --- a/codelimit/__main__.py +++ b/codelimit/__main__.py @@ -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) @@ -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) diff --git a/codelimit/commands/app.py b/codelimit/commands/app.py index f9cdba0..b1b1b25 100644 --- a/codelimit/commands/app.py +++ b/codelimit/commands/app.py @@ -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) diff --git a/codelimit/commands/check.py b/codelimit/commands/check.py index a78a5fc..db9f692 100644 --- a/codelimit/commands/check.py +++ b/codelimit/commands/check.py @@ -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) diff --git a/codelimit/common/Language.py b/codelimit/common/Language.py index 0887853..2029ae3 100644 --- a/codelimit/common/Language.py +++ b/codelimit/common/Language.py @@ -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 diff --git a/codelimit/common/ScanResultTable.py b/codelimit/common/ScanResultTable.py index a042f37..e0d8a0d 100644 --- a/codelimit/common/ScanResultTable.py +++ b/codelimit/common/ScanResultTable.py @@ -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): diff --git a/codelimit/common/Scanner.py b/codelimit/common/Scanner.py index 9bb7e8c..6fa3274 100644 --- a/codelimit/common/Scanner.py +++ b/codelimit/common/Scanner.py @@ -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) @@ -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()): @@ -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: @@ -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) diff --git a/codelimit/common/utils.py b/codelimit/common/utils.py index 65fda0d..ecb947b 100644 --- a/codelimit/common/utils.py +++ b/codelimit/common/utils.py @@ -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: diff --git a/codelimit/languages/C.py b/codelimit/languages/C.py index bc9aa64..7e245b1 100644 --- a/codelimit/languages/C.py +++ b/codelimit/languages/C.py @@ -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, "{", "}") diff --git a/codelimit/languages/Cpp.py b/codelimit/languages/Cpp.py index db9afaa..e8a4f62 100644 --- a/codelimit/languages/Cpp.py +++ b/codelimit/languages/Cpp.py @@ -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, "{", "}") diff --git a/codelimit/languages/Java.py b/codelimit/languages/Java.py index 4337a8d..2fc65c7 100644 --- a/codelimit/languages/Java.py +++ b/codelimit/languages/Java.py @@ -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( diff --git a/codelimit/languages/JavaScript.py b/codelimit/languages/JavaScript.py index 8d78adf..b149d20 100644 --- a/codelimit/languages/JavaScript.py +++ b/codelimit/languages/JavaScript.py @@ -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( @@ -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, "{", "}") diff --git a/codelimit/languages/Python.py b/codelimit/languages/Python.py index 4afb732..0adb5fa 100644 --- a/codelimit/languages/Python.py +++ b/codelimit/languages/Python.py @@ -12,7 +12,7 @@ class Python(Language): def __init__(self): - super().__init__('Python') + super().__init__("Python") def extract_headers(self, tokens: list[Token]) -> list[Header]: return get_headers( @@ -20,7 +20,7 @@ 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]: lines = _get_token_lines(tokens) result = [] diff --git a/codelimit/languages/TypeScript.py b/codelimit/languages/TypeScript.py index 208d72b..8109484 100644 --- a/codelimit/languages/TypeScript.py +++ b/codelimit/languages/TypeScript.py @@ -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( @@ -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, "{", "}") diff --git a/codelimit/languages/__init__.py b/codelimit/languages/__init__.py index 2c0ad36..1c70853 100644 --- a/codelimit/languages/__init__.py +++ b/codelimit/languages/__init__.py @@ -21,4 +21,4 @@ class Languages: JavaScript.name: JavaScript, Python.name: Python, TypeScript.name: TypeScript, - } \ No newline at end of file + } diff --git a/codelimit/utils.py b/codelimit/utils.py index bc88ba4..a57c2d6 100644 --- a/codelimit/utils.py +++ b/codelimit/utils.py @@ -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( diff --git a/main.py b/main.py index 50693b0..0c8a3d7 100755 --- a/main.py +++ b/main.py @@ -1,5 +1,5 @@ #!/usr/bin/env python3 from codelimit import __main__ -if __name__ == '__main__': - __main__.cli() +if __name__ == "__main__": + __main__.cli() diff --git a/tests/commands/test_check.py b/tests/commands/test_check.py index 17a370e..500742a 100644 --- a/tests/commands/test_check.py +++ b/tests/commands/test_check.py @@ -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() @@ -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) diff --git a/tests/common/test_Scanner.py b/tests/common/test_Scanner.py index 2fe032c..235246d 100644 --- a/tests/common/test_Scanner.py +++ b/tests/common/test_Scanner.py @@ -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))