Skip to content

Commit da309d8

Browse files
committed
Create BaseProcessor class to keep common abilities
1 parent 9b9f19c commit da309d8

File tree

1 file changed

+34
-24
lines changed

1 file changed

+34
-24
lines changed

src/ruffen_docs/formatters.py

Lines changed: 34 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import contextlib
44
import re
55
import textwrap
6+
from abc import ABC, abstractmethod
67
from bisect import bisect
78
from collections.abc import Generator, Sequence
89
from pathlib import Path
@@ -33,34 +34,14 @@
3334
)
3435

3536

36-
class BlackFormatter:
37-
def __init__(
38-
self,
39-
# FIXME:
40-
# The original default value fails with
41-
# > TypeError: set object expected; got dataclasses.Field
42-
target_versions: set[TargetVersion] | None = None,
43-
line_length: int = DEFAULT_LINE_LENGTH,
44-
string_normalization: bool = True,
45-
is_pyi: bool = False,
46-
preview: bool = False,
47-
) -> None:
48-
if target_versions is None:
49-
target_versions = set()
50-
37+
class BaseProcessor(ABC):
38+
def __init__(self) -> None:
5139
self.errors: list[CodeBlockError] = []
5240
self.off_ranges: list[tuple[int, int]] = []
5341

54-
self.mode: Mode = Mode(
55-
target_versions=target_versions,
56-
line_length=line_length,
57-
string_normalization=string_normalization,
58-
is_pyi=is_pyi,
59-
preview=preview,
60-
)
61-
42+
@abstractmethod
6243
def process_code_block(self, code_block: str) -> str:
63-
return black.format_str(code_block, mode=self.mode)
44+
pass
6445

6546
def _within_off_range(self, code_range: tuple[int, int]) -> bool:
6647
index = bisect(self.off_ranges, code_range)
@@ -312,3 +293,32 @@ def format_file(
312293
f.write(new_contents)
313294

314295
return 1
296+
297+
298+
class BlackFormatter(BaseProcessor):
299+
def __init__(
300+
self,
301+
# FIXME:
302+
# The original default value fails with
303+
# > TypeError: set object expected; got dataclasses.Field
304+
target_versions: set[TargetVersion] | None = None,
305+
line_length: int = DEFAULT_LINE_LENGTH,
306+
string_normalization: bool = True,
307+
is_pyi: bool = False,
308+
preview: bool = False,
309+
) -> None:
310+
if target_versions is None:
311+
target_versions = set()
312+
313+
self.mode: Mode = Mode(
314+
target_versions=target_versions,
315+
line_length=line_length,
316+
string_normalization=string_normalization,
317+
is_pyi=is_pyi,
318+
preview=preview,
319+
)
320+
321+
super().__init__()
322+
323+
def process_code_block(self, code_block: str) -> str:
324+
return black.format_str(code_block, mode=self.mode)

0 commit comments

Comments
 (0)