-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdb.py
112 lines (93 loc) · 2.82 KB
/
db.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
111
# coding=utf8
from datetime import date
from model import AdjustmentRecord
from model import DayRecord
from model import MonthRecord
from model import TimeSheet
from model import YearRecord
from util import mkdirp
from util import parse_csv
from util import parse_time
from util import str_to_date
from util import str_to_timedelta
import os
import re
YEAR_DIR_PATTERN = re.compile(r'^\d\d\d\d$')
MONTH_FILE_PATTERN = re.compile(r'^\d\d\.csv$')
def parse_db(path):
year_dirs = sorted(
i for i in os.listdir(path)
if YEAR_DIR_PATTERN.match(i) is not None
)
return TimeSheet(
records=[
parse_year(os.path.join(path, year_dir))
for year_dir
in year_dirs
],
adjustments=parse_adjustments(os.path.join(path, 'adjustments.csv')),
)
def parse_year(year_path):
year = int(os.path.basename(year_path))
month_files = sorted(
i for i in os.listdir(year_path)
if MONTH_FILE_PATTERN.match(i) is not None
)
return YearRecord(
year=year,
records=[
parse_month(os.path.join(year_path, month_file), year)
for month_file in month_files
]
)
def parse_month(month_path, year):
month = int(os.path.basename(month_path).split('.')[0])
dicts = parse_csv(month_path)
return MonthRecord(
year=year,
month=month,
records=[
_day_dict_from_csv_to_day_record(d=d, year=year, month=month)
for d in dicts
]
)
def parse_adjustments(adjustments_path):
dicts = parse_csv(adjustments_path)
return [
AdjustmentRecord(
day=str_to_date(d['day']),
delta=str_to_timedelta(d['delta']),
)
for d in dicts
]
def write_day_record(db_path, day_record):
month_path = os.path.join(
db_path,
str(day_record.day.year),
'{:02d}.csv'.format(day_record.day.month)
)
mkdirp(os.path.dirname(month_path))
if not os.path.exists(month_path):
with open(month_path, 'w') as f:
f.write('day,day_type,checkin,checkout\n')
month_record = parse_month(month_path, year=day_record.day.year)
if month_record.get_day_record(day_record.day.day):
return False
with open(month_path, 'a') as f:
f.write(
','.join([
str(day_record.day.day),
day_record.day_type,
day_record.checkin.strftime('%H:%M'),
day_record.checkout.strftime('%H:%M'),
]) + '\n'
)
return True
def _day_dict_from_csv_to_day_record(d, year, month):
day = date(year=year, month=month, day=int(d['day']))
return DayRecord(
day=day,
day_type=d['day_type'],
checkin=parse_time(d['checkin'], day),
checkout=parse_time(d['checkout'], day),
)