-
Notifications
You must be signed in to change notification settings - Fork 1
/
luftsachsen.py
96 lines (81 loc) · 2.96 KB
/
luftsachsen.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
95
96
#!/usr/bin/env python3
import os
import datetime
import logging, traceback
log = logging.getLogger('uws')
from AirImport import importer, converter
def main(data_dir, basedir, use_git = False):
# Fail early if git is needed
if use_git:
from git import Repo
def s(n):
return '0' * (2 - len(str(n))) + str(n)
date = datetime.datetime.now()
end_date = '{}-{}'.format(s(date.month), s(date.year))
dirs = [os.path.join(s(date.year), s(date.month))]
start_date = end_date
# Download previous month as well on the first day of the month
if date.day == 1:
td = datetime.timedelta(days = 1)
start_date = date - td
dirs.append(os.path.join(s(start_date.year), s(start_date.month)))
start_date = '{}-{}'.format(s(start_date.month), s(start_date.year))
# Set taregt dirs
(raw_dir, out_dir) = (
os.path.join(data_dir, 'raw'), os.path.join(data_dir, 'joint'))
# Download data from Umwelt Sachsen
importer.main(date = start_date, end_date = end_date,
out_dir = raw_dir, basedir = basedir)
# Convert data to joint csv
for dir in dirs:
converter.main(data_dir = os.path.join(raw_dir, dir),
out_dir = os.path.join(out_dir, dir), basedir = basedir)
# Automatically commit and push
if use_git:
for d in ('raw', 'joint'):
repo = Repo(data_dir)
index = repo.index
index.add([os.path.join(d, dir) for dir in dirs])
index.commit('Updated {} data for {}'.format(d,
date.strftime('%Y-%m-%d %H:%M')))
repo.remotes.origin.push()
if __name__ == '__main__':
from logging import StreamHandler
import sys
stdout_logger = StreamHandler(sys.stdout)
stderr_logger = StreamHandler(sys.stderr)
log.addHandler(stdout_logger)
log.addHandler(stderr_logger)
stdout_logger.setLevel(logging.WARNING)
stderr_logger.setLevel(logging.ERROR)
from optparse import OptionParser
class OptParser(OptionParser):
def format_epilog(self, formatter):
return self.epilog
parser = OptParser(epilog = """
This script is meant to be run regularly in order to generate and push data
automatically to the repository.
""")
parser.add_option(
"-d", "--data-dir",
dest = "data_dir",
help = u"Directory that contains all data."
)
parser.add_option(
"-b", "--base-dir",
dest = "basedir",
default = os.getcwd(),
help = u"No files outside of this directory may be accessed."
)
parser.add_option(
"-g", "--git",
action = 'store_true',
dest = "use_git",
default = False,
help = u"No files outside of this directory may be accessed."
)
(options, arguments) = parser.parse_args()
if not options.data_dir:
parser.error('Call with --data-dir')
main(data_dir = options.data_dir, basedir = options.basedir,
use_git = options.use_git)