add more docstrings
[logparse.git] / logparse / util.py
index 8f905f470b297dcf2d60d5c4722f3641267dba52..3aca9043b196784f5f0f56cbd9f68c5588684d9e 100644 (file)
@@ -1,43 +1,65 @@
-#
-#   utilities.py
-#
-#   Commonly used general functions
-#
+"""
+Commonly used general functions.
 
-import re
+This module provides the following methods:
+    - `hostname`:       get the current machine's hostname
+    - `getlocaldomain`: get the current machine's domain name
+    - `resolve`:        attempt to convert a local/public IP to hostname
+    - `readlog`:        read contents of a log file from disk
+"""
+
+from datetime import datetime, timedelta
+import inspect
+import logging
 import os
+from pkg_resources import Requirement, resource_filename
+import re
 import socket
-import inspect
 from systemd import journal
-from datetime import datetime, timedelta
 
-import logging
-logger = logging.getLogger(__name__)
+from logparse import config
 
-from pkg_resources import Requirement, resource_filename
 
-from logparse import config
+logger = logging.getLogger(__name__)
+
 
 def hostname(path): # get the hostname of current server
+    """
+    Get the hostname of the current machine using the file supplied in the
+    `hostname-path` config option.
+    """
+
     hnfile = open(path, 'r')
     hn = re.search('^(\w*)\n*', hnfile.read()).group(1)
     return hn
 
+
 def getlocaldomain(): # get the parent fqdn of current server
-    domain = socket.getfqdn().split('.', 1) # Note: if socket.fetfqdn() returns localhost, make sure the first entry in /etc/hosts contains the fqdn
+    """
+    Get parent domain name (possibly FQDN) of the current machine. Note: if
+    `socket.fetfqdn()` returns localhost, make sure the first entry in the
+    hostname file includes the FQDN.
+    """
+
+    domain = socket.getfqdn().split('.', 1)
     if len(domain) != 2:
-        logger.warning('Could not get domain of this server, only hostname. Please consider updating /etc/hosts')
+        logger.warning("Could not get domain of this server, only hostname. Please consider updating the hostname file at {0}".format(config.prefs.get("logparse", "hostname-path")))
         return 'localdomain'
     else:
         return domain[-1]
 
+
 def resolve(ip, fqdn=None):        # try to resolve an ip to hostname
-    # Possible values for fqdn:
-    #   fqdn            show full hostname and domain
-    #   fqdn-implicit   show hostname and domain unless local
-    #   host-only       only show hostname
-    #   ip              never resolve anything
-    # resolve-domains defined in individual sections of the config take priority over global config
+    """
+    Attempt to resolve an IP into a hostname or FQDN.
+    Possible values for fqdn:
+        - fqdn            show full hostname and domain
+        - fqdn-implicit   show hostname and domain unless local
+        - host-only       only show hostname
+        - ip              never resolve anything
+    Note resolve-domains settings defined in individual sections of the config
+    take priority over the global config (this is enforced in parser modules)
+    """
     
     if not fqdn:
         fqdn = config.prefs.get("logparse", "resolve-domains")
@@ -55,7 +77,7 @@ def resolve(ip, fqdn=None):        # try to resolve an ip to hostname
         elif fqdn == 'host-only':
             return(hn.split('.')[0])
         else:
-            logger.warning("invalid value for fqdn config")
+            logger.warning("Invalid value for FQDN config")
             return(hn)
     except socket.herror:
         # cannot resolve ip
@@ -69,10 +91,15 @@ def resolve(ip, fqdn=None):        # try to resolve an ip to hostname
         logger.warning("failed to resolve hostname for " + ip + ": " + str(err))
         return(ip)  # return ip if no hostname exists
 
-def readlog(path = None, mode = 'r'):   # read file
+
+def readlog(path = None, mode = 'r'):
+    """
+    Read a logfile from disk and return string
+    """
+    
     if (path == None):
-        logger.error("no path provided")
-        return
+        logger.error("No path provided")
+        return 1
     else:
         if (os.path.isfile(path) is False):
             logger.error("Log at {0} was requested but does not exist".format(path))