<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>K&#039;s Lair &#187; python</title>
	<atom:link href="http://andreas.goelzer.de/tag/python/feed" rel="self" type="application/rss+xml" />
	<link>http://andreas.goelzer.de</link>
	<description>Electronics and small programs and other stuff</description>
	<lastBuildDate>Thu, 02 Sep 2010 12:30:24 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>GPS plasmoid</title>
		<link>http://andreas.goelzer.de/gps-plasmoid</link>
		<comments>http://andreas.goelzer.de/gps-plasmoid#comments</comments>
		<pubDate>Thu, 09 Apr 2009 16:16:20 +0000</pubDate>
		<dc:creator>Andreas Goelzer</dc:creator>
				<category><![CDATA[Computers]]></category>
		<category><![CDATA[gps]]></category>
		<category><![CDATA[plasmoid]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://andreas.goelzer.de/?p=133</guid>
		<description><![CDATA[Having recently acquired a bluetooth GPS receiver for as little as 20 €, and just having switched to KDE 4, i tried to combine those two, and write a little plasmoid to show the current position on the desktop. And &#8230; <a href="http://andreas.goelzer.de/gps-plasmoid">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Having recently acquired a bluetooth <a href="http://en.wikipedia.org/wiki/Gps">GPS</a> receiver for as little as 20 €, and just having switched to <a href="http://www.kde.org/announcements/4.2/desktop.php">KDE 4</a>, i tried to combine those two, and write a little plasmoid to show the current position on the desktop.</p>
<p>And that is basically everything to say, enjoy the little applet.</p>
<h3>Installation</h3>
<p>Make sure <a href="http://gpsd.berlios.de/">gpsd</a> is installed and set up properly. Also make sure the python bindings are installed.<br />
In Ubuntu you would need to:</p>
<pre>sudo apt-get install gpsd python-gps
sudo dpkg-reconfigure gpsd
sudo apt-get install python-plasma python-dev</pre>
<p>After those are installed,  you can proceed to install the plasmoid with plasmapkg -i.</p>
<h3>Problems and TODOs</h3>
<p>This is all rather dirty and bloated. The correct way to do it would probably be to write a data engine for plasma, which would provide the positional data. Maybe someone will do it, especially if <a href="http://edu.kde.org/marble/">Marble</a> will one day <strike>be able to support overlays</strike> integrate <a href="http://geoclue.freedesktop.org/">GeoClue</a> or something similar.</p>
<p>Right now the data travels from the GPS device to the PC in the form of <a href="http://www.gpsinformation.org/dale/nmea.htm">NMEA sentences</a>, gets then converted into gpsd&#8217;s own format, python-gps connects to gpsd over TCP port 2947, interprets the gpsd output, hands it over to the applet, which reformats the data yet again. For something so useless that just wastes too many CPU cycles. <a href="http://andreas.goelzer.de/gps-mouse-in-python">Interpreting GPS data</a> entirely in python is not hard, but still a bit harder than with gpsd, especially if one wants to cover a lot of GPS devices. Don&#8217;t get me wrong, gpsd does its job quite well, it is just a bit overkill for this task.</p>
<p>Another improvement would be readable coordinates. Something like &#8220;10 km northwest of Zürich&#8221; or even &#8220;200 meters south of Sternen Oerlikon, Zürich&#8221; would really be much more useful.</p>
<p>Update February 2010: Version 0.16 now uses KDE&#8217;s geolocation data engine. This drops the (direct) dependency on gpsd, and allows for ip-based location &#8211; unfortnately i now can no longer get my gps to work with it, hoping for kubuntu lucid.<br /><hr /><a href='http://andreas.goelzer.de/download/gps-plasmoid_0.16.plasmoid'><img src="http://andreas.goelzer.de/wp-content/plugins/kfile/fileicons/file-generic.png" width="16" height="16" alt="filetype" class="icon16" /> gps-plasmoid_0.16.plasmoid</a> (5.85 kiB, 2010-02-13)<br/><a href='http://andreas.goelzer.de/download/gps-plasmoid_0.15.plasmoid'><img src="http://andreas.goelzer.de/wp-content/plugins/kfile/fileicons/file-generic.png" width="16" height="16" alt="filetype" class="icon16" /> gps-plasmoid_0.15.plasmoid</a> (2.59 kiB, 2009-04-11)<br/><a href='http://andreas.goelzer.de/download/gps-plasmoid012.png'><img src="http://andreas.goelzer.de/wp-content/plugins/kfile/fileicons/file-image.png" width="16" height="16" alt="filetype" class="icon16" /> gps-plasmoid012.png</a> (911.48 kiB, 2009-04-09)<br/><hr />
]]></content:encoded>
			<wfw:commentRss>http://andreas.goelzer.de/gps-plasmoid/feed</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>GPS Mouse in Python</title>
		<link>http://andreas.goelzer.de/gps-mouse-in-python</link>
		<comments>http://andreas.goelzer.de/gps-mouse-in-python#comments</comments>
		<pubDate>Sat, 08 Nov 2008 00:15:49 +0000</pubDate>
		<dc:creator>Andreas Goelzer</dc:creator>
				<category><![CDATA[Computers]]></category>
		<category><![CDATA[gps]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://andreas.goelzer.de/?p=74</guid>
		<description><![CDATA[Some months ago I aquired a cheap gps mouse (Royaltek RGM-2000, really cheap, 2 € + shipping) at ebay. My plans to use it in combination with an microcontroller and a display haven&#8217;t worked out, it is a lot of &#8230; <a href="http://andreas.goelzer.de/gps-mouse-in-python">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Some months ago I aquired a cheap gps mouse (Royaltek RGM-2000, really cheap, 2 € + shipping) at ebay. My plans to use it in combination with an microcontroller and a display haven&#8217;t worked out, it is a lot of work and the garmin units do that job fine.<br />
However, playing around with it in python lead to better results. Getting the data to <a href="http://earth.google.com/">google earth</a> works nicely, and at least <a href="http://qlandkarte.sourceforge.net/">qlandkarte</a> can read the generated gpx tracks.</p>
<h4>Usage</h4>
<ul>
<li>Start the Program, add a parameter for the port your nmea device is attached to (/dev/ttyUSB0 is the default</li>
<li>Start google earth or something, load the generated kml file (/tmp/posdate by default)</li>
<li>Exit the program by pressing ctrl-c, track data will be written to /tmp/track-some time.gpx</li>
</ul>
<p>Guess I should code something with <a href="http://xkcd.com/407/">extra geek value</a> now.<br />
It might be comfortable to have google earth automatically reload the file, so instead open sth. like</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;UTF-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;kml</span> <span style="color: #000066;">xmlns</span>=<span style="color: #ff0000;">&quot;http://earth.google.com/kml/2.1&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;NetworkLink<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>GPS<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/name<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;flyToView<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>1<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/flyToView<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;Url<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;href<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>/tmp/posdata.kml<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/href<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;refreshMode<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>onInterval<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/refreshMode<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
		<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;refreshInterval<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>10<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/refreshInterval<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
	<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/Url<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/NetworkLink<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/kml<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">#!/usr/bin/python</span>
<span style="color: #483d8b;">&quot;&quot;&quot;
Script to read nmea data from a GPS mouse
Copyright (C) 2008  Andreas Goelzer
&nbsp;
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
&nbsp;
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
&nbsp;
You should have received a copy of the GNU General Public License
along with this program.  If not, see &lt;http://www.gnu.org/licenses/&gt;.
&nbsp;
This program reads data from a nmea device (uses the GGA sentence),
and creates a google kml file with the current position and the path
leading there. Additionally, a gpx file is generated containing the
track data with time information.
&quot;&quot;&quot;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">from</span> serial <span style="color: #ff7700;font-weight:bold;">import</span> Serial<span style="color: #66cc66;">;</span>
<span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">time</span> <span style="color: #ff7700;font-weight:bold;">import</span> strftime, gmtime<span style="color: #66cc66;">;</span>
<span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">optparse</span> <span style="color: #ff7700;font-weight:bold;">import</span> OptionParser<span style="color: #66cc66;">;</span>
&nbsp;
&nbsp;
hist = <span style="color: #483d8b;">&quot;&quot;</span><span style="color: #66cc66;">;</span>
gpx = <span style="color: #483d8b;">&quot;&quot;</span><span style="color: #66cc66;">;</span>
&nbsp;
kmlmask=<span style="color: #483d8b;">&quot;&quot;&quot;&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
&lt;kml xmlns=&quot;http://earth.google.com/kml/2.0&quot;&gt;
&lt;Document&gt;
	&lt;Placemark&gt;
		&lt;name&gt;Your Position&lt;/name&gt;
		&lt;Point&gt;
			&lt;coordinates&gt;%s&lt;/coordinates&gt;
		&lt;/Point&gt;
	&lt;/Placemark&gt;
	&lt;Placemark&gt;
		&lt;name&gt;Previous positions&lt;/name&gt;
		&lt;description&gt;Path that lead you here&lt;/description&gt;
		&lt;LineString&gt;
			&lt;coordinates&gt;%s&lt;/coordinates&gt;
		&lt;/LineString&gt;
	&lt;/Placemark&gt;
&lt;/Document&gt;
&lt;/kml&gt;&quot;&quot;&quot;</span>
<span style="color: #808080; font-style: italic;">#			&lt;altitudeMode&gt;absolute&lt;/altitudeMode&gt;</span>
&nbsp;
gpxmask = <span style="color: #483d8b;">&quot;&quot;&quot;&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot; ?&gt;&lt;gpx xmlns=&quot;http://www.topografix.com/GPX/1/1&quot; xmlns:gpxx=&quot;http://www.garmin.com/xmlschemas/GpxExtensions/v3&quot; xmlns:gpxtpx=&quot;http://www.garmin.com/xmlschemas/TrackPointExtension/v1&quot; creator=&quot;Colorado 300&quot; version=&quot;1.1&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; xsi:schemaLocation=&quot;http://www.topografix.com/GPX/1/1 http://www.topografix.com/GPX/1/1/gpx.xsd http://www.garmin.com/xmlschemas/GpxExtensions/v3 http://www.garmin.com/xmlschemas/GpxExtensions/v3/GpxExtensionsv3.xsd http://www.garmin.com/xmlschemas/TrackPointExtension/v1 http://www.garmin.com/xmlschemas/TrackPointExtension/v1/TrackPointExtensionv1.xsd&quot;&gt;
&lt;trk&gt;&lt;name&gt;%s&lt;/name&gt;&lt;trkseg&gt;%s&lt;/trkseg&gt;&lt;/trk&gt;&lt;/gpx&gt;&quot;&quot;&quot;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> gpstime<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
	<span style="color: #ff7700;font-weight:bold;">return</span> strftime<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;%Y-%m-%dT%H:%M:%SZ&quot;</span>,gmtime<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> readlatlon<span style="color: black;">&#40;</span>value,inverse<span style="color: black;">&#41;</span>:
	lat = <span style="color: #008000;">float</span><span style="color: black;">&#40;</span>value<span style="color: black;">&#41;</span><span style="color: #66cc66;">;</span>
	lat2 = <span style="color: #008000;">int</span><span style="color: black;">&#40;</span>lat<span style="color: black;">&#41;</span> / <span style="color: #ff4500;">100</span><span style="color: #66cc66;">;</span>
	lat = lat2 + <span style="color: black;">&#40;</span>lat - <span style="color: #ff4500;">100</span> <span style="color: #66cc66;">*</span> lat2<span style="color: black;">&#41;</span>/ <span style="color: #ff4500;">60</span><span style="color: #66cc66;">;</span> 
	<span style="color: #ff7700;font-weight:bold;">if</span> inverse:
		lat = -lat<span style="color: #66cc66;">;</span>
	<span style="color: #ff7700;font-weight:bold;">return</span> lat<span style="color: #66cc66;">;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> calcchecksum<span style="color: black;">&#40;</span>checkstr<span style="color: black;">&#41;</span>:
	cs = <span style="color: #008000;">ord</span><span style="color: black;">&#40;</span>checkstr<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">;</span>
	<span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span>,<span style="color: #008000;">len</span><span style="color: black;">&#40;</span>checkstr<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>:
		cs ^= <span style="color: #008000;">ord</span><span style="color: black;">&#40;</span>checkstr<span style="color: black;">&#91;</span>i<span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
	<span style="color: #ff7700;font-weight:bold;">return</span> cs<span style="color: #66cc66;">;</span>
&nbsp;
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> sentencevalidator:
	<span style="color: #483d8b;">&quot;Check checksum and other stuff and split to fields&quot;</span>
	<span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>,line<span style="color: black;">&#41;</span>:
		<span style="color: #ff7700;font-weight:bold;">if</span><span style="color: black;">&#40;</span>line<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span> == <span style="color: #483d8b;">'$'</span> <span style="color: #ff7700;font-weight:bold;">and</span> line<span style="color: black;">&#91;</span>-<span style="color: #ff4500;">5</span><span style="color: black;">&#93;</span> == <span style="color: #483d8b;">'*'</span> <span style="color: #ff7700;font-weight:bold;">and</span> calcchecksum<span style="color: black;">&#40;</span>line<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span>:-<span style="color: #ff4500;">5</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span> ==  <span style="color: #008000;">int</span><span style="color: black;">&#40;</span>line<span style="color: black;">&#91;</span>-<span style="color: #ff4500;">4</span>:-<span style="color: #ff4500;">2</span><span style="color: black;">&#93;</span>,<span style="color: #ff4500;">16</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>:
			<span style="color: #008000;">self</span>.<span style="color: black;">valid</span> = <span style="color: #008000;">True</span><span style="color: #66cc66;">;</span>
			<span style="color: #008000;">self</span>.<span style="color: black;">sentence</span> = line<span style="color: black;">&#91;</span><span style="color: #ff4500;">3</span>:<span style="color: #ff4500;">6</span><span style="color: black;">&#93;</span><span style="color: #66cc66;">;</span>
			<span style="color: #008000;">self</span>.<span style="color: black;">data</span> = line<span style="color: black;">&#91;</span><span style="color: #ff4500;">7</span>:-<span style="color: #ff4500;">5</span><span style="color: black;">&#93;</span>.<span style="color: black;">split</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">','</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">;</span>
		<span style="color: #ff7700;font-weight:bold;">else</span> :
			<span style="color: #008000;">self</span>.<span style="color: black;">valid</span> = <span style="color: #008000;">False</span><span style="color: #66cc66;">;</span>
&nbsp;
<span style="color: #dc143c;">parser</span> = OptionParser<span style="color: black;">&#40;</span>version=<span style="color: #483d8b;">&quot;%prog 0.1&quot;</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">;</span>
<span style="color: #dc143c;">parser</span>.<span style="color: black;">add_option</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;-s&quot;</span>, <span style="color: #483d8b;">&quot;--serial&quot;</span>, dest=<span style="color: #483d8b;">&quot;serial&quot;</span>,
                  <span style="color: #008000;">help</span>=<span style="color: #483d8b;">&quot;serial terminal of the gps unit&quot;</span>, default=<span style="color: #483d8b;">'/dev/ttyUSB0'</span>, metavar=<span style="color: #483d8b;">&quot;FILE&quot;</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">;</span>
<span style="color: #dc143c;">parser</span>.<span style="color: black;">add_option</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;-g&quot;</span>, <span style="color: #483d8b;">&quot;--gpxfile&quot;</span>, dest=<span style="color: #483d8b;">&quot;gpxfile&quot;</span>,
                  <span style="color: #008000;">help</span>=<span style="color: #483d8b;">&quot;GPX output file&quot;</span>, default=<span style="color: #483d8b;">&quot;/tmp/track&quot;</span> + gpstime<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> + <span style="color: #483d8b;">&quot;.gpx&quot;</span>, metavar=<span style="color: #483d8b;">&quot;FILE&quot;</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">;</span>
<span style="color: #dc143c;">parser</span>.<span style="color: black;">add_option</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;-k&quot;</span>, <span style="color: #483d8b;">&quot;--kmlfile&quot;</span>, dest=<span style="color: #483d8b;">&quot;kmlfile&quot;</span>,
                  <span style="color: #008000;">help</span>=<span style="color: #483d8b;">&quot;KML output file&quot;</span>, default=<span style="color: #483d8b;">&quot;/tmp/posdata.kml&quot;</span>, metavar=<span style="color: #483d8b;">&quot;FILE&quot;</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">;</span>
<span style="color: #dc143c;">parser</span>.<span style="color: black;">add_option</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;-v&quot;</span>, <span style="color: #483d8b;">&quot;--verbose&quot;</span>,
                  action=<span style="color: #483d8b;">&quot;store_true&quot;</span>, dest=<span style="color: #483d8b;">&quot;verbose&quot;</span>, default=<span style="color: #008000;">False</span>,
                  <span style="color: #008000;">help</span>=<span style="color: #483d8b;">&quot;print debug messages&quot;</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">;</span>
&nbsp;
<span style="color: black;">&#40;</span>options, args<span style="color: black;">&#41;</span> = <span style="color: #dc143c;">parser</span>.<span style="color: black;">parse_args</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">;</span>
&nbsp;
gps = Serial<span style="color: black;">&#40;</span>options.<span style="color: black;">serial</span>, <span style="color: #ff4500;">4800</span>, timeout=<span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">;</span>
&nbsp;
&nbsp;
<span style="color: #808080; font-style: italic;">#on sirf chips, activate all sentences</span>
<span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span>,<span style="color: #ff4500;">5</span><span style="color: black;">&#41;</span>:
	<span style="color: #dc143c;">cmd</span> = <span style="color: #483d8b;">&quot;PSRF103,0%d,00,05,01&quot;</span> <span style="color: #66cc66;">%</span> i<span style="color: #66cc66;">;</span>
	realcmd=<span style="color: #483d8b;">&quot;$%s*%x<span style="color: #000099; font-weight: bold;">\r</span><span style="color: #000099; font-weight: bold;">\n</span>&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span><span style="color: #dc143c;">cmd</span>,calcchecksum<span style="color: black;">&#40;</span><span style="color: #dc143c;">cmd</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">;</span>
	<span style="color: #ff7700;font-weight:bold;">print</span> realcmd<span style="color: #66cc66;">;</span>
	gps.<span style="color: black;">write</span><span style="color: black;">&#40;</span>realcmd<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">try</span>:
	<span style="color: #ff7700;font-weight:bold;">while</span> <span style="color: #ff4500;">1</span>:
		line = gps.<span style="color: #dc143c;">readline</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
		<span style="color: #ff7700;font-weight:bold;">if</span><span style="color: black;">&#40;</span>options.<span style="color: black;">verbose</span><span style="color: black;">&#41;</span>: <span style="color: #ff7700;font-weight:bold;">print</span> line,
		s = sentencevalidator<span style="color: black;">&#40;</span>line<span style="color: black;">&#41;</span><span style="color: #66cc66;">;</span>
		<span style="color: #ff7700;font-weight:bold;">if</span><span style="color: black;">&#40;</span>s.<span style="color: black;">valid</span> <span style="color: #ff7700;font-weight:bold;">and</span> s.<span style="color: black;">sentence</span> == <span style="color: #483d8b;">&quot;GGA&quot;</span> <span style="color: #ff7700;font-weight:bold;">and</span> <span style="color: #008000;">int</span><span style="color: black;">&#40;</span>s.<span style="color: black;">data</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">5</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span> <span style="color: #66cc66;">!</span>= <span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span>:
			lat = readlatlon<span style="color: black;">&#40;</span>s.<span style="color: black;">data</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span>,s.<span style="color: black;">data</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">2</span><span style="color: black;">&#93;</span>==<span style="color: #483d8b;">'S'</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">;</span>
			lon = readlatlon<span style="color: black;">&#40;</span>s.<span style="color: black;">data</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">3</span><span style="color: black;">&#93;</span>,s.<span style="color: black;">data</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">4</span><span style="color: black;">&#93;</span>==<span style="color: #483d8b;">'W'</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">;</span>
			alt = <span style="color: #008000;">float</span><span style="color: black;">&#40;</span>s.<span style="color: black;">data</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">8</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">;</span>
&nbsp;
			gpx += <span style="color: #483d8b;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span> + <span style="color: #483d8b;">'&lt;trkpt lat=&quot;%f&quot; lon=&quot;%f&quot;&gt;&lt;ele&gt;%f&lt;/ele&gt;&lt;time&gt;%s&lt;/time&gt;&lt;/trkpt&gt;'</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>lon,lat,alt,gpstime<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
			position =  <span style="color: #483d8b;">&quot;%f, %f, %f&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>lon,lat,alt<span style="color: black;">&#41;</span><span style="color: #66cc66;">;</span>
			<span style="color: #ff7700;font-weight:bold;">print</span> position
			hist += <span style="color: #483d8b;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span> + position<span style="color: #66cc66;">;</span>
			f=<span style="color: #008000;">open</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;/tmp/posdata.kml&quot;</span>,<span style="color: #483d8b;">'w'</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">;</span>
			f.<span style="color: black;">write</span><span style="color: black;">&#40;</span>kmlmask <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>position,hist<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">;</span>
			f.<span style="color: black;">close</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">except</span>:
	<span style="color: #ff4500;">0</span>
&nbsp;
f=<span style="color: #008000;">open</span><span style="color: black;">&#40;</span>options.<span style="color: black;">gpxfile</span>,<span style="color: #483d8b;">'w'</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">;</span>
f.<span style="color: black;">write</span><span style="color: black;">&#40;</span>gpxmask <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>gpstime<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>,gpx<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">;</span>
f.<span style="color: black;">close</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">;</span>
&nbsp;
gps.<span style="color: black;">close</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></td></tr></table></div>

<hr /><a href='http://andreas.goelzer.de/download/readgps.py'><img src="http://andreas.goelzer.de/wp-content/plugins/kfile/fileicons/file-source-alt.png" width="16" height="16" alt="filetype" class="icon16" /> readgps.py</a> (4.46 kiB, 2008-11-08)<br/><br />
<hr />
]]></content:encoded>
			<wfw:commentRss>http://andreas.goelzer.de/gps-mouse-in-python/feed</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>kernel config based on lsmod output</title>
		<link>http://andreas.goelzer.de/kernel-config-based-on-lsmod-output</link>
		<comments>http://andreas.goelzer.de/kernel-config-based-on-lsmod-output#comments</comments>
		<pubDate>Mon, 06 Oct 2008 11:35:20 +0000</pubDate>
		<dc:creator>Andreas Goelzer</dc:creator>
				<category><![CDATA[Computers]]></category>
		<category><![CDATA[config]]></category>
		<category><![CDATA[kernel]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://andreas.goelzer.de/?p=23</guid>
		<description><![CDATA[After reading about the amazing 5-seconds bootup I decided to once again compile a kernel myself. Compiling a kernel is almost trivial these days, but customizing the configuration can still be quite confusing. For example, the names of the modules &#8230; <a href="http://andreas.goelzer.de/kernel-config-based-on-lsmod-output">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>After reading about the amazing <a href="http://lwn.net/Articles/299483/">5-seconds bootup</a> I decided to once again compile a kernel myself. Compiling a kernel is almost trivial these days, but customizing the configuration can still be quite confusing. For example, the names of the modules in lsmod aren&#8217;t the ones you select as config options. To map them, I found some scripts in the <a href="http://wiki.linuxquestions.org/wiki/Configuring_linux_kernel">LQWiki</a>, but they weren&#8217;t that easily to use, and also programming in bash is just painful.</p>
<p>So i wrote a python variant, that takes an input config(for example your distributions config) and changes the reply to &#8220;y&#8221; for all config options if the respective module is loaded(ie. if ext3 is loaded, CONFIG_EXT3_FS=y will be set).</p>
<p>In almost all cases you still want to tweak the resulting config file with make menuconfig. Also keep in mind that some things don&#8217;t work that easily if compiled in, for example if firmware has to be loaded from a file, the disk should be accessible.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">#!/usr/bin/python</span>
<span style="color: #808080; font-style: italic;"># -*- coding: utf-8 -*-</span>
<span style="color: #483d8b;">&quot;&quot;&quot;
Script to modify kernel config based on lsmod output
Copyright (C) 2008  Andreas Goelzer
&nbsp;
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
&nbsp;
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
&nbsp;
You should have received a copy of the GNU General Public License
along with this program.  If not, see &lt;http://www.gnu.org/licenses/&gt;.
&nbsp;
&nbsp;
Based on a previous config and the output of lsmod, this script determines
which modules could be compiled in and generates a new config.
See http://andreas.goelzer.de/kernel-config-based-on-lsmod-output for updates
&quot;&quot;&quot;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">re</span><span style="color: #66cc66;">;</span>
<span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">optparse</span> <span style="color: #ff7700;font-weight:bold;">import</span> OptionParser<span style="color: #66cc66;">;</span>
<span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">os</span> <span style="color: #ff7700;font-weight:bold;">import</span> popen<span style="color: #66cc66;">;</span>
<span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">sys</span> <span style="color: #ff7700;font-weight:bold;">import</span> stderr, stdout, stdin<span style="color: #66cc66;">;</span>
&nbsp;
<span style="color: #dc143c;">parser</span> = OptionParser<span style="color: black;">&#40;</span>version=<span style="color: #483d8b;">&quot;%prog 0.31&quot;</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">;</span>
<span style="color: #dc143c;">parser</span>.<span style="color: black;">add_option</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;-i&quot;</span>, <span style="color: #483d8b;">&quot;--infile&quot;</span>, dest=<span style="color: #483d8b;">&quot;cfgfile&quot;</span>,
                  <span style="color: #008000;">help</span>=<span style="color: #483d8b;">&quot;input config file&quot;</span>, default=<span style="color: #483d8b;">&quot;.config&quot;</span>, metavar=<span style="color: #483d8b;">&quot;FILE&quot;</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">;</span>
<span style="color: #dc143c;">parser</span>.<span style="color: black;">add_option</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;-o&quot;</span>, <span style="color: #483d8b;">&quot;--outfile&quot;</span>, dest=<span style="color: #483d8b;">&quot;outfile&quot;</span>,
                  <span style="color: #008000;">help</span>=<span style="color: #483d8b;">&quot;output config file&quot;</span>, default=<span style="color: #483d8b;">&quot;-&quot;</span>, metavar=<span style="color: #483d8b;">&quot;FILE&quot;</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">;</span>
<span style="color: #dc143c;">parser</span>.<span style="color: black;">add_option</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;-l&quot;</span>, <span style="color: #483d8b;">&quot;--logfile&quot;</span>, dest=<span style="color: #483d8b;">&quot;logfile&quot;</span>,
                  <span style="color: #008000;">help</span>=<span style="color: #483d8b;">&quot;file to log errors to&quot;</span>, default=<span style="color: #483d8b;">&quot;-&quot;</span>, metavar=<span style="color: #483d8b;">&quot;FILE&quot;</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">;</span>
<span style="color: #dc143c;">parser</span>.<span style="color: black;">add_option</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;-s&quot;</span>, <span style="color: #483d8b;">&quot;--sourcedir&quot;</span>, dest=<span style="color: #483d8b;">&quot;sourcedir&quot;</span>,
                  <span style="color: #008000;">help</span>=<span style="color: #483d8b;">&quot;kernel source tree&quot;</span>, default=<span style="color: #483d8b;">&quot;.&quot;</span>, metavar=<span style="color: #483d8b;">&quot;DIR&quot;</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">;</span>
<span style="color: #808080; font-style: italic;">#parser.add_option(&quot;-v&quot;, &quot;--verbose&quot;,</span>
                  <span style="color: #808080; font-style: italic;">#action=&quot;store_true&quot;, dest=&quot;verbose&quot;, default=False,</span>
                  <span style="color: #808080; font-style: italic;">#help=&quot;print debug messages&quot;);</span>
&nbsp;
<span style="color: black;">&#40;</span>options, args<span style="color: black;">&#41;</span> = <span style="color: #dc143c;">parser</span>.<span style="color: black;">parse_args</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">;</span>
<span style="color: #ff7700;font-weight:bold;">if</span><span style="color: black;">&#40;</span>options.<span style="color: black;">outfile</span> == <span style="color: #483d8b;">'-'</span><span style="color: black;">&#41;</span>: of = stdout<span style="color: #66cc66;">;</span>
<span style="color: #ff7700;font-weight:bold;">else</span>: of = <span style="color: #008000;">open</span><span style="color: black;">&#40;</span>options.<span style="color: black;">outfile</span>, <span style="color: #483d8b;">'w'</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">;</span>
<span style="color: #ff7700;font-weight:bold;">if</span><span style="color: black;">&#40;</span>options.<span style="color: black;">logfile</span> <span style="color: #66cc66;">!</span>= <span style="color: #483d8b;">'-'</span><span style="color: black;">&#41;</span>: stderr = <span style="color: #008000;">open</span><span style="color: black;">&#40;</span>options.<span style="color: black;">logfile</span>, <span style="color: #483d8b;">'w'</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">;</span>
&nbsp;
&nbsp;
loadedmods=popen<span style="color: black;">&#40;</span><span style="color: #483d8b;">'lsmod | tail -n+2'</span><span style="color: black;">&#41;</span>.<span style="color: black;">readlines</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">;</span>
getmodname=<span style="color: #dc143c;">re</span>.<span style="color: #008000;">compile</span><span style="color: black;">&#40;</span>r<span style="color: #483d8b;">&quot;^(?P&lt;modname&gt;<span style="color: #000099; font-weight: bold;">\w</span>*)&quot;</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">#prob. need to replace kernel with sth. like (kernel|ubuntu) for an ubuntu kernel source</span>
parsepath=<span style="color: #dc143c;">re</span>.<span style="color: #008000;">compile</span><span style="color: black;">&#40;</span>r<span style="color: #483d8b;">&quot;/kernel(?P&lt;path&gt;/.*/)(?P&lt;file&gt;.*).ko&quot;</span><span style="color: black;">&#41;</span>
&nbsp;
wantin=<span style="color: black;">&#91;</span><span style="color: black;">&#93;</span><span style="color: #66cc66;">;</span>
<span style="color: #ff7700;font-weight:bold;">for</span> module <span style="color: #ff7700;font-weight:bold;">in</span> loadedmods:
	modname = <span style="color: #dc143c;">re</span>.<span style="color: black;">search</span><span style="color: black;">&#40;</span>getmodname,module<span style="color: black;">&#41;</span>.<span style="color: black;">group</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'modname'</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">;</span>
	moduleprops=<span style="color: #dc143c;">re</span>.<span style="color: black;">search</span><span style="color: black;">&#40;</span>parsepath,popen<span style="color: black;">&#40;</span><span style="color: #483d8b;">'modinfo -n '</span> + modname<span style="color: black;">&#41;</span>.<span style="color: black;">read</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">;</span>
	<span style="color: #ff7700;font-weight:bold;">if</span><span style="color: black;">&#40;</span>moduleprops<span style="color: black;">&#41;</span>:
		<span style="color: #808080; font-style: italic;">#search the makefile for the module name</span>
		<span style="color: #ff7700;font-weight:bold;">try</span>:
			f=<span style="color: #008000;">open</span><span style="color: black;">&#40;</span>options.<span style="color: black;">sourcedir</span> + moduleprops.<span style="color: black;">group</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'path'</span><span style="color: black;">&#41;</span> + <span style="color: #483d8b;">'Makefile'</span> , <span style="color: #483d8b;">'r'</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">;</span>
		<span style="color: #ff7700;font-weight:bold;">except</span> <span style="color: #008000;">IOError</span>:
			stderr.<span style="color: black;">write</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'Could not find Makefile for '</span> + modname + <span style="color: #483d8b;">'<span style="color: #000099; font-weight: bold;">\n</span>'</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">;</span>
			<span style="color: #ff7700;font-weight:bold;">continue</span>
		cont=f.<span style="color: black;">read</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">;</span>
		f.<span style="color: black;">close</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">;</span>
		m=<span style="color: #dc143c;">re</span>.<span style="color: black;">search</span><span style="color: black;">&#40;</span>r<span style="color: #483d8b;">&quot;obj-<span style="color: #000099; font-weight: bold;">\$</span><span style="color: #000099; font-weight: bold;">\(</span>(?P&lt;cfgname&gt;[A-Z0-9_]*)<span style="color: #000099; font-weight: bold;">\)</span><span style="color: #000099; font-weight: bold;">\W</span>*<span style="color: #000099; font-weight: bold;">\+</span>=<span style="color: #000099; font-weight: bold;">\W</span>*&quot;</span>+moduleprops.<span style="color: black;">group</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'file'</span><span style="color: black;">&#41;</span>+r<span style="color: #483d8b;">&quot;<span style="color: #000099; font-weight: bold;">\.</span>o&quot;</span>,cont<span style="color: black;">&#41;</span><span style="color: #66cc66;">;</span>
		<span style="color: #ff7700;font-weight:bold;">if</span><span style="color: black;">&#40;</span>m<span style="color: black;">&#41;</span>:wantin.<span style="color: black;">append</span><span style="color: black;">&#40;</span>m.<span style="color: black;">group</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'cfgname'</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">;</span>
		<span style="color: #ff7700;font-weight:bold;">else</span>:stderr.<span style="color: black;">write</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'Could not determine config name for '</span> + modname + <span style="color: #483d8b;">'<span style="color: #000099; font-weight: bold;">\n</span>'</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">;</span>
	<span style="color: #ff7700;font-weight:bold;">else</span>:
		stderr.<span style="color: black;">write</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'Could not parse modinfo for '</span> + modname + <span style="color: #483d8b;">'<span style="color: #000099; font-weight: bold;">\n</span>'</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">;</span>
&nbsp;
&nbsp;
&nbsp;
<span style="color: #ff7700;font-weight:bold;">if</span><span style="color: black;">&#40;</span>options.<span style="color: black;">cfgfile</span> <span style="color: #66cc66;">!</span>= <span style="color: #483d8b;">'-'</span><span style="color: black;">&#41;</span>: 
	f=<span style="color: #008000;">open</span><span style="color: black;">&#40;</span>options.<span style="color: black;">cfgfile</span>, <span style="color: #483d8b;">'r'</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">;</span>
	lines=f.<span style="color: black;">readlines</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">;</span>
	f.<span style="color: black;">close</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">;</span>
<span style="color: #ff7700;font-weight:bold;">else</span>:
	lines=stdin.<span style="color: black;">readlines</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">;</span>
&nbsp;
confparse = <span style="color: #dc143c;">re</span>.<span style="color: #008000;">compile</span><span style="color: black;">&#40;</span>r<span style="color: #483d8b;">&quot;<span style="color: #000099; font-weight: bold;">\W</span>*(?P&lt;iscomment&gt;#?)<span style="color: #000099; font-weight: bold;">\W</span>*(?P&lt;cfgname&gt;CONFIG_[A-Z0-9_]*)<span style="color: #000099; font-weight: bold;">\W</span>*=?<span style="color: #000099; font-weight: bold;">\W</span>*(?P&lt;answer&gt;[nmy]?)&quot;</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">for</span> line <span style="color: #ff7700;font-weight:bold;">in</span> lines:
	matches = <span style="color: #dc143c;">re</span>.<span style="color: black;">search</span><span style="color: black;">&#40;</span>confparse,line<span style="color: black;">&#41;</span><span style="color: #66cc66;">;</span>
	<span style="color: #ff7700;font-weight:bold;">if</span><span style="color: black;">&#40;</span>matches <span style="color: #ff7700;font-weight:bold;">and</span> matches.<span style="color: black;">group</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'cfgname'</span><span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">in</span> wantin<span style="color: black;">&#41;</span>: 
		of.<span style="color: black;">write</span><span style="color: black;">&#40;</span>matches.<span style="color: black;">group</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'cfgname'</span><span style="color: black;">&#41;</span>+<span style="color: #483d8b;">'=y<span style="color: #000099; font-weight: bold;">\n</span>'</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">;</span>
	<span style="color: #ff7700;font-weight:bold;">else</span>: 
		<span style="color: #808080; font-style: italic;">#if(matches and matches.group('answer') == 'm'):of.write(matches.group('cfgname')+'=n\n');</span>
		of.<span style="color: black;">write</span><span style="color: black;">&#40;</span>line<span style="color: black;">&#41;</span><span style="color: #66cc66;">;</span></pre></td></tr></table></div>

<hr /><a href='http://andreas.goelzer.de/download/customconfig.py'><img src="http://andreas.goelzer.de/wp-content/plugins/kfile/fileicons/file-source-alt.png" width="16" height="16" alt="filetype" class="icon16" /> customconfig.py</a> (3.41 kiB, 2009-05-19)<br/><br />
<hr />To use the script, call it for example like <code>./customconfig.py -i /boot/config-2.6.24-21-generic -o .config</code> if it is located in a linux kernel directory. Then modify the resulting config to suit your needs with <code>make menuconfig</code>, and then you can proceed to compile your kernel.</p>
]]></content:encoded>
			<wfw:commentRss>http://andreas.goelzer.de/kernel-config-based-on-lsmod-output/feed</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Feeding a screensaver from the digikam database</title>
		<link>http://andreas.goelzer.de/feeding-a-screensaver-from-the-digikam-database</link>
		<comments>http://andreas.goelzer.de/feeding-a-screensaver-from-the-digikam-database#comments</comments>
		<pubDate>Mon, 02 Jun 2008 20:17:09 +0000</pubDate>
		<dc:creator>Andreas Goelzer</dc:creator>
				<category><![CDATA[Computers]]></category>
		<category><![CDATA[Photography]]></category>
		<category><![CDATA[digikam]]></category>
		<category><![CDATA[Linux]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[screensaver]]></category>

		<guid isPermaLink="false">http://andreas.goelzer.de/?p=12</guid>
		<description><![CDATA[After several months of living without any screensaver, I decided it just would be nice to once again have something nice to watch and show while idling. I really like the photo slideshow that ships with windows vista, but as &#8230; <a href="http://andreas.goelzer.de/feeding-a-screensaver-from-the-digikam-database">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>After several months of living without any screensaver, I decided it just would be nice to once again have something nice to watch and show while idling. I really like the photo slideshow that ships with windows vista, but as my laptop runs linux only that was not possible. The next best thing i found was glslideshow from the <a href="http://www.jwz.org/xscreensaver/">xscreensaver compilation</a>. Like most slideshow programs, glslideshow will look into a given directory, and show files out of it in random order. However, there are downsides to that simple approach: My photo folder contains some private pictures, and also some pictures that are just ugly. So i decided to connect it to my <a href="http://www.digicam.org">digikam</a> database, which was surprisingly simple.</p>
<p>By default, xscreensaver takes the directory out of its configfile ~/.xscreensaver, and passes it to a helper script xscreensaver-getimage-file, which then prints the complete path to a random file in that directory. So all one needs to do to add another source for pictures is to replace that file, there is already <a href="http://jrwren.wrenfam.com/blog/2005/02/20/flickr-xscreensaver-fun/">xscreensaver-getimage-flickr<br />
</a>, which pulls photos off flickr. So I wrote a similar script in python to print a random file from my digikam database. Maybe there are more?</p>
<h4>Installation</h4>
<p>The script is written in python, so that has to be installed (which is usually the case), it also requires sqlite and python bindings for it (apt-get install python-pysqlite2 in debian and ubuntu). If those are installed, backup the original xscreensaver-getimage-file, and copy the script there. Check that it is executable.<br />
the overall process would look like(assuming xscreensaver-glslideshow already works)</p>
<pre>
sudo apt-get install python-pysqlite2
cd ~
wget http://andreas.goelzer.de/download/xscreensaver-getimage-digikam.py
chmod +x xscreensaver-getimage-digikam.py
cd /usr/bin
sudo mv xscreensaver-getimage-file xscreensaver-getimage-file-original
sudo mv ~/xscreensaver-getimage-digikam.py /usr/bin/
ln -s xscreensaver-getimage-digikam.py xscreensaver-getimage-file
</pre>
<p>If you had previously pointed xscreensaver to your digikam photo folder, that should work now. If not, edit ~/.xscreensaver and add a line pointing to that directory, in my case:</p>
<pre>
imageDirectory: /home/goelzera/Documents/Bilder
</pre>
</p>
<h4>Modification</h4>
<p>Right now this is all a bit hacky. at the beginning of the file, there is a line</p>
<pre>bad_tags = "('private','me','Nophoto','Notmeanttobenice')"</pre>
<p>If called, the script will take a random picture out of the database, then check if it has one of those tags. If it is tagged with one of those, it will be dismissed, similarly, if it has an extension indicating xscreensaver won&#8217;t be able to display it. So, set those line to the tags you do not want in your slideshow. For everything else you need to change the sql queries or the source code.</p>
<h4>Running</h4>
<p>Set your screensaver to glslideshow and wait for it to start</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>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
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">#!/usr/bin/python</span>
<span style="color: #808080; font-style: italic;"># -*- coding: utf-8 -*-</span>
<span style="color: #483d8b;">&quot;&quot;&quot;
Script to choose random pictures from the digikam database
Copyright (C) 2008  Andreas Goelzer
&nbsp;
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
&nbsp;
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
&nbsp;
You should have received a copy of the GNU General Public License
along with this program.  If not, see &lt;http://www.gnu.org/licenses/&gt;.
&nbsp;
&nbsp;
This program takes one random picture out of a sqlite-database
made by digikam.
This is useful for screensavers, xscreensaver-glslideshow in particular.
&quot;&quot;&quot;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">#import sys;</span>
<span style="color: #808080; font-style: italic;">#ef=open('/home/goelzera/Documents/Programme/python/xscreensaver-errlog','a')</span>
<span style="color: #808080; font-style: italic;">#sys.stderr = ef</span>
&nbsp;
good_extensions = <span style="color: black;">&#40;</span><span style="color: #483d8b;">'jpg'</span>, <span style="color: #483d8b;">'jpeg'</span>, <span style="color: #483d8b;">'pjpeg'</span>, <span style="color: #483d8b;">'pjpg'</span>, <span style="color: #483d8b;">'png'</span>, <span style="color: #483d8b;">'gif'</span>, <span style="color: #483d8b;">'tif'</span>, <span style="color: #483d8b;">'tiff'</span>, <span style="color: #483d8b;">'xbm'</span>, <span style="color: #483d8b;">'xpm'</span><span style="color: black;">&#41;</span>
bad_tags = <span style="color: #483d8b;">&quot;('private','me','Nophoto','Notmeanttobenice')&quot;</span>
<span style="color: #808080; font-style: italic;">#good_tags = &quot;('public')&quot;</span>
good_tags = <span style="color: #483d8b;">&quot;&quot;</span>
attempts = <span style="color: #ff4500;">200</span>
&nbsp;
&nbsp;
<span style="color: #ff7700;font-weight:bold;">import</span> sqlite3<span style="color: #66cc66;">;</span>
<span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">sys</span> <span style="color: #ff7700;font-weight:bold;">import</span> argv, exit<span style="color: #66cc66;">;</span>
<span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">os</span>.<span style="color: black;">path</span> <span style="color: #ff7700;font-weight:bold;">import</span> splitext<span style="color: #66cc66;">;</span>
&nbsp;
digikamversion = <span style="color: #483d8b;">&quot;4&quot;</span><span style="color: #66cc66;">;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">if</span><span style="color: black;">&#40;</span><span style="color: #008000;">len</span><span style="color: black;">&#40;</span>argv<span style="color: black;">&#41;</span> <span style="color: #66cc66;">&gt;</span> <span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span>:
	dirname = argv<span style="color: black;">&#91;</span><span style="color: #008000;">len</span><span style="color: black;">&#40;</span>argv<span style="color: black;">&#41;</span> - <span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span>
<span style="color: #ff7700;font-weight:bold;">else</span>:
	dirname = <span style="color: #483d8b;">&quot;/home/goelzera/Documents/Pictures&quot;</span>
&nbsp;
con = sqlite3.<span style="color: black;">connect</span><span style="color: black;">&#40;</span>dirname + <span style="color: #483d8b;">&quot;/digikam&quot;</span> + digikamversion + <span style="color: #483d8b;">&quot;.db&quot;</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">if</span><span style="color: black;">&#40;</span>digikamversion == <span style="color: #483d8b;">&quot;4&quot;</span><span style="color: black;">&#41;</span>:
	url = <span style="color: #483d8b;">&quot;relativePath&quot;</span>
	dirid = <span style="color: #483d8b;">&quot;album&quot;</span>
<span style="color: #ff7700;font-weight:bold;">else</span>:
	url = <span style="color: #483d8b;">&quot;url&quot;</span>
	dirid = <span style="color: #483d8b;">&quot;dirid&quot;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">#from time import time</span>
<span style="color: #808080; font-style: italic;">#f=open('/home/goelzera/Documents/Programme/python/xscreensaver-log','a')</span>
<span style="color: #808080; font-style: italic;">#f.write(str(time()) + '\t' + str(argv) + '\t')</span>
&nbsp;
<span style="color: #808080; font-style: italic;">#get tagids for bad tags</span>
<span style="color: #ff7700;font-weight:bold;">def</span> getTagIdsFromNames<span style="color: black;">&#40;</span>names<span style="color: black;">&#41;</span>:
	tag_ids = <span style="color: #483d8b;">' '</span><span style="color: #66cc66;">;</span>
	<span style="color: #ff7700;font-weight:bold;">for</span> row <span style="color: #ff7700;font-weight:bold;">in</span> con.<span style="color: black;">execute</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;SELECT id FROM Tags WHERE name IN &quot;</span>+ names<span style="color: black;">&#41;</span>:
		tag_ids += <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>row<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span> + <span style="color: #483d8b;">','</span>
	tag_ids = <span style="color: #483d8b;">'('</span> + tag_ids<span style="color: black;">&#91;</span>:-<span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span> + <span style="color: #483d8b;">')'</span>
	<span style="color: #ff7700;font-weight:bold;">return</span> tag_ids
&nbsp;
<span style="color: #ff7700;font-weight:bold;">if</span><span style="color: black;">&#40;</span>bad_tags <span style="color: #66cc66;">!</span>= <span style="color: #483d8b;">&quot;&quot;</span><span style="color: black;">&#41;</span>: bad_tag_ids = getTagIdsFromNames<span style="color: black;">&#40;</span>bad_tags<span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">if</span><span style="color: black;">&#40;</span>good_tags <span style="color: #66cc66;">!</span>= <span style="color: #483d8b;">&quot;&quot;</span><span style="color: black;">&#41;</span>: good_tag_ids = getTagIdsFromNames<span style="color: black;">&#40;</span>good_tags<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #008000;">file</span> = <span style="color: #483d8b;">'none'</span><span style="color: #66cc66;">;</span>
<span style="color: #808080; font-style: italic;">#search for images, take a random one out of the db and see if it fulfills the criteria</span>
<span style="color: #ff7700;font-weight:bold;">for</span> attempt <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span>attempts<span style="color: black;">&#41;</span>:
	<span style="color: #808080; font-style: italic;">#get a random picture, i guess this is the wrong way, performs horrible</span>
	<span style="color: #808080; font-style: italic;">#truely random, but painfully slow</span>
	<span style="color: #808080; font-style: italic;">#row = con.execute(&quot;SELECT id, name, $dirid FROM Images ORDER BY RANDOM() LIMIT 0,1&quot;).fetchone()</span>
	<span style="color: #808080; font-style: italic;">#fast, but not as random</span>
	row = con.<span style="color: black;">execute</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;SELECT id, name, &quot;</span>+ dirid+<span style="color: #483d8b;">&quot; FROM Images WHERE id &gt;= (abs(RANDOM()) % (SELECT max(id) FROM Images)) LIMIT 0,1&quot;</span><span style="color: black;">&#41;</span>.<span style="color: black;">fetchone</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
	<span style="color: #808080; font-style: italic;">#f.write(str(row[0]) + ',')</span>
&nbsp;
	<span style="color: #808080; font-style: italic;">#check for bad tag</span>
	<span style="color: #ff7700;font-weight:bold;">if</span><span style="color: black;">&#40;</span>bad_tags <span style="color: #66cc66;">!</span>= <span style="color: #483d8b;">&quot;&quot;</span><span style="color: black;">&#41;</span>:
		<span style="color: #ff7700;font-weight:bold;">if</span><span style="color: black;">&#40;</span>con.<span style="color: black;">execute</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;SELECT 1 FROM ImageTags WHERE imageid = &quot;</span> + <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>row<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span> + <span style="color: #483d8b;">&quot; AND tagid IN &quot;</span>+ bad_tag_ids<span style="color: black;">&#41;</span>.<span style="color: black;">fetchone</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>: <span style="color: #ff7700;font-weight:bold;">continue</span>
&nbsp;
	<span style="color: #808080; font-style: italic;">#check for good tag</span>
	<span style="color: #ff7700;font-weight:bold;">if</span><span style="color: black;">&#40;</span>good_tags <span style="color: #66cc66;">!</span>= <span style="color: #483d8b;">&quot;&quot;</span><span style="color: black;">&#41;</span>:
		<span style="color: #ff7700;font-weight:bold;">if</span><span style="color: black;">&#40;</span><span style="color: #ff7700;font-weight:bold;">not</span> con.<span style="color: black;">execute</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;SELECT 1 FROM ImageTags WHERE imageid = &quot;</span> + <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>row<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span> + <span style="color: #483d8b;">&quot; AND tagid IN &quot;</span>+ good_tag_ids<span style="color: black;">&#41;</span>.<span style="color: black;">fetchone</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>: <span style="color: #ff7700;font-weight:bold;">continue</span>
&nbsp;
&nbsp;
	<span style="color: #808080; font-style: italic;">#check for invalid extension</span>
	ext = splitext<span style="color: black;">&#40;</span>row<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
	ext = ext<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span>:<span style="color: black;">&#93;</span>.<span style="color: black;">lower</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
	<span style="color: #ff7700;font-weight:bold;">if</span><span style="color: black;">&#40;</span><span style="color: #ff7700;font-weight:bold;">not</span> ext <span style="color: #ff7700;font-weight:bold;">in</span> good_extensions<span style="color: black;">&#41;</span>: <span style="color: #ff7700;font-weight:bold;">continue</span>
&nbsp;
	<span style="color: #808080; font-style: italic;">#not rejected, get directory</span>
	drow = con.<span style="color: black;">execute</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;SELECT &quot;</span>+url+<span style="color: #483d8b;">&quot; FROM Albums WHERE id = &quot;</span> + <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>row<span style="color: black;">&#91;</span><span style="color: #ff4500;">2</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>.<span style="color: black;">fetchone</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
	<span style="color: #008000;">file</span> = dirname + drow<span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span> + <span style="color: #483d8b;">'/'</span> + row<span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span>
	<span style="color: #ff7700;font-weight:bold;">break</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">print</span> <span style="color: #008000;">file</span>.<span style="color: black;">encode</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'utf-8'</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #808080; font-style: italic;">#f.write(file.encode('latin-1') + '\n')</span></pre></td></tr></table></div>

<hr /><a href='http://andreas.goelzer.de/download/xscreensaver-getimage-digikam.py'><img src="http://andreas.goelzer.de/wp-content/plugins/kfile/fileicons/file-source-alt.png" width="16" height="16" alt="filetype" class="icon16" /> xscreensaver-getimage-digikam.py</a> (3.29 kiB, 2009-08-06)<br/><br />
<hr />
]]></content:encoded>
			<wfw:commentRss>http://andreas.goelzer.de/feeding-a-screensaver-from-the-digikam-database/feed</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>

<!-- Performance optimized by W3 Total Cache. Learn more: http://www.w3-edge.com/wordpress-plugins/

Minified using disk
Page Caching using disk (enhanced)

Served from: andreas.goelzer.de @ 2010-09-06 09:08:36 -->