Skip to main content

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)

Metadata

Release files for indentation-converter 0.1.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for indentation-converter 0.1.0
File Size Uploaded
indentation_converter-0.1.0.tar.gz 7.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for indentation-converter 0.1.0
File Interpreter ABI Platform
indentation_converter-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 15.0 kB

Release files / indentation_converter-0.1.0.tar.gz

Download URL indentation_converter-0.1.0.tar.gz
Size 7.4 kB
Tags Source
SHA-256 checksum
How to use checksums
045e4bd10a323c8e37a780ea4cad7b3a0061ec6ff191c3eb07a0730e6dbecd6f
BLAKE2b-256 checksum
How to use checksums
4e6afc5d332ac00baf7c35f3dff6b9ffcd05b037c66b26121404a360610ef4cf
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.3 CPython/3.10.5 Windows/10

Release files / indentation_converter-0.1.0-py3-none-any.whl

Download URL indentation_converter-0.1.0-py3-none-any.whl
Size 7.6 kB
Tags Python 3
SHA-256 checksum
How to use checksums
587c03b8508a6efa16ee28f7925cf722a3ca63a5b32699b00035868a934f87b7
BLAKE2b-256 checksum
How to use checksums
261e2cdaa60d68f328a6280ecf355f33bc85b3184874099f8db0179668b3bd01
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.8.3 CPython/3.10.5 Windows/10

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page