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
|
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
#
# PartMgr - Part-DB V0.1.3 RW import filter
#
# Copyright 2014 Michael Buesch <m@bues.ch>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
#
from partmgr.core.database import *
import sys
import getopt
import mysql.connector
class Partdb_Part(object):
def __init__(self,
id, id_category, name, instock, mininstock,
comment, id_footprint, id_storeloc, id_supplier,
supplierpartnr):
self.id = id
self.id_category = id_category
self.name = name
self.instock = instock
self.mininstock = mininstock
self.comment = comment
self.id_footprint = id_footprint
self.id_storeloc = id_storeloc
self.id_supplier = id_supplier
self.supplierpartnr = supplierpartnr
class Partdb_Category(object):
def __init__(self,
id, name, parentnode):
self.id = id
self.name = name
self.parentnode = parentnode
class Partdb_Footprint(object):
def __init__(self,
id, name):
self.id = id
self.name = name
class Partdb_Storeloc(object):
def __init__(self,
id, name):
self.id = id
self.name = name
class Partdb_Supplier(object):
def __init__(self,
id, name):
self.id = id
self.name = name
class Partdb_Price(object):
def __init__(self,
id, part_id, ma, price, timestamp):
self.id = id
self.part_id = part_id
self.ma = ma
self.price = price
self.timestamp = timestamp
def addCategories(catMap, db, conn, parentId):
c = conn.cursor(buffered = True)
c.execute("SELECT id, name, parentnode "
"FROM categories "
"WHERE parentnode=%s "
"ORDER BY name;",
(str(parentId),))
for row in c:
srcCat = Partdb_Category(*row)
if parentId:
parent = catMap[parentId]
else:
parent = None
cat = Category(name = srcCat.name,
parent = parent)
db.modifyCategory(cat)
catMap[srcCat.id] = cat
addCategories(catMap, db, conn, srcCat.id)
c.close()
def addFootprints(footpMap, db, conn):
c = conn.cursor()
c.execute("SELECT id, name "
"FROM footprints;")
for row in c:
srcFootp = Partdb_Footprint(*row)
footp = Footprint(name = srcFootp.name)
db.modifyFootprint(footp)
footpMap[srcFootp.id] = footp
c.close()
def addSuppliers(suppMap, db, conn):
c = conn.cursor()
c.execute("SELECT id, name "
"FROM suppliers;")
for row in c:
srcSupp = Partdb_Supplier(*row)
supp = Supplier(name = srcSupp.name)
db.modifySupplier(supp)
suppMap[srcSupp.id] = supp
c.close()
def addLocation(locMap, db, conn):
c = conn.cursor()
c.execute("SELECT id, name "
"FROM storeloc;")
for row in c:
srcLoc = Partdb_Storeloc(*row)
loc = Location(name = srcLoc.name)
db.modifyLocation(loc)
locMap[srcLoc.id] = loc
c.close()
def importFromPartdb(db, conn):
if db.countRootCategories():
print("Error: The target database %s is not empty" %\
db.filename)
return 1
# Build the category tree.
catMap = {} # key = partdb-id, value = Category()
addCategories(catMap, db, conn, 0)
# Build the footprints.
footpMap = {} # key = partdb-id, value = Footprint()
addFootprints(footpMap, db, conn)
# Build the store locations.
locMap = {} # key = partdb-id, value = Location()
addLocation(locMap, db, conn)
# Build the suppliers.
suppMap = {} # key = partdb-id, value = Supplier()
addSuppliers(suppMap, db, conn)
# Get all prices.
priceMap = {} # key = partdb-part-id, value = Partdb_Price()
c = conn.cursor()
c.execute("SELECT id, part_id, ma, preis, t "
"FROM preise;")
for row in c:
price = Partdb_Price(*row)
priceMap[price.part_id] = price
c.close()
# Build the stock items.
c = conn.cursor()
c.execute("SELECT id, id_category, name, instock, mininstock, "
"comment, id_footprint, id_storeloc, id_supplier, "
"supplierpartnr "
"FROM parts;")
for row in c:
srcPart = Partdb_Part(*row)
cat = catMap[srcPart.id_category]
footp = footpMap[srcPart.id_footprint]
supp = suppMap[srcPart.id_supplier]
loc = locMap[srcPart.id_storeloc]
part = Part(name = srcPart.name,
category = cat)
db.modifyPart(part)
minQty = srcPart.mininstock
if minQty == 0:
targetQty = 0
else:
targetQty = minQty + int(round((minQty * 0.25)))
if targetQty == minQty:
targetQty += 1
stockItem = StockItem(name = srcPart.name,
description = srcPart.comment,
part = part,
category = cat,
footprint = footp,
minQuantity = minQty,
targetQuantity = targetQty)
db.modifyStockItem(stockItem)
try:
price = priceMap[srcPart.id].price
priceStamp = priceMap[srcPart.id].timestamp
except KeyError:
price = Origin.NO_PRICE
priceStamp = 0
origin = Origin(name = "",
stockItem = stockItem,
supplier = supp,
orderCode = srcPart.supplierpartnr,
price = price,
priceTimeStamp = priceStamp)
db.modifyOrigin(origin)
storage = Storage(name = "",
stockItem = stockItem,
location = loc,
quantity = srcPart.instock)
db.modifyStorage(storage)
c.close()
return 0
def usage():
print("PartMgr - Part-DB-V0.1.3-RW import filter")
print()
print("Usage: partmgr-import-partdb TARGETFILE.pmg SQLHOST SQLUSER SQLPASSWORD [SQLDATABASE]")
print()
print(" TARGETFILE.pmg The target database file to write to")
print(" SQLHOST The Part-DB MySQL hostname")
print(" SQLUSER The Part-DB MySQL username")
print(" SQLPASSWORD The Part-DB MySQL password")
print(" SQLDATABASE The Part-DB MySQL database (default: partdb)")
def main():
try:
(opts, args) = getopt.getopt(sys.argv[1:],
"h",
[ "help", ])
except getopt.GetoptError as e:
printError(str(e))
usage()
return 1
for (o, v) in opts:
if o in ("-h", "--help"):
usage()
return 0
if len(args) < 4 or len(args) > 5:
usage()
return 1
targetfile = args[0]
host = args[1]
username = args[2]
password = args[3]
if len(args) > 4:
database = args[4]
else:
database = "partdb"
app = QApplication(sys.argv)
conn = None
db = None
try:
conn = mysql.connector.connect(user = username,
password = password,
host = host,
database = database)
db = Database(targetfile)
res = importFromPartdb(db, conn)
db.close(collectGarbage = (res == 0),
updateRevision = (res == 0))
conn.close()
return res
except (mysql.connector.Error, PartMgrError) as e:
try:
if db:
db.close(commit = False)
except Exception:
pass
try:
if conn:
conn.close()
except Exception:
pass
print(e)
return 1
if __name__ == "__main__":
sys.exit(main())
|