From 0abfb6cd9183c36ede1b52f43ab5c79b6eac88dd Mon Sep 17 00:00:00 2001 From: matejcik Date: Fri, 24 Aug 2018 15:20:25 +0200 Subject: [PATCH] coin_info: tweak API for less repetition --- tools/coin_info.py | 34 +++++++++++++++++++++++++--------- tools/coins_details.py | 6 +++--- tools/cointool.py | 9 ++++----- 3 files changed, 32 insertions(+), 17 deletions(-) diff --git a/tools/coin_info.py b/tools/coin_info.py index f6f2cf43a9..db5713b21e 100755 --- a/tools/coin_info.py +++ b/tools/coin_info.py @@ -278,7 +278,7 @@ def latest_releases(): latest = {} for v in ("1", "2"): releases = requests.get(RELEASES_URL.format(v)).json() - latest[v] = max(tuple(r["version"]) for r in releases) + latest["trezor" + v] = max(tuple(r["version"]) for r in releases) return latest @@ -421,8 +421,6 @@ def mark_duplicate_shortcuts(coins): # they *still* keep duplicate status (and possibly are deleted). continue - nontokens = [coin for coin in values if not is_token(coin)] - for coin in values: # allow overrides to skip this; if not listed in overrides, assume True is_dup = overrides.get(coin["key"], True) @@ -441,7 +439,7 @@ def _btc_sort_key(coin): return coin["name"] -def get_all(deduplicate=True): +def collect_coin_info(): """Returns all definition as dict organized by coin type. `coins` for btc-like coins, `eth` for ethereum networks, @@ -473,17 +471,35 @@ def get_all(deduplicate=True): _ensure_mandatory_values(coins) - if deduplicate: - mark_duplicate_shortcuts(all_coins.as_list()) - all_coins["erc20"] = [ - coin for coin in all_coins["erc20"] if not coin.get("duplicate") - ] + return all_coins + +def coin_info_with_duplicates(): + """Collects coin info, detects duplicates but does not remove them. + + Returns the CoinsInfo object and duplicate buckets. + """ + all_coins = collect_coin_info() + buckets = mark_duplicate_shortcuts(all_coins.as_list()) + return all_coins, buckets + + +def coin_info(): + """Collects coin info, marks and prunes duplicate ERC20 symbols, fills out support + info and returns the result. + """ + all_coins, _ = coin_info_with_duplicates() + all_coins["erc20"] = [ + coin for coin in all_coins["erc20"] if not coin.get("duplicate") + ] return all_coins def search(coins, keyword): kwl = keyword.lower() + if isinstance(coins, CoinsInfo): + coins = coins.as_list() + for coin in coins: key = coin["key"].lower() name = coin["name"].lower() diff --git a/tools/coins_details.py b/tools/coins_details.py index 8822fa1ec0..711bbb2785 100755 --- a/tools/coins_details.py +++ b/tools/coins_details.py @@ -153,8 +153,8 @@ def update_simple(coins, support_info, type): name=coin["name"], shortcut=coin["shortcut"], type=type, - t1_enabled=_is_supported(support, 1), - t2_enabled=_is_supported(support, 2), + t1_enabled=_is_supported(support, "trezor1"), + t2_enabled=_is_supported(support, "trezor2"), ) for k in OPTIONAL_KEYS: if k in coin: @@ -294,7 +294,7 @@ if __name__ == "__main__": handler.setLevel(logging.DEBUG) root.addHandler(handler) - defs = coin_info.get_all() + defs = coin_info.coin_info() support_info = coin_info.support_info(defs) coins = {} diff --git a/tools/cointool.py b/tools/cointool.py index 6d96836fc6..a3e4b4df16 100755 --- a/tools/cointool.py +++ b/tools/cointool.py @@ -498,8 +498,7 @@ def check(backend, icons, show_duplicates): if icons and not CAN_BUILD_DEFS: raise click.ClickException("Missing requirements for icon check") - defs = coin_info.get_all(deduplicate=False) - buckets = coin_info.mark_duplicate_shortcuts(defs.as_list()) + defs, buckets = coin_info.coin_info_with_duplicates() all_checks_passed = True print("Checking BTC-like coins...") @@ -544,7 +543,7 @@ def check(backend, icons, show_duplicates): @click.option("-o", "--outfile", type=click.File(mode="w"), default="./coins.json") def coins_json(outfile): """Generate coins.json for consumption in python-trezor and Connect/Wallet""" - coins = coin_info.get_all().coins + coins = coin_info.coin_info().coins support_info = coin_info.support_info(coins) by_name = {} for coin in coins: @@ -564,7 +563,7 @@ def coindefs(outfile): This is currently unused but should enable us to add new coins without having to update firmware. """ - coins = coin_info.get_all().coins + coins = coin_info.coin_info().coins coindefs = {} for coin in coins: key = coin["key"] @@ -603,7 +602,7 @@ def render(paths, outfile, verbose): raise click.ClickException("Option -o can only be used with single input file") # prepare defs - defs = coin_info.get_all() + defs = coin_info.coin_info() support_info = coin_info.support_info(defs) # munch dicts - make them attribute-accessible