-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsplit.py
executable file
·205 lines (153 loc) · 5.05 KB
/
split.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#!/usr/bin/python
# -*- coding: utf-8 -*-
from xlrd import *
import sys
import re
reload(sys)
sys.setdefaultencoding('utf-8')
sheet = None
class Lecture:
def save(self):
for x in self.subgroups:
if int(x) > 2:
self.subgroups.remove(x)
if not '2' in self.subgroups:
self.subgroups.append(2)
return '<lecture subject="%s" lector="%s" room="%s" start="%s" end="%s" mode="%s" weeks="%s" subgroups="%s"/>' % (
self.subject,
self.lector,
self.room,
self.start,
self.end,
self.mode,
str(self.weeks),
str(self.subgroups))
class Day:
def __init__(self):
self.lectures = []
def save(self):
return '<day index="%i">%s</day>' % (self.index, ''.join([x.save() for x in self.lectures]))
class Week:
def __init__(self):
self.days = []
def save(self):
return '<week group="%s" subg="%s" week="%s">%s</week>' % (self.group, self.subgroup, self.week, ''.join([x.save() for x in self.days]))
def get(x, y):
return sheet.cell_value(y, x)
def merged_origin(x, y):
for i in range(0, x+4):
if len(get(i, y)) > 100:
return i, y
return None
def parse_lecture(x,y):
if merged_origin(x,y) is None:
return parse_lecture_simple(x,y)
else:
return parse_lecture_merged(*merged_origin(x, y))
def filter_lector(s):
bl = 'ЙЦУКЕНГШЩЗХЪФЫВАПРОЛДЖЭЯЧСМИТЬБЮ'
return ' '.join(filter(lambda x: x[0] in bl, s.split()))
def parse_lecture_simple(x, y):
l = Lecture()
l.subject = get(x+3, y)
l.mode = get(x+4, y)
l.room = get(x+5, y)
l.lector = filter_lector(get(x+6, y))
l.start, l.end = get(x+1, y).split('-')
l.weeks = [1, 2, 3, 4]
if get(x,y) != '':
l.weeks = [int(w.strip()) for w in get(x,y).split(',')]
l.subgroups = [1, 2]
if get(x+2, y) != '':
l.subgroups = [int(w.strip()) for w in get(x+2, y).split()]
return l
def parse_lecture_merged(x, y):
data = get(x,y)
n = ''
while len(data) > 0:
if re.match(' .', data):
data = data[3:]
else:
n += data[0]
data = data[1:]
data = n.strip().replace(' ', ' ')
l = Lecture()
l.weeks = [1, 2, 3, 4]
if get(x-3,y) != '':
l.weeks = [int(w.strip()) for w in get(x-3,y).split(',')]
l.start, l.end = get(x-2, y).split('-')
l.subgroups = [1, 2]
data = data.split()
if len(data) == 4:
l.subject = ' '.join(data[0:2])
l.mode = ''
l.room = ''
l.lector = ''
if 'ауд.' in data:
l.room = data[data.index('ауд.')+1]
else:
l.subject = data[0]
l.mode = data[1]
l.room = data[3]
l.lector = filter_lector(' '.join(data[4:]))
return l
def parse_day(x, y, h, idx):
d = Day()
d.index = idx
for r in range(y, h):
if get(x + 3, r) != '' or (merged_origin(x+3, r) is not None):
try:
d.lectures.append(parse_lecture(x, r))
except:
pass
return d
def parse_week(x, y):
r = y
last_hour = 23
start = y
day = 0
week = Week()
while r < sheet.nrows:
if get(x+1, r) != '' or (merged_origin(x+1,r) is not None):
if merged_origin(x+1,r) is not None:
xx, yy = merged_origin(x+1,r)
hr = int(get(xx - 2, yy).split(':')[0])
else:
hr = int(get(x+1,r).split(':')[0])
if hr < last_hour:
if start != r:
week.days.append(parse_day(x, start, r, day))
day += 1
start = r
last_hour = hr
r += 1
week.days.append(parse_day(x, start, r, day))
return week
# Load sheet
book = open_workbook(sys.argv[1], formatting_info=True, encoding_override='cp1251')
sheet = book.sheet_by_index(0)
# Find groups
idx = 0
for i in range(1, sheet.ncols):
cv = get(i, 3)
if cv != '':
id = cv.split()[-1]
print 'Parsing group %s' % id
week = parse_week(1+idx*7, 5)
# Split in four
for wd in range(1,5):
# Split in two
for x in range(1,3):
nw = Week()
nw.week = str(wd)
nw.group = id
nw.subgroup = str(x)
for d in week.days:
nd = Day()
nd.index = d.index
nw.days.append(nd)
for l in d.lectures:
if (wd in l.weeks) and (x in l.subgroups):
nd.lectures.append(l)
open('timetable/%s-%i-%i.xml'%(id,x,wd), 'w').write(nw.save())
idx += 1