-
Notifications
You must be signed in to change notification settings - Fork 0
/
combine_ini_gateways.py
executable file
·47 lines (34 loc) · 1.14 KB
/
combine_ini_gateways.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
#!/usr/bin/env python3
import sys
import os
import configparser
pgmname = os.path.split(sys.argv[0])[-1]
if len(sys.argv) < 3:
sys.stderr.write(f"Usage: {pgmname} output_file input_file...\n")
sys.exit(2)
output_filename = sys.argv[1]
gateways = {}
for filename in sys.argv[2:]:
ini = configparser.ConfigParser()
with open(filename, "r") as f:
ini.read_file(f)
for section in ini.sections():
if section.startswith("gateway:"):
gateway = section.split(":", 1)[1]
if ini.has_option(section, "sinks"):
sinks = set(ini.get(section, "sinks").replace(",", " ").split())
else:
sinks = set()
if gateway in gateways:
sinks |= gateways[gateway]
gateways[gateway] = sinks
gateways_sorted = list(gateways.keys())
gateways_sorted.sort()
with open(output_filename, "w") as f:
for gateway in gateways_sorted:
f.write(f"[gateway:{gateway}]\n")
sinks = list(gateways[gateway])
if len(sinks) > 0:
sinks.sort()
f.write("sinks: " + ",".join(sinks) + ",\n")
f.write("\n")