-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsws-archive.py
More file actions
executable file
·97 lines (78 loc) · 3 KB
/
Copy pathsws-archive.py
File metadata and controls
executable file
·97 lines (78 loc) · 3 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
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
97
#!/usr/bin/env python3
# SPDX-License-Identifier: GPL-3.0-or-later
# Copyright (c) 2023 Jerome Marchand
import argparse
import datetime as dt
from dateutil.relativedelta import relativedelta
import re
import lzma
import shutil
def vprint(*args, **kwargs):
if verbose:
print(*args, **kwargs)
def archive(s, month):
fname = ifile + f'.{month.year}-{month.month:02d}.xz'
f = open(fname, 'wb')
vprint(f'Compressing {fname}')
f.write(lzma.compress(s.encode('utf-8')))
f.close()
def main():
parser = argparse.ArgumentParser(description='Archive Meteodata file')
parser.add_argument('-v', '--verbose', action='store_true',
help='be more verbose')
dategroup = parser.add_mutually_exclusive_group()
dategroup.add_argument('-m', '--month', action='store_true',
help='split file by months (default)')
dategroup.add_argument('-y', '--year', action='store_true',
help='split file by year')
parser.add_argument('-b', '--backup', action='store_true',
help='backup original file')
parser.add_argument('ifile', help='input file')
args = parser.parse_args()
global verbose
global ifile
verbose = args.verbose
ifile = args.ifile
if args.backup:
vprint(f'Backup file: {ifile}.bak')
shutil.copyfile(ifile, ifile + '.bak')
by_year = args.year
last_month = dt.date(dt.date.today().year, dt.date.today().month, 1) + relativedelta(months=-1)
dont_archive = False
working_month = None
next_working_month = None
out = ''
with open(ifile, 'r', encoding='utf-8') as f:
l = re.compile(r'(?P<date>\d{4}-\d\d-\d\d) (\d\d:\d\d)\s*(\d* \d)\s*(-?\d*.\d)C (\d*)%')
for line in f:
if line[0] == '#':
# TODO: uses regex to allow blank char before '#'?
out += line
continue
m = l.match(line)
if not m:
print("Line doesn't match")
d = dt.date.fromisoformat(m.group('date'));
if not dont_archive and d >= last_month:
# From now on, keep the data uncompressed,
# it's going back to the input file
dont_archive = True
if working_month:
archive(out, working_month)
out = ''
vprint('Don\'t archive this month or the last: exit')
if not working_month:
working_month = dt.date(d.year, d.month, 1)
next_working_month = working_month + relativedelta(months=+1)
if not dont_archive and d >= next_working_month:
archive(out, working_month)
working_month = dt.date(d.year, d.month, 1)
next_working_month = working_month + relativedelta(months=+1)
out = ''
out+= line
f.close()
f = open(ifile, 'w', encoding='utf-8')
f.write(out)
f.close()
if __name__ == '__main__':
main()