/
project_gen.py
303 lines (274 loc) · 7.96 KB
/
project_gen.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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import xml.etree.ElementTree as ET
from xml.parsers import expat
from state import StateDia
import datetime
import gantt
class ProjectParser:
def __init__(self, filename):
self.root = ET.parse(filename).getroot()
self.name = self.root.find("./name").text
self.code = self.root.find("./code").text
self.espd_code = self.root.find("./espd_code").text
self.lider = self.root.find("./lider").text
class PeopleParser:
def __init__(self, filename):
self.root = ET.parse(filename).getroot()
def get(self, id):
people = self.root.find("./people[@id='%s']" % id)
if people == None:
return None
res = {}
res["fullname"] = people.find("fullname").text
res["post"] = people.find("post").text
res["division"] = people.find("division").text
res["subdivision"] = people.find("subdivision").text
return res
def idarray(self):
peoples = self.root.findall("./people")
for people in peoples:
yield people.attrib["id"]
class PlanEntity:
def __init__(self):
self.id = ""
self.name = ""
self.description = []
self.result = []
self.people = []
self.starttime = ""
self.endtime = ""
self.dfrom = []
self.dto = []
self.note = ""
self.history = []
class PlanEntityLink:
def __init__(self):
self.name = ""
self.direction = 0
class PlanEntityHistory:
def __init__(self):
self.text = ""
self.date = None
class PlanXMLParser:
def __init__(self, filename):
self.es = StateDia()
self.xml = ET.parse(filename).getroot()
self.entityes = []
self.parse()
def parse(self):
entityes = self.xml.findall("./entity")
for entity in entityes:
new_e = PlanEntity()
new_e.id = entity.attrib["id"]
new_e.name = entity.find("name").text
ds = entity.findall("./description")
for d in ds:
new_e.description.append(d.text)
ds = entity.findall("./result")
for d in ds:
new_e.result.append(d.text)
ds = entity.findall("./people")
for d in ds:
new_e.people.append(d.text)
try:
str_starttime = entity.find("./starttime").text
str_endtime = entity.find("./endtime").text
starttime = datetime.datetime.strptime(str_starttime, "%d.%m.%y").date()
endtime = datetime.datetime.strptime(str_endtime, "%d.%m.%y").date()
except:
new_e.starttime = None
new_e.endtime = None
else:
new_e.starttime = starttime
new_e.endtime = endtime
try:
new_e.note = entity.find("./note").text
except:
pass
history = entity.findall("./history")
for d in history:
a = PlanEntityHistory()
a.text = d.text
a.date = datetime.datetime.strptime( d.attrib.get("date", "0") , "%d.%m.%y").date()
new_e.history.append(a)
froms = entity.findall("./from")
for d in froms:
a = PlanEntityLink()
a.name = d.text
a.direction = int(d.attrib.get("direction", "0"))
new_e.dfrom.append(a)
toes = entity.findall("./to")
for d in toes:
a = PlanEntityLink()
a.name = d.text
a.direction = int(d.attrib.get("direction", "0"))
new_e.dto.append(a)
self.entityes.append(new_e)
class StateDiaGen:
def __init__(self, plan):
self.es = StateDia()
self.plan = plan
self.entityes = plan.entityes
def gen(self):
for entity in self.entityes:
id = entity.id
name = entity.name
e = self.es.entity(id, name)
ds = entity.description
for d in ds:
e.add_desctiption(d)
ds = entity.result
for d in ds:
e.add_desctiption(u"Результат: %s" % d)
ds = entity.people
for d in ds:
e.add_desctiption(u"Исполнители: %s" % d)
starttime = entity.starttime
endtime = entity.endtime
if starttime != None :
e.add_desctiption(u"Срок выполнения: %s - %s" % (starttime, endtime))
ds = entity.history
if len(ds) > 0 :
e.add_desctiption(u"История выполнения:" )
for d in ds:
e.add_desctiption(u"%s: %s" % (d.date, d.text))
if entity.note != "":
e.note = entity.note
for entity in self.entityes:
id = entity.id
froms = entity.dfrom
for d in froms:
l = self.es.link(d.name, id)
if l == None:
print "ERROR: Can't create link on '%s'" % entity.id
continue
l.direction = d.direction
toes = entity.dto
for d in toes:
l = self.es.link(id, d.name)
if l == None:
print "ERROR: Can't create link on '%s'" % entity.id
continue
l.direction = d.direction
def output(self):
self.gen()
return self.es.plantuml().encode("utf8")
def outfile(self, filename):
f = open(filename, "w")
f.write(self.output())
f.close()
class PlanCSVGen:
def __init__(self, plan, peoples, project):
self.es = StateDia()
self.plan = plan
self.entityes = plan.entityes
self.peoples = peoples
def gen(self):
s = ""
s += u"Отдел|Подразделение|Шифр проекта|Описание работ|Дата начала|Дата окончания|Исполнитель|Куратор|Результаты выполнения|Примечание\n"
for entity in self.entityes:
if len(entity.people) == 0:
print "WARINIG no people!!!"
continue
people_id = entity.people[0]
people = self.peoples.get(people_id)
if people == None:
print "WARINIG no %s in reest !!!" % (people_id)
continue
entity_peoples = ""
ds = entity.people
for d in ds:
entity_peoples += "%s, " % d
entity_history = ""
ds = entity.history
for d in ds:
entity_history += "%s: %s\n" % (d.date, d.text)
description = reduce(lambda res, x: "%s\n%s" % (res, x), entity.description, "")
s += "\"%s\"|\"%s\"|\"%s\"|\"%s\"|\"%s\"|\"%s\"|\"%s\"|\"%s\"|\"%s\"|\"%s\"\n" % (
people["division"],
people["subdivision"],
project.name,
entity.name,
entity.starttime,
entity.endtime,
entity_peoples,
project.lider,
entity_history,
"%s\n%s" % (description, entity.note)
)
return s
def output(self):
return self.gen().encode("utf8")
def outfile(self, filename):
f = open(filename, "w")
f.write(self.output())
f.close()
class GanttGen:
def __init__(self, plan, peoples, project):
self.es = StateDia()
self.plan = plan
self.entityes = plan.entityes
self.peoples = peoples
self.project = project
self.gproject = None
self.gen()
def gen(self):
# Change font default
gantt.define_font_attributes(fill='black',
stroke='black',
stroke_width=0,
font_family="Verdana")
self.gproject = gantt.Project(name=self.project.name)
resources = {}
for pid in self.peoples.idarray():
resources[pid] = gantt.Resource(pid)
for entity in self.entityes:
resarray = []
for people in entity.people:
resarray.append(resources[people])
task = gantt.Task(name=entity.id,
fullname=entity.name,
start=entity.starttime,
stop=entity.endtime,
#duration=(entity.endtime - entity.starttime).days,
resources=resarray)
self.gproject.add_task(task)
tasks = self.gproject.get_tasks()
for entity in self.entityes:
task = None
for t in tasks:
if t.name == entity.id:
task = t
break
froms = entity.dfrom
for d in froms:
for t in tasks:
if t.name == d.name:
task.add_depends([t])
break
return self.gproject
def outtasks(self, filename):
self.gproject.make_svg_for_tasks(filename=filename,
today=datetime.date.today(),
scale=gantt.DRAW_WITH_WEEKLY_SCALE)
def outresources(self, filename):
self.gproject.make_svg_for_resources(filename=filename,
today=datetime.date.today(),
scale=gantt.DRAW_WITH_WEEKLY_SCALE)
def outcsv(self, filename):
self.gproject.csv(filename)
project = ProjectParser("project.xml")
people = PeopleParser("people.xml")
plan = PlanXMLParser("state.xml")
state_dia = StateDiaGen(plan)
#parser.es.debug_print()
#print state_dia.output()
plancsv = PlanCSVGen(plan, people, project)
#print plancsv.gen()
ganttgen = GanttGen(plan, people, project)
state_dia.outfile("out_statedia.txt")
plancsv.outfile("out_plan.csv")
ganttgen.outtasks("out_gantt.svg")
ganttgen.outresources("out_resources.svg")
ganttgen.outcsv("out_csv.csv")