-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDocManSQL.py
More file actions
239 lines (213 loc) · 6.56 KB
/
DocManSQL.py
File metadata and controls
239 lines (213 loc) · 6.56 KB
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
import sqlite3 as lite
import csv
import os
from datetime import datetime
import configparser
"""
DocManSql - wrapper om SQLite
Exceptions : moeten door de UI worden afgehandeld.
"""
class DocManSql:
# Phytonista : BasisClass voor database van DocMan.
con=None
cur=None
FileLocation=None
DbLocation=None
def __init__(self):
# >>> if platform.uname().node == 'GdH-Surface':
# ... print(platform.uname())
# ...
# uname_result(system='Windows', node='GdH-Surface', release='10', version='10.0.15063', machine='AMD64', processor='Intel64 Family 6 Model 78 Stepping 3, GenuineIntel')
# >>>
# GreyHound
config = configparser.ConfigParser()
config.read('DocMan.ini')
print(config.sections())
# GdH - Dit is de correcte vorm : (16-10-2017) : get !!!
try:
self.FileLocation = config.get('FileLocations', 'DocTreeRoot')
self.DbLocation = config.get('FileLocations', 'DocTreeDB')
except:
print( 'ERROR - Docman.ini file niet correct')
exit(0)
self.con = lite.connect(self.DbLocation)
# Surface
# self.con = lite.connect('C:\\Users\\Geert\\OneDrive\\Archief\\DocMan.db')
# C:\Users\Geert\OneDrive\Archief
# Gebruik de dictionary om velden te selecteren
# (GdH - Niet) self.con.row_factory = lite.Row
self.cur =self.con.cursor()
def __del__(self):
if self.con:
self.con.close()
print('DB Closed')
def CreateTables(self):
# 9-8-2014
# 3-8-2017 - uitgebreid met Categorie en DocSoort
print('ERROR - Deze functie niet draaien, verwijderd Docman !')
return
exit
try:
sql="DROP TABLE IF EXISTS DocMan"
# self.cur.execute(sql)
except lite.Error as e:
print (e.args[0]) # dit is de letterlijke tekst van de fout.
sql="""CREATE TABLE DocMan
(DocCat TEXT
,DocSoort TEXT
,Pad TEXT
,Filenaam TEXT
,Referentie TEXT
,Datum DATETIME
,PRIMARY KEY(Pad)
)
"""
try:
self.cur.execute(sql)
except lite.Error as e:
print (e.args[0])
# Categorie
try:
sql="DROP TABLE IF EXISTS DocCat"
self.cur.execute(sql)
except lite.Error as e:
print (e.args[0]) # dit is de letterlijke tekst van de fout.
sql="""CREATE TABLE DocCat
(DocCat TEXT
,PRIMARY KEY(DocCat)
)
"""
try:
self.cur.execute(sql)
except lite.Error as e:
print (e.args[0])
#DocSoort
try:
sql="DROP TABLE IF EXISTS DocSoort"
self.cur.execute(sql)
except lite.Error as e:
print (e.args[0]) # dit is de letterlijke tekst van de fout.
sql="""CREATE TABLE DocSoort
(DocSoort TEXT
,PRIMARY KEY(DocSoort)
)
"""
try:
self.cur.execute(sql)
except lite.Error as e:
print (e.args[0])
def insDocCat(self, Categorie):
sql="""INSERT INTO DocCat Values('""" + Categorie + """');"""
try:
self.cur.execute(sql)
self.con.commit()
except lite.Error as e:
print (e.args[0])
def DumpDbDocMan(self):
'''
maak een volledige dump naar een csv file.
DocManDb<timestamp>.csv
print(datetime.now().strftime('%Y%m%d%H%M%S'))
'''
sql="""SELECT * FROM DocMan;"""
try:
self.cur.execute(sql)
rows = self.cur.fetchall()
for row in rows:
print(row)
# ToDo Nu rows aanbieden aan de CSV module voor export
except lite.Error as e:
print("Dump naar CSV", e.args[0])
exit()
fname = 'DocMan' + datetime.now().strftime('%Y%m%d%H%M%S') + '.csv'
print(fname)
header=['DocCat' ,'DocSoort','Pad','Filenaam','Referentie' ,'Datum']
with open(fname, 'wt') as csvout:
file_writer = csv.writer(csvout, dialect='unix')
file_writer.writerow(header)
file_writer.writerows(rows)
if self.con:
self.con.close()
def insDocMan(self, DocCat, DocSoort, Pad, Filenaam, Referentie, Datum):
try:
#self.cur.execute(sql)
self.cur.execute("INSERT INTO DocMan Values(?,?,?,?,?,?)", (DocCat, DocSoort, Pad, Filenaam, Referentie, Datum))
self.con.commit()
except lite.Error as e:
print (e.args[0])
def DbRecordMissingHandler(self, FilePad):
'''
Handler voor het geval de integriteitscheck tussen Db en filesysteem mislukt.
standaard wordt deze geprint, maar kan door bijvoorbeeld TKinter worden overruled
'''
print(FilePad + " bestaat in Db, niet op deze bestandsloctie.")
def IntCheckFilesFromDb(self):
'''
integriteitscheck : loop door de Db en controleer of de file aanwezig is volgens het pad.
Het pad wordt samemgesteld op basis van de rubriek en jaar informatie.
- select * from DocMan.
- loop door cursor
- haal variabelen op (datum, categorie, fname)
- maak en test het pad
- try /except
- test NOK: door naar handler
- standaard handler : print de betreffende file op de standaard output.
- handler kan worden overruled in TKinter bijvoorbeeld om files in een listboc te tonen
'''
sql="""SELECT DocCat, Filenaam, Datum FROM DocMan ORDER BY DocCat, datum;"""
n = 0 # Aantal missende documenten
try:
self.cur.execute(sql)
rows = self.cur.fetchall()
for row in rows:
# print(row)
# Ophalen van de velden
DocCat = row[0]
Filenaam = row[1]
Datum = row[2]
jaar=Datum[:4]
# pad samenstellen
FullPath = os.path.join(self.FileLocation, DocCat)
FullPath = os.path.join(FullPath, jaar)
FullPath = os.path.join(FullPath, Filenaam)
FullPath = os.path.join(FullPath + '.pdf')
if os.path.isfile(FullPath) == False:
# Db record bestaat niet op schijf.
n+=1
self.DbRecordMissingHandler(FullPath)
except lite.Error as e:
print("Integriteitscheck op Db", e.args[0])
exit()
if n == 0:
print("Check files from Db - Geen afwijkingen")
else:
print("Check files from Db")
def Process(self, VolledigPad):
print(VolledigPad)
def IntCheckDbFromFiles(self, DocRoot):
'''
'''
for root, dirs, files in os.walk(DocRoot, topdown=False):
for name in files:
zk=name.find('.pdf')
if zk != -1:
Process(os.path.join(root, name))
# Change - SQL's kunnen INSERTS of UPDATES zijn.
# Exceptions door fouten in de primary key; melden via de UI
def Change(self, CSQL) :
self.cur.execute(CSQL)
self.con.commit()
# Query - return een dictionary met veldnamen en waarden
def Query(self, QSQL) :
self.cur.execute(QSQL)
# Uitzoeken hoe het row object is opgebouwd (tuple ?) return een list of rows?
# *************************************************************************************
t=DocManSql()
#5t.CreateTables()
#t.insDocCat('Software')
#t.insDocMan('Software','Factuur','c:\\mijn documenten\\test\\6556449.pdf', '6556449', 'ANS784448', '2009-01-12')
#t.DumpDbDocMan()
#t.DumpDbDocMan()
t.IntCheckFilesFromDb()
del t
# insert into DocMan Values ('Software','Factuur','c:\mijn documenten\test\6556447.pdf', '6556447', 'ANS784448', '2009-01-12');