Use type narrowing for extract-strings and transform-string

Fixes mypy errors in latest version
This commit is contained in:
Damien Elmes 2023-03-31 14:02:12 +10:00
parent 1f347d09d5
commit 066db4bae8
2 changed files with 9 additions and 8 deletions

View File

@ -11,7 +11,7 @@ import json
import os
from fluent.syntax import parse
from fluent.syntax.ast import Junk
from fluent.syntax.ast import Junk, Message
from fluent.syntax.serializer import serialize_element
root = ".."
@ -23,7 +23,7 @@ for path in ftl_files:
for ent in obj.body:
if isinstance(ent, Junk):
raise Exception(f"file had junk! {path} {ent}")
if getattr(ent, "id", None):
if isinstance(ent, Message):
key = ent.id.name
val = "".join(serialize_element(elem) for elem in ent.value.elements)
if val in keys_by_value:

View File

@ -10,7 +10,7 @@ import glob
import os
from fluent.syntax import parse, serialize
from fluent.syntax.ast import Junk
from fluent.syntax.ast import Junk, Message, TextElement
template_root = ".."
template_files = glob.glob(
@ -32,15 +32,16 @@ def transform_string_in_file(path):
for ent in obj.body:
if isinstance(ent, Junk):
raise Exception(f"file had junk! {path} {ent}")
if getattr(ent, "id", None):
if isinstance(ent, Message):
key = ent.id.name
for target_key, src, dst in target_repls:
if key == target_key:
for elem in ent.value.elements:
newval = elem.value.replace(src, dst)
if newval != elem.value:
elem.value = newval
changed = True
if isinstance(elem, TextElement):
newval = elem.value.replace(src, dst)
if newval != elem.value:
elem.value = newval
changed = True
if changed:
open(path, "w", encoding="utf8").write(serialize(obj))