Skip to content

Commit

Permalink
Read Flynt>=0.78 config from pyproject.toml
Browse files Browse the repository at this point in the history
  • Loading branch information
akaihola committed Jul 14, 2023
1 parent e128f8a commit 524d05a
Showing 1 changed file with 54 additions and 7 deletions.
61 changes: 54 additions & 7 deletions src/darker/fstring.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,25 @@

import logging
from pathlib import Path
from typing import Any
from typing import Any, Optional

from darker.exceptions import MissingPackageError
from darker.git import EditedLinenumsDiffer
from darker.utils import TextDocument
from flynt.pyproject_finder import find_pyproject_toml, parse_pyproject_toml

try:
import flynt

flynt_fstringify_code_by_line = flynt.process.fstringify_code_by_line
if hasattr(flynt.state, "State"):
State = flynt.state.State
else:
State = None
except ImportError:
# `flynt` is an optional dependency. Prevent the `ImportError` if it's missing.
flynt = None
State = None

def flynt_fstringify_code_by_line( # type: ignore[misc]
*args: Any, **kwargs: Any
Expand Down Expand Up @@ -42,9 +48,9 @@ def apply_flynt(
responsibility of the caller to filter output to modified lines only.
:param content: The contents of the Python source code file to sort imports in
:param src: The relative path to the file. This must be the actual path in the
repository, which may differ from the path given on the command line in
case of VSCode temporary files.
:param src: The path to the file relative to the repository root. This must be the
actual path in the repository, which may differ from the path given on
the command line in case of VSCode temporary files.
:param edited_linenums_differ: Helper for finding out which lines were edited
:return: Original Python source file contents with modifications from ``flynt``
Expand All @@ -56,18 +62,59 @@ def apply_flynt(
)
if not edited_linenums:
return content
return _call_flynt_fstringify(content)
state = _get_flynt_configuration(edited_linenums_differ.root / src)
return _call_flynt_fstringify(content, state)


def _call_flynt_fstringify(content: TextDocument) -> TextDocument:
def _get_flynt_configuration(src: Path) -> Optional[State]:
"""Read ``pyproject.toml`` Flynt configuration for the given Python file
:param src: The absolute path to the Python file to run Flynt on. This must be the
actual path in the repository, which may differ from the path given on
the command line in case of VSCode temporary files.
:return: A ``flynt`` configuration, or ``None`` for Flynt versions <0.78
"""
if State is None: # flynt<0.78
return None
state = State(quiet=True)
toml_file = find_pyproject_toml((str(src),))
if toml_file:
cfg = parse_pyproject_toml(toml_file)
mapping = {
# (state attribute name, `pyproject.toml` option)
("aggressive", "aggressive"),
("len_limit", "line_length"),
("multiline", "not no_multiline"),
("transform_concat", "transform_concats"),
("transform_format", "transform_format"),
("transform_join", "transform_joins"),
("transform_percent", "transform_percent"),
}
for state_attr, cfg_option in mapping:
if cfg_option not in cfg:
continue
if cfg_option.startswith("not "):
value = not cfg[cfg_option[4:]]
else:
value = cfg[cfg_option]
setattr(state, state_attr, value)
return state


def _call_flynt_fstringify(
content: TextDocument, state: Optional[State]
) -> TextDocument:
"""Call ``flynt.process.fstringify_code_by_line()``, return result `TextDocument`
:param content: The contents of the Python source code file to fstringify
:param state: The ``flynt`` configuration to use, or ``None`` for ``flynt<0.78``
:return: Original Python source code contents with modifications from ``flynt``
"""
logger.debug("flynt.process.fstringify_code_by_line(code=...)")
result, _ = flynt_fstringify_code_by_line(content.string)
args = () if state is None else (state,) # `()` for flynt<0.78, (state,) for >=0.78
result, _ = flynt_fstringify_code_by_line(content.string, *args)
return TextDocument.from_str(
result,
encoding=content.encoding,
Expand Down

0 comments on commit 524d05a

Please sign in to comment.