-
Notifications
You must be signed in to change notification settings - Fork 27
/
history.py
62 lines (46 loc) · 1.71 KB
/
history.py
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
from datetime import datetime
import json
from pathlib import Path
from typing import TypedDict, Protocol
from weather_api_service import Weather
from weather_formatter import format_weather
class WeatherStorage(Protocol):
"""Interface for any storage saving weather"""
def save(self, weather: Weather) -> None:
raise NotImplementedError
class PlainFileWeatherStorage:
"""Store weather in plain text file"""
def __init__(self, file: Path):
self._file = file
def save(self, weather: Weather) -> None:
now = datetime.now()
formatted_weather = format_weather(weather)
with open(self._file, "a") as f:
f.write(f"{now}\n{formatted_weather}\n")
class HistoryRecord(TypedDict):
date: str
weather: str
class JSONFileWeatherStorage:
"""Store weather in JSON file"""
def __init__(self, jsonfile: Path):
self._jsonfile = jsonfile
self._init_storage()
def save(self, weather: Weather) -> None:
history = self._read_history()
history.append({
"date": str(datetime.now()),
"weather": format_weather(weather)
})
self._write(history)
def _init_storage(self) -> None:
if not self._jsonfile.exists():
self._jsonfile.write_text("[]")
def _read_history(self) -> list[HistoryRecord]:
with open(self._jsonfile, "r") as f:
return json.load(f)
def _write(self, history: list[HistoryRecord]) -> None:
with open(self._jsonfile, "w") as f:
json.dump(history, f, ensure_ascii=False, indent=4)
def save_weather(weather: Weather, storage: WeatherStorage) -> None:
"""Saves weather in the storage"""
storage.save(weather)