import datetime
import logging
import os
+from typing import Any
import urllib.request
import astral # type: ignore
@persistent.persistent_autoloaded_singleton()
-class CachedDetailedWeatherForecast(object):
+class CachedDetailedWeatherForecast(persistent.Persistent):
def __init__(self, forecasts = None):
if forecasts is not None:
self.forecasts = forecasts
@classmethod
@overrides
- def load(cls):
+ def load(cls) -> Any:
if persistent.was_file_written_within_n_seconds(
config.config['weather_forecast_cachefile'],
config.config['weather_forecast_stalest_acceptable'].total_seconds(),
return None
@overrides
- def save(self):
+ def save(self) -> bool:
import pickle
with open(config.config['weather_forecast_cachefile'], 'wb') as wf:
pickle.dump(
wf,
pickle.HIGHEST_PROTOCOL,
)
+ return True
self.remote_executor: Optional[RemoteExecutor] = None
def ping(self, host) -> bool:
+ logger.debug(f'RUN> ping -c 1 {host}')
command = ['ping', '-c', '1', host]
return subprocess.call(
command,
return self.process_executor
def remote_pool(self) -> RemoteExecutor:
+ logger.info('Looking for some helper machines...')
if self.remote_executor is None:
pool: List[RemoteWorkerRecord] = []
if self.ping('cheetah.house'):
+ logger.info('Found cheetah.house')
pool.append(
RemoteWorkerRecord(
username = 'scott',
),
)
if self.ping('video.house'):
+ logger.info('Found video.house')
pool.append(
RemoteWorkerRecord(
username = 'scott',
),
)
if self.ping('wannabe.house'):
+ logger.info('Found wannabe.house')
pool.append(
RemoteWorkerRecord(
username = 'scott',
),
)
if self.ping('meerkat.cabin'):
+ logger.info('Found meerkat.cabin')
pool.append(
RemoteWorkerRecord(
username = 'scott',
),
)
if self.ping('backup.house'):
+ logger.info('Found backup.house')
pool.append(
RemoteWorkerRecord(
username = 'scott',
count = 4,
),
)
+ if self.ping('kiosk.house'):
+ logger.info('Found kiosk.house')
+ pool.append(
+ RemoteWorkerRecord(
+ username = 'pi',
+ machine = 'kiosk.house',
+ weight = 1,
+ count = 2,
+ ),
+ )
if self.ping('puma.cabin'):
+ logger.info('Found puma.cabin')
pool.append(
RemoteWorkerRecord(
username = 'scott',
count = 4,
),
)
+
+ # The controller machine has a lot to do; go easy on it.
+ for record in pool:
+ if record.machine == platform.node() and record.count > 1:
+ logger.info(f'Reducing workload for {record.machine}.')
+ record.count = 1
+
policy = WeightedRandomRemoteWorkerSelectionPolicy()
policy.register_worker_pool(pool)
self.remote_executor = RemoteExecutor(pool, policy)
for bigram in string_utils.ngrams_presplit(words, 2):
bigram = ' '.join(bigram)
if self.is_bad_word(bigram):
- logger.debug('"{bigram}" is profanity')
+ logger.debug(f'"{bigram}" is profanity')
return True
if len(words) > 2:
for trigram in string_utils.ngrams_presplit(words, 3):
trigram = ' '.join(trigram)
if self.is_bad_word(trigram):
- logger.debug('"{trigram}" is profanity')
+ logger.debug(f'"{trigram}" is profanity')
return True
return False
color = color.lower()
return ansi.COLOR_NAMES_TO_RGB.get(color, None)
+ @abstractmethod
+ def status(self) -> str:
+ pass
+
@abstractmethod
def turn_on(self) -> bool:
pass
ask_google(f"turn {self.goog_name()} off")
)
+ @overrides
+ def status(self) -> str:
+ if self.is_on():
+ return 'ON'
+ return 'off'
+
@overrides
def is_on(self) -> bool:
r = ask_google(f"is {self.goog_name()} on?")
def get_status(self) -> Dict[str, Any]:
return self.bulb.status()
+ @overrides
+ def status(self) -> str:
+ ret = ''
+ for k, v in self.bulb.status().items():
+ ret += f'{k} = {v}\n'
+ return ret
+
@overrides
def turn_on(self) -> bool:
self.bulb.turn_on()
@overrides
def set_dimmer_level(self, level: int) -> bool:
+ logger.debug(f'Setting brightness to {level}')
self.bulb.set_brightness(level)
return True
@overrides
def make_color(self, color: str) -> bool:
rgb = BaseLight.parse_color_string(color)
+ logger.debug(f'Light color: {color} -> {rgb}')
if rgb is not None:
self.bulb.set_colour(rgb[0], rgb[1], rgb[2])
return True
self.info_ts = None
return None
+ @overrides
+ def status(self) -> str:
+ ret = ''
+ for k, v in self.get_info().items():
+ ret += f'{k} = {v}\n'
+ return ret
+
def get_on_duration_seconds(self, child: str = None) -> int:
self.info = self.get_info()
if child is None:
if line == "":
continue
logger.debug(f'SH-CONFIG> {line}')
- (mac, name, keywords) = line.split(",")
+ try:
+ (mac, name, keywords) = line.split(",")
+ except ValueError:
+ logger.warning(f'SH-CONFIG> {line} is malformed?!')
+ continue
mac = mac.strip()
name = name.strip()
keywords = keywords.strip()