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