3 """Location/site dependent data."""
7 from dataclasses import dataclass
8 from typing import Callable, Optional
10 # Note: this module is fairly early loaded. Be aware of dependencies.
12 from type.locations import Location
14 logger = logging.getLogger(__name__)
16 args = config.add_commandline_args(
17 f'Global Site Config ({__file__})',
18 'Args related to global site-specific configuration',
21 '--site_config_override_location',
25 choices=['HOUSE', 'CABIN', 'NONE'],
26 help='Where are we, HOUSE, CABIN? Overrides standard detection code.',
31 class SiteConfig(object):
32 """The set of information specific to where the program is running."""
38 network_router_ip: str
39 presence_location: Location
40 is_anyone_present: Callable
41 arper_minimum_device_count: int
45 def get_location_name():
49 >>> location = get_location_name()
50 >>> location == 'HOUSE' or location == 'CABIN'
54 return get_config().location_name
59 Returns location as an enum instead of a string.
61 >>> from type.locations import Location
62 >>> location = get_location()
63 >>> location == Location.HOUSE or location == Location.CABIN
67 return get_config().location
70 def is_anyone_present_wrapper(location: Location):
73 p = base_presence.PresenceDetection()
74 return p.is_anyone_in_location_now(location)
77 def other_location() -> str:
79 Returns the location where this program is _NOT_ running.
81 >>> x = other_location()
82 >>> x in set(['HOUSE', 'CABIN'])
85 >>> y = this_location()
90 this = this_location()
96 raise Exception(f"{this} doesn't tell me where I'm running?!")
99 def this_location() -> str:
101 Returns the location where this program _IS_ running.
103 >>> x = this_location()
104 >>> x in set(['HOUSE', 'CABIN'])
108 hostname = platform.node()
109 if '.house' in hostname:
111 elif '.cabin' in hostname:
113 elif '.local' in hostname:
116 raise Exception(f"{hostname} doesn't help me know where I'm running?!")
120 def effective_location(location_override: Optional[str] = None) -> str:
121 """Detects and returns a location taking into account two override
124 >>> x = effective_location()
125 >>> x in set(['HOUSE', 'CABIN'])
128 >>> effective_location('HOUSE')
132 if location_override is None:
134 location_override = config.config['site_config_override_location']
136 location_override = None
138 if location_override is None or location_override == 'NONE':
139 location = this_location()
141 logger.debug('site_config\'s location_override was set to: %s', location_override)
142 location = location_override
146 def get_config(location_override: Optional[str] = None):
148 Get a configuration dataclass with information that is
149 site-specific including the current running location.
151 >>> cfg = get_config()
152 >>> cfg.location_name == 'HOUSE' or cfg.location_name == 'CABIN'
156 location = effective_location(location_override)
157 if location == 'HOUSE':
159 location_name='HOUSE',
160 location=Location.HOUSE,
161 network='10.0.0.0/24',
162 network_netmask='255.255.255.0',
163 network_router_ip='10.0.0.1',
164 presence_location=Location.HOUSE,
165 is_anyone_present=lambda x=Location.HOUSE: is_anyone_present_wrapper(x),
166 arper_minimum_device_count=50,
167 arper_cache_file='/home/scott/cache/.arp_table_cache_house',
169 elif location == 'CABIN':
171 location_name='CABIN',
172 location=Location.CABIN,
173 network='192.168.0.0/24',
174 network_netmask='255.255.255.0',
175 network_router_ip='192.168.0.1',
176 presence_location=Location.CABIN,
177 is_anyone_present=lambda x=Location.CABIN: is_anyone_present_wrapper(x),
178 arper_minimum_device_count=15,
179 arper_cache_file='/home/scott/cache/.arp_table_cache_cabin',
182 raise Exception(f'Unknown site location: {location}')
185 if __name__ == '__main__':