-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathmkwikitable
executable file
·135 lines (113 loc) · 3.95 KB
/
mkwikitable
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/usr/bin/env python3
import sys
import os.path
import jinja2
from argparse import ArgumentParser
from socket import AF_INET, AF_INET6, inet_pton
from filereader import get_communities_data
from apireader import get_api_dict, get_api_data
template_dict = {"ip-netze.j2": """====IPv6====
{| class="wikitable sortable"
|- bgcolor="#efefef"
! Netzwerk
! Community
! AS
! Kontakt (tech-c)
{% for network in ipv6networks %}
|-
| {{ network.network }}
{%- if network.community_url != "" %}
| [{{ network.community_url }} {{ network.community_name }}]
{%- else %}
| {{ network.community_name }}
{%- endif %}
| {{ network.asn }}
| {% for mail in network.techc -%}
{{- "[mailto:%s Mail]"|format(mail)|replace('@','--at--') -}}
{%- if not loop.last %}, {% endif -%}
{%- endfor -%}
{%- endfor %}
|- bgcolor="#efefef"
! Netzwerk
! Community
! AS
! Kontakt (tech-c)
|}
====IPv4====
{| class="wikitable sortable"
|- bgcolor="#efefef"
! Netzwerk
! Community
! AS
! Kontakt (tech-c)
{% for network in ipv4networks %}
|-
| {{ network.network }}
{%- if network.community_url != "" %}
| [{{ network.community_url }} {{ network.community_name }}]
{%- else %}
| {{ network.community_name }}
{%- endif %}
| {{ network.asn }}
| {% for mail in network.techc -%}
{{- "[mailto:%s Mail]"|format(mail)|replace('@','--at--') -}}
{%- if not loop.last %}, {% endif -%}
{%- endfor -%}
{%- endfor %}
|- bgcolor="#efefef"
! Netzwerk
! Community
! AS
! Kontakt (tech-c)
|}"""}
def get_networklist(meta_src, api_data, family):
"""
Returns a sorted list of all freifunk networks (family:ipv4/ipv6).
Each entry is a dict containing the network and
additional information about the network (asn, tech-c, community_name and community_url).
"""
all_networks = []
for community, data in get_communities_data(srcdir=meta_src, exclude=""):
try:
networks = data["networks"][family]
except (TypeError, KeyError): # NOTE: no networks configured
continue
asn = data.get("asn", "")
techc = list(data.get("tech-c", list()))
community_url = api_data.get(community, dict()).get("url", "")
community_name = api_data.get(community, dict()).get("name", community)
for network in networks:
all_networks.append({"network": network,
"community_name": community_name,
"community_url": community_url,
"asn": asn,
"techc": techc})
adr_family = AF_INET6 if family == "ipv6" else AF_INET
all_networks = sorted(all_networks, key=lambda x:
inet_pton(adr_family, x["network"].split("/")[0]))
return all_networks
def mkwikitable(meta_src):
if not os.path.isdir(meta_src):
print("Error: Couldn't find icvpn-meta repository {}.".format(meta_src), file=sys.stderr)
return
api_dict = get_api_dict()
api_data = get_api_data(api_dict)
ipv6networks = get_networklist(meta_src, api_data, family="ipv6")
ipv4networks = get_networklist(meta_src, api_data, family="ipv4")
template_loader = jinja2.DictLoader(template_dict)
template_env = jinja2.Environment(loader=template_loader)
template_name = "ip-netze.j2"
template = template_env.get_template(template_name)
context = {"ipv4networks": ipv4networks, "ipv6networks": ipv6networks}
output = template.render(context)
print(output)
if __name__ == "__main__":
PARSER = ArgumentParser(description="""Generate a mediawiki table about all ipv4/ipv6-networks.
Uses 'api_data.dump' generated by apireader.py, if available.""")
PARSER.add_argument("--meta-dir",
metavar="DIR", dest="meta_src",
default="../icvpn-meta/",
help="Path to local icvpn-meta clone. Default: "
"../icvpn-meta/")
ARGS = PARSER.parse_args()
mkwikitable(ARGS.meta_src)