Skip to content
This repository has been archived by the owner on Jul 28, 2024. It is now read-only.

Commit

Permalink
feat: pip package and CLI support
Browse files Browse the repository at this point in the history
  • Loading branch information
sweetbrulee committed Jul 21, 2024
1 parent 98c848d commit 9f29d84
Show file tree
Hide file tree
Showing 8 changed files with 413 additions and 185 deletions.
162 changes: 162 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,162 @@
# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
share/python-wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.nox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
*.py,cover
.hypothesis/
.pytest_cache/
cover/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3
db.sqlite3-journal

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
.pybuilder/
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
# For a library or package, you might want to ignore these files since the code is
# intended to run in multiple environments; otherwise, check them in:
# .python-version

# pipenv
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
# However, in case of collaboration, if having platform-specific dependencies or dependencies
# having no cross-platform support, pipenv may install dependencies that don't work, or not
# install all needed dependencies.
#Pipfile.lock

# poetry
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
# This is especially recommended for binary packages to ensure reproducibility, and is more
# commonly ignored for libraries.
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
#poetry.lock

# pdm
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
#pdm.lock
# pdm stores project-wide configurations in .pdm.toml, but it is recommended to not include it
# in version control.
# https://pdm.fming.dev/latest/usage/project/#working-with-version-control
.pdm.toml
.pdm-python
.pdm-build/

# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
__pypackages__/

# Celery stuff
celerybeat-schedule
celerybeat.pid

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json

# Pyre type checker
.pyre/

# pytype static type analyzer
.pytype/

# Cython debug symbols
cython_debug/

# PyCharm
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
# and can be added to the global gitignore or merged into this file. For a more nuclear
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
#.idea/
2 changes: 2 additions & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
include README.md
include LICENSE
200 changes: 114 additions & 86 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,86 +1,114 @@
## Files Flattener

This repository contains implementations for recursively reading all files in a specified directory and outputting their filenames and contents to a specified output file.

- [Getting Started](#getting-started)
- [Python](#python)

## Why do I need this?

Imagine you are working on a project and need to share the project structure and file contents with a collaborator or an **AI platform** for analysis. Instead of manually copying and pasting each file's content, you can use the files_flattener.py script to generate a single file with all the necessary information. This consolidated file can then be easily shared, ensuring that the recipient has all the context needed to understand the project.

### Example

Assume you have the following directory structure:

```
|
|--folder1
| |--file1.txt
| |--file2.txt
|--file3.txt
```

And you have an `.ignore` file in the root directory with the following content:

```
folder1/file2.txt
# Exclude itself
.ignore
```

Running the implementation will process the files, ignoring `folder1/file2.txt` and `.ignore` itself as specified in the `.ignore` file. The output file will contain the contents of the remaining files in the following format:

```
**folder1/file1.txt:**
[...file1.txt's content...]
**file3.txt:**
[...file3.txt's content...]
```

## Getting Started

### Python

#### Usage

1. Ensure you have Python installed on your system.
2. Install the required dependencies by running:

```sh
pip install -r requirements.txt
```

3. Run the script using the command:

```sh
python files_flattener.py <directory> <output_file> [<ignore_file>]
```

#### Parameters

- `<directory>`: The path of the directory containing the files to be flattened.
- `<output_file>`: The path of the output file where the contents of the files will be written.
- `[<ignore_file>]`: (Optional) The path to a file containing patterns of files to ignore. If not provided, the script will look for a '.ignore' file in the specified directory. If the '.ignore' file is not found, no files will be ignored.

## TODO

- [ ] Add dry-run mode to preview the output before writing to the file.
- [ ] Make a Node library version.
- [ ] Make a web app based on Vue3.
- [x] Use `.ignore` to exclude or include files for flattening.

## License

This project is licensed under the MIT License.
# Files Flattener

This repository contains implementations for recursively reading all files in a specified directory and outputting their filenames and contents to a specified output file.

- [Why do I need this?](#why-do-i-need-this)
- [Example](#example)
- [Getting Started](#getting-started)
- [Usage](#usage)
- [Development](#development)
- [TODO](#todo)
- [License](#license)

## Why do I need this?

Imagine you are working on a project and need to share the project structure and file contents with a collaborator or an **AI platform** for analysis. Instead of manually copying and pasting each file's content, you can use the files_flattener.py script to generate a single file with all the necessary information. This consolidated file can then be easily shared, ensuring that the recipient has all the context needed to understand the project.

## Example

Assume you have the following directory structure:

```
|
|--folder1
| |--file1.txt
| |--file2.txt
|--file3.txt
```

And you have an `.ignore` file in the root directory with the following content:

```
folder1/file2.txt
# Exclude itself
.ignore
```

Running the implementation will process the files, ignoring `folder1/file2.txt` and `.ignore` itself as specified in the `.ignore` file. The output file will contain the contents of the remaining files in the following format:

```
**folder1/file1.txt:**
[...file1.txt's content...]
**file3.txt:**
[...file3.txt's content...]
```

## Getting Started

### Usage

1. Ensure you have Python installed on your system.
2. Install the package using pip:
```sh
pip install files_flattener
```
3. Run the command:

```sh
flt <directory> <output_file> [<ignore_file>]
```

- `<directory>`: The path of the directory containing the files to be flattened.
- `<output_file>`: The path of the output file where the contents of the files will be written.
- `[<ignore_file>]`: (Optional) The path to a file containing patterns of files to ignore. If not provided, the script will look for a '.ignore' file in the specified directory. If the '.ignore' file is not found, no files will be ignored.

### Development

#### Install the required dependencies

```sh
pip install -r requirements.txt
```

#### Build the package

Ensure `wheel` is installed:

```sh
pip install wheel
```

Generate the distribution files:

```sh
rm -rf build/ dist/
python setup.py sdist bdist_wheel
```

#### Publish the package to PyPI

Install `twine` if you haven't already:

```sh
pip install twine
```

Upload the distribution files to PyPI:

```sh
twine upload dist/*
```

## TODO

- [ ] Add dry-run mode to preview the output before writing to the file.
- [ ] Make a web app based on Vue3.
- [x] Use `.ignore` to exclude or include files for flattening.

## License

This project is licensed under the MIT License.
Empty file added files_flattener/__init__.py
Empty file.
24 changes: 24 additions & 0 deletions files_flattener/cli.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import sys
from .core import list_files, write_files_to_output, USAGE


def main():
if len(sys.argv) < 3:
print(USAGE)
sys.exit(1)

directory = sys.argv[1]
output_file = sys.argv[2]
ignore_file = sys.argv[3] if len(sys.argv) > 3 else None # Optional

files_list = list_files(directory, ignore_file)

for file in files_list:
print(file)

write_files_to_output(directory, output_file, files_list)
print(f"All files have been successfully written to {output_file}")


if __name__ == "__main__":
main()
Loading

0 comments on commit 9f29d84

Please sign in to comment.