This article is a short tutorial showing GIO’s file reading and writing functionality.
Recently I ported Exaile’s Files panel to use GIO instead of Python’s
os module. The reason is simple: remote browsing.
Yes, you can now browse an SMB share or an SFTP server through the Files panel. Just mount the remote filesystem with
gvfs-mount and enter the URL into the location bar. Previously Aren ported Exaile’s core to GIO as well, so remote files can be played just fine.
This is the kind of convenience you get out of using GIO/GVFS. In this article I’ll briefly explain what GIO is and how to do some basic file operations—similar to what the Files panel does—using GIO.
Playing with Unicode in Python 2 is not fun, and combining this with third-party libraries brings even more headaches. This post explains how Unicode in PyGTK is handled.
Note: This information is only valid for Python 2.x. It will likely change when PyGTK releases support for Python 3.
Calling GTK+ functions: PyGTK accepts
unicode objects as input.
str objects are assumed to be in UTF-8. If you pass a non-UTF-8
str to a GTK+ function, it will work until you try to show it, where you’ll get a “PangoWarning: Invalid UTF-8 string passed to pango_layout_set_text()”.
Handling GTK+ return values: PyGTK functions always return strings as
str objects. In most (all?) cases, the strings are encoded in UTF-8. Ideally, Python programs should use
unicode strings internally, so it’s wise to convert the output of PyGTK function calls to
label1.set_text("Some UTF-8 string") label1.set_text(u"Some Unicode string") x = label1.get_text() # x is an str object containing UTF-8 string. y = unicode(x, 'utf-8') # y is the unicode version of x. y = x.decode('utf-8') # Same as above.
I took a long hiatus from all my involvements in the free/open-source software world, including writing in this blog. Right now I’ve decided to go back, though I may not be as active as before.
Things happening during my break that I want to comment on:
- Development on Exaile 0.3 started.
- A lot of the database-related codebase was rewritten, but the DB has never been my main interest anyway.
- I don’t see Unicode support improving. At this stage I’ll just wait for Python 3 support.
- Portability bugs have crept in; I’ve fixed some of them
, but haven’t been able to run Exaile on my only Windows (Vista) box.
- The main Bazaar branch now contains several symbolic links. The problem is, Bazaar doesn’t support symlinks in Windows. This makes working on Exaile that much of a hassle, since I effectively can’t checkout the main branch from Windows. Yes, there are ways around this, but the truth is, I hate fighting with revision control systems just to be able to do my work. </rant>
- Python 3 released.
- In isolation, this news means little. Python 3 itself is not perfect—I’ve bumped into a couple of problems—so it’ll be a while before it gets mass adoption. However, this release means everyone should start thinking about upgrading their code to the new syntax and API.
- Rules of three: I don’t realistically expect all of Exaile’s dependencies to be ported to Python 3 anytime soon. There are essentially 3 camps we are waiting on: PyGTK, gst-python, and Mutagen. 3 months is my optimistic estimate of the time we need to wait before we can start porting Exaile. After that it’s probably another 3 months’ work for porting.
- The Australian government is pushing for Internet content filtering.
- The founder of iiNet (a big Aussie ISP) made a pretty strong comment against this.
… is a pain.
One of the things I like about Python is that it normally makes it harder to shoot yourself in the foot (monkey patching, anyone?). The only exception that is very frustrating for me is Python 2’s Unicode support, which is ugly and difficult to get right.
Really, at this point I don’t care much about other (planned) changes in Python 3. If Unicode support can be made as transparent as in Java or .NET, I would be really happy.
I’ve known about PyPy for some time, even tried it at one point, but I never knew it was more than just “Python in Python”.
For one, it has a translation layer that basically takes the implementation (which can also be for languages other than Python) written in RPython and turns it into code for various platforms (native, CLI, JVM, etc.).
It’s also home to some interesting experiments in Python; watch the new development blog to get a taste. For example, the post about the rope data structure is really cool if you’re into that sort of stuff.
In general, a project worth monitoring.