-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.py
executable file
·76 lines (65 loc) · 2.65 KB
/
util.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import board
from database import fetch_actions
from datetime import datetime, timedelta
import configparser
class Util:
@staticmethod
def get_pins():
pins = [pin for pin in dir(board) if pin.startswith('D')]
return pins
@staticmethod
def get_pin(pin):
return getattr(board, pin)
def fetch_and_separete_actions(dt_from, dt_to):
dt_from = dt_from + timedelta(days=1)
dt_to = dt_to + timedelta(days=1)
actions = fetch_actions(dt_from, dt_to)
actors = {}
for action in actions:
# name the variables for better understanding
time = action[1]
actor = action[2]
state = action[3]
if actor not in actors:
# if the actor is not in the dictionary, add it
actors[actor] = []
if state == 0:
# if the actor is off, add a dummy entry
actors[actor].append({'dt_from': datetime.strftime(dt_from, '%Y-%m-%dT%H:%M:%S'), 'dt_to': time})
else:
actors[actor].append({'dt_from': time, 'dt_to': None})
else:
if state == 1:
if actors[actor][-1]['dt_to'] is None:
# if the actor is already on, remove the last entry
actors[actor].pop()
actors[actor].append({'dt_from': time, 'dt_to': None})
else:
actors[actor][-1]['dt_to'] = time
# check if some actor is still on
for actor in actors:
if actors[actor][-1]['dt_to'] is None:
actors[actor][-1]['dt_to'] = datetime.strftime(dt_to, '%Y-%m-%dT%H:%M:%S')
return actors
class UIConfigs:
_instance = None
def __new__(cls, *args, **kwargs):
if cls._instance is None:
cls._instance = super(UIConfigs, cls).__new__(cls, *args, **kwargs)
cls._instance._initialize(*args, **kwargs)
return cls._instance
def _initialize(self):
self.config_file_name = 'config.ini'
self.config = configparser.ConfigParser()
self.config.read(self.config_file_name)
def write(self, name, value):
self.config['ui-configuration'][name] = value
with open('config.ini', 'w') as configfile:
self.config.write(configfile)
def read(self, name):
if name not in self.config['ui-configuration']:
return None
return self.config['ui-configuration'][name]
def read_all(self):
config = {name: value for name, value in self.config['ui-configuration'].items()}
return config