<?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>/dev/stu &#187; Uncategorized</title>
	<atom:link href="http://www.stuartaxon.com/category/uncategorized/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.stuartaxon.com</link>
	<description>Adding LOC to the web.</description>
	<lastBuildDate>Mon, 09 Jan 2012 18:11:32 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.1</generator>
		<item>
		<title>Cairo with python Ctypes</title>
		<link>http://www.stuartaxon.com/2012/01/09/cairo-with-python-ctypes/</link>
		<comments>http://www.stuartaxon.com/2012/01/09/cairo-with-python-ctypes/#comments</comments>
		<pubDate>Mon, 09 Jan 2012 18:11:32 +0000</pubDate>
		<dc:creator>stu</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.stuartaxon.com/?p=299</guid>
		<description><![CDATA[Uploaded some experiments with python and ctypes to here: https://gitorious.org/pycairo-ctypes/pycairo-ctypes &#160; This is a really rough proof of concept that the pycairo API can be implemented with ctypes + metclasses. &#160; &#160; So far only ImageSurface is supported on the SVG backend, along with Contexts.   The nice thing about this is that you can use [...]]]></description>
			<content:encoded><![CDATA[<p>Uploaded some experiments with python and ctypes to here:</p>
<p><a href="https://gitorious.org/pycairo-ctypes/pycairo-ctypes">https://gitorious.org/pycairo-ctypes/pycairo-ctypes</a></p>
<p>&nbsp;</p>
<p>This is a really rough proof of concept that the pycairo API can be implemented with ctypes + metclasses.</p>
<p>&nbsp;</p>
<p>&nbsp;</p>
<p>So far only ImageSurface is supported on the SVG backend, along with Contexts.   The nice thing about this is that you can use a pycairo like API on pypy, where things should be faster (in theory).</p>
<p>&nbsp;</p>
<p>There&#8217;s a test that can be run to show the outputs the same for pycairo and cairo ctypes.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.stuartaxon.com/2012/01/09/cairo-with-python-ctypes/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using PyCha with Django</title>
		<link>http://www.stuartaxon.com/2011/02/25/using-pycha-with-django/</link>
		<comments>http://www.stuartaxon.com/2011/02/25/using-pycha-with-django/#comments</comments>
		<pubDate>Fri, 25 Feb 2011 00:10:50 +0000</pubDate>
		<dc:creator>stu</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[cairo]]></category>
		<category><![CDATA[charting]]></category>
		<category><![CDATA[charts]]></category>
		<category><![CDATA[django]]></category>
		<category><![CDATA[pycha]]></category>

		<guid isPermaLink="false">http://www.stuartaxon.com/?p=290</guid>
		<description><![CDATA[A quick post on using Python Charts to generate nice SVG charts for your django website (I&#8217;ve had the code hanging around for ages &#8211; so should just post it). The code is based on the examples there, here I integrate it into Django. To install you&#8217;ll need to do pip install pycha Heres the [...]]]></description>
			<content:encoded><![CDATA[<p>A quick post on using <a href="http://bitbucket.org/lgs/pycha/">Python Charts</a> to generate nice SVG charts for your django website (I&#8217;ve had the code hanging around for ages &#8211; so should just post it).  The code is based on the examples there, here I integrate it into Django.</p>
<p>To install you&#8217;ll need to do</p>
<pre>pip install pycha</pre>
<p>Heres the code for a simple view to output a chart directly to SVG:</p>
<p><a href="http://www.stuartaxon.com/wp-content/uploads/2011/02/linechart.png"><img class="alignnone size-full wp-image-291" title="linechart" src="http://www.stuartaxon.com/wp-content/uploads/2011/02/linechart.png" alt="" width="401" height="316" /></a></p>
<pre class="brush:python"># views.py

from StringIO import StringIO
from django.http import HttpResponse

import cairo

def colors(request):
    in_req = 1

    svg_buffer = StringIO()

    width, height = (500, 400)
    surface = cairo.SVGSurface(svg_buffer, width, height)

    dataSet = (
     ('dataSet 1', ((0, 1), (1, 3), (2, 2.5))),
     ('dataSet 2', ((0, 2), (1, 4), (2, 3))),
     ('dataSet 3', ((0, 5), (1, 1), (2, 0.5))),
    )

    options = {
       'legend': {'hide': True},
       'background': {'color': '#f0f0f0'},
    }

    #import pycha.bar
    #chart = pycha.bar.VerticalBarChart(surface, options)

    import pycha.line
    chart = pycha.line.LineChart(surface, options)
    chart.addDataset(dataSet)
    chart.render()

    del chart
    del surface

    response = ''
    response = HttpResponse(mimetype='image/svg+xml')
    svg_buffer.seek(0)
    response.write( svg_buffer.read() )
    return response
</pre>
<p>The basic idea is that &#8211; instead of the chart outputting to an svg file, the output goes to a buffer, this is triggered by calling the destructors of the chart and it&#8217;s cairo surface.  Once the data is in the buffer, it is rewound and played back to the Http Response.</p>
<p>&nbsp;</p>
<h2>Inline SVG</h2>
<p>As is, this won&#8217;t work as inline svg, because outputting the XML preamble in the middle of the page will cause problems.  Below is an example that let&#8217;s you decide if you need the preamble (full SVG output), or not (SVG fragment for inclusion in a page or another SVG):</p>
<pre class="brush:python"># Create your views here.

from StringIO import StringIO
from django.http import HttpResponse
from django.shortcuts import render_to_response

import cairo

XML_PREAMBLE = '&lt;?xml version="1.0" encoding="UTF-8"?&gt;'

def colors_chart(inline = False):
    """
    Generate colours chart

    Set inline to True to disable the XML preamble
    """
    in_req = 1

    svg_buffer = StringIO()

    width, height = (500, 400)
    surface = cairo.SVGSurface(svg_buffer, width, height)

    dataSet = (
     ('dataSet 1', ((0, 1), (1, 3), (2, 2.5))),
     ('dataSet 2', ((0, 2), (1, 4), (2, 3))),
     ('dataSet 3', ((0, 5), (1, 1), (2, 0.5))),
    )

    options = {
       'legend': {'hide': True},
       'background': {'color': '#f0f0f0'},
    }

    import pycha.bar
    chart = pycha.bar.VerticalBarChart(surface, options)

    #import pycha.line
    #chart = pycha.line.LineChart(surface, options)
    chart.addDataset(dataSet)
    chart.render()

    del chart
    del surface

    response = ''

    if inline:
        svg_buffer.seek(len(XML_PREAMBLE))
    else:
        svg_buffer.seek(0)
    return svg_buffer.read()

def colors_svg(request):
    """ render a pure SVG chart """
    response = HttpResponse(mimetype='image/svg+xml')
    response.write(colors_chart(inline = False))
    return response

def index(request):
    """ render a chart into the template """
    chart_svg = colors_chart(inline = True)

    return render_to_response(
        'shapes_index.html',
        { "chart" : chart_svg },
        mimetype='application/xhtml+xml')
</pre>
<p>An example project with the above code is available here:<br />
<a href="http://www.stuartaxon.com/wp-content/uploads/2011/02/pycha_django.zip">pycha_django</a></p>
<p>&nbsp;</p>
<p>[EDIT 25/2/2011] Fixed PyCha URL</p>
]]></content:encoded>
			<wfw:commentRss>http://www.stuartaxon.com/2011/02/25/using-pycha-with-django/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title></title>
		<link>http://www.stuartaxon.com/2010/09/30/281/</link>
		<comments>http://www.stuartaxon.com/2010/09/30/281/#comments</comments>
		<pubDate>Thu, 30 Sep 2010 23:59:21 +0000</pubDate>
		<dc:creator>stu</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.stuartaxon.com/?p=281</guid>
		<description><![CDATA[[EDIT: 2010/10/05] Oops, I&#8217;ve been using TCC/LE by JPSoft as my shell&#8230; the following doesn&#8217;t work in the normal command prompt [/EDIT] I just had a look at SET and it lets you set user/system variables: Display, create, modify, or delete environment variables. SET [/A /D /E /P /R file... /S /U /V /X] [name[=][value [...]]]></description>
			<content:encoded><![CDATA[<p>[EDIT: 2010/10/05]</p>
<p>Oops, I&#8217;ve been using TCC/LE by JPSoft as my shell&#8230; the following doesn&#8217;t work in the normal command prompt</p>
<p>[/EDIT]</p>
<p>I just had a look at SET and it lets you set user/system variables:</p>
<pre>Display, create, modify, or delete environment variables.

<span style="color: #404040;"><span style="color: #000000;">SET</span> [/A /D /E /P /R file... /S /U /V /X] [name[=][value ]]
        file:  One or more files containing variable definitions
        /A(rithmetic)           <span style="color: #000000;">/S(ystem variables)</span>
        /D(efault variables)    <span style="color: #000000;">/U(ser variables)
</span>        /E(nv vars)             /V(olatile variables)
        /R(ead from file)       /X override VariableExclude
        /P(ause)</span></pre>
<p>So to set save a variables MYAPP_HOME as the current directory, just do</p>
<pre>SET /U MYAPP_HOME=%CD</pre>
<p>Wish I&#8217;d have looked that up a few years ago, it&#8217;ll save a few seconds next time I need to setup some dev sdk <img src='http://www.stuartaxon.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.stuartaxon.com/2010/09/30/281/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Access files in a Linux virtual machine from Windows</title>
		<link>http://www.stuartaxon.com/2010/01/21/access-files-in-a-linux-virtual-machine-from-windows/</link>
		<comments>http://www.stuartaxon.com/2010/01/21/access-files-in-a-linux-virtual-machine-from-windows/#comments</comments>
		<pubDate>Thu, 21 Jan 2010 04:46:17 +0000</pubDate>
		<dc:creator>stu</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[ext3]]></category>
		<category><![CDATA[filesystem]]></category>
		<category><![CDATA[interoperability]]></category>
		<category><![CDATA[linux]]></category>
		<category><![CDATA[virtual-machine]]></category>
		<category><![CDATA[vm]]></category>
		<category><![CDATA[vmware]]></category>
		<category><![CDATA[windows]]></category>
		<category><![CDATA[workflow]]></category>

		<guid isPermaLink="false">http://www.stuartaxon.com/?p=195</guid>
		<description><![CDATA[I recently found a neat way of accessing files in a linux VMWare image.   This is really useful, as theres never really a good time to break your VMWare image, this is also handy if you don&#8217;t want to run the whole machine, but just access the files inside. There is one caveat: It only [...]]]></description>
			<content:encoded><![CDATA[<p>I recently found a neat way of accessing files in a linux VMWare image.   This is really useful, as theres never really a good time to break your VMWare image, this is also handy if you don&#8217;t want to run the whole machine, but just access the files inside.</p>
<p>There is one caveat:</p>
<ul>
<li>It only works if the filesystem is ext2 (ext3 works, and ext4 probably works).</li>
</ul>
<h2>Install VMWare DiskMount Utility</h2>
<p>Accept the EULA, download and install the <a href="http://www.vmware.com/download/eula/diskmount_ws_v55.html">VMWare DiskMount utility</a>.</p>
<p>For convenience add the utilities folder to the path:</p>
<pre>C:\Program Files\VMware\VMware DiskMount Utility</pre>
<blockquote><p>Do this through the Windows Gui, or even use my <a href="http://code.google.com/p/batch-flow/">addpath</a> utility.</p></blockquote>
<p>At this point you can mount Windows VMWare images.</p>
<p>The usage is:  <em>vmware-mount drive-letter vmdk-image</em>.</p>
<p>Heres how I mount my Ubuntu image to the j: drive</p>
<blockquote>
<pre>[C:\vmware\Ubuntu]vmware-mount j: ubuntu.vmdk</pre>
<pre>[C:\vmware\Ubuntu]</pre>
</blockquote>
<p>No output here indicates success.</p>
<p>At this point everything seems fine, but a crucial piece of the puzzle is still missing; try and view the files and you still can&#8217;t:</p>
<p style="text-align: center;"><a href="http://www.stuartaxon.com/wp-content/uploads/2010/01/vmware-mount-no-ext2.png"><img class="size-full wp-image-198 aligncenter" title="vmware-mount-no-ext2" src="http://www.stuartaxon.com/wp-content/uploads/2010/01/vmware-mount-no-ext2.png" alt="Failing to see files in an ext2 VMWare image" width="677" height="340" /></a></p>
<div style="clear:both;"></div>
<p>The next step is to make Windows understand the ext2 filesystem, using a special driver.</p>
<h2>Install ext2ifs</h2>
<p>Grab ext2ifs from <a href="http://www.fs-driver.org/">www.fs-driver.org</a> and install.</p>
<p>If the following steps don&#8217;t work then you may need to reboot.</p>
<h2>Thats it!</h2>
<p>You should be now able to access files inside your VMWare image (assuming it&#8217;s ext2 and not reiserfs), remount the image and have a go:</p>
<p>In my case I did:</p>
<blockquote>
<pre>[C:\vmware\Ubuntu]vmware-mount j: ubuntu.vmdk</pre>
<pre>[C:\vmware\Ubuntu] dir j:</pre>
</blockquote>
<p>Heres the output &#8211; hooray, I can copy my work out of the image !</p>
<p><a href="http://www.stuartaxon.com/wp-content/uploads/2010/01/vmware-mount-and-ext2ifs.png"><img class="size-full wp-image-197 aligncenter" title="vmware-mount-and-ext2ifs" src="http://www.stuartaxon.com/wp-content/uploads/2010/01/vmware-mount-and-ext2ifs.png" alt="Viewing files inside a VMWare image with ext2fs" width="677" height="580" /></a></p>
<div style="clear:both;"></div>
<p>This is very useful, especially if you do dist-upgrade in ubuntu and can&#8217;t access the network.</p>
<h2>Bonus tip:</h2>
<p>Newer versions of VMWare player let you install VMWare tools from inside the GUI, this is another way to fix the kind of catestrophic problems you can cause yourself by accidentally upgrading the kernel in an image.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.stuartaxon.com/2010/01/21/access-files-in-a-linux-virtual-machine-from-windows/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using Java2Python to port a JavaCairo tutorial</title>
		<link>http://www.stuartaxon.com/2009/08/21/using-java2python-to-port-a-javacairo-tutorial/</link>
		<comments>http://www.stuartaxon.com/2009/08/21/using-java2python-to-port-a-javacairo-tutorial/#comments</comments>
		<pubDate>Fri, 21 Aug 2009 00:22:15 +0000</pubDate>
		<dc:creator>stu</dc:creator>
				<category><![CDATA[projects]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[cairo]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[javagnome]]></category>
		<category><![CDATA[porting]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://www.stuartaxon.com/?p=158</guid>
		<description><![CDATA[I recently came across Java2Python.  As I&#8217;m interested in Cairo I thought it would be interesting to try porting one of the example tutorials from ZetCode. I&#8217;ll run through the steps involved in porting then try and reach some conclusions at the end . 1. Get setup This is easiest in Linux, I&#8217;m running Ubuntu [...]]]></description>
			<content:encoded><![CDATA[<p>I recently came across <a title="J2Py" href="http://code.google.com/p/java2python/">Java2Python</a>.  As I&#8217;m interested in Cairo I thought it would be interesting to try porting one of the <a href="http://zetcode.com/tutorials/javagnometutorial/firststeps/">example tutorials</a> from ZetCode.</p>
<p>I&#8217;ll run through the steps involved in porting then try and reach some conclusions at the end <img src='http://www.stuartaxon.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> .</p>
<h4>1. Get setup</h4>
<p>This is easiest in Linux, I&#8217;m running Ubuntu (in vmware), and installed</p>
<pre>antlr 2.x
python2.5.x
sun java6
pygtk
java-gnome</pre>
<p>You can install them like this:</p>
<pre class="brush:shell"># sudo apt-get install antlr python2.5 sun-java6-bin libjava-gnome-java
</pre>
<p>Then install Python2Java with easy_install</p>
<pre class="brush:shell"># sudo easy_install-2.5 java2python</pre>
<p>To test if it&#8217;s working run j2py -i.  It should complain there is no file:</p>
<pre class="brush:shell"># j2py -i
Usage: j2py [options]

j2py: error: -i option requires an argument</pre>
<p>If you get any other errors your missing some packages.</p>
<h4>2. Get the Java Code from the Simple Example.</h4>
<p>Save the <a href="http://zetcode.com/tutorials/javagnometutorial/firststeps/">&#8216;simple.java&#8217; example</a> as &#8216;GSimple.java&#8217;</p>
<p>If java-gnome is running ok, compiling and running it you should see a window:</p>
<pre>
# javac GSimple.java
# java GSimple
</pre>
<p><img src="http://www.stuartaxon.com/wp-content/uploads/2009/08/gsimple.png" alt="gsimple" title="gsimple" width="258" height="178" class="alignnone size-full wp-image-171" /></p>
<div style="clear:both;">
<p>Now we&#8217;ll run through the code, it&#8217;s important to understand what it does before we port it&#8230;</p>
<p><span id="more-158"></span></p>
<pre class="brush:java">package com.zetcode;

import org.gnome.gdk.Event;
import org.gnome.gtk.Gtk;
import org.gnome.gtk.Widget;
import org.gnome.gtk.Window;
import org.gnome.gtk.WindowPosition;

/**
 * ZetCode Java Gnome tutorial
 *
 * This program is a simple Java Gnome
 * application.
 *
 * @author jan bodnar
 * website zetcode.com
 * last modified March 2009
 */

public class GSimple extends Window  {

    public GSimple() {

        setTitle("Simple");

        connect(new Window.DeleteEvent() {
            public boolean onDeleteEvent(Widget source, Event event) {
                Gtk.mainQuit();
                return false;
            }
        });

        setDefaultSize(250, 150);
        setPosition(WindowPosition.CENTER);
        show();
    }

    public static void main(String[] args) {
        Gtk.init(args);
        new GSimple();
        Gtk.main();
    }
}</pre>
<h5>Code Runthrough</h5>
<ul>
<li> Import java-gnome bindings</li>
<li> Create a class that extends org.gnome.gtk.Window</li>
<li> Set the size of the window</li>
<li> Connect the &#8216;delete&#8217; event to an InnerClass.  When a delete event arrives,  &#8216;onDeleteEvent&#8217; is called which in turn calls Gtk.mainQuit().<br />
<blockquote><p>Gtk.mainQuit() will quit program and cleanup gtk.</p></blockquote>
</li>
<li> set window size</li>
<li> show the window</li>
<h4>3. Use j2py to create the initial python code.</h4>
<p>The first stage is to generate the python.</p>
<pre class="brush:shell"># j2py -i GSimple.java &gt; gsimple.py</pre>
<p>Output [gsimple.py]:</p>
<pre class="brush:python">#!/usr/bin/env python
# -*- coding: utf-8 -*-

class GSimple(Window):
    """ generated source for GSimple

    """

    def __init__(self):
        setTitle("Simple")

        def onDeleteEvent(self, source, event):
            Gtk.mainQuit()
            return False

        connect(Window.DeleteEvent())
        setDefaultSize(250, 150)
        setPosition(WindowPosition.CENTER)
        show()

    @classmethod
    def main(cls, args):
        Gtk.init(args)
        GSimple()
        Gtk.cls.main()

if __name__ == '__main__':
    import sys
    GSimple.main(sys.argv)</pre>
<h4>5. Analyze</h4>
<p>Unfortunately j2py is not magic, it&#8217;s time to have a look at the code..</p>
<p>Things are missing and others look wrong, heres an initial list of problems:</p>
<ul>
<li>no imports</li>
<li>&#8216;self&#8217; not specified in class constructor</li>
<li>CamelCase used &#8211; this is suspicious, it&#8217;s unlikely the cairo bindings work like this</li>
<li>@classmethod is probably not nessacary here</li>
<li>Gtk.cls.main() &#8211; This looks particularly suspicious</li>
</ul>
<p>Indeed &#8211; If you try and run it, the missing imports become apparent:</p>
<pre class="brush:shell">bagside@bagvapp:~/jythongnome$ python gsimple.py
Traceback (most recent call last):
  File "gsimple.py", line 5, in
    class GSimple(Window):
NameError: name 'Window' is not defined</pre>
<h4>6.  Fixing the initial Problems</h4>
<p>Some problems are easy to fix, others involve learning the differences between the java and python APIs.</p>
<p>Starting with the easiest&#8230;</p>
<h5>&#8216;self&#8217; not specified in class constructor</h5>
<p>This looks easiest so we&#8217;ll tackle it first, heres the new constructor:</p>
<pre class="brush:python">        self.setTitle("Simple")

        def onDeleteEvent(self, source, event):
            Gtk.mainQuit()
            return False

        self.connect(Window.DeleteEvent())
        self.setDefaultSize(250, 150)
        self.setPosition(WindowPosition.CENTER)
        self.show()</pre>
<p>Doing this does emphasize how wrong the CamelCase looks, also the onDeleteEvent being a function in the constructor doesn&#8217;t look right, we could probably move it up a level.</p>
<h5>No imports</h5>
<p>In the java code there is</p>
<pre class="brush:java">import org.gnome.gdk.Event;
import org.gnome.gtk.Gtk;
import org.gnome.gtk.Widget;
import org.gnome.gtk.Window;
import org.gnome.gtk.WindowPosition;</pre>
<p>Googling for <strong>Window module</strong> and <strong>gtk</strong> you&#8217;ll end up at the <a href="http://library.gnome.org/devel/pygtk/stable/class-gtkwindow.html">pygtk site</a>.<br />
The documentation shows the Window class is in the gtk module.  Further research on the site or in the python prompt shows most of the other classes are in the same module too.</p>
<pre class="brush:python"># To do research in the python prompt, simply start python and try
import gtk
dir(gtk)
help(gtk)
help(gtk.Window)</pre>
<p>It&#8217;s worth having a glance at the python help to see if the bindings look the same, start python and try:</p>
<pre class="brush:python">import gtk
help(gtk.Window)</pre>
<p>&#8230;[output snipped]&#8230;</p>
<pre class="brush:python"> |  set_skip_taskbar_hint(...)
 |
 |  set_startup_id(...)
 |
 |  set_title(...)
 |
 |  set_transient_for(...)
 |
 |  set_type_hint(...)
 |
 |  set_urgency_hint(...)</pre>
<p>It&#8217;s fairly obvious that CamelCase is not used here, as suspected (remember to change this later&#8230;)</p>
<p>As &#8220;Window&#8221; is quite generic, I&#8217;ll move everything into the &#8216;gtk&#8217; namespace, add</p>
<pre class="brush:python">import gtk</pre>
<p>To the top of the code, and change all instances of the gtk classes to &#8216;gtk.ClassName&#8217;, e.g. gtk.Window:</p>
<pre class="brush:python">#!/usr/bin/env python
# -*- coding: utf-8 -*-

import gtk

class GSimple(gtk.Window):
    """ generated source for GSimple

    """

    def __init__(self):
        self.setTitle("Simple")

        def onDeleteEvent(self, source, event):
            Gtk.mainQuit()
            return False

        self.connect(gtk.Window.DeleteEvent())
        self.setDefaultSize(250, 150)
        self.setPosition(WindowPosition.CENTER)
        self.show()

    @classmethod
    def main(cls, args):
        Gtk.init(args)
        GSimple()
        Gtk.cls.main()

if __name__ == '__main__':
    import sys
    GSimple.main(sys.argv)</pre>
<p>And try running it:</p>
<pre class="brush:shell">Traceback (most recent call last):
  File "gsimple.py", line 31, in
    GSimple.main(sys.argv)
  File "gsimple.py", line 25, in main
    Gtk.init(args)
NameError: global name 'Gtk' is not defined</pre>
<p>It&#8217;s complaining about the &#8216;main&#8217; class method on GSimple, we can just remove it and move everything into the main method of the module:</p>
<pre class="brush:python">if __name__ == '__main__':
    import sys
    Gtk.init(args)
    GSimple()
    Gtk.cls.main()</pre>
<p>This still won&#8217;t work for sure as &#8216;Gtk&#8217; is not in the namespace.</p>
<p>The first step is to work out whats going on, consult the java code then the relevant documentation.</p>
<pre class="brush:java">
<pre>[GSimple.java]</pre>
<p>public static void main(String[] args) {</p>
<p>Gtk.init(args);</p>
<p>new GSimple();</p>
<p>Gtk.main();</p>
<p>}</pre>
<p>OK, the code is the same, what does the java gnome documentation say about Gtk.init ?</p>
<blockquote><p>init</p>
<p>public static void init(String[] args)</p>
<p>Initialize the GTK libraries. This must be called before any other org.gnome.* classes are used.</p>
<p>Parameters:<br />
args &#8211; The command line arguments array. This is passed to the underlying library to allowing user (or window manager) to alter GTK&#8217;s behaviour.<br />
Since:<br />
4.0.0</p></blockquote>
<p>Looking in pygtk there isn&#8217;t &#8216;init&#8217; (it would be confusing having this and __init__ anyway).</p>
<p>Looking in the pygtk documentation there is no &#8216;init&#8217; method (which makes sense as it could be confused with  &#8216;__init__&#8217;.   There is however <a href="http://www.pygtk.org/docs/pygtk/gtk-functions.html#function-gtk--main">gtk.main</a>:</p>
<blockquote><p>gtk.main</p>
<p>def gtk.main()</p>
<p>The gtk.main() function runs the main loop until the gtk.main_quit() function is called. You can nest calls to gtk.main(). In that case the call to the gtk.main_quit() function will make the innermost invocation of the main loop return.</p></blockquote>
<p>Here I confess some prior knowledge, I already had a vague idea the main loop might be involved as I&#8217;d worked with gtk before.</p>
<p>So we&#8217;ll try gtk.main, and commenting out the line calling main on the class as I&#8217;m not entirely sure what it does:</p>
<pre class="brush:python">if __name__ == '__main__':
    import sys
    gtk.main()  # Note gtk.main takes no parameters
    GSimple()
    # Gtk.cls.main()</pre>
<h5>Try running</h5>
<p>And&#8230; nothing happens &#8211; you have to quit with CTRL-C!</p>
<p>It looks like it&#8217;s not even getting to the constructor (as there should be errors here, maybe gtk.main() needs to run *after* the GSimple() constructor..</p>
<p>Sure enough:</p>
<pre class="brush:shell"># python gsimple.py
Traceback (most recent call last):
  File "gsimple.py", line 30, in
    GSimple()
  File "gsimple.py", line 13, in __init__
    self.setTitle("Simple")
AttributeError: 'GSimple' object has no attribute 'setTitle'</pre>
<h5>Fixing the CamelCase</h5>
<p>The pygtk documentation seemed all be underscored_lowercase, it&#8217;s probably safe to speculatively change everything to this then fix any errors:</p>
<pre class="brush:python">#!/usr/bin/env python
# -*- coding: utf-8 -*-

import gtk

class GSimple(gtk.Window):
    """ generated source for GSimple

    """

    def __init__(self):
        self.set_title("Simple")

        def on_delete_event(self, source, event):
            gtk.main_quit()
            return False

        self.connect(gtk.Window.DeleteEvent())
        self.set_default_size(250, 150)
        self.set_position(WindowPosition.CENTER)
        self.show()

if __name__ == '__main__':
    import sys
    GSimple()
    gtk.main()  # Note gtk.main takes no parameters
    #    Gtk.cls.main()</pre>
<p>Output:</p>
<pre> python gsimple.py
gsimple.py:12: GtkWarning: gtk_window_set_title: assertion `GTK_IS_WINDOW (window)' failed
  self.set_title("Simple")
Traceback (most recent call last):
  File "gsimple.py", line 28, in
    GSimple()
  File "gsimple.py", line 18, in __init__
    self.connect(gtk.Window.DeleteEvent())
AttributeError: type object 'gtk.Window' has no attribute 'DeleteEvent'</pre>
<p>Thats progress!  It means the self.set_title call probably works, and there is a self.connect call too.</p>
<p>There is no DeleteEvent in the <a href="http://library.gnome.org/devel/pygtk/stable/">pygtk documentation</a>&#8230; which is not suprising as it looks more java than python.<br />
Better to step back and have a look at self.connect, in the end I googled</p>
<pre>gtk connect delete event</pre>
<p>And got to a <a href="http://www.pygtk.org/pygtk2tutorial/ch-GettingStarted.html">getting started with pygtk</a> page:</p>
<blockquote><pre>
36  # When the window is given the "delete_event" signal (this is given
37  # by the window manager, usually by the "close" option, or on the
38  # titlebar), we ask it to call the delete_event () function
39  # as defined above. The data passed to the callback
40  # function is NULL and is ignored in the callback function.
41  self.window.connect("delete_event", self.delete_event)</pre>
</blockquote>
<p>Note here they have &#8216;window&#8217; as their delegating, our generated code extends this class.</p>
<p>The signature is slightly different, also instead of &#8216;on_delete_event&#8217; the function is just called &#8216;delete_event&#8217;, this seems more pythonic so we&#8217;ll do the same.</p>
<p>This would also be a good time to move the function into the main class, out of the constructor:</p>
<pre class="brush:python">class GSimple(gtk.Window):
    """ generated source for GSimple

    """

    def __init__(self):
        self.set_title("Simple")

        self.connect('delete_event', delete_event)
        self.set_default_size(250, 150)
        self.set_position(WindowPosition.CENTER)
        self.show()

    def delete_event(self, source, event):
	gtk.main_quit()
	return False</pre>
<p>And the output:</p>
<pre>gsimple.py:12: GtkWarning: gtk_window_set_title: assertion `GTK_IS_WINDOW (window)' failed
  self.set_title("Simple")
Traceback (most recent call last):
  File "gsimple.py", line 29, in
    GSimple()
  File "gsimple.py", line 14, in __init__
    self.connect('delete_event', delete_event)
NameError: global name 'delete_event' is not defined</pre>
<p>The first line assertion <em>`GTK_IS_WINDOW (window)&#8217; failed </em>is the important one here; it makes sense, the constructor of the gtk.Window has not been called yet.</p>
<p>Add
<pre>gtk.Window.__init__(self)</pre>
<p> to the top of the constructor and run.</p>
<pre>
Traceback (most recent call last):
File "gsimple.py", line 30, in <module>
GSimple()
File "gsimple.py", line 17, in __init__
self.set_position(WindowPosition.CENTER)
NameError: global name 'WindowPosition' is not defined
</pre>
<h6>And again&#8230;</h6>
<p>Repeating the same process for WindowPosition and set_position, you&#8217;ll find that the set_position line should look like this:</p>
<pre class="brush:python">
self.set_position(gtk.WIN_POS_CENTER)
</pre>
<p>And the output&#8230;<br />
<img src="http://www.stuartaxon.com/wp-content/uploads/2009/08/gsimple.png" alt="gsimple" title="gsimple" width="258" height="178" class="alignnone size-full wp-image-171" /></p>
<div style="clear:both;">
<p>Huzzah!  Success!</p>
<h4>Final Source code</h4>
<pre class="brush:python">
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import gtk

class GSimple(gtk.Window):

    def __init__(self):
	gtk.Window.__init__(self)
        self.set_title("Simple")

        self.connect('delete_event', self.delete_event)
        self.set_default_size(250, 150)
        self.set_position(gtk.WIN_POS_CENTER)
        self.show()

    def delete_event(self, source, event):
	gtk.main_quit()
	return False

if __name__ == '__main__':
    import sys
    GSimple()
    gtk.main()
</pre>
<h4>Conclusions</h4>
<p>Although Cairo APIs are available for Java and Python they are of course quite different.   One implication of this is that if you wanted to use Cairo from a Jython java applet (via native methods) it couldn&#8217;t have code 100% the same as a normal python app.<br />
java2python is good, but you still need to put the work in.<br />
Working in this way is also a good way to find differences within implementations of an API.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.stuartaxon.com/2009/08/21/using-java2python-to-port-a-javacairo-tutorial/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>The Bloggage and slacking</title>
		<link>http://www.stuartaxon.com/2009/08/07/the-bloggage-and-slacking/</link>
		<comments>http://www.stuartaxon.com/2009/08/07/the-bloggage-and-slacking/#comments</comments>
		<pubDate>Fri, 07 Aug 2009 01:28:25 +0000</pubDate>
		<dc:creator>stu</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://www.stuartaxon.com/?p=150</guid>
		<description><![CDATA[Just been checking out what Mr Palmer has been up to&#8230; quite a lot it seems&#8230;   if I could get further than 2 chapters in 6 months with Getting Things Done maybe I could stop procrastination and start GTD too And I&#8217;ve been checking out some online comics&#8230; My 6 monthlyish check on SayUncle comics [...]]]></description>
			<content:encoded><![CDATA[<p>Just been checking out what <a href="http://brettpalmergroup.webs.com/apps/blog/">Mr Palmer</a> has been up to&#8230; quite a lot it seems&#8230;   if I could get further than 2 chapters in 6 months with Getting Things Done maybe I could stop procrastination and start GTD too <img src='http://www.stuartaxon.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>And I&#8217;ve been checking out some online comics&#8230;</p>
<p>My 6 monthlyish check on <a href="http://sayunclecomics.livejournal.com/">SayUncle comics blog</a> always makes me want to do some drawing.</p>
<p>Also Quite enjoying the <a href="http://nedroidcomics.livejournal.com/240308.html">Beartato in space storyline</a> in nedroid.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.stuartaxon.com/2009/08/07/the-bloggage-and-slacking/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Toast</title>
		<link>http://www.stuartaxon.com/2009/01/09/toast/</link>
		<comments>http://www.stuartaxon.com/2009/01/09/toast/#comments</comments>
		<pubDate>Fri, 09 Jan 2009 19:29:36 +0000</pubDate>
		<dc:creator>stu</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[toast]]></category>

		<guid isPermaLink="false">http://www.stuartaxon.com/?p=144</guid>
		<description><![CDATA[Lyrics]]></description>
			<content:encoded><![CDATA[<p><object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="425" height="344" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,40,0"><param name="allowFullScreen" value="true" /><param name="allowscriptaccess" value="always" /><param name="src" value="http://www.youtube.com/v/WJmKStqugMc&amp;hl=en&amp;fs=1" /><embed type="application/x-shockwave-flash" width="425" height="344" src="http://www.youtube.com/v/WJmKStqugMc&amp;hl=en&amp;fs=1" allowscriptaccess="always" allowfullscreen="true"></embed></object></p>
<p><a href="http://www.lyricskeeper.com/paul_young-lyrics/224965-toast-lyrics.htm">Lyrics</a></p>
]]></content:encoded>
			<wfw:commentRss>http://www.stuartaxon.com/2009/01/09/toast/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Router upgrade nixed xbmc</title>
		<link>http://www.stuartaxon.com/2008/12/14/router-upgrade-nixed-xbmc/</link>
		<comments>http://www.stuartaxon.com/2008/12/14/router-upgrade-nixed-xbmc/#comments</comments>
		<pubDate>Sun, 14 Dec 2008 23:26:50 +0000</pubDate>
		<dc:creator>stu</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[network]]></category>
		<category><![CDATA[router]]></category>
		<category><![CDATA[samba]]></category>
		<category><![CDATA[xmbc]]></category>

		<guid isPermaLink="false">http://www.stuartaxon.com/?p=140</guid>
		<description><![CDATA[It turns out my lack of networked media consumption utopia via xbmc was all down to upgrading my router&#8230; the xbox was fixed on 10.0.0.x while everything else moved over to 192.168.1.x  &#8230;so I learned that SMB doesn&#8217;t like that at all. Oh well, one task down&#8230; many more todo&#8230; maybe I can actually setup [...]]]></description>
			<content:encoded><![CDATA[<p>It turns out my lack of networked media consumption utopia via xbmc was all down to upgrading my router&#8230; the xbox was fixed on 10.0.0.x while everything else moved over to 192.168.1.x  &#8230;so I learned that SMB doesn&#8217;t like that at all.</p>
<p>Oh well, one task down&#8230; many more todo&#8230; maybe I can actually setup my kurobox in the next few days and then some really nice things might be possible.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.stuartaxon.com/2008/12/14/router-upgrade-nixed-xbmc/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Noodleglue &#8211; found!</title>
		<link>http://www.stuartaxon.com/2008/10/01/noodleglue-found/</link>
		<comments>http://www.stuartaxon.com/2008/10/01/noodleglue-found/#comments</comments>
		<pubDate>Wed, 01 Oct 2008 12:13:38 +0000</pubDate>
		<dc:creator>stu</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[blender]]></category>
		<category><![CDATA[blender3d]]></category>
		<category><![CDATA[gnome]]></category>
		<category><![CDATA[gtk]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[java-gnome]]></category>
		<category><![CDATA[language bindings]]></category>
		<category><![CDATA[noodleglue]]></category>
		<category><![CDATA[processing]]></category>
		<category><![CDATA[processing.org]]></category>

		<guid isPermaLink="false">http://www.stuartaxon.com/?p=112</guid>
		<description><![CDATA[Download noodleglue here: I had some trouble finding it. - Maybe can be used to link processing and Blender.]]></description>
			<content:encoded><![CDATA[<p>For the last few days I&#8217;ve been looking for a project called NoodleGlue.  I got interested when I wanted to look into generating java wrappings for the <a href="http://verse.blender.org/">Verse</a> library.</p>
<p><strong>If you just want to download Noodleglue skip to the end of the article</strong>.</p>
<p>Verse is a network protocol that lets 3d applications talk to each other in realtime, being developed by the Blender foundation.  I was wondering how difficult it would be to link the ease of use of <a href="http://processing.org">processing</a> with the power of Blender.</p>
<p><span id="more-112"></span></p>
<p>Processing being a simplified java, I started to look at ways of generating java bindings.</p>
<p>I knew that GTK had switched to generating their java bindings automatically, so started looking at technologies to do this, immediately I found out about noodleglue which seemed to fit the bill.</p>
<p>Slight problem &#8211; the site was down and the developers nowhere to be seen, after much searching I realised I should be looking for something written in C/C++ not java, searched for NoodleGlue.tar.gz and &#8216;bing!&#8217; there it is on one persons website.</p>
<p>I&#8217;m uploading it here, although I should probably put it on google code as well.</p>
<p><a href="http://www.stuartaxon.com/files/NoodleGlue.tar.gz">NoodleGlue.tar.gz</a></p>
<p><a href="http://www.stuartaxon.com/files/NoodleGlueGenerator.jar">NoodleGlueGenerator.jar</a></p>
<p><a title="Archive of Noodleglue.org" href="http://web.archive.org/web/20070205204525rn_1/www.noodleglue.org/noodleglue/noodleglue.html">Snapshot of noodleglue.org</a> in in the internet wayback machine.</p>
<p>I must also mention <a href="http://code.google.com/p/cibyl/">Cibyl</a>, a project for compiling C to java, it worked best for me in linux&#8230; currently a bit of a pain to setup but an interesting project nonetheless.</p>
<p><a href="http://code.google.com/p/cibyl/">Cibyl Project</a></p>
<p>Eplilogue &#8211; There was a version on the internet wayback machine the whole time, not sure why I didn&#8217;t check, however the version I&#8217;ve found is newer &#8211; at least 18/Apr/2007.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.stuartaxon.com/2008/10/01/noodleglue-found/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Track all your forum posts in delicious</title>
		<link>http://www.stuartaxon.com/2008/09/21/track-all-your-forum-posts-in-delicious/</link>
		<comments>http://www.stuartaxon.com/2008/09/21/track-all-your-forum-posts-in-delicious/#comments</comments>
		<pubDate>Sun, 21 Sep 2008 04:11:41 +0000</pubDate>
		<dc:creator>stu</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[delicious]]></category>
		<category><![CDATA[delicious.com]]></category>
		<category><![CDATA[productivity]]></category>

		<guid isPermaLink="false">http://www.stuartaxon.com/?p=95</guid>
		<description><![CDATA[I&#8217;ve been using delicious for a while, but have started bookmarking all my forum posts in there, it should be a lot easier for me to find out whatever I was talking about in the future]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been using <a title="delicious" href="http://delicous.com" target="_blank">delicious</a> for a while, but have started bookmarking all my forum posts in there, it should be a lot easier for me to find out whatever I was talking about in the future <img src='http://www.stuartaxon.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://www.stuartaxon.com/2008/09/21/track-all-your-forum-posts-in-delicious/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

