Since this thing is on the innerwebs I suppose it should have a
[python_utils.git] / site_config.py
index 81185bf046d1004579679d082aa9d3aaa4841284..5604de676c0ded41ce5edcc051809ac7968c6286 100644 (file)
 #!/usr/bin/env python3
 
-from dataclasses import dataclass
+# © Copyright 2021-2022, Scott Gasch
+
+"""Location/site dependent data."""
+
 import logging
 import platform
-from typing import Optional
+from dataclasses import dataclass
+from typing import Callable, Optional
 
+# Note: this module is fairly early loaded.  Be aware of dependencies.
 import config
+from type.locations import Location
 
 logger = logging.getLogger(__name__)
+
 args = config.add_commandline_args(
-    f'({__file__})',
-    'Args related to __file__'
+    f'Global Site Config ({__file__})',
+    'Args related to global site-specific configuration',
 )
-
 args.add_argument(
-    '--site_config_location',
-    default='AUTO',
-    const='AUTO',
+    '--site_config_override_location',
+    default='NONE',
+    const='NONE',
     nargs='?',
-    choices=('HOUSE', 'CABIN', 'AUTO'),
-    help='Where are we, HOUSE or CABIN?'
+    choices=['HOUSE', 'CABIN', 'NONE'],
+    help='Where are we, HOUSE, CABIN?  Overrides standard detection code.',
 )
 
 
 @dataclass
 class SiteConfig(object):
+    """The set of information specific to where the program is running."""
+
+    location_name: str
+    location: Location
     network: str
     network_netmask: str
     network_router_ip: str
+    presence_location: Location
+    is_anyone_present: Callable
+    arper_minimum_device_count: int
+    arper_cache_file: str
+
+
+def get_location_name():
+    """
+    Where are we?
+
+    >>> location = get_location_name()
+    >>> location == 'HOUSE' or location == 'CABIN'
+    True
+
+    """
+    return get_config().location_name
 
 
 def get_location():
-    location = config.config['site_config_location']
-    if location == 'AUTO':
-        hostname = platform.node()
-        if '.house' in hostname:
-            location = 'HOUSE'
-        elif '.cabin' in hostname:
-            location = 'CABIN'
-        else:
-            raise Exception(f'Unknown hostname {hostname}, help.')
+    """
+    Returns location as an enum instead of a string.
+
+    >>> from type.locations import Location
+    >>> location = get_location()
+    >>> location == Location.HOUSE or location == Location.CABIN
+    True
+
+    """
+    return get_config().location
+
+
+def is_anyone_present_wrapper(location: Location):
+    import base_presence
+
+    p = base_presence.PresenceDetection()
+    return p.is_anyone_in_location_now(location)
+
+
+def other_location() -> str:
+    """
+    Returns the location where this program is _NOT_ running.
+
+    >>> x = other_location()
+    >>> x in set(['HOUSE', 'CABIN'])
+    True
+
+    >>> y = this_location()
+    >>> x == y
+    False
+
+    """
+    this = this_location()
+    if this == 'HOUSE':
+        return 'CABIN'
+    elif this == 'CABIN':
+        return 'HOUSE'
+    else:
+        raise Exception(f"{this} doesn't tell me where I'm running?!")
+
+
+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'
+    elif '.local' in hostname:
+        location = 'HOUSE'
+    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 = get_location()
+def get_config(location_override: Optional[str] = None):
+    """
+    Get a configuration dataclass with information that is
+    site-specific including the current running location.
+
+    >>> cfg = get_config()
+    >>> cfg.location_name == 'HOUSE' or cfg.location_name == 'CABIN'
+    True
+
+    """
+    location = effective_location(location_override)
     if location == 'HOUSE':
         return SiteConfig(
-            network = '10.0.0.0/24',
-            network_netmask = '255.255.255.0',
-            network_router_ip = '10.0.0.1',
+            location_name='HOUSE',
+            location=Location.HOUSE,
+            network='10.0.0.0/24',
+            network_netmask='255.255.255.0',
+            network_router_ip='10.0.0.1',
+            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(
-            network = '192.168.0.0/24',
-            network_netmask = '255.255.255.0',
-            network_router_ip = '192.168.0.1',
+            location_name='CABIN',
+            location=Location.CABIN,
+            network='192.168.0.0/24',
+            network_netmask='255.255.255.0',
+            network_router_ip='192.168.0.1',
+            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('Unknown site location')
+        raise Exception(f'Unknown site location: {location}')
+
+
+if __name__ == '__main__':
+    import doctest
+
+    doctest.testmod()