7. Email APIs
This document describes the nprstuff email API, which provides the lower-level back-end functionality for rest_email. These modules live in nprstuff.npremail.
The main module implements a bunch of different things
validating and converting reStructuredText strings to rich HTML.
doing stuff with Imgur credentials.
doing stuff with Google OAuth2 authentication to services that will send email, and get google contacts, using Google’s APIs.
A single convenience widget,
HtmlView, that acts as a rudimentary browser that can display rich HTML.
- class nprstuff.npremail.HtmlView(parent, htmlString='')
A convenient PyQt5 widget that displays rich and interactive HTML (HTML with CSS and Javascript). This extends
QWebEngineView. This defines new actions, with shortcuts, to move forward one page, backward one page, or reset.- Parameters:
- Variables:
initHTMLString (str) – this widget has the ability to reset to the initial HTML web page used to construct it. This attribute stores that rich HTML as a
string.
- reset()
Reverts to the initial website.
- waitUntilReady()
A subordinate process that loads all website elements (CSS and Javascript) until done.
- nprstuff.npremail.check_imgurl_credentials(clientID, clientSECRET, clientREFRESHTOKEN, verify=True)
validate the Imgur API credentials with the provided API client ID, secret, and refresh token.
- Parameters:
- Returns:
whether the new credentials are correct.
- Return type:
See also
- nprstuff.npremail.format_size(fsize_b)
Formats the bytes value into a string with KiB, MiB or GiB units. This code has been copied from
deluge's format_size.- Parameters:
fsize_b (int) – the filesize in bytes.
- Returns:
formatted string in KiB, MiB or GiB units.
- Return type:
Usage
>>> format_size( 112245 ) '109.6 KiB'
- nprstuff.npremail.get_imgurl_credentials()
retrieves the Imgur API credentials from the SQLite3 configuration database.
{ 'clientID': XXXX, 'clientSECRET': XXXX, 'clientREFRESHTOKEN': XXXX, 'mainALBUMID': XXXX, 'mainALBUMTITLE': XXXX }
- Return type:
See also
- nprstuff.npremail.md5sum(filename)
Taken from This StackOverflow on how to calculate the MD5 sum of a file.
- nprstuff.npremail.oauthGetGoogleCredentials(verify=True)
Gets the Google Oauth2 credentials, stored in the SQLite3 configuration database, in the form of a refreshed
Credentialsobject. This OAuth2 authentication method is used for ALL the services accessed by NPRStuff.- Parameters:
verify (bool) – optional argument, whether to verify SSL connections. Default is
True.- Returns:
a
Credentialsform of the Google Oauth2 credentials for various OAuth2 services.- Return type:
See also
oauthCheckGoogleCredentials.
- nprstuff.npremail.oauth_generate_google_permission_url()
Generates a Google OAuth2 web-based flow for all the Google services used in NPRStuff. The authentication process that uses this flow is described in this subsection. Here are the programmatic steps to finally generate an
Credentialsobject.Get the
Flowand authentication URI.flow, auth_uri = oauth_generate_google_permission_url( )
Go to the URL,
auth_uri, in a browser, grant permissions, and copy the authorization code in the browser window. This authorization code is referred to asauthorization_code.Create the
Credentialsusingauthorization_code.fetch_token( code = authorization_code ) credentials = flow.credentials
- Returns:
a
tupleof two elements. The first element is aFlowweb server flow object. The second element is the redirection URIstringthat redirects the user to begin the authorization flow.- Return type:
See also
oauthCheckGoogleCredentials.
- nprstuff.npremail.oauth_store_google_credentials(credentials)
Store the form of a
Credentialsobject, as a JSON string, into the SQLite3 configuration database.- Parameters:
credentials – the
Credentialsobject to store into the database.
See also
oauthCheckGoogleCredentials.
- nprstuff.npremail.store_imgurl_credentials(clientID, clientSECRET, clientREFRESHTOKEN, verify=True, mainALBUMID=None, mainALBUMNAME=None)
stores the Imgur API credentials into the SQLite3 configuration database.
- Parameters:
- Returns:
the string
"SUCCESS"if could store the new Imgur credentials. Otherwise, the string'ERROR, COULD NOT STORE IMGURL CREDENTIALS.'.- Return type:
See also
7.1. email module
This module contains common utilities to retrieve contacts, craft email, and send email. This module lives in nprstuff.npremail.npremail.
- nprstuff.npremail.npremail.get_all_email_contacts_dict(verify=True, people_service=None, pagesize=4000)
Returns all the Google contacts using the Google Contacts API.
- Parameters:
verify (bool) – optional argument, whether to verify SSL connections. Default is
True.people_service – optional argument, the
Resourcerepresenting the Google people service used to retrieve one’s Google contacts. IfNone, then generated here.pagesize (int) – optional argument, the maximum number of candidate contacts to search through. Must be \(\ge 1\).
- Returns:
a
dictof contacts. The key is the contact name, and the value is thesetof email addresses for that contact.- Return type:
- nprstuff.npremail.npremail.get_email_service(verify=True, credentials=None)
This returns a working
Resourcerepresenting the Google email service used to send and receive emails.- Parameters:
verify (bool) – optional argument, whether to verify SSL connections. Default is
True.- Returns:
the
Resourcerepresenting the Google email service used to send and receive emails.- Return type:
Resource
- nprstuff.npremail.npremail.get_people_service(verify=True, credentials=None)
This returns a working
Resourcerepresenting the Google people service used to get a list of one’s Google contacts.- Parameters:
verify (bool) – optional argument, whether to verify SSL connections. Default is
True.- Returns:
the
Resourcerepresenting the Google people service.- Return type:
Resource
- nprstuff.npremail.npremail.send_collective_email_full(mainHTML, subject, fromEmail, to_emails, cc_emails, bcc_emails, verify=True, email_service=None, attachments=[])
Sends the HTML email to the following
TOrecipients,CCrecipients, andBCCrecipients altogether. It uses the GMail API.- Parameters:
subject (str) – the email subject.
verify (bool) – optional argument, whether to verify SSL connections. Default is
True.email_service – optional argument, the
Resourcerepresenting the Google email service used to send and receive emails. IfNone, then generated here.attachments (list) – the collection of attachments to send out.
- nprstuff.npremail.npremail.send_email_lowlevel(msg, email_service=None, verify=True)
Sends out an email using the Google Contacts API. If process is unsuccessfull, prints out an error message,
"problem with <TO-EMAIL>", where<TO-EMAIL>is the recipient’s email address.- Parameters:
msg (MIMEMultiPart) – the
MIMEMultiPartemail message to send. At a high level, this is an email with body, sender, recipients, and optional attachments.email_service – optional argument, the
Resourcerepresenting the Google email service used to send and receive emails. IfNone, then generated here.verify (bool) – optional argument, whether to verify SSL connections. Default is
True.userId (str) – The user ID to send email as, using the Google Mail API. Can be an RFC 2047 sender’s email with name, but default is
me. See this page for info on method signature.
See also
get_email_service.
- nprstuff.npremail.npremail.send_individual_email_full(mainHTML, subject, emailAddress, name=None, attach=None, attachName=None, attachType='txt', verify=True, email_service=None)
Sends the HTML email, with optional single attachment, to a single recipient email address, using the GMail API. Unlike
send_individual_email_full_withsingleattach, the attachment type is also set.- Parameters:
subject (str) – the email subject.
emailAddress (str) – the recipient email address.
name (str) – optional argument. If given, the recipient’s name.
mydate (date) – optional argument. The
dateat which the email is sent. Default isnow( ).attach (str) – optional argument. If defined, the Base64 encoded attachment.
attachName (str) – optional argument. The
listof attachment names, if there is an attachment. If defined, thenattachDatamust also be defined.attachType (str) – the attachment type. Default is
txt.verify (bool) – optional argument, whether to verify SSL connections. Default is
True.email_service – optional argument, the
Resourcerepresenting the Google email service used to send and receive emails. IfNone, then generated here.
7.2. email_imgur module
This module hooks into the Imgur account, and the albums and images in albums, that it contains. It also implements widgets in rest_email that manipulate the images in one’s Imgur album. This module lives in nprstuff.npremail.email_imgur.
- class nprstuff.npremail.email_imgur.NPRStuffIMGClient(verify=True, data_imgurl=None)
This object contains and implements the collection of images located in a single main album in the Imgur account. This uses the Imgur API to peform all operations. This object is constructed using Imgur credentials – the client ID, secret, and refresh token – stored in the SQLite3 configuration database, and stores the following attributes: the client ID, secret, refresh token, and the access token used for API access to your Imgur album and images.
If the Imgur albums cannot be accessed, or there are no albums, then
self.albumID = None, theself.imgHashesis an emptydict.The main album ID is stored as a
stringin the Imgur configuration under themainALBUMIDkey, if it exists; otherwise the Imgur configuration dictionary does not have amainALBUMIDkey.If there is no main album ID defined, or if there is no album with that ID, then we choose the first album found, and reset the Imgur configuration data into the SQLite3 database with this album ID and name.
If the configured album exists in our Imgur library, then continue with this the main album.
Once the main album is found, populate
self.imghasheswith all the pictures in this album. The pictures in an album in our Imgur account are expected to be filled through methods in this object.The key is the MD5 hash of the image in that library.
The value is a four element
tuple: image name, image ID, the URL link to this image, and thedatetimeat which the image was uploaded.
- Parameters:
verify (bool) – optional argument, whether to verify SSL connections. Default is
True.data_imgurl (dict) – optional argument. If defined, must have the following keys:
clientID,clientSECRET, andclientREFRESHTOKEN. Must be consistent withdictreturned byget_imgurl_credentials.
- Variables:
- Raises:
ValueError – if images in the new album cannot be accessed.
See also
refreshImages.- change_album_name(new_album_name)
Changes the main album name to a new name, only if the new name is different from the old name.
See also
refreshImages.
- change_name(imgMD5, new_name)
Changes the name of an image in the main Imgur library.
- Parameters:
- Returns:
Trueif image could be found and its name changed. Otherwise returnsFalse.- Return type:
See also
- delete_candidate_album(candidate_album_name)
This deletes the candidate album from the Imgur account. This album with that name must exist in the Imgur account.
- Parameters:
candidate_album_name (str) – the name of the album to remove, with its underlying images.
See also
refreshImages.
- delete_image(b64img, imgMD5=None)
Removes an image from the main Imgur library.
- Parameters:
- Returns:
Trueif image can be found and returned. Otherwise returnsFalse.- Return type:
See also
- get_candidate_album_names()
- Returns:
a
listof album names in the Imgur account.set_main_albumcan use this method to determine the valid album name to choose.
See also
get_candidate_albums.
- get_candidate_albums()
- classmethod get_image_md5(image)
- Returns:
the MD5 hash of the image.
- Parameters:
image (
PngImageFile) – the native Pillow PNG image object.
- get_main_album_name()
- refreshImages()
Refreshes the collection of images in the main Imgur album, by filling out
self.imghashes. The pictures in an album in our Imgur account are expected to be filled through methods in this object.
- set_main_album(new_album_name)
Sets or changes the main Imgur album used for storing and displaying images to a new album name. If
new_album_nameexists in the Imgur account, then sets that name. Ifnew_album_namedoes not exist, then creates this new Imgur album.Once this album is set or created,
sets the new Imgur credentials using
store_imgur_credentials.populates
self.imghasheswith all the images found in this library. If the album does not exist, thenself.imghashesis an emptydict.
- Parameters:
new_album_name (str) – the new name of the Imgur album to use for images.
- Raises:
ValueError – if images in the new album cannot be accessed.
See also
refreshImages.
- upload_image(b64img, name, imgMD5=None)
Uploads a Base64 encoded file into the main Imgur album. If the image exists, then returns information (from
self.imghashes) about the file. If not, create it, put it intoself.imghashes, and then return its information.- Parameters:
- Returns:
a 4-element
tuple: image name, image ID, the URL link to this image, and thedatetimeat which the image was uploaded.- Return type:
See also
- class nprstuff.npremail.email_imgur.PNGPicDelegate
- createEditor(self, parent: QWidget, option: QStyleOptionViewItem, index: QModelIndex) QWidget
- setEditorData(self, editor: QWidget, index: QModelIndex)
- class nprstuff.npremail.email_imgur.PNGPicLabel(img, parent=None)
This class that extends
QLabelfollows advice from this StackOverflow article. This object also generates its ownQPixMapfrom theQImageit keepts.- Parameters:
- Variables:
img – the source
QImageused to regenerate its own backgroundQPixMap.
- class nprstuff.npremail.email_imgur.PNGPicObject(initdata, pImgClient)
This provides a GUI widget to the Imgur interface implemented in
PlexIMGClient. Initializaton of the image can either upload this image to the Imgur account, or retrieve the image from the main Imgur album. This object can also launch a GUI dialog window throughgetInfoGUI.- Parameters:
initdata (dict) –
the low-level dictionary that contains important information on the image, located in a file, that will either be uploaded into the main Imgur album or merely kept in memory. The main key that determines operation is
initialization. It can be one of"FILE"or"SERVER".If
initializationis"FILE", then upload the the image to the main album in the Imgur account. Here are the required keys ininitdata.filenameis the location of the image file on disk.actNameis the PNG filename to be used. It must end inpng.
If
initializationis"SERVER", then retrieve this image from the main album in the Imgur account. Here are the required keys ininitdata.pImgClient (PlexIMGClient) – the
PlexIMGClientused to access and manipulate (add, delete, rename) images in the main Imgur album.
- Variables:
actName (str) – the file name without full path, which must end in
png.img (QImage) – the
QImagerepresentation of this image.originalImage (Image) – the
Imagerepresentation of this image.originalWidth (float) – the inferred width in cm.
currentWidth (float) – the current image width in cm. It starts off as equal to
originalWidthb64string (str) – the Base64 encoded representation of this image as a PNG file.
imgurlLink (str) – the URL link to the image.
imgDateTime (datetime) – the
datetimeat which this image was first uploaded to the main album in the Imgur account.
- Raises:
ValueError – if
initdata['initialization']is neither"FILE"nor"SERVER".
- b64String()
- changeName(new_name, nImgClient)
changes the filename into a new name.
- Parameters:
new_name (str) – the new name of the image file to be changed in the main album on the Imgur account. This must end in
png.nImgClient (NPRStuffIMGClient) – the
NPRStuffIMGClientused to access and manipulate (add, delete, rename) images in the main Imgur album.
- classmethod createPNGPicObjects(pImgClient)
- Parameters:
pImgClient (PlexIMGClient) – the
PlexIMGClientused to access and manipulate (add, delete, rename) images in the main Imgur album.- Returns:
a
listofPNGPicObjectrepresenting the images in the main Imgur album.- Return type:
- getInfoGUI(parent)
Launches a
QDialogthat contains the underlying image and some other labels:ACTNAMEis the actual PNG file name,URLis the image’s Imgur link, andUPLOADED ATis the date and time at which the file was uploaded. An example image is shown below,
Fig. 7.1 An example PNG image that can be stored in the main Imgur library. Note the three rows above the image: the name of the PNG image; its URL; and the date and time it was uploaded.
- class nprstuff.npremail.email_imgur.PNGPicQSortFilterModel(model)
- class nprstuff.npremail.email_imgur.PNGPicTableModel(parent)
-
- flags(self, index: QModelIndex) Qt.ItemFlags
- class nprstuff.npremail.email_imgur.PNGPicTableView(parent)
- contextMenuEvent(self, a0: QContextMenuEvent)
- resizeEvent(self, e: QResizeEvent)
- class nprstuff.npremail.email_imgur.PNGWidget(parent)