Snippets
From Woofgui
is script run from desktop or console
#!/usr/bin/env python import os # Returns '1' for /sbin/init if our script was launched from a desktop-environment and will # return a process-id higher than 1 if the script was launched from a console - This way, # we now have a method to deliver GUI-output / console-output accordingly! parentprocessid = os.getppid()
create notify-bubble using dbus
#!/usr/bin/env python
import dbus
import sys
# Use session bus
session_bus = dbus.SessionBus()
# Use notifications object
notifications_object = session_bus.get_object('org.freedesktop.Notifications',
'/org/freedesktop/Notifications')
notifications_interface = dbus.Interface(notifications_object, 'org.freedesktop.Notifications')
# Sample notification
notification_id = notifications_interface.Notify(sys.argv[0], 0, , 'Sample summary',
'Sample body', dbus.Array([], signature='s'), dbus.Array([], signature='(sv)'), -1)
# Sample notification with actions and hints
notification_id = notifications_interface.Notify(sys.argv[0], 0, , 'Sample summary',
'Sample body', ['sample', 'Sample action'], {'x': 100, 'y': 100}, -1)
create icon in systray with notification-bubble using pynotify
#!/usr/bin/env python
import gtk
import pygtk
import pynotify
def callback(icon):
notification.show()
pynotify.init("Woofgui")
notification = pynotify.Notification("Title", "Text in body", "dialog-warning")
# the dialog-warning specifies icon type
notification.set_urgency(pynotify.URGENCY_NORMAL)
notification.set_timeout(pynotify.EXPIRES_DEFAULT)
# _NEVER sets timeout to never, user has to click
icon = gtk.status_icon_new_from_stock(gtk.STOCK_ABOUT)
icon.connect('activate', callback)
notification.attach_to_status_icon(icon)
gtk.main()
test for desktop-environment
#!/usr/bin/env python
import os
# Returns 'gnome' or 'kde' or ...
desktoptype = os.environ.get('DESKTOP_SESSION')
This won't work I'm afraid, since [1] it appears it needs gdm to set this to gnome. I get the output 'default'. If that is gnome-only, then you can add 'default' as indicator of gnome. Otherwise use for example something like this:
#!/bin/bash
if [ "`ps -e | grep gdm`" ]; then
echo "You're running Gnome"
elif [ "`ps -e | grep kdm`" ]; then
echo "You're running KDE"
else
exit 0
fi
find out user-language
#!/usr/bin/env python
import os
# e.g. returns de_DE.UTF-8
usedlanguage = os.environ.get('LANG')
get user-home directory
#!/usr/bin/env python
import os
# Returns /home/username
homepath = os.environ.get('HOME')
example wxpython textentry
#!/usr/bin/env python
import wx
app = wx.PySimpleApp()
dlg = wx.TextEntryDialog(None, 'Enter value', 'Title', )
if dlg.ShowModal() == wx.ID_OK:
val = dlg.GetValue() # this line should be indented
dlg.Destroy()
print "you have entered %s" % val
Check for system language and according files + new translation function
#!/usr/bin/env python
import gettext
#tell gettext where to look for translation files
gettext.install('woof_ui', './locale', unicode=True)
#check system language
translation = False
syslang = os.environ.get('LANG')[0] + os.environ.get('LANG')[1]
#check for translation files; use if found
if os.path.exists('./locale/%s/LC_MESSAGES/woof_ui.mo' % (syslang)):
gettext.translation('woof_ui', './locale', languages=['%s' % (syslang)]).install()
translation = True
#check if a translation file is installed; if so return the translation of the token, else return the english text
def translate(text, token):
if translation:
return _(token)
else:
return text
#e.g. dialogtitle = translate('Share this file?', 'token1') with "token1" being the new identifier in the translation files
(OLD: test if kde or gnome)
#!/usr/bin/env python import subprocess as data p1 = data.Popen(["ps", "-e"], stdout = data.PIPE) p2 = data.Popen(["grep", "kdesktop"], stdin = p1.stdout, stdout = data.PIPE) if p2.stdout.read() != : print 'kde' else: print 'gnome'
