-
Notifications
You must be signed in to change notification settings - Fork 0
/
timedatapoints.py
executable file
·67 lines (51 loc) · 1.81 KB
/
timedatapoints.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
import os
import importlib
import importlib.machinery
import importlib.util
import sys
import time
from pathlib import Path
def main():
# Get environment configuration
# @todo Let's make these arguments passed when the python script is called
# instead of environment variables?
datapointmod = os.environ.get('DATAPOINTMOD')
persistencemod = os.environ.get('PERSISTENCEMOD')
period = os.environ.get('DATAPOINTPERIOD')
if datapointmod is None or persistencemod is None:
if datapointmod is None:
print('Please add the DATAPOINTMOD env var.')
if persistencemod is None:
print('Please add the PERSISTENCEMOD env var.')
quit()
if period is None or period == 0:
print('Please add the DATAPOINTPERIOD env var.')
quit()
# Load the datapoint class
datapoint = dynamic_imp(datapointmod)
# Load the persistence class
persistence = dynamic_imp(persistencemod)
# Set the logging period
period = int(period)
# Get the datapoint and write it to the persistence every number of seconds
# defined by the period variable.
starttime = time.monotonic()
previous_job = None
while True:
job = execute(datapoint, persistence, previous_job)
previous_job = job
time.sleep(period - ((time.monotonic() - starttime) % period))
# dynamic import
def dynamic_imp(name):
try:
mod = __import__(name)
return mod
except ImportError:
print ("module not found: " + name)
def execute(datapoint, persistence, previous_job):
current_time = time.localtime()
formatted_time = time.strftime('%Y-%m-%d %H:%M', current_time)
return persistence.write(formatted_time, datapoint.get(), previous_job)
# Function Call
if __name__ == '__main__':
main()