A CLI tool to flatten project files into a single file with descriptive paths
Project description
Flatten Tool
A command-line tool to flatten project files into a single file with descriptive paths, designed for Python and JavaScript projects on Unix/Linux. It supports auto-detection of files and directories, wildcard patterns, and one-depth import inclusion, making it ideal for code sharing, documentation, or analysis.
Features
- Auto-Detection: Automatically handles files (e.g.,
./file.js) and directories (e.g.,./src/,.). - Wildcard Support: Flatten files matching patterns (e.g.,
**/readme.md). - Imports: Include one-depth dependencies with
--with-imports. - Modular Design: Organized as a Python package with plugins for extensibility.
- Output Formats: Supports
txt,md, andjson. - Parallel Processing: Uses
multiprocessingfor performance. - Interactive Setup: Configure projects with
flatten init.
Installation
Recommended: Pipx (Isolated and Global)
Install flattenify in an isolated environment with a globally accessible command:
-
Install
pipx(if not already installed):pip3 install pipx pipx ensurepath
Restart your terminal after running
pipx ensurepath. -
Install
flattenify:pipx install flattenify
-
Run the tool:
flatten init flatten ./src/ --recursive
Benefits:
- Isolated dependencies, no conflicts with system Python or other projects.
- No
sudorequired. - Global
flattencommand available everywhere. - Easy to uninstall:
pipx uninstall flattenify.
Alternative 1: Local (Sandboxed)
Run the tool in a local virtual environment:
-
Clone the repository:
git clone https://github.com/mshittiah/flattenify.git cd flattenify
-
Install locally:
chmod +x install.sh ./install.sh --local
-
Install the package:
pip install -e .
-
Run the tool:
python -m flatten_tool.flatten.cli init python -m flatten_tool.flatten.cli flatten ./src/ --recursive
Benefits:
- Fully isolated, no system interference.
- No
sudorequired. - Easy to uninstall:
./uninstall.sh --localor delete theflattenifydirectory.
Alternative 2: Global
Install system-wide (use with caution):
-
Install from source:
git clone https://github.com/mshittiah/flattenify.git cd flattenify chmod +x install.sh ./install.sh --global
-
Run the tool:
flatten init flatten ./src/ --recursive
Notes:
- Requires
sudofor file copying. - Checks for dependency conflicts and prompts for confirmation.
- Risk of conflicts with existing Python packages.
Uninstallation
Pipx
pipx uninstall flattenify
Local
chmod +x uninstall.sh
./uninstall.sh --local
Global
chmod +x uninstall.sh
./uninstall.sh --global
Directory Structure
.github/workflows/: GitHub Actions for CI/CD.src/flatten_tool/: Core Python package, plugins, and templates.flatten/: CLI, config, file handling, output, and logging modules.plugins/: Custom import parsers.templates/: Sample configurations (e.g., for Next.js).
tests/: Unit tests using pytest.install.sh: Installs the tool (pipx, local, or global).uninstall.sh: Removes the tool and dependencies.pyproject.toml: Package metadata for PyPI.README.md: Project documentation.CONTRIBUTING.md: Contribution guidelines.CODE_OF_CONDUCT.md: Community standards.LICENSE: MIT License.docs/: Additional documentation, including tool overview..pre-commit-config.yaml: Pre-commit hooks for linting..vscode/settings.json: VS Code settings for linting and formatting.
Development
Setup
-
Clone the repository:
git clone https://github.com/mshittiah/flattenify.git cd flattenify
-
Create a virtual environment:
python3 -m venv .venv source .venv/bin/activate
-
Install dependencies and the package in editable mode:
pip install -e .[dev] pip install pre-commit pytest pytest-cov
-
Install pre-commit hooks:
pre-commit install -
Configure VS Code:
-
Open the project in VS Code:
code . -
Ensure the
.venvinterpreter is selected (Ctrl+Shift+P, "Python: Select Interpreter", choose.venv/bin/python). -
Install recommended extensions:
code --install-extension ms-python.python code --install-extension ms-python.black-formatter code --install-extension ms-python.flake8
-
The
.vscode/settings.jsonconfigures automatic linting and formatting on save.
-
Linting and Formatting
- Flake8: Enforces code quality (e.g., undefined names, line length).
- Black: Formats code with a consistent style.
- isort: Organizes imports.
Run manually:
flake8 . --max-line-length=120 --extend-exclude=.venv,venv,dist,build,.git,.github,.pre-commit-cache
black . --line-length=120
isort . --profile=black --line-length=120
Pre-commit hooks run these automatically on commit. VS Code applies Black and Flake8 on save.
Troubleshooting Pre-commit Failures:
-
Dependency Conflicts:
-
If pre-commit fails with a
CalledProcessError, clear the cache:rm -rf ~/.cache/pre-commit pre-commit install
-
Update hooks to stable versions:
pre-commit autoupdate
-
-
Flake8 Errors:
- Run
flake8 . --max-line-length=120 --extend-exclude=.venv,venv,dist,build,.git,.github,.pre-commit-cacheto identify issues. - Fix errors manually or use VS Code’s linting (save files to apply fixes).
- Run
-
Black/isort Modifications:
- If Black or isort reformats files, save changes in VS Code (Ctrl+S) before committing.
- Run
black . --line-length=120andisort . --profile=black --line-length=120manually to fix.
-
Unstaged Files:
- Pre-commit stashes unstaged changes. After a failed commit, check modified files (
git diff), stage them (git add .), and recommit.
- Pre-commit stashes unstaged changes. After a failed commit, check modified files (
-
General:
- Run
pre-commit run --all-filesto test all hooks. - Ensure
.venvis active (source .venv/bin/activate) and dependencies are installed.
- Run
Testing
Run tests after installing the package:
pip install -e .
pytest tests/test_flatten.py
Generate coverage report:
pytest tests/test_flatten.py --cov=src/flatten_tool/flatten --cov-report=xml
Troubleshooting Test Failures:
-
ModuleNotFoundError or Circular Imports:
-
Ensure the package is installed in editable mode:
pip install -e .
-
Check for circular imports in
src/flatten_tool/flatten/. Refactor modules to avoid mutual dependencies. -
Run tests with
python -m pytestto ensure proper module resolution:python -m pytest tests/test_flatten.py
-
-
Test Discovery:
- Check that test files are in
tests/and follow thetest_*.pynaming convention. - Run
pytest --collect-onlyto debug test collection.
- Check that test files are in
-
ImportError: attempted relative import with no known parent package:
- Avoid running
python src/flatten_tool/flatten/cli.pydirectly. - Use
python -m flatten_tool.flatten.clior install the package (pip install -e .) and runflatten.
- Avoid running
-
Command Not Found:
- Ensure the package is installed (
pip install -e .) and the virtual environment is active (source .venv/bin/activate). - Check
pyproject.toml’s[project.scripts]entry:flatten = "flatten_tool.flatten.cli:main".
- Ensure the package is installed (
-
Flake8 E902 FileNotFoundError for .venv, venv, dist, build, etc.:
- Ensure
.flake8and.pre-commit-config.yamlexclude these paths (e.g.,.venv,.git,tests/__pycache__). - Clear pre-commit cache:
rm -rf ~/.cache/pre-commit. - Run Flake8 manually to verify:
flake8 . --config=.flake8.
- Ensure
-
Test Failures in tests/test_flatten.py:
- test_load_config: If
ValidationErroroccurs, ensureconfig.pymerges partial configs withDEFAULT_CONFIG. - test_flatten_wildcard: Create
.flattendirectory before writingconfig.jsonin tests. - test_collect_files: Set
supported_extensionsto include relevant file types (e.g.,.js) in tests. - test_flatten_file_without_imports: Verify output has one
# File path:marker per file. - Run tests:
pytest tests/test_flatten.py --cov=src/flatten_tool/flatten --cov-report=xml.
- test_load_config: If
Running the CLI
After installing the package:
pip install -e .
flatten init
flatten ./src/ --recursive
Alternatively, run as a module:
python -m flatten_tool.flatten.cli init
python -m flatten_tool.flatten.cli flatten ./src/ --recursive
Troubleshooting CLI Issues:
- ImportError:
- Avoid running
python src/flatten_tool/flatten/cli.pydirectly, as it breaks relative imports. - Use
python -m flatten_tool.flatten.clior install the package (pip install -e .) and runflatten.
- Avoid running
- Command Not Found:
- Ensure the package is installed and the virtual environment is active.
- Check
pyproject.toml’s[project.scripts]entry.
Updating Hooks
To keep pre-commit hooks stable:
pre-commit autoupdate
git add .pre-commit-config.yaml
git commit -m "Update pre-commit hooks to stable versions"
Check versions in .pre-commit-config.yaml to avoid unstable releases.
Contributing
We welcome contributions! See CONTRIBUTING.md for guidelines on reporting issues, submitting pull requests, adding plugins, and more.
Code of Conduct
Please adhere to our Code of Conduct to ensure a welcoming and inclusive community.
Feedback
Submit feedback:
flatten feedback
Feedback is saved to .flatten/feedback/.
License
This project is licensed under the MIT License. See LICENSE for details.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file flattenify-0.1.0.tar.gz.
File metadata
- Download URL: flattenify-0.1.0.tar.gz
- Upload date:
- Size: 19.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.10.17
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
dace768d211cb889c6f2f5aa3912652a246baf8b18a0628281f9cb7e94224fe3
|
|
| MD5 |
97fb337ecf45a48c6c05b6cda5e083c6
|
|
| BLAKE2b-256 |
d29a4940dfe06b16f9139c39debd07727132369ca1d65016c40d0c3208abf286
|
File details
Details for the file flattenify-0.1.0-py3-none-any.whl.
File metadata
- Download URL: flattenify-0.1.0-py3-none-any.whl
- Upload date:
- Size: 17.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.10.17
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7015249f1d47d37ed4e3fd483b3204f6b2b0bce73887f6e75cfb70ac0a02edaa
|
|
| MD5 |
f8fbc5e4517609f3023e1cc7ebce0d0d
|
|
| BLAKE2b-256 |
c23284869e420b3f4d72b34d19a2c34c1b366b5f8b691fc4d341d8814b6c51cd
|