Saturday, June 25, 2016

The Nest of Heliopolis PPA repository on Launchpad gets updates after a while

Recently I managed to find the time to update a couple of new packages from my Personal Package Archive (PPA) on Launchpad. For those of you who don't know where to find it, click here. If you want to automatically get updates as they come out, you can install it on your Ubuntu by simply issuing
$ sudo apt-add-repository ppa:phoenix1987/ppa
$ sudo apt-get update
from your terminal. In brief, the changes involved Sopcast Player Plus, a fork of SopCast Player by Jason Scheunemann, and gtumbler, the application I have developed in the Summer 2012 as part of the first edition of the Ubuntu App Showdown. So let me now give you the details of what has changed with this new releases.

Table of Contents

Sopcast Player Plus

Sopcast Player Plus is my fork of SopCast Player, a GUI for the P2PTV broadcast service SopCast, that brings a native graphical interface to the Linux system. SopCast Player is originally developed by Jason Scheunemann, who hosts his version in his PPA (you can also find it the PPA of Roberto Ferramosca, the editor of the Italian Linux e-magazine Linux Freedom for Life, or LffL as it is also known as for short, together with the most recent release of SopCast for Linux).

My fork introduces new features to the app and, in particular, it extends the control the user has over the video output. With Sopcast Player Plus, which likewise SopCast Player uses vlc to reproduce the audio/video stream, you can adjust the geometry of the video by changing the aspect ratio, crop settings and the zoom level. This way you can, e.g., crop the black margins displayed by a broadcaster streaming a 16:9 movie in a 4:3 format, or select the most appropriate aspect ratio (similarly to what you can do with a TV set).

Another interesting feature is the integration with websites that offer event schedules. This is an expandable feature of the app that is based on python modules, which means that you can write your own module based on your favourite schedule website. Furthermore, if you need to redirect all the HTML traffic (unfortunately you cannot divert the SopCast traffic at the moment, unless you decide to do so with a system-wide configuration), you can configure an HTTP or a SOCKS proxy from the Proxy tab of the Preferences dialog, accessible from the Edit menu. Finally, as the original app SopCast Player, Sopcast Player Plus associates the sop:// URI schema with the itself, so that every time you click on a sop:// link, the web browser will ask you if you want to open it with Sopcast Player Plus.

How to install Sopcast Player Plus

If you have Ubuntu 15.04 (Vivid Vervet) and you have already added my PPA to your source list with the previous terminal command, installing Sopcast Player Plus is very easy, just type
$ sudo apt-get install sopcast-player-plus
For later versions of Ubuntu, I intend to introduce support later on, but for the time being, if your current version of Ubuntu is not supported yet, you can still install the application by downloading the vivid builds of sp-auth, the Linux release of SopCast, and sopcast-player-plus. To do so you can use the following commands from terminal
$ wget
$ wget
$ sudo dpkg -i sp-auth_3.2.6-0ubuntu1_amd64.deb sopcast-player-plus_1.0-0ubuntu2_amd64.deb
$ rm sp-auth_3.2.6-0ubuntu1_amd64.deb sopcast-player-plus_1.0-0ubuntu2_amd64.deb
for a 64-bit install of Ubuntu. If you are using a 32-bit install, then use the following commands instead
$ wget
$ wget
$ sudo dpkg -i sp-auth_3.2.6-0ubuntu1_i386.deb sopcast-player-plus_1.0-0ubuntu2_i386.deb
$ rm sp-auth_3.2.6-0ubuntu1_i386.deb sopcast-player-plus_1.0-0ubuntu2_i386.deb


With this application I have entered the first edition of the Ubuntu App Showdown, held back in 2012. Although it didn't win me any prizes, its birth has been welcomed by many people around the world (see, for instance, this post on the already cited LffL, or this article in French on La Vache Libre, or this other article in Italian from one of the contributors of The motivation behind it is a rather concrete one. At that time there wasn't an application for Linux with a decent and intuitive interface that allowed you to crop the margins of PDF files. Being a researcher in academia implies that you will have to read through many papers, and sometimes it is more convenient to have a printed version with you. Of course you can print a booklet in order to save ink/toner, paper and have something easier to handle (I personally find that A5 paper is easier to handle than the larger A4, but maybe this is just personal taste). However, if the pages in the pdf file have wide white margins, you would end up with something rather hard to read. To solve this problem you can crop the margins off, thus allowing your printer to fit the page content on the whole half of, say, an A4 sheet.

