Stop trying to cache mac addresses from house and cabin in the same
[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, Optional
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     arper_cache_file: str
39
40
41 def get_location_name():
42     """
43     Where are we?
44
45     >>> location = get_location_name()
46     >>> location == 'HOUSE' or location == 'CABIN'
47     True
48
49     """
50     return get_config().location_name
51
52
53 def get_location():
54     """
55     Returns location as an enum instead of a string.
56
57     >>> from type.locations import Location
58     >>> location = get_location()
59     >>> location == Location.HOUSE or location == Location.CABIN
60     True
61
62     """
63     return get_config().location
64
65
66 def is_anyone_present_wrapper(location: Location):
67     import base_presence
68
69     p = base_presence.PresenceDetection()
70     return p.is_anyone_in_location_now(location)
71
72
73 def other_location() -> str:
74     hostname = platform.node()
75     if '.house' in hostname:
76         location = 'CABIN'
77     elif '.cabin' in hostname:
78         location = 'HOUSE'
79     else:
80         raise Exception(f"{hostname} doesn't help me know where I'm running?!")
81     return location
82
83
84 def this_location() -> str:
85     hostname = platform.node()
86     if '.house' in hostname:
87         location = 'HOUSE'
88     elif '.cabin' in hostname:
89         location = 'CABIN'
90     else:
91         raise Exception(f"{hostname} doesn't help me know where I'm running?!")
92     return location
93
94
95 def get_config(location_override: Optional[str] = None):
96     """
97     Get a configuration dataclass with information that is
98     site-specific including the current running location.
99
100     >>> cfg = get_config()
101     >>> cfg.location_name == 'HOUSE' or cfg.location_name == 'CABIN'
102     True
103
104     """
105     if location_override is None:
106         try:
107             location_override = config.config['site_config_override_location']
108         except KeyError:
109             location_override = None
110
111     if location_override is None or location_override == 'NONE':
112         location = this_location()
113     else:
114         location = location_override
115
116     if location == 'HOUSE':
117         return SiteConfig(
118             location_name='HOUSE',
119             location=Location.HOUSE,
120             network='10.0.0.0/24',
121             network_netmask='255.255.255.0',
122             network_router_ip='10.0.0.1',
123             presence_location=Location.HOUSE,
124             is_anyone_present=lambda x=Location.HOUSE: is_anyone_present_wrapper(x),
125             arper_minimum_device_count=50,
126             arper_cache_file='/home/scott/cache/.arp_table_cache_house',
127         )
128     elif location == 'CABIN':
129         return SiteConfig(
130             location_name='CABIN',
131             location=Location.CABIN,
132             network='192.168.0.0/24',
133             network_netmask='255.255.255.0',
134             network_router_ip='192.168.0.1',
135             presence_location=Location.CABIN,
136             is_anyone_present=lambda x=Location.CABIN: is_anyone_present_wrapper(x),
137             arper_minimum_device_count=15,
138             arper_cache_file='/home/scott/cache/.arp_table_cache_cabin',
139         )
140     else:
141         raise Exception(f'Unknown site location: {location}')
142
143
144 if __name__ == '__main__':
145     import doctest
146
147     doctest.testmod()