Skip to content

Commit 49858e7

Browse files
committed
feat: make to_xml support tag and locale whitelist
1 parent 1f0d691 commit 49858e7

1 file changed

Lines changed: 21 additions & 2 deletions

File tree

hearthstone/cardxml.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import tempfile
2-
from typing import Any, Callable, Iterator, Tuple
2+
from typing import Any, Callable, Iterator, Optional, Sequence, Tuple
33

44
from .enums import (
55
CardClass, CardSet, CardType, Faction, GameTag,
@@ -131,29 +131,45 @@ def __str__(self):
131131
def __repr__(self):
132132
return "<%s: %r>" % (self.id, self.name)
133133

134-
def to_xml(self):
134+
def to_xml(
135+
self,
136+
tags: Optional[Sequence[GameTag]] = None,
137+
locales: Optional[Sequence[str]] = None
138+
):
135139
ret = ElementTree.Element("Entity", CardID=self.id, ID=str(self.dbf_id))
136140
if self.version:
137141
ret.attrib["version"] = str(self.version)
138142

139143
for tag in LOCALIZED_TAGS:
144+
if tags is not None and tag not in tags:
145+
continue
146+
140147
value = self.strings[tag]
141148
if value:
142149
e = ElementTree.SubElement(ret, "Tag", enumID=str(int(tag)), name=tag.name)
143150
e.attrib["type"] = "LocString"
144151
for locale, localized_value in sorted(value.items()):
152+
if locales is not None and locale not in locales:
153+
continue
154+
145155
if localized_value:
146156
loc_element = ElementTree.SubElement(e, locale)
147157
loc_element.text = str(localized_value)
148158

149159
for tag in STRING_TAGS:
160+
if tags is not None and tag not in tags:
161+
continue
162+
150163
value = self.strings[tag]
151164
if value:
152165
e = ElementTree.SubElement(ret, "Tag", enumID=str(int(tag)), name=tag.name)
153166
e.attrib["type"] = "String"
154167
e.text = value
155168

156169
for tag, value in sorted(self.tags.items()):
170+
if tags is not None and tag not in tags:
171+
continue
172+
157173
if value:
158174
e = _make_tag_element(ret, "Tag", tag, value)
159175

@@ -162,6 +178,9 @@ def to_xml(self):
162178
e.attrib["cardID"] = self.hero_power
163179

164180
for tag, value in sorted(self.referenced_tags.items()):
181+
if tags and tag not in tags:
182+
continue
183+
165184
e = _make_tag_element(ret, "ReferencedTag", tag, value)
166185

167186
return ret

0 commit comments

Comments
 (0)