With gtumbler you can easily solve this problem as its main feature is to offer the user complete control over the various pdf boxes (i.e. the Art, Bleed, Crop, Margin and Trim boxes). Other interesting features are the possibility of concatenating many PDF documents together, as well as converting any PS file to PDF beforehand.

The version of gtumbler that I have provided for the Ubuntu App Showdown was coded using Quickly. At the times of Ubuntu 12.04 (Precise Pangolin), applications created with Quickly relied on DesktopCouch for storing and retrieving user preferences. As support for this technology became obsolete with subsequent releases of Ubuntu, gtumbler users could no longer start the application. I was happy to discover that someone else (see this question on AskUbuntu) has decided to solve the issue with a workaround (this has disabled the possibility for the user to use custom settings), repackage the app and distribute it through their PPA. As I have now managed to find some spare time, I decided to solve the problem, fix some bugs and release a new working version of the app. Users preferences can now be stored using GSettings, and a major bug affecting the concatenation of multiple PDF files has also been solved.

This will most likely bee the last stable release of this version (1.0) of gtumbler. A while ago I started working on version 2.0, which features a complete restyling of the user interface. The idea is to make the app look more like a PDF viewer, with the extra features extending it. Thus, the document preview, which currently feels like a marginal part of the app, will become central in future releases.

How to install gtumbler

Installation instructions for gtumbler are very similar to those provided above for Sopcast Player Plus. If you have Ubuntu 15.04 (Vivid Vervet) and you have already added my PPA to your source list with the previous terminal command, use the following command
$ sudo apt-get install gtumbler
Otherwise, while you wait for me to bring support for later versions of Ubuntu, you can download the DEB package and install it with
$ wget
$ sudo dpkg -i gtumbler_1.0-0ubuntu3_all.deb
$ rm gtumbler_1.0-0ubuntu3_all.deb


This is it from me for now. I hope that you will find these recent updates useful. Please do not hesitate to leave any comments, either good or bad, below :). To the next post!

Tuesday, January 21, 2014

How to convert a SIM into a MicroSIM

Until a few days ago, all my mobile phones were capable of hosting an ordinary SIM card in their belly. And since I'm talking about 5 years old technology residing in my pocket, it was quite time for a change and get some new hardware. As I didn't really follow the recent developments of telephony (5 years are indeed a long time span if you compare it with the way shorter characteristic time in Moore's law), I came to know that my future Android mobile will only be able to swallow MicroSIM cards.

So how can I put a SIM card into my new masterpiece of technology right away? After consulting Mr Google for a few advices, two possible solutions popped out: either your phone company will be happy to replace your SIM with an MicroSIM (or a NanoSIM. Yes, there are NanoSIM as well if you, like me, didn't even know about the existence of MicroSIM), or ... wait for it ... you can make your own MicroSIM out of your old SIM!

As I haven't looked at the websites of many mobile network operators, I'm not sure all of them will substitute your old SIM with a smaller one (it turns out mine does), so what I'll be discussing in this post is how to carve a MicroSIM out of your full size SIM (I won't deal with NanoSIM explicitly, but it is straightforward to adapt the following procedure to this case, just keep reading :)).

Before we proceed with our manufacturing process, a few words of warning (also a spoiler alert, for you're going to find out what we are about to do to your old SIM card, and you might not like it :P) and a disclaimer, for I won't take any responsibility whatsoever on damage you might cause to your SIM card while following these advices. The procedure I'm going to describe requires you to literally cut your MicroSIM out of the SIM you own, so basically there is no turning back afterwards. And I don't need to say to you that this might damage your SIM card beyond repair, voiding any warranty or whatever binding agreement you have with your company and, again, I don't take any responsibility for this. So keep reading only if you think you're brave enough to endure the sight of your SIM card being ripped off of its excessive plastic.

Here we go then. If you are already wondering "Hang on ... how am I supposed to know the exact shape and size of a MicroSIM?", there is a really simple answer to this question: download this PDF document (, you can also have a look at their guide at and print it with no scaling, i.e. 100% original size. This document comes with instructions on the right margin, and in particular point 4 below shows you that the actual chip is just a tiny portion of your SIM, just in the middle of it, so that it is quite a safe procedure to cut the excess plastic away from it. You are of course free to follow that guide, but what if you then want to put your new MicroSIM (or NanoSIM) back into a phone which hosts a full size SIM? Well obviously you can buy a MicroSIM to SIM adapter, that's for sure, but why should we waste the extra plastic from our old SIM? It seems that, if we're careful enough, we can manufacture our own adapter, right? Of course, if you own a 3D printer, or you somehow have access to one of those cuties, you can always print your own adapter. In my case this wasn't applicable, so I decided to do some yoga to get enough patience to carefully carve the MicroSIM out of my old SIM card. How did I do it? Simple! I've used a box cutter! Yes, this has indeed been my only precision tool. And of course, since I'm writing this post, the whole process must have been successful in my case.

So what's the strategy then? If your SIM is not extremely colourful, you'll be able to draw the cutting lines (for this, just follow the first steps in the PDF document above) with just a pencil. Make sure the tip is sharp enough to not mark the whole surface of your SIM, or it'll be hard to tell where you have to cut. Once you have completed this step (it might be really helpful to stick the SIM on the paper you've printed, as suggested in the document, with double-sided tape. In my case I didn't and I really struggled to keep the SIM card steady while incising it). What you want to do is to start incising with the cutter in the middle of all the lines you drew in the previous step, and keep going back and forth to mark those lines. Be patient and keep incising for as long as you reach the other side of the SIM. And be extremely careful not to overcut it, or the stability of your adapter might be compromised. You just want to leave the frame intact, with no incisions. Don't worry too much if the edges will be too irregular, for we're going to smoothen them up with a file (the one provided with a nail clipper is well suited for the job, and that's add another tool to our toolbox of precision tools :P). Once you've carved your MicroSIM out of your SIM, use the file for as long as the new MicroSIM will fit nicely into your device, then smoothen up the adapter as well. As a final touch I've put some sellotape on the bottom side (or top side, depending on your preferred point of view on things, but to be clear I'm referring to the side which carries the logo of your company, i.e. the one that once was opposite to the metal chip) so that when I'll put the MicroSIM into it it'll stay in position.
My adapter at its final stage. You can notice the pencil marks, and probably the sellotape at the bottom.
So we come to the conclusion. I hope this short post has been of help and that you haven't screwed up with your SIM. If you did well you'll have a perfectly working MicroSIM, exactly like mine, and a cute little adapter, all for free.

To the next adventure!

Tuesday, December 25, 2012

How to fix a double clicking Logitech MX1000 wireless laser mouse

I've got an almost 7 years old Logitech MX1000 wireless laser mouse. After a few years from the purchase I started experiencing a rather annoying issue: every now and then a single click was followed by a quick misfired second double click. So it became hard to accomplish simple tasks like moving or resizing a window with this mouse. Now I have decided that I had enough and I started looking for a solution. Googling around it turns out that this is a very common problem among Logitech mouse users, e.g. [1, 2]. As you can read from those posts, you have basically two roads: either your mouse is still covered by the warranty period and therefore you can ask for a replacement, or you fix it by yourself (of course you can always choose to throw your Logitech mouse away and go for a replacement, but I won't discuss this option here :P).

As you can easily deduce from above, my mouse is of course out of warranty, so in this post I'll tell you how I fixed my MX1000 mouse. All you really need is a screwdriver to "crack open" your mouse. Start by revealing the 4 screws from the bottom of your mouse: a pair is located under the sticky plastic oval-shaped supports near the charging pins; the other pair of screws one is under the label with the electrical characteristics of the device. Of course you have to pierce this label in order to access the screws, and this'd be the proof that you have tampered with your mouse. As a precaution I'd suggest you use your mouse until the battery is dried out and then switch it off before opening it.

Once you have managed to open your mouse, the first thing you might want to do is to remove as much dust and dirt as possible from the inside (especially in the area around the wheel). Then the last step, as suggested by the user jlohnes in [2] (yeah I know it's a different mouse, but the behaviour is the same, and as you have noticed you still have to open your mouse in order to remove the battery. But hopefully this won't be necessary, keep reading!), is to drain any residual static charge accumulated in your mouse, particularly in the area around the faulty button (the left one in my case). With a metallic screwdriver touch as many metal parts and exposed circuitry you can. Play a wee bit with the buttons and finally carefully close your mouse to its original state.

Now it's time to test the job: switch the mouse on, put it on charge and wait until fully charged. Then remove it from the charger and start using your mouse again. Hopefully you'll be as lucky as I've been and your mouse will be as good as new!

Hope this helped. Feel free to leave any comments or feedbacks below!



Saturday, July 21, 2012

gtumbler - A New Graphical PDF Manager for Ubuntu

I am delighted to announce the first stable release of gtumbler (formerly gistiller), a new graphical PDF manager completely written in Python. It is distributed under the terms of the GNU GPL v3 license (the pyPDF python modules shipped with the app are distributed under a modified version of the BSD license). If you want to give it a try you can get it directly from my PPA "The Nest of Heliopolis" hosted by Launchpad. If you are using Ubuntu 12.04 Precise Pangolin, you can add my PPA to your APT sources list by simply typing

sudo apt-add-repository ppa:phoenix1987/ppa
sudo apt-get update

Then you can install gtumbler with the command

sudo apt-get install gtumbler

or directly from the Software Center.

The major features of gtumbler are:

  • convert PostScript documents to PDF format;
  • merge multiple PostScript/PDF documents into a single one
  • extract pages from document;
  • modify the bounding box of each page in a document (e.g. to crop white margins)
  • pattern renaming of a list of documents You can also modify each document by invoking a PDF editor directly from gtumbler. By default gtumbler uses Xournal.

    Here is a screenshot of the application in action

  • Tuesday, November 29, 2011

    How to make your wireless card work with Network Manager after Debian 6.0.3 "squeeze" installation

    I have a pretty old HP Pavilion dv1000 laptop with a Intel Pentium M Processor 740 at 1.7 GHz. I have always used Ubuntu along with the pre-installed version of Windows on that machine, but as new releases became more resource-hungry, I started to experience an unacceptable slowdown. Of course you can use a lighter window manager, such as Xcfe, but since I'm too affectionate to GNOME 2 I decided to make a new start with Debian. I then downloaded the latest 6.0.3 stable release and burnt it on a CD-RW (the other option is to boot the installer from the net, since it seems that you can't boot anything from removable USB media).

    The installation process of my new OS went just fine, but in order to download updated packages from the internet I had to use my cable connection, since the Intel wifi adapter needed proprietary firmware that wasn't shipped with the first Debian CD. So I configured my ethernet card, and this turned out to be a source of problems, maybe the one you are experiencing now if you are reading this little guide. During the installation I had configured my ethernet to a static IP address, and everything went fine, Debian booted beautifully, fast and clean.

    The first thing I did then was to install the Intel wifi adapter firmware in order to make my wifi card work properly. I have then enabled the non-free part of the official Debian repositories in my sources.list file and executed the following command

    # apt-get update && apt-get install firmware-ipw2x00 wireless-tools

    Then I have restarted the networking service and Network Manager with

    # /etc/init.d/networking restart
    # /etc/init.d/network-manager restart

    At this point I configured my wireless connection with the graphical frontend to Network Manager (accessible through the icon in the notification area, usually near the clock on the top-right corner of the desktop) and...surprise! The association with the access point went just fine, but I couldn't ping my router nor any other machine on the local network. Weird, uh?

    It turned out that this issue was related to the previous network configuration, the one carried out during the OS installation process, since in that case the installer wasn't supposed to use Network Manager to manage the connection. The solution is then pretty simple, I just had to edit my /etc/network/interfaces to look like the following minimal example

    auto lo
    iface lo inet loopback

    and after rebooting my wifi adapter started working like a charm again.

    As a final remark I'd like to inform you that with Intel Wifi ipw2200 adapters you might need to use the firmware version 3.0 for the WEP security encryption. So the best thing to do is probably to move to a more secure encryption protocol, like WPA or WPA2.

    Sunday, June 26, 2011

    Google Books plugin for Referencer

    I'm going to break this rather long silence with a post on a plugin for Referencer.

    Referencer is a GNOME application especially designed for scientists, for it organizes any type of documents BiBTeX can handle, and ultimately allows you to export the whole collection into a single BiBTeX file (see this article @ Wikipedia and the project homepage for more details). Among the capabilities of Referencer there is the possibility of adding references from ID like the DOI code or the arXiv identifier. One feature that is still lacking is that of adding documents from web urls, although you can create an empty reference with only the field "url" filled. Since I'm currently working on my thesis for my Master's degree in Theoretical Physics, I need a plugin that instructs Referencer how to fetch books metadata from a web page containing the description of books. One of such sites is the fabled Google Books and in what follows I'm going to explain why I've chosen this particular site. If you search a book on Google Books, go on its details page (if you're in the preview page, just click on "About this book" on the left) and scroll down to the bottom of the page. There you'll find three buttons that will allow you export the bibliographic metadata in three different format. Fortunately one of such formats is BiBTeX, and Referencer is able to fill a document metadata from a bibtex file. I then wrote a plugin that allows to fetch this bibtex file from Google Books by pasting the address of the book into Referencer.

    How to install the plugin:
    1. download the plugin file from here;
    2. put into directory ~/.referencer/plugin (you might need to create this folder);
    3. run Referencer and enable gbooks if not already enabled.

    How to use this plugin:
    1. run Referencer and go to menu entry Documents->Add reference with ID...
    2. select Web URL from the dropdown menu
    3. paste the Google Books url in the text field and press OK

    Sunday, January 16, 2011

    pyGtkPlot: yet another graphical front-end for gnuplot

    The main window of pyGtkPlot.
    As the title of this post suggests, I'm going to introduce to you a new graphical interface for gnuplot. The name of the project, pyGtkPlot, surely synthesizes the programming language employed and the target window manager: python and gnome/gtk+ respectively. This project has a (not so) old C ancestor, the gtkplot project at (a C++ translation was on its way, but was never released to the public, and probably it'll never be). Since the development of the C and C++ version was becoming very slow due to the fact that the team of developers was composed of only one member (:P), that very same one member decided to turn his head towards python. This important decision took some time to become reality, because basically that implied the choice between performances and development speed, but now the pyGtkPlot has officially came to life, and it is hosted at There are no stable releases at the moment, but you may consider each new revision as a release candidate. This is because pyGtkPlot embeds the gnuplot x11 terminal inside its graphical interface (for this to work you must have the latest version of gnuplot installed on your linux machine), still retaining all the original interactiveness of gnuplot. Moreover you have the opportunity of sending commands to gnuplot directly, as though you were running it from a console. The only difference is that you have some additional aids at your disposal coming from the surrounding gtk+ widgets of the interface, which will increase in number at each new code revision pushed on Launchpad.

    Basically, you start with a new gnuplot session with default settings and add new plots by configuring the panel at the left and then clicking o the + (Add) button in the toolbar, or the Edit -> Add menu entry (keyb. shortcut: Alt + a). You can save the current session for later uses or load a previously saved session in order to modify it or export the plots in different formats, actually in all the terminal supported by your installation of gnuplot (of course you may not want to choose a terminal that does not write to a file, like wxt or x11 :P). You can embed mathatematical formulae too using LaTeX syntax and export in png format directly (check the menu File for this). But, as ever, the best way to learn how to use pyGtkPlot is by playing around with its commands.

    Monday, January 3, 2011

    How to implement a non-blocking two-way pipe in Python

    I was surprised by the lack of a way to read from a stream without blocking in Python, and this is the main reason why I'm writing this post. Some years ago I had the urge to open a two-way channel of communication between the program I was writing and an external command-line program (actually a GUI). I had solved this problem in mainly two different ways in C and C++ on Unix, by using pseudo-terminals in one case, and duplicated file descriptors in the other. Here we are going to create an object which inherits from a preexisting class which is provided by one of the modules for Python.
    But let us just start by looking at the reason why we can't be sure that a read operation on an open stream will not hang indefinitely in our Python code. Here's our scenario: we want to code a program that interacts with an external command-line driven application, by sending and receiving data from it. Pretty simple, uh? The module subprocess defines one class called Popen that basically created a two-way pipe between our parent process and a new child process, forked from the parent one and used to spawn the external application. This is just the standard fork/exec common practice and as it turns out it is exactly what we need to accomplish our task. But here comes the problem, as soon as we look at the methods of the class Popen: just the subprocess.Popen.communicate() method allows us to send data to the external application, read its response and then terminate it. But most of the time this is not what we want to do. We'd like to continuously send and receive data to and from the external application, so we'd like the connection to be kept open instead of being closed after the very first input ignition. Actually things are not this dramatic and we are indeed not to a dead end. Every instance of the class subprocess.Popen have attributes too, and among them we notice subprocess.Popen.stdin, .stdout and .stderr. As claimed by the Python documentation, these three attributes are actually standard Python file objects, and this means that we can use their read() and write() methods to respectively read from and write to the external application in the child process. We can have access to these file objects only if we have passed subprocess.PIPE to the corresponding arguments of the constructor of subprocess.Popen, but I won't give much more details about this here because we will find everything later in the example code below. Here we shall convince ourself that if the child process has no available data to be read in the stdout stream, then calling its read() method will cause our program to hang indefinitely, waiting for some data to show up at the reading end of the pipe. Here is the code to prove what we claimed. I'm assuming that you are running a Unix system and that you have the utility cat installed on your system. Now we open the python interpreter in a shell and we type in the following few lines of code
    Python 2.6.6 (r266:84292, Sep 15 2010, 15:52:39)
    [GCC 4.4.5] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import subprocess as subp
    >>> p = subp.Popen("cat",stdin=subp.PIPE,stdout=subp.PIPE)
    >>> p.stdin.write("Hello")
    ^CTraceback (most recent call last):
      File "<stdin>", line 1, in <module>
    We open a two-way pipe with the external application cat and we ask the constructor of subprocess.Popen to attach the stdin and stdout end to our parent process. We can thus write on and read from them, and a first step we write a string on the stream p.stdin. cat will just echo out our string and send it back to us through its stdout. In this case we are sure that there is data to be read from the stream p.stdout, as shown by the string returned by If we now try to read even only 1 more byte from the stream, the interpreter will hang indefinitely, waiting for something to read from p.stdout, and the interpreter freezes, refusing to accept additional code. All that we can do at this point is send the SIGINT signal with the key combo Ctrl+C. I suggest you to revisit this example, substituting the line with where n is a integer greater than 5.

    So far we became aware of the potential pitfalls that we might experience using the subprocess.Popen class. Before working out the solution for the aforementioned issue, I kindly suggest you to check this web page out. As you can read from it, what we have encountered with the previous example is something that is well known among the team maintaining Python. So the solution we are going to work out might be just a temporary remedy. All we need is one low-level system call, namely poll(), provided by the module select by means of the class select.poll. The poll Unix system call waits until an event occurs at some set of file descriptors for a certain amount of time. We can use this feature to check if the stream associated to the strout end of the pipe has data ready to be read before actually reading it. Only if data is available we the proceed by reading from the stream, otherwise we skip this step, avoiding the locking of our process. Here follows a very simple example of a class, Pipe that inherits from subprocess.Popen and extends it with a few new methods, the most interesting of whom are certainly read(), readlines() and write()
    import select
    import subprocess as subp
    class Pipe(subp.Popen):
        def __init__(self, exe, args = None, timeout = 0):
            self.timeout = timeout
            argv = [exe]
            if args != None:
                argv = argv + args
            subp.Popen.__init__(self, argv, stdin = subp.PIPE, stdout = subp.PIPE, stderr = subp.STDOUT)
        def close(self):
        def write(self, data):
            poll = select.poll()
            poll.register(self.stdin.fileno(), select.POLLOUT)
            fd = poll.poll(self.timeout)
            if len(fd):
                f = fd[0]
                if f[1] > 0:
        def read(self, n = 1):
            poll = select.poll()
            poll.register(self.stdout.fileno(), select.POLLIN | select.POLLPRI)
            fd = poll.poll(self.timeout)
            if len(fd):
                f = fd[0]
                if f[1] > 0:
        def readlines(self, n = 1):
            c =
            string = ""
            while c != None:
                string = string + str(c)
                c =
            return string
        def set_timeout(self, timeout):
            self.timeout = timeout
    We shall have a look at read() because the other two methods are then straightforward to understand. Here we single it out from the rest of the code
    def read(self, n = 1):
            poll = select.poll()
            poll.register(self.stdout.fileno(), select.POLLIN or select.POLLPRI)
            fd = poll.poll(self.timeout)
            if len(fd):
                f = fd[0]
                if f[1] > 0:
    The first line is just the construction of a new instance of the object select.poll(). Its method select.poll.register() registers a file descriptor with the specified flags, i.e. instructs the method select.poll.poll() (see below) to watch the stream associated with the file descriptor for the occurrence of the events indicated by the flags. Then the method select.poll.poll() starts watching the registered file descriptors for an amount of time specified by its argument. If all the file descriptors are ready, then the method returns, even if the whole timeout interval hasn't elapsed, and the return value is a list containing 2-tuples (fd, event) for each file descriptor that has earlier been registered. Otherwise the method stops the execution until the timeout is reached, and the returns a (possibly empty) list of all the file descriptors that have been found ready for I/O operations. This is the most general behaviour of the method select.poll.poll(). In the case that we are now analyzing we deal with only one file descriptor, namely the one associated with the stream self.stdout. As you can see from the code, the way to obtain the file descriptor associated to a file object is to call its method fileno(). With the first if we check if the method poll() has returned any file descriptor (it would be our file descriptor since we have registered only one of them). The second if checks that a valid event has occurred on the file descriptor, just to be even more sure that everything's going to be fine, and this being the case we finally perform our reading operation.
    The method, when called with no arguments, reads at most 1 byte. The reason for this default behavior must be clear to you at this point. With the very first example above in mind, we shall rest to think on the fact that the method poll() tells us whether there is data available on the stream, but not how much data is ready to be read. We can be sure that at least a byte is available for us, but by no means we can be sure that more than a byte is available on the stream. This implies that reading just a byte is the safest thing we can do. This gives a sense to the existence of the method pipe.Pipe.readlines(). It reads from the stdout end, byte-by-byte, until no more data is available on the stream. Then the method returns a string with all the bytes read.
    As a concluding remark we shall explain why the method pipe.Pipe.write() looks so similar to .read(). In most of the cases the stdin end of the pipe will always be ready for I/O operations, and we really don't need to care too much about this issue. But it can happen (as a fact of life) that the stream associated stdin is temporary unable to receive data from our program. This again would cause the parent process to hang until the data can be written successfully on the stream. A way to avoid this issue is again to make use of the poll() system call, and this explains why pipe.Pipe.write() and look so similar to each other.
    Before definitely parting from our journey into the realm of pipes, we shall write down a simple application that implements the class pipe.Pipe. Here is a very basic example, using again cat as scapegoat.
    #!/usr/bin/env python
    import pipe
    import sys
    if __name__ == "__main__":
        # Execute cat
        p = pipe.Pipe("cat", timeout = 100)
        # Try to read something. At this stage no data should be ready to be read
        print 'Reading %s' % p.readlines()
        # If the execution did not hang, the following line is executed
        p.write("Hello World!")
        # Now some data should be available
        print 'Reading %s' % p.readlines()

    Monday, December 27, 2010

    sopcast-player: a graphical frontend for sopcast

    SopCast is P2P way of broadcasting video and audio contents over the internet. The main line of development revolves around the Windows platform, but there is a unix command-line alternative. Jason Scheunemann has developed a graphical frontend for Gnome known as sopcast-player, today at version 0.4.1. I wanted to add some useful features to this great application, so I started editing the version 0.3.3. The result has been released as a branch of the main program at launchpad, where you can find more details about the differences and the source as well.

    Saturday, December 25, 2010

    emeseLaTeX: LaTeX plugin for emesene

    I'm a Linux Ubuntu user and I've been using amsn for years because of the TeXIM plugin that enable to send formulae to other contacts which are rendered with LaTeX. The latest version of amsn (0.98.4) still relies on tcl/tk for its interface, and amsn 2, which promises to integrate natively on the most popular window managers, is still a "work in progress" project at the moment. Nevertheless an very appreciable alternative exists: emesene. It integrates wonderfully in Gnome, and a v2 is underway as well. Looking for something similar to TeXIM for emesene, one day i discovered the emeseLaTeX plugin, developed by the author of the Dainaccio blog, and the latest version i think is that of the 28th of July, 2008. While using emeseLaTeX i become aware of some bugs and limitations and therefore I started to work on it in order to improve it. I managed to fix the "integer only" zoom scale factor bug in the settings window, and I've also added the possibility of choosing a custom preamble file. Moreover, longer and more complex LaTeX commands can be enclosed in the XML-ish <tex>...</tex> tags, in addition to the standard in-line math environment $...$ (replaced internally with $$...$$) which was already present in the original edition of the plugin.

    The new configuration window.
    I've decided to retain the original name for the plugin, i.e. emeseLaTeX. Here the credits follow:

    The original idea was from the author of the Dainaccio blog. As you can read from the original source code file, it
    # Uses a Dainaccio's modified version of
    #, created by Kamil Kisie
    # (that you can find at the end of this file)

    Have fun with LaTeX in your conversations!