X-Git-Url: https://wannabe.guru.org/gitweb/?a=blobdiff_plain;f=site_config.py;h=5a4eeffbafd5c2f32ddd412f41986ec92f481985;hb=c6fca944b41f292b66efaba27ebf3fd0a37956b8;hp=d98c6bc36b8e5671ea581f14dd21f31356debc99;hpb=4d03debb5b84b5b3e096add468ecd87c55ed0f5f;p=python_utils.git diff --git a/site_config.py b/site_config.py index d98c6bc..5a4eeff 100644 --- a/site_config.py +++ b/site_config.py @@ -1,5 +1,7 @@ #!/usr/bin/env python3 +"""Location/site dependent data.""" + import logging import platform from dataclasses import dataclass @@ -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 @@ -71,28 +75,60 @@ def is_anyone_present_wrapper(location: Location): def other_location() -> str: - hostname = platform.node() - if '.house' in hostname: - location = 'CABIN' - elif '.cabin' in hostname: - location = 'HOUSE' + """ + 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"{hostname} doesn't help me know where I'm running?!") - return location + 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'] @@ -102,7 +138,7 @@ def effective_location(location_override: Optional[str] = None) -> str: if location_override is None or location_override == 'NONE': location = this_location() else: - logger.debug(f'site_config\'s location_override was set to: {location_override}') + logger.debug('site_config\'s location_override was set to: %s', location_override) location = location_override return location