]> gitweb.michael.orlitzky.com - djbdns-logparse.git/blobdiff - djbdns/dnscache.py
djbdns/{dnscache,tinydns}.py: explicit imports from djbdns.common.
[djbdns-logparse.git] / djbdns / dnscache.py
index a7fc56d3abb3a67066574ca09e78acd14dd470e6..a67060bb545ed0d57dbb5ab7da6345e9dc7a2fe3 100644 (file)
@@ -1,9 +1,11 @@
-from re import compile
+# Don't clobber the global compile() with a named import.
+import re
+
 from typing import Optional
-from djbdns.common import *
+from djbdns.common import convert_ip, query_type_name, timestamp_pat
 
 # The regex to match dnscache log lines.
-dnscache_log_re = compile(fr'({timestamp_pat}) (\w+)(.*)')
+dnscache_log_re = re.compile(fr'({timestamp_pat}) (\w+)(.*)')
 
 
 def decode_client(words : list, i : int):
@@ -57,8 +59,8 @@ def decode_client(words : list, i : int):
 
     if len(chunks) == 3:
         # For a "query" entry's clientip:clientport:id field.
-        id = int(chunks[2], 16)
-        words[i] += f" (id {id})"
+        packet_id = int(chunks[2], 16)
+        words[i] += f" (id {packet_id})"
 
 def decode_ip(words : list, i : int):
     r"""
@@ -205,7 +207,7 @@ def decode_type(words : list, i : int):
 
     """
     qt = words[i]
-    words[i] = query_type.get(int(qt), qt)
+    words[i] = query_type_name.get(int(qt), qt)
 
 def handle_dnscache_log(line : str) -> Optional[str]:
     r"""
@@ -287,9 +289,11 @@ def handle_dnscache_log(line : str) -> Optional[str]:
         decode_ttl(words, 1)
         if words[2] not in ("cname", "mx", "ns", "ptr", "soa"):
             decode_type(words, 2)
-            if words[2] == "a":         # decode answer to an A query
+            if words[2] == "a":
+                # Decode the response to an 'A' query
                 decode_ip(words, 4)
-            if words[2] == "txt":       # text record
+            if words[2] == "txt":
+                # Decode the TXT record's data from hex to ASCII.
                 response = words[4]
                 if response.endswith("..."):
                     ellipsis = "..."