mirror of
https://github.com/trezor/trezor-firmware.git
synced 2025-02-01 10:20:59 +00:00
feat(common/tools): use pathlib in cointool
This commit is contained in:
parent
ac65867771
commit
e72b6abf47
@ -1,10 +1,10 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
import glob
|
|
||||||
import json
|
import json
|
||||||
import logging
|
import logging
|
||||||
import os
|
import os
|
||||||
import re
|
import re
|
||||||
from collections import OrderedDict, defaultdict
|
from collections import OrderedDict, defaultdict
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
try:
|
try:
|
||||||
import requests
|
import requests
|
||||||
@ -13,20 +13,22 @@ except ImportError:
|
|||||||
|
|
||||||
log = logging.getLogger(__name__)
|
log = logging.getLogger(__name__)
|
||||||
|
|
||||||
DEFS_DIR = os.path.abspath(
|
ROOT = (Path(__file__).parent / "..").resolve()
|
||||||
os.environ.get("DEFS_DIR") or os.path.join(os.path.dirname(__file__), "..", "defs")
|
|
||||||
)
|
if os.environ.get("DEFS_DIR"):
|
||||||
|
DEFS_DIR = Path(os.environ.get("DEFS_DIR")).resolve()
|
||||||
|
else:
|
||||||
|
DEFS_DIR = ROOT / "defs"
|
||||||
|
|
||||||
|
|
||||||
def load_json(*path):
|
def load_json(*path):
|
||||||
"""Convenience function to load a JSON file from DEFS_DIR."""
|
"""Convenience function to load a JSON file from DEFS_DIR."""
|
||||||
if len(path) == 1 and path[0].startswith("/"):
|
if len(path) == 1 and isinstance(path[0], Path):
|
||||||
filename = path[0]
|
file = path[0]
|
||||||
else:
|
else:
|
||||||
filename = os.path.join(DEFS_DIR, *path)
|
file = Path(DEFS_DIR, *path)
|
||||||
|
|
||||||
with open(filename) as f:
|
return json.loads(file.read_text(), object_pairs_hook=OrderedDict)
|
||||||
return json.load(f, object_pairs_hook=OrderedDict)
|
|
||||||
|
|
||||||
|
|
||||||
# ====== CoinsInfo ======
|
# ====== CoinsInfo ======
|
||||||
@ -206,13 +208,13 @@ def validate_btc(coin):
|
|||||||
def _load_btc_coins():
|
def _load_btc_coins():
|
||||||
"""Load btc-like coins from `bitcoin/*.json`"""
|
"""Load btc-like coins from `bitcoin/*.json`"""
|
||||||
coins = []
|
coins = []
|
||||||
for filename in glob.glob(os.path.join(DEFS_DIR, "bitcoin", "*.json")):
|
for file in DEFS_DIR.glob("bitcoin/*.json"):
|
||||||
coin = load_json(filename)
|
coin = load_json(file)
|
||||||
coin.update(
|
coin.update(
|
||||||
name=coin["coin_label"],
|
name=coin["coin_label"],
|
||||||
shortcut=coin["coin_shortcut"],
|
shortcut=coin["coin_shortcut"],
|
||||||
key="bitcoin:{}".format(coin["coin_shortcut"]),
|
key="bitcoin:{}".format(coin["coin_shortcut"]),
|
||||||
icon=filename.replace(".json", ".png"),
|
icon=str(file.with_suffix(".png")),
|
||||||
)
|
)
|
||||||
coins.append(coin)
|
coins.append(coin)
|
||||||
|
|
||||||
@ -234,9 +236,9 @@ def _load_erc20_tokens():
|
|||||||
for network in networks:
|
for network in networks:
|
||||||
chain = network["chain"]
|
chain = network["chain"]
|
||||||
|
|
||||||
chain_path = os.path.join(DEFS_DIR, "ethereum", "tokens", "tokens", chain)
|
chain_path = DEFS_DIR / "ethereum" / "tokens" / "tokens" / chain
|
||||||
for filename in sorted(glob.glob(os.path.join(chain_path, "*.json"))):
|
for file in sorted(chain_path.glob("*.json")):
|
||||||
token = load_json(filename)
|
token = load_json(file)
|
||||||
token.update(
|
token.update(
|
||||||
chain=chain,
|
chain=chain,
|
||||||
chain_id=network["chain_id"],
|
chain_id=network["chain_id"],
|
||||||
@ -251,7 +253,7 @@ def _load_erc20_tokens():
|
|||||||
|
|
||||||
def _load_nem_mosaics():
|
def _load_nem_mosaics():
|
||||||
"""Loads NEM mosaics from `nem/nem_mosaics.json`"""
|
"""Loads NEM mosaics from `nem/nem_mosaics.json`"""
|
||||||
mosaics = load_json("nem", "nem_mosaics.json")
|
mosaics = load_json("nem/nem_mosaics.json")
|
||||||
for mosaic in mosaics:
|
for mosaic in mosaics:
|
||||||
shortcut = mosaic["ticker"].strip()
|
shortcut = mosaic["ticker"].strip()
|
||||||
mosaic.update(shortcut=shortcut, key="nem:{}".format(shortcut))
|
mosaic.update(shortcut=shortcut, key="nem:{}".format(shortcut))
|
||||||
@ -269,17 +271,19 @@ def _load_misc():
|
|||||||
def _load_fido_apps():
|
def _load_fido_apps():
|
||||||
"""Load FIDO apps from `fido/*.json`"""
|
"""Load FIDO apps from `fido/*.json`"""
|
||||||
apps = []
|
apps = []
|
||||||
for filename in sorted(glob.glob(os.path.join(DEFS_DIR, "fido", "*.json"))):
|
for file in sorted(DEFS_DIR.glob("fido/*.json")):
|
||||||
app_name = os.path.basename(filename)[:-5].lower()
|
app_name = file.stem.lower()
|
||||||
app = load_json(filename)
|
app = load_json(file)
|
||||||
app.setdefault("use_sign_count", None)
|
app.setdefault("use_sign_count", None)
|
||||||
app.setdefault("use_self_attestation", None)
|
app.setdefault("use_self_attestation", None)
|
||||||
app.setdefault("u2f", [])
|
app.setdefault("u2f", [])
|
||||||
app.setdefault("webauthn", [])
|
app.setdefault("webauthn", [])
|
||||||
|
|
||||||
icon_path = os.path.join(DEFS_DIR, "fido", app_name + ".png")
|
icon_file = file.with_suffix(".png")
|
||||||
if not os.path.exists(icon_path):
|
if not icon_file.exists():
|
||||||
icon_path = None
|
icon_path = None
|
||||||
|
else:
|
||||||
|
icon_path = str(icon_file)
|
||||||
|
|
||||||
app.update(key=app_name, icon=icon_path)
|
app.update(key=app_name, icon=icon_path)
|
||||||
apps.append(app)
|
apps.append(app)
|
||||||
@ -530,7 +534,6 @@ def fill_blockchain_links(all_coins):
|
|||||||
coin["blockbook"] = []
|
coin["blockbook"] = []
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def _btc_sort_key(coin):
|
def _btc_sort_key(coin):
|
||||||
if coin["name"] in ("Bitcoin", "Testnet", "Regtest"):
|
if coin["name"] in ("Bitcoin", "Testnet", "Regtest"):
|
||||||
return "000000" + coin["name"]
|
return "000000" + coin["name"]
|
||||||
|
Loading…
Reference in New Issue
Block a user