Skip to content

Commit

Permalink
Use ruff, fix some of the warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
insolor committed Jun 1, 2024
1 parent 3d9f772 commit 9e8c66b
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 21 deletions.
8 changes: 4 additions & 4 deletions df_translation_toolkit/parse/parse_raws.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,23 +75,23 @@ def parse_raw_file(file: Iterable[str]) -> Iterator[FilePartInfo]:
context = None
for token in tokenize_raw_file(file):
if not token.is_tag:
yield FilePartInfo(token.line_number, False, context, text=token.text)
yield FilePartInfo(line_number=token.line_number, translatable=False, context=context, text=token.text)
else:
tag = token.text
tag_parts = split_tag(tag)

if tag_parts[0] == "OBJECT":
object_name = tag_parts[1]
yield FilePartInfo(token.line_number, False, context, tag=tag)
yield FilePartInfo(line_number=token.line_number, translatable=False, context=context, tag=tag)
elif object_name and (
tag_parts[0] == object_name
or (object_name in {"ITEM", "BUILDING"} and tag_parts[0].startswith(object_name))
or object_name.endswith("_" + tag_parts[0])
):
context = ":".join(tag_parts)
yield FilePartInfo(token.line_number, False, context, tag=tag)
yield FilePartInfo(token.line_number, translatable=False, context=context, tag=tag)
else:
yield FilePartInfo(token.line_number, True, context, tag=tag, tag_parts=tag_parts)
yield FilePartInfo(token.line_number, translatable=True, context=context, tag=tag, tag_parts=tag_parts)


def extract_translatables_from_raws(file: Iterable[str]) -> Iterator[TranslationItem]:
Expand Down
6 changes: 3 additions & 3 deletions df_translation_toolkit/validation/validation_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@ class ProblemSeverity(Enum):
ERROR = auto()
WARNING = auto()

def __str__(self):
def __str__(self) -> str:
return self.name.title()


class ValidationProblem(NamedTuple):
text: str
severity: ProblemSeverity = ProblemSeverity.ERROR

def __str__(self):
def __str__(self) -> str:
return f"{self.severity}: {self.text}"

@staticmethod
Expand All @@ -28,5 +28,5 @@ class ValidationException(Exception):
def __init__(self, problems: list[ValidationProblem]):
self.problems = problems

def __str__(self):
def __str__(self) -> str:
return "\n".join(str(error) for error in self.problems)
28 changes: 22 additions & 6 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,8 @@ cairosvg = "^2.7.0"
pytest = "^8.2.1"
pytest-cov = "^5.0.0"
pytest-mock = "^3.14.0"
flake8 = "^7.0.0"
black = "^24.4.2"
isort = "^5.13.2"
coverage = "^7.5.3"
flake8 = "^7.0.0"
refurb = "^2.0.0"
ruff = "^0.4.7"

Expand All @@ -45,8 +43,26 @@ profile = "black"
line_length = 120

[tool.ruff]
target-version = "py310"
line-length = 120
select = ["E", "F", "UP"]
ignore = ["UP015"]

[tool.ruff.lint]
select = ["ALL"]
ignore = [
"D",
"RUF001",
]
fixable = ["ALL"]
unfixable = ["F401"]
target-version = "py310"

[tool.ruff.lint.per-file-ignores]
"tests/*" = [
"S101", # Don't warn about using of asserts in tests
"ANN001",
"ANN201",
"ANN202",
"D", # Don't warn about missing documentation in tests
"PT006",
"PT007",
"E501",
]
2 changes: 1 addition & 1 deletion tests/test_objects_po_to_csv.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"original_parts,translation_parts,result",
[
(
# TODO: replace this test with a hypothesis test
# TODO(@insolor): replace this test with a hypothesis test
["abc", "cde", "def"],
["wefw", "rtt", "jty"],
[("abc", "wefw"), ("cde", "rtt"), ("def", "jty")],
Expand Down
10 changes: 5 additions & 5 deletions tests/test_parse_raws.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
|[GENERAL_CHILD_NAME:cardinal hatchling:cardinal hatchlings]
|[CREATURE_TILE:144][COLOR:4:0:1]
|[PETVALUE:30][NATURAL][PET]
"""
""",
).strip(),
[
TranslationItem(
Expand Down Expand Up @@ -75,7 +75,7 @@
|[ATTACK:BLUNT:1:10:lash:lashes:NO_SUB:5000] - 5000 is not translatable
| [ATTACK_PREPARE_AND_RECOVER:4:4]
| [ATTACK_FLAG_BAD_MULTIATTACK]
"""
""",
).strip(),
[
TranslationItem(context="ITEM_WEAPON:ITEM_WEAPON_WHIP", text="[NAME:whip:whips]"),
Expand Down Expand Up @@ -139,7 +139,7 @@ def test_extract_translatables_from_raws(content, expected):
| Some comment
| [MATERIAL_SIZE:1] trailing comments are trimmed
| [ATTACK:BLUNT:1:10:lash:lashes:NO_SUB:5000]
"""
""",
).strip(),
{
(
Expand All @@ -164,9 +164,9 @@ def test_extract_translatables_from_raws(content, expected):
| Some comment
| [MATERIAL_SIZE:1]
| [ATTACK:BLUNT:1:10:Lash:Lashes:NO_SUB:5000]
"""
""",
).strip(),
)
),
],
)
def test_translate_raw_file(content, dictionary, result):
Expand Down
4 changes: 2 additions & 2 deletions tests/test_translate_raws_batch.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ def function(po_filename, path, encoding):
directory_mock = Mock("directory")
directory_mock.is_dir = lambda: True
directory_mock.match = lambda pattern: pattern == "*/" + directory_name
base_dir_mock.rglob = lambda *args: [directory_mock]
base_dir_mock.rglob = lambda *_args: [directory_mock]
po_directory = PurePath("po_directory")
encoding = "utf-8"
postfix = "postfix"

assert list(
translate_files(base_dir_mock, po_directory, encoding, po_name_postfix=postfix, directory_patterns=patterns)
translate_files(base_dir_mock, po_directory, encoding, po_name_postfix=postfix, directory_patterns=patterns),
) == [
f"Matched {directory_name} pattern",
(po_directory / f"{po_filename}_{postfix}.po", directory_mock, encoding),
Expand Down

0 comments on commit 9e8c66b

Please sign in to comment.