X-Git-Url: https://wannabe.guru.org/gitweb/?a=blobdiff_plain;f=site_config.py;h=7f6410d39356c1d0d8f910cfcece67eaf4cff29e;hb=e8fbbb7306430478dec55d2c963eed116d8330cc;hp=3bf049e3787f66a38f42b5be0330642f4f39e645;hpb=65e6f781dffb268be6ef0a015f4cc89b57b62a5e;p=python_utils.git diff --git a/site_config.py b/site_config.py index 3bf049e..7f6410d 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,6 +75,14 @@ def is_anyone_present_wrapper(location: Location): 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' @@ -82,6 +94,14 @@ def other_location() -> str: 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' @@ -92,15 +112,17 @@ def this_location() -> str: 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. +def effective_location(location_override: Optional[str] = None) -> str: + """Detects and returns a location taking into account two override + mechanisms. - >>> cfg = get_config() - >>> cfg.location_name == 'HOUSE' or cfg.location_name == 'CABIN' + >>> x = effective_location() + >>> x in set(['HOUSE', 'CABIN']) True + >>> effective_location('HOUSE') + 'HOUSE' + """ if location_override is None: try: @@ -111,8 +133,22 @@ def get_config(location_override: Optional[str] = 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. + + >>> cfg = get_config() + >>> cfg.location_name == 'HOUSE' or cfg.location_name == 'CABIN' + True + + """ + location = effective_location(location_override) if location == 'HOUSE': return SiteConfig( location_name='HOUSE',