More cleanup, yey!
[python_utils.git] / site_config.py
index b09e735c79c8f92665438b2c064f9ccf652d33bd..7f6410d39356c1d0d8f910cfcece67eaf4cff29e 100644 (file)
@@ -1,9 +1,11 @@
 #!/usr/bin/env python3
 
-from dataclasses import dataclass
+"""Location/site dependent data."""
+
 import logging
 import platform
-from typing import Callable
+from dataclasses import dataclass
+from typing import Callable, Optional
 
 # Note: this module is fairly early loaded.  Be aware of dependencies.
 import config
@@ -27,6 +29,8 @@ args.add_argument(
 
 @dataclass
 class SiteConfig(object):
+    """The set of information specific to where the program is running."""
+
     location_name: str
     location: Location
     network: str
@@ -35,6 +39,7 @@ class SiteConfig(object):
     presence_location: Location
     is_anyone_present: Callable
     arper_minimum_device_count: int
+    arper_cache_file: str
 
 
 def get_location_name():
@@ -69,7 +74,71 @@ def is_anyone_present_wrapper(location: Location):
     return p.is_anyone_in_location_now(location)
 
 
-def get_config():
+def other_location() -> str:
+    """
+    Returns the location where this program is _NOT_ running.
+
+    >>> x = other_location()
+    >>> x in set(['HOUSE', 'CABIN'])
+    True
+
+    """
+    hostname = platform.node()
+    if '.house' in hostname:
+        location = 'CABIN'
+    elif '.cabin' in hostname:
+        location = 'HOUSE'
+    else:
+        raise Exception(f"{hostname} doesn't help me know where I'm running?!")
+    return location
+
+
+def this_location() -> str:
+    """
+    Returns the location where this program _IS_ running.
+
+    >>> x = this_location()
+    >>> x in set(['HOUSE', 'CABIN'])
+    True
+
+    """
+    hostname = platform.node()
+    if '.house' in hostname:
+        location = 'HOUSE'
+    elif '.cabin' in hostname:
+        location = 'CABIN'
+    else:
+        raise Exception(f"{hostname} doesn't help me know where I'm running?!")
+    return location
+
+
+def effective_location(location_override: Optional[str] = None) -> str:
+    """Detects and returns a location taking into account two override
+    mechanisms.
+
+    >>> x = effective_location()
+    >>> x in set(['HOUSE', 'CABIN'])
+    True
+
+    >>> effective_location('HOUSE')
+    'HOUSE'
+
+    """
+    if location_override is None:
+        try:
+            location_override = config.config['site_config_override_location']
+        except KeyError:
+            location_override = None
+
+    if location_override is None or location_override == 'NONE':
+        location = this_location()
+    else:
+        logger.debug('site_config\'s location_override was set to: %s', location_override)
+        location = location_override
+    return location
+
+
+def get_config(location_override: Optional[str] = None):
     """
     Get a configuration dataclass with information that is
     site-specific including the current running location.
@@ -79,16 +148,7 @@ def get_config():
     True
 
     """
-    hostname = platform.node()
-    try:
-        location_override = config.config['site_config_override_location']
-    except KeyError:
-        location_override = 'NONE'
-    if location_override == 'NONE':
-        if '.house' in hostname:
-            location = 'HOUSE'
-        elif '.cabin' in hostname:
-            location = 'CABIN'
+    location = effective_location(location_override)
     if location == 'HOUSE':
         return SiteConfig(
             location_name='HOUSE',
@@ -99,6 +159,7 @@ def get_config():
             presence_location=Location.HOUSE,
             is_anyone_present=lambda x=Location.HOUSE: is_anyone_present_wrapper(x),
             arper_minimum_device_count=50,
+            arper_cache_file='/home/scott/cache/.arp_table_cache_house',
         )
     elif location == 'CABIN':
         return SiteConfig(
@@ -110,6 +171,7 @@ def get_config():
             presence_location=Location.CABIN,
             is_anyone_present=lambda x=Location.CABIN: is_anyone_present_wrapper(x),
             arper_minimum_device_count=15,
+            arper_cache_file='/home/scott/cache/.arp_table_cache_cabin',
         )
     else:
         raise Exception(f'Unknown site location: {location}')