Monday, September 1, 2008

Internship @ Pardus

I've entered my TOEFL exam and it was pretty good, except for the listening part in which I lost my attention and thought about the examination software (it was written in Java but the Cambridge, Longman etc. preparation CDs are prepared in Shockwave as I saw). I asked myself: "Why don't they write them in Java too so that I can use them on Linux? And guess what, at that moment, a lecture was being given on the computer and I lost lots of details :D

Nevermind, I started my third internship last week, in TÜBİTAK (National Scientific Research Center) on Pardus Linux Distribution Project. It's been very beneficial for me so far. I've learned lots of things about software, python, GUI design and opensource. I want to share what I've learned in this blog as much as I can do. You can see examples here. I've written a program called Sahip, which is an XML generator. It produces the installation settings for Yali (installation software of Pardus) to perform silent installs.

PyQt4 GUI Building
Let's start with PyQt4 GUI building. I used to think that we could not import GUI files from python modules and this caused us to refactor our python code whenever we wanted to change the gui, unlike GTK with Glade (you can import XML from python). But I was wrong. There was some options I could follow. One of them is the kdedesigner module for python. But I used the other one, inheriting the compiled GUI.

After you design a GUI on Qt Designer 4, you can compile it to Python code with:

pyuc4 gui.ui -o -x

and after that we can import and inherit it as written below:

from PyQt4 import QtCore, QtGui
from sahip.usergui import Ui_UserDialog

class UserDialog(QtGui.QDialog):
def __init__(self, caller=None, user=None):
QtGui.QDialog.__init__(self, None)
self.ui = Ui_UserDialog()

# All the other stuff here or in other methods.

if __name__ == "__main__":
import sys
app = QtGui.QApplication(sys.argv)
UserDia = UserDialog(None)

That's all!

Opening up a new dialog
I imported UserDialog class above and used it as a dialog.

def slotUserNew(self):
self.userDialog = QtGui.QDialog(self)
self.userDialog.ui = UserDialog(self) # caller=self

Lists items with checkboxes

You can set all the items of a list have a checkbox near it with the following code:

for i in range(count):
item = self.ui.groupList.item(i)
item.setFlags(QtCore.Qt.ItemIsUserCheckable | QtCore.Qt.ItemIsEnabled)

In lists, I could only store the username of a user but the rest of the information should be stored somewhere else. That could be a dictionary, list etc. Dictionary was the most convenient for me but I had to define a dictionary for each list/combobox. That was a replication of code! So I wrote WidgetHandler, and specialized ListHandler, ComboBoxHandler to store them and defined addItem/removeItem, etc. methods to update both the dictionary and the widget. It was much more clear.


I have always wondered about internationalization and I saw that it was pretty easy. What you should do is to add the following code to the beginning of each of your files you want to be translated.

import gettext
__trans = gettext.translation('sahip', fallback=True)
_ = __trans.ugettext

and you will use _('string') instead of 'string' if you want that string to be translated. And here comes the pot file generation part. As you can see the example project on the link above, I have a tools, po and sahip directory. So I recommend you to create such directories. I put pot generator sh file into tools dir and po/pot files into po dir. After creating an empty po dir, you can execute the following script (above po dir)

LANGUAGES=`ls po/*.po`
set -x

xgettext -L "python" -k__tr -k_ sahip/sahip sahip/*.py -o po/sahip.pot
for lang in $LANGUAGES
msgmerge -U $lang po/sahip.pot

As you might understand, this script crawls through the given paths (sahip/sahip and sahip/*.py) and generates pot file by reading the py files and main (non-extensioned) sahip file. You will then find the pot file in your po directory. You can translate and rename it to lang.po format (tr.po, de.po, es.po). Then the python setup script will probably compile po file into mo file and copy it into proper place.

Setting Icon
  1. Create a directory called images in the same directory of your codes.
  2. Put an image file in it, such as icon.png
  3. Create a qrc file such as resources.qrc and fill it with the content


  4. Open your GUI with Qt4 Designer and click on ... button of windowIcon on the Property Editor when main form is selected.
  5. A dialog will be shown, click on pen and then the open button (middle). Select the qrc file you created.
  6. Select the icon appeared on the right side of the dialog and click OK.
  7. Save the GUI.
  8. On the console, apply the following command:

    pyrcc4 resources.qrc -o

  9. Generate your gui py file with pyuic4 and that's all!
Publishing the Code
I needed to copy YALI file and modify it for sahip. This file compiles po and qrc files, and then copy required files to the system for installation. Then I wrote a script which copies the program directory to desktop and removes the unnecessary ones. Then the file compresses the directory and sha1sums it, updates a sample pisi pspec.xml file with the sha1sum, uploads the compressed targz file to ftp server and then build the pisi package, and install it.

It was a good way of automation for me. I could make modifications and try the result in seconds. I might have written this entry too confusing, sorry but I don't have much time. My next project is to develop a web site where files can be searched within the pisi packages so that for instance you can find which package the 'ls' file comes from. I'm currently writing the database generator and after I'll pass to the Django side.