-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathextract.py
More file actions
64 lines (52 loc) · 1.89 KB
/
Copy pathextract.py
File metadata and controls
64 lines (52 loc) · 1.89 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
#!/usr/bin/env python3
import argparse
from collections import namedtuple
from pprint import pprint
from openpyxl import load_workbook, Workbook
from openpyxl.worksheet import Worksheet
from blocks import Block
from formatter_csv import format_csv
from formatter_sql import format_sql
LEADER_ROW = 32
BLOCK_ROW_MAP = { # block -> row
Block.FX: 2,
Block.BM: 7,
Block.DM: 12,
Block.QT: 17,
Block.XH: 22,
Block.ZH: 27
}
def extract_staff_name(name: str) -> str:
return name.split('-')[0].strip()
ScheduleOfWeek = namedtuple('ScheduleOfWeek', ['leader', 'staffs'])
def extract(filename):
wb: Workbook = load_workbook(filename=filename)
ws: Worksheet = wb.active
schedule = {}
for week in range(1, 7):
column = chr(ord('A') + week)
leader = extract_staff_name(ws[f"{column}{LEADER_ROW}"].value)
staffs = {}
for block, rowIndex in BLOCK_ROW_MAP.items():
staffs[block] = []
for i in range(5):
cell = ws[f"{column}{rowIndex + i}"]
if cell.value:
staffs[block].append(extract_staff_name(cell.value))
schedule[week] = ScheduleOfWeek(leader, staffs)
return schedule
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Parse schedule excel into something useful.')
parser.add_argument('-f', '--format', type=str, default='print',
help='the output format')
parser.add_argument('-t', '--table', type=str, default='operators',
help='the table of sql_update output')
parser.add_argument('input', metavar='INPUT',
help='the input file')
args = parser.parse_args()
schedule = extract(args.input)
{
'print': pprint,
'sql_update': lambda schedule: format_sql(schedule, table=args.table),
'csv': format_csv
}[args.format](schedule)