From: Michael Tremer Date: Tue, 15 Oct 2019 14:54:55 +0000 (+0000) Subject: Import X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=1ada224d61dfd76f566ed62287eb87e344874f2f;p=location%2Flocation-database.git Import Signed-off-by: Michael Tremer --- diff --git a/compile-database b/compile-database index 01c12e8..6dc82d2 100755 --- a/compile-database +++ b/compile-database @@ -31,6 +31,11 @@ if not len(sys.argv) > 1: path = sys.argv[1] database = tools.Database(vendor="IPFire Project") + +# Import countries +database.import_countries("countries.txt") + +# Import all RIRs for RIR in tools.RIRS: rir = RIR() diff --git a/tools/database.py b/tools/database.py index 8ad61c5..823faaf 100644 --- a/tools/database.py +++ b/tools/database.py @@ -20,6 +20,7 @@ # # ############################################################################### +import iso3166 import logging import re @@ -108,3 +109,26 @@ class Database(object): n.asn = asn logging.debug("Added new network: %s" % n) + + def import_countries(self, filename): + with open(filename) as f: + for line in f: + if line.startswith("#"): + continue + + try: + country_code, continent_code = line.split() + except: + logging.error("Could not parse line in %s: %s" (filename, line)) + raise + + c = self.writer.add_country(country_code) + + # Add continent code + if continent_code: + c.continent_code = continent_code + + # Add name + country = iso3166.countries_by_alpha2.get(country_code) + if country: + c.name = country.apolitical_name