Python Goes Karamba
Total Page:16
File Type:pdf, Size:1020Kb
KNOW HOW SuperKaramba Brighten up your KDE background with SuperKaramba and Python Python Goes Karamba SuperKaramba helps you do something useful with your KDE background. Now, with just a little help from Python, you can define exactly what that extra functionality you need is going to be. BY HAGEN HÖPFNER nce you start using Super- Snake Charming /small_text_xmms.py Karamba [2] to a add clock and If we want to use Python to add more Oan MP3 player to your KDE functionality to themes such as the one Now open the theme file, small_text_ background [1], you will probably start discussed in [1], we do not need to mod- xmms.theme, by selecting Open… in the yearning for more. The good news is that ify the existing code. Instead, we can main SuperKaramba window, to trans- you can use the Python [3] programming simply add another file to the theme late the Python source code into Python language to add a wide range of interac- directory. The file will use the same byte code (see Box 1). (If you cannot see tive features to Karamba themes. For name as the theme itself, and is recog- the hidden .superkaramba directory, in example, you can create menus, dynami- nizable by its extension, .py. The the file selection dialog, press [F8].) As cally modify texts, and add drag & drop SuperKaramba developers recommend the compiler displays error and debug- support to your themes. starting off with a template (see [6]), and ging information on the standard output The underlying idea is as follows: modifying the template. You will find the channel, it makes sense to launch When a KDE event occurs (e.g. moving sample theme at [4]. SuperKaramba manually by typing the mouse, launching a program, or Unpack the latter by entering superkaramba (you may need to specify selecting a menu item), the program the path) in an X terminal (for example responsible for this event sends out a sig- tar -C ~/.superkaramba -xjvf U konsole). nal that is received and interpreted by a small_text_xmms.tar.bz2 For example, a message such as My function. Of course, SuperKaramba can Python extension has been loaded!, is interpret events of this kind. The in the ~/.superkaramba directory (you caused by the following print command SuperKaramba Python API, which is may need to create the directory first). described in detail at [5], allows you to This will create a theme directory called print "My Python extension hasU specify what small_text_xmms. Now copy the tem- been loaded!" happens. plate file, template.py, to the directory. Make sure you change the name to in the last line of the template. This reflect the name of your theme: allows me to ensure that the Python file translates correctly. cp template.py ~/.superkarambaU Sadly, this was not the case with /small_text_xmmsU my Suse 9 installation. It would seem that the distribution sets some of the environment variables used by Python incorrectly. Typing the following two commands in konsole before launch- ing SuperKaramba soon sorted out the problem: export PYTHONPATH=/usr/libU /python2.3/ export PYTHONHOME=/usr/libU /python2.3/ Drag & Drop Our first extension will be drag & drop for .mp3 files, as described in [1], that allows users to drag a file from Kon- queror to the integrated MP3 player. For 44 May 2004 www.linux-magazine.com SuperKaramba KNOW HOW the time being, we want XMMS to play the files. Table 1: SuperKaramba Signal Gages Function Called when Listing 1 shows our solution. If you do initWidget(widget) A SuperKaramba widget is created. not want to develop the template file, widgetUpdated(widget) The theme is updated.The update interval is defined in the .theme file. simply overwrite small_text_xmms.py widgetClicked(widget, x, y,button) A mouse click occurs within the theme. x and y indicate the coordinates with this file. (relative to the theme) where the click occurred. button indicates the mouse To allow a SuperKaramba theme to button that was pressed. support drag & drop, the theme needs to widgetMouseMoved(widget, x, y,button) A mouse movement occurs within the theme. x and y indicate the current be initialized. The best way to do this is coordinates (relative to the theme); button provides the number of the to create a SuperKaramba widget. This button and indicates the hold status. menuItemClicked(widget, menu, id) A menu item is selected.This passes on the menu handle (see the text) and transmits a signal that is picked up by the menu item that was clicked (id). one of SuperKaramba’s “gages”, the so- menuOptionChanged(widget, key,value) An item in the theme configuration menu is selected. key provides the menu called callback function initWidget(). item handle, and value the new value (true or false). We can enter meterClicked(widget, meter,button) A display device is clicked. meter provides the handle,button the number of the mouse button pressed. def initWidget(widget): commandOutput(widget, pid, output) A program is called by executeInteractive(),provided that this causes output to stdout.pid is the process ID of the program,output contains the text out- put. to define the reaction in the next line itemDropped(widget, dropText) Objects (e.g. icons) for drag & drop operations are dropped on the theme. when we use the theme as our desktop dropText contains the text for the object (e.g. its URL, see section “Drag and background: Drop”). startupAdded(widget, startup) KDE launches a new application.When the program launch operation has karamba.acceptDrops(widget) been completed, a startupRemoved() signal follows, and is turn followed by taskAdded(). In this case, we want the widget to startupRemoved(widget, startup) See startupAdded(). accept drop events. taskAdded(widget, task) See startupAdded(). SuperKaramba uses itemDropped() to taskRemoved(widget, task) A program is terminated. handle the signal. The function expects activeTaskChanged(widget,task) Another application is moved to the foreground. two pieces of information from the drop signal, as indicated in Table 1: a pointer string.split(string.rstrip(strU we did not do this, any MP3 files that to the widget and – in the case of files – a (dropText)),"\n") contained the file: string, would be multiple-line list of filenames in the hacked to pieces. The result is a list with dropText variable, where each line repre- We are using the newline character \n as two entries, which we store in another sents a file. a separator. The innermost function, variable, temp. The first element, Unfortunately, each filename is pre- str(), allows the content of dropText to temp[0], contains only an empty string ceded by the file: keyword. As XMMS be handled as a string, while after the first split operation. cannot handle full URLs of the string.rstrip() removes any non-standard For each iteration of the loop, we file:/path/to/file.mp3 type, but expects a characters from the right. append the filename to the existing con- filename like /path/to/file.mp3, we need Now we have the individual URLs, we tent of the xmms_filename variable. As to strip the superfluous data from drop- can use a for loop to remove the file: pre- the filename is the second element in the Text before passing it on to the media fixes. We will write the URLs to the temp list, we can access the name as player. filename variable one by one, and then temp[1]: call Stripping xmms_filename=xmms_filename + U We will need string manipulation func- string.split(U " \"" + string.rstrip(U tions [7] from import string. The next filename, "file:", 1) temp[1]) + "\"" step is to define an empty variable xmms_filename=”” where we can store to remove the file: prefix from the file- To allow XMMS to play MP3s with the stripped filename list. We can then name. To be more precise, we split the spaces in their filenames, we need to call string.split() to split the URL list content of filename into two parts at the enclose the filenames in quotes. As from dropText into its URL components: first (1) instance of the file: separator. If Python also uses quotes, ”, to separate GLOSSARY API: An Application Programming Interface Stdout: The standard output channel speci- also points to the screen, and a stdin channel defines a number of functions within a soft- fies where Linux applications should send for input, which typically points to the key- ware that other programs written in a specific their text output.This is typically the screen, board. language can call. In our case, Python applica- but you can redirect standard output to a file Widget: A generic term for the control ele- tions can use the functions implemented in or a printer. In addition to stdout, Linux also ments of a graphical interface.These include SuperKaramba. has a stderror channel for error output that windows, buttons, menus, checklists, and tabs. www.linux-magazine.com May 2004 45 KNOW HOW SuperKaramba tem is now stored in the global variable Listing 1: The theme supports drag & drop selectmenu. This is referred to as a han- 01 import karamba dle, as the number is used to identify the 02 import string menu internally. The following, some- 03 what lengthy construct stores a menu 04 def initWidget(widget): with two items in my_menu. The first of 05 karamba.acceptDrops(widget) these … 06 07 def itemDropped(widget, dropText): karamba.addMenuItem(widget, U 08 xmms_filename="" selectmenu, "konqueror", U 09 for filename in string.split(string.rstrip(str(dropText)),"\n"): "konqueror.png") 10 temp=string.split(filename, "file:", 1) 11 xmms_filename=xmms_filename + " \"" + string.rstrip(temp[1]) … creates an entry for konqueror in + "\"" selectmenu. The konqueror.png icon, 12 karamba.execute("xmms" + xmms_filename) which also has to be stored in the theme 13 directory, completes the menu item.