old_contents: """
  Hearthstone Strings file
  
  File format: TSV. Lines starting with `#` are ignored.
  Key is always `TAG`
  """
  import csv
  from typing import Dict
  
  import hearthstone_data
  
  
  StringsRow = Dict[str, str]
  StringsDict = Dict[str, StringsRow]
  
  _cache: Dict[str, StringsDict] = {}
  
  
  def load(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(locale="enUS") -> StringsDict:
  	path: str = hearthstone_data.get_strings_file(locale, filename="GLOBAL.txt")
  	if path not in _cache:
  		with open(path, "r", encoding="utf-8-sig") as f:
  			_cache[path] = load(f)
  
  	return _cache[path]
  
  
  if __name__ == "__main__":
  	import json
  	import sys
  
  	for path in sys.argv[1:]:
  		with open(path, "r") as f:
  			print(json.dumps(load(f)))
  
new_contents: """
  Hearthstone Strings file
  
  File format: TSV. Lines starting with `#` are ignored.
  Key is always `TAG`
  """
  import csv
  from typing import Dict, Optional, Tuple
  
  import hearthstone_data
  import requests
  
  
  StringsRow = Dict[str, str]
  StringsDict = Dict[str, StringsRow]
  
  _cache: Dict[Tuple[str, str], StringsDict] = {}
  
  
  def load_json(fp) -> StringsDict:
  	hsjson_strings = json.loads(fp)
  	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]:
  	response = requests.get(
  		"https://api.hearthstonejson.com/v1/strings/%s/GLOBAL.json" % locale,
  		stream=True
  	)
  
  	try:
  		if response.ok:
  			response.raw.decode_content = True
  			return load_json(response.raw)
  		else:
  			return None
  	except requests.exceptions.RequestException:
  		return None
  
  
  def _load_globalstrings_from_library(locale="enUS") -> StringsDict:
  	path: str = hearthstone_data.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()
  
  		if not sd:
  			sd = _load_globalstrings_from_library(locale=locale)
  
  		_cache[key] = sd
  
  	return _cache[key]
  
  
  if __name__ == "__main__":
  	import json
  	import sys
  
  	for path in sys.argv[1:]:
  		with open(path, "r") as f:
  			print(json.dumps(load_txt(f)))
  
subject: Support optionally boostrapping strings from api.hearthstonejson.com