Useful information for people (aka me) teaching themselves python by writing small python aps.

21 02 2008

I have been writing a one or two small python scripts to do some useful stuff the last couple of days and since I don’t much about python learning a few things along the way. Here is some of the small things I had to lookup. The free book “Dive Into Python” by Mark Pilgrim was very useful you can read it here or see if it is included in your distro on Ubuntu the .hlm version was installed here.

Date and Time

You need to import: import datetime

To get the date and time and put it in variable date_time:

date_time = datetime.datetime.now()

Output with print date_time: “Thu Feb 21 11:36:31 2008″

To get the difference between to dates:

date1 = datetime.datetime.now()

later_date = datetime.datetime.now()

dif_date = later_date - date1

dif_date.seconds

dif_date.days

dif_date.microseconds

Output with print dif_date.microseconds:

String and Int

To declare a string:
mystring = "Hi"
To declare a int:
myint = 1

To output a string and int on same line using print command:

print "Going to print string and Int, String: "+mystring+" and Int: "+str(myint)

Lists

Declare empty list:
mylist = []

Declare list with some values (items):
mylist = ["val1","val2",4,8]

Print all items in list:
for val in mylist:
print val

Add item to list:
mylist.append("new_value")

Remove item from list:
mylist.remove("new_value")

Remove duplicate items from list:
for item in mylist:
ocurence = mylist.count(item) #number of times item occurs
for x in range(ocurence-1) : #remove all occurences exept one (-1)
mylist.remove(file)

Files and Directories
Might need to import: import shutil for copying
Get file extension:
ext = os.path.splitext("/home/fredre/test.txt")[1]
If directory does not exist create directory:
if not os.path.isdir("/home/fredre/test"):
os.mkdir("/home/fredre/test")

Copy file to another directory:
shutil.copy("/home/fredre/test.txt","/home/fredre/test")
Print all files in directory recursively:
for dirs in walk:
if(dirs[2]):
for file in dirs[2]:
working_file = os.path.join(dirs[0],file)
print working_file

Other stuff
Execute command on host operating system and display output from that command
You need to import: import commands
for out in commands.getstatusoutput("ls -l /home/fredre"):
print out





Put Rhythmbox om the D-Bus

13 02 2008

You probably already know what Rhythmbox is right ? Well for the less informed Rhythmbox is “Music management and playback software for GNOME” in other words a program that can play your mp3 and ogg’s among others. D-Bus is a system for interprocess communication (IPC). Using D-Bus you can control Rhythmbox from other programs or the terminal.

Like I said before D-Bus is used for IPC and was designed to allow different desktop applications to communicate with each other. D-Bus also allows the operating system to communicate with some of the different desktop applications running on the computer. In our case we want to use D-Bus to control Rhythmbox from the shell.

In order to call a method using D-Bus we need the service, name of remote object, interface and the method. The image below will make these four things a bit more clear.

Dbus

In our case the service is: org.gnome.Rhythmbox the remote object: /org/gnome/Rhythmbox/Player (can be more then one) the interface: org.gnome.Rhythmbox.Player and the method getPlayingUri() (can be more then one). In other words there will be one service for the program we want to communicate with, this service will expose a number of remote objects each remote object will expose an interface that will provide a number of methods. Using these four things along with dbus-send will allow us to control Rhythmbox from from the terminal.

To call methods that Rhythmbox exposes to D-Bus we will use the dbus-send command in the shell. For more information on dbus-send you can have a look at the man page but the basic command to get the full path of the current song playing in Rhythmbox will be: dbus-send --print-reply --dest=org.gnome.Rhythmbox /org/gnome/Rhythmbox/Player org.gnome.Rhythmbox.Player.getPlayingUri

Lets break the command down and have a look at the different parts. The first part dbus-send --print-reply calls dbus-send and tels the program to display any output that might result in running this command. The next part specifies the service we want to use, the full name of the service is specified with: --dest=org.gnome.Rhythmbox . Next we provide the remote object: /org/gnome/Rhythmbox/Player and finally the interface with the method at the end: org.gnome.Rhythmbox.Player.getPlayingUri . If all went well after executing the command you should get the full path of the current song playing. In my case the output was:

method return sender=:1.33 -> dest=:1.34 reply_serial=2
string “file:///home/fredre/Music/Mp3’s/mp3’s/Led%20Zeppelin%20-%20Stairwa %202%20Heaven.mp3″

This is where most tutorials end on the subject of Rhythmbox and D-Bus, most of them explain how to get the current playing URI (strange). But that is all we are interested in right ? No not really, a few questions remain like how do I get all the remote objects of a service and all the methods and signals that that service provides in this case org.gnome.Rhythmbox. To get this information there are two options available, one is to go look at the .XML files shipped with the source code of the application and the other more convenient option is to use the Introspect method along with dbus-send.

The org.freedesktop.DBus.Introspectable interface provides a method named Introspect() that will return an XML document describing the objects that a service provides and the methods of those objects. Just think about it as a method provided by each service and object of that service (in this case org.gnome.Rhythmbox) to find out more about the service.

To find out more about org.gnome.Rhythmbox lets first find out what remote objects is available. One can accomplish this with the following command: dbus-send --print-reply --dest=org.gnome.Rhythmbox /org/gnome/Rhythmbox org.freedesktop.DBus.Introspectable.Introspect . The command starts of the same as before with dbus-send -print-reply to allow us to see the output on the screen. Next the service we want to use: dest=org.gnome.Rhythmbox". The the remote object follows you will notice that this time the remote object is the same as the service: /org/gnome/Rhythmbox this is because we want to see all the remote objects offered by the service. Finally the Introspectable interface is called with the Introspect method at the end: org.freedesktop.DBus.Introspectable.Introspect. The resulting output is an XML document showing all the remote objects:

method return sender=:1.29 -> dest=:1.34 reply_serial=2

string “<!DOCTYPE node PUBLIC “-//freedesktop//DTD D-BUS Object Introspection 1.0//EN”
“http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd”>
<node>

<node name=”Player”/>

<node name=”PlaylistManager”/>

<node name=”Shell”/>

<node name=”Visualizer”/>

</node>

In the document each remote object is shown with: <node name="Player"/>. Nice so know we know that except the Player object we also have the PlaylistManager, Shell and Visualizer objects to play with.

To get the specific methods of each object we can just append the object name in our dbus-send command to the remote object part. So the command to get all methods and signals from the Player object we will use the following command to get our XML file: dbus-send --print-reply --dest=org.gnome.Rhythmbox /org/gnome/Rhythmbox/Player org.freedesktop.DBus.Introspectable.Introspect the only difference now is the Player object append to the remote object: /org/gnome/Rhythmbox/Player the resulting output should look like this:

” method return sender=:1.29 -> dest=:1.38 reply_serial=2

string “<!DOCTYPE node PUBLIC “-//freedesktop//DTD D-BUS Object Introspection 1.0//EN”

“http://www.freedesktop.org/standards/dbus/1.0/introspect.dtd”>
<node>

<interface name=”org.freedesktop.DBus.Introspectable”>

</interface>


</interface>

<interface name=”org.gnome.Rhythmbox.Player”>

<method name=”getMute”>

<arg name=”mute” type=”b” direction=”out”/>

</method>

<method name=”setMute”>

<arg name=”mute” type=”b” direction=”in”/>

</method>

<method name=”getPlayingUri”>
<arg name=”uri” type=”s” direction=”out”/>
</method>

<signal name=”playingSongPropertyChanged”>
<arg type=”s”/>

<arg type=”s”/>

<arg type=”v”/>

<arg type=”v”/>

</signal>

</interface>

</node>

I have abbreviated the output slightly because we are interested in the part that starts with: <interface name="org.gnome.Rhythmbox.Player"> this tells us that we are viewing all the methods and signals available for the Player interface.

There are two nodes in the <interface> node they are the <signal> node and the <method> node the former being the one we are interested in. As you can see a method has a arg name, a type and a direction. The direction can be in or out. Mostly when you want to get some kind of information you will use the out direction methods, for example the getMute method that return a true of false based on the mute setting. The type will indicate what the method returns, or needs as an argument in case the direction is in. In the case with getMute a boolean value will be returned. For more information on using methods with an in direction requiring arguments check the dbus-send man page.

You will probably only use dbus-send to quickly test methods or use the command in a shell script. A number of languages has D-Bus bindings making it not to difficult to use D-Bus in regular applications enjoy.

References:

http://dbus.freedesktop.org/doc/dbus-tutorial.html

http://unmaintainable.wordpress.com/2006/12/10/controlling-rhythmbox-using-dbus/

http://www.kuliniewicz.org/blog/archives/2007/12/12/cop-versus-bus-d-edition/

http://unmaintainable.wordpress.com/2006/12/19/using-dbus-introspection/





NetBeans Kaffeine plug in (module)

7 02 2008

I created a netbeans module that allows the Kaffeine media player to be controlled from inside netbeans when I was still doing a lot of java development in netbeans. At the moment I am mostly playing with mono, C# and monodevelop and have switched to rhythmbox as my main music player. Maybe my module can be useful to someone els.


NetBeans Kaffeine plug in nbm file
NetBeans Kaffeine plug in source code

Browse code and other files in subversion repository





Welcome to Defcon3

4 02 2008

Please have a look around… I am working to get everything ready.

The image below is provided by CrunchBang:


” I’ve now created a set of Ubuntu advocacy images designed to be displayed randomly.

The images are based on a set of Ubuntu banners that I created last year. The images are exactly the same size, dimensionally, as the Christmas image, and therefore replacement should be a simple operation.”

Thanks Philip….

Get your own here: http://crunchbang.org/archives/2008/01/25/random-ubuntu-advocacy/