Using GtkBuilder and connect_signals

I have been hunting spontaneous crashes with errors from the C GTK libs. The errors looked somewhat like these:

Warning: g_object_ref: assertion `G_IS_OBJECT (object)' failed
  gtk.main()
Warning: instance of invalid non-instantiatable type `AtkSelection'
  gtk.main()
Warning: g_signal_emit_valist: assertion `G_TYPE_CHECK_INSTANCE (instance)' failed
  gtk.main()
(...)
Warning: instance of invalid non-instantiatable type `'
  gtk.main()

In most versions of GTK window decorators, this caused a crash – in a few lucky cases, it didn’t! My (wrong) assumption was that calling connect_signals was only of interest, if I actually had signal handlers to connect. This is also the case when looking at the docs. For instance:

The function connect_signals and variants thereof can MUST be used to connect handlers to the named signals in the description.

(…)

For each of handlers that cannot be found, a RuntimeWarning is issued.

However, when the warning was issued, GTK would crash anyways. This behavior was most like caused by dereferencing and deleting GTK widgets, which caused GTK to look for an on_delete_event handler as some sort of default behavior. Since I have a program that adds and removes GTK widgets during runtime, my program needed to handle these signals, even if the event handler would simply be empty. To make a long story short, you should ALWAYS make sure to call connect_signals no matter what, as it will connect core event handlers that you may not be aware of. This is how it should look:

class YourObject():
    def __init__(self):
        glade = add_from_file("x.glade")
        win = glade.get_object("window")
        win.show_all()
        # ALWAYS DO THIS:
        glade.connect_signals(self)
 
    def on_delete_event(self, *args):
        pass

And even if the above may not fail, here is something that will potentially fail if you don’t connect the signals. Please beware, that using the same on_delete_event handler for two different objects is a bad strategy.

Always take care that widgets are removed and deleted, and that delete events may be invoked at very separate places in your application. You should also be aware that the python garbage collector could be the one initiating these events, causing those strange c library reference errors.

Posted in Python | Tagged , , , | Leave a comment

Headaches over disabling/enabling services in Ubuntu?

I’ve recently found myself trying to disable a service in Ubuntu 10.10 — problem was that every time I rebooted, it started anyways. The service I was trying to disable had changed from inetd to rc.d to Upstart during the past year or so, and I was looking all the wrong places.

The thing is, that you have to look for different ways that your service might be registered. Since Upstart has been only half-way integrated in 10.10, there are some different places you still have to look if the documentation isn’t clear about how the service is launched (and usually it isn’t). The following examples are for disabling services, as I think most services installed are automatically configured to run, and this is the task you’d be looking for…

update-rc.d

The old System V style of doing things is to call the update-rc.d command to disable any start-up links located in /etc/rcX.d/

$ sudo update-rc.d NAMEOFJOB disable 0123456

You should always invoke this command, as it explicitly disables any start-up links that may reappear when Apt is doing updates to your programs. So don’t just go and delete the start-up links!

/etc/init/yourjob.conf

Another place to look is the configuration folder of jobs. Because all of these .conf files are loaded and if they contain a description of when to be run, then that command will be executed. Look for something like this:

start on (filesystem
        and net-device-up IFACE!=lo)
stop on runlevel [!2345]

In order to disable the job, you simply have to comment out the start on part, like so:

#start on (filesystem
#        and net-device-up IFACE!=lo)
stop on runlevel [!2345]

inetd (OpenBSD style)

Another common convention is to rename the .conf file to .disabled as Upstart only loads .conf files.

Some services are started in OpenBSD style, ie. their descriptions are found in /etc/inetd.conf, the Internet superserver configuration database.

The reason inetd is still used is basically historical. Back in the days, it was desired to save memory by not loading programs until there was a need for them. One thing inetd is still used for is to load ancient TFTP servers when a UDP request is made on the TFTP port (see /etc/services for a list of port aliases in Linux). This description looks like so, and you simply have to place a ‘#’ in front of the line to disable it.

tftp           dgram   udp4    wait    nobody /usr/sbin/tcpd /usr/sbin/in.tftpd --tftpd-timeout 300 --retry-timeout 5 --mcast-port 1758 --mcast-addr 239.239.239.0-255 --mcast-ttl 1 --maxthread 100 --verbose=5 /srv/tftp

Other places…

  • /etc/X11/Xsession.d/ is the folder where scripts to be run when the X server is started are kept. I’ve never needed to change anything here, and I wonder why some things are kept in here and not explicitly run when switching to runlevel 5…
  • Gnome services can be disabled if you navigate to System->Preferences->Startup Applications. For instance, you can go here to disable the login sound (right?), Bluetooth manager (if you don’t use bt), some Evolution Alarm (since you don’t use Evolution, right?), Ubuntu One (don’t use neither) and Checking for new hardware drivers (since I guess you don’t constantly change hardware).
Posted in Ubuntu | Tagged , , , | 3 Comments

Changing the Django Admin site title

Often the Django Admin should look a little different for the sake of your users or for the sake of yourself (running multiple django sites with identical looks and titles can be such a pain). Often users don’t know what Django is, and it takes ages to explain, and even after that they have no clue. Also, often my administration has nothing to do with a website, so I don’t want the text “Site administration”.

First of all, you wanna add templates/admin/base_site.html to your project. This file can safely be overwritten, since it’s a file that the django devs have intended for the exact purpose of customizing your admin site a bit. Here’s an example of what to put in the file:

{% extends "admin/base.html" %}
{% load i18n %}
 
{% block title %}{{ title }} | {% trans 'Some Organisation' %}{% endblock %}
 
{% block branding %}
<style type="text/css">
  #header
  {
    /* your style here */
  }
</style>
<h1 id="site-name">{% trans 'Organisation Website' %}</h1>
{% endblock %}
 
{% block nav-global %}{% endblock %}

This is common practice. But I noticed after this that I was still left with an annoying “Site Administration” on the main admin index page. And this string was not inside any of the template, but rather set inside the admin view. Luckily it’s quite easy to change. Assuming your language is set to English, run the following commands from your project directory:

$ mkdir locale
$ ./manage.py makemessages -l en

Now open up the file locale/en/LC_MESSAGES/django.po and add two lines after the header information (the last two lines of this example)

"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2010-04-03 03:25+0200\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
 
msgid "Site administration"
msgstr "Main administration index"

After this, remember to run this and reload your project’s server:

$ ./manage.py compilemessages
Posted in Django | Tagged , , | 2 Comments

Uservoice feedback widget: Changing its style

I have a feedback tab that’s blocking vital content on a website. Here’s how to alter the style of the Uservoice Feedback Tab — for instance the vertical offset: You have to insert this AFTER your widget js code, so that it will effectively overwrite the CSS declarations generated by Uservoice. Also it’s pretty annoying that it shows up on printed media, so the last style block is to remove it from print.

<style type="text/css">
  body a#uservoice-feedback-tab,
  body a#uservoice-feedback-tab:link
  {
    margin-top: 10px !important;
  }
</style>
<style rel="stylesheet" type="text/css" media="print">
  body a#uservoice-feedback-tab,
  body a#uservoice-feedback-tab:link
  {
    display: none !important;
  }
</style>
Posted in Web | Tagged , , , , | 1 Comment