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