@@ -121,7 +121,8 @@ def build_page(element: ET.Element, byte: str, dct: dict):
121
121
return sheet
122
122
123
123
def with_tokens (self , * ,
124
- version : OsVersion = None , tokens : Tokens = None , file = None , lang : str = 'en' ) -> 'TokenIDESheet' :
124
+ version : OsVersion = None , tokens : Tokens = None , file = None ,
125
+ lang : str = 'en' , strict : bool = False ) -> 'TokenIDESheet' :
125
126
"""
126
127
Constructs a copy of this sheet updated with the specified token data from the token sheets
127
128
@@ -132,10 +133,13 @@ def with_tokens(self, *,
132
133
:param tokens: A Tokens container of tokens to add (defaults to all tokens)
133
134
:param file: A file object to read tokens from (defaults to the 8X token sheet)
134
135
:param lang: A language code (defaults to "en")
136
+ :param strict: Whether to remove tokens not present in the targeted version (default to False)
135
137
:return: A TokenIDESheet containing the union of this sheet and the specified token data
136
138
"""
137
139
138
140
sheet = self .sheet .copy ()
141
+ if strict :
142
+ sheet ["tokens" ] = {}
139
143
140
144
if tokens is None :
141
145
if file is None :
@@ -156,38 +160,41 @@ def with_tokens(self, *,
156
160
157
161
leading , trailing = byte [:1 ], byte [1 :]
158
162
159
- dct = sheet ["tokens" ]
160
- value = f"$ { leading . hex (). upper () } "
163
+ old = self . sheet . copy () ["tokens" ]
164
+ new = sheet [ "tokens" ]
161
165
162
- if value not in dct :
163
- dct [value ] = {"string" : None , "variants" : set (), "attrib" : {}, "tokens" : {}}
166
+ value = f"${ leading .hex ().upper ()} "
167
+ if value not in new :
168
+ new [value ] = old .get (value , {"string" : None , "variants" : set (), "attrib" : {}, "tokens" : {}})
169
+ if strict :
170
+ new [value ]["tokens" ] = {}
164
171
165
172
if trailing :
166
- dct = dct [value ]["tokens" ]
173
+ old = old [value ]["tokens" ]
174
+ new = new [value ]["tokens" ]
167
175
value = f"${ trailing .hex ().upper ()} "
168
176
169
- if value not in dct :
170
- dct [value ] = {"string" : None , "variants" : set (), "attrib" : {}, "tokens" : {}}
177
+ new [value ] = old .get (value , {"string" : None , "variants" : set (), "attrib" : {}, "tokens" : {}})
171
178
172
179
translation = token .langs .get (lang , "en" )
173
180
display = translation .display
174
181
175
- if dct [value ]["string" ] not in [* translation .names (), display ]:
176
- dct [value ]["string" ] = translation .accessible
182
+ if new [value ]["string" ] not in [* translation .names (), display ]:
183
+ new [value ]["string" ] = translation .accessible
177
184
178
- dct [value ]["variants" ] |= {name for name in translation .names () if all_names .count (name ) == 1 }
185
+ new [value ]["variants" ] |= {name for name in translation .names () if all_names .count (name ) == 1 }
179
186
180
- string = dct [value ]["string" ]
187
+ string = new [value ]["string" ]
181
188
if string not in display and display not in string and all_names .count (display ) == 1 :
182
- dct [value ]["variants" ].add (display )
189
+ new [value ]["variants" ].add (display )
183
190
184
- dct [value ]["variants" ] -= {string }
191
+ new [value ]["variants" ] -= {string }
185
192
186
193
if byte in TokenIDESheet .STARTERS :
187
- dct [value ]["attrib" ]["stringStarter" ] = "true"
194
+ new [value ]["attrib" ]["stringStarter" ] = "true"
188
195
189
196
if byte in TokenIDESheet .TERMINATORS :
190
- dct [value ]["attrib" ]["stringTerminator" ] = "true"
197
+ new [value ]["attrib" ]["stringTerminator" ] = "true"
191
198
192
199
return TokenIDESheet (sheet )
193
200
0 commit comments