diff --git a/poetry.lock b/poetry.lock index c7c5857..c102682 100644 --- a/poetry.lock +++ b/poetry.lock @@ -2011,6 +2011,20 @@ postgresql-psycopgbinary = ["psycopg[binary] (>=3.0.7)"] pymysql = ["pymysql"] sqlcipher = ["sqlcipher3_binary"] +[[package]] +name = "tabulate" +version = "0.9.0" +description = "Pretty-print tabular data" +optional = false +python-versions = ">=3.7" +files = [ + {file = "tabulate-0.9.0-py3-none-any.whl", hash = "sha256:024ca478df22e9340661486f85298cff5f6dcdba14f3813e8830015b9ed1948f"}, + {file = "tabulate-0.9.0.tar.gz", hash = "sha256:0095b12bf5966de529c0feb1fa08671671b3368eec77d7ef7ab114be2c068b3c"}, +] + +[package.extras] +widechars = ["wcwidth"] + [[package]] name = "tenacity" version = "8.2.3" @@ -2330,4 +2344,4 @@ testing = ["big-O", "jaraco.functools", "jaraco.itertools", "more-itertools", "p [metadata] lock-version = "2.0" python-versions = "^3.9" -content-hash = "589cdd522a70b0964459ddb3d95a9037fde5cbb2e37aaddd827eb1e9462c24f4" +content-hash = "e3f1853e698b0db39d6059179213444250c2ed44c1ce1b1e4bfc55e1a0c87c0d" diff --git a/pyproject.toml b/pyproject.toml index 8430e23..91c743a 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -13,6 +13,7 @@ importlib-metadata = ">=4.8.0" langchain = "^0.1.6" poetry-dynamic-versioning = "^1.2.0" langchain-openai = "^0.0.5" +tabulate = "^0.9.0" [tool.poetry.group.dev.dependencies] pytest = {version = ">=7.1.2"} diff --git a/src/codergpt/cli.py b/src/codergpt/cli.py index f9ce3a7..56ca41b 100644 --- a/src/codergpt/cli.py +++ b/src/codergpt/cli.py @@ -1,6 +1,8 @@ """Command line interface for CoderGPT.""" import logging +from pathlib import Path +from typing import TextIO, Union import click @@ -37,8 +39,8 @@ def main(verbose: int, quiet: bool): @main.command() @click.argument("path", type=click.Path(exists=True)) -def inspect(path: str): - """Inspect pckage to show file-language-map.""" +def inspect(path: Union[str, Path, TextIO]): + """Inspect package to show file-language-map.""" coder = CoderGPT() coder.inspect_package(path=path) diff --git a/src/codergpt/constants.py b/src/codergpt/constants.py index 25f0c73..232a7ae 100644 --- a/src/codergpt/constants.py +++ b/src/codergpt/constants.py @@ -5,3 +5,5 @@ SRC = Path(__file__).resolve().parents[1] PACKAGE_DIR = SRC / "codergpt" EXTENSION_MAP_FILE = PACKAGE_DIR / "extensions.yaml" +LANGUAGE_MAP_KEY = "language-map" +INSPECTION_HEADERS = ["File", "Language"] diff --git a/src/codergpt/explainer/__init__.py b/src/codergpt/explainer/__init__.py new file mode 100644 index 0000000..894a12b --- /dev/null +++ b/src/codergpt/explainer/__init__.py @@ -0,0 +1 @@ +"""Code explanation module.""" diff --git a/src/codergpt/main.py b/src/codergpt/main.py index c2f94af..13e90bb 100644 --- a/src/codergpt/main.py +++ b/src/codergpt/main.py @@ -2,13 +2,14 @@ import os from pathlib import Path -from typing import Dict, Union +from typing import Union import yaml from langchain_core.prompts import ChatPromptTemplate from langchain_openai import ChatOpenAI +from tabulate import tabulate -from codergpt.constants import EXTENSION_MAP_FILE +from codergpt.constants import EXTENSION_MAP_FILE, INSPECTION_HEADERS class CoderGPT: @@ -37,38 +38,39 @@ def __init__(self): # } # ) - def inspect_package(self, path: Union[str, Path]) -> Dict[str, str]: - """Inspecting the code and returning a mapping of files to their languages.""" + def inspect_package(self, path: Union[str, Path]): + """Inspecting the code and displaying a mapping of files to their languages in a table.""" print("Inspecting the code.") with open(EXTENSION_MAP_FILE, "r") as file: extension_to_language = yaml.safe_load(file) - # Initialize an empty dictionary to store the results - file_language_map = {} - # Convert path to Path object if it's a string path = Path(path) + # Initialize an empty list to store the results + file_language_list = [] + # Check if the path is a directory or a file if path.is_dir(): # Iterate over all files in the directory and subdirectories - file_language_map = { - str(file): extension_to_language["language-map"].get(file.suffix) - for file in path.rglob("*.*") - if extension_to_language["language-map"].get(file.suffix) is not None - } + for file in path.rglob("*.*"): + language = extension_to_language["language-map"].get(file.suffix) + if language is not None: + file_language_list.append((str(file), language)) elif path.is_file(): # Get the language for the single file language = extension_to_language["language-map"].get(path.suffix) if language is not None: - file_language_map[str(path)] = language + file_language_list.append((str(path), language)) else: print(f"The path {path} is neither a file nor a directory.") - return file_language_map - return file_language_map + return + + # Display the results as a table + print(tabulate(file_language_list, headers=INSPECTION_HEADERS)) if __name__ == "__main__": diff --git a/src/codergpt/optimizer/__init__.py b/src/codergpt/optimizer/__init__.py new file mode 100644 index 0000000..62bd859 --- /dev/null +++ b/src/codergpt/optimizer/__init__.py @@ -0,0 +1 @@ +"""Code optimization module.""" diff --git a/src/codergpt/test_writer/__init__.py b/src/codergpt/test_writer/__init__.py new file mode 100644 index 0000000..5cb2099 --- /dev/null +++ b/src/codergpt/test_writer/__init__.py @@ -0,0 +1 @@ +"""Test writing module."""