1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
|
#!/usr/bin/env python3
from dataclasses import dataclass
import logging
import platform
from typing import Callable, Optional
import config
import presence
logger = logging.getLogger(__name__)
args = config.add_commandline_args(
f'({__file__})',
'Args related to __file__'
)
args.add_argument(
'--site_config_override_location',
default='NONE',
const='NONE',
nargs='?',
choices=('HOUSE', 'CABIN', 'NONE'),
help='Where are we, HOUSE, CABIN?',
)
@dataclass
class SiteConfig(object):
location: str
network: str
network_netmask: str
network_router_ip: str
presence_location: presence.Location
is_anyone_present: Callable[None, bool]
def get_location():
"""
Where are we?
>>> location = get_location()
>>> location == 'HOUSE' or location == 'CABIN'
True
"""
return get_config().location
def is_anyone_present_wrapper(location: presence.Location):
p = presence.PresenceDetection()
return p.is_anyone_in_location_now(location)
def get_config():
"""
Get a configuration dataclass with information that is
site-specific including the current running location.
>>> cfg = get_config()
>>> cfg.location == 'HOUSE' or cfg.location == 'CABIN'
True
"""
hostname = platform.node()
try:
location_override = config.config['site_config_override_location']
except KeyError:
location_override = 'NONE'
if location_override == 'NONE':
if '.house' in hostname:
location = 'HOUSE'
elif '.cabin' in hostname:
location = 'CABIN'
if location == 'HOUSE':
return SiteConfig(
location = 'HOUSE',
network = '10.0.0.0/24',
network_netmask = '255.255.255.0',
network_router_ip = '10.0.0.1',
presence_location = presence.Location.HOUSE,
is_anyone_present = lambda x=presence.Location.HOUSE: is_anyone_present_wrapper(x),
)
elif location == 'CABIN':
return SiteConfig(
location = 'CABIN',
network = '192.168.0.0/24',
network_netmask = '255.255.255.0',
network_router_ip = '192.168.0.1',
presence_location = presence.Location.CABIN,
is_anyone_present = lambda x=presence.Location.CABIN: is_anyone_present_wrapper(x),
)
else:
raise Exception(f'Unknown site location: {location}')
if __name__ == '__main__':
import doctest
doctest.testmod()
|