comparison weather_server/locations.py @ 1:f66df122f18d

get the skeleton of a webserver up?
author Paul Fisher <paul@pfish.zone>
date Sun, 29 Sep 2019 00:52:13 -0400
parents
children f1ea183d28ba
comparison
equal deleted inserted replaced
0:efe7a1eff167 1:f66df122f18d
1 """Manages the directory containing the various logs."""
2
3 import configparser
4 import pathlib
5 import typing as t
6
7 import attr
8 import pytz
9
10 from . import logfile
11 from . import types
12
13
14 CONFIG_FILE = 'config.ini'
15 LOG = 'log.bson'
16
17
18 @attr.s(frozen=True, slots=True)
19 class LocationInfo:
20 name = attr.ib(type=str)
21 tz_name = attr.ib(type=str)
22 password = attr.ib(type=str)
23
24 def timezone(self):
25 try:
26 return pytz.timezone(self.tz_name)
27 except pytz.UnknownTimeZoneError:
28 return pytz.UTC
29
30 @classmethod
31 def load(cls, config_file: pathlib.Path) -> 'LocationInfo':
32 parser = configparser.ConfigParser(interpolation=None)
33 parser.read(config_file)
34 return LocationInfo(
35 name=parser.get('location', 'name', fallback='Weather station'),
36 tz_name=parser.get('location', 'timezone', fallback='UTC'),
37 password=parser.get('location', 'password'))
38
39
40 class Locations:
41 def __init__(self, base: str):
42 self._path = pathlib.Path(base)
43
44 def paths(self) -> t.Tuple[str, ...]:
45 return tuple(sorted(f.name for f in self._path.iterdir()))
46
47 def get(self, name) -> t.Tuple[LocationInfo, logfile.Logger]:
48 try:
49 directory = self._path / name
50 return (
51 LocationInfo.load(directory / CONFIG_FILE),
52 logfile.Logger.create(str(directory / LOG)))
53 except OSError:
54 raise KeyError(name)