From ed9e9e07ab8732f69e01797014de92cb739b27b6 Mon Sep 17 00:00:00 2001 From: Harshad Hegde Date: Fri, 9 Feb 2024 09:12:37 -0600 Subject: [PATCH] Inspect package coded --- pyproject.toml | 8 +-- src/codergpt/cli.py | 15 ++--- src/codergpt/constants.py | 7 ++ src/codergpt/extensions.yaml | 48 +++++++++++++ src/codergpt/main.py | 127 ++++++++++++++++++----------------- 5 files changed, 129 insertions(+), 76 deletions(-) create mode 100644 src/codergpt/constants.py create mode 100644 src/codergpt/extensions.yaml diff --git a/pyproject.toml b/pyproject.toml index 83c69c7..8430e23 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -50,10 +50,10 @@ extend-ignore = [ line-length = 120 # Allow autofix for all enabled rules (when `--fix`) is provided. -fixable = ["ALL"] +lint.fixable = ["ALL"] # Select or ignore from https://beta.ruff.rs/docs/rules/ -select = [ +lint.select = [ "B", # bugbear "D", # pydocstyle "E", # pycodestyle errors @@ -63,10 +63,10 @@ select = [ "W", # Warning ] -unfixable = [] +lint.unfixable = [] target-version = "py310" -[tool.ruff.mccabe] +[tool.ruff.lint.mccabe] # Unlike Flake8, default to a complexity level of 10. max-complexity = 10 diff --git a/src/codergpt/cli.py b/src/codergpt/cli.py index f35e7c6..f9ce3a7 100644 --- a/src/codergpt/cli.py +++ b/src/codergpt/cli.py @@ -5,7 +5,7 @@ import click from codergpt import __version__ -from codergpt.main import start +from codergpt.main import CoderGPT __all__ = [ "main", @@ -36,14 +36,11 @@ def main(verbose: int, quiet: bool): @main.command() -def run(): - """Run the CoderGPT's demo command.""" - start() - - -def explore(): - """Explore the code.""" - print("Exploring the code.") +@click.argument("path", type=click.Path(exists=True)) +def inspect(path: str): + """Inspect pckage to show file-language-map.""" + coder = CoderGPT() + coder.inspect_package(path=path) if __name__ == "__main__": diff --git a/src/codergpt/constants.py b/src/codergpt/constants.py new file mode 100644 index 0000000..25f0c73 --- /dev/null +++ b/src/codergpt/constants.py @@ -0,0 +1,7 @@ +"""All constants used in the package.""" + +from pathlib import Path + +SRC = Path(__file__).resolve().parents[1] +PACKAGE_DIR = SRC / "codergpt" +EXTENSION_MAP_FILE = PACKAGE_DIR / "extensions.yaml" diff --git a/src/codergpt/extensions.yaml b/src/codergpt/extensions.yaml new file mode 100644 index 0000000..126eec2 --- /dev/null +++ b/src/codergpt/extensions.yaml @@ -0,0 +1,48 @@ +language-map: + ".py": "Python" + ".js": "JavaScript" + ".java": "Java" + ".c": "C" + ".cpp": "C++" + ".html": "HTML" + ".css": "CSS" + ".php": "PHP" + ".rb": "Ruby" + ".rs": "Rust" + ".go": "Go" + ".swift": "Swift" + ".kt": "Kotlin" + ".ts": "TypeScript" + ".sh": "Shell" + ".json": "JSON" + ".xml": "XML" + ".yml": "YAML" + ".yaml": "YAML" + ".sql": "SQL" + ".pl": "Perl" + ".lua": "Lua" + ".r": "R" + ".cs": "C#" + ".m": "Objective-C" + ".scala": "Scala" + ".groovy": "Groovy" + ".dart": "Dart" + ".jsx": "JSX" + ".tsx": "TSX" + ".h": "C Header" + ".hpp": "C++ Header" + ".hh": "C++ Header" + ".hxx": "C++ Header" + ".mjs": "ES Module" + ".cjs": "CommonJS" + ".svelte": "Svelte" + ".vue": "Vue" + ".elm": "Elm" + ".erl": "Erlang" + ".ex": "Elixir" + ".exs": "Elixir" + ".zig": "Zig" + ".nim": "Nim" + ".cr": "Crystal" + ".v": "V" + ".vala": "Vala" diff --git a/src/codergpt/main.py b/src/codergpt/main.py index a158f83..c2f94af 100644 --- a/src/codergpt/main.py +++ b/src/codergpt/main.py @@ -1,75 +1,76 @@ """Main python file.""" - import os from pathlib import Path -from typing import Dict, Set, Union +from typing import Dict, Union +import yaml from langchain_core.prompts import ChatPromptTemplate from langchain_openai import ChatOpenAI - -# Define a function to determine the programming language from a file extension. -def get_language_from_extension(ext: Set[str]) -> Dict[str, str]: - """Get language from file extension.""" - # Initialize the ChatOpenAI object with an API key from the environment variables. - llm = ChatOpenAI(openai_api_key=os.environ.get("OPENAI_API_KEY")) - - # Create a prompt template with predefined messages to set the context for the AI. - # Here, we tell the AI that it is a "world class software developer". - prompt = ChatPromptTemplate.from_messages( - [("system", "You are world class software developer."), ("user", "{input}")] - ) - - # Combine the prompt template with the ChatOpenAI object to create a chain. - # This chain will be used to send the input to the AI in the correct context. - chain = prompt | llm - - # Invoke the chain with the specific input asking about the programming language - # associated with the given file extension. The input is formatted to include the - # file extension in the question and requests a one-word response. - response = chain.invoke( - { - "input": f"Given the file extensions {ext},\ - return just programming language name e.g. 'Python' or 'JavaScript'." - } - ) - return response.content - - -def inspecting_code(path: Union[str, Path]) -> Dict[str, str]: - """Inspecting the code and returning a mapping of files to their languages.""" - print("Inspecting the code.") - - # Initialize an empty dictionary to store the results - file_language_map = {} - - # Convert path to Path object if it's a string - path = Path(path) - - # Check if the path is a directory or a file - if path.is_dir(): - # Iterate over all files in the directory and subdirectories - for file in path.rglob("*.*"): - language = get_language_from_extension(file.suffix) - file_language_map[str(file)] = language - elif path.is_file(): - # Get the language for the single file - language = get_language_from_extension(path.suffix) - file_language_map[str(path)] = language - else: - print(f"The path {path} is neither a file nor a directory.") +from codergpt.constants import EXTENSION_MAP_FILE + + +class CoderGPT: + """CoderGPT class.""" + + def __init__(self): + """Initialize the CoderGPT class.""" + # Initialize the ChatOpenAI object with an API key from the environment variables. + self.llm = ChatOpenAI(openai_api_key=os.environ.get("OPENAI_API_KEY")) + # Create a prompt template with predefined messages to set the context for the AI. + # Here, we tell the AI that it is a "world class software developer". + self.prompt = ChatPromptTemplate.from_messages( + [("system", "You are world class software developer."), ("user", "{input}")] + ) + # Combine the prompt template with the ChatOpenAI object to create a chain. + # This chain will be used to send the input to the AI in the correct context. + self.chain = self.prompt | self.llm + + # # Invoke the chain with the specific input asking about the programming language + # # associated with the given file extension. The input is formatted to include the + # # file extension in the question and requests a one-word response. + # response = chain.invoke( + # { + # "input": f"Given the file extensions {ext},\ + # return just programming language name e.g. 'Python' or 'JavaScript'." + # } + # ) + + def inspect_package(self, path: Union[str, Path]) -> Dict[str, str]: + """Inspecting the code and returning a mapping of files to their languages.""" + 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) + + # 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 + } + + 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 + + else: + print(f"The path {path} is neither a file nor a directory.") + return file_language_map return file_language_map - return file_language_map - - -def start(): - """Entry point function.""" - path = Path("src") - language_map = inspecting_code(path) - print(language_map) - if __name__ == "__main__": - start() + coder = CoderGPT() + coder.inspect_package("src")