5. Top Level API
This document describes the lower level NPRStuff API, upon which all the command line and GUI tools are based. It lives in nprstuff.
This module implements the lower-level functionality that does or has the following:
access and retrieve configuration and other data from an SQLite3 database using SQLAlchemy object relational mapping (ORM) classes. The SQLite3 database is stored in
~/.config/nprstuff/app.db.NPRStuffConfigis an ORM class that stores configuration information.create_allinstantiates necessary SQLite3 tables in the configuration table if they don’t already exist.low level PyQt5 derived widgets used for the other GUIs in NPRStuff:
ProgressDialog,QDialogWithPrinting, andQLabelWithSave.
- class nprstuff.NPRStuffConfig(**kwargs)
This SQLAlchemy ORM class contains the configuration data used for running all the NPRStuff tools. Stored into the
nprconfigtable in the SQLite3 configuration database.- Variables:
service – the name of the configuration service we store. Index on this unique key. This is a
Columncontaining aStringof size 65536.data –
the JSON formatted information on the data stored here. For instance, username and password can be stored in the following way
{ 'username' : USERNAME, 'password' : PASSWORD }
This is a
Columncontaining aJSONobject.
- class nprstuff.ProgressDialog(parent, windowTitle='', doQuit=True)
A convenient PyQt5 widget, inheriting from
QDialogWithPrinting, that acts as a GUI blocking progress window for longer lasting operations. Like its parent class, this dialog widget is also resizable. This shows the passage of the underlying slow process in 5 second increments.This progress dialog exposes three methods –
addText,stopDialog, andstartDialog– to which a customQThreadobject can connect.startDialogis triggered on long operation start, sometimes with an initial message.addTextis triggered when some intermediate progress text must be returned.stopDialogis triggered on process end.
- Parameters:
parent (
QWidget) – the parentQWidgeton which this dialog widget blocks.windowTitle (str) – the label to put on this progress dialog in an internal
QLabel.doQuit (bool) – if
True, then using the quit shortcuts (EscorCtrl+Shift+Q) will cause the underlying program to exit. Otherwise, hide the progress dialog.
- Variables:
mainDialog (
QTextEdit) – the main dialog widget in this GUI.parsedHTML (
BeautifulSoup) – theBeautifulSoupstructure that contains the indexable tree of progress dialogs.elapsedTime (
QLabel) – the bottomQLabelwidget that displays how much time (in seconds) has passed.timer (
QTimer) – theQTimersub-thread that listens every 5 seconds before emitting a signal.t0 (float) – the UNIX time, in seconds with resolution of microseconds.
- addText(text)
adds some text to this progress dialog window.
- Parameters:
text (str) – the text to add.
- showTime()
method connected to the internal
timerthat prints out how many seconds have passed, on the underlyingelapsedTimeQLabel.
- startDialog(initString='')
starts running the progress dialog, with an optional labeling string, and starts the timer.
- Parameters:
initString (str) – optional internal labeling string.
- stopDialog()
stops running, and hides, this progress dialog.
- class nprstuff.ProgressDialogThread(parent, title)
This subclassing of
QThreadprovides a convenient scaffolding to run, in a non-blocking fashion, some long-running processes with an asssociatedProgressDialogwidget.Subclasses of this object need to have a particular structure for their
__init__method. The first three arguments MUST beself,parent,selfis a reference to this object.parentis the parentQWidgetto which theProgressDialogattribute, namedprogress_dialog, is the child.titleis the title ofprogress_dialog. Here is an example, where an example class namedProgressDialogThreadChildClassinherits fromProgressDialogThread.def __init__( self, parent, *args, **kwargs ): super( ProgressDialogThreadChildClass, self ).__init__( parent, title ) # own code to initialize based on *args and **kwargs
This thing has an associated
runmethod that is expected to be partially implemented in the following manner for subclasses ofProgressDialogThread.It must start with
self.progress_dialog.show( )to show the progress dialog widget.It must end with this command,
self.stopDialog.emit( )to hide the progress dialog widget.
Here is an example.
def run( self ): self.progress_dialog.show( ) # run its own way self.stopDialog.emit( )
In the
runmethod, if one wants to print out something intoprogess_dialog, then there should be these types of commands inrun:self.emitString.emit( mystr ), wheremystris astrmessage to show inprogress_dialog, andemitStringis apyqtsignalconnected to theprogress_dialogobject’saddText( ).- Parameters:
- Variables:
emitString – the signal, with
strsignature, that is triggered to send progress messages intoprogress_dialog.stopDialog – the signal that is triggered to stop the
progress_dialog, callingstopDialog.startDialog – the signal, with
strsignature, that is triggered to restart theprogress_dialogwidget, callingstartDialog.progress_dialog – the GUI that shows, in a non-blocking fashion, the progress on some longer-running method.
time0 (int) – a convenience attribute, the UTC time at which the
progress_dialogobject was first instantiated. Can be used to determine the time each submethod takes (for example,time.time( ) - self.time0).
See also
- class nprstuff.QDialogWithPrinting(parent, isIsolated=True, doQuit=True)
A convenient PyQt5 widget, inheriting from
QDialog, that allows for screen grabs and keyboard shortcuts to either hide this dialog window or quit the underlying program. This PyQt5 widget is also resizable, in relative increments of 5% larger or smaller, to a maximum of \(1.05^5\) times the initial size, and to a minimum of \(1.05^{-5}\) times the initial size.- Parameters:
parent (
QWidget) – the parentQWidgetto this dialog widget.isIsolated (bool) – If
True, then this widget is detached from its parent. IfFalse, then this widget is embedded into a layout in the parent widget.doQuit (bool) – if
True, then using the quit shortcuts (EscorCtrl+Shift+Q) will cause the underlying program to exit. Otherwise, hide the progress dialog.
- Variables:
indexScalingSignal – a
pyqtSignalthat can be connected to other PyQt5 events or methods, if resize events want to be recorded.initWidth (int) – the initial width of the GUI in pixels.
initHeight (int) – the initial heigth of the GUI in pixels.
- makeBigger()
makes the widget incrementally 5% larger, for a maximum of \(1.05^5\), or approximately 28% larger than, the initial size.
- makeSmaller()
makes the widget incrementally 5% smaller, for a minimum of \(1.05^{-5}\), or approximately 28% smaller than, the initial size.
- resetSize()
reset the widget size to the initial size.
- reset_sizes()
Sets the default widget size to the current size.
- screenGrab()
take a screen shot of itself and saver to a PNG file through a
QFileDialogwidget.See also
- class nprstuff.QLabelWithSave(parent=None)
A convenient PyQt5 widget that inherits from
QLabel, but allows screen shots.- contextMenuEvent(event)
Constructs a context menu with a single action, Save Pixmap, that takes a screen shot of this widget, using
screenGrab.- Parameters:
event (QEvent) – default
QEventargument needed to create a context menu. Is not used in this reimplementation.
- screenGrab()
take a screen shot of itself and save to a PNG file through a
QFileDialogwidget.See also
- nprstuff.baseConfDir = '/home/tanim/.config/nprstuff'
the directory where NPRStuff user data is stored –
~/.config/nprstuff.
- nprstuff.create_all()
creates the necessary SQLite3 tables into the database file
~/.config/nprstuff/app.dbif they don’t already exist, but only if not building documentation in Read the docs.