Skip to main content

Convert indentation between spaces and tabs in text files and directories, ignoring hidden files, binary files, and files in `.gitiginore`.

Project description

Indentation Converter

Convert indentation between spaces and tabs in text files and directories, ignoring hidden files, binary files, and files in .gitiginore.

PyPI version Python versions support
GitHub action: Build Codecov License
GitHub Contributors Issues GitHub pull requests

Report Bug · Request Feature · Ask Question

Table of Contents

About the Project

This Python script provides a flexible tool for converting indentation styles in text files between spaces and tabs. It supports converting leading spaces to tabs, leading tabs to spaces, and even converting from one number of spaces to another, all while respecting the files detailed in the .gitignore, hidden files, and binary files.

In addition to being able to run this script on its own, you can also installed it as a package via PyPI (more information on how to install it here).

Project Structure

ASCII directory structure
/
│   .gitignore
│   .pre-commit-config.yaml
│   LICENSE
│   poetry.lock
│   pyproject.toml
│   README.md
│   requirements.txt
│
├───.github
│   └───workflows
│           build.yaml
│           publish.yaml
│
├───src
│   └───indentation_converter
│           __init__.py
│           indentation_converter.py
│
└───tests
        test_indentation_converter.py

Built With

  • pathspec for .gitignore style pattern matching of file paths.
  • binaryornot to guess whether a file is binary or text.

Getting Started

Script Installation

  1. Download the .zip file from the latest release.

  2. (Optional but recommended) Create a Python virtual environment in the project root. If you're using virtualenv, you would run virtualenv venv.

  3. (Optional but recommended) Activate the virtual environment:

    # on Windows
    . venv/Scripts/activate
    # if you get the error `FullyQualifiedErrorId : UnauthorizedAccess`, run this:
    Set-ExecutionPolicy Unrestricted -Scope Process
    # and then . venv/Scripts/activate
    
    # on macOS and Linux
    source venv/Scripts/activate
    
  4. Open the command line and run pip install -r path/to/requirements.txt to install the required packages to run the script.

Package Installation

Install the PyPI package by running pip install indentation-converter.

(back to top)

Usage

Running the Program

NOTE: The commands in each usage example do the same thing. The only thing that changes is the value passed to the conversion mode (-m).


To convert the indentation of a file from 2 spaces to tabs:

python path/to/indentation_converter.py [FILE_PATH] -m st -s 2
python path/to/indentation_converter.py [FILE_PATH] -m spaces_to_tabs -s 2

To convert the indentation of a file from 4 spaces to tabs:

python path/to/indentation_converter.py [FILE_PATH] -m st
python path/to/indentation_converter.py [FILE_PATH] -m spaces_to_tabs 

The value of the -s flag defaults to 4, so it's not necessary to specify it in this case.


To convert the indentation of the files of a directory from 4 spaces to tabs:

python path/to/indentation_converter.py [DIRECTORY_PATH] -m st
python path/to/indentation_converter.py [DIRECTORY_PATH] -m spaces_to_tabs

To convert the indentation of a file from tabs to 2 spaces:

python path/to/indentation_converter.py [FILE_PATH] -m ts -s 2
python path/to/indentation_converter.py [FILE_PATH] -m tabs_to_spaces -s 2

Script Flags

Flag Description
-h or --help Displays help text for the program.
-m or --mode Conversion mode. The possible inputs are spaces-to-tabs, st, tabs-to-spaces, and ts. st is the short form of spaces_to_tabs and ts is the short form of tabs_to_spaces.
-s or --spaces-per-tab Number of spaces per tab. The default value is 4.

Using the Package in Your Project

After following the steps in the Getting Started section, import the package and the wrapper function(s) you want to use:

# Using the `process_file` and `convert_leading_spaces_to_tabs` functions
import indentation_converter as ic

ic.process_file("file.txt", ic.convert_leading_spaces_to_tabs, spaces_per_tab=4)

(back to top)

Roadmap

You can propose a new feature creating an issue.

Contributing

Contributions are what make the open source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.

License

Distributed under the MIT License. See LICENSE for more information.

Authors

See also the list of contributors who participated in this project.

Support

Would you like to support the project? That's very kind of you! You can go to my Ko-Fi profile by clicking on the button down below.

ko-fi

(back to top)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

indentation_converter-0.1.0.tar.gz (7.4 kB view hashes)

Uploaded Source

Built Distribution

indentation_converter-0.1.0-py3-none-any.whl (7.6 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page