-
-
Save hzno/732267 to your computer and use it in GitHub Desktop.
fork form epynotify by podhmo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#-*- coding:utf-8 -*- | |
import os.path | |
import datetime | |
import pynotify | |
# __all__ = ['epynotify', 'gnotify', 'messages'] | |
# __version__ = '0.0.1' | |
DEFAULT_TITLE = 'Hello, World!' | |
DEFAULT_MESSAGE = '' | |
DIR = os.path.dirname(os.path.abspath(__file__)) | |
DEFAULT_IMAGE = os.path.join(DIR, "attention.png") | |
class ActionHandler: | |
def __init__(self, *action_clses): | |
self.queue = action_clses | |
def add(self, action_cls): | |
self.queue.append(action_cls) | |
return self # for method chainnig | |
def run(self, title, message, image=DEFAULT_IMAGE): | |
for action_cls in self.queue: | |
action_cls(title, message, image).run() | |
self.queue = [] | |
class NotifyAction: | |
def __init__(self, title, message, image=DEFAULT_IMAGE): | |
self.title = title | |
self.message = message | |
self.image = image | |
def run(self): | |
""" show Notification """ | |
pynotify.init(self.title) | |
notfy = pynotify.Notification(self.title, self.message, self.image) | |
notfy.show() | |
class MessageAction: | |
def __init__(self, title, message, image=DEFAULT_IMAGE): | |
self.title = title | |
self.message = message | |
self.image = image | |
def run(self): | |
dt = datetime.datetime.now() | |
if not self.message: | |
print('*' * 80) | |
print('\t{0}\n'.format(self.title)) | |
else: | |
print('*' * 80) | |
print('\t{0}\n\t{1}\n'.format(self.title, self.message)) | |
print('{year}/{month:#02d}/{day:#02d} ' | |
'[{hour:#02d}:{minute:#02d}:{second:#02d}]'.format( | |
year=dt.year, month=dt.month, day=dt.day, hour=dt.hour, | |
minute=dt.minute, second=dt.second)) | |
print('*' * 80) | |
def demo(): | |
from time import sleep | |
ach = ActionHandler(NotifyAction) | |
ach.run('Hello, World!', 'test') | |
sleep(5) | |
ach.add(NotifyAction).add(MessageAction) | |
ach.run('test', 'test') | |
sleep(5) | |
if __name__ == '__main__': | |
demo() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment