Mercurial > personal > weather-server
comparison weather_server/locations.py @ 26:7def5611895b
Add support for an index page displaying all locations.
This seems as good a time as any to declare 0.1.0.
author | Paul Fisher <paul@pfish.zone> |
---|---|
date | Sun, 10 Nov 2019 23:46:51 -0500 |
parents | 20c8ec56e447 |
children | 9bc3687e1e5e |
comparison
equal
deleted
inserted
replaced
25:a4147ecb18b3 | 26:7def5611895b |
---|---|
18 """Raised when a location can't be loaded.""" | 18 """Raised when a location can't be loaded.""" |
19 | 19 |
20 | 20 |
21 class Location: | 21 class Location: |
22 | 22 |
23 def __init__(self, root: pathlib.Path): | 23 def __init__(self, root: pathlib.Path, key: str): |
24 parser = configparser.ConfigParser(interpolation=None) | 24 parser = configparser.ConfigParser(interpolation=None) |
25 self.root = root | 25 self.root = root |
26 self.key = key | |
26 config_file = root / CONFIG_FILE | 27 config_file = root / CONFIG_FILE |
27 try: | 28 try: |
28 with open(config_file, 'r') as infile: | 29 with open(config_file, 'r') as infile: |
29 parser.read_file(infile) | 30 parser.read_file(infile) |
30 self.name = parser.get( | 31 self.name = parser.get( |
79 def get(self, name: str) -> Location: | 80 def get(self, name: str) -> Location: |
80 self._maybe_reload() | 81 self._maybe_reload() |
81 locs, _ = self.info | 82 locs, _ = self.info |
82 return locs[name] | 83 return locs[name] |
83 | 84 |
85 def locations(self) -> t.Dict[str, Location]: | |
86 return self.info[0] | |
87 | |
84 def _maybe_reload(self) -> None: | 88 def _maybe_reload(self) -> None: |
85 new_mtime = self.root.stat().st_mtime_ns | 89 new_mtime = self.root.stat().st_mtime_ns |
86 _, old_mtime = self.info | 90 _, old_mtime = self.info |
87 if old_mtime == new_mtime: | 91 if old_mtime == new_mtime: |
88 return | 92 return |
89 locations = {} | 93 locations = {} |
90 for child in self.root.iterdir(): | 94 for child in self.root.iterdir(): |
91 try: | 95 try: |
92 locations[child.name] = Location(child) | 96 locations[child.name] = Location(child, child.name) |
93 except ConfigError: | 97 except ConfigError: |
94 pass # It's OK. Skip this. | 98 pass # It's OK. Skip this. |
95 self.info = locations, new_mtime | 99 self.info = locations, new_mtime |