{"id":24062461,"url":"https://github.com/mathavansg/context-collector","last_synced_at":"2025-10-18T17:45:28.490Z","repository":{"id":247229270,"uuid":"825319477","full_name":"MathavanSG/Context-Collector","owner":"MathavanSG","description":null,"archived":false,"fork":false,"pushed_at":"2024-07-07T13:09:09.000Z","size":7,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-01-09T08:56:13.125Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Python","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/MathavanSG.png","metadata":{"files":{"readme":"ReadMe.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2024-07-07T12:59:33.000Z","updated_at":"2024-07-07T13:09:12.000Z","dependencies_parsed_at":"2024-07-07T14:29:22.467Z","dependency_job_id":"76f10fc3-1340-421f-b927-f28e2e363657","html_url":"https://github.com/MathavanSG/Context-Collector","commit_stats":null,"previous_names":["mathavansg/context-collector"],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MathavanSG%2FContext-Collector","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MathavanSG%2FContext-Collector/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MathavanSG%2FContext-Collector/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/MathavanSG%2FContext-Collector/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/MathavanSG","download_url":"https://codeload.github.com/MathavanSG/Context-Collector/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":240890586,"owners_count":19874027,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-01-09T08:56:06.267Z","updated_at":"2025-10-18T17:45:23.469Z","avatar_url":"https://github.com/MathavanSG.png","language":"Python","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Context Collector\n\n\nhttps://github.com/MathavanSG/Context-Collector/assets/121884337/1ce23ada-a20a-48c7-b7e9-ce46257020e5\n\n\n\nContext Collector is a Python script designed to gather and concatenate all Python files within a specified directory. This tool is particularly useful for projects with multiple `.py` files spread across various directories, allowing you to compile them into a single file for easier analysis, context gathering, or documentation purposes.\n\n\n## Features\n\n- **Automated Gathering**: Recursively collects all `.py` files in the specified directory, excluding specified directories such as `env`, `venv`, `__pycache__`, and `site-packages`.\n- **Exclusion Handling**: Allows exclusion of specific directories to avoid unwanted files.\n- **Concatenation**: Combines the contents of all gathered Python files into a single output file, with each file's content prefixed by its file path.\n\n## Usage\n\n1. **Clone the Repository**:\n   ```bash\n   git clone https://github.com/MathavanSG/Context-Collector.git\n   cd Context-Collector\n   ```\n\n2. **Create a Python Script**:\n   Create a new Python file (e.g., `context_collector.py`) and paste the following code:\n\n   ```python\n   import os\n\n   def gather_py_files(directory):\n       py_files = []\n       exclude_dirs = {'env', 'venv', '__pycache__', 'site-packages'}  # Add other directories to exclude if needed\n       for root, dirs, files in os.walk(directory):\n           # Exclude specified directories\n           dirs[:] = [d for d in dirs if d not in exclude_dirs]\n           for file in files:\n               if file.endswith(\".py\") and file != os.path.basename(__file__):  # Exclude this script itself\n                   py_files.append(os.path.join(root, file))\n       return py_files\n\n   def concatenate_files(files, output_file):\n       with open(output_file, 'w', encoding='utf-8') as outfile:\n           for file in files:\n               with open(file, 'rb') as infile:\n                   content = infile.read().decode('utf-8', errors='ignore')\n                   outfile.write(f\"# Contents of {file}\\n\")\n                   outfile.write(content)\n                   outfile.write(\"\\n\\n\")\n\n   def main():\n       output_file = input(\"Enter the name of the output file (including .py extension): \")\n\n       if output_file:\n           directory = os.getcwd()  # Use the current working directory\n           py_files = gather_py_files(directory)\n           if py_files:\n               concatenate_files(py_files, output_file)\n               print(f\"All .py files have been concatenated into {output_file}\")\n           else:\n               print(\"No .py files found in the current directory.\")\n       else:\n           print(\"Please provide the name of the output file.\")\n\n   if __name__ == '__main__':\n       main()\n   ```\n\n3. **Run the Script**:\n   Execute the script in your terminal or command prompt:\n   ```bash\n   python context_collector.py\n   ```\n\n4. **Specify the Output File**:\n   When prompted, enter the name of the output file (including `.py` extension) where you want to save the concatenated contents. For example:\n   ```\n   Enter the name of the output file (including .py extension): combined_project.py\n   ```\n\n5. **Output**:\n   The script will gather all `.py` files from the current working directory and its subdirectories, concatenate their contents, and save them into the specified output file. Each file's content will be prefixed by a comment indicating its original path.\n\n## Explanation of the Code\n\n- **`gather_py_files(directory)`**:\n  - Recursively walks through the given directory.\n  - Collects paths of all `.py` files, excluding directories specified in `exclude_dirs`.\n  - Excludes the script file itself to avoid self-inclusion.\n\n- **`concatenate_files(files, output_file)`**:\n  - Opens the specified output file for writing.\n  - Reads and writes the content of each gathered `.py` file to the output file.\n  - Adds a comment indicating the file path before the content of each file.\n\n- **`main()`**:\n  - Prompts the user to enter the name of the output file.\n  - Uses the current working directory to gather `.py` files.\n  - Calls `gather_py_files()` and `concatenate_files()` to process and save the files.\n\n## Benefits\n\n- **Efficiency**: Eliminates the need for manual copying and pasting of code across multiple files.\n- **Context Gathering**: Provides a consolidated view of all your project's Python files, aiding in understanding and documentation.\n- **Scalability**: Handles projects with a large number of files and complex directory structures.\n\n## Example Use Case\n\nImagine you have a project with over 1500 lines of code spread across different `.py` files in various directories. Using Context Collector, you can easily gather all the code into a single file, making it easier to provide a broader context of your project for review, analysis, or documentation.\n\nFeel free to contribute to the project or raise issues if you encounter any problems!\n\n---\n\nIf you find this tool useful, consider starring the repository on GitHub!\n\n---\n\n[GitHub Repository](https://github.com/MathavanSG/Context-Collector)\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmathavansg%2Fcontext-collector","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmathavansg%2Fcontext-collector","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmathavansg%2Fcontext-collector/lists"}