Hurricane Electric IPv6, m0n0wall, and Dynamic IPs

After years of wanting it, I finally got around to setting up an IPv6 tunnel. (My brothers’ Flash plugins weren’t too pleased for some reason, but I’ll re-enable IPv6 on their machines later)

I quickly discovered that there was one small problem though. Hurricane Electric’s TunnelBroker.net doesn’t offer a DynDNS-style API for DNS-O-Matic to replicate my m0n0wall DynDNS updates to.

They do provide a more homegrown HTTP API and I tried e-mailing DNS-O-Matic to see if the could add support for it, but got no response and, since m0n0wall doesn’t let you hook on-reconnect events, I needed something ddclient-like.

I checked around to see if anyone else had m0n0wall-based solutions, but found nothing and, being the geek I am, I felt more like whipping up a custom tool in an afternoon than spending an hour trying to puzzle out the search keywords for a more flexible ddclient-alike.

upd_ipv6.py requires Python 2.5 (I think) and LXML (m0n0wall’s interfaces status page isn’t well-formed enough for ElementTree and I didn’t feel like doing any SAX-style parsing by hand). It’ll probably work on Windows and MacOS, but I’ve only tested it on Linux and it’s not a daemon, but that’s what cron and the Windows task scheduler are for.

To configure it, run ./upd_ipv6.py --dump-config and then edit the config file at the path it mentions. You’ll probably want to create a custom m0n0wall user that can only access the Status > Interfaces page.

Once that’s done, just stick it somewhere out-of-the-way, add a cron line like this to run it once every five minutes:

*/5     *       *       *       *       ~/bin/upd_ipv6.py

It’ll only contact TunnelBroker.net if your IP changes.

Posted in Geek Stuff | Leave a comment

A Python programmer’s first impression of CoffeeScript

CoffeeScript is a simple, clean, fast language which compiles to JavaScript, either at build time, with a caching framework plugin on the server, or at runtime in the browser.

The syntax looks like a cross between Python and Haskell1 and, generally speaking, is used similarly enough to Python that you’ll have little trouble picking it up and using it comfortably.

It does have a few of what non-Ruby programmers will consider distasteful warts, but for the most part, it’s an elegant and delightful language.

You’ll probably want to keep the syntax reference open in a background tab at first, but if you’re comfortable with Python and you’ve done any programming with jQuery or some other modern JavaScript library, you’ll feel right at home.

Below, I’ve summarized the points which the documentation doesn’t completely prepare you for as a Python programmer.

Helpful features with no direct Python analogue

A.K.A. Things which you’ll probably forget to use at first, but which you really should work to remember.

Use almost anything as an expression
Not only does CoffeeScript have anonymous functions, you can treat pretty much anything as an expression and it’ll be wrapped in an anonymous function if needed.
Indent-stripping multi-line strings
If you use single-quotes rather than triple-quotes for multi-line strings, it’ll still work and leading indentation common to all lines will be stripped. (like textwrap.dedent(), if you’re familiar with it)
Embedding expressions in strings
Given how much string templating is done in JavaScript, it really helps that you can put any expression directly inside the Ruby-style #{string interpolation operator}
for own key of
To iterate over only the properties which weren’t inherited, use for own key of rather than for key of. (Very useful since JavaScript doesn’t make a distinction between properties and associative array keys)
Fat-arrow function syntax
To bind this to the parent object, even in a callback, simply define it with => rather than ->
Ultra-concise constructors
Rather than filling your class constructors with this.foo = foo, you can simply use the @ shorthand for this. and write constructor: (@foo, @bar) -> with an empty method body.
Checking for the existence of variables and properties is concise.
Use variable ? "default value" and obj.method?().thing?.widget

Similarities and differences with Python not explicitly mentioned

A.K.A. What the documentation won’t directly prepare you for if you have Python instincts.

Ternary syntax will break your muscle memory
For a ternary expression, Python uses result = a if test else b while CoffeeScript  uses result = if test then a else b. (The if statement syntax on a single line)
a = b and c or d works Pythonically but you don’t need backwards-compatibility with pre-ternary Python releases
Boolean operators don’t coerce the return value to a boolean (a will be c or d, not true or false) but 99% of the time, what you really want is the ternary operator.
Comprehensions use when, not if
If you write result = (x for x in list if x % 2 == 0), you won’t get an error… but you will get a loop inside a conditional rather than a conditional inside a loop.
(If item in the parent scope isn’t evenly divisible by 2, the loop will be skipped. If it is, result will be identical to list)
Comprehensions iterating two lists produce a two-dimensional array
In Python, this syntax will produce a one-dimensional list while, in CoffeeScript, it produces a list of lists.

a+b for a in A for b in B
newList = list1 + list2 + list3 is not array concatenation
You could use newList = [].concat list1, list2, list3 instead, but a cleaner and more flexible alternative is to use splats. This example cleanly concatenates three lists and two individual numbers into a single list:

newList = [listA..., numA, numB, listB..., listC...]

In my opinion, this makes CoffeeScript cleaner and more intuitive than Python for this task.

When foo is an array, if foo is always true
[] != false so, to check whether a list is empty, you have to do if foo.length instead.
someFunction(arg3 = 1) is not the syntax for positional arguments
…but because assignment is valid in an expression, it won’t raise an error. What you want is someFunction null, null, 1 or someFunction(null, null, 1)
There is an equivalent to someFunction(x, y, *args)
As in Python, splats aren’t just for function definitions.

 someFunction x, y, args...

Counter-intuitive Perl/PHP/Ruby/etc.-isms

A.K.A. Where most of your subtle, hard-to-find bugs will come from as a Python programmer.

If you don’t use parentheses in a function call, CoffeeScript will guess them for you
…but Haskell programmers and shell scripters will be surprised when a b c d means a(b(c(d))) rather than a(b,c,d). This also means that foo () is sometimes invalid when foo() is OK.
On the plus side, it works very well for ensuring that anonymously defined callbacks aren’t an exception to the “indents, not braces” block syntax.
The rules are simple but it’ll still take some getting used to before I’ll stop occasionally tripping over them. Here’s how jashkenas explained it to me:

The call wraps forwards to the end of the line, or to the end of the indented block, with one specific exception for postfix conditionals.

alert a alert(a) alert inspect a alert(inspect(a)) alert inspect a if b alert(inspect(a)) if b
Colons aren’t part of the block syntax
Habitually typing a colon after a function name or control statement will turn it into an object property (think dict literals)… which can do all manner of crazy things if it’s something like “else:” which is still valid CoffeeScript.
You can’t shadow a higher-level variable
…only refer to it as in Ruby’s “local scope”, so expect to cause subtle bugs if you habitually reuse a handful of temporary variable names. If you want to get out of the habit, PyLint‘s default configuration will complain when you do this in your Python code.
Like Perl and Ruby and unlike JavaScript, CoffeeScript does implicit returns
…so expect subtle bugs until you internalize that CoffeeScript’s function syntax is really a multi-line lambda.
(As the docs say when talking about comprehensions intended only for their side-effects, “Be careful that you’re not accidentally returning the results of the comprehension in these cases, by adding a meaningful return value, like true, or null, to the bottom of your function.”)
As with PHP, single- and double-quoted strings have different meanings
…with interpolation only working in double-quoted strings.
Like in C and many C-inspired syntaxes, and explicitly unlike Python, you can do assignment inside an expression.
So expect to shoot yourself in the foot on occasion by accidentally typing = instead of == and not getting an error message. (You can, however, reduce the risks by habituating yourself to the is and isnt aliases for == and !=)

Caveats to re-mention for JavaScript programmers

A.K.A. Where subtle, hard-to-find bugs may come from if you have a lot of experience working with plain old JavaScript.

Switch statements don’t allow you to fall-through to the next case
CoffeeScript automatically inserts break; into every case. However, when statements will accept comma-separated lists instead. (I seem to have misplaced the response which gave me this tip. Help appreciated in tracking it down so I can link it.)
To avoid namespace pollution, everything is run in an anonymous wrapper
…so you have to explicitly attach things to the window object if that’s what you want.
== and != are converted into === and !==
If you really want the intransitive, coercion-inducing version, wrap the expression in backticks to mark it as raw, untranslated JavaScript.
CoffeeScript’s in is Python’s in
JavaScript’s in is CoffeeScript’s of.

Python features still to find equivalents for

Did I miss something?

I’m new to CoffeeScript, so please let me know if I’m wrong or if I forgot to mention some area where Python instincts will steer you wrong with CoffeeScript.

Also, don’t forget to read the FAQ. It’s not as noticeable as the docs, but it’s almost as useful. 🙂

1. Technically, the syntax is inspired by Ruby and YAML, but I know quite a few Python programmers for whom that wouldn’t mean anything, so I compare to Python and Haskell first instead.

Posted in Geek Stuff | 45 Comments

GQView Collections on the Framebuffer

So there I was, waiting for some nVidia drivers to compile so I could get back into X11, flipping through images in fbi, when I realized I was sick and tired of fragile, irritating, one-liners to pipe lists of images into it.

Enter gqfbi. A little, one-hour creation of mine which makes using fbi so much more comfortable.

GQfbi is a small, Python-based wrapper script for the fbi framebuffer image viewer which will make it just Do What I Mean™.

Features include:

  • Automatically excluding files fbi won’t recognize to prevent it from pausing for a second or two to force you to read the “couldn’t load this image” message.
  • Support for taking any combination of paths to images, directories, and GQview/Geeqie collections.
  • Calls fbi with readahead and auto-scaling enabled. (I’ll make them optional when I get around to actually making optparse do something useful)

Planned features:

  • Support for asking fbi to slideshow, shuffle, and loop.
  • Support for taking Zip/RAR/CBZ/CBR archives as input for framebuffer manga-reading.
  • Its own git repository rather than lurking in my roaming profile’s ~/bin

If you like it, please let me know. I don’t use it often, so you’ll probably be single-handedly responsible for getting a feature implemented.

Posted in Geek Stuff | Leave a comment

The Real Meaning Behind The XDG basedir config/data split

For the longest time, I wasn’t entirely sure why the XDG base directory specification split non-cache data into config and data. I knew there must be a difference which made it a useful thing to do, but I just couldn’t figure out why.

Then, as it tends to happen, while on a completely different topic, I ran across an entry in the Aquaria development mailing list which explained it simply and clearly.

Config files and data files are separated because config files are, in some ways, like cache files. If you lose them, it’ll be annoying, but they’re not too difficult to replace. On the other hand, I know I’d be pissed as hell if I lost a saved game that took me hours or days to build up.

It’s not as clear a distinction as something like “roaming vs. non-roaming” but given that config files are also more likely to be system-specific, that can play a part in it too.

Update: In my opinion, this blog post explains it even more effectively.

Posted in Web Wandering & Opinion | Leave a comment

Casual Sandboxing for Wine

For anyone who, like me, uses a variety of applications on Wine, it soon becomes obvious that Wine seems to trust Windows applications a little too much. Little or no support for automatically removing .desktop files created by a Windows installer, full access to the system via Z: without the application even having to try to be nefarious, and telling Windows applications to use places like ~/Documents may help to integrate Windows applications better into a Linux desktop, but they also allow Windows applications’ slothful, sloppy habits to run rampant in your nice, tidy user profile.

IMPORTANT: This will not improve your security. This is only to limit Windows applications’ ability to cause innocent mayhem because their developers didn’t think before they coded.

The hardest to discover (because, when I asked for it, Alexandre Julliard firmly declared it WONTFIX) but most useful trick (especially if you uninstall stuff more frequently than “never”)  is to kill off start menu icon generation so applications can’t leave cruft in your nice clean launcher menu. (Especially if you’re on a DE without a menu-editing GUI, which seemed to be everything but KDE 4 last I checked)

Simply set WINEDLLOVERRIDES="winemenubuilder.exe=d" as an environment variable. Wine will complain to stderr, but otherwise nothing bad will happen.

Next trick: Keeping your Wine applications from “helping” by adding new clutter to your profile. (eg. new folders inside My Documents, new samples in My Music, etc.)

This one is simply a matter of either going into your Wine profile and replacing the relevant symlinks or opening up winecfg and using the Desktop Integration tab.

If you’re really in a hurry, you can also brute-force it by turning all symlinks in your Wine prefix’s C: into folders with this command:

find ${WINEPREFIX:-~/.wine}/drive_c -type l -exec sh -c 'rm "{}"; mkdir "{}"' \;

Finally, while it’s not really a supported thing, I’ve had no problems with deleting the ~/.wine/dosdevices/z: symlink to give programs one less way to innocently mess up my files like some A.I. baby doodling on the wall in permanent marker.

If you really want security though (of the type that actually protects against certain types of exploits), research using cgroups to tie your filesystem in knots. It’s like chroot without the migraine.

Posted in Geek Stuff | Leave a comment

Minecraft + xkcd + Python = …

Just a little gag module to implement “import creeper” [1] [2]. Now you know what I do when I’ve got a moment with nothing better to do. 😛

Posted in Geek Stuff | Leave a comment

Making Tk applications a bit less ugly

Ever had that one application you found too useful to replace, but it looked like a refugee from 1989? Chances are the application is written using either Motif or Tk and, while I can’t help you poor souls with in-house Motif applications, I can help you beautify your Tk applications like git gui and idle.

As of Tk version 8.5, a new variation on the widget API known as Ttk (themed Tk, formerly Tile) has become available and every Tk-using application I’m aware of has been quick to adjust for compatibility.

Users of Ttk on Windows and MacOS X should already have the Ttk native theming engines enabled, but since they’ve had pretty native-looking theming even before Ttk, they’re not my concern.

Users of Linux, FreeBSD, and other non-Windows, non-OSX desktops will be running a Ttk theme named “default” which, while not as ugly as “classic”, still reeks of the old motif design aesthetic and, frustratingly, there is no “native theme” engine for X11 desktops. (The TkTable guy did write tile-gtk and tile-qt engines, but they haven’t been updated to build against current Ttk… at least on my system)

Thankfully, we’re not completely out of options. Ttk includes a theme named “clam” based on an older version of Xfce’s GTK+ theme. It’ll only get you to “refugee from 1999”, but a decade’s jump for one line of config file isn’t too bad.

Just add *TkTheme: clam to your ~/.Xresources and run xrdb -merge ~/.Xresources to apply it without logging out.

Also, if anyone knows what happened to the Plastik and Keramik themes shown on the Tile website, please let me know. They still wouldn’t fit with stuff like Canonical’s Human theme, but they’d be closer than clam.

UPDATE: I’ve found a Clearlooks pixmap theme that I’m now using instead.

Posted in Geek Stuff | 6 Comments