forked from HearthSim/python-hearthstone
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstringsfile.py
75 lines (53 loc) · 1.76 KB
/
stringsfile.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
"""
Hearthstone Strings file
File format: TSV. Lines starting with `#` are ignored.
Key is always `TAG`
"""
import csv
import json
import sys
import tempfile
from typing import Dict, Optional, Tuple
from hearthstone.xmlutils import download_to_tempfile_retry
StringsRow = Dict[str, str]
StringsDict = Dict[str, StringsRow]
_cache: Dict[Tuple[str, str], StringsDict] = {}
def load_json(fp) -> StringsDict:
hsjson_strings = json.loads(fp.read())
return {k: {"TEXT": v} for k, v in hsjson_strings.items()}
def load_txt(fp) -> StringsDict:
reader = csv.DictReader(
filter(lambda row: row.strip() and not row.startswith("#"), fp),
delimiter="\t"
)
stripped_rows = [{k: v for k, v in row.items() if k and v} for row in reader]
return {
stripped_row.pop("TAG"): stripped_row for stripped_row in stripped_rows
if stripped_row
}
def _load_globalstrings_from_web(locale="enUS") -> Optional[StringsDict]:
with tempfile.TemporaryFile() as fp:
json_url = "https://api.hearthstonejson.com/v1/strings/%s/GLOBAL.json" % locale
if download_to_tempfile_retry(json_url, fp):
fp.flush()
fp.seek(0)
return load_json(fp)
else:
return None
def _load_globalstrings_from_library(locale="enUS") -> StringsDict:
from hearthstone_data import get_strings_file
path: str = get_strings_file(locale, filename="GLOBAL.txt")
with open(path, "r", encoding="utf-8-sig") as f:
return load_txt(f)
def load_globalstrings(locale="enUS") -> StringsDict:
key = (locale, "GLOBAL.txt")
if key not in _cache:
sd = _load_globalstrings_from_web(locale=locale)
if not sd:
sd = _load_globalstrings_from_library(locale=locale)
_cache[key] = sd
return _cache[key]
if __name__ == "__main__":
for path in sys.argv[1:]:
with open(path, "r") as f:
print(json.dumps(load_txt(f)))