import sqlite3
from . import downloader
+from . import util
FILENAME_ASNUMS = "asnums.txt"
FILENMAE_NETWORKS = "networks.txt"
f.write("# Generated at %s\n" % self.start_time)
f.write("#\n\n")
- def _split_line(self, line):
- key, colon, val = line.partition(":")
-
- # Strip any excess space
- key = key.strip()
- val = val.strip()
-
- return key, val
-
def fetch_data(self):
if not self.rir.database_urls:
raise NotImplementedError("Database URLs not set")
logging.debug(line)
# Split line
- key, val = self._split_line(line)
+ key, val = util.split_line(line)
if key == "inetnum":
start_address, delim, end_address = val.partition("-")
logging.debug(line)
# Split line
- key, val = self._split_line(line)
+ key, val = util.split_line(line)
# Keep any significant data
if key in ("route6", "route"):
logging.debug(line)
# Split line
- key, val = self._split_line(line)
+ key, val = util.split_line(line)
if key == "aut-num":
m = RE_AS.match(val)
logging.debug(line)
# Split line
- key, val = self._split_line(line)
+ key, val = util.split_line(line)
if key in ("organisation", "org-name", "country"):
org[key] = val
import logging
import urllib.request
+from . import util
+
# Setup logger
log = logging.getLogger("downloader")
log.propagate = 1
"""
Makes the object iterable by going through each block
"""
- block = []
-
- for line in self.body:
- # Convert to string
- for charset in ("utf-8", "latin1"):
- try:
- line = line.decode(charset)
- except UnicodeDecodeError:
- continue
- else:
- break
-
- # Strip line-endings
- line = line.rstrip()
-
- # Skip commented lines
- if line.startswith("#"):
- continue
-
- if line:
- block.append(line)
- continue
-
- # End the block on an empty line
- if block:
- yield block
-
- # Reset the block
- block = []
+ return util.iterate_over_blocks(self.body)
@property
def headers(self):
--- /dev/null
+#!/usr/bin/python3
+###############################################################################
+# #
+# location-database - A database to determine someone's #
+# location on the Internet #
+# Copyright (C) 2018 Michael Tremer #
+# #
+# This program is free software: you can redistribute it and/or modify #
+# it under the terms of the GNU General Public License as published by #
+# the Free Software Foundation, either version 3 of the License, or #
+# (at your option) any later version. #
+# #
+# This program is distributed in the hope that it will be useful, #
+# but WITHOUT ANY WARRANTY; without even the implied warranty of #
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the #
+# GNU General Public License for more details. #
+# #
+# You should have received a copy of the GNU General Public License #
+# along with this program. If not, see <http://www.gnu.org/licenses/>. #
+# #
+###############################################################################
+
+def iterate_over_blocks(f, charsets=("utf-8", "latin1")):
+ block = []
+
+ for line in f:
+ # Convert to string
+ for charset in charsets:
+ try:
+ line = line.decode(charset)
+ except UnicodeDecodeError:
+ continue
+ else:
+ break
+
+ # Strip line-endings
+ line = line.rstrip()
+
+ # Skip commented lines
+ if line.startswith("#"):
+ continue
+
+ if line:
+ block.append(line)
+ continue
+
+ # End the block on an empty line
+ if block:
+ yield block
+
+ # Reset the block
+ block = []
+
+def split_line(line):
+ key, colon, val = line.partition(":")
+
+ # Strip any excess space
+ key = key.strip()
+ val = val.strip()
+
+ return key, val