From: Michael Orlitzky Date: Thu, 22 Sep 2022 01:49:38 +0000 (-0400) Subject: djbdns/*.py: capitalize global variables. X-Git-Tag: 0.0.1~6 X-Git-Url: http://gitweb.michael.orlitzky.com/?p=djbdns-logparse.git;a=commitdiff_plain;h=49ee5a403cafc02a8e398c840b9bbc4c9d9e0f72 djbdns/*.py: capitalize global variables. --- diff --git a/djbdns/common.py b/djbdns/common.py index 06124bf..ea5dd31 100644 --- a/djbdns/common.py +++ b/djbdns/common.py @@ -3,7 +3,7 @@ from struct import pack # A pattern to match the timestamp format that the tai64nlocal program # produces. It appears in both dnscache and tinydns lines, after # they've been piped through tai64nlocal, of course. -timestamp_pat = r'[\d-]+ [\d:\.]+' +TIMESTAMP_PAT = r'[\d-]+ [\d:\.]+' # A dictionary mapping query type identifiers, in decimal, to their # friendly names for tinydns. Reference: @@ -12,7 +12,7 @@ timestamp_pat = r'[\d-]+ [\d:\.]+' # # Note that mapping here is non-exhaustive, and that tinydns will # log responses for record types that it does not know about. -query_type_name = { +QUERY_TYPE_NAME = { 1: "a", 2: "ns", 5: "cname", diff --git a/djbdns/dnscache.py b/djbdns/dnscache.py index a67060b..ac8b41c 100644 --- a/djbdns/dnscache.py +++ b/djbdns/dnscache.py @@ -2,10 +2,10 @@ import re from typing import Optional -from djbdns.common import convert_ip, query_type_name, timestamp_pat +from djbdns.common import QUERY_TYPE_NAME, TIMESTAMP_PAT, convert_ip # The regex to match dnscache log lines. -dnscache_log_re = re.compile(fr'({timestamp_pat}) (\w+)(.*)') +DNSCACHE_LOG_RE = re.compile(fr'({TIMESTAMP_PAT}) (\w+)(.*)') def decode_client(words : list, i : int): @@ -207,17 +207,17 @@ def decode_type(words : list, i : int): """ qt = words[i] - words[i] = query_type_name.get(int(qt), qt) + words[i] = QUERY_TYPE_NAME.get(int(qt), qt) def handle_dnscache_log(line : str) -> Optional[str]: r""" - Handle a single log line if it matches the ``dnscache_log_re`` regex. + Handle a single log line if it matches the ``DNSCACHE_LOG_RE`` regex. Parameters ---------- line : string - The log line that might match ``dnscache_log_re``. + The log line that might match ``DNSCACHE_LOG_RE``. Returns ------- @@ -253,7 +253,7 @@ def handle_dnscache_log(line : str) -> Optional[str]: >>> handle_dnscache_log(line) """ - match = dnscache_log_re.match(line) + match = DNSCACHE_LOG_RE.match(line) if not match: return None diff --git a/djbdns/tinydns.py b/djbdns/tinydns.py index 5c4275e..2d9741e 100644 --- a/djbdns/tinydns.py +++ b/djbdns/tinydns.py @@ -2,21 +2,21 @@ import re from typing import Optional -from djbdns.common import convert_ip, query_type_name, timestamp_pat +from djbdns.common import QUERY_TYPE_NAME, TIMESTAMP_PAT, convert_ip # The "hex4" pattern matches a string of four hexadecimal digits. This # is used, for example, by tinydns to encode the query type # identifier. -hex4_pat = r'[0-9a-f]{4}' +HEX4_PAT = r'[0-9a-f]{4}' # The IP pattern matches a string of either 8 or 32 hexadecimal # characters, which correspond to IPv4 and IPv6 addresses, # respectively, in tinydns logs. -ip_pat = r'[0-9a-f]{8,32}' +IP_PAT = r'[0-9a-f]{8,32}' # The regex to match tinydns log lines. -tinydns_log_re = re.compile( - rf'({timestamp_pat}) ({ip_pat}):({hex4_pat}):({hex4_pat}) ([\+\-IC/]) ({hex4_pat}) (.*)' +TINYDNS_LOG_RE = re.compile( + rf'({TIMESTAMP_PAT}) ({IP_PAT}):({HEX4_PAT}):({HEX4_PAT}) ([\+\-IC/]) ({HEX4_PAT}) (.*)' ) # tinydns can drop a query for one of three reasons; this dictionary @@ -24,7 +24,7 @@ tinydns_log_re = re.compile( # reason. We include the "+" case here, indicating that the query was # NOT dropped, to avoid a special case later on when we're formatting # the human-readable output. -query_drop_reason = { +QUERY_DROP_REASON = { "+": None, "-": "no authority", "I": "invalid query", @@ -35,13 +35,13 @@ query_drop_reason = { def handle_tinydns_log(line : str) -> Optional[str]: r""" - Handle a single log line if it matches the ``tinydns_log_re`` regex. + Handle a single log line if it matches the ``TINYDNS_LOG_RE`` regex. Parameters ---------- line : string - The log line that might match ``tinydns_log_re``. + The log line that might match ``TINYDNS_LOG_RE``. Returns ------- @@ -61,7 +61,7 @@ def handle_tinydns_log(line : str) -> Optional[str]: >>> handle_tinydns_log(line) """ - match = tinydns_log_re.match(line) + match = TINYDNS_LOG_RE.match(line) if not match: return None @@ -74,11 +74,11 @@ def handle_tinydns_log(line : str) -> Optional[str]: # using the query_type dictionary. If the right name isn't present # in the dictionary, we use the (decimal) type id instead. query_type = int(query_type, 16) # "001c" -> 28 - query_type = query_type_name.get(query_type, type) # 28 -> "aaaa" + query_type = QUERY_TYPE_NAME.get(query_type, type) # 28 -> "aaaa" line_tpl = "{timestamp} " - reason = query_drop_reason[code] + reason = QUERY_DROP_REASON[code] if code == "+": line_tpl += "sent response to {ip}:{port} (id {request_id}): " line_tpl += "{query_type} {name}"