Skip to content

Commit 8d3e85f

Browse files
Update tool to generate small icons table
1 parent d4fd1c1 commit 8d3e85f

File tree

1 file changed

+66
-46
lines changed

1 file changed

+66
-46
lines changed

tools/gen_networks.py

Lines changed: 66 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -18,104 +18,124 @@ def __init__(self, chain_id: int, name: str, ticker: str):
1818
self.ticker = ticker
1919

2020

21-
def get_network_glyph_name(net: Network) -> str:
22-
return "stax_chain_%u_64px" % (net.chain_id)
21+
def get_network_glyph_name(net: Network, small: bool) -> str:
22+
if small:
23+
return f"stax_app_chain_{net.chain_id}"
24+
return f"stax_chain_{net.chain_id}_64px"
2325

2426

2527
def get_header() -> str:
26-
return """\
28+
return f"""\
2729
/*
28-
* Generated by %s
30+
* Generated by {sys.argv[0]}
2931
*/
3032
31-
""" % (sys.argv[0])
33+
"""
3234

3335

34-
def gen_icons_array_inc(networks: list[Network], path: str) -> bool:
35-
with open(path + ".h", "w") as out:
36-
print(get_header() + """\
36+
def gen_icons_array_inc(networks: int, small_networks: int, path: str) -> None:
37+
with open(path + ".h", "w", encoding="utf-8") as out:
38+
print(get_header() + f"""\
3739
#ifndef NETWORK_ICONS_GENERATED_H_
3840
#define NETWORK_ICONS_GENERATED_H_
3941
4042
#include <stdint.h>
4143
#include "nbgl_types.h"
4244
43-
typedef struct {
45+
typedef struct {{
4446
uint64_t chain_id;
4547
const nbgl_icon_details_t *icon;
46-
} network_icon_t;
48+
}} network_icon_t;
4749
48-
extern const network_icon_t g_network_icons[%u];
50+
extern const network_icon_t g_network_icons[{networks}];
51+
extern const network_icon_t g_network_small_icons[{small_networks}];
4952
5053
#endif // NETWORK_ICONS_GENERATED_H_ \
51-
""" % (len(networks)), file=out)
52-
return True
54+
""", file=out)
5355

5456

55-
def gen_icons_array_src(networks: list[Network], path: str) -> bool:
56-
with open(path + ".c", "w") as out:
57-
print(get_header() + """\
57+
def gen_network_array_src(networks: list[Network], small: bool, out) -> None:
58+
59+
if small:
60+
print(f"const network_icon_t g_network_small_icons[{len(networks)}] = {{", file=out)
61+
else:
62+
print(f"const network_icon_t g_network_icons[{len(networks)}] = {{", file=out)
63+
64+
for net in networks:
65+
glyph_name = get_network_glyph_name(net, small)
66+
glyph_file = f"glyphs/{glyph_name}.gif"
67+
if os.path.isfile(glyph_file):
68+
if os.path.islink(glyph_file):
69+
glyph_name = Path(os.path.realpath(glyph_file)).stem
70+
print(f" {{.chain_id = {net.chain_id}, .icon = &C_{glyph_name}}}, // {net.name}",
71+
file=out)
72+
73+
print("};", file=out)
74+
75+
76+
def gen_icons_array_src(networks: list[Network], small_networks: list[Network], path: str) -> None:
77+
with open(path + ".c", "w", encoding="utf-8") as out:
78+
print(get_header() + f"""\
5879
#include "glyphs.h"
59-
#include "%s.h"
80+
#include "{os.path.basename(path)}.h"
81+
""", file=out)
82+
# const network_icon_t g_network_icons[{len(networks)}] = {{""", file=out)
6083

61-
const network_icon_t g_network_icons[%u] = {\
62-
""" % (os.path.basename(path), len(networks)), file=out)
84+
gen_network_array_src(networks, False, out)
85+
gen_network_array_src(small_networks, True, out)
6386

64-
for net in networks:
65-
glyph_name = get_network_glyph_name(net)
66-
glyph_file = "glyphs/%s.gif" % (glyph_name)
67-
if os.path.isfile(glyph_file):
68-
if os.path.islink(glyph_file):
69-
glyph_name = Path(os.path.realpath(glyph_file)).stem
70-
print(" "*4, end="", file=out)
71-
print("{.chain_id = %u, .icon = &C_%s}, // %s" % (net.chain_id,
72-
glyph_name,
73-
net.name),
74-
file=out)
87+
# for net in networks:
88+
# glyph_name = get_network_glyph_name(net, False)
89+
# glyph_file = f"glyphs/{glyph_name}.gif"
90+
# if os.path.isfile(glyph_file):
91+
# if os.path.islink(glyph_file):
92+
# glyph_name = Path(os.path.realpath(glyph_file)).stem
93+
# print(f" {{.chain_id = {net.chain_id}, .icon = &C_{glyph_name}}}, // {net.name}",
94+
# file=out)
7595

76-
print("};", file=out)
77-
return True
96+
# print("};", file=out)
7897

7998

80-
def gen_icons_array(networks: list[Network], path: str) -> bool:
99+
def gen_icons_array(networks: list[Network], small_networks: list[Network], path: str) -> None:
81100
path += "/net_icons.gen"
82-
if not gen_icons_array_inc(networks, path) or \
83-
not gen_icons_array_src(networks, path):
84-
return False
85-
return True
101+
gen_icons_array_inc(len(networks), len(small_networks), path)
102+
gen_icons_array_src(networks, small_networks, path)
86103

87104

88105
def network_icon_exists(net: Network) -> bool:
89-
return os.path.isfile("glyphs/%s.gif" % (get_network_glyph_name(net)))
106+
return os.path.isfile(f"glyphs/{get_network_glyph_name(net, False)}.gif")
107+
108+
109+
def network_small_icon_exists(net: Network) -> bool:
110+
return os.path.isfile(f"glyphs/{get_network_glyph_name(net, True)}.gif")
90111

91112

92113
def main(output_dir: str) -> bool:
93-
networks: list[Network] = list()
114+
networks: list[Network] = []
94115

95116
# get chain IDs and network names
96117
expr = r"{\.chain_id = ([0-9]*), \.name = \"(.*)\", \.ticker = \"(.*)\"},"
97-
with open("src/network.c") as f:
118+
with open("src/network.c", encoding="utf-8") as f:
98119
for line in f.readlines():
99120
line = line.strip()
100121
if line.startswith("{") and line.endswith("},"):
101122
m = re.search(expr,
102123
line)
103-
assert(m.lastindex == 3)
124+
assert m.lastindex == 3
104125
networks.append(Network(int(m.group(1)),
105126
m.group(2),
106127
m.group(3)))
107128

108129
networks.sort(key=lambda x: x.chain_id)
109130

110-
if not gen_icons_array(list(filter(network_icon_exists, networks)),
111-
output_dir):
112-
return False
113-
return True
131+
gen_icons_array(list(filter(network_icon_exists, networks)),
132+
list(filter(network_small_icon_exists, networks)),
133+
output_dir)
114134

115135

116136
if __name__ == "__main__":
117137
parser = argparse.ArgumentParser()
118138
parser.add_argument("OUTPUT_DIR")
119139
args = parser.parse_args()
120140
assert os.path.isdir(args.OUTPUT_DIR)
121-
quit(0 if main(args.OUTPUT_DIR) else 1)
141+
main(args.OUTPUT_DIR)

0 commit comments

Comments
 (0)