env/utils/openbsd/pf-martians/iana-ip-special-registry-parser.py

39 lines
835 B
Python

#!/usr/bin/env python3
import csv
import re
import sys
REGEX_FOOTNOTES = re.compile(r" \[\d+\]")
REGEX_WHITESPACES = re.compile(r"\s+")
def parse_iana_csv(csvfile):
reader = csv.DictReader(csvfile)
for row in reader:
fmt = ""
reachable = row["Globally Reachable"]
if reachable == "":
continue
if "False" in reachable:
fmt = "{}"
elif "True" in reachable:
fmt = "!{}"
elif "N/A" in reachable:
fmt = "#{}"
else:
fmt = "#[" + reachable + "]: {}"
blocks = row["Address Block"]
blocks = REGEX_FOOTNOTES.sub("", blocks)
blocks = REGEX_WHITESPACES.sub("", blocks)
for block in blocks.split(","):
print(fmt.format(block))
if __name__ == "__main__":
def usage():
print("Usage: {}".format(sys.argv[1]), file=sys.stderr)
sys.exit(1)
if len(sys.argv) != 1:
usage()
parse_iana_csv(sys.stdin)