X-Git-Url: https://wannabe.guru.org/gitweb/?a=blobdiff_plain;f=site_config.py;h=3bf049e3787f66a38f42b5be0330642f4f39e645;hb=65e6f781dffb268be6ef0a015f4cc89b57b62a5e;hp=1281661abb6ac52d9fd6384916ce617731ad34b3;hpb=3ff520c22a4fcba0590400a20f822ad096927f6b;p=python_utils.git diff --git a/site_config.py b/site_config.py index 1281661..3bf049e 100644 --- a/site_config.py +++ b/site_config.py @@ -1,9 +1,9 @@ #!/usr/bin/env python3 -from dataclasses import dataclass 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 @@ -13,7 +13,7 @@ logger = logging.getLogger(__name__) args = config.add_commandline_args( f'Global Site Config ({__file__})', - 'Args related to global site-specific configuration' + 'Args related to global site-specific configuration', ) args.add_argument( '--site_config_override_location', @@ -35,6 +35,7 @@ class SiteConfig(object): presence_location: Location is_anyone_present: Callable arper_minimum_device_count: int + arper_cache_file: str def get_location_name(): @@ -64,11 +65,34 @@ def get_location(): def is_anyone_present_wrapper(location: Location): import base_presence + p = base_presence.PresenceDetection() return p.is_anyone_in_location_now(location) -def get_config(): +def other_location() -> str: + 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: + 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 get_config(location_override: Optional[str] = None): """ Get a configuration dataclass with information that is site-specific including the current running location. @@ -78,37 +102,40 @@ 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' + 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: + location = location_override + if location == 'HOUSE': return SiteConfig( - 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, + 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( - 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, + 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(f'Unknown site location: {location}') @@ -116,4 +143,5 @@ def get_config(): if __name__ == '__main__': import doctest + doctest.testmod()