5 from dataclasses import dataclass
6 from typing import Callable, Optional
8 # Note: this module is fairly early loaded. Be aware of dependencies.
10 from type.locations import Location
12 logger = logging.getLogger(__name__)
14 args = config.add_commandline_args(
15 f'Global Site Config ({__file__})',
16 'Args related to global site-specific configuration',
19 '--site_config_override_location',
23 choices=['HOUSE', 'CABIN', 'NONE'],
24 help='Where are we, HOUSE, CABIN? Overrides standard detection code.',
29 class SiteConfig(object):
34 network_router_ip: str
35 presence_location: Location
36 is_anyone_present: Callable
37 arper_minimum_device_count: int
41 def get_location_name():
45 >>> location = get_location_name()
46 >>> location == 'HOUSE' or location == 'CABIN'
50 return get_config().location_name
55 Returns location as an enum instead of a string.
57 >>> from type.locations import Location
58 >>> location = get_location()
59 >>> location == Location.HOUSE or location == Location.CABIN
63 return get_config().location
66 def is_anyone_present_wrapper(location: Location):
69 p = base_presence.PresenceDetection()
70 return p.is_anyone_in_location_now(location)
73 def other_location() -> str:
74 hostname = platform.node()
75 if '.house' in hostname:
77 elif '.cabin' in hostname:
80 raise Exception(f"{hostname} doesn't help me know where I'm running?!")
84 def this_location() -> str:
85 hostname = platform.node()
86 if '.house' in hostname:
88 elif '.cabin' in hostname:
91 raise Exception(f"{hostname} doesn't help me know where I'm running?!")
95 def effective_location(location_override: Optional[str] = None) -> str:
96 if location_override is None:
98 location_override = config.config['site_config_override_location']
100 location_override = None
102 if location_override is None or location_override == 'NONE':
103 location = this_location()
105 logger.debug(f'site_config\'s location_override was set to: {location_override}')
106 location = location_override
110 def get_config(location_override: Optional[str] = None):
112 Get a configuration dataclass with information that is
113 site-specific including the current running location.
115 >>> cfg = get_config()
116 >>> cfg.location_name == 'HOUSE' or cfg.location_name == 'CABIN'
120 location = effective_location(location_override)
121 if location == 'HOUSE':
123 location_name='HOUSE',
124 location=Location.HOUSE,
125 network='10.0.0.0/24',
126 network_netmask='255.255.255.0',
127 network_router_ip='10.0.0.1',
128 presence_location=Location.HOUSE,
129 is_anyone_present=lambda x=Location.HOUSE: is_anyone_present_wrapper(x),
130 arper_minimum_device_count=50,
131 arper_cache_file='/home/scott/cache/.arp_table_cache_house',
133 elif location == 'CABIN':
135 location_name='CABIN',
136 location=Location.CABIN,
137 network='192.168.0.0/24',
138 network_netmask='255.255.255.0',
139 network_router_ip='192.168.0.1',
140 presence_location=Location.CABIN,
141 is_anyone_present=lambda x=Location.CABIN: is_anyone_present_wrapper(x),
142 arper_minimum_device_count=15,
143 arper_cache_file='/home/scott/cache/.arp_table_cache_cabin',
146 raise Exception(f'Unknown site location: {location}')
149 if __name__ == '__main__':