-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.py
729 lines (680 loc) · 33.1 KB
/
db.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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
#!/usr/bin/python3
#Δημιουργήθηκε από τον Xoristzatziki στο el.wiktionary.org
#2017
import os, sys
import sqlite3
import inspect
#try:
#from . import auxiliary
#except:
#import auxiliary
#myprint = auxiliary.myprint
#getnonZtime = auxiliary.getnonZtime
def getnonZtime(whichZtime):
'''Return a TS in format YYYYMMDDHHMMSS'''
if 'Z' in whichZtime:
return whichZtime[:4] + whichZtime[5:7] + whichZtime[8:10] + whichZtime[11:13] + whichZtime[14:16] + whichZtime[17:19]
else:
#return as is, not a Z time
return whichZtime
class DBRow:
def __init__(self,title,ns,start,charlen,timestamp,content,isredirect):
self.title = title
self.ns = ns
self.start = start
self.charlen = charlen
self.timestamp = timestamp
self.content = content
self.isredirect = isredirect
#TODO?: if worth add these?
#self.id = id
#self.username = username.strip()
#self.ipedit = ipedit
#self.revisionid = revisionid
#self.comment = comment
class DB:
'''Encapsulate all comunication with the DB.'''
def __enter__(self):
return self
def __exit__(self, t, v, tb):
if self.myconn != None:
self.myconn.close()
if self.txtfile != None:
self.txtfile.close()
return True
def __str__(self):
if self.myconn:
output = 'connected to:' + self.dbfilename + ' and isolation_level:' + 'None' if self.myconn.isolation_level == None else self.myconn.isolation_level
else:
output = 'NOT connected to:' + self.dbfilename
return output
def __repr__(self):
if self.myconn:
output = 'connected to:' + self.dbfilename + ' and isolation_level:' + 'None' if self.myconn.isolation_level == None else self.myconn.isolation_level
else:
output = 'NOT connected to:' + self.dbfilename
return "<"+self.__module__+'.'+self.__class__.__name__+" "+repr(output)+">"
def __init__(self, dbfile, txtfile = None):
self.dbfilename = os.path.abspath(dbfile)
if txtfile:
self.txtfilename = os.path.abspath(txtfile)
else:
self.txtfilename = dbfile[:-len('.db')] + '.txt'
self.myconn = None
self.txtfile = None
#myprint( inspect.stack()[0][3],'self.txtfilename',self.txtfilename)
if os.path.exists(self.dbfilename):
self.myconn = sqlite3.connect(self.dbfilename, isolation_level = None)
self.myconn.row_factory = sqlite3.Row
#print('connected')
if os.path.exists(self.txtfilename):
#myprint( inspect.stack()[0][3],'file txt exists')
self.txtfile = open(self.txtfilename,'r+t', encoding="utf-8")
#myprint( inspect.stack()[0][3],'file txt opened')
def test(self):
print('DB TEST')
def createAnEmptyDB(self):
try:
if os.path.exists(self.dbfilename):
os.remove(self.dbfilename)
print('old db deleted')
time.sleep(2)
with sqlite3.connect(self.dbfilename, isolation_level = 'EXCLUSIVE' ) as conn:
#conn = sqlite3.connect(dbfilename)#, isolation_level = 'EXCLUSIVE' )
conn.execute('''CREATE TABLE maintbl
(lemma text NOT NULL,
lns INTEGER, lredirect INTEGER, lstart INTEGER, lcharlen INTEGER, ltimestamp text);''')
conn.commit()
conn.execute('''CREATE TABLE attribstbl
(atimestamp text NOT NULL, adumpTS text NOT NULL, asiteurl text NOT NULL, asitenses text NOT NULL);''')
conn.commit()
#conn.close()
#myprint(inspect.stack()[0][3], self.dbfilename)
return True
except Exception as e:
print(inspect.stack()[0][3],e)
return False
def fillemptydb(self, listForDB, theTS, siteurl, sitenses):
print('fiiling empty db')
try:
self.myconn.close()
#print('connection closed 1')
self.myconn = sqlite3.connect(self.dbfilename, isolation_level = 'EXCLUSIVE')
#print('new connection opened EXCLUSIVE 1')
#print(self.myconn)
#print(self)
#self.myconn.execute('''INSERT INTO maintbl(lemma, lns, lredirect, lstart, lcharlen, ltimestamp) VALUES (?,?,?,?,?,?);''', listForDB[0])
#self.myconn.commit()
#print(listForDB[0])
#xcounter = 0
#for arecord in listForDB:
#xcounter += 1
#if xcounter % 10000 == 0:
#self.myconn.commit()
#self.myconn.execute("begin")
#print('commited 100', xcounter)
#self.myconn.execute('''INSERT INTO maintbl(lemma, lns, lredirect, lstart, lcharlen, ltimestamp) VALUES (?,?,?,?,?,?);''', arecord)
#affected = self.myconn.executemany('''INSERT INTO maintbl(lemma, lns, lredirect, lstart, lcharlen, ltimestamp) VALUES (?,?,?,?,?,?);''', listForDB).rowcount
self.myconn.executemany('''INSERT INTO maintbl(lemma, lns, lredirect, lstart, lcharlen, ltimestamp) VALUES (?,?,?,?,?,?);''', listForDB)
#self.myconn.commit()
print('all commited')
#print('time:', str(time.time() - starttime), 'affected', str(affected))
#self.myconn.commit()
self.myconn.execute('''CREATE INDEX "lnsndx" on maintbl (lns ASC)''')
#self.myconn.commit()
print( 'ns index created')
self.myconn.execute('''CREATE INDEX "lemmandx" on maintbl (lemma ASC)''')
#self.myconn.commit()
print('lemma index created')
self.myconn.execute('''CREATE INDEX "lTSndx" on maintbl (ltimestamp ASC)''')
#self.myconn.commit()
print('TS index created')
mycursor = self.myconn.execute('''SELECT MAX(ltimestamp) FROM maintbl;''')
arow = mycursor.fetchone()
#print('arow', arow)
#myprint('arow 0', arow[0])
basemaxTS = getnonZtime(arow[0])
#siteurl, sitenses = GetInfoFromDump(dbfullpath)
self.myconn.execute('''INSERT INTO attribstbl(atimestamp, adumpTS, asiteurl, asitenses) VALUES (?,?,?,?);''', (basemaxTS, theTS, siteurl, sitenses))
self.myconn.commit()
#close and reopen because we may be in a with and someone will call after another function
self.myconn.close()
self.myconn = sqlite3.connect(self.dbfilename, isolation_level = None)
self.myconn.row_factory = sqlite3.Row
#myprint(inspect.stack()[0][3],basemaxTS, theTS, siteurl, sitenses)
return True
except self.myconn.Error as e:
print('an error')
print( e)
except Exception as e:
#myprint(inspect.stack()[0][3],e)
#return False
raise e
def updateSiteInfo(self, newbaseTS, siteurl, sitenses):
try:
#myprint('updateSiteInfo')
self.myconn.execute('''UPDATE attribstbl set atimestamp =?, asiteurl=?, asitenses=?;''', (newbaseTS, siteurl, sitenses))
#myprint('updateSiteInfo ok')
except Exception as e:
#myprint('updateSiteInfo failed')
#myprint('Exception in:', inspect.stack()[0][3],e.args)
raise
def updateSiteInfo3(self, sitenses):
print(sitenses)
print('updateSiteInfo3')
def updateSiteInfo2(self, sitenses):
try:
#print('updateSiteInfo2')
mycursor = self.myconn.execute('''SELECT MAX(ltimestamp) FROM maintbl;''')
arow = mycursor.fetchone()
#myprint('arow', arow)
#myprint('arow 0', arow[0])
newbaseTS = getnonZtime(arow[0])
#print(newbaseTS)
#siteurl, sitenses = GetInfoFromDump(dbfullpath)
self.myconn.execute('''UPDATE attribstbl set atimestamp =?, asitenses=?;''', (newbaseTS, sitenses))
#print('updateSiteInfo2 ok')
except Exception as e:
print('updateSiteInfo 2 failed')
#myprint('Exception in:', inspect.stack()[0][3],e.args)
raise e
def getSiteInfo(self):
#myprint(inspect.stack()[0][3],'one')
#myprint('===================', self.myconn.row_factory)
try:
mycursor = self.myconn.cursor()
mycursor.execute('''SELECT * FROM attribstbl;''')
arow = mycursor.fetchone()
#myprint(inspect.stack()[0][3],'got one', arow.keys())
if arow[0] == None:
return False, None
else:
return True, arow
except Exception as e:
#myprint('Exception in:', inspect.stack()[0][3],e.args)
raise
def getSiteURL(self):
try:
mycursor = self.myconn.cursor()
mycursor.execute('''SELECT asiteurl FROM attribstbl;''')
arow = mycursor.fetchone()
#myprint(inspect.stack()[0][3],'got one', arow.keys())
return arow[0]
except Exception as e:
#myprint('Exception in:', inspect.stack()[0][3],e.args)
raise
def getusablespace(self, thisstart):
try:
mycursor = self.myconn.cursor()
mycursor.execute('''SELECT MIN(lstart) FROM maintbl WHERE lstart > ? ;''', ( thisstart,))
arow = mycursor.fetchone()
if arow[0] == None:
mycursor.execute('''SELECT MAX(lstart), lcharlen FROM maintbl;''')
arow = mycursor.fetchone()
if int(arow[0]) == thisstart:
return -1
else:
#myprint(inspect.stack()[0][3],'Start not found in db.')
raise
else:
return int(arow[0]) - thisstart - len('\n')
except Exception as e:
#myprint('Exception in:', inspect.stack()[0][3],e.args)
raise
def deleteWithTransaction(self, listoftitles):
#print('deleteWithTransaction')
try:
self.myconn.close()
self.myconn = sqlite3.connect(self.dbfilename, isolation_level = 'EXCLUSIVE')
self.myconn.execute("begin")
for atitle in listoftitles:
#self.deleteLemma(atitle)
self.myconn.execute('''DELETE FROM maintbl WHERE lemma = ? ;''', (atitle,))
self.myconn.commit()
self.myconn.close()
self.myconn = sqlite3.connect(self.dbfilename, isolation_level = None)
self.myconn.row_factory = sqlite3.Row
#print('deleteWithTransaction END')
except sqlite3.Error as e:
print(e)
raise e
except Exception as e:
print(e)
raise e
def deleteLemma(self, title):
#print('deleteLemma')
try:
#myprint("==================trying to delete:", title)
mycursor = self.myconn.cursor()
mycursor.execute('''SELECT * FROM maintbl WHERE lemma = ? ;''', ( title,))
arow = mycursor.fetchone()
if arow == None:
#print('None found', title)
#print('deleteLemma ---------- None found')
return True
#logit(self.errorlogfile, title + ' not found in db and not deleted.')
#myprint(inspect.stack()[0][3],title + ' not found in db and not deleted.')
#return True
else:
#print('======= found', arow)
#return True
start = int(arow['lstart'])
usablespace = self.getusablespace(int(arow['lstart']))
#first delete the entry then the txt
mycursor.execute('''DELETE FROM maintbl WHERE lemma = ? ;''', (title,))
self.textdelete(start, usablespace)
#print('deleteLemma ---------- DELETED')
return True
except sqlite3.Error as e:
#logit(self.errorlogfile, title + ' raised an error in deletelemma.')
#myprint('title',title)
#myprint('Exception in:', inspect.stack()[0][3],e.args)
print('deleteLemma ---------- ERROR')
raise e
def appendWithTransaction(self, dictwithlemmas):
fields = {}
for pageid in dictwithlemmas:
#print('appendWithTransaction 1',dictwithlemmas[pageid]['title'])
#print("dictwithlemmas[pageid]",dictwithlemmas[pageid])
titlestart = self.textappend(dictwithlemmas[pageid]['revisions'][0]['*'])
fields[dictwithlemmas[pageid]['pageid']] = DBRow(title = dictwithlemmas[pageid]['title'],
ns = dictwithlemmas[pageid]['ns'],
isredirect = 1 if 'redirect' in dictwithlemmas[pageid] else 0,
timestamp = dictwithlemmas[pageid]['revisions'][0]['timestamp'],
start = titlestart,
charlen = len(dictwithlemmas[pageid]['revisions'][0]['*']),#rest are dummy values
content = ''
)
#print('field added')
#{'lemma': dictwithlemmas[pageid]['title'], 'lns': dictwithlemmas[pageid]['ns'],
#'lredirect':1 if 'redirect' in dictwithlemmas[pageid],
#'lstart':titlestart,
#'lcharlen':len(bytes(dictwithlemmas[pageid]['revisions'][0]['*'], 'utf-8')),
#'ltimestamp':dictwithlemmas[pageid]['revisions'][0]['timestamp']}
#lemma, lns, lredirect, lstart, lcharlen, ltimestamp
self.myconn.close()
self.myconn = sqlite3.connect(self.dbfilename, isolation_level = 'EXCLUSIVE')
self.myconn.execute("begin")
#print('started transaction')
for pageid in fields:
self.myconn.execute('''INSERT INTO maintbl(lemma, lns, lredirect, lstart, lcharlen, ltimestamp) VALUES (?,?,?,?,?,?);''',
(fields[pageid].title, fields[pageid].ns, fields[pageid].isredirect,
fields[pageid].start, fields[pageid].charlen, fields[pageid].timestamp))
self.myconn.commit()
self.myconn.close()
self.myconn = sqlite3.connect(self.dbfilename, isolation_level = None)
self.myconn.row_factory = sqlite3.Row
#print('reopend (with isolation_level = None?)', self.myconn.isolation_level)
def appendLemma(self, entry):
#print('appendLemma')
titlestart = self.textappend(entry.content)
if titlestart:
#myprint('insert new lemma in db',entry.title, entry.ns, titlestart, len(entry.content), entry.timestamp)
#print('------------')
self.myconn.execute('''INSERT INTO maintbl(lemma, lns, lredirect, lstart, lcharlen, ltimestamp) VALUES (?,?,?,?,?,?);''',
(entry.title, entry.ns, entry.isredirect, titlestart, len(entry.content), entry.timestamp))
#print('appendLemma END')
return True
else:
print('appendLemma END ------- FALSE')
return False
def updateExistingLemmas(self, dictwithlemmas):
#fields = {}
#try:
self.myconn.close()
self.myconn = sqlite3.connect(self.dbfilename, isolation_level = 'EXCLUSIVE')
self.myconn.execute("begin")
#print('started transaction')
for pageid in dictwithlemmas:
#print(dictwithlemmas[pageid]['title'])
#print('redirect' in dictwithlemmas[pageid])
#print(dictwithlemmas[pageid]['ns'])
#print(dictwithlemmas[pageid]['revisions'][0]['timestamp'])
#print('appendWithTransaction 1',dictwithlemmas[pageid]['title'])
#print("dictwithlemmas[pageid]",dictwithlemmas[pageid])
titlestart = self.textappend(dictwithlemmas[pageid]['revisions'][0]['*'])
#print(titlestart)
self.myconn.execute('''UPDATE maintbl SET lns = ?, lredirect = ?, lstart = ?, lcharlen = ?, ltimestamp = ? WHERE lemma = ?;''',
( dictwithlemmas[pageid]['ns'],
1 if 'redirect' in dictwithlemmas[pageid] else 0,
titlestart,
len(dictwithlemmas[pageid]['revisions'][0]['*']),
dictwithlemmas[pageid]['revisions'][0]['timestamp'],
dictwithlemmas[pageid]['title']))
#fields[dictwithlemmas[pageid]['pageid']] = DBRow(title = dictwithlemmas[pageid]['title'],
#ns = dictwithlemmas[pageid]['ns'],
#isredirect = 1 if 'redirect' in dictwithlemmas[pageid] else 0,
#timestamp = dictwithlemmas[pageid]['revisions'][0]['timestamp'],
#start = titlestart,
#charlen = len(bytes(dictwithlemmas[pageid]['revisions'][0]['*'], 'utf-8')),#rest are dummy values
#content = ''
#)
#print('field added')
#{'lemma': dictwithlemmas[pageid]['title'], 'lns': dictwithlemmas[pageid]['ns'],
#'lredirect':1 if 'redirect' in dictwithlemmas[pageid],
#'lstart':titlestart,
#'lcharlen':len(bytes(dictwithlemmas[pageid]['revisions'][0]['*'], 'utf-8')),
#'ltimestamp':dictwithlemmas[pageid]['revisions'][0]['timestamp']}
#lemma, lns, lredirect, lstart, lcharlen, ltimestamp
#print('started transaction')
#for pageid in fields:
#self.myconn.execute('''UPDATE maintbl SET lns = ?, lredirect = ?, lstart = ?, lcharlen = ?, ltimestamp = ?) WHERE lemma = ?;''',
#( fields[pageid].ns, fields[pageid].isredirect,
#fields[pageid].start, fields[pageid].charlen, fields[pageid].timestamp, fields[pageid].title))
self.myconn.commit()
self.myconn.close()
self.myconn = sqlite3.connect(self.dbfilename, isolation_level = None)
self.myconn.row_factory = sqlite3.Row
#except sqlite3.Error as e:
#print(e)
#raise e
#except Exception as e:
#print(e)
#raise e
def updateExistingLemma(self,newentrywitholddata):
#print('updateExistingLemma')
start = int(newentrywitholddata.start)
#print(' in updatelemma else' , start)
usablespace = self.getusablespace(start)
#print(' in updatelemma usablespace' , usablespace)
byteslength = len(bytes(newentrywitholddata.content, 'utf-8'))
if usablespace < byteslength:#either at end or no room for content
ok = self.textdelete(start, usablespace)
#print('updateExistingLemma textdeleted')
#if not ok:
#myprint()
#return False
newstart = self.textappend(newentrywitholddata.content)
else:#enough space. Do not update start.
ok = self.textinsertnew(newentrywitholddata.content, start, usablespace)
newstart = start
#print('updateExistingLemma text is ok. Try update',newstart)
try:
mycursor = self.myconn.cursor()
mycursor.execute('''UPDATE maintbl SET lstart = ?, lcharlen = ?, ltimestamp =? WHERE lemma = ? ;''',
(newstart, len(newentrywitholddata.content), newentrywitholddata.timestamp, newentrywitholddata.title ))
#print('UPDATE executed with',len(entry.content), entry.timestamp, entry.title)
#print('updateExistingLemma END')
return True
except sqlite3.Error as e:
#myprint('Exception in:', inspect.stack()[0][3],e.args)
#logit(self.errorlogfile, entry.title + ' raised an error in db update. ' + e.args)
print('updateExistingLemma END --------------- ERROR')
raise e
def updateLemma(self, entry):
'''Write new data for an entry.
If entry does not exist append it.
If entry has enough space in the text file use that space. Change timestamp.
If not enough space, delete the old data from text file, and append to end changing start (and len)
'''
#myprint(entry.title, entry.ns, len(entry.content), entry.timestamp,entry.content)
#print(entry.title, entry.ns, len(entry.content), entry.timestamp,entry.content)
#print('updateLemma')
try:
mycursor = self.myconn.cursor()
mycursor.execute('''SELECT * FROM maintbl WHERE lemma = ?;''', ( entry.title , ))
arow = mycursor.fetchone()
#print("arow",arow)
#myprint(' in updatelemma')
if arow == None:
#not found just append it
#print('not found', entry.title, 'will append')
#myprint('arow == None in updatelemma')
titlestart = self.textappend(entry.content)
if titlestart:
#myprint('insert new lemma in db',entry.title, entry.ns, titlestart, len(entry.content), entry.timestamp)
#print('------------')
self.myconn.execute('''INSERT INTO maintbl(lemma, lns, lredirect, lstart, lcharlen, ltimestamp) VALUES (?,?,?,?,?,?);''',
(entry.title, entry.ns, entry.isredirect, titlestart, len(entry.content), entry.timestamp))
#self.myconn.commit()
#myprint('INSERT DONE')
return True
else:
#myprint('text not appended !!!!!!!!!!!!!!!!!!!!!!!!')
#logit(self.errorlogfile, entry.title + ' not appended. Could not append text.')
return False
else:
start = int(arow['lstart'])
#print(' in updatelemma else' , start)
usablespace = self.getusablespace(start)
#print(' in updatelemma usablespace' , usablespace)
byteslength = len(bytes(entry.content, 'utf-8'))
if usablespace < byteslength:#either at end or no room for content
ok = self.textdelete(start, usablespace)
if not ok:
#myprint()
return False
newstart = self.textappend(entry.content)
try:
mycursor.execute('''UPDATE maintbl SET lstart = ?, lcharlen = ?, ltimestamp =? WHERE lemma = ? ;''',
(newstart, len(entry.content), entry.timestamp, entry.title ))
#print('UPDATE executed with',len(entry.content), entry.timestamp, entry.title)
return True
except Exception as e:
#print('Exception as inst in updatelemma',inst.args)
#logit(self.errorlogfile, entry.title + ' raised an error in db update. ' + e.args)
#myprint('Exception in:', inspect.stack()[0][3],e.args)
raise
else:#enough space. Do not update start.
ok = self.textinsertnew(entry.content, start, usablespace)
if ok:
try:
mycursor.execute('''UPDATE maintbl SET lcharlen = ?, ltimestamp =? WHERE lemma = ? ;''',
(len(entry.content), entry.timestamp, entry.title ))
#print('UPDATE executed with',len(entry.content), entry.timestamp, entry.title)
return True
except Exception as e:
#myprint('Exception in:', inspect.stack()[0][3],e.args)
#logit(self.errorlogfile, entry.title + ' raised an error in db update. ' + e.args)
raise
except Exception as e:
#return False
#logit(self.errorlogfile, entry.title + ' not updated. Raised an error.' + e.args)
#myprint(' entry.title :', entry.title )
#myprint('Exception in:', inspect.stack()[0][3],e.args)
raise
def getLemmaContent(self,lemma):
mycursor = self.myconn.cursor()
try:
mycursor.execute('''SELECT * FROM maintbl WHERE lemma = ?;''', (lemma,))
myrow = mycursor.fetchone()
#return (myrow != None), myrow
#myprint('fetched one')
except sqlite3.IntegrityError:
return 'DB IntegrityError ERROR', None
except Exception as e:
#myprint('Exception in:', inspect.stack()[0][3],e.args)
raise
if myrow != None:
#myprint('fetched one myrow != None:',int(myrow['lstart']))
#print('fetched one myrow != None:',int(myrow['lstart']), int(myrow['lcharlen']))
self.txtfile.seek(int(myrow['lstart']))
return self.txtfile.read(int(myrow['lcharlen'])), myrow['ltimestamp']
else:
#myprint('fetched one myrow != None:',lemma)
return '',None
def getLemmasLike(self,text):
mycursor = self.myconn.cursor()
try:
mycursor.execute('''SELECT lemma FROM maintbl WHERE lemma LIKE ?;''', ('%' + text + '%',))
manyrows = mycursor.fetchall()
titles = [x[0] for x in manyrows]
if len(titles):
return '\n'.join(titles), ''
else:
return '' ,''
except sqlite3.IntegrityError:
return 'DB IntegrityError ERROR', None
except Exception as e:
#myprint('Exception in:', inspect.stack()[0][3],e.args)
raise
def iterLemmas(self, ns = None):
print("iterLemmas before all 1")
try:
#myprint("before all")
#print("before all")
mycursor = self.myconn.cursor()
if ns != None:
mycursor.execute('''SELECT * FROM maintbl WHERE lns = ? ORDER BY lstart;''', (ns,))
else:
mycursor.execute('''SELECT * FROM maintbl ORDER BY lstart;''')
manyrows = mycursor.fetchall()
print(len(manyrows),type(manyrows))
for row in manyrows:
#print("in row")
ns = row['lns']
start = row['lstart']
charstoread = row['lcharlen']
title = row['lemma']
isredirect = (row['lredirect'] == 1)
timestamp = row['ltimestamp']
#myprint("in row")
self.txtfile.seek(int(start))
if charstoread == 0:
content = ''
else:
content = self.txtfile.read(int(charstoread))
#content = unescape(content)
#myprint("before yield")
#print("before yield")
yield DBRow(title = title,
ns = ns,
start = start,
charlen = charstoread,
timestamp = timestamp,
content = content,
isredirect = isredirect)
except sqlite3.IntegrityError:
#print('IntegrityError in:')
return None
except Exception as e:
#myprint('Exception in:', inspect.stack()[0][3],e.args)
#print('Exception in:')
raise
def textappend(self, text):
try:
self.txtfile.seek(0,2)
titlestart = str(self.txtfile.tell())
#titlelen = len(text)
self.txtfile.write(text+ '\n')
return titlestart
except Exception as e:
#return None
#myprint('Exception in:', inspect.stack()[0][3],e.args)
raise
def textdelete(self, start, usablespace):
#myprint('textdelete start, usablespace top', start, usablespace)
try:
if usablespace == -1:
#is the last lemma
#truncate to lstart and return true
self.txtfile.truncate(start)
self.txtfile.seek(start)
return True
elif usablespace < -1:
#myprint( inspect.stack()[0][3],'usablespace < -1 in text delete, should not come here.')
raise
if usablespace < len('\n'):
newtext = ''
else:
newtext = ''.ljust(usablespace-len('\n'))
self.txtfile.seek(int(start))
self.txtfile.write(newtext+'\n')
return True
except Exception as e:
#return False
#myprint('textdelete', start, usablespace)
#myprint('Exception in:', inspect.stack()[0][3],e.args)
raise e
def getLemmasInList(self, tupleoflemmas):
#print(self.myconn.isolation_level)
mycursor = self.myconn.cursor()
try:
sql='''SELECT * FROM maintbl WHERE lemma IN ({seq})'''.format(
seq=','.join(['?']*len(tupleoflemmas)))
#print('=============== SQL ==========')
#print(sql)
#print(tupleoflemmas)
mycursor.execute(sql, tupleoflemmas)
#mycursor.execute('''SELECT lemma FROM maintbl WHERE lemma IN (?);''', ('%' + text + '%',))
manyrows = mycursor.fetchall()
#print('returned from execute',len(manyrows))
if len(manyrows):
lemmasasfields = {}
#lns, lredirect, lstart, lcharlen, ltimestamp
#print('in manyrows',manyrows[0]['lemma'])
for arow in manyrows:
#print('arow in getLemmasInList', arow)
#print(x['lemma'], '==================')
#print('lemma in manyrows',manyrows[x]['lemma'])
#print({key : x[key] for key in x.keys() if key != 'lemma'})
lemmasasfields[arow['lemma']] = { key : arow[key] for key in arow.keys() if key != 'lemma'}
#lemmasasfields[manyrows[x]['lemma']] = { 'lns': manyrows[x]['lns'],
#'lredirect':manyrows[x]['lredirect'],
#'lstart':manyrows[x]['lstart'],
#'lcharlen':manyrows[x]['lcharlen'],
#'ltimestamp':manyrows[x]['ltimestamp']
#}
#print(lemmasasfields['promulgate'])
return lemmasasfields
else:
return {}
except sqlite3.IntegrityError:
print('DB IntegrityError ERROR')
return None
except Exception as e:
#myprint('Exception in:', inspect.stack()[0][3],e.args)
print('Exception in:getLemmasInList',e)
raise e
def textinsertnew(self, text, start, usablespaceinbytes):
try:
padding = usablespaceinbytes - len('\n') - len(bytes(text, 'utf-8'))
newtext = text + (" " * padding)
self.txtfile.seek(start)
self.txtfile.write(newtext+'\n')
return True
except Exception as e:
#return False
#myprint('Exception in:', inspect.stack()[0][3],e.args)
raise
if __name__ == "__main__":
realfile = os.path.realpath(__file__)
realfile_dir = os.path.dirname(os.path.abspath(realfile))
dbf ='/home/ilias/Λήψεις/Προγραμματισμός1/dumps/ukwiktionary-latest-pages-meta-current.xml.db'
txtf ='/home/ilias/Λήψεις/Προγραμματισμός1/dumps/ukwiktionary-latest-pages-meta-current.xml.txt'
try:
with DB(dbf,txtf) as localydb:
print(localydb)
localydb.updateSiteInfo2('blahblah')
#b = db.getLemmasLike('zebra')
#print(len(b))
#for c in b:
#print(db.getLemmaContent(c),end = '\t')
#print("+++++++++++++-----++++++++++++++++")
#print(c)
#print("++++++++++++++++++++++++++++++++++")
#print(db.getLemmaContent(c)[0])
#print('----------------------------------')
#print()
#b = db.getLemmaContent('α')
#print(b[0])
#r = DBRow(title = 'α',
#ns = 0,
#content = 'mplah mplah \n blah',
#timestamp = '20170606',
#start = 0,
#charlen = 0)
#c = db.updatelemma(r)
#try:
#for l in db.iterLemmas(10):
#print("+++++++++++++-----++++++++++++++++")
#print(l.title)
#print("++++++++++++++++++++++++++++++++++")
#print(l.content)
#print('----------------------------------')
#except:
#raise
#b = db.deletelemma('Template:Wikitext talk page converted to Flow')
#print('b=',b)
except Exception as e:
raise