More cleanup.
[python_utils.git] / site_config.py
1 #!/usr/bin/env python3
2
3 """Location/site dependent data."""
4
5 import logging
6 import platform
7 from dataclasses import dataclass
8 from typing import Callable, Optional
9
10 # Note: this module is fairly early loaded.  Be aware of dependencies.
11 import config
12 from type.locations import Location
13
14 logger = logging.getLogger(__name__)
15
16 args = config.add_commandline_args(
17     f'Global Site Config ({__file__})',
18     'Args related to global site-specific configuration',
19 )
20 args.add_argument(
21     '--site_config_override_location',
22     default='NONE',
23     const='NONE',
24     nargs='?',
25     choices=['HOUSE', 'CABIN', 'NONE'],
26     help='Where are we, HOUSE, CABIN?  Overrides standard detection code.',
27 )
28
29
30 @dataclass
31 class SiteConfig(object):
32     """The set of information specific to where the program is running."""
33
34     location_name: str
35     location: Location
36     network: str
37     network_netmask: str
38     network_router_ip: str
39     presence_location: Location
40     is_anyone_present: Callable
41     arper_minimum_device_count: int
42     arper_cache_file: str
43
44
45 def get_location_name():
46     """
47     Where are we?
48
49     >>> location = get_location_name()
50     >>> location == 'HOUSE' or location == 'CABIN'
51     True
52
53     """
54     return get_config().location_name
55
56
57 def get_location():
58     """
59     Returns location as an enum instead of a string.
60
61     >>> from type.locations import Location
62     >>> location = get_location()
63     >>> location == Location.HOUSE or location == Location.CABIN
64     True
65
66     """
67     return get_config().location
68
69
70 def is_anyone_present_wrapper(location: Location):
71     import base_presence
72
73     p = base_presence.PresenceDetection()
74     return p.is_anyone_in_location_now(location)
75
76
77 def other_location() -> str:
78     """
79     Returns the location where this program is _NOT_ running.
80
81     >>> x = other_location()
82     >>> x in set(['HOUSE', 'CABIN'])
83     True
84
85     """
86     hostname = platform.node()
87     if '.house' in hostname:
88         location = 'CABIN'
89     elif '.cabin' in hostname:
90         location = 'HOUSE'
91     else:
92         raise Exception(f"{hostname} doesn't help me know where I'm running?!")
93     return location
94
95
96 def this_location() -> str:
97     """
98     Returns the location where this program _IS_ running.
99
100     >>> x = this_location()
101     >>> x in set(['HOUSE', 'CABIN'])
102     True
103
104     """
105     hostname = platform.node()
106     if '.house' in hostname:
107         location = 'HOUSE'
108     elif '.cabin' in hostname:
109         location = 'CABIN'
110     else:
111         raise Exception(f"{hostname} doesn't help me know where I'm running?!")
112     return location
113
114
115 def effective_location(location_override: Optional[str] = None) -> str:
116     """Detects and returns a location taking into account two override
117     mechanisms.
118
119     >>> x = effective_location()
120     >>> x in set(['HOUSE', 'CABIN'])
121     True
122
123     >>> effective_location('HOUSE')
124     'HOUSE'
125
126     """
127     if location_override is None:
128         try:
129             location_override = config.config['site_config_override_location']
130         except KeyError:
131             location_override = None
132
133     if location_override is None or location_override == 'NONE':
134         location = this_location()
135     else:
136         logger.debug('site_config\'s location_override was set to: %s', location_override)
137         location = location_override
138     return location
139
140
141 def get_config(location_override: Optional[str] = None):
142     """
143     Get a configuration dataclass with information that is
144     site-specific including the current running location.
145
146     >>> cfg = get_config()
147     >>> cfg.location_name == 'HOUSE' or cfg.location_name == 'CABIN'
148     True
149
150     """
151     location = effective_location(location_override)
152     if location == 'HOUSE':
153         return SiteConfig(
154             location_name='HOUSE',
155             location=Location.HOUSE,
156             network='10.0.0.0/24',
157             network_netmask='255.255.255.0',
158             network_router_ip='10.0.0.1',
159             presence_location=Location.HOUSE,
160             is_anyone_present=lambda x=Location.HOUSE: is_anyone_present_wrapper(x),
161             arper_minimum_device_count=50,
162             arper_cache_file='/home/scott/cache/.arp_table_cache_house',
163         )
164     elif location == 'CABIN':
165         return SiteConfig(
166             location_name='CABIN',
167             location=Location.CABIN,
168             network='192.168.0.0/24',
169             network_netmask='255.255.255.0',
170             network_router_ip='192.168.0.1',
171             presence_location=Location.CABIN,
172             is_anyone_present=lambda x=Location.CABIN: is_anyone_present_wrapper(x),
173             arper_minimum_device_count=15,
174             arper_cache_file='/home/scott/cache/.arp_table_cache_cabin',
175         )
176     else:
177         raise Exception(f'Unknown site location: {location}')
178
179
180 if __name__ == '__main__':
181     import doctest
182
183     doctest.testmod()