Commit 21f41a4c2d639ef4b35df2b855e932c695d185a9

Implemented parser.

Signed-off-by: Heikki Salo <heikki.ao.salo@iki.fi>
candy.py
(59 / 0)
  
1# -*- coding: utf8 -*-
2import sys
3import datetime
4
5separator = ";"
6
7class WorkEvent(object):
8 begins = datetime.datetime.now()
9 ends = begins + datetime.timedelta(hours=1)
10 phase = "[Phase]"
11 subphase = "[Subphase]"
12 worker = "[Worker]"
13 comment = "[Comment]"
14
15def parse_date(day, time):
16 dstr, mstr, ystr = day.split(".")
17 day = int(dstr)
18 month = int(mstr)
19 year = int(ystr)
20 hstr, mstr = time.split(":")
21 hour = int(hstr)
22 min = int(mstr)
23 if hour == 24: # There's no "hour 24"
24 t = datetime.datetime(year, month, day, 0, 0 ,0)
25 return t + datetime.timedelta(days=1)
26 return datetime.datetime(year, month, day, hour, min, 0)
27
28def parse_events(csv_file_name):
29 file = open(csv_file_name)
30 events = []
31 for line in file:
32 try:
33 day,begint,endt,phase,subphase,worker,comment = line.split(separator)
34 begins = parse_date(day, begint)
35 ends = parse_date(day, endt)
36
37 event = WorkEvent()
38 event.begins = begins
39 event.ends = ends
40 event.phase = phase
41 event.subphase = subphase
42 event.worker = worker
43 event.comment = comment
44 events.append(event)
45 except:
46 print """Malformed line in %s: "%s".""" \
47 % (csv_file_name, line)
48 exit(1)
49 return events
50
51def main(csv_file_name):
52 worker_hours_by_weeks = dict()
53 events = parse_events(csv_file_name)
54
55if __name__ == "__main__":
56 if len(sys.argv) != 2:
57 print "Usage: python candy.py data.csv"
58 exit(1)
59 main(sys.argv[1])