Skip to main content

Less uncompromising Python code formatter

Project description

gray

Package Version Build Status

Less uncompromising Python code formatter.

Gray stands on the shoulders of giants:

Usage

usage:
        gray myapp.py
        gray myproj/ tests/
        gray --log-level debug --formatters isort,unify ~/app


Less uncompromising Python code formatter. Args that start with '--' (eg.
--pool-size) can also be set in a config file (/Users/dizballanze/.gray or
/etc/gray.conf or ./gray.conf). Config file syntax allows: key=value,
flag=true, stuff=[a,b,c] (for details, see syntax at https://goo.gl/R74nmi).
If an arg is specified in more than one place, then commandline values
override config file values which override defaults.

positional arguments:
  paths                 Paths to format (default: (PosixPath('.'),))

optional arguments:
  -h, --help            show this help message and exit
  --pool-size POOL_SIZE
                        process pool size (default: 8)
  --do-not-detect-venv  Don't try to detect virtualenv (default: False)

Logging options:
  --log-level {debug,info,warning,error,fatal}
  --log-format {stream,color,json,syslog}

Formatters options:
  -f FORMATTERS, --formatters FORMATTERS
                        Enabled formatters separated by comma (default: add-
                        trailing-comma,autoflake,isort,pyupgrade,trim,unify)
  --min-python-version MIN_PYTHON_VERSION
                        Minimum python version to support (default: (3, 7))

pyupgrade options:
  --pyupgrade-keep-percent-format
                        Do not upgrade percent formatted strings to f-strings
                        (default: False)

unify options:
  --unify-quote UNIFY_QUOTE
                        preferred quote (default: ")

isort options:
  --isort-line-length ISORT_LINE_LENGTH
                        isort section (default: 80)
  --isort-virtual-env ISORT_VIRTUAL_ENV
                        virtual env path (default:
                        /Users/dizballanze/apps/gray/env)
  --isort-include-trailing-comma ISORT_INCLUDE_TRAILING_COMMA
                        include a trailing comma on multi line imports
                        (default: 1)
  --isort-lines-after-imports ISORT_LINES_AFTER_IMPORTS
                        empty lines after imports (default: 2)

autoflake options:
  --autoflake-ignore-init-module-imports AUTOFLAKE_IGNORE_INIT_MODULE_IMPORTS
  --autoflake-expand-star-imports AUTOFLAKE_EXPAND_STAR_IMPORTS
  --autoflake-remove-all-unused-imports AUTOFLAKE_REMOVE_ALL_UNUSED_IMPORTS
  --autoflake-remove-duplicate-keys AUTOFLAKE_REMOVE_DUPLICATE_KEYS
  --autoflake-remove-unused-variables AUTOFLAKE_REMOVE_UNUSED_VARIABLES

trim options:
  --trim-leading-newlines TRIM_LEADING_NEWLINES

Git Hook

You can setup gray formatting before each commit with pre-commit git hook. Add following file to .git/hooks/pre-commit and make it executable with chmod +x .git/hooks/pre-commit.

#!/usr/bin/env python
from gray.hooks import git_pre_commit

exit(git_pre_commit(stop_on_modify=True))

If stop_on_modify argument is True, hook will prevent commit if there are any unstaged changes in files you about to commit.

Otherwise, any unstaged changes in this files will be added to the index by the hook.

Config file

Gray is looking for config file in ./gray.conf, /etc/gray.conf or ~/.gray.

Example of grayconf:

formatters = add-trailing-comma,isort,unify
min-python-version = 3.5

Features

  • TODO

Git pre-commit hook

Use pre-commit. Once you have it installed, add this to the .pre-commit-config.yaml in your repository:

repos:
  - repo: https://github.com/dizballanze/gray
    rev: master # Replace by any tag/branch: https://github.com/dizballanze/gray/tags
    hooks:
      - id: gray

and run pre-commit install.

Using with Sourcetree

Sourcetree may run without gray being available via PATH.

MacOS

Make sure gray is available via PATH and run open /Applications/Sourcetree.app. Or better create an Automator workflow with source ~/.bash_profile && open /Applications/Sourcetree.app script.

History

0.7.0

0.6.2

  • isort: keep line breaks before local imports

0.6.1

  • Fix isort imports grouping

0.6.0

  • Bump isort to 5.4.2
  • Use parentheses in multiline imports

0.5.0

  • Add autoflake formatter
  • Add trim formatter

0.4.0

  • Minimum python version option
  • Pyupgrade options
  • Detecting and skipping virtualenv

0.3.0

0.2.0

  • Pre-commit hook
  • Some formating options

0.1.0 (2020-04-06)

  • First release on PyPI.

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

gray-0.7.0.tar.gz (13.9 kB view details)

Uploaded Source

Built Distribution

gray-0.7.0-py2.py3-none-any.whl (15.6 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file gray-0.7.0.tar.gz.

File metadata

  • Download URL: gray-0.7.0.tar.gz
  • Upload date:
  • Size: 13.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.7.7

File hashes

Hashes for gray-0.7.0.tar.gz
Algorithm Hash digest
SHA256 b4cce65f043272f96aab30e0ae97fa81a8223303bc675be986b59c2501db61a5
MD5 eba2763633ed881a496733a9390639c2
BLAKE2b-256 d66f0d8d8747e30b5ddbf3d2652714bb84ffa3a9a3391d4cc880733187c174fe

See more details on using hashes here.

File details

Details for the file gray-0.7.0-py2.py3-none-any.whl.

File metadata

  • Download URL: gray-0.7.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 15.6 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.48.2 CPython/3.7.7

File hashes

Hashes for gray-0.7.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 db9694bb287aef0753e85194336f4623902585479e3be55945534b12eae59322
MD5 7073af60457943f1205a2f723d51e41b
BLAKE2b-256 69d3e53d69b2df13e21ea4f093d3f5f9b0901b05fd5be136b10c217784d72796

See more details on using hashes here.

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