Skip to content

Commit

Permalink
Merge pull request #46 from matham/master
Browse files Browse the repository at this point in the history
Add compat module, remove decoding of strings in notification
  • Loading branch information
akshayaurora committed Mar 20, 2014
2 parents d29cbdc + 8dcaa19 commit d33b9ac
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 16 deletions.
10 changes: 8 additions & 2 deletions examples/notification/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,19 @@

from plyer import notification
from plyer.utils import platform
from plyer.compat import PY2


class NotificationDemo(BoxLayout):

def do_notify(self, mode='normal'):
kwargs = {'title': self.ids.notification_title.text,
'message': self.ids.notification_text.text}
title = self.ids.notification_title.text
message = self.ids.notification_text.text
if PY2:
title = title.decode('utf8')
message = message.decode('utf8')
kwargs = {'title': title, 'message': message}

if mode == 'fancy':
kwargs['app_name'] = "Plyer Notification Example"
if platform == "win":
Expand Down
34 changes: 34 additions & 0 deletions plyer/compat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
'''
Compatibility module for Python 2.7 and > 3.3
=============================================
'''

__all__ = ('PY2', 'string_types', 'queue', 'iterkeys',
'itervalues', 'iteritems')

import sys
try:
import queue
except ImportError:
import Queue as queue

#: True if Python 2 intepreter is used
PY2 = sys.version_info[0] == 2

#: String types that can be used for checking if a object is a string
string_types = None
text_type = None
if PY2:
string_types = basestring
text_type = unicode
else:
string_types = text_type = str

if PY2:
iterkeys = lambda d: d.iterkeys()
itervalues = lambda d: d.itervalues()
iteritems = lambda d: d.iteritems()
else:
iterkeys = lambda d: iter(d.keys())
itervalues = lambda d: iter(d.values())
iteritems = lambda d: iter(d.items())
20 changes: 11 additions & 9 deletions plyer/platforms/win/libs/balloontip.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@

import time
import ctypes
import win_api_defs
from plyer.platforms.win.libs import win_api_defs
from plyer.compat import PY2
from threading import RLock


Expand Down Expand Up @@ -56,8 +57,10 @@ def __init__(self, title, message, app_name, app_icon='', timeout=10):
'''

wnd_class_ex = win_api_defs.get_WNDCLASSEXW()
wnd_class_ex.lpszClassName = ('PlyerTaskbar' +
str(WindowsBalloonTip._get_unique_id())).decode('utf8')
class_name = 'PlyerTaskbar' + str(WindowsBalloonTip._get_unique_id())
if PY2:
class_name = class_name.decode('utf8')
wnd_class_ex.lpszClassName = class_name
# keep ref to it as long as window is alive
wnd_class_ex.lpfnWndProc =\
win_api_defs.WindowProc(win_api_defs.DefWindowProcW)
Expand All @@ -81,11 +84,11 @@ def __init__(self, title, message, app_name, app_icon='', timeout=10):
# load icon
if app_icon:
icon_flags = LR_LOADFROMFILE | LR_DEFAULTSIZE
hicon = win_api_defs.LoadImageW(None, app_icon.decode('utf8'),
IMAGE_ICON, 0, 0, icon_flags)
hicon = win_api_defs.LoadImageW(None, app_icon, IMAGE_ICON, 0, 0,
icon_flags)
if hicon is None:
raise Exception('Could not load icon {}'.
format(icon_path_name).decode('utf8'))
format(icon_path_name))
self._balloon_icon = self._hicon = hicon
else:
self._hicon = win_api_defs.LoadIconW(None,
Expand Down Expand Up @@ -119,9 +122,8 @@ def notify(self, title, message, app_name):
if self._balloon_icon is not None:
icon_flag = NIIF_USER | NIIF_LARGE_ICON
notify_data = win_api_defs.get_NOTIFYICONDATAW(0, self._hwnd,
id(self), flags, 0, hicon, app_name.decode('utf8')[:127], 0, 0,
message.decode('utf8')[:255], NOTIFYICON_VERSION_4,
title.decode('utf8')[:63], icon_flag, win_api_defs.GUID(),
id(self), flags, 0, hicon, app_name, 0, 0, message,
NOTIFYICON_VERSION_4, title, icon_flag, win_api_defs.GUID(),
self._balloon_icon)

self._notify_data = notify_data
Expand Down
11 changes: 6 additions & 5 deletions plyer/platforms/win/libs/win_api_defs.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,13 @@
'DestroyWindow', 'LoadIconW')

import ctypes
from ctypes import Structure, windll, sizeof, byref, POINTER, memset,\
WINFUNCTYPE
from ctypes.wintypes import DWORD, HICON, HWND, UINT, WCHAR, WORD, BYTE,\
HRESULT, LPCWSTR, LPWSTR, INT, LPVOID, HINSTANCE, HMENU, LPARAM, WPARAM,\
HBRUSH, HMODULE, ATOM, BOOL, HANDLE, LONG, HHOOK
from ctypes import (Structure, windll, sizeof, byref, POINTER, memset,
WINFUNCTYPE)
from ctypes.wintypes import (DWORD, HICON, HWND, UINT, WCHAR, WORD, BYTE,
LPCWSTR, LPWSTR, INT, LPVOID, HINSTANCE, HMENU, LPARAM, WPARAM,
HBRUSH, HMODULE, ATOM, BOOL, HANDLE, LONG, HHOOK)
LRESULT = LPARAM
HRESULT = HANDLE
HCURSOR = HICON


Expand Down

0 comments on commit d33b9ac

Please sign in to comment.