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
.NPRStuffConfig
is an ORM class that stores configuration information.create_all
instantiates 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
nprconfig
table in the SQLite3 configuration database.- Variables:
service – the name of the configuration service we store. Index on this unique key. This is a
Column
containing aString
of 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
Column
containing aJSON
object.
- 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 customQThread
object can connect.startDialog
is triggered on long operation start, sometimes with an initial message.addText
is triggered when some intermediate progress text must be returned.stopDialog
is triggered on process end.
- Parameters:
parent (
QWidget
) – the parentQWidget
on 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 (Esc
orCtrl+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
) – theBeautifulSoup
structure that contains the indexable tree of progress dialogs.elapsedTime (
QLabel
) – the bottomQLabel
widget that displays how much time (in seconds) has passed.timer (
QTimer
) – theQTimer
sub-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
timer
that prints out how many seconds have passed, on the underlyingelapsedTime
QLabel
.
- 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
QThread
provides a convenient scaffolding to run, in a non-blocking fashion, some long-running processes with an asssociatedProgressDialog
widget.Subclasses of this object need to have a particular structure for their
__init__
method. The first three arguments MUST beself
,parent
,self
is a reference to this object.parent
is the parentQWidget
to which theProgressDialog
attribute, namedprogress_dialog
, is the child.title
is the title ofprogress_dialog
. Here is an example, where an example class namedProgressDialogThreadChildClass
inherits 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
run
method 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
run
method, if one wants to print out something intoprogess_dialog
, then there should be these types of commands inrun
:self.emitString.emit( mystr )
, wheremystr
is astr
message to show inprogress_dialog
, andemitString
is apyqtsignal
connected to theprogress_dialog
object’saddText( )
.- Parameters:
- Variables:
emitString – the signal, with
str
signature, 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
str
signature, that is triggered to restart theprogress_dialog
widget, 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_dialog
object 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 parentQWidget
to 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 (Esc
orCtrl+Shift+Q
) will cause the underlying program to exit. Otherwise, hide the progress dialog.
- Variables:
indexScalingSignal – a
pyqtSignal
that 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
QFileDialog
widget.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
QEvent
argument 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
QFileDialog
widget.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.db
if they don’t already exist, but only if not building documentation in Read the docs.