· 6 years ago · Dec 19, 2019, 05:36 PM
1import os, xml.dom.minidom
2import sqlite3 as db
3
4emptydb = """
5PRAGMA foreign_keys = ON;
6
7create table if not exists Vidstanka
8(code integer primary key,
9 country text,
10 year integer,
11 mark text);
12
13create table if not exists Vidremonta
14(code integer primary key,
15 name text,
16 time integer,
17 cost integer,
18 special text);
19
20create table if not exists Remont
21(code integer primary key,
22 vidstanka integer references Vidstanka(code) on update cascade on delete set null,
23 data text,
24 vidremonta integer references Vidremonta(code) on update cascade on delete set null,
25 speciall text);
26"""
27
28class datasql:
29 def read(self,inp,hos):
30 conn = db.connect(inp)
31 curs = conn.cursor()
32 curs.execute('select code,country,year,mark from Vidstanka')
33 data=curs.fetchall()
34 for r in data:hos.newVidstanka(r[0],r[1],r[2],r[3])
35 curs.execute('select code,name,time,cost,special from Vidremonta')
36 data=curs.fetchall()
37 for r in data:hos.newVidremonta(r[0],r[1],r[2],r[3],r[4])
38 curs.execute('select code,vidstanka,data,vidremonta,speciall from Remont')
39 data=curs.fetchall()
40 for r in data:hos.newRemont(r[0],hos.findVidstankaByCode(int(r[1])),r[2],hos.findVidremontaByCode(int(r[3])),r[4])
41 conn.close()
42
43
44 def write(self,out,hos):
45 conn = db.connect(out)
46 curs = conn.cursor()
47 curs.executescript(emptydb)
48 for c in hos.getVidstankaCodes():
49 curs.execute("insert into Vidstanka(code,country,year,mark) values('%s','%s','%s','%s')"%(
50 str(c),
51 hos.getVidstankaCountry(c),
52 str(hos.getVidstankaYear(c)),
53 hos.getVidstankaMark(c)))
54 for c in hos.getVidremontaCodes():
55 curs.execute("insert into Vidremonta(code,name,time,cost,special) values('%s','%s','%s','%s','%s')"%(
56 str(c),
57 hos.getVidremontaName(c),
58 str(hos.getVidremontaTime(c)),
59 str(hos.getVidremontaCost(c)),
60 hos.getVidremontaSpecial(c)))
61 for c in hos.getRemontCodes():
62 curs.execute("insert into Remot(code,vidstanka,data,vidremonta,speciall) values('%s','%s','%s','%s','%s')"%(
63 str(c),
64 str(hos.getRemontVidstankaCode(c)),
65 str(hos.getRemontData(c)),
66 str(hos.getRemontVidremontaCode(c)),
67 hos.getRemontSpeciall(c)))
68
69 conn.commit()
70 conn.close()
71#GENERAL
72class general:
73 def __init__(self,code=0):
74 self.setCode(code)
75 def setCode(self,value): self.__code=value
76 def getCode(self):return self.__code
77#GENERALLIST
78class generallist :
79 def __init__(self):self.__list = []
80 def clear(self): self.__list = []
81 def findByCode(self, code):
82 for l in self.__list:
83 if l.getCode() == code:
84 return l
85 break
86 def getCodes(self) : return [s.getCode() for s in self.__list]
87 def appendList (self,value):self.__list.append(value)
88 def removeList(self,code):
89 for s in self.__list:
90 if s.getCode()==code:self.__list.remove(s)
91#GENERALLISTEDIT
92class generallistEdit(generallist):
93 def getNewCode(self):
94 m=0
95 for c in self.getCodes():
96 if c>m:m=c
97 return m+1
98 #def setName(self,code,value):return self.findByCode(code).setName(value)
99
100class Vidstanka(general):
101 def __init__(self,country='',year=0,mark='',code=0):
102 self.setCountry(country)
103 self.setYear(year)
104 self.setMark(mark)
105 self.setCode(code)
106 def setCountry(self,value):self.__country=value
107 def setYear(self,value):self.__year=value
108 def setMark(self,value):self.__mark=value
109 def setCode(self,value):self.__code=value
110 def getCountry(self):return self.__country
111 def getYear(self):return self.__year
112 def getMark(self):return self.__mark
113 def getCode(self):return self.__code
114
115class VidstankalistEdit(generallistEdit):
116 def newRec (self,country='',year=0,mark='',code=0):
117 self.appendList(Vidstanka(country,year,mark,code))
118 def setCountry(self,code,value):self.findByCode(code).setCountry(value)
119 def setYear(self,code,value):self.findByCode(code).setYear(value)
120 def setMark(self,code,value):self.findByCode(code).setMark(value)
121 def getCountry(self,code):return self.findByCode(code).getCountry()
122 def getYear(self,code):return self.findByCode(code).getYear()
123 def getMark(self,code):return self.findByCode(code).getMark()
124
125class Vidremonta(general):
126 def __init__(self,name='',time=0,cost=0,special='',code=0):
127 self.setName(name)
128 self.setTime(time)
129 self.setCost(cost)
130 self.setSpecial(special)
131 self.setCode(code)
132 def setName(self,value):self.__name=value
133 def setTime(self,value):self.__time=value
134 def setCost(self,value):self.__cost=value
135 def setSpecial(self,value):self.__special=value
136 def setCode(self,value):self.__code=value
137 def getName(self):return self.__name
138 def getTime(self):return self.__time
139 def getCost(self):return self.__cost
140 def getSpecial(self):return self.__special
141 def getCode(self):return self.__code
142
143class VidremontalistEdit(generallistEdit):
144 def newRec (self,name='',time=0,cost=0,special='',code=0):
145 self.appendList(Vidremonta(name,time,cost,special,code))
146 def setName(self,code,value):self.findByCode(code).setName(value)
147 def setTime(self,code,value):self.findByCode(code).setTime(value)
148 def setCost(self,code,value):self.findByCode(code).setCost(value)
149 def setSpecial(self,code,value):self.findByCode(code).setSpecial(value)
150 def getName(self,code):return self.findByCode(code).getName()
151 def getTime(self,code):return self.findByCode(code).getTime()
152 def getCost(self,code):return self.findByCode(code).getCost()
153 def getSpecial(self,code):return self.findByCode(code).getSpecial()
154
155class Remont(general):
156 def __init__(self,vidstanka=None,data='',vidremonta=None,speciall='',code=0):
157 self.setVidstanka(vidstanka)
158 self.setData(data)
159 self.setVidremonta(vidremonta)
160 self.setSpeciall(speciall)
161 self.setCode(code)
162 def setVidstanka(self,value):self.__vidstanka=value
163 def setData(self,value):self.__data=value
164 def setVidremonta(self,value):self.__vidremonta=value
165 def setSpeciall(self,value):self.__speciall=value
166 def setCode(self,value):self.__code=value
167 def getVidstanka(self):return self.__vidstanka
168 def getVidstankaCode(self): return self.__vidstanka.getCode()
169 def getData(self):return self.__data
170 def getVidremonta(self):return self.__vidremonta
171 def getVidremontaCode(self): return self.__vidremonta.getCode()
172 def getSpeciall(self):return self.__speciall
173 def getCode(self):return self.__code
174 def info(self):
175 s='%s %s %s %s %s %s %s %s %s %s %s %s'%(self.__vidstanka.getCountry(),self.__vidstanka.getYear(),self.__vidstanka.getMark(),self.__vidstanka.getCode(),self.__vidremonta.getName(),self.__vidremonta.getTime(),self.__vidremonta.getCost(),self.__vidremonta.getSpecial(),self.__vidremonta.getCode(),self.getData(),self.getSpeciall(),self.getCode())
176 return(s)
177
178class RemontlistEdit(generallistEdit):
179 def newRec (self,vidstanka=None,data='',vidremonta=None,speciall='',code=0):
180 self.appendList(Remont(vidstanka,data,vidremonta,speciall,code))
181 def setVidstanka(self,code,value):self.findByCode(code).setVidstanka(value)
182 def setData(self,code,value):self.findByCode(code).setData(value)
183 def setVidremonta(self,code,value):self.findByCode(code).setVidremonta(value)
184 def setSpeciall(self,code,value):self.findByCode(code).setSpeciall(value)
185 def getVidstankaCountry(self,code): return self.findByCode(code).getVidstankaCountry()
186 def getVidstankaYear(self, code): return self.findByCode(code).getVidstankaYear()
187 def getVidstankaMark(self, code): return self.findByCode(code).getVidstankaMark()
188 def getVidstankaCode(self, code): return self.findByCode(code).getVidstankaCode()
189 def getData(self,code):return self.findByCode(code).getData()
190 def getVidremontaName(self, code): return self.findByCode(code).getVidremontaName()
191 def getVidremontaTime(self, code): return self.findByCode(code).getVidremontaTime()
192 def getVidremontaCost(self, code): return self.findByCode(code).getVidremontaCost()
193 def getVidremontaSpecial(self, code): return self.findByCode(code).getVidremontaSpecial()
194 def getVidremontaCode(self, code): return self.findByCode(code).getVidremontaCode()
195 def getSpeciall(self,code):return self.findByCode(code).getSpeciall()
196
197class Remontstankov :
198 def __init__(self):
199 self.__Vidstanka=VidstankalistEdit()
200 self.__Vidremonta=VidremontalistEdit()
201 self.__Remont=RemontlistEdit()
202 def removeVidstanka(self,code):
203 b = True
204 for c in self.getRemontCodes():
205 if self.getRemontVidstankaCode(c) == code:
206 b = False
207 break
208 if b:
209 self.__Vidstanka.removeList(code)
210
211 def removeVidremonta(self,code):
212 d = True
213 for e in self.getRemontCodes():
214 if self.getRemontVidremontaCode(e) == code:
215 d = False
216 break
217 if d:
218 self.__Vidremonta.removeList(code)
219 # def removeVidstanka(self,code):
220 # self.__Vidstanka.removeList(self,code)
221 # for c in self.__Remont.getCodes():
222 # self.__Remont.removeVidstanka(c,code)
223 # def removeVidremonta(self,code):
224 # self.__Vidremonta.removeList(self,code)
225 # for c in self.__Remont.getCodes():
226 # self.__Remont.removeVidremonta(c,code)
227
228 def clear(self):
229 self.__Vidstanka.clear()
230 self.__Vidremonta.clear()
231 self.__Remont.clear()
232
233
234 def newVidstanka(self,country='',year=0,mark='',code=0):
235 self.__Vidstanka.newRec(country,year,mark,code)
236 def FindVidstankaByCode(self,code):return self.__Vidstanka.findByCode(code)
237 def setVidstankaCountry(self,code,value): self.__Vidstanka.setCountry(code, value)
238 def setVidstankaYear(self,code,value): self.__Vidstanka.setYear(code, value)
239 def setVidstankaMark(self,code,value): self.__Vidstanka.setMark(code, value)
240 def getVidstankaNewCode(self): return self.__Vidstanka.getNewCode()
241 def getVidstankaCodes(self): return self.__Vidstanka.getCodes()
242 def getVidstankaCountry(self,code): return self.__Vidstanka.getCountry(code)
243 def getVidstankaYear(self,code): return self.__Vidstanka.getYear(code)
244 def getVidstankaMark(self,code): return self.__Vidstanka.getMark(code)
245
246 def newVidremonta(self,name='',time=0,cost=0,special='',code=0):
247 self.__Vidremonta.newRec(name,time,cost,special,code)
248 def FindVidremontaByCode(self,code):return self.__Vidremonta.findByCode(code)
249 def setVidremontaName(self,code,value): self.__Vidremonta.setName(code, value)
250 def setVidremontaTime(self,code,value): self.__Vidremonta.setTime(code, value)
251 def setVidremontaCost(self,code,value): self.__Vidremonta.setCost(code, value)
252 def setVidremontaSpecial(self,code,value): self.__Vidremonta.setSpecial(code, value)
253 def getVidremontaNewCode(self): return self.__Vidremonta.getNewCode()
254 def getVidremontaCodes(self): return self.__Vidremonta.getCodes()
255 def getVidremontaName(self,code): return self.__Vidremonta.getName(code)
256 def getVidremontaTime(self,code): return self.__Vidremonta.getTime(code)
257 def getVidremontaCost(self,code): return self.__Vidremonta.getCost(code)
258 def getVidremontaSpecial(self,code): return self.__Vidremonta.getSpecial(code)
259
260 def removeRemont(self,code):
261 self.__Remont.removeList(code)
262 def newRemont(self,vidstanka=None,data='',vidremonta=None,speciall='',code=0):
263 self.__Remont.newRec(vidstanka,data,vidremonta,speciall,code)
264 def FindRemontByCode(self,code):return self.__Remont.findbycode(code)
265
266 def setRemontVidstanka(self,code,dcode): self.__Remont.setVidstanka(code, self.findVidstankaByCode(dcode))
267 def setRemontData(self,code,value): self.__Remont.setData(code, value)
268 def setRemontVidremonta(self,code,pcode): self.__Remont.setVidremonta(code, self.findVidremontaByCode(pcode))
269 def setRemontSpeciall(self,code,value): self.__Remont.setSpeciall(code, value)
270
271 def getRemontCodes(self): return self.__Remont.getCodes()
272 def getRemontNewCodes(self): return self.__Remont.getNewCodes()
273 def getRemontData(self,code): return self.__Remont.getData(code)
274 def getRemontSpeciall(self,code): return self.__Remont.getSpeciall(code)
275 def getRemontVidstankaCode(self, code): return self.__Remont.getVidstankaCode(code)
276 def getRemontVidstankaCountry(self, code): return self.__Remont.getVidstankaCountry(code)
277 def getRemontVidstankaYear(self, code): return self.__Remont.getVidstankaYear(code)
278 def getRemontVidstankaMark(self, code): return self.__Remont.getVidstankaMark(code)
279 def getRemontVidremontaCode(self, code): return self.__Remont.getVidremontaCode(code)
280 def getRemontVidremontaName(self, code): return self.__Remont.getVidremontaName(code)
281 def getRemontVidremontaTime(self, code): return self.__Remont.getVidremontaTime(code)
282 def getRemontVidremontaCost(self, code): return self.__Remont.getVidremontaCost(code)
283 def getRemontVidremontaSpecial(self, code): return self.__Remont.getVidremontaSpecial(code)
284 def RemontInfo(self, code): return self.__Remont.info(code)
285
286
287class dataxml:
288
289 def read(self, inp, hos):
290 dom = xml.dom.minidom.parse(inp)
291 dom.normalize()
292 for node in dom.childNodes[0].childNodes:
293 if (node.nodeType == node.ELEMENT_NODE) and (node.nodeName == 'Vidstanka'):
294 country,year,mark,code ='',0,'',0
295 for t in node.attributes.items():
296 if t[0] == "country": country = t[1]
297 if t[0] == "year": year = int(t[1])
298 if t[0] == "mark": mark = t[1]
299 if t[0] == "code": code = int(t[1])
300 hos.newVidstanka(country,year,mark,code)
301 if (node.nodeType == node.ELEMENT_NODE) and (node.nodeName == 'Vidremonta'):
302 name,time,cost,special,code ='',0,0,'',0
303 for t in node.attributes.items():
304 if t[0] == "name": name = t[1]
305 if t[0] == "time": time = int(t[1])
306 if t[0] == "cost": cost = int(t[1])
307 if t[0] == "special": special = t[1]
308 if t[0] == "code": code = int(t[1])
309 hos.newVidremonta(name,time,cost,special,code)
310 if (node.nodeType == node.ELEMENT_NODE) and (node.nodeName == 'Remont'):
311 vidstanka,data,vidremonta,speciall,code =None,'',None,'',0
312 for t in node.attributes.items():
313 if t[0] == "vidstanka": vidstanka = hos.FindVidstankaByCode(int(t[1]))
314 if t[0] == "data": data = t[1]
315 if t[0] == "vidremonta": vidremonta = hos.FindVidremontaByCode(int(t[1]))
316 if t[0] == "speciall": speciall = t[1]
317 if t[0] == "code": code = int(t[1])
318 hos.newRemont(vidstanka,data,vidremonta,speciall,code)
319 #for n in node.childNodes:
320 #if (n.nodeType==n.ELEMENT_NODE) and (n.nodeName=='vidstanka'):
321 #for t in n.attributes.items():
322 #if t[0]=="code":vidstanka=hos.FindVidstankaByCode(int(t[1]))
323 #hos.appendRemontVidstanka(code,Vidstanka)
324 #for n in node.childNodes:
325 #if (n.nodeType==n.ELEMENT_NODE) and (n.nodeName=='vidremonta'):
326 #for t in n.attributes.items():
327 #if t[0]=="code":vidremonta=hos.FindVidremontaByCode(int(t[1]))
328 #hos.appendRemontVidremonta(code,Vidremonta)
329
330
331 def write(self, out, hos):
332 dom = xml.dom.minidom.Document()
333 root = dom.createElement("Remontstankov")
334 dom.appendChild(root)
335 for c in hos.getVidstankaCodes():
336 vsk = dom.createElement("Vidstanka")
337 vsk.setAttribute('country', hos.getVidstankaCountry(c))
338 vsk.setAttribute('year', str(hos.getVidstankaYear(c)))
339 vsk.setAttribute('mark', hos.getVidstankaMark(c))
340 vsk.setAttribute('code', str(c))
341 root.appendChild(vsk)
342 for c in hos.getVidremontaCodes():
343 vrm = dom.createElement("Vidremonta")
344 vrm.setAttribute('name', hos.getVidremontaName(c))
345 vrm.setAttribute('time', str(hos.getVidremontaTime(c)))
346 vrm.setAttribute('cost', str(hos.getVidremontaCost(c)))
347 vrm.setAttribute('special', hos.getVidremontaSpecial(c))
348 vrm.setAttribute('code', str(c))
349 root.appendChild(vrm)
350 for c in hos.getRemontCodes():
351 rem = dom.createElement("Remont")
352 rem.setAttribute('vidstanka', str(hos.getRemontVidstankaCode(c)))
353 rem.setAttribute('data', str(hos.getRemontData(c)))
354 rem.setAttribute('vidremonta', str(hos.getRemontVidremontaCode(c)))
355 rem.setAttribute('speciall', hos.getRemontSpeciall(c))
356 rem.setAttribute('code', str(c))
357 root.appendChild(rem)
358 f = open(out, "w")
359 f.write(dom.toprettyxml())
360vs1=Vidstanka('Russia',2000,'Bosh',0)
361vr1=Vidremonta('Top342',2,40,'brokenbutton',0)
362r1=Remont(vs1,'20072019',vr1,'brokenbutton1',0)
363print(r1.info())
364rem1 = Remontstankov()
365dat1 = dataxml()
366dat1.read('old.xml',rem1)
367dat1=datasql()
368dat1.write('new.sqlite',rem1)
369dat1=datasql()
370dat1.read('new.sqlite',rem1)
371dat1 = dataxml()
372dat1.write('new.xml',rem1)
373#rem1.newVidstanka('Russia',2000,'Bosh',0)
374#rem1.newVidremonta('Top342',2,40,'brokenbutton',0)
375#rem1.newRemont(rem1.FindVidstankaByCode(0),'20072019',rem1.FindVidremontaByCode(0),'brokenbutton1',0)
376#rem1.removeRemont(1)
377#rem1.removeVidstanka(1)
378#rem1.removeVidremonta(1)
379 #<Remontstankov>
380 #<Vidstanka country = 'Russia' year= '2000' mark = 'Bosh' code= '1' />
381
382
383 #<Vidremonta name = 'Top342' time = '2' cost = '40' special = 'brokenbutton' code= '1'/>
384
385
386 #<Remont vidstanka= '1' data = '20.07.2019' vidremonta = '1' speciall = 'brokenbutton1' code = '1'>
387 #</Remont>
388
389
390 #</Remontstankov>