2013-03-10

Playing with REST: SilverShield USB power web frontend

Yesterday I started to code a small tool that lets me turn on and off power outlets on my USB controlled  power strip. You can find the humble beginnings over at github. It uses the sispmctl tool to turn on and off the sockets. It is implemented in Python using the Tornado web server. The rendering is simple, not styled and not yet HTML compliant. But it already scans for attached devices, and lets you toggle single outlets. However, today the power strip sort of died on me, and now I have to look for another project to practice REST with...

2013-03-09

How to convert a Python list into a string in a strange way

Given a list in Python, suppose you wanted a string representation of that list. Easy enough:
str( [ 1, 2, 3 ] )
However, suppose you did not want the standard notation, but rather apply some function to each string element or simply do away with the brackets and commas, you can use list comprehension, the join function and an empty string literal:
''.join( str( i ) for i in [ 1, 2, 3 ] )
I already knew list comprehension, but using it in this scenario and with a string literal as an object was new to me. Anyway, using such code is probably a bad idea, since it might be hard to read! At the very least, one should stow it away in a function with a fitting name, such as jointStringRepresentationOfListItems( list ). But really, I am not even sure what I would use that for...

Update: even better is this:

','.join( map( str, [ 1, 2, 3 ] ) )

2013-03-07

How to turn the Raspberry Pi into a music server

I have set up my Raspberry Pi as a music and file server. My requirements were:

  • Big local storage (HD), shared over WiFi
  • Local music playing capability, remotely controllable
  • AirPlay speaker capability
The means by which I fulfilled the requirements were:
  • Platinum 1TB MyDrive and Edimax EW-7711UTn USB wireless adapter
  • mpd Music Player Daemon
  • shairport AirPort emulator
For the first part, I bought a WiFi adapter, the Edimax EW-7711UTn. This one works out of the box with Raspbian, using WPA encryption (Note: I switched to a RTL8188 based dongle by now). It identifies itself with lsusb as:

Bus 001 Device 004: ID 7392:7711 Edimax Technology Co., Ltd EW-7711UTn nLite Wireless Adapter [Ralink RT2870]

This can be easily configured using the wpa_gui tool that comes with the default Raspbian installation. Settings will be saved and restored upon reboot.

Second, I installed samba, samba-common and samba-common-bin for sharing my USB drive. The latter one is a Platinum MyDrive, which is attached to a powered Belkin 7-port USB hub, so that I only need two power supplies. One for the Raspberry Py, and one for the Hub and its attached devices. The MyDrive has been formatted with NTFS, so as to be easily mountable under Linux, OS X and Windows. I mount it using the standard /etc/fstab mechanism. Just added one line to the file:

/dev/sda1       /media/MyDrive  ntfs-3g defaults          0       0

The /etc/samba/smb.conf gets in its first iteration only one additional share, a write-for-all public share, as a big file dump: 

[BigDump]
    comment = Free for all
    read only = no
    path = /media/MyDrive/FreeForAll
    guest ok = yes

Note that literally everybody in your LAN can access this and write to it! You may want to fine tune this...

Now to the mpd. It is easily installed by doing apt-get install mpd. When configuring it via /etc/mpd.conf make sure to change the following lines:

music_directory         "/media/MyDrive/Music/"
password                "MyVeryOwnPassword@read,add,control,admin"
bind_to_address         "any"

Change the directory and password to your liking. Then restart the service or reboot you device. You can control the mpd using a magnitude of clients. For example Theremin for OS X or Mpod for iOS.

Finally, I would like to be able to use the RasPi as an AirPlay target for my Mac and my iOS devices. This can be done via shairport. There are already a lot of good howtos for shairport on the Raspberry Pi. So I refer you to one of those. Two things come to my mind, though:
  1. The Net::SDP library, required by shairport, is not available on Raspbian by default. It is best to clone the github mirror, and go by its installation instructions. Installation via CPAN fails, plus CPAN needs huge amounts of RAM.
  2. The configuration needs to be tweaked a bit. The /etc/init.d/shairport script should be tuned to a sensible name for your RasPi. 
This way, you will be able to see the RasPi in your AirPlay speakers list and it will be happily churning along.


2013-01-22

How to determine if current buffer is tramp buffer in Emacs

Tramp is Emacs' way to access remote files, e.g. via SSH. To determine if the current buffer is accessing a tramp file (e.g. to not try to run gtags or similar), try this:
(not (tramp-tramp-file-p (buffer-file-name (current-buffer))))

2013-01-08

Quoting a set of parameters for program arguments using sed

I have a script which launches another program. That program takes some command line arguments, which in turn can have parameters. Those parameters may include ";", which has a special meaning in the Bash. To quote these, you can use the following sed command:
$ echo "foo --bar=1 --baz=2" | sed -e 's/\(--[^[:space:]]*=\)\([^[:space:]]*\)/\1"\2"/g'
foo --bar="1" --baz="2"

2012-10-30

Counting Color Pages in PDF Files

Printing books and such in color can be expensive. So here is how to count the color pages using a recent version of GhostScript and a UNIX shell:
gs -o - -sDEVICE=inkcov input.pdf | grep -v "^ 0.00000  0.00000  0.00000" | grep "^ " | wc -l

2012-10-29

Improved handling of background GNU Global update

Earlier this year I detailed how to run gtags of GNU Global when Emacs is idle. However, this collides with some special Emacs modes, like ediff. That mode makes its own Emacs frame layout, which can be destroyed by the xgtags incremental update buffer. So I conceived a way to disable gtags temporarily as long as ediff is active:
;; This stuff is only needed, if you haven't got xgtags loaded yet
(autoload 'gtags-mode "gtags" "" t)
(require 'xgtags)
(add-hook 'c-mode-common-hook (lambda () (xgtags-mode 1)))
(defun global-update-incrementally () (shell-command "global -u -q" "*Messages*" "*Messages*") )

;; Call gtags update when idle for some time
(defcustom my-gtags-update-idle-time 60
  "Number of idle seconds before an incremental gtags update is launched"
  :group 'my-group
  :type 'integer
  )

;; initially allow gtags updates
(setq my-gtags-update-active t)

(run-with-idle-timer my-gtags-update-idle-time t
                     (lambda ()
                       (if (and my-gtags-update-active
                                (not (minibufferp) )
                                )
                           (progn
                             (message "Running gtags...")
                             (global-update-incrementally)
                             )
                         )
                       )
                     )

(add-hook 'ediff-quit-hook 
          (lambda () 
            (message "Activating gtags update.")
            (setq my-gtags-update-active t) 
            )
          )

(add-hook 'ediff-before-setup-hook 
          (lambda () 
            (message "Deactivating gtags update.")
            (setq my-gtags-update-active nil) 
            )
          )