summaryrefslogtreecommitdiff
path: root/waterspout_radar/_storage.py
blob: aa9f0b14fa355531140b908abbdba99130bed448 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
import datetime
import typing

import tinydb

from . import _radar

Period = typing.Tuple[datetime.datetime, datetime.datetime]


class Storage:
	def __init__(self, pathname):
		self.db = tinydb.TinyDB(pathname, create_dirs=True)

	def show(self, period: Period=None):
		return map(_radar.Prediction.from_json, self.db.all())

	def extend(self, predictions: typing.Iterable[_radar.Prediction]):
		for prediction in predictions:
			self.db.insert(prediction.json())

	def __iter__(self):
		return self.show()