Skip to content

Instantly share code, notes, and snippets.

@hzno
Created November 25, 2010 23:12
Show Gist options
  • Save hzno/716055 to your computer and use it in GitHub Desktop.
Save hzno/716055 to your computer and use it in GitHub Desktop.
(epynotify) Module to be able to use "pynotify" easily.
#-*- coding:utf-8 -*-
# Author: Iyori Komiyama
# Contact: hazimarino@gmail.co.jp
# site: http://hazimarino.blogspot.com/
"""\
Module to be able to use 'pynotify' easily.
Classes:
_EPYNotify
Functions:
epynotify(title='', message='', image=None)
find_icon(key='dialog', size='48')
messages(title, message='', image=None)
"""
import os, os.path
import datetime
try:
import pynotify
if not pynotify.init('epynotify'):
raise
except ImportError as e:
print("{0} : Please install pynotify".format(e))
except:
print("there was a problem initializing the pynotify module")
__all__ = ['epynotify', 'find_icon', 'messages']
__version__ = '0.5.1'
class _EPYNotify(pynotify.Notification):
""" EPYNotify(title, message='', image=None)
title -> a title displayed in pynotify
message -> a message is displayed (It is possible to omit it).
image -> choose a number from 0 to 4. or icon name
"""
def __init__(self, title, message='', image=None):
self.title = title
self.message = message
self.image = image
self.mddir = os.path.dirname(os.path.abspath(__file__))
self.icons = {0: "dialog-information", 1: "distributor-logo",
2: "un-reboot", 3: "dialog-error", 4: "gnome-run"}
if isinstance(self.image, int):
self.image = self.icons[self.image]
elif self.image.startswith('/'): pass
elif '.' in self.image:
self.image = '{0}/{1}'.format(self.mddir, self.image)
pynotify.init(self.title)
pynotify.Notification.__init__(self, self.title, self.message, self.image)
def find_icon(key='dialog', size='48'):
"""\
It looks for a icon.
"""
if os.path.exists("/usr/share/icons"):
icon_path = "/usr/share/icons"
num = 0
gicons = ((os.path.join(i[0], j) for j in i[2] if key in j)
for i in os.walk(icon_path) if size in i[0])
for icl in gicons:
for ic in icl:
if icl:
print(ic)
num += 1
else:
print("{0} icons were found.".format(num))
else:
print("Not found file path.")
def epynotify(title='', message='', image=None):
"""epynotify([string[, string[, absolute-icon-path]]])
title='' notify's title
message='' notify's message
image=None default icon is num 0.
"""
if not title:
title = 'Hello, World'
message = 'test'
image = 0
notify = _EPYNotify(title, message, image)
notify.show()
else:
notify = _EPYNotify(title, message, image)
notify.show()
def messages(title, message='', image=None):
"""\
The message and time are displayed in "Console" with "Notification".
"""
dt = datetime.datetime.now()
if not message:
message = 'Hello, World'
image = 3
notify = _EPYNotify(title, message, image)
notify.show()
print('*' * 80)
print('\t{0}\n'.format(title))
else:
notify = _EPYNotify(title, message, image)
notify.show()
print('*' * 80)
print('\t{0}\n\t{1}\n'.format(title, 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
# test
epynotify()
sleep(10)
epynotify("Hello", image=4)
sleep(10)
messages('Looks for a icon', 'It looks for a icon.', 2)
find_icon()
if __name__ == '__main__':
demo()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment