<?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>Ceres Logic :: Blog &#187; gconf</title>
	<atom:link href="http://www.cereslogic.com/pages/tag/gconf/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.cereslogic.com/pages</link>
	<description>Mobile and Web App Development</description>
	<lastBuildDate>Tue, 23 Aug 2011 14:45:28 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Python and GConf</title>
		<link>http://www.cereslogic.com/pages/2007/11/16/python-and-gconf/</link>
		<comments>http://www.cereslogic.com/pages/2007/11/16/python-and-gconf/#comments</comments>
		<pubDate>Fri, 16 Nov 2007 17:15:00 +0000</pubDate>
		<dc:creator>mdesjardins</dc:creator>
				<category><![CDATA[blog]]></category>
		<category><![CDATA[awn]]></category>
		<category><![CDATA[gconf]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://mikedesjardins.us/wordpress/2007/11/python-and-gconf/</guid>
		<description><![CDATA[The weather applet and clock/calendar applet for the Avant Window Navigator both use GConf to store their configuration settings. GConf is part of the GNOME environment on Linux. It maintains a hierarchical set of configuration data in (key,value) pairs, much like the registry on Windows or the &#8220;plist files&#8221; on OSX. One of the nice [...]]]></description>
			<content:encoded><![CDATA[<p>The <a href="http://wiki.awn-project.org/index.php?title=Weather_Applet">weather applet</a> and <a href="http://wiki.awn-project.org/index.php?title=Clock/Calendar_Applet">clock/calendar applet</a> for the <a href="http://www.awn-project.org/">Avant Window Navigator</a> both use <a href="http://www.gnome.org/projects/gconf/">GConf</a> to store their configuration settings.  GConf is part of the GNOME environment on Linux.  It maintains a hierarchical set of configuration data in (key,value) pairs, much like the registry on Windows or the &#8220;plist files&#8221; on OSX.  One of the nice things about GConf is that you can register your application to receive notifications in a callback function whenever any interesting configuration values change.</p>
<p>I noticed that there weren&#8217;t a lot of tutorials out there on the topic of mixing Python with GConf, so I decided to write one.</p>
<p>First, you&#8217;ll need to import the Python bindings for GConf:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> gconf</pre></div></div>

<p>Next, you want to create a GConf &#8220;client&#8221; in your Python app.  You&#8217;ll probably want to do this in an __init__() function somewhere.  You&#8217;ll also want to register your application to receive notifications when ever your configuration values change.  The hierarchy of configuration values in GConf follows the familiar slash-separated path notation.  For example, the configuration values for my AWN weather applet are stored in /apps/avant-window-navigator/applets/weather.  So, if I want to register a callback named config_event to be called whenever configuration values on that path are modified, I&#8217;d do the following:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #008000;">self</span>.<span style="color: black;">gconf_client</span> = gconf.<span style="color: black;">client_get_default</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
<span style="color: #008000;">self</span>.<span style="color: black;">gconf_client</span>.<span style="color: black;">notify_add</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;/apps/avant-window-navigator/applets/weather&quot;</span>, <span style="color: #008000;">self</span>.<span style="color: black;">config_event</span><span style="color: black;">&#41;</span></pre></div></div>

<p><span style="font-size:100%;">I usually write a generic &#8220;get_config&#8221; function, and have the callback call get_config.  That way, I can use the same configuration code when I initialize.  The config callback then looks simple&#8230; something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">def</span> config_event<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, gconf_client, <span style="color: #66cc66;">*</span>args, <span style="color: #66cc66;">**</span>kwargs<span style="color: black;">&#41;</span>:
  <span style="color: #008000;">self</span>.<span style="color: black;">get_config</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></div></div>

<p><span style="font-size:100%;">The kwargs parameter gives you a list of parameters that changed.  You can fine-tune your configuration code based on this, but I usually just ignore it and re-read everything because I don&#8217;t usually have very many parameters.</p>
<p>GConf provides functions for reading your parameters.  They look like this:</span></p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">foo = <span style="color: #008000;">self</span>.<span style="color: black;">gconf_client</span>.<span style="color: black;">get_string</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;/path/to/my/config/data/foo&quot;</span><span style="color: black;">&#41;</span>
bar = <span style="color: #008000;">self</span>.<span style="color: black;">gconf_client</span>.<span style="color: black;">get_int</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;/path/to/my/config/data/bar&quot;</span><span style="color: black;">&#41;</span>
baz = <span style="color: #008000;">self</span>.<span style="color: black;">gconf_client</span>.<span style="color: black;">get_bool</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;/path/to/my/config/data/baz&quot;</span><span style="color: black;">&#41;</span></pre></div></div>

<p><span style="font-size:100%;">All of the functions except </span><span style=";font-family:courier new;font-size:100%;"  >get_bool</span>return None if the key isn&#8217;t found.  Oddly, <span style=";font-family:courier new;font-size:100%;"  >get_bool</span> seems to return False if the key isn&#8217;t found.  In my configuration code, I like to initialize my GConf values when the key isn&#8217;t found.  So when if my code were to read the &#8220;foo&#8221; parameter like the above example, it&#8217;d actually be coded something like this:</span></p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">foo = <span style="color: #008000;">self</span>.<span style="color: black;">gconf_client</span>.<span style="color: black;">get_string</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;/path/to/my/config/foo&quot;</span><span style="color: black;">&#41;</span>
<span style="color: #ff7700;font-weight:bold;">if</span> foo == <span style="color: #008000;">None</span>:
  <span style="color: #008000;">self</span>.<span style="color: black;">gconf_client</span>.<span style="color: black;">set_string</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;/path/to/my/config/foo&quot;</span>, <span style="color: #483d8b;">&quot;Default Value&quot;</span><span style="color: black;">&#41;</span>
  foo = <span style="color: #483d8b;">&quot;Default Value&quot;</span></pre></div></div>

<p><span style="font-size:100%;"><br />And I usually wrap the above idiom in its own function that accepts a key name and a default value.<br /></span><br /><a onblur="try {parent.deselectBloggerImageGracefully();} catch(e) {}" href="http://www.dragonflymarsh.com/blog/uploaded_images/Screenshot-Configuration-Editor---weather-703851.png"><img style="margin: 0pt 0pt 10px 10px; float: right; cursor: pointer; width: 264px; height: 210px;" src="http://mikedesjardins.us/blog/uploaded_images/Screenshot-Configuration-Editor---weather-703844.png" alt="" border="0" /></a><br /><span style="font-size:100%;">Note that you can edit and interact with your GConf settings in realtime using </span><span style="font-size:100%;">the GNOME configuration tool.  If you use Ubuntu, then this utility may be found under the &#8220;System Tools&#8221; menu.  Editing configuration values in the configuration tool will result in your callback being executed as you might expect.</p>
<p>This also makes creating a configuration dialog easy.  The configuration dialog just needs to write its updated values to gconf when the user clicks Apply or OK.  If you&#8217;ve created a callback and generic configuration function, then the application will automatically reconfigure itself after the user applies their modifications!</span></p>
]]></content:encoded>
			<wfw:commentRss>http://www.cereslogic.com/pages/2007/11/16/python-and-gconf/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

