-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlenses.py
executable file
·110 lines (82 loc) · 2.53 KB
/
lenses.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#!/usr/bin/env python3.8
import logging
from argparse import ArgumentParser
from datetime import datetime, timedelta
from os import path
LOG_NAME = f"{path.dirname(path.abspath(__file__))}/lenses.log"
DATE_FORMAT = "%Y-%m-%d"
def parse_args():
parser = ArgumentParser()
parser.add_argument(
'--clear', '-c',
action='store_true',
help='clear lenses account log file',
)
parser.add_argument(
'--date', '-d',
help="date when lenses were used or unused (default: today)",
)
parser.add_argument(
'--fill', '-f',
action='store_true',
help='option to set all unspecified dates between the first and the last dates as missed',
)
parser.add_argument(
'--missed', '-m',
action='store_true',
help='option to specify that lenses were unused',
)
parser.add_argument(
'--used', '-u',
action='store_true',
help='option to specify that lenses were used',
)
return parser.parse_args()
def init_logger():
logging.basicConfig(
filename=LOG_NAME,
level=logging.INFO,
format='%(message)s',
)
return logging.getLogger()
def clear_log():
with open(LOG_NAME, 'w') as log:
log.truncate(0)
def add_log_line(logger, date, msg):
logger.info(f"{date}:{msg}")
def fill_log(logger):
with open(LOG_NAME) as log:
lines = [line.split(':') for line in log.read().splitlines()]
lines.sort()
item, count = 0, len(lines)
while not item >= count - 1:
if (datetime.strptime(lines[item + 1][0], DATE_FORMAT) - \
(date := datetime.strptime(lines[item][0], DATE_FORMAT))
).total_seconds() > 60 * 60 * 24:
lines.insert(item + 1, [(date + timedelta(1)).strftime(DATE_FORMAT), '0'])
item += 1
count = len(lines)
clear_log()
for date, result in lines:
add_log_line(logger, date, result)
def account():
total_used = 0
with open(LOG_NAME) as log:
for line in log.readlines():
print(line, end="")
total_used += int(line.split(':')[-1])
if total_used:
print('------------')
print('USED:', total_used)
def main():
args = parse_args()
logger = init_logger()
if args.clear:
clear_log()
if msg := '1' if args.used else '0' if args.missed else '':
add_log_line(logger, args.date or datetime.now().strftime(DATE_FORMAT), msg)
if args.fill:
fill_log(logger)
account()
if __name__ == '__main__':
main()