Skip to content

Commit

Permalink
Merge pull request #501 from akaihola/darkgraylib
Browse files Browse the repository at this point in the history
Extract common parts with Graylint into Darkgraylib
  • Loading branch information
akaihola authored Mar 8, 2024
2 parents d7d3397 + 889844a commit 61c78d1
Show file tree
Hide file tree
Showing 46 changed files with 191 additions and 5,370 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/isort.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ jobs:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
- run: pip install 'isort>=5.0.1'
- uses: wearerequired/lint-action@v2.1.0
- uses: wearerequired/lint-action@v2.3.0
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
isort: true
Expand Down
4 changes: 3 additions & 1 deletion .github/workflows/mypy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ jobs:
- run: |
pip install -U \
black \
git+https://github.com/akaihola/darkgraylib.git@main \
flynt \
git+https://github.com/akaihola/graylint.git@main \
isort \
mypy>=0.990 \
pytest \
types-requests \
types-toml
- uses: wearerequired/lint-action@v2.1.0
- uses: wearerequired/lint-action@v2.3.0
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
mypy: true
Expand Down
4 changes: 3 additions & 1 deletion .github/workflows/pylint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@ jobs:
pip install -U \
airium \
black \
git+https://github.com/akaihola/darkgraylib.git@main \
defusedxml \
git+https://github.com/akaihola/graylint.git@main \
pip-requirements-parser \
pygments \
pylint \
Expand All @@ -26,7 +28,7 @@ jobs:
ruamel.yaml \
toml
pip list
- uses: wearerequired/lint-action@v2.1.0
- uses: wearerequired/lint-action@v2.3.0
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
pylint: true
Expand Down
1 change: 1 addition & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ Added
- Allow ``-`` as the single source filename when using the ``--stdin-filename`` option.
This makes the option compatible with Black.
- Upgrade NixOS tests to use Python 3.11 on both Linux and macOS.
- Move ``git_repo`` fixture to ``darkgraylib``.

Fixed
-----
Expand Down
41 changes: 21 additions & 20 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,26 @@ The following `command line arguments`_ can also be used to modify the defaults:
revision range from the ``PRE_COMMIT_FROM_REF`` and ``PRE_COMMIT_TO_REF``
environment variables. If those are not found, Darker works against ``HEAD``.
Also see ``--stdin-filename=`` for the ``:STDIN:`` special value.
--stdin-filename PATH
The path to the file when passing it through stdin. Useful so Darker can find the
previous version from Git. Only valid with ``--revision=<rev1>..:STDIN:``
(``HEAD..:STDIN:`` being the default if ``--stdin-filename`` is enabled).
-c PATH, --config PATH
Make ``darker``, ``black`` and ``isort`` read configuration from ``PATH``. Note
that other tools like ``flynt``, ``mypy``, ``pylint`` or ``flake8`` won't use
this configuration file.
-v, --verbose
Show steps taken and summarize modifications
-q, --quiet
Reduce amount of output
--color
Enable syntax highlighting even for non-terminal output. Overrides the
environment variable PY_COLORS=0
--no-color
Disable syntax highlighting even for terminal output. Overrides the environment
variable PY_COLORS=1
-W WORKERS, --workers WORKERS
How many parallel workers to allow, or ``0`` for one per core [default: 1]
--diff
Don't write the files back, just output a diff for each file on stdout. Highlight
syntax if on a terminal and the ``pygments`` package is available, or if enabled
Expand All @@ -331,10 +351,6 @@ The following `command line arguments`_ can also be used to modify the defaults:
Force complete reformatted output to stdout, instead of in-place. Only valid if
there's just one file to reformat. Highlight syntax if on a terminal and the
``pygments`` package is available, or if enabled by configuration.
--stdin-filename PATH
The path to the file when passing it through stdin. Useful so Darker can find the
previous version from Git. Only valid with ``--revision=<rev1>..:STDIN:``
(``HEAD..:STDIN:`` being the default if ``--stdin-filename`` is enabled).
--check
Don't write the files back, just return the status. Return code 0 means nothing
would change. Return code 1 means some files would be reformatted.
Expand All @@ -343,24 +359,11 @@ The following `command line arguments`_ can also be used to modify the defaults:
-i, --isort
Also sort imports using the ``isort`` package
-L CMD, --lint CMD
Also run a linter on changed files. ``CMD`` can be a name or path of the linter
Run a linter on changed files. ``CMD`` can be a name or path of the linter
binary, or a full quoted command line with the command and options. Linters read
their configuration as normally, and aren't affected by ``-c`` / ``--config``.
Linter output is syntax highlighted when the ``pygments`` package is available if
run on a terminal and or enabled by explicitly (see ``--color``).
-c PATH, --config PATH
Ask ``black`` and ``isort`` to read configuration from ``PATH``. Note that other
tools like flynt, Mypy, Pylint and Flake8 won't use this configuration file.
-v, --verbose
Show steps taken and summarize modifications
-q, --quiet
Reduce amount of output
--color
Enable syntax highlighting even for non-terminal output. Overrides the
environment variable PY_COLORS=0
--no-color
Disable syntax highlighting even for terminal output. Overrides the environment
variable PY_COLORS=1
-S, --skip-string-normalization
Don't normalize string quotes or prefixes
--no-skip-string-normalization
Expand All @@ -375,8 +378,6 @@ The following `command line arguments`_ can also be used to modify the defaults:
-t VERSION, --target-version VERSION
[py33|py34|py35|py36|py37|py38|py39|py310|py311|py312] Python versions that
should be supported by Black's output. [default: per-file auto-detection]
-W WORKERS, --workers WORKERS
How many parallel workers to allow, or ``0`` for one per core [default: 1]

To change default values for these options for a given project,
add a ``[tool.darker]`` or ``[tool.black]`` section to ``pyproject.toml`` in the
Expand Down
4 changes: 0 additions & 4 deletions mypy.ini
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,6 @@ disallow_any_explicit = False
[mypy-darker.config]
disallow_subclassing_any = False

[mypy-darker.highlighting.lexers]
disallow_any_unimported = False
disallow_subclassing_any = False

[mypy-darker.tests.conftest]
disallow_any_unimported = False

Expand Down
6 changes: 6 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ target-version = ["py311"]

[tool.isort]
profile = "black"
known_first_party = ["darkgraylib", "graylint"]
known_third_party = ["pytest"]

[tool.darker]
Expand All @@ -16,3 +17,8 @@ src = [
"src",
]
revision = "origin/master..."

[tool.pylint."messages control"]
# Check import order only with isort. Pylint doesn't support a custom list of first
# party packages. We want to consider "darkgraylib" and "graylint" as first party.
disable = ["wrong-import-order"]
5 changes: 2 additions & 3 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ packages = find:
install_requires =
# NOTE: remember to keep `constraints-oldest.txt` in sync with these
black>=21.5b1,<24.2 # upper limit until incompatibility fixed
darkgraylib @ git+https://github.com/akaihola/darkgraylib.git@main
graylint @ git+https://github.com/akaihola/graylint.git@main
toml>=0.10.0
# NOTE: remember to keep `.github/workflows/python-package.yml` in sync
# with the minimum required Python version
Expand All @@ -39,9 +41,6 @@ where = src
[options.entry_points]
console_scripts =
darker = darker.__main__:main_with_error_handling
pygments.lexers =
lint_location = darker.highlighting.lexers:LocationLexer
lint_description = darker.highlighting.lexers:DescriptionLexer

[options.extras_require]
flynt =
Expand Down
68 changes: 29 additions & 39 deletions src/darker/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
from pathlib import Path
from typing import Collection, Generator, List, Optional, Tuple

import darker.black_compat
from darker.black_diff import (
BlackConfig,
filter_python_files,
Expand All @@ -20,35 +19,35 @@
from darker.chooser import choose_lines
from darker.command_line import parse_command_line
from darker.concurrency import get_executor
from darker.config import Exclusions, OutputMode, dump_config
from darker.config import Exclusions, OutputMode, validate_config_output_mode
from darker.diff import diff_chunks
from darker.exceptions import DependencyError, MissingPackageError
from darker.fstring import apply_flynt, flynt
from darker.git import (
PRE_COMMIT_FROM_TO_REFS,
STDIN,
WORKTREE,
EditedLinenumsDiffer,
RevisionRange,
get_missing_at_revision,
get_path_in_repo,
git_get_content_at_revision,
git_get_modified_python_files,
git_is_repository,
)
from darker.help import get_extra_instruction
from darker.highlighting import colorize, should_use_color
from darker.import_sorting import apply_isort, isort
from darker.linting import run_linters
from darker.utils import (
GIT_DATEFORMAT,
DiffChunk,
TextDocument,
debug_dump,
get_common_root,
glob_any,
)
from darker.utils import debug_dump, glob_any
from darker.verification import ASTVerifier, BinarySearch, NotEquivalentError
from darkgraylib.black_compat import find_project_root
from darkgraylib.config import show_config_if_debug
from darkgraylib.git import (
PRE_COMMIT_FROM_TO_REFS,
STDIN,
WORKTREE,
RevisionRange,
git_get_content_at_revision,
)
from darkgraylib.highlighting import colorize, should_use_color
from darkgraylib.log import setup_logging
from darkgraylib.main import resolve_paths
from darkgraylib.utils import GIT_DATEFORMAT, DiffChunk, TextDocument
from graylint.linting import run_linters

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -480,23 +479,19 @@ def main( # pylint: disable=too-many-locals,too-many-branches,too-many-statemen
should be) reformatted; 0 otherwise.
"""
if argv is None:
argv = sys.argv[1:]
args, config, config_nondefault = parse_command_line(argv)
logging.basicConfig(level=args.log_level)
if args.log_level == logging.INFO:
formatter = logging.Formatter("%(levelname)s: %(message)s")
logging.getLogger().handlers[0].setFormatter(formatter)

# Make sure there aren't invalid option combinations after merging configuration and
# command line options.
OutputMode.validate_diff_stdout(args.diff, args.stdout)
OutputMode.validate_stdout_src(args.stdout, args.src, args.stdin_filename)
validate_config_output_mode(config)

setup_logging(args.log_level)
# Make sure we don't get excessive debug log output from Black
logging.getLogger("blib2to3.pgen2.driver").setLevel(logging.WARNING)

if args.log_level <= logging.DEBUG:
print("\n# Effective configuration:\n")
print(dump_config(config))
print("\n# Configuration options which differ from defaults:\n")
print(dump_config(config_nondefault))
print("\n")
show_config_if_debug(config, config_nondefault, args.log_level)

if args.isort and not isort:
raise MissingPackageError(
Expand All @@ -520,16 +515,11 @@ def main( # pylint: disable=too-many-locals,too-many-branches,too-many-statemen
if args.skip_magic_trailing_comma is not None:
black_config["skip_magic_trailing_comma"] = args.skip_magic_trailing_comma

stdin_mode = args.stdin_filename is not None
if stdin_mode:
paths = {Path(args.stdin_filename)}
# `parse_command_line` guarantees that `args.src` is empty
else:
paths = {Path(p) for p in args.src}
# `parse_command_line` guarantees that `args.stdin_filename` is `None`
root = get_common_root(paths)
paths, root = resolve_paths(args.stdin_filename, args.src)

revrange = RevisionRange.parse_with_common_ancestor(args.revision, root, stdin_mode)
revrange = RevisionRange.parse_with_common_ancestor(
args.revision, root, args.stdin_filename is not None
)
output_mode = OutputMode.from_args(args)
write_modified_files = not args.check and output_mode == OutputMode.NOTHING
if write_modified_files:
Expand Down Expand Up @@ -574,7 +564,7 @@ def main( # pylint: disable=too-many-locals,too-many-branches,too-many-statemen
# In other modes, only reformat files which have been modified.
if git_is_repository(root):
# Get the modified files only.
repo_root = darker.black_compat.find_project_root([str(root)])
repo_root = find_project_root([str(root)])
changed_files = {
(repo_root / file).relative_to(root)
for file in git_get_modified_python_files(paths, revrange, repo_root)
Expand Down
Loading

0 comments on commit 61c78d1

Please sign in to comment.