You are hereCoding
Coding
Coding tips and tricks, pitfalls, snippets, tutorials and more.
Linux: Automatically reconnect NetworkManager's VPN Plugin using D-Bus
In a previous post, I talked about how to use D-Bus to get information about network status changes. Now, since I have been using a private VPN with my Ubuntu as a client, I came across another scenario, where a script using D-Bus could help: The VPN works fine, but sometimes just disconnects. It does not happen very often, but when it happens, it is annoying and I'd like to see it reconnecting automatically. Moreover I'd like to stay in the VPN whenever I have a network connection available. Unfortunately, I didn't found any option within the NetworkManager to manage this. I ended up in writing this script which can be invoked like autovpn <VPN_NAME>, where <VPN_NAME> is the name of the VPN which is displayed in the NetworkManager under VPNConnections. You can even run it automatically on system startup by placing it in "Startup Applications". It does two things:
- It tries to connect the VPN whenever it detects an active internet connection.
- It tries to reconnect the VPN, whenever it disconnects unintentionally. The number of attempts is limited to 5 and there is artificial delay of 5 seconds between attempts.
Class Introspection in C++? Ask the physicists!
In my experience as a programmer, I have noticed that there always has been demand for a good way to achieve class introspection. Then the arguing starts: some languages already have class and object introspection, like Obj-C or Python, which, though becoming increasingly more popular, are still the minority of used languages. Then we have C++, which doesn't have class introspection by default, but most people use a lot. Is there a solution to this problem? Surprisingly, physicists have developed a great tool!
Parser Generator in Python - Quick PLY Introduction
I have been using regular expressions within my Python projects for a while now, but I've never felt the need to use a full-featured parser generator. This changed yesterday and I had a look at PLY, a pythonic implementation for Lex/Yacc. Being used to parser generation in Java using ANTLR or Cup I came across some remarkable differences which I want to point out for you after the stop. You will also see a small calculator code snippet.
Using D-Bus to communicate with Gnome Network Manager
Today, I tried to figure out how to execute scripts in Ubuntu on network connection changes. I came across the D-Bus system, which is used for inter-application communication in Linux. It is indeed very easy to access D-Bus within a Python program. If you read on, you will see the source code of a Python script, that - when launched - receives events from the Gnome Network Manager via the D-Bus interface.
Creativity - New LAN Game on Project Page!

In the last couple of days, my brother and me spent some time to code a little game in Python/PyQt. Though the code is quite a hack, you can still use the program to spend some funny nights with your friends. The program runs on all major platforms and is simple to use. The only thing you will need is a game file with questions and answers. Read more about everything on the application project page here. Feel free to leave your comments.
Python Coolness 2: Tips and Tricks
Welcome back Pythoneers! Last time we learned the underlying concepts of the Python programming language and runtime environment. In this second part, I would like to show you some of the Python tricks, that make this programming language so special. I will also briefly highlight some of the areas, which prove to be more difficult or dangerous than in other common languages, such as Java and C++. After this tutorial you will be able to write lines of code that will definitely impress your fellow geeks.
Python Coolness Series, Part 1
Hello fellow coders! Today, I present you the Python Coolness Series, which will (hopefully) show how you can use Python to make cool apps and tools, that would otherwise take days or weeks to create. We will make use of many cool high-level Python APIs, that wrap complex code, and allow you to focus on the aspects that interest you most. So, you won't be seeing any Hello-Worlds in this series, but actually some really cool tools that you can show off to your friends. Take note, that some of these tools will be Mac OS X only. I also recommend all Mac users, who would like to follow this series, to grab a copy of Fink, so that they can install Python modules painlessly.
Script: Highlight Output of Program
I was running a rather large configure script this morning, and keeping my eyes peeled for any lines on OpenGL (Leopard users will know why). Now, a Linux ubern00b would probably roll his eyes and tell me that I should simply pipe configure through grep, but I would also like to see the rest of the configure output, just in case there are any errors.
Enter my little highlight script. Simply pass it a grep regular expression, and any lines that match will be highlighted in yellow. Non-matching lines will be left as they are.
#!/bin/bash # Usage: higrep <regexp> if [[ "$#" != "1" ]]; then echo "Usage: higrep <regexp>" exit -1 fi while read LINE; do FOUND=$(echo "$LINE" | grep -c "$1") if [[ $FOUND -gt 0 ]]; then echo -e '\E[30;43m'${LINE }'\033[0m' else echo -e "$LINE" fi done
So here we first check the usage, and then pass each line to grep. If a match is found, we output the highlighted line. Otherwise we output the line as it was. Of course, this script could be extended to allow more colors, or it could be simplified to use only bash expression matching to avoid the call of another process. Anyway, save this to a script called higrep (or anything else geeky sounding), and do something like:
ps -A | higrep 'Applications'
Happy hi-grepping!