跳转到主要内容

更不妥协的Python代码格式化工具

项目描述

灰色

Package Version Build Status

Less uncompromising Python code formatter.

Gray stands on the shoulders of giants

  • isort - imports sorting and more
  • pyupgrade - automatically upgrades syntax for newer versions of Python
  • autoflake - remove unused imports and variables
  • 添加尾随逗号
  • trim - remove trailing whitespaces
  • unify - unify quotes style
  • fixit - various code formatters on LibCST
  • black - optional - the uncompromising Python code formatter

使用方法

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


Less uncompromising Python code formatter.

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

optional arguments:
  -h, --help            show this help message and exit
  --exclude EXCLUDE     A regular expression matching files and directories that should be
                        excluded from formatting. Passing an explicit empty value means not paths
                        get excluded. Use '/' as directory separator, including on Windows.
                        [default: (.*/)?(\.direnv|\.eggs|\.git|\.hg|\.mypy_cache|\.nox|\.tox|\.ven
                        v|venv|\.svn|_build|buck-out|build|dist|__pypackages__)$] (default: (.*/)?
                        (\.direnv|\.eggs|\.git|\.hg|\.mypy_cache|\.nox|\.tox|\.venv|venv|\.svn|_bu
                        ild|buck-out|build|dist|__pypackages__)$)
  --extend-exclude EXTEND_EXCLUDE
                        An additional regular expression to use besides --exclude. This allows to
                        keep the default regex from --exclude. (default: None)
  --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}

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

pyupgrade options:
  --pyupgrade-keep-percent-format PYUPGRADE_KEEP_PERCENT_FORMAT
                        Do not upgrade percent formatted strings to f-strings (default: False)
  --pyupgrade-keep-mock PYUPGRADE_KEEP_MOCK
                        Disable rewrite of mock imports (default: False)
  --pyupgrade-keep-runtime-typing PYUPGRADE_KEEP_RUNTIME_TYPING
                        Disable pep 585 typing rewrites (default: False)

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

isort options:
  --isort-profile ISORT_PROFILE
                        Base profile type to use for configuration. (default: None)
  --isort-line-length ISORT_LINE_LENGTH
                        The max length of an import line (used for wrapping long imports)
                        (default: 80)
  --isort-wrap-length ISORT_WRAP_LENGTH
                        Specifies how long lines that are wrapped should be, if not set
                        line_length is used. NOTE: wrap_length must be LOWER than or equal to
                        line_length (default: None)
  --isort-multi-line-output ISORT_MULTI_LINE_OUTPUT
                        Multi line output (0-grid, 1-vertical, 2-hanging, 3-vert-hanging, 4-vert-
                        grid, 5-vert-grid-grouped, 6-deprecated-alias-for-5, 7-noqa, 8-vertical-
                        hanging-indent-bracket, 9-vertical-prefix-from-module-import, 10-hanging-
                        indent-with-parentheses). (default: 5)
  --isort-known-third-party ISORT_KNOWN_THIRD_PARTY
                        Force isort to recognize a module as being part of a third party library.
                        (default: None)
  --isort-known-first-party ISORT_KNOWN_FIRST_PARTY
                        Force isort to recognize a module as being part of the current python
                        project. (default: None)
  --isort-known-local-folder ISORT_KNOWN_LOCAL_FOLDER
                        Force isort to recognize a module as being a local folder. Generally, this
                        is reserved for relative imports (from . import module). (default: None)
  --isort-virtual-env ISORT_VIRTUAL_ENV
                        virtual env path (default: env)
  --isort-include-trailing-comma ISORT_INCLUDE_TRAILING_COMMA
                        Includes a trailing comma on multi line imports that include parentheses.
                        (default: True)
  --isort-lines-after-imports ISORT_LINES_AFTER_IMPORTS
                        empty lines after imports (default: 2)
  --isort-skip-gitignore ISORT_SKIP_GITIGNORE
                        Treat project as a git repository and ignore files listed in .gitignore.
                        (default: True)
  --isort-use-parentheses ISORT_USE_PARENTHESES
                        Use parentheses for line continuation on length limit instead of slashes.
                        **NOTE**: This is separate from wrap modes, and only affects how
                        individual lines that are too long get continued, not sections of multiple
                        imports.. (default: True)
  --isort-length-sort ISORT_LENGTH_SORT
                        Sort imports by their string length. (default: False)

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

fixit options:
  --fixit-redundant-fstrings FIXIT_REDUNDANT_FSTRINGS
  --fixit-redundant-lambdas FIXIT_REDUNDANT_LAMBDAS
  --fixit-redundant-list-comprehensions FIXIT_REDUNDANT_LIST_COMPREHENSIONS
  --fixit-to-comprehensions FIXIT_TO_COMPREHENSIONS
  --fixit-to-literals FIXIT_TO_LITERALS
  --fixit-to-fstrings FIXIT_TO_FSTRINGS

black options:
  --black-line-length BLACK_LINE_LENGTH
                        How many characters per line to allow. (default: 88)
  --black-skip-magic-trailing-comma BLACK_SKIP_MAGIC_TRAILING_COMMA
                        Don't use trailing commas as a reason to split lines. (default: False)
  --black-skip-string-normalization BLACK_SKIP_STRING_NORMALIZATION
                        Don't normalize string quotes or prefixes. (default: False)

Args that start with '--' (eg. --exclude) can also be set in a config file (/Users/stephane/.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.

Git钩子

您可以使用pre-commit git钩子在每次提交之前设置灰色格式。将以下文件添加到.git/hooks/pre-commit,并使用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))

如果stop_on_modify参数为True,钩子将阻止提交,如果您要提交的文件中有任何未暂存的更改。

否则,此文件中的任何未暂存更改都将由钩子添加到索引中。

配置文件

灰色正在查找配置文件在./gray.conf/etc/gray.conf~/.gray

grayconf示例

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

功能

  • 待办事项

Git预提交钩子

使用 pre-commit。一旦安装,请将以下内容添加到您仓库中的 .pre-commit-config.yaml

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

并运行 pre-commit install

与 Sourcetree 一起使用

Sourcetree 可能在没有通过 PATH 可用的情况下运行 gray

MacOS

确保 gray 通过 PATH 可用,并运行 open /Applications/Sourcetree.app。或者更好的方法是 创建 一个 Automator 工作流程,包含脚本 source ~/.bash_profile && open /Applications/Sourcetree.app

历史记录

0.15.0

  • 添加对 isort add-imports 和 remove-imports 功能的支持(由 @sodul 实现)

0.14.0

  • 取消对 Python 3.7 的支持,添加对 Python 3.12 的支持,升级 ifixit 到 v2(由 @sodul 实现)

0.13.0

  • 添加对 Python 3.11 的支持(由 @mosquito 实现)

0.12.0

  • 添加支持排除文件和文件夹(由 @sodul 实现)可能存在与默认排除列表的向后兼容性问题。某些文件和文件夹现在可能被排除,而在旧版本中则会被处理。
  • 删除 travis.yml,因为它不再可用。

0.11.0

  • 添加 GitHub Action 以验证 PR(由 @sodul 实现)
  • 更新第三方要求(由 @sodul 实现)
  • 取消对 Python 3.6 的支持(由 @sodul 实现)

0.10.0

0.9.0

  • 更新 python 依赖项

0.8.1

  • 添加对 python 3.9 的支持

0.8.0

0.7.0

0.6.2

  • isort: 保持本地导入前的换行符

0.6.1

  • 修复 isort 导入分组问题

0.6.0

  • 将 isort 升级到 5.4.2
  • 在多行导入中使用括号

0.5.0

  • 添加 autoflake 格式化器
  • 添加 trim 格式化器

0.4.0

  • 最小 python 版本选项
  • Pyupgrade 选项
  • 检测并跳过虚拟环境

0.3.0

0.2.0

  • Pre-commit 钩子
  • 一些格式化选项

0.1.0 (2020-04-06)

  • 首次发布在 PyPI 上。

项目详细信息


下载文件

下载您平台上的文件。如果您不确定选择哪个,请了解更多关于 安装软件包 的信息。

源分布

gray-0.15.0.tar.gz (23.0 kB 查看哈希值)

上传时间

构建分布

gray-0.15.0-py2.py3-none-any.whl (21.5 kB 查看哈希值)

上传时间 Python 2 Python 3

支持者