Skip to content

Instantly share code, notes, and snippets.

@podhmo
Created December 7, 2010 17:24
Show Gist options
  • Save podhmo/732088 to your computer and use it in GitHub Desktop.
Save podhmo/732088 to your computer and use it in GitHub Desktop.
#-*- coding:utf-8 -*-
import os.path
import datetime
import pynotify
import collections #namedtuple
DEFAULT_TITLE = 'Hello, World!'
DEFAULT_MESSAGE = ''
DIR = os.path.dirname(os.path.abspath(__file__))
DEFAULT_IMAGE = os.path.join(DIR, "attention.png")
ActionState = collections.namedtuple('ActionState', 'title message image')
class ActionHandler:
def __init__(self, *action_clses):
self.queue = list(action_clses)
def add(self, action_cls):
self.queue.append(action_cls)
return self # for method chainnig
def run(self,state, clean=False):
for action_cls in self.queue:
action_cls(state).run()
if clean:
self.queue = []
class Action:
def __init__(self, state):
self.state = state
self.varidate()
def varidate(self):
state = self.state
for attr in ("image", "message", "title"):
if not getattr(state, attr):
raise NotImplemented(attr)
class NotifyAction(Action):
def __init__(self, state):
Action.__init__(self, state)
def run(self):
""" show Notification """
state = self.state
pynotify.init(state.title)
notfy = pynotify.Notification(state.title, state.message, state.image)
notfy.show()
class MessageAction(Action):
def __init__(self, state):
Action.__init__(self, state)
def run(self):
state = self.state
dt = datetime.datetime.now()
if not state.message:
print('*' * 80)
print('\t{0}\n'.format(state.title))
else:
print('*' * 80)
print('\t{0}\n\t{1}\n'.format(state.title, state.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)
arg = ActionState(title="test", message="Hello, World", image=DEFAULT_IMAGE)
ach.run(arg)
sleep(5)
ach.add(MessageAction)
ach.run(ActionState(title="test", message="test", image=DEFAULT_IMAGE))
sleep(5)
if __name__ == '__main__':
demo()
@podhmo
Copy link
Author

podhmo commented Dec 8, 2010

ActionはABC使った方が良いのかな?こんなvaridateとかして意味があるのか疑問だけれど。

@podhmo
Copy link
Author

podhmo commented Dec 8, 2010

ほんとうは、default値を指定して省略していきたいのだけれど。propertyとか弄っていけば良いのかな?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment