Ugh, a bunch of things. @overrides. --lmodule. Chromecasts. etc...
[python_utils.git] / site_config.py
1 #!/usr/bin/env python3
2
3 from dataclasses import dataclass
4 import logging
5 import platform
6 from typing import Callable
7
8 # Note: this module is fairly early loaded.  Be aware of dependencies.
9 import config
10 import presence
11
12 logger = logging.getLogger(__name__)
13 args = config.add_commandline_args(
14     f'({__file__})',
15     'Args related to __file__'
16 )
17 args.add_argument(
18     '--site_config_override_location',
19     default='NONE',
20     const='NONE',
21     nargs='?',
22     choices=('HOUSE', 'CABIN', 'NONE'),
23     help='Where are we, HOUSE, CABIN?',
24 )
25
26
27 @dataclass
28 class SiteConfig(object):
29     location: str
30     network: str
31     network_netmask: str
32     network_router_ip: str
33     presence_location: presence.Location
34     is_anyone_present: Callable[None, bool]
35     arper_minimum_device_count: int
36
37
38 def get_location():
39     """
40     Where are we?
41
42     >>> location = get_location()
43     >>> location == 'HOUSE' or location == 'CABIN'
44     True
45
46     """
47     return get_config().location
48
49
50 def is_anyone_present_wrapper(location: presence.Location):
51     p = presence.PresenceDetection()
52     return p.is_anyone_in_location_now(location)
53
54
55 def get_config():
56     """
57     Get a configuration dataclass with information that is
58     site-specific including the current running location.
59
60     >>> cfg = get_config()
61     >>> cfg.location == 'HOUSE' or cfg.location == 'CABIN'
62     True
63
64     """
65     hostname = platform.node()
66     try:
67         location_override = config.config['site_config_override_location']
68     except KeyError:
69         location_override = 'NONE'
70     if location_override == 'NONE':
71         if '.house' in hostname:
72             location = 'HOUSE'
73         elif '.cabin' in hostname:
74             location = 'CABIN'
75     if location == 'HOUSE':
76         return SiteConfig(
77             location = 'HOUSE',
78             network = '10.0.0.0/24',
79             network_netmask = '255.255.255.0',
80             network_router_ip = '10.0.0.1',
81             presence_location = presence.Location.HOUSE,
82             is_anyone_present = lambda x=presence.Location.HOUSE: is_anyone_present_wrapper(x),
83             arper_minimum_device_count = 50,
84         )
85     elif location == 'CABIN':
86         return SiteConfig(
87             location = 'CABIN',
88             network = '192.168.0.0/24',
89             network_netmask = '255.255.255.0',
90             network_router_ip = '192.168.0.1',
91             presence_location = presence.Location.CABIN,
92             is_anyone_present = lambda x=presence.Location.CABIN: is_anyone_present_wrapper(x),
93             arper_minimum_device_count = 15,
94         )
95     else:
96         raise Exception(f'Unknown site location: {location}')
97
98
99 if __name__ == '__main__':
100     import doctest
101     doctest.testmod()