-
Notifications
You must be signed in to change notification settings - Fork 1
/
mainMenu.py
405 lines (359 loc) · 17.5 KB
/
mainMenu.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
#!/usr/bin/python
# encoding: utf-8
##############################################################################
# mainMenu.py - Main menu screen
#
##############################################################################
# Copyright (c) 2022, 2023 David Villena
# All rights reserved.
# Licensed under the New BSD License
# (http://www.freebsd.org/copyright/freebsd-license.html)
##############################################################################
import datetime
import os.path
import sqlite3
import sys
import time
import tracemalloc
import linecache
import npyscreen
from npyscreen import util_viewhelp
import author
import authorSelector
import book
import bookListing
import bookSelector
import bsWidgets as bs
import config
import identification
import publisher
import publisherSelector
import user
import userSelector
import utilities
import warehouse
import warehouseSelector
import dbIntegrityCheck
import deleteMultipleRecords
from config import SCREENWIDTH as WIDTH
AUTHENTICATE = config.AUTHENTICATE
DATEFORMAT = config.dateFormat
helpText = "\nA simple menu form.\n\n\n" +\
"* There's a 'Main menu' title that is a plain npyscreen.FixedText widget.\n\n" +\
"* Under the title, there's the main selector widget, an adapted npyscreen.MultiLineAction.\n\n" +\
"* Options can be accessed through the arrow keys + Enter, or through the direct pressing of a number key.\n\n" +\
"* Menu '6.Utilities' leads to another submenu.\n\n" +\
"* The main purpose of this program is to share my experience with the npyscreen terminal user interface and of course to learn some Python."
class bookstoreApp(npyscreen.NPSAppManaged):
def onStart(self):
"Override this method to perform any initialization."
self.connect_database()
# check tables' existence:
cur = config.conn.cursor()
DBprefix = "bookstore."
table_list = [ "author",
"book",
"book_author",
"publisher",
"user",
"warehouse",
"book_warehouse"
]
while True: # locking the SQLite single user DB
for tname in table_list:
tname = DBprefix + tname
sqlQuery = "SELECT EXISTS ( SELECT name FROM sqlite_schema WHERE type='table' AND name=? )"
try:
cur.execute(sqlQuery, (tname,) )
if cur.fetchone()[0] != 1 :
bs.notify_OK("\n Database: Table does not exist: '" + tname + "'","Error")
sys.exit()
except sqlite3.OperationalError: # default timeout is 5 sec
bs.notify_OK("\n Database is locked, please wait.", "Bookstore")
continue # to the loop
break # go on
npyscreen.setTheme(npyscreen.Themes.DefaultTheme)
# Forms __inits__() are executed now:
self.registerForm("MAIN", MainMenuForm(name="MainMenu", parentApp=self, help=helpText, \
lines=0, columns=0, minimum_lines=25, minimum_columns=WIDTH, maximum_columns=WIDTH))
self.registerForm("IDENTIFICATION", identification.ID_Form(name="Identification", parentApp=self, \
help=identification.helpText, lines=0, columns=0, minimum_lines=25, minimum_columns=WIDTH))
self.registerForm("BOOKSELECTOR", bookSelector.BookSelectForm(name="BookSelector", parentApp=self, \
help=bookSelector.helpText, lines=0, columns=0, minimum_lines=25, minimum_columns=WIDTH))
self.registerForm("BOOK", book.BookForm(name="BookForm", parentApp=self, \
help=book.helpText, lines=0, columns=0, minimum_lines=25, minimum_columns=WIDTH))
self.registerForm("AUTHORSELECTOR", authorSelector.AuthorSelectForm(name="AuthorSelector", parentApp=self, \
help=authorSelector.helpText, lines=0, columns=0, minimum_lines=25, minimum_columns=WIDTH))
self.registerForm("AUTHOR", author.AuthorForm(name="AuthorForm", parentApp=self, \
help=author.helpText, lines=0, columns=0, minimum_lines=25, minimum_columns=WIDTH))
self.registerForm("PUBLISHERSELECTOR", publisherSelector.PublisherSelectForm(name="PublisherSelector", parentApp=self, \
help=publisherSelector.helpText, lines=0, columns=0, minimum_lines=25, minimum_columns=WIDTH))
self.registerForm("PUBLISHER", publisher.PublisherForm(name="PublisherForm", parentApp=self, \
help=publisher.helpText, lines=0, columns=0, minimum_lines=25, minimum_columns=WIDTH))
self.registerForm("WAREHOUSESELECTOR", warehouseSelector.WarehouseSelectForm(name="WarehouseSelector", parentApp=self, \
help=warehouseSelector.helpText, lines=0, columns=0, minimum_lines=25, minimum_columns=WIDTH))
self.registerForm("WAREHOUSE", warehouse.WarehouseForm(name="WarehouseForm", parentApp=self, \
help=warehouse.helpText, lines=0, columns=0, minimum_lines=25, minimum_columns=WIDTH))
self.registerForm("BOOKLISTING", bookListing.BookListingForm(name="BookListingForm", parentApp=self, \
help=bookListing.helpText, lines=0, columns=0, minimum_lines=25, minimum_columns=WIDTH))
self.registerForm("UTILITIES", utilities.UtilitiesMenuForm(name="UtilitiesForm", parentApp=self, \
help=utilities.helpText, lines=0, columns=0, minimum_lines=25, minimum_columns=WIDTH))
self.registerForm("USERSELECTOR", userSelector.UserSelectForm(name="UserSelector", parentApp=self, \
help=userSelector.helpText, lines=0, columns=0, minimum_lines=25, minimum_columns=WIDTH))
self.registerForm("USER", user.UserForm(name="UserForm", parentApp=self, \
help=user.helpText, lines=0, columns=0, minimum_lines=25, minimum_columns=WIDTH))
self.registerForm("DB_INTEGRITY_CHECK", dbIntegrityCheck.DBintegrityCheckForm(name="DBintegrityCheckForm", parentApp=self, \
help=dbIntegrityCheck.helpText, lines=0, columns=0, minimum_lines=25, minimum_columns=WIDTH))
self.registerForm("DELETE_MULTIPLE_RECORDS", deleteMultipleRecords.DeleteMultipleRecordsForm(name="DeleteMultipleRecordsForm", parentApp=self, \
help=deleteMultipleRecords.helpText, lines=0, columns=0, minimum_lines=25, minimum_columns=WIDTH))
def onInMainLoop(self):
"""Called between each screen while the application is running. Not called before the first screen. Override at will"""
if self.NEXT_ACTIVE_FORM == 'IDENTIFICATION':
form = self._Forms["IDENTIFICATION"]
form.editw = 1 # focus to widget 1 (grid) so InputOpt field lose it.
elif self.NEXT_ACTIVE_FORM == 'BOOKSELECTOR':
form = self._Forms["BOOKSELECTOR"]
form.editw = 1 # focus to widget 1 (grid) so InputOpt field lose it.
elif self.NEXT_ACTIVE_FORM == 'AUTHORSELECTOR':
form = self._Forms["AUTHORSELECTOR"]
form.editw = 1 # focus to widget 1 (grid) so InputOpt field lose it.
elif self.NEXT_ACTIVE_FORM == 'PUBLISHERSELECTOR':
form = self._Forms["PUBLISHERSELECTOR"]
form.editw = 1 # focus to widget 1 (grid) so InputOpt field lose it.
elif self.NEXT_ACTIVE_FORM == 'WAREHOUSESELECTOR':
form = self._Forms["WAREHOUSESELECTOR"]
form.editw = 1 # focus to widget 1 (grid) so InputOpt field lose it.
elif self.NEXT_ACTIVE_FORM == 'BOOKLISTING':
form = self._Forms["BOOKLISTING"]
form.editw = 1 # focus to widget 1 (grid) so InputOpt field lose it.
elif self.NEXT_ACTIVE_FORM == 'USERSELECTOR':
form = self._Forms["USERSELECTOR"]
form.editw = 1 # focus to widget 1 (grid) so InputOpt field lose it.
def connect_database(self):
"Check and connect database."
# SQLite DB file exists:
DBpath = config.dataPath
DBname = config.dbname
self.DBfilename = DBpath + DBname
if not os.path.exists(self.DBfilename):
bs.notify_OK("\n Database file "+self.DBfilename+" does not exist.", "Error")
sys.exit()
# DB Connection creation
conn = None
try:
conn = sqlite3.connect(self.DBfilename)
except sqlite3.Error as e:
print(e)
config.conn = conn # connection for this instance of bookstore
def onCleanExit(self):
"""Override this method to perform any cleanup when application is exiting without error."""
class MainMenuForm(npyscreen.FormBaseNew):
"Main menu form."
def __init__(self, name=None, parentApp=None, framed=None, help=None, color='FORMDEFAULT', widget_list=None, \
cycle_widgets=False, *args, **keywords):
""" Crea el padre, npyscreen._FormBase """
super().__init__(name, parentApp, framed, help, color, widget_list, cycle_widgets, *args, **keywords) # goes to _FormBase.__init__()
self.password_entered = False
self.app = parentApp
def create(self):
"The standard constructor will call the method .create(), which you should override to create the Form widgets."
self.framed = True # form with frame
self.how_exited_handers[npyscreen.wgwidget.EXITED_ESCAPE] = self.exitApplication # Escape exit
self.setup_key_handlers() # shortcuts
self.nextrely += 1 # vertical distance between menu title and menu
pname, version = config.pname, config.program_version
self.formTitle = pname + " " + version + " - Main"
self.name = self.formTitle
# Screen title line
wg = self.add(npyscreen.FixedText, name="MainTitle", value=self.name, relx=2, rely=0, editable=False)
# Menu title line
wg = self.add(npyscreen.FixedText, name="MenuTitle", value="Main menu", relx=32, rely=6, editable=False)
# Main menu selector
wg = self.mainSelector()
# Bottom status line
self.statusLine = " Select option "
wg = self.add(npyscreen.FixedText, name="MainStatus", value=self.statusLine, relx=2, rely=24, use_max_space=True, editable=False)
def setup_key_handlers(self):
self.add_handlers({"1": self.keyHandler}) # menu 1
self.add_handlers({"2": self.keyHandler}) # menu 2
self.add_handlers({"3": self.keyHandler}) # menu 3
self.add_handlers({"4": self.keyHandler}) # menu 4
self.add_handlers({"5": self.keyHandler}) # menu 5
self.add_handlers({"6": self.keyHandler}) # menu 6
self.add_handlers({"q": self.keyHandler}) # exit with "q"
self.add_handlers({"Q": self.keyHandler}) # exit with "Q"
def pre_edit_loop(self):
if AUTHENTICATE and not self.password_entered:
self.password_entered = True
identification.ID_Form.set_ID()
def post_edit_loop(self):
pass
def _during_edit_loop(self):
pass
def mainSelector(self):
value_list = [
"1. Book edition",
"2. Author edition",
"3. Publisher edition",
"4. Warehouse edition",
"5. Book listing",
"6. Utilities",
"Q. Quit program" ]
self.selector = self.add(VerticalMenu,
w_id=None,
max_height=11,
rely=9,
relx=29,
name="MainMenu",
footer="",
values=value_list,
editable=True,
hidden=False,
slow_scroll=False
)
return self.selector
def keyHandler(self, keyAscii):
match keyAscii:
case 49: # menu 1
self.selector.cursor_line=0
self.display()
time.sleep(0.2)
self.menuBookSelector()
case 50: # menu 2
self.selector.cursor_line=1
self.display()
time.sleep(0.2)
self.menuAuthorSelector()
case 51: # menu 3
self.selector.cursor_line=2
self.display()
time.sleep(0.2)
self.menuPublisherSelector()
case 52: # menu 4
self.selector.cursor_line=3
self.display()
time.sleep(0.2)
self.menuWarehouseSelector()
case 53: # menu 5
self.selector.cursor_line=4
self.display()
time.sleep(0.2)
self.menuBookListing()
case 54: # menu 6
self.selector.cursor_line=5
self.display()
time.sleep(0.2)
self.menuUtilities()
case ( 81 | 113 ): # menu Q/q
self.selector.cursor_line=9
self.display()
time.sleep(0.2)
self.exitApplication()
def menuBookSelector(self):
# Calls books selector
selectorForm = self.parentApp._Forms['BOOKSELECTOR']
selectorForm.update_grid() # must be read here to get config.fileRows right
selectorForm.ask_option()
self.app.switchForm("BOOKSELECTOR")
def menuAuthorSelector(self):
# Calls authors selector
selectorForm = self.parentApp._Forms['AUTHORSELECTOR']
selectorForm.update_grid() # must be read here to get config.fileRows right
selectorForm.ask_option()
self.app.switchForm("AUTHORSELECTOR")
def menuPublisherSelector(self):
# Calls publishers selector
selectorForm = self.parentApp._Forms['PUBLISHERSELECTOR']
selectorForm.update_grid() # must be read here to get config.fileRows right
selectorForm.ask_option()
self.app.switchForm("PUBLISHERSELECTOR")
def menuWarehouseSelector(self):
# Calls warehouse selector
selectorForm = self.parentApp._Forms['WAREHOUSESELECTOR']
selectorForm.update_grid() # must be read here to get config.fileRows right
selectorForm.ask_option()
self.app.switchForm("WAREHOUSESELECTOR")
def menuBookListing(self):
# Calls book listing
form = self.parentApp._Forms['BOOKLISTING']
form.initialize()
self.app.switchForm("BOOKLISTING")
def menuUtilities(self):
# Call utilities menu.
self.app.switchForm("UTILITIES")
def updateMenu(self):
self.display(clear=True) # repaints, coming from F1_Help
self.edit()
def exitApplication(self):
if config.TRACEMALLOC:
snapshot = tracemalloc.take_snapshot()
self.display_top(snapshot)
if config.CONFIRMEXIT:
message = " Press OK to exit the application"
if bs.notify_ok_cancel(message, title="", wrap=True, editw = 1,):
print(config.normal_exit_message)
sys.exit()
else:
print(config.normal_exit_message)
sys.exit()
def h_display_help(self, input):
"Adaptation from FormBase to redraw the menu screen"
if self.help == None: return
if self.name:
help_name="%s - Help" %(self.name)
else:
help_name=None
util_viewhelp.view_help(self.help, title=help_name, autowrap=self.WRAP_HELP)
self.display()
self.updateMenu()
return True
def get_today(self):
sep = DATEFORMAT[2]
if DATEFORMAT[0] == "d":
format = "%d"+sep+"%m"+sep+"%Y"
else:
format = "%m"+sep+"%d"+sep+"%Y"
return datetime.datetime.today().strftime(format)
def display_top(self, snapshot, key_type='lineno', limit=10):
"tracemalloc results."
snapshot = snapshot.filter_traces((
tracemalloc.Filter(False, "<frozen importlib._bootstrap>"),
tracemalloc.Filter(False, "<unknown>"),
))
top_stats = snapshot.statistics(key_type)
print("Top %s lines" % limit)
for index, stat in enumerate(top_stats[:limit], 1):
frame = stat.traceback[0]
print("#%s: %s:%s: %.1f KiB"
% (index, frame.filename, frame.lineno, stat.size / 1024))
line = linecache.getline(frame.filename, frame.lineno).strip()
if line:
print(' %s' % line)
other = top_stats[limit:]
if other:
size = sum(stat.size for stat in other)
print("%s other: %.1f KiB" % (len(other), size / 1024))
total = sum(stat.size for stat in top_stats)
print("Total allocated size: %.1f KiB" % (total / 1024))
class VerticalMenu(bs.MyMultiLineAction):
" Main Menu "
def __init__(self, *args, **keywords):
super().__init__(*args, **keywords)
self.how_exited = True
def actionHighlighted(self, act_on_this, keypress):
"Select by arrows + Enter key."
form = self.parent
if act_on_this[0] == "1": # Book selector
form.menuBookSelector()
elif act_on_this[0] == "2": # Author selector
form.menuAuthorSelector()
elif act_on_this[0] == "3": # Publisher selector
form.menuPublisherSelector()
elif act_on_this[0] == "4": # Warehouse selector
form.menuWarehouseSelector()
elif act_on_this[0] == "5": # Book listing
form.menuBookListing()
elif act_on_this[0] == "6": # Utilities
form.menuUtilities()
elif act_on_this[0] == "Q": # Quit program
form.exitApplication()