-
Notifications
You must be signed in to change notification settings - Fork 0
/
tasks.py
94 lines (71 loc) · 2.49 KB
/
tasks.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import glob
import json
import logging
import os
from datalogger import save_as_daily_files, save_multiple_files
from uploaders import ftp_upload_files, sftp_upload_files, upload_ip_file
from utils import archive_past_days, archive_uploaded
logger = logging.getLogger(__name__)
class Task:
# Converts dict to object so that attributes are accessed with "." notation
def __init__(self, dict):
self.__dict__.update(dict)
def save(self, data):
save_as_daily_files(data, folder=self.local_folder, prefix=self.prefix)
def upload(self):
local_files = sorted(glob.glob(f"{self.local_folder}/*.csv"))
ftp_upload_files(
local_files,
ip=self.ip,
user=self.user,
passwd=self.passwd,
dir=self.dir,
annual_folders=self.annual_folders,
)
def archive(self):
archive_past_days(src_folder=self.local_folder)
def run(self, data=None):
logger.debug(f"{'-' * 10} Running {self.__class__.__name__} {'-' * 10}")
self.save(data=data)
self.upload()
self.archive()
def __repr__(self):
return f"{self.__dict__}"
class NasTask(Task):
def __init__(self, dict):
super(NasTask, self).__init__(dict)
def upload_ip(self):
upload_ip_file(self.ip, self.user, self.passwd, self.ip_file)
class MymeasurementsTask(Task):
def __init__(self, dict):
super(MymeasurementsTask, self).__init__(dict)
def save(self, data):
save_multiple_files(
data,
folder=self.local_folder,
records_per_file=5,
prefix=self.prefix,
)
def archive(self):
archive_uploaded(src_folder=self.local_folder)
class PyranocamTask(Task):
def __init__(self, dict):
super(PyranocamTask, self).__init__(dict)
def upload(self):
local_files = sorted(glob.glob(f"{self.local_folder}/*.csv"))
sftp_upload_files(
local_files,
host=self.host,
user=self.user,
passwd=self.passwd,
known_hosts_file=self.known_hosts_file,
dir=self.dir,
subdir=self.subdir,
)
dname = os.path.dirname(os.path.abspath(__file__))
config_file = os.path.join(dname, "tasks_config.json")
with open(config_file, "r") as f:
configs = json.load(f)
nas_task = NasTask(configs["nas"])
mymeasurements_task = MymeasurementsTask(configs["mymeasurements"])
pyranocam_task = PyranocamTask(configs["pyranocam"])