Skip to content

Commit 6045028

Browse files
committed
Change pre-commit hooks and add tests for convert_parse
1 parent bf3dfa7 commit 6045028

10 files changed

Lines changed: 926 additions & 22 deletions

File tree

.pre-commit-config.yaml

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,17 @@ repos:
1616
- id: check-merge-conflict
1717
- id: mixed-line-ending
1818
- id: check-case-conflict
19+
- id: check-added-large-files
1920
- id: sort-simple-yaml
2021
files: .pre-commit-config.yaml
21-
- repo: https://github.com/psf/black-pre-commit-mirror
22-
rev: 25.1.0
23-
hooks:
24-
- id: black
2522
- repo: https://github.com/astral-sh/ruff-pre-commit
26-
rev: v0.12.2
23+
rev: v0.12.7
2724
hooks:
28-
- id: ruff
25+
- id: ruff-format
26+
- id: ruff-check
2927
types: [file]
3028
types_or: [python, pyi, toml]
29+
args: ["--show-fixes"]
3130
- repo: https://github.com/CoolCat467/badgie
3231
rev: v0.9.6
3332
hooks:

README.md

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ Script for simplifying the process of translating Language (.lang) files
55

66
[![pre-commit.ci status](https://results.pre-commit.ci/badge/github/CoolCat467/Localization-Translation-Utility/main.svg)](https://results.pre-commit.ci/latest/github/CoolCat467/Localization-Translation-Utility/main)
77
[![pre-commit](https://img.shields.io/badge/pre--commit-enabled-brightgreen?logo=pre-commit)](https://github.com/pre-commit/pre-commit)
8-
[![code style: black](https://img.shields.io/badge/code_style-black-000000.svg)](https://github.com/psf/black)
9-
[![Ruff](https://img.shields.io/endpoint?url=https://raw.githubusercontent.com/astral-sh/ruff/main/assets/badge/v2.json)](https://github.com/astral-sh/ruff)
108

119
<!-- END BADGIE TIME -->
1210

src/localization_translation/convert_parse.py

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ class IndentFormat(NamedTuple):
6767

6868
def lang_to_dict(
6969
lang_data: str,
70-
) -> tuple[dict[str, Any], CommentData]:
70+
) -> tuple[dict[int | str, Any], CommentData]:
7171
"""Return data and comments from lua table."""
7272
all_tokens: list[lua_parser.Token] = list(lua_parser.tokenize_cst(lang_data))
7373
parser = lua_parser.Parser([t for t in all_tokens if not isinstance(t, lua_parser.CSTToken)])
@@ -160,11 +160,12 @@ def cartrage_return() -> None:
160160

161161
root = cast("dict[str | int, Any]", data)
162162
roots: list[dict[str | int, Any]] = []
163-
last_key: str | int | None = None
163+
last_key: str | int | float | None = None
164164

165165
in_blocks = 0
166166
has_keys = False
167167
in_unmarked = False
168+
just_did_list_entry = False
168169
unmarked_index = 1
169170
unmarked_groups: list[tuple[bool, int]] = []
170171

@@ -189,22 +190,36 @@ def cartrage_return() -> None:
189190

190191
# Handle case where it's just a list and no identifiers
191192
# recorded
192-
if all(isinstance(key, int) and not isinstance(value, dict) for key, value in root.items()):
193+
if root and all(isinstance(key, int) and not isinstance(value, dict) for key, value in root.items()):
194+
just_did_list_entry = True
193195
true_start = min(map(int, root))
194196
start = max(true_start, 1)
195197
end = max(map(int, root))
196198
# print(f'{root = }')
197199
# print(f'{start = } {end = }')
200+
unbroken_chain = True
198201
for index in range(start, end + 1):
199202
# print(f'{index = }', end=" ")
200-
value = root[index]
201-
value = f'"{value}"' if isinstance(value, str) else repr(value)
203+
value = root.get(index)
204+
if value is None:
205+
unbroken_chain = False
206+
continue
207+
if isinstance(value, str):
208+
value = f'"{value}"'
209+
elif isinstance(value, int | bool):
210+
value = repr(value).lower()
211+
else:
212+
raise ValueError(f"Unhandled key {type(value) = }")
213+
if not unbroken_chain:
214+
line += f"[{index}] = "
202215
line += value
203216
# No comma if end and does not need other recorded value like 0
204217
if index != end or start != true_start:
205218
line += ","
206-
cartrage_return()
219+
cartrage_return()
207220
# print()
221+
else:
222+
just_did_list_entry = False
208223
elif token.type_ == TokenType.EndBracket:
209224
in_blocks -= 1
210225
in_unmarked, unmarked_index = unmarked_groups.pop()
@@ -225,23 +240,26 @@ def cartrage_return() -> None:
225240
if in_blocks > 0:
226241
line += ","
227242
cartrage_return()
243+
just_did_list_entry = False
228244
elif token.type_ == TokenType.Newline:
229245
cartrage_return()
230246
elif token.type_ in {TokenType.Identifier, TokenType.Numeric}:
231247
has_keys = True
232248
last_key = token.text
233249
if token.type_ == TokenType.Numeric:
234250
line += f"[{token.text}]"
235-
last_key = int(last_key)
251+
last_key = int(last_key) if "." not in last_key else float(last_key)
236252
else:
237253
line += token.text
238254
line += " = "
239255
if comments.keys[idx + 1].type_ != TokenType.StartBracket:
256+
if last_key not in root:
257+
continue
240258
value = root[last_key]
241-
assert isinstance(value, str | int | float), "should not unpack collections"
259+
assert isinstance(value, str | int | float | bool), "should not unpack collections"
242260

243-
if isinstance(value, int | float):
244-
line += repr(value)
261+
if isinstance(value, int | float | bool):
262+
line += repr(value).lower()
245263
else:
246264
line += f'"{value}"'
247265
if comments.keys[idx + 1].type_ != TokenType.EndBracket:
@@ -251,6 +269,8 @@ def cartrage_return() -> None:
251269
line += token.text
252270
cartrage_return()
253271
elif token.type_ == TokenType.Whitespace:
272+
if just_did_list_entry:
273+
continue
254274
line += token.text
255275
##elif isinstance(token, lua_parser.Separator):
256276
## line += token.text
@@ -259,7 +279,7 @@ def cartrage_return() -> None:
259279

260280
## write_dict(comments.keys)
261281

262-
return "\n".join(lines)
282+
return "\n".join(lines) or "{}"
263283

264284

265285
def run() -> None:

src/localization_translation/extricate.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ def read_block(data: Any) -> tuple[list[str], list[str]]:
8686
intersect = set(str(key)) & (set(CHAR_TYPE) | {SEP})
8787
if intersect:
8888
raise ValueError(
89-
'Dict key contains CHAR_TYPE value(s) "' f"{''.join(intersect)}" + '"',
89+
f'Dict key contains CHAR_TYPE value(s) "{"".join(intersect)}' + '"',
9090
)
9191

9292
key = wrap_quotes(key, TYPE_CHAR[type(key).__name__])

src/localization_translation/lua_parser.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -390,7 +390,7 @@ def expect_or(self, options: Collection[str]) -> Token:
390390
token = self.next()
391391
if token.text not in options:
392392
self.fail(
393-
f"Expected {list_or([repr(x) for x in options])}, got {token.text!r} ({token.location()})",
393+
f"Expected {list_or([repr(x) for x in sorted(options)])}, got {token.text!r} ({token.location()})",
394394
)
395395
return token
396396

src/localization_translation/mineos_auto_trans.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ async def abstract_translate(
238238
if isinstance(entry, str) and entry.endswith("English.lang"):
239239
search.add(section)
240240

241-
print(f'\nSections with English.lang files: {", ".join(search)}\n')
241+
print(f"\nSections with English.lang files: {', '.join(search)}\n")
242242

243243
new_files = 1
244244

0 commit comments

Comments
 (0)