-
Notifications
You must be signed in to change notification settings - Fork 3
/
util.py
113 lines (87 loc) · 2.62 KB
/
util.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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
from rich.console import Console
from typing import List, Dict
from rich.table import Table
from functools import wraps
from config import CHAIN
import anyio
import json
import os
import csv
import re
console = Console()
def run_async(func):
@wraps(func)
def wrapper(*args, **kwargs):
async def coro_wrapper():
return await func(*args, **kwargs)
return anyio.run(coro_wrapper)
return wrapper
def get_chain_by_name(chain_name:str = None):
if not chain_name:
raise Exception("No chain name provided")
for chain in CHAIN:
if chain.SLUG.lower() == chain_name.lower():
return chain
return None
def mkdir(path=None):
if not os.path.exists(path):
os.makedirs(path)
return path
def write_csv(data, filename):
with open(filename, "w") as f:
if isinstance(data, list):
data = [
{k: str(v).strip("'").replace("::jsonb", "") for k, v in row.items()}
for row in data
]
writer = csv.DictWriter(f, fieldnames=data[0].keys())
writer.writeheader()
writer.writerows(data)
def load_csv(filename):
# open csv file and convert it to json with headers as keys
with open(filename, "r") as f:
reader = csv.DictReader(f)
# convert keys to snake case lower case, make sure key is not empty
return [
{snake_case(k): v for k, v in row.items() if k and "@" not in k}
for row in reader
]
def is_valid_address(address: str):
return re.match(r"^0x[a-fA-F0-9]{40}$", address)
def display_table(
data: List[Dict] = None,
header=None,
border=False,
return_table=False,
expand_table=False,
):
if not data:
raise Exception("No data to display")
table = Table(
show_header=True,
header_style="bold cyan",
title=header,
show_lines=border,
expand=expand_table,
)
for key in data[0].keys():
table.add_column(key)
for row in data:
# convert to string
table.add_row(*[str(v) for v in row.values()])
if return_table:
return table
else:
console.print(table)
def export_file(data: List[Dict] = None, filename: str = None):
if not data:
raise Exception("No data to display")
if not filename:
raise Exception("No filename provided")
if filename.endswith(".csv"):
write_csv(data, filename)
elif filename.endswith(".json"):
with open(filename, "w") as f:
json.dump(data, f)
else:
raise Exception("Invalid file type, must end in .json or .csv")