X-Git-Url: https://wannabe.guru.org/gitweb/?a=blobdiff_plain;f=site_config.py;h=233589970ea2058b2768de16ec649602eda2be2f;hb=31c81f6539969a5eba864d3305f9fb7bf716a367;hp=332731277dde2ef16b983da04e749ba19975c496;hpb=7e6972bc7c8e891dc669645fa5969ed76fe38314;p=python_utils.git diff --git a/site_config.py b/site_config.py index 3327312..2335899 100644 --- a/site_config.py +++ b/site_config.py @@ -1,52 +1,71 @@ #!/usr/bin/env python3 -from dataclasses import dataclass import logging import platform -from typing import Callable, Optional +from dataclasses import dataclass +from typing import Callable +# Note: this module is fairly early loaded. Be aware of dependencies. import config -import presence +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_override_location', default='NONE', const='NONE', nargs='?', - choices=('HOUSE', 'CABIN', 'NONE'), - help='Where are we, HOUSE, CABIN?', + choices=['HOUSE', 'CABIN', 'NONE'], + help='Where are we, HOUSE, CABIN? Overrides standard detection code.', ) @dataclass class SiteConfig(object): - location: str + location_name: str + location: Location network: str network_netmask: str network_router_ip: str - presence_location: presence.Location - is_anyone_present: Callable[None, bool] + presence_location: Location + is_anyone_present: Callable + arper_minimum_device_count: int -def get_location(): +def get_location_name(): """ Where are we? - >>> location = get_location() + >>> location = get_location_name() >>> location == 'HOUSE' or location == 'CABIN' True + """ + return get_config().location_name + + +def get_location(): + """ + 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: presence.Location): - p = presence.PresenceDetection() +def is_anyone_present_wrapper(location: Location): + import base_presence + + p = base_presence.PresenceDetection() return p.is_anyone_in_location_now(location) @@ -56,7 +75,7 @@ def get_config(): site-specific including the current running location. >>> cfg = get_config() - >>> cfg.location == 'HOUSE' or cfg.location == 'CABIN' + >>> cfg.location_name == 'HOUSE' or cfg.location_name == 'CABIN' True """ @@ -72,21 +91,25 @@ def get_config(): location = 'CABIN' if location == 'HOUSE': return SiteConfig( - location = 'HOUSE', - network = '10.0.0.0/24', - network_netmask = '255.255.255.0', - network_router_ip = '10.0.0.1', - presence_location = presence.Location.HOUSE, - is_anyone_present = lambda x=presence.Location.HOUSE: is_anyone_present_wrapper(x), + 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, ) elif location == 'CABIN': return SiteConfig( - location = 'CABIN', - network = '192.168.0.0/24', - network_netmask = '255.255.255.0', - network_router_ip = '192.168.0.1', - presence_location = presence.Location.CABIN, - is_anyone_present = lambda x=presence.Location.CABIN: is_anyone_present_wrapper(x), + 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, ) else: raise Exception(f'Unknown site location: {location}') @@ -94,4 +117,5 @@ def get_config(): if __name__ == '__main__': import doctest + doctest.testmod()