-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtasks.py
More file actions
53 lines (44 loc) · 1.8 KB
/
tasks.py
File metadata and controls
53 lines (44 loc) · 1.8 KB
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
# Storage for scheduled tasks
#
# The Tasks class contains a dict with run-times for the scheduled
# tasks and functions to load this dict from a json file and store
# its content to a json file.
#
# Copyright 2022 (c) Erik de Lange
# Released under MIT license
import json
import logging
class Tasks:
TASKS_FILE = "tasks.json"
def __init__(self):
self.task = {
"start_low": [22, 30],
"start_medium": [7, 0],
"ntp_time_sync": [5, 0]
} # default values for first time use, run-time format = [hh, mm]
self.load()
def load(self, filename=TASKS_FILE):
try:
# load previously saved run times (if found)
with open(filename) as fp:
temp = json.loads(fp.read())
# json format and content check: reject file if keys
# and value data types don't match dict 'self.tasks'
if not all(key in temp for key in self.task):
raise KeyError(f"missing key in file {filename}")
for key in temp:
if not (type(temp[key]) is list and len(temp[key]) == 2):
raise TypeError("expected list with length of 2 "
f"for key '{key}', found {type(temp[key]).__name__}"
)
self.task = temp
except (ValueError, KeyError, TypeError) as e:
print(f"{e.__class__.__name__} loading file {filename} - {e}")
except OSError as e:
print(f"{e} - loading file {filename}")
def save(self, filename=TASKS_FILE):
try:
with open(filename, "w") as fp:
json.dump(self.task, fp)
except OSError as e:
logging.critical(f"[Errno {e.args[0]}] {e.args[1]}: {filename}")