-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathinventory.py
executable file
·351 lines (284 loc) · 9.5 KB
/
inventory.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
#!/usr/local/bin/python3
# FIXME: This is an OLD interactive command line....
import io
import os
import sys
import util as ut
# file format:
# - 8 bytes for current key
# - 8 bytes how many rows
# - 8 bytes key, name, serial
dbfile = None
dbkeygen = -1
dbnumrows = -1
dbnumfields = 5
dbdata = dict()
def safeexit(code):
closedb(dbfile)
exit(code)
# function - opens a db file
# filename - name of file to open
# return - None on fail, a file object on sucess
def opendb(filename):
if not os.path.exists(filename):
ut.output("dbfile does not exist. please create it.")
return None
try:
file = open(filename, "a+")
except FileNotFoundError:
ut.output("file does not exist.")
return None
global dbdata
global dbkeygen
global dbnumrows
# TODO: add error checking here
file.seek(0, 0)
tmp = file.readlines(2)
tmp[0] = tmp[0].replace('\n', "")
dbkeygen = int(tmp[0])
tmp[1] = tmp[1].replace('\n', "")
dbnumrows = int(tmp[1])
lines = file.readlines()
i = 0
for line in lines:
line = line.replace('\n', "")
s = line.split(",")
if len(s) != 5:
ut.output("line {} was not parsed sucessfully.".format(i))
else:
dbdata[int(s[0])] = (s[1], s[2], s[3], s[4])
return file
def closedb(file):
if file != None:
if not file.closed:
file.close()
dbdata.clear()
return 0
def savedb():
# ensure db is open
if dbfile == None or dbfile.closed:
ut.output("db file is closed. please open a db.")
return 1
dbfile.truncate
def keygen() -> int:
global dbkeygen
if dbkeygen == -1:
ut.output("invalid key seed. unable to gen key.")
return -1
else:
dbkeygen += 1
return dbkeygen
def createrow(key, data) -> int:
global dbnumrows
# ensure db is open
if dbfile == None or dbfile.closed:
ut.output("db file is closed. please open a db.")
return 1
if key == -1:
ut.output("invalid key specified for new row. aborting...")
return 1
dbdata[key] = data
dbnumrows += 1
return 0
def savedb():
if dbfile == None or dbfile.closed:
ut.output("db file is closed. please open a db.")
return 1
# delete the whole file and start over
dbfile.seek(0)
dbfile.truncate()
# write metadata
dbfile.seek(0)
dbfile.write('{}\n'.format(dbkeygen))
dbfile.write('{}\n'.format(dbnumrows))
i = 0
for key in dbdata.keys():
print("writing row {} to the file...".format(i))
i += 1
data = dbdata[key]
dbfile.write("{},{},{},{},{}\n".format(key, data[0], data[1], data[2], data[3]))
dbfile.flush()
return 0
def createdb(filename) -> int:
# don't overwrite existing file w/o permission
if os.path.exists(filename):
ut.output("file exists... overwrite?")
a = input("(y/n) > ")
if a == "y" or a == "yes":
ut.output("overwriting file...")
else:
ut.output("aborting creation...")
return 1
try:
tmpfile = open(filename, "x")
except:
ut.output("error creating file.")
return 1
# keygen number
tmpfile.write('0\n')
# num rows in file
tmpfile.write('0\n')
tmpfile.close()
return 0
def searchdb(value, field: int):
global dbfile
global dbnumfields
if dbfile == None or dbfile.closed:
ut.output("db is closed.")
return None
if field >= dbnumfields or field < 0:
ut.output("{} is not a field in this db.")
return None
if field == 0: # search based on keys
return dbdata[value]
else: # search based on fields in the tuple
# find all hits for value and put them into r
r = list()
for k in dbdata.keys():
if dbdata[k][field - 1] == value:
r.append((k, dbdata[k]))
if len(r) == 0:
return None # if no hits were found return None
else:
return r # if hits were found, return the list
def main():
global dbfile
filename = ""
# if args.. attempt to open file
if len(sys.argv) > 1:
filename = sys.argv[1]
dbfile = opendb(filename)
if dbfile == None:
ut.output("no db file is open.")
else:
ut.output("db file {} is open".format(filename))
while True:
line = input("> ")
split = line.split(" ")
commandstr = split[0]
if commandstr == "new":
key = keygen()
if key == -1:
ut.output("unable to gen key for new row. aborting...")
continue
name = input("name > ")
serial = input("serial > ")
location = input("location > ")
comment = input("comment > ")
createrow(key, (name, serial, location, comment))
elif commandstr == "delete":
key = input("key > ")
if dbdata[key] == None:
ut.output("{} is not a key in the db.".format(key))
else:
dbdata.pop(key)
elif commandstr == "update":
line = input("key > ")
# convert line str to int
try: key = int(line)
except:
ut.output("{} is not a valid key".format(key))
continue
if dbdata[key] == None:
ut.output("{} is not a key in the db.".format(key))
else:
data = dbdata[key]
fields = input("fields to update > ") # get what fields need to be modified
fields = fields.split(" ") # split into the names
name = data[0]
serial = data[1]
location = data[2]
comments = data[3]
for f in fields:
v = input("{} > ".format(f))
if f == "name":
name = v
elif f == "serial":
serial = v
elif f == "location":
location = v
elif f == "comments":
comments = v
else:
ut.output("{} is nota valid field id")
# create a new tuple with modified values
t = (name, serial, location, comments)
dbdata[key] = t
elif commandstr == "search":
# ensure a field was specified to preform search
if len(split) <= 1:
ut.output("please specify a field to search.")
else:
# switch based on field specified
field = split[1]
hits = None
if field == "key":
key = input("key > ")
try: key = int(key)
except: ut.output("{} must be an integer.".format(key))
hits = searchdb(key, 0)
elif field == "name":
name = input("name > ")
hits = searchdb(name, 1)
elif field == "serial":
serial = input("serial > ")
hits = searchdb(serial, 2)
elif field == "location":
location = input("location > ")
hits = searchdb(location, 3)
elif field == "comments":
comments = input("comments > ")
hits = searchdb(comments, 4)
else:
ut.output("{} is not a searchable field.".format(field))
continue
if hits == None:
ut.output("no hits found.")
else:
for h in hits:
ut.output("{}: {}".format(h[0], h[1]))
elif commandstr == "list":
if len(dbdata) == 0:
ut.output("db is empty.")
else:
for k in dbdata.keys():
print("{}: {}".format(k, dbdata[k]))
elif commandstr == "save":
if savedb() != 0:
ut.output("unable to save db.")
else:
ut.output("db saved.")
elif commandstr == "numrows":
if dbfile == None or dbfile.closed:
ut.output("no db file is open.")
else:
ut.output("numrows: {}".format(dbnumrows))
elif commandstr == "open":
if dbfile != None and not dbfile.closed:
ut.output("a db is already open... continue?")
a = input("(y/n) > ")
if a != "y" and a != "yes":
ut.output("aborting open...")
continue
fn = input("filename > ")
dbfile = opendb(fn)
if dbfile == None:
ut.output("db was not opened sucessfully")
else:
ut.output("db opened.")
elif commandstr == "close":
if closedb(dbfile) != 0:
ut.output("db not closed sucessfully.")
else:
ut.output("db closed.")
elif commandstr == "create":
fn = input("filename > ")
if createdb(fn) != 0:
ut.output("db was not created sucessfully.")
else:
ut.output("db created.")
elif commandstr == "exit":
safeexit(0)
else:
ut.output("{} is not a valid command.".format(commandstr))
main()