a.shortbrain

Open-source, Unix and python related ressources
home » code

Startwm : launch custom wm

It's just a little script to allow me to easily launch a custom window manager when starting X.

It's quite simple, you use startwm [option] instead of startx. Here there is juste dwm and openbox, but you can add what you want.

For example, to launch openbox, you'll have to do : startwm openbox.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#!/bin/bash
# startx
# author : Vincent Demeester <vincent@demeester.name>
# Time-stamp: "2007-10-05 20:44:11 demeester"

# A tiny script to launch a custom WM
# based on .xinitrc.xxx

dwm="$HOME/.xinitrc.dwm"
openbox="$HOME/.xinitrc.openbox"

if [ $# -ne 0 ]; then
    eval xwm='$'$1
else
    #default wm
    eval xwm=$dwm
fi

echo -ne "Backing up current conf.. "
cp $HOME/.xinitrc $HOME/.xinitrc.backup
if [ $? -ne 0 ]; then
    echo "failed"
    exit 1
else
    echo "ok"
fi
echo -ne "Loading config.. "
cp $xwm $HOME/.xinitrc
if [ $? -ne 0 ]; then
    echo "failed"
    exit 1
else
    echo "ok"
fi
echo "Starting $xwm"
exec startx -- -br -nolisten tcp -deferglyphs 16

¶ October 6th, 2007 at 5:05PM, in bash, xorg, wm, dwm, openbox and xinitrc.

Quick & dirty Django binary field

Here is a very quick and dirty solution to have a Binary field in django.

We just define a custom BinaryField, and overwrite the db_type() method, to let know django that the field type in the database should be a binary one.

Support multiple database backend

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
from django.db.models import fields

class BinaryField(fields.Field):
    def __init__(self, **kwargs):
        super(BinaryField, self).__init__(**kwargs)

    def db_type(self):
        """Hack to tell django to use binary in place
        of other.. Watch for the backend too"""
        from django.db import get_creation_module
        creation_db = get_creation_module().__name__.split('.')
        if 'ado_mssql' in creation_db: return 'varbinary(%(maxlength)s)' % self.__dict__
        if 'sqlite3' in creation_db: return 'BLOB' % self.__dict__
        if 'postgresql_psycopg2' in creation_db or 'postgresql' in creation_db: return 'bytea' % self.__dict__
        if 'mysql' in creation_db: return 'varbinary(%(maxlength)s)' % self.__dict__

    def get_manipulator_field_objs(self):
        from django import oldforms
        return [oldforms.HiddenField]

¶ September 23rd, 2007 at 8:32PM, in python, django, binary and field.

BBC Radio player & ripper

I like to listen the BBC Radio 1 as well as the other channel of the BBC Radio network. But I don't want to have firefox launched to listen them, nor I want to install the Linux real player application.

So, I write a tiny bash script that do the job for me in one command line. It works as the following.

$ bbc_radio.sh essmix # to play the essential mix of the week
$ bbc_radio.sh essmix -d essmix-20070901 # to rip the stream in ~/media/music/radio/essmix-20070901.rm

Envoy !

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/bin/sh
# Time-stamp: "2007-09-01 15:11:41 demeester"
# Vincent <vincent@demeester.name>
# Play radio bbc stream, 'cause that's the best

mplayer="/usr/bin/mplayer"

# Live stream
r1_live="rtsp://rmlive.bbc.co.uk/bbc-rbs/rmlive/ev7/live24/radio1/live/r1_dsat_g2.ra"
r2_live="rtsp://rmlive.bbc.co.uk/bbc-rbs/rmlive/ev7/live24/radio2/live/r2_dsat_g2.ra"
r3_live="rtsp://rmlive.bbc.co.uk/bbc-rbs/rmlive/ev7/live24/radio2/live/r3_dsat_g2.ra"
r4_live="rtsp://rmlive.bbc.co.uk/bbc-rbs/rmlive/ev7/live24/radio2/live/r4_dsat_g2.ra"
1extra_live="rtsp://rmlive.bbc.co.uk/bbc-rbs/rmlive/ev7/live24/1xtra/live/1xtra_dsat_g2.ra"
5live="rtsp://rmlive.bbc.co.uk/bbc-rbs/rmlive/ev7/live24/radio5/live/r5_tl_int_g2.ra"
5live_extra="rtsp://rmlive.bbc.co.uk/bbc-rbs/rmlive/ev7/live24/radio5/sportsextra/live/r5sx_tl_int_g2.ra"
bb7_live="rtsp://rmlive.bbc.co.uk/bbc-rbs/rmlive/ev7/live24/bbc7/live/bbc7_dsat_g2.ra"
6music_live="rtsp://rmlive.bbc.co.uk/bbc-rbs/rmlive/ev7/live24/6music/live/6music_dsat_g2.ra"
# Trance & else
essmix="rtsp://rmv8.bbc.net.uk/radio1/essmix.ra"
judge="rtsp://rmv8.bbc.net.uk/radio1/jules.ra"
# The Chris Moyles Show
cms_mo="rtsp://rmv8.bbc.net.uk/radio1/r1moyles_mon.ra"
cms_tu="rtsp://rmv8.bbc.net.uk/radio1/r1moyles_tue.ra"
cms_we="rtsp://rmv8.bbc.net.uk/radio1/r1moyles_wed.ra"
cms_th="rtsp://rmv8.bbc.net.uk/radio1/r1moyles_thu.ra"
cms_fr="rtsp://rmv8.bbc.net.uk/radio1/r1moyles_fri.ra"
# Annie Mac
amac="rtsp://rmv8.bbc.net.uk/radio1/r1anniemac.ra"


# Determine where is the functions file, and source it.
if [ -f "${HOME}/lib/functions/scripts" ]; then
    source "${HOME}/lib/functions/scripts"
elif [ -f "${HOME}/bin/functions/scripts" ]; then
    source "${HOME}/bin/functions/scripts"
elif [ -f "$(dirNAME "${0}")/functions/scripts" ]; then
    source "$(dirNAME "${0}")/functions/scripts"
elif [ -f "$(dirNAME "${0}")/../lib/functions/scripts" ]; then
    source "$(dirNAME "${0}")../lib/functions/scripts"
else
    echo "The required functions file could not be found, Aborting..."
    exit 54
fi

if [ ! -x $mplayer ]; then
    print_error 1 "mplayer bin or alias not defined"
    exit 1
fi

# if -d, use wget in place of mplayer
function process () {
    if [ "${2}" == "-d" ]; then
        # follow this scheme ``mplayer -dumpstream "<url>" -dumpfile <file>``
        $mplayer -dumpstream "${1}" -dumpfile ~/media/radio/$3.rm
    else
        $mplayer $1
    fi
    exit 0
}

eval stream='$'$1
echo $stream
process $stream $2 $3
if [ $? -ne 0]; then
    print_warning 1 "No stream founded. exiting.."
    exit 0
else
    print_info 1 "bye.."
fi

¶ Last update : September 5th, 2007 at 8:06PM

¶ September 1st, 2007 at 3:15PM, in bash, radio, bbc, player and encoder.

Photos sync

That's my photo sync script. The feature are currently simple : rewrite tag for eash photo in order to insert copyright, organize to construct the filesystem with photo organized, and upload to upload it to my website (here indeed).

The print_info, and other print_xxx are defined in ${HOME}/lib/functions/scripts. A code should be here soon.

Sources : Gérer ses métadonnées - Ici et ailleurs

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
#!/usr/bin/env sh
# Shell script for processing my photo(s)
# Vincent Demeester <vincent@demeester.name>

# CONST and GLOBAL Var
LOG_ENABLED=1
APP_BASEEXEC="photos.sh"
APP_HUMNNAME="Photos shell script"
NAME='Vincent Demeester'
PHOTOS_DIR='/home/demeester/images/photos'
IMPORT_DIR="$PHOTOS_DIR/import"
JPEG_DIR="$PHOTOS_DIR/jpeg"
RAW_DIR="$PHOTOS_DIR/raw"
# cmd
exiftool=`which exiftool`
convert=`which convert`

# Determine where is the functions file, and source it.
if [ -f "${HOME}/lib/functions/scripts" ]; then
    source "${HOME}/lib/functions/scripts"
elif [ -f "${HOME}/bin/functions/scripts" ]; then
    source "${HOME}/bin/functions/scripts"
elif [ -f "$(dirNAME "${0}")/functions/scripts" ]; then
    source "$(dirNAME "${0}")/functions/scripts"
elif [ -f "$(dirNAME "${0}")/../lib/functions/scripts" ]; then
    source "$(dirNAME "${0}")../lib/functions/scripts"
else
    echo "The required functions file could not be found, Aborting..."
    exit 54
fi

if [ ! -x $exiftool ]; then
    print_error 1 "exiftool not found"
    exit 1
fi

if [ ! -x $convert ]; then
    print_warning 1 "convert not found"
    print_warning 2 "some commands won't be available"
fi

quit(){
    exit 0
}

usage () {
  echo "Usage ${APP_BASEEXEC} commands [options] files.."
  echo ""
  echo "${APP_HUMNNAME} is a photography tools"
  echo "rewriting photography metadata, organize photos, upload them throw ssh, ftp.. etc."
  echo ""
  echo "Commands:"
  echo "   rewrite - rewrite exif data"
  echo "   help - This page"
  echo ""
}

function backup_import() {
    print_info 1 "Backup the import directory.. " 0
    cp -u -r $IMPORT_DIR/* $IMPORT_DIR.backup/
    if [ $? -ne 0 ]; then
        echo "${ERROR}[failed]${FG_CLEAR}"
        exit 1
    else
        echo -e "${GOOD}[ok]${FG_CLEAR}"
    fi
}

#TODO: add possibility to custom the rewrite
function rewrite() {
    backup_import
    print_info 1 "Rewrite tags.."
    for elt in $@; do
        if [ -d $elt ]; then
            print_info 1 "Process $elt directory.. " 0
            options='-r'
        elif [ -f $elt ]; then
            print_info 1 "Process $elt file.. " 0
        else
            print_warning 1 "Can't process $elt"
            break
        fi
        $exiftool $options -overwrite_original \
            -xmp:Creator="$NAME" \
            -xmp:WebStatement='http://creativecommons.org/licenses/by-nc-nd/3.0/' \
            -xmp:Rights="Copyright $NAME. This work is licensed under a Creative Commons Attribution-Noncommercial-No Derivative Works 3.0  License." \
            -iptc:By-line="$NAME" \
            -iptc:CopyrightNotice="Copyright $NAME. This work is licensed under a Creative Commons Attribution-Noncommercial-No Derivative Works 3.0  License." \
            -exif:Artist="$NAME" \
            -exif:Copyright="Copyright $NAME. This work is licensed under a Creative Commons Attribution-Noncommercial-No Derivative Works 3.0  License." \
            $elt > /dev/null 2>&1
    if [ $? -ne 0 ]; then
        echo "${ERROR}[failed]${FG_CLEAR}"
    else
        echo -e "${GOOD}[ok]${FG_CLEAR}"
    fi
    done
    exit 0
}

function upload() {
    print_info 1 "Preparing upload.. "
    print_info 2 "Override.. " 0
    if [ "${1}" == "override" ]; then
        override=1
    fi
    override=1 # well.. not faire but.. it's not what i want
    if [ ! "$override" == "1" ]; then
        toup=`find $JPEG_DIR -type f -iname '*.JPG' -cnewer ~/.photos.last_sync`
    else
        toup="$JPEG_DIR/*"
    fi
    echo "yes"
    print_info 2 "backup $JPEG_DIR" 0
    mkdir $JPEG_DIR.backup > /dev/null 2>&1
    cp -u -r $JPEG_DIR/* $JPEG_DIR.backup/
    if [ $? -ne 0 ]; then
        echo "${ERROR}[failed]${FG_CLEAR}"
        exit 1
    else
        echo -e "${GOOD}[ok]${FG_CLEAR}"
    fi
    for src in $JPEG_DIR/*; do
        for file in $src/*.jpg; do
            print_info 2 "converting.. " 0
            copy=`echo $file.tmp`
            cp $file $copy
            if [ $? -ne 0 ]; then
                echo -e "${ERROR}[failed]${FG_CLEAR}"
                exit 1
            else
                echo -n "tmp.. "
            fi
            $convert -resize 1600x1600 -size 1600x1600 $file $file.bmp
            if [ $? -ne 0 ]; then
                echo -e "${ERROR}[failed]${FG_CLEAR}"
                exit 1
            else
                echo -n "bmp.. "
            fi
            $convert -quality 90 $file.bmp $file
            if [ $? -ne 0 ]; then
                echo -e "${ERROR}[failed]${FG_CLEAR}"
                exit 1
            else
                echo -e "${GOOD}[ok]${FG_CLEAR}"
            fi
            print_info 3 "Rewrite metadata.." 0
            $exiftool -q -overwrite_original \
	            -TagsFromFile $copy \
	            -exif:all \
	            -xmp:Description \
	            -xmp:Title \
	            -xmp:Subject \
	            -xmp:Location \
	            -xmp:City \
	            -xmp:State \
	            -xmp:Country \
	            -xmp:CountryCode \
	            -xmp:Rights \
	            -xmp:Creator \
	            '-xmp-exif:all > exif:all' \
	            '-xmp:Description > exif:ImageDescription' \
	            '-xmp:Title > iptc:Headline' \
	            '-xmp:Subject > iptc:Keywords' \
	            '-xmp:Location > iptc:Sub-location' \
	            '-xmp:City > iptc:City' \
	            '-xmp:State > iptc:Province-State' \
	            '-xmp:Country > iptc:Country-PrimaryLocationName' \
	            '-xmp:CountryCode > iptc:Country-PrimaryLocationCode' \
	            '-xmp:Rights > iptc:CopyrightNotice' \
	            '-xmp:Rights > exif:Copyright' \
	            '-xmp:Creator > iptc:By-line' \
	            '-xmp:Creator > exif:Artist' \
	            '-exif:DateTimeOriginal > exif:CreateDate' \
	            '-xmp:Lens > exif:Lens' \
	            -xmp:WebStatement \
	            -ICC_Profile \
	            '-exif:DateTimeOriginal>FileModifyDate' \
	            $file > /dev/null 2>&1
            rm $copy $file.bmp
            if [ $? -ne 0 ]; then
                echo -e "${ERROR}[failed]${FG_CLEAR}"
                exit 1
            else
                echo -e "${GOOD}[ok]${FG_CLEAR}"
            fi
         done
    done
    print_info 1 "Upload photos.. " 0
    scp -2 -p -r $toup shortbrain:~/public_html/photos/ > /dev/null 2>&1
    if [ $? -ne 0 ]; then
        echo "${ERROR}[failed]${FG_CLEAR}"
        exit 1
    else
        echo -e "${GOOD}[ok]${FG_CLEAR}"
    fi
    print_info 1 "Restoring photos.." 0
    rm -fR $JPEG_DIR
    mv $JPEG_DIR.backup $JPEG_DIR
    if [ $? -ne 0 ]; then
        echo "${ERROR}[failed]${FG_CLEAR}"
        exit 1
    else
        echo -e "${GOOD}[ok]${FG_CLEAR}"
    fi
    touch ~/.photos.last_sync
    exit 0
}

function organize() {
    backup_import
    print_info 1 "Organize photos"
    print_info 2 "processing jpg and raw (nef) separatly"
    for elt in $@; do
        if [ -d $elt ]; then
            print_info 1 "Process $elt directory.. " 0
            options="-r"
        elif [ -f $elt ]; then
            print_info 1 "Process $elt file.. " 0
        else
            print_warning 1 "Can't process $elt"
            break
        fi
        $exiftool -ext .jpg $options -d $JPEG_DIR/%Y-%m-%d/%%f%%c.jpg \
            "-filename<datetimeoriginal" $elt > /dev/null 2>&1
        $exiftool -ext .nef $options -d $RAW_DIR/%Y-%m-%d/%%f%%c.nef \
            "-filename<datetimeoriginal" $elt > /dev/null 2>&1
        if [ $? -ne 0 ]; then
            echo "${ERROR}[failed]${FG_CLEAR}"
        else
            echo -e "${GOOD}[ok]${FG_CLEAR}"
        fi
    done
    exit 0
}

if [ "${1}" == "rewrite" ]; then
    shift # next argument
    rewrite $@
elif [ "${1}" == "upload" ]; then
    shift # next argument
    upload $@
elif [ "${1}" == "organize" ]; then
    shift # next argument
    organize $@
elif [ "${1}" == "help" ]; then
    # print usage
    usage
    # quit now
    quit
fi

¶ Last update : September 5th, 2007 at 8:06PM

¶ August 25th, 2007 at 3:55PM, in photos, bash, sync, exif, xmp and exiftool.

Hello code

Just an Hello World for the code application

1
2
def hello():
    print 'world'

¶ Last update : September 5th, 2007 at 8:06PM

¶ August 20th, 2007 at 12:08PM, in shortbrain and hello.