]> gitweb.michael.orlitzky.com - djbdns-logparse.git/blobdiff - bin/djbdns-logparse.py
bin/djbdns-logparse.py: add a docstring for decode_ttl().
[djbdns-logparse.git] / bin / djbdns-logparse.py
index 8f30e1430cbfa1e8c6f9af11e398f5159d3ebd75..5262fe0e965d012e744955478a6ecb81b549a984 100755 (executable)
@@ -1,5 +1,5 @@
 #!/usr/bin/python3
-"""
+r"""
 Convert tinydns and dnscache logs to human-readable form
 """
 
@@ -80,7 +80,7 @@ query_drop_reason = {
 
 
 def convert_ip(ip : str) -> str:
-    """
+    r"""
     Convert a hex string representing an IP address to
     human-readable form.
 
@@ -166,22 +166,91 @@ def decode_client(words : list, i : int):
         id = int(chunks[2], 16)
         words[i] += f" (id {id})"
 
-def decode_ip(words, i):
+def decode_ip(words : list, i : int):
+    r"""
+    Helper function to decode the ip field in a dnscache log
+    entry.
+
+    A single "serverip" field is present in the lame, nodata,
+    nxdomain, and rr entry types.
+
+    Parameters
+    ----------
+
+    words : list
+        The ``words`` list (a list of fields) from
+        :func:`handle_dnscache_log`.
+
+    i : int
+        The index of the ip field within ``words``
+
+    Returns
+    -------
+
+    Nothing; the ``i``th entry in the ``words`` list is modified
+    in-place.
+
+    Examples
+    --------
+
+        >>> words = ["foo", "bar", "7f000001", "quux"]
+        >>> decode_ip(words, 2)
+        >>> words
+        ['foo', 'bar', '127.0.0.1', 'quux']
+
+        >>> words = ["foo", "00000000000000000000ffff7f000001", "bar", "quux"]
+        >>> decode_ip(words, 1)
+        >>> words
+        ['foo', '0000:0000:0000:0000:0000:ffff:7f00:0001', 'bar', 'quux']
+
+    """
     words[i] = convert_ip(words[i])
 
-def decode_ttl(words, i):
+def decode_ttl(words : list, i : int):
+    r"""
+    Helper function to decode the ttl field in a dnscache log
+    entry.
+
+    A single "ttl" field is present in the nodata, nxdomain, and
+    rr entry types.
+
+    Parameters
+    ----------
+
+    words : list
+        The ``words`` list (a list of fields) from
+        :func:`handle_dnscache_log`.
+
+    i : int
+        The index of the ttl field within ``words``
+
+    Returns
+    -------
+
+    Nothing; the ``i``th entry in the ``words`` list is modified
+    in-place.
+
+    Examples
+    --------
+
+        >>> words = ["c0a80101", "20865", "1", "www.example.com.", "5db8d822"]
+        >>> decode_ttl(words, 1)
+        >>> words
+        ['c0a80101', 'TTL=20865', '1', 'www.example.com.', '5db8d822']
+
+    """
     words[i] = f"TTL={words[i]}"
 
-def decode_serial(words, i):
+def decode_serial(words : list, i : int):
     serial = int(words[i])
     words[i] = f"#{serial}"
 
-def decode_type(words, i):
+def decode_type(words : list, i : int):
     qt = words[i]
     words[i] = query_type.get(int(qt), qt)
 
-def handle_dnscache_log(line) -> typing.Optional[str]:
-    """
+def handle_dnscache_log(line : str) -> typing.Optional[str]:
+    r"""
     Handle a single log line if it matches the ``dnscache_log_re`` regex.
 
     Parameters
@@ -306,7 +375,7 @@ def handle_dnscache_log(line) -> typing.Optional[str]:
 
 
 def handle_tinydns_log(line : str) -> typing.Optional[str]:
-    """
+    r"""
     Handle a single log line if it matches the ``tinydns_log_re`` regex.
 
     Parameters