PK!0siphono_notify/__init__.py"""Vanity imports for the package.""" from siphono_notify.notifier import Notifier from siphono_notify.progress_bar import ProgressBar from siphono_notify.target import Target __version__ = '0.1.0' PK!siphono_notify/client.py"""Client module for messaging backend notify api.""" import requests from siphono_notify import configs def send_notification( target_token: str, group_name: str, title: str, body: str ): """ Send a notification at the specified target. :param target_token: A token identifying the device to send the notification to. :param group_name: The name of the group associated with the notification. This should be useful in identifing the process send the message. :param title: The title of the notification. :param body: The main content of the notification. """ url = '{host}/targets/{id}/notifications'.format( host=configs.NOTIFY_API, id=target_token, ) requests.post( url, json={'group': group_name, 'title': title, 'body': body} ) def send_progress_bar( target_token: str, name: str, progress: float, message: str = None, ): """ Send a progress bar to the specified target. :param target_token: A token identifying the device to send the notification to. :param name: The name of the progress bar. Ths should identify the job or process who's progress is being monitored. :param progress: A number from 0 to 1 denoting the progress. 0 correlates to 0% and 1 correlates to 100%. :param message: A short message to associate with the progress bar update. This may be used to denote the stage that the job or process is currently in. """ progress_bar_id = name + '+' + target_token url = '{host}/progress-bars/{id}'.format( host=configs.NOTIFY_API, id=progress_bar_id, ) requests.put(url, json={'message': message, 'progress': progress}) PK!wXJJsiphono_notify/configs.py"""Configuration module.""" NOTIFY_API = 'https://notify-api.siphono.com' PK!˅siphono_notify/notifier.py"""Definition of the notifier element for sending notifications.""" import functools import traceback import typing from siphono_notify import client class Notifier(object): """A named notifier aimed at a specific target.""" def __init__(self, target_token: str, name: str): """ Initialize the notifier. :param target_token: The siphono notify token. This is expected to be composed of 4 words. i.e. correct-horse-battery-staple. :param name: The name of the notifier. This should identify the source of the notification. """ self._token = target_token self._name = name def send(self, title: str, message: str): """ Send a notification to the target :param title: The title of the notification being sent. :param message: The message body of the notification being sent """ client.send_notification(self._token, self._name, title, message) def relay_exceptions(self, func: typing.Callable) -> typing.Callable: """ Decorate a function to catch all errors coming from it, send them as notifications, and reraise the error. :param func: The function to decorate. All exceptions originating from the function will be caught and sent as notifications. """ @functools.wraps(func) def wrapper(*args, **kwargs): """ Catch any exception, send it as a notification and then reraise the exception. """ try: return func(*args, **kwargs) except Exception as err: title = str(err) or type(err).__name__ or 'Error' msg = traceback.format_exc(limit=100) client.send_notification(self._token, self._name, title, msg) raise return wrapper PK!)siphono_notify/progress_bar.py"""Module for progress bar.""" from siphono_notify import client class ProgressBar(object): """A named progress bar aimed at a specific target.""" def __init__(self, target_token: str, name: str): """ Initialize the progress bar. :param target_token: The siphono target token. This is expected to be composed of 4 words. i.e. correct-horse-battery-staple. :param name: The name of the progress bar. Ths should identify the job or process who's progress is being monitored. """ self._token = target_token self._name = name self._message = None def update(self, progress: float, message: str = None): """ Update the progress bar. :param progress: A number from 0 to 1 denoting the progress. 0 correlates to 0% and 1 correlates to 100%. :param message: A short message to associate with the progress bar update. This may be used to denote the stage that the job or process is currently in. If no message is given then the previous value will be used. """ self._message = message if message is not None else self._message self._message = None if self._message == '' else self._message client.send_progress_bar( self._token, self._name, progress, self._message ) PK!vsiphono_notify/target.py"""Module defining a messaging target.""" from siphono_notify.notifier import Notifier from siphono_notify.progress_bar import ProgressBar class Target(object): """A target to interact with.""" def __init__(self, token: str): """ Initialize the target. :param token: The siphono notify token. This is expected to be composed of 4 words. i.e. correct-horse-battery-staple. """ self._token = token def notifier(self, name: str) -> Notifier: """ Generate a notifier for the target. :param name: The name to assign to the notifier. :return: A notifier for the target. """ return Notifier(self._token, name) def progress_bar(self, name: str) -> ProgressBar: """ Generate a progress bar for the target. :param name: The name of the progress bar. :return: A progress bar for the target. """ return ProgressBar(self._token, name) PK!HڽTU$siphono_notify-0.1.0.dist-info/WHEEL A н#Z;/"d&F[xzw@Zpy3Fv]\fi4WZ^EgM_-]#0(q7PK!H"0{ 'siphono_notify-0.1.0.dist-info/METADATAOKo0 WI4jĤv {ִٚKR+ A/ e)2)ذn1'i[PK!Ho\6%siphono_notify-0.1.0.dist-info/RECORDˮ@;PA@EXUP׷;ztf+{ In`!D 1yq MI) hyW~0i m]0,Ũ1j^#nOpr'i Ȭ܀K!u˒()}WUK~nF'<6*p~NIx~/A~Dy-]@[y9{תW}F~iS^ίcL4$<ׅp_#SǮw,p\>\k:;HOlV&ߚ}U%{ñ;8bܘ_!mF%_w@ndpKߏS;*e⻂h[͔nGߺY:7CK*'!mdGOCaF޿ԅ) PK!0siphono_notify/__init__.pyPK!siphono_notify/client.pyPK!wXJJEsiphono_notify/configs.pyPK!˅siphono_notify/notifier.pyPK!)siphono_notify/progress_bar.pyPK!vsiphono_notify/target.pyPK!HڽTU$siphono_notify-0.1.0.dist-info/WHEELPK!H"0{ 'tsiphono_notify-0.1.0.dist-info/METADATAPK!Ho\6%siphono_notify-0.1.0.dist-info/RECORDPK