summaryrefslogtreecommitdiff
path: root/waterspout_radar/_storage.py
blob: 97d16fd4f268b2fa39af69a068c205899f393916 (plain)
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
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):
		if not period:
			now = datetime.datetime.now()
			period = (now, now + datetime.timedelta(days=5))

		def _is_between(prediction):
			return period[0] < prediction.time and period[1] > prediction.time

		return filter(_is_between, 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()