Skip to content

Commit 6a819d1

Browse files
committed
Target versions directly in TokenIDE scripts
1 parent 27d7908 commit 6a819d1

File tree

2 files changed

+17
-40
lines changed

2 files changed

+17
-40
lines changed

scripts/build.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,4 +28,4 @@
2828

2929
for model in "TI-82", "TI-83", "TI-83+", "TI-84+", "TI-84+CSE", "TI-84+CE":
3030
with open(f"built/tokenide/{model}.xml", "w+", encoding="UTF-8") as outfile:
31-
outfile.write(sheet.with_tokens(version=OsVersion(model, "latest"), strict=True).to_xml_string())
31+
outfile.write(sheet.for_version(version=OsVersion(model, "latest")).to_xml_string())

scripts/tokenide.py

Lines changed: 16 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -120,76 +120,53 @@ def build_page(element: ET.Element, byte: str, dct: dict, depth: int = 0):
120120
build_page(sheet, "", self.sheet)
121121
return sheet
122122

123-
def with_tokens(self, *,
124-
version: OsVersion = None, tokens: Tokens = None, file=None,
125-
lang: str = 'en', strict: bool = False) -> 'TokenIDESheet':
123+
def for_version(self, *, version: OsVersion = None, lang: str = 'en') -> 'TokenIDESheet':
126124
"""
127-
Constructs a copy of this sheet updated with the specified token data from the token sheets
125+
Constructs a copy of this sheet tailored to a given OS version
128126
129127
If a token is entirely absent, its accessible name is used as its string value.
130128
Metadata is always preserved.
131129
132-
:param version: A minimum OS version to target (defaults to latest)
133-
:param tokens: A Tokens container of tokens to add (defaults to all tokens)
134-
:param file: A file object to read tokens from (defaults to the 8X token sheet)
130+
:param version: The OS version to target (defaults to latest)
135131
:param lang: A language code (defaults to "en")
136-
:param strict: Whether to remove tokens not present in the targeted version (default to False)
137-
:return: A TokenIDESheet containing the union of this sheet and the specified token data
132+
:return: A TokenIDESheet supporting the given version with this sheet's metadata attached
138133
"""
139134

140-
sheet = self.sheet.copy()
141-
if strict:
142-
sheet["tokens"] = {}
135+
sheet = {"meta": self.sheet["meta"].copy(), "tokens": {}}
143136

144-
if tokens is None:
145-
if file is None:
146-
with open(os.path.join(os.path.dirname(__file__), "../8X.xml"), encoding="UTF-8") as file:
147-
tokens = Tokens.from_xml_string(file.read(), version or OsVersions.LATEST)
137+
with open(os.path.join(os.path.dirname(__file__), "../8X.xml"), encoding="UTF-8") as file:
138+
tokens = Tokens.from_xml_string(file.read(), version or OsVersions.LATEST)
148139

149-
else:
150-
tokens = Tokens.from_xml_string(file.read(), version or OsVersions.LATEST)
151-
152-
all_bytes = tokens.bytes
153-
154-
all_names = [name for token in all_bytes.values()
155-
for name in [*token.langs.get(lang, "en").names(), token.langs.get(lang, "en").display]]
156-
157-
for byte, token in all_bytes.items():
140+
for byte, token in tokens.bytes.items():
158141
if version is not None and token.since > version:
159142
continue
160143

161144
leading, trailing = byte[:1], byte[1:]
162145

163-
old = self.sheet.copy()["tokens"]
164146
new = sheet["tokens"]
165-
147+
attrib = self.sheet["tokens"].copy()
166148
value = f"${leading.hex().upper()}"
149+
167150
if value not in new:
168-
new[value] = old.get(value, {"string": None, "variants": set(), "attrib": {}, "tokens": {}})
169-
if strict:
170-
new[value]["tokens"] = {}
151+
new[value] = {"string": None, "variants": set(), "attrib": {}, "tokens": {}}
171152

172153
if trailing:
173-
old = old[value]["tokens"]
154+
attrib = attrib[value]["tokens"]
174155
new = new[value]["tokens"]
175156
value = f"${trailing.hex().upper()}"
176157

177-
new[value] = old.get(value, {"string": None, "variants": set(), "attrib": {}, "tokens": {}})
158+
new[value] = {"string": None, "variants": set(), "attrib": {}, "tokens": {}}
178159

179160
translation = token.langs.get(lang, "en")
180161
display = translation.display
181162

182163
if new[value]["string"] not in [*translation.names(), display]:
183164
new[value]["string"] = translation.accessible
184165

185-
new[value]["variants"] |= {name for name in translation.names() if all_names.count(name) == 1}
186-
187-
string = new[value]["string"]
188-
if string not in display and display not in string and all_names.count(display) == 1:
189-
new[value]["variants"].add(display)
190-
191-
new[value]["variants"] -= {string}
166+
new[value]["variants"] |= {*translation.names()}
167+
new[value]["variants"] -= {new[value]["string"]}
192168

169+
new[value]["attrib"] |= attrib.get(value, {}).get("attrib", {})
193170
if byte in TokenIDESheet.STARTERS:
194171
new[value]["attrib"]["stringStarter"] = "true"
195172

0 commit comments

Comments
 (0)