Skip to content

Commit

Permalink
gen-old-pools: arg parser, allow specifying dir
Browse files Browse the repository at this point in the history
  • Loading branch information
0xB10C committed Mar 6, 2023
1 parent a7668b2 commit 7a50157
Showing 1 changed file with 41 additions and 32 deletions.
73 changes: 41 additions & 32 deletions contrib/generate-old-pools-json.py
Original file line number Diff line number Diff line change
@@ -1,38 +1,47 @@
#!/usr/bin/env python3

import argparse
import json
import glob
import sys

output_file_name = "pools.json"

if len(sys.argv) == 2:
print(f"Using {sys.argv[1]} as output file name.")
output_file_name = sys.argv[1]

entity_files = glob.glob("pools/*.json")

addresses = dict()
tags = dict()

for file_path in entity_files:
with open(file_path, "r") as f:
e = json.load(f)
name = e["name"]
link = e["link"]
for addr in e["addresses"]:
addresses[addr] = { "name": name, "link": link }
for tag in e["tags"]:
tags[tag] = { "name": name, "link": link }

# sort dicts to be at least somewhat deterministic
addresses = dict(sorted(addresses.items()))
tags = dict(sorted(tags.items()))

with open(output_file_name, "w") as out:
content = {
"payout_addresses": addresses,
"coinbase_tags": tags
}
json.dump(content, out, indent = 2, ensure_ascii=False)

DEFAULT_ENTITIES_DIR = "pools/"
DEFAULT_OUTPUT = "pools.json"

def main(args):
parser = argparse.ArgumentParser()
parser.add_argument("output", default=DEFAULT_OUTPUT, type=str, help="output file name", nargs="?")
parser.add_argument("entities", default=DEFAULT_ENTITIES_DIR, type=str, help="entities directory", nargs="?")
args = parser.parse_args(args)

if args.output != DEFAULT_OUTPUT:
print(f"Using {args.output} as output file name.")

entity_files = glob.glob(args.entities + "/*.json")

addresses = dict()
tags = dict()

for file_path in entity_files:
with open(file_path, "r") as f:
e = json.load(f)
name = e["name"]
link = e["link"]
for addr in e["addresses"]:
addresses[addr] = { "name": name, "link": link }
for tag in e["tags"]:
tags[tag] = { "name": name, "link": link }

# sort dicts to be at least somewhat deterministic
addresses = dict(sorted(addresses.items()))
tags = dict(sorted(tags.items()))

with open(args.output, "w") as out:
content = {
"payout_addresses": addresses,
"coinbase_tags": tags
}
json.dump(content, out, indent = 2, ensure_ascii=False)

if __name__ == "__main__":
main(sys.argv[1:])

0 comments on commit 7a50157

Please sign in to comment.