Skip to content

Commit

Permalink
Better comments and styler selection
Browse files Browse the repository at this point in the history
Better comments and styler selection
commit_hash:36057929f91c10bc1e10ab50f40420d8c478fc36
  • Loading branch information
andrei-levitskii committed Oct 22, 2024
1 parent ed4843a commit c2f82b7
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 15 deletions.
6 changes: 3 additions & 3 deletions devtools/ya/handlers/style/style.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def _discover_style_targets(args) -> dict[type[styler.BaseStyler], list[Target]]
styler_class = styler.select_styler(target=target, ruff=args.use_ruff)

if not styler_class:
logger.warning('skip %s (could not sufficient styler)', target)
logger.warning('skip %s (sufficient styler not found)', target)
continue

if file_types and styler_class.SPEC.kind not in file_types:
Expand All @@ -65,8 +65,8 @@ def _discover_style_targets(args) -> dict[type[styler.BaseStyler], list[Target]]
def _setup_logging(quiet: bool = False) -> None:
console_log = logging.StreamHandler()

for h in logging.root.handlers:
logging.root.removeHandler(h)
while logging.root.hasHandlers():
logging.root.removeHandler(logging.root.handlers[0])

console_log.setLevel(logging.ERROR if quiet else logging.INFO)
console_log.setFormatter(coloredlogs.ColoredFormatter('%(levelname).1s | %(message)s'))
Expand Down
25 changes: 13 additions & 12 deletions devtools/ya/handlers/style/styler.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,17 +50,19 @@ def _register(cls: type[BaseStyler]) -> type[BaseStyler]:


def select_styler(target: PurePath, ruff: bool) -> type[BaseStyler] | None:
"""Find matching spec and return respective styler class"""

if target.suffix in _SUFFIX_MAPPING:
key = target.suffix
elif target.name in _SUFFIX_MAPPING:
key = target.name
else:
return

target_spec = Spec(_SUFFIX_MAPPING[key][0].kind, ruff)
# find the first full match
for spec in _SUFFIX_MAPPING[key]:
if spec == target_spec:
return _REGISTRY[spec]
if (s := Spec(spec.kind, ruff)) in _REGISTRY:
return _REGISTRY[s]


@exts.func.lazy
Expand Down Expand Up @@ -92,13 +94,12 @@ def _flush_to_terminal(path: Path, content: str, formatted_content: str, full_ou


class BaseStyler(abc.ABC):
"""Whether a styler should run if not selected explicitly"""

DEFAULT_ENABLED: bool
"""Unique description of a styler"""
SPEC: Spec
"""Series of strings upon which the proper styler is selected"""
SUFFIXES: tuple[tp.LiteralString, ...]
# Whether a styler should run if not selected explicitly
DEFAULT_ENABLED: tp.ClassVar[bool]
# Unique description of a styler
SPEC: tp.ClassVar[Spec]
# Series of strings upon which the proper styler is selected
SUFFIXES: tp.ClassVar[tuple[tp.LiteralString, ...]]

def __init__(self, args) -> None:
self.args = args
Expand All @@ -124,7 +125,7 @@ def run_format() -> str:
print(run_format())
else:
target = tp.cast(Path, target)
if self.args.force or rules.style_required(str(target), content):
if self.args.force or not (reason := rules.get_skip_reason(str(target), content)):
formatted_content = run_format()
if formatted_content != content:
if self.args.dry_run:
Expand All @@ -133,7 +134,7 @@ def run_format() -> str:
_flush_to_file(target, formatted_content)
return 1
else:
logger.warning("skip by rules %s", target)
logger.warning("skip by rule: %s", reason)
return 0


Expand Down

0 comments on commit c2f82b7

Please sign in to comment.