<?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>defuze.org</title>
	<atom:link href="http://www.defuze.org/feed" rel="self" type="application/rss+xml" />
	<link>http://www.defuze.org</link>
	<description></description>
	<lastBuildDate>Tue, 06 Sep 2011 18:55:04 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>ws4py &#8211; WebSocket client and server library for Python</title>
		<link>http://www.defuze.org/archives/271-ws4py-websocket-client-and-server-library-for-python.html</link>
		<comments>http://www.defuze.org/archives/271-ws4py-websocket-client-and-server-library-for-python.html#comments</comments>
		<pubDate>Tue, 06 Sep 2011 18:54:11 +0000</pubDate>
		<dc:creator>Sylvain Hellegouarch</dc:creator>
				<category><![CDATA[android]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[websockets]]></category>
		<category><![CDATA[ws4py]]></category>
		<category><![CDATA[cherrypy]]></category>

		<guid isPermaLink="false">http://www.defuze.org/?p=271</guid>
		<description><![CDATA[Recently I released ws4py, a package that provides client and server WebSocket support for Python 2.6 and 2.7. Let&#8217;s first have a quick overview of what ws4py offers for now: WebSocket specification draft-10 of the current specification. A threaded client. &#8230; <a href="http://www.defuze.org/archives/271-ws4py-websocket-client-and-server-library-for-python.html">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[
<p>Recently I released <a title="ws4py - github repository" href="https://github.com/Lawouach/WebSocket-for-Python" target="_blank">ws4py</a>, a package that provides client and server WebSocket support for Python 2.6 and 2.7.</p>
<p>Let&#8217;s first have a quick overview of what ws4py offers for now:</p>
<ul>
<li>WebSocket specification <a title="WebSocket draft-10 specification" href="http://tools.ietf.org/html/draft-ietf-hybi-thewebsocketprotocol-10" target="_blank">draft-10</a> of the current specification.</li>
<li>A threaded client. This gives a simple client that doesn&#8217;t require an external dependency.</li>
<li>A Tornado client. This client is based on <a title="Tornado Home" href="http://www.tornadoweb.org/" target="_blank">Tornado 2.0</a> which is quite a popular way of running asynchronous networking code these days. Tornado provides its own server implementation so I didn&#8217;t include mine in ws4py.</li>
<li>A <a title="CherryPy Home" href="http://www.cherrypy.org" target="_blank">CherryPy</a> extension so that you can integrate WebSocket from within your <a title="CherryPy 3.2.1 downloads" href="http://download.cherrypy.org/cherrypy/3.2.1/" target="_blank">CherryPy 3.2.1</a> server.</li>
<li>A gevent server based on the popular <a title="gevent PyPi" href="http://pypi.python.org/pypi/gevent/0.13.6" target="_blank">gevent library</a>. This is courtesy of <a href="https://github.com/progrium" target="_blank">Jeff Lindsay</a>.</li>
<li>Based on Jeff&#8217;s work, a pure WSGI middleware as well (available in the current master branch only until the next release).</li>
<li>ws4py runs on Android devices thanks to the <a href="http://code.google.com/p/android-scripting/" target="_blank">SL4A</a> package</li>
</ul>
<p>Hopefully more client and servers will be added along the way as well as Python 3.x support. The former should be rather simple to add due to the way I designed ws4py.</p>
<p>The main idea is to make a distinction between the bytes provider and the bytes processing. The former is essentially reading and writing bytes from the connected socket. The latter is the function of making something out of the received bytes based on the WebSocket specification. In most implementations I have seen so far, both are rather heavily intertwined making it difficult to use a different bytes provider.</p>
<p>ws4py tries a different path by relying on a great feature of Python: the possibility to <a href="http://www.python.org/dev/peps/pep-0342/" target="_blank">send data back to a generator</a>. For instance, the frame parsing yields the quantity of bytes each time it needs more and the caller feeds back the generator those bytes once they are received. In fact, the caller of a frame parser is a stream object which acts the same way. The caller of that stream object is in fact the bytes provider (a client or a server). The stream is in charge of aggregating frames into a WebSocket message. Thanks to that design, both the frame and stream objects are totally unaware of the bytes provider and can be easily adapted in various contexts (gevent, tornado, CherryPy, etc.).</p>
<p>On my TODO list for ws4py:</p>
<ul>
<li>Upgrade to a more recent version of the specification</li>
<li>Python 3.x implementation</li>
<li>Better documentation, read, write documentation.</li>
<li>Better performances on very large WebSocket messages</li>
</ul>
<div><span style="font-size: small;"><span style="line-height: 24px;"><br />
</span></span></div>

]]></content:encoded>
			<wfw:commentRss>http://www.defuze.org/archives/271-ws4py-websocket-client-and-server-library-for-python.html/feed</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>Acceptance testing a CherryPy application with Robot Framework</title>
		<link>http://www.defuze.org/archives/266-acceptance-testing-a-cherrypy-application-with-robot-framework.html</link>
		<comments>http://www.defuze.org/archives/266-acceptance-testing-a-cherrypy-application-with-robot-framework.html#comments</comments>
		<pubDate>Sun, 12 Jun 2011 17:21:18 +0000</pubDate>
		<dc:creator>Sylvain Hellegouarch</dc:creator>
				<category><![CDATA[python]]></category>
		<category><![CDATA[acceptance testing]]></category>
		<category><![CDATA[cherrypy]]></category>
		<category><![CDATA[robot framework]]></category>
		<category><![CDATA[selenium]]></category>

		<guid isPermaLink="false">http://www.defuze.org/?p=266</guid>
		<description><![CDATA[I recently received the Python Testing Cookbook authored by Greg L. Turnquist and was happy to read about recipes on acceptance testing using Robot Framework. We&#8217;ve been using this tool at work for a few weeks now with great results. &#8230; <a href="http://www.defuze.org/archives/266-acceptance-testing-a-cherrypy-application-with-robot-framework.html">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[
<p>I recently received the <a href="http://pythontestingcookbook.posterous.com/" target="_blank">Python Testing Cookbook</a> authored by <a href="http://www.linkedin.com/in/greglturnquist" target="_blank">Greg L. Turnquist</a> and was happy to read about recipes on acceptance testing using <a href="http://code.google.com/p/robotframework/" target="_blank">Robot Framework</a>. We&#8217;ve been using this tool at work for a few weeks now with great results. Greg shows how to test a web application using the <a href="http://code.google.com/p/robotframework-seleniumlibrary/" target="_blank">Selenium Library extension</a> for Robot Framework and I thought it&#8217;d be fun to demonstrate how to test a <a href="http://www.cherrypy.org/" target="_blank">CherryPy</a> application following his recipe. So here we go.</p>
<p>First some requirements:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ mkvirtualenv <span style="color: #660033;">--distribute</span> <span style="color: #660033;">--no-site-packages</span> <span style="color: #660033;">--unzip-setuptools</span> acceptance
<span style="color: #7a0874; font-weight: bold;">&#40;</span>acceptance<span style="color: #7a0874; font-weight: bold;">&#41;</span>$ pip <span style="color: #c20cb9; font-weight: bold;">install</span> cherrypy
<span style="color: #7a0874; font-weight: bold;">&#40;</span>acceptance<span style="color: #7a0874; font-weight: bold;">&#41;</span>$ pip <span style="color: #c20cb9; font-weight: bold;">install</span> robotframework
<span style="color: #7a0874; font-weight: bold;">&#40;</span>acceptance<span style="color: #7a0874; font-weight: bold;">&#41;</span>$ pip <span style="color: #c20cb9; font-weight: bold;">install</span> robotframework-seleniumlibrary</pre></div></div>

<p>Let&#8217;s define a simple CherryPy application, which displays a input text where to type a message. When the submit button is pressed, the message is sent to the server and returned as-is. Well it&#8217;s an echo message really.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> cherrypy
&nbsp;
__all__ = <span style="color: black;">&#91;</span><span style="color: #483d8b;">'Echo'</span><span style="color: black;">&#93;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> Echo<span style="color: black;">&#40;</span><span style="color: #008000;">object</span><span style="color: black;">&#41;</span>:
    @cherrypy.<span style="color: black;">expose</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> index<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #483d8b;">&quot;&quot;&quot;&lt;html&gt;
&lt;head&gt;&lt;title&gt;Robot Framework Test for CherryPy&lt;/title&gt;&lt;/head&gt;
&lt;body&gt;
&lt;form method=&quot;post&quot; action=&quot;/echo&quot;&gt;
&lt;input type=&quot;text&quot; name=&quot;message&quot; /&gt;
&lt;input type=&quot;submit&quot; /&gt;
&lt;/form&gt;
&lt;/body&gt;
&lt;/html&gt;&quot;&quot;&quot;</span>
&nbsp;
    @cherrypy.<span style="color: black;">expose</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> echo<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, message<span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> message
&nbsp;
<span style="color: #ff7700;font-weight:bold;">if</span> __name__ == <span style="color: #483d8b;">'__main__'</span>:
    cherrypy.<span style="color: black;">quickstart</span><span style="color: black;">&#40;</span>Echo<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>Save the code above in a module named myapp.py</p>
<p>Next, we create an extension to Robot Framework that will manage CherryPy. Save the following in a module CherryPyLib.py. It&#8217;s important to respect that name since Robot Framework expects the module and its class to match in names.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">imp</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">os</span>, <span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">import</span> cherrypy
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> CherryPyLib<span style="color: black;">&#40;</span><span style="color: #008000;">object</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">def</span> setup_cherrypy<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, conf_file=<span style="color: #008000;">None</span><span style="color: black;">&#41;</span>:
        <span style="color: #483d8b;">&quot;&quot;&quot;
        Configures the CherryPy engine and server using
        the built-in 'embedded' environment mode.
&nbsp;
        If provided, `conf_file` is a path to a CherryPy
        configuration file used in addition.
        &quot;&quot;&quot;</span>
        cherrypy.<span style="color: black;">config</span>.<span style="color: black;">update</span><span style="color: black;">&#40;</span><span style="color: black;">&#123;</span><span style="color: #483d8b;">&quot;environment&quot;</span>: <span style="color: #483d8b;">&quot;embedded&quot;</span><span style="color: black;">&#125;</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">if</span> conf_file:
            cherrypy.<span style="color: black;">config</span>.<span style="color: black;">update</span><span style="color: black;">&#40;</span>conf_file<span style="color: black;">&#41;</span>            
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> start_cherrypy<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #483d8b;">&quot;&quot;&quot;
        Starts a CherryPy engine.
        &quot;&quot;&quot;</span>
        cherrypy.<span style="color: black;">engine</span>.<span style="color: black;">start</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> exit_cherrypy<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #483d8b;">&quot;&quot;&quot;
        Terminates a CherryPy engine.
        &quot;&quot;&quot;</span>
        cherrypy.<span style="color: black;">engine</span>.<span style="color: black;">exit</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> mount_application<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, appmod, appcls, directory=<span style="color: #008000;">None</span><span style="color: black;">&#41;</span>:
        <span style="color: #483d8b;">&quot;&quot;&quot;
        Mounts an application to be tested. `appmod` is the name
        of a Python module containing `appcls`. The module is
        looked for in the given directory. If not provided, we use
        the current one instead.
        &quot;&quot;&quot;</span>
        directory = directory <span style="color: #ff7700;font-weight:bold;">or</span> <span style="color: #dc143c;">os</span>.<span style="color: black;">getcwd</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">file</span>, filename, description = <span style="color: #dc143c;">imp</span>.<span style="color: black;">find_module</span><span style="color: black;">&#40;</span>appmod, <span style="color: black;">&#91;</span>directory<span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
        mod = <span style="color: #dc143c;">imp</span>.<span style="color: black;">load_module</span><span style="color: black;">&#40;</span>appmod, <span style="color: #008000;">file</span>, filename, description<span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">hasattr</span><span style="color: black;">&#40;</span>mod, appcls<span style="color: black;">&#41;</span>:
            cls = <span style="color: #008000;">getattr</span><span style="color: black;">&#40;</span>mod, appcls<span style="color: black;">&#41;</span>
            app = cls<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
            cherrypy.<span style="color: black;">tree</span>.<span style="color: black;">mount</span><span style="color: black;">&#40;</span>app<span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">else</span>:
            <span style="color: #ff7700;font-weight:bold;">raise</span> <span style="color: #008000;">ImportError</span>, <span style="color: #483d8b;">&quot;cannot import name %s from %s&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>appcls, appmod<span style="color: black;">&#41;</span></pre></div></div>

<p>Note that we start and stop the CherryPy server during the test itself, meaning you don&#8217;t need to start it separately. Pure awesomeness.</p>
<p>Finally let&#8217;s write a straightforward acceptance test to validate the overall workflow of echoing a message using our little application.</p>
<pre>
***Settings***
Library	SeleniumLibrary
Library	CherryPyLib
Suite Setup	Start Dependencies
Suite Teardown	Shutdown Dependencies
Test Setup	Mount Application	myapp	Echo

***Variables***
${MSG}	Hello World
${HOST}	http://localhost:8080/

***Test Cases***
Echo ${MSG}
     Open Browser	${HOST}
     Input text		message		${MSG}
     Submit form
     Page Should Contain		${MSG}
     Close All Browsers

***Keywords***
Start Dependencies
    Setup Cherrypy
    Start CherryPy
    Start Selenium Server
    Sleep 	3s

Shutdown Dependencies
    Stop Selenium Server
    Exit CherryPy
</pre>
<p>Save the test above into a file named testmyapp.txt. You can finally run the test as follow:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">&#40;</span>acceptance<span style="color: #7a0874; font-weight: bold;">&#41;</span>$ pybot <span style="color: #660033;">--pythonpath</span> . testmyapp.txt</pre></div></div>

<p>This will start CherryPy, Selenium&#8217;s proxy server and Firefox within which the test case will be run. Easy, elegant and powerful.</p>

]]></content:encoded>
			<wfw:commentRss>http://www.defuze.org/archives/266-acceptance-testing-a-cherrypy-application-with-robot-framework.html/feed</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Hosting a Django application on a CherryPy server</title>
		<link>http://www.defuze.org/archives/262-hosting-a-django-application-on-a-cherrypy-server.html</link>
		<comments>http://www.defuze.org/archives/262-hosting-a-django-application-on-a-cherrypy-server.html#comments</comments>
		<pubDate>Sun, 05 Jun 2011 17:47:04 +0000</pubDate>
		<dc:creator>Sylvain Hellegouarch</dc:creator>
				<category><![CDATA[python]]></category>
		<category><![CDATA[cherrypy]]></category>
		<category><![CDATA[django]]></category>

		<guid isPermaLink="false">http://www.defuze.org/?p=262</guid>
		<description><![CDATA[Recently at work I&#8217;ve had the requirement to host a Django application in a CherryPy server. I first looked for various projects I knew were doing just that. Unfortunately, after trying them I was rather disapointed. Their approach is to &#8230; <a href="http://www.defuze.org/archives/262-hosting-a-django-application-on-a-cherrypy-server.html">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[
<p>Recently at work I&#8217;ve had the requirement to host a Django application in a CherryPy server. I first looked for <a href="http://djangopackages.com/grids/g/webserver/" target="_blank">various projects</a> I knew were doing just that. Unfortunately, after trying them I was rather disapointed. Their approach is to provide a command similar to<a href="https://docs.djangoproject.com/en/1.3/ref/django-admin/#runserver-port-or-address-port" target="_blank"> the famous Django runserver</a>&#8216;s one but I&#8217;ve found it to be more complex than necessary. So I wrote my own module that performs those operations by staying much closer to how CherryPy does work, most specifically by using the <a href="http://www.defuze.org/archives/198-managing-your-process-with-the-cherrypy-bus.html" target="_blank">process bus</a> coming with CherryPy.</p>
<p>I&#8217;m sharing a stripped down version of the module I wrote which shows how one could host a Django application in a CherryPy server. Hopefully this might help some of you.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;"># Python stdlib imports</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">sys</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">logging</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">os</span>, <span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>
&nbsp;
<span style="color: #808080; font-style: italic;"># Third-party imports</span>
<span style="color: #ff7700;font-weight:bold;">import</span> cherrypy
<span style="color: #ff7700;font-weight:bold;">from</span> cherrypy.<span style="color: black;">process</span> <span style="color: #ff7700;font-weight:bold;">import</span> wspbus, plugins
<span style="color: #ff7700;font-weight:bold;">from</span> cherrypy <span style="color: #ff7700;font-weight:bold;">import</span> _cplogging, _cperror
<span style="color: #ff7700;font-weight:bold;">from</span> django.<span style="color: black;">conf</span> <span style="color: #ff7700;font-weight:bold;">import</span> settings
<span style="color: #ff7700;font-weight:bold;">from</span> django.<span style="color: black;">core</span>.<span style="color: black;">handlers</span>.<span style="color: black;">wsgi</span> <span style="color: #ff7700;font-weight:bold;">import</span> WSGIHandler
<span style="color: #ff7700;font-weight:bold;">from</span> django.<span style="color: black;">http</span> <span style="color: #ff7700;font-weight:bold;">import</span> HttpResponseServerError
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> Server<span style="color: black;">&#40;</span><span style="color: #008000;">object</span><span style="color: black;">&#41;</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><span style="color: black;">&#41;</span>:
        <span style="color: #008000;">self</span>.<span style="color: black;">base_dir</span> = <span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">abspath</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">os</span>.<span style="color: black;">getcwd</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>, <span style="color: #483d8b;">&quot;cpdjango&quot;</span><span style="color: black;">&#41;</span>
&nbsp;
        conf_path = <span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">base_dir</span>, <span style="color: #483d8b;">&quot;..&quot;</span>, <span style="color: #483d8b;">&quot;server.cfg&quot;</span><span style="color: black;">&#41;</span>
        cherrypy.<span style="color: black;">config</span>.<span style="color: black;">update</span><span style="color: black;">&#40;</span>conf_path<span style="color: black;">&#41;</span>
&nbsp;
        <span style="color: #808080; font-style: italic;"># This registers a plugin to handle the Django app</span>
        <span style="color: #808080; font-style: italic;"># with the CherryPy engine, meaning the app will</span>
        <span style="color: #808080; font-style: italic;"># play nicely with the process bus that is the engine.</span>
        DjangoAppPlugin<span style="color: black;">&#40;</span>cherrypy.<span style="color: black;">engine</span>, <span style="color: #008000;">self</span>.<span style="color: black;">base_dir</span><span style="color: black;">&#41;</span>.<span style="color: black;">subscribe</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> run<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        engine = cherrypy.<span style="color: black;">engine</span>
        engine.<span style="color: black;">signal_handler</span>.<span style="color: black;">subscribe</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
        <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">hasattr</span><span style="color: black;">&#40;</span>engine, <span style="color: #483d8b;">&quot;console_control_handler&quot;</span><span style="color: black;">&#41;</span>:
            engine.<span style="color: black;">console_control_handler</span>.<span style="color: black;">subscribe</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
        engine.<span style="color: black;">start</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        engine.<span style="color: black;">block</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> DjangoAppPlugin<span style="color: black;">&#40;</span>plugins.<span style="color: black;">SimplePlugin</span><span style="color: black;">&#41;</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>, bus, base_dir<span style="color: black;">&#41;</span>:
        <span style="color: #483d8b;">&quot;&quot;&quot;
        CherryPy engine plugin to configure and mount
        the Django application onto the CherryPy server.
        &quot;&quot;&quot;</span>
        plugins.<span style="color: black;">SimplePlugin</span>.<span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, bus<span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">base_dir</span> = base_dir
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> start<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #008000;">self</span>.<span style="color: black;">bus</span>.<span style="color: black;">log</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Configuring the Django application&quot;</span><span style="color: black;">&#41;</span>
&nbsp;
        <span style="color: #808080; font-style: italic;"># Well this isn't quite as clean as I'd like so</span>
        <span style="color: #808080; font-style: italic;"># feel free to suggest something more appropriate</span>
        <span style="color: #ff7700;font-weight:bold;">from</span> cpdjango.<span style="color: black;">settings</span> <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #66cc66;">*</span>
        app_settings = <span style="color: #008000;">locals</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>.<span style="color: #dc143c;">copy</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">del</span> app_settings<span style="color: black;">&#91;</span><span style="color: #483d8b;">'self'</span><span style="color: black;">&#93;</span>
        settings.<span style="color: black;">configure</span><span style="color: black;">&#40;</span><span style="color: #66cc66;">**</span>app_settings<span style="color: black;">&#41;</span>
&nbsp;
        <span style="color: #008000;">self</span>.<span style="color: black;">bus</span>.<span style="color: black;">log</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Mounting the Django application&quot;</span><span style="color: black;">&#41;</span>
        cherrypy.<span style="color: black;">tree</span>.<span style="color: black;">graft</span><span style="color: black;">&#40;</span>HTTPLogger<span style="color: black;">&#40;</span>WSGIHandler<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
        <span style="color: #008000;">self</span>.<span style="color: black;">bus</span>.<span style="color: black;">log</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Setting up the static directory to be served&quot;</span><span style="color: black;">&#41;</span>
        <span style="color: #808080; font-style: italic;"># We server static files through CherryPy directly</span>
        <span style="color: #808080; font-style: italic;"># bypassing entirely Django</span>
        static_handler = cherrypy.<span style="color: black;">tools</span>.<span style="color: black;">staticdir</span>.<span style="color: black;">handler</span><span style="color: black;">&#40;</span>section=<span style="color: #483d8b;">&quot;/&quot;</span>, <span style="color: #008000;">dir</span>=<span style="color: #483d8b;">&quot;static&quot;</span>,
                                                          root=<span style="color: #008000;">self</span>.<span style="color: black;">base_dir</span><span style="color: black;">&#41;</span>
        cherrypy.<span style="color: black;">tree</span>.<span style="color: black;">mount</span><span style="color: black;">&#40;</span>static_handler, <span style="color: #483d8b;">'/static'</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> HTTPLogger<span style="color: black;">&#40;</span>_cplogging.<span style="color: black;">LogManager</span><span style="color: black;">&#41;</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>, app<span style="color: black;">&#41;</span>:
        _cplogging.<span style="color: black;">LogManager</span>.<span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, <span style="color: #008000;">id</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>, cherrypy.<span style="color: black;">log</span>.<span style="color: black;">logger_root</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">app</span> = app
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__call__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, environ, start_response<span style="color: black;">&#41;</span>:
        <span style="color: #483d8b;">&quot;&quot;&quot;
        Called as part of the WSGI stack to log the incoming request
        and its response using the common log format. If an error bubbles up
        to this middleware, we log it as such.
        &quot;&quot;&quot;</span>
        <span style="color: #ff7700;font-weight:bold;">try</span>:
            response = <span style="color: #008000;">self</span>.<span style="color: black;">app</span><span style="color: black;">&#40;</span>environ, start_response<span style="color: black;">&#41;</span>
            <span style="color: #008000;">self</span>.<span style="color: black;">access</span><span style="color: black;">&#40;</span>environ, response<span style="color: black;">&#41;</span>
            <span style="color: #ff7700;font-weight:bold;">return</span> response
        <span style="color: #ff7700;font-weight:bold;">except</span>:
            <span style="color: #008000;">self</span>.<span style="color: black;">error</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">traceback</span>=<span style="color: #008000;">True</span><span style="color: black;">&#41;</span>
            <span style="color: #ff7700;font-weight:bold;">return</span> HttpResponseServerError<span style="color: black;">&#40;</span>_cperror.<span style="color: black;">format_exc</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> access<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, environ, response<span style="color: black;">&#41;</span>:
        <span style="color: #483d8b;">&quot;&quot;&quot;
        Special method that logs a request following the common
        log format. This is mostly taken from CherryPy and adapted
        to the WSGI's style of passing information.
        &quot;&quot;&quot;</span>
        atoms = <span style="color: black;">&#123;</span><span style="color: #483d8b;">'h'</span>: environ.<span style="color: black;">get</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'REMOTE_ADDR'</span>, <span style="color: #483d8b;">''</span><span style="color: black;">&#41;</span>,
                 <span style="color: #483d8b;">'l'</span>: <span style="color: #483d8b;">'-'</span>,
                 <span style="color: #483d8b;">'u'</span>: <span style="color: #483d8b;">&quot;-&quot;</span>,
                 <span style="color: #483d8b;">'t'</span>: <span style="color: #008000;">self</span>.<span style="color: #dc143c;">time</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>,
                 <span style="color: #483d8b;">'r'</span>: <span style="color: #483d8b;">&quot;%s %s %s&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>environ<span style="color: black;">&#91;</span><span style="color: #483d8b;">'REQUEST_METHOD'</span><span style="color: black;">&#93;</span>, environ<span style="color: black;">&#91;</span><span style="color: #483d8b;">'REQUEST_URI'</span><span style="color: black;">&#93;</span>, environ<span style="color: black;">&#91;</span><span style="color: #483d8b;">'SERVER_PROTOCOL'</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>,
                 <span style="color: #483d8b;">'s'</span>: response.<span style="color: black;">status_code</span>,
                 <span style="color: #483d8b;">'b'</span>: <span style="color: #008000;">str</span><span style="color: black;">&#40;</span><span style="color: #008000;">len</span><span style="color: black;">&#40;</span>response.<span style="color: black;">content</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>,
                 <span style="color: #483d8b;">'f'</span>: environ.<span style="color: black;">get</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'HTTP_REFERER'</span>, <span style="color: #483d8b;">''</span><span style="color: black;">&#41;</span>,
                 <span style="color: #483d8b;">'a'</span>: environ.<span style="color: black;">get</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'HTTP_USER_AGENT'</span>, <span style="color: #483d8b;">''</span><span style="color: black;">&#41;</span>,
                 <span style="color: black;">&#125;</span>
        <span style="color: #ff7700;font-weight:bold;">for</span> k, v <span style="color: #ff7700;font-weight:bold;">in</span> atoms.<span style="color: black;">items</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: #008000;">isinstance</span><span style="color: black;">&#40;</span>v, <span style="color: #008000;">unicode</span><span style="color: black;">&#41;</span>:
                v = v.<span style="color: black;">encode</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'utf8'</span><span style="color: black;">&#41;</span>
            <span style="color: #ff7700;font-weight:bold;">elif</span> <span style="color: #ff7700;font-weight:bold;">not</span> <span style="color: #008000;">isinstance</span><span style="color: black;">&#40;</span>v, <span style="color: #008000;">str</span><span style="color: black;">&#41;</span>:
                v = <span style="color: #008000;">str</span><span style="color: black;">&#40;</span>v<span style="color: black;">&#41;</span>
            <span style="color: #808080; font-style: italic;"># Fortunately, repr(str) escapes unprintable chars, \n, \t, etc</span>
            <span style="color: #808080; font-style: italic;"># and backslash for us. All we have to do is strip the quotes.</span>
            v = <span style="color: #dc143c;">repr</span><span style="color: black;">&#40;</span>v<span style="color: black;">&#41;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span>:-<span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span>
            <span style="color: #808080; font-style: italic;"># Escape double-quote.</span>
            atoms<span style="color: black;">&#91;</span>k<span style="color: black;">&#93;</span> = v.<span style="color: black;">replace</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'&quot;'</span>, <span style="color: #483d8b;">'<span style="color: #000099; font-weight: bold;">\\</span>&quot;'</span><span style="color: black;">&#41;</span>
&nbsp;
        <span style="color: #ff7700;font-weight:bold;">try</span>:
            <span style="color: #008000;">self</span>.<span style="color: black;">access_log</span>.<span style="color: black;">log</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">logging</span>.<span style="color: black;">INFO</span>, <span style="color: #008000;">self</span>.<span style="color: black;">access_log_format</span> <span style="color: #66cc66;">%</span> atoms<span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">except</span>:
            <span style="color: #008000;">self</span>.<span style="color: black;">error</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">traceback</span>=<span style="color: #008000;">True</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">if</span> __name__ == <span style="color: #483d8b;">'__main__'</span>:
    Server<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>.<span style="color: black;">run</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>You can find the code along side a minimal Django application showing how this works <a href="http://www.defuze.org/oss/misc/django-served-by-cherrypy-0.1.0.tgz" target="_blank">here</a> (BSD licence). I used Django 1.3 to generate a default project but the code above works well with older version of Django.</p>

]]></content:encoded>
			<wfw:commentRss>http://www.defuze.org/archives/262-hosting-a-django-application-on-a-cherrypy-server.html/feed</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Should we assess OpenData innovation and impacts?</title>
		<link>http://www.defuze.org/archives/254-should-we-assess-opendata-innovation-and-impacts.html</link>
		<comments>http://www.defuze.org/archives/254-should-we-assess-opendata-innovation-and-impacts.html#comments</comments>
		<pubDate>Fri, 25 Mar 2011 07:31:37 +0000</pubDate>
		<dc:creator>Sylvain Hellegouarch</dc:creator>
				<category><![CDATA[opendata]]></category>

		<guid isPermaLink="false">http://www.defuze.org/?p=254</guid>
		<description><![CDATA[Yesterday, I was at a talk titled: &#8220;OpenData, basis for a new political technologies ?&#8221; at la Cantine Numérique Rennaise [fr], a place about the digital age located in Rennes. During the debate, I asked how we could assess the &#8230; <a href="http://www.defuze.org/archives/254-should-we-assess-opendata-innovation-and-impacts.html">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[
<p>Yesterday, I was at a talk titled: &#8220;OpenData, basis for a new political technologies ?&#8221; at <a href="http://www.lacantine-rennes.net/prog/?event_id=55">la Cantine Numérique Rennaise</a> [fr], a place about the digital age located in <a href="http://en.wikipedia.org/wiki/Rennes">Rennes</a>. During the debate, I asked how we could assess the impacts of OpenData without some sort of measuring instruments. This is question the <a href="http://www.epractice.eu/en/editorial/5290077">EU asked itself</a> in a recent report.</p>
<p><a href="http://twitter.com/xcrouan">Xavier Crouan</a>, who has been digital innovative and Informations director in Rennes for the past few years and has communicated extensively on OpenData, made a comment that I felt was a misunderstanding of my own question. He roughly stated that it felt typically French to request for tools, indicators whenever risks and innovation were taken. He believed this was saddening to hear French engineers being so grounded and felt innovation should not have to justify itself.</p>
<p>Honestly that wasn&#8217;t what I was leading at. The discussion at that point of the debate was about how OpenData would eventually make a difference in people&#8217;s life politically as much as economically. In that context, it seemed sensible to ask how we could measure the impacts of OpenData so that we could tweak, tune, improve its usage.</p>
<p>Now in regards to innovation itself, I believe you usually need <strong>simple</strong> indicators to gauge whether or not you&#8217;re walking onto a fruitful path.</p>
<p>For instance, Rennes has held a contest for building applications on data it has recently opened. Xavier Crouan has indicated that 2000 people had voted. One might consider it is an indicator whether or not the contest was publicly a success and if not, how to tune it if there&#8217;s another contest next year.</p>
<p>Shooting in different directions in hope one path will lead to strong innovation is shortsighted in my book. You need to define a few criteria that will assess how each direction fares. This is what OpenData promotes too: improving efficiency in reusing of public sector data.</p>
<p>Innovation is not incompatible with retrospective.</p>

]]></content:encoded>
			<wfw:commentRss>http://www.defuze.org/archives/254-should-we-assess-opendata-innovation-and-impacts.html/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Thoughts on technical strategies for an OpenData world</title>
		<link>http://www.defuze.org/archives/239-thoughts-on-technical-strategies-for-an-opendata-world.html</link>
		<comments>http://www.defuze.org/archives/239-thoughts-on-technical-strategies-for-an-opendata-world.html#comments</comments>
		<pubDate>Tue, 22 Mar 2011 20:33:48 +0000</pubDate>
		<dc:creator>Sylvain Hellegouarch</dc:creator>
				<category><![CDATA[opendata]]></category>

		<guid isPermaLink="false">http://www.defuze.org/?p=239</guid>
		<description><![CDATA[I&#8217;ve been part of a very interesting discussion on a French forum around some technical aspects of the opening of public data. To provide a bit of context, the French city of Rennes, Brittany, has been leading the way in &#8230; <a href="http://www.defuze.org/archives/239-thoughts-on-technical-strategies-for-an-opendata-world.html">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[
<p>I&#8217;ve been part of a very interesting discussion on a French forum around some technical aspects of the opening of public data. To provide a bit of context, the French city of Rennes, Brittany, has been leading the way in France in 2010 on the subject of <a href="http://www.data.rennes-metropole.fr/" target="_blank">opening its data</a> [fr]. The city has released local authorities and transport datasets. The former are cold datasets served as structured files while the latter is a mix of hot and cold datasets provided through a dedicated <a href="http://data.keolis-rennes.com/fr/les-donnees/donnees-et-api.html" target="_blank">API</a> [fr].</p>
<p>The whole discussion started from that API which is basically a <a href="http://en.wikipedia.org/wiki/Web_service#Remote_procedure_calls" target="_blank">RPC</a> web service that returns data such as:</p>
<ul>
<li>Current number of available bikes at a public bike station</li>
<li>Current number of free slots to park a bike at a station</li>
<li>Bus and subway transit status</li>
<li>Location of travel card/ticket vendors</li>
</ul>
<p><a href="http://www.data.rennes-metropole.fr/vos-idees/vos-forums/?tx_mmforum_pi1[action]=list_post&amp;tx_mmforum_pi1[tid]=49" target="_blank">Someone on the forum</a> [fr] wondered why the API wasn&#8217;t following a more RESTful approach to which I chimed in backing up that idea. Then the discussion went off whether or not REST would bring any added value in comparison to the RPC web service in place. That got me thinking about one side of OpenData that is not too often put ahead, naming the &#8220;how&#8221;. Indeed, mostly, discussions around OpenData deal with the kind of data to release, the reason why and under which conditions.</p>
<p>However it should not be forgotten that releasing public data isn&#8217;t a trivial operation, even when local authorities and other public bodies do have datasets at hand.</p>
<h2>Data source constraints</h2>
<p>By talking to folks working in public agencies you quickly realise that they meet many issues even when they are determined to open up their datasets.</p>
<h3>Old information system or none at all</h3>
<p>It&#8217;s not uncommon that local authorities don&#8217;t have a proper information system. They may deal with documents such as Excel files or their system is just not designed to handled some modern operations.</p>
<h3>Information System Lock-in</h3>
<p>In some cases, namely with <a href="http://en.wikipedia.org/wiki/Geographic_information_system" target="_blank">GIS</a>, local authorities don&#8217;t even really have a way to perform export operations. When that happens it makes the release process more complicated and delay it.</p>
<h3>Unfit data model</h3>
<p>We often cry for more data to be opened but we fail to understand that the model to publicly expose data isn&#8217;t necessarily the one used internally by local authorities.</p>
<h2>Data provider strategies</h2>
<p>One critical question local authorities must ask themselves is whether or not they want to engage into the data/web-service game. In other words, do they want to give an access, as raw as possible to their data and be left alone, or do they want to have control and perhaps create something richer. This is not a light question as the implications can seriously impact the level of commitment a local authority will have to show.</p>
<h3>Flat data provider</h3>
<p>In that configuration the local authority decides to provide the lowest level of features by putting files (flat or structured) on the web for anyone to download. The advantages is the simplicity of deployment and maintenance as well as a very low cost. However this might also create a barrier for non technical folks who would want to provide innovative services as they would be required to process those files first.</p>
<p>This is how Rennes has done for its GIS datasets and, indeed, everyone I met had to perform operations on those files before they could start on their services.</p>
<h3>Raw data web service</h3>
<p>By raw I mean a web service that stays rather close of the dataset it serves. The advantage is the rather simplicity to setup such a service and the low cost it requires. In some cases such a web service might be just enough if the data model exposed by the web service doesn&#8217;t force the developer to create his own intermediary datamodel since, in that case, the web service isn&#8217;t better than flat files.</p>
<p>The city of Rennes uses that kind of service for some of its transit data.</p>
<h3>Rich web service</h3>
<p>Here the provider decides to go the extra mile by offering richer web service. In that case, the local authority defines a much richer data model on top of its own internal data structures. The big advantage is that some common functionalities are provided by the service and allows the developer to build much richer applications too. For instance rather than solely providing a GTFS file for its public transportation a developer must download, parse and create his own model, a provider might offer an API to query that dataset for useful information such as &#8220;what is the fastest way to go from A to B whilst avoiding traffic and with the lowest impact on the environment?&#8221;.</p>
<p>The city of Rennes doesn&#8217;t provide a rich web service  which can be explained by the fact that defining a rich model requires you have a good idea of how the data may be exploited. If you take some of Google&#8217;s APIs, it has taken some time before they reached their current matureness. Moreover Rennes follows the common pattern of &#8220;<a href="http://en.wikipedia.org/wiki/Release_early,_release_often">release often, release early</a>&#8221; and the current API was released so that developers could rapidly start playing with it.</p>
<h2>Data support</h2>
<p>This is an aspect that is almost never approached. Should a local authority be accountable for data integrity, updates, support? The question is rather complex since it begs the question whether or not it&#8217;s within the local authority&#8217;s role to support those functions.</p>
<p>Implicitly this also leads to question whether or not a local authority should become a full fledged data provider. Should we start creating dedicated public, or even private, bodies to perform those tasks?</p>
<h2>Which strategy to follow?</h2>
<p>Eventually the decision is made based on various factors such as the kind of data a local authority owns, how it&#8217;s stored, its accessibility but also a question of cost, time and politics. I strongly believe that not thinking enough about <strong>how</strong> it should be distributed will impair the way the OpenData movement is perceived on the long run. Indeed, let&#8217;s not forget that, at least in France, the biggest consumer of public data are public services themselves and to win the OpenData uptake, the &#8220;how&#8221; must be carefully designed and implemented.</p>
<p>If public data are too complex to consume and process, OpenData will partly fail I believe.</p>

]]></content:encoded>
			<wfw:commentRss>http://www.defuze.org/archives/239-thoughts-on-technical-strategies-for-an-opendata-world.html/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>WebSocket for CherryPy 3.2</title>
		<link>http://www.defuze.org/archives/232-websocket-for-cherrypy-32.html</link>
		<comments>http://www.defuze.org/archives/232-websocket-for-cherrypy-32.html#comments</comments>
		<pubDate>Mon, 22 Nov 2010 21:50:04 +0000</pubDate>
		<dc:creator>Sylvain Hellegouarch</dc:creator>
				<category><![CDATA[python]]></category>
		<category><![CDATA[websockets]]></category>
		<category><![CDATA[cherrypy]]></category>
		<category><![CDATA[websocket]]></category>

		<guid isPermaLink="false">http://www.defuze.org/?p=232</guid>
		<description><![CDATA[Just a quick note about the first draft of support for WebSocket in CherryPy. You can find the code here. Note that this is still work in progress but does work against Chrome and the pywebsocket echo client. It supports &#8230; <a href="http://www.defuze.org/archives/232-websocket-for-cherrypy-32.html">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[
<p>Just a quick note about the first draft of support for WebSocket in <a href="http://www.cherrypy.org/">CherryPy</a>. You can find the code <a href="https://github.com/Lawouach/WebSocket-for-CherryPy" target="_blank">here</a>.</p>
<p>Note that this is still work in progress but does work against Chrome and the <a href="http://code.google.com/p/pywebsocket/">pywebsocket</a> echo client. It supports <a href="http://tools.ietf.org/html/draft-hixie-thewebsocketprotocol-76">draft-76</a> of the specification only and I&#8217;m waiting for the <a href="https://www.ietf.org/mailman/listinfo/hybi">working-group</a> to settle a bit more before making any further modification.</p>
<p>The updated code has started integrating draft-06 as well but this is a work in progress.</p>

]]></content:encoded>
			<wfw:commentRss>http://www.defuze.org/archives/232-websocket-for-cherrypy-32.html/feed</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>Running CherryPy on Android with SL4A</title>
		<link>http://www.defuze.org/archives/228-running-cherrypy-on-android-with-sl4a.html</link>
		<comments>http://www.defuze.org/archives/228-running-cherrypy-on-android-with-sl4a.html#comments</comments>
		<pubDate>Mon, 23 Aug 2010 20:05:12 +0000</pubDate>
		<dc:creator>Sylvain Hellegouarch</dc:creator>
				<category><![CDATA[android]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[cherrypy]]></category>
		<category><![CDATA[sl4a]]></category>

		<guid isPermaLink="false">http://www.defuze.org/?p=228</guid>
		<description><![CDATA[CherryPy runs on Android thanks to the SL4A project. So if you feel like running Python and your own web server on your Android device, well you can just do so. You&#8217;ve probably not heard something that awesome since the &#8230; <a href="http://www.defuze.org/archives/228-running-cherrypy-on-android-with-sl4a.html">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[
<p><a href="http://www.cherrypy.org">CherryPy</a> runs on <a href="http://developer.android.com" target="_blank">Android</a> thanks to the <a href="http://code.google.com/p/android-scripting/" target="_blank">SL4A</a> project. So if you feel like running Python and your own web server on your Android device, well you can just do so. You&#8217;ve probably not heard something that awesome since the pizza delivery guy rung the door.</p>
<p>How to get on about it? Well that&#8217;s the surprise, CherryPy in itself doesn&#8217;t need to be patched. Granted I haven&#8217;t tried all the various tools provided by CherryPy but the server and the dispatching works just fine.</p>
<p>First, you need get the CherryPy source code, build and copy the resulting cherrypy package into the SL4A scripts directory.</p>
<p>Once you&#8217;ve plugged your phone to your machine through USB, run the next commands:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">svn</span> <span style="color: #c20cb9; font-weight: bold;">co</span> http:<span style="color: #000000; font-weight: bold;">//</span>svn.cherrypy.org<span style="color: #000000; font-weight: bold;">/</span>trunk cp3-trunk
$ <span style="color: #7a0874; font-weight: bold;">cd</span> cp3-trunk
$ python setup.py build
$ <span style="color: #c20cb9; font-weight: bold;">cp</span> <span style="color: #660033;">-r</span> build<span style="color: #000000; font-weight: bold;">/</span>lib.linux-i686-<span style="color: #000000;">2.6</span><span style="color: #000000; font-weight: bold;">/</span>cherrypy<span style="color: #000000; font-weight: bold;">/</span> <span style="color: #000000; font-weight: bold;">/</span>media<span style="color: #000000; font-weight: bold;">/</span>usb0<span style="color: #000000; font-weight: bold;">/</span>sl4a<span style="color: #000000; font-weight: bold;">/</span>scripts<span style="color: #000000; font-weight: bold;">/</span></pre></div></div>

<p>Just change the path to match your environment.Â That&#8217;s it.</p>
<p>Now you can copy your own script, let&#8217;s assume you use something like below:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;"># -*- coding: utf-8 -*-</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">logging</span>
<span style="color: #808080; font-style: italic;"># The multiprocessing package isn't</span>
<span style="color: #808080; font-style: italic;"># part of the ASE installation so</span>
<span style="color: #808080; font-style: italic;"># we must disable multiprocessing logging</span>
<span style="color: #dc143c;">logging</span>.<span style="color: black;">logMultiprocessing</span> = <span style="color: #ff4500;">0</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">import</span> android
<span style="color: #ff7700;font-weight:bold;">import</span> cherrypy
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> Root<span style="color: black;">&#40;</span><span style="color: #008000;">object</span><span style="color: black;">&#41;</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><span style="color: black;">&#41;</span>:
        <span style="color: #008000;">self</span>.<span style="color: black;">droid</span> = android.<span style="color: black;">Android</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
    @cherrypy.<span style="color: black;">expose</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> index<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #008000;">self</span>.<span style="color: black;">droid</span>.<span style="color: black;">vibrate</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #483d8b;">&quot;Hello from my phone&quot;</span>
&nbsp;
    @cherrypy.<span style="color: black;">expose</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> location<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        location = <span style="color: #008000;">self</span>.<span style="color: black;">droid</span>.<span style="color: black;">getLastKnownLocation</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>.<span style="color: black;">result</span>
        location = location.<span style="color: black;">get</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'network'</span>, location.<span style="color: black;">get</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'gps'</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #483d8b;">&quot;LAT: %s, LON: %s&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>location<span style="color: black;">&#91;</span><span style="color: #483d8b;">'latitude'</span><span style="color: black;">&#93;</span>,
                                     location<span style="color: black;">&#91;</span><span style="color: #483d8b;">'longitude'</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> run<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:
    cherrypy.<span style="color: black;">config</span>.<span style="color: black;">update</span><span style="color: black;">&#40;</span><span style="color: black;">&#123;</span><span style="color: #483d8b;">'server.socket_host'</span>: <span style="color: #483d8b;">'0.0.0.0'</span><span style="color: black;">&#125;</span><span style="color: black;">&#41;</span>
    cherrypy.<span style="color: black;">quickstart</span><span style="color: black;">&#40;</span>Root<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>, <span style="color: #483d8b;">'/'</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">if</span> __name__ == <span style="color: #483d8b;">'__main__'</span>:
    run<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>As you can see we must disable the multiprocessing logging since the multiprocessing package isn&#8217;t included with SL4A. </p>
<p>Save that script on your computer as cpdroid.py for example. Copy that file into the scripts directory of SL4A.</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">cp</span> cpdroid.py <span style="color: #000000; font-weight: bold;">/</span>media<span style="color: #000000; font-weight: bold;">/</span>usb0<span style="color: #000000; font-weight: bold;">/</span>sl4a<span style="color: #000000; font-weight: bold;">/</span>scripts<span style="color: #000000; font-weight: bold;">/</span></pre></div></div>

<p>Unplug your phone and go to the SL4A application. Click on the cpdroid.py script, it should start fine. Then from your browser, go to http://phone_IP:8080/ and tada! You can also go to the /location path to get the geoloc of your phone.</p>

]]></content:encoded>
			<wfw:commentRss>http://www.defuze.org/archives/228-running-cherrypy-on-android-with-sl4a.html/feed</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Integrating SQLAlchemy into a CherryPy application</title>
		<link>http://www.defuze.org/archives/222-integrating-sqlalchemy-into-a-cherrypy-application.html</link>
		<comments>http://www.defuze.org/archives/222-integrating-sqlalchemy-into-a-cherrypy-application.html#comments</comments>
		<pubDate>Sat, 31 Jul 2010 20:03:50 +0000</pubDate>
		<dc:creator>Sylvain Hellegouarch</dc:creator>
				<category><![CDATA[python]]></category>
		<category><![CDATA[cherrypy]]></category>

		<guid isPermaLink="false">http://www.defuze.org/?p=222</guid>
		<description><![CDATA[Quite often, people come on the CherryPy IRC channel asking about the way to use SQLAlchemy with CherryPy. There are a couple of good recipes on the tools wiki but I find them a little complex to begin with. Not &#8230; <a href="http://www.defuze.org/archives/222-integrating-sqlalchemy-into-a-cherrypy-application.html">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[
<p>Quite often, people come on the <a href="http://www.cherrypy.org/wiki/CherryPyIrcChannel">CherryPy IRC channel</a> asking about the way to use SQLAlchemy with CherryPy. There are a <a href="http://code.google.com/p/webpyte/source/browse/trunk/webpyte/sqlalchemy_tool.py">couple</a> of good <a href="http://tools.cherrypy.org/wiki/SQLA">recipes</a> on the tools wiki but I find them a little complex to begin with. Not to the recipes&#8217; fault, many people don&#8217;t necessarily know about CherryPy <a href="http://www.cherrypy.org/wiki/CustomTools">tools</a> and <a href="http://www.cherrypy.org/wiki/CustomPlugins">plugins</a> at that stage.</p>
<p>The following recipe will try to make the example complete whilst as simple as possible to allow folks to start up with <a href="http://www.sqlalchemy.org/">SQLAlchemy</a> and <a href="http://www.cherrypy.org/">CherryPy</a>.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;"># -*- coding: utf-8 -*-</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">os</span>, <span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">import</span> cherrypy
<span style="color: #ff7700;font-weight:bold;">from</span> cherrypy.<span style="color: black;">process</span> <span style="color: #ff7700;font-weight:bold;">import</span> wspbus, plugins
&nbsp;
<span style="color: #ff7700;font-weight:bold;">from</span> sqlalchemy <span style="color: #ff7700;font-weight:bold;">import</span> create_engine
<span style="color: #ff7700;font-weight:bold;">from</span> sqlalchemy.<span style="color: black;">orm</span> <span style="color: #ff7700;font-weight:bold;">import</span> scoped_session, sessionmaker
<span style="color: #ff7700;font-weight:bold;">from</span> sqlalchemy.<span style="color: black;">ext</span>.<span style="color: black;">declarative</span> <span style="color: #ff7700;font-weight:bold;">import</span> declarative_base
<span style="color: #ff7700;font-weight:bold;">from</span> sqlalchemy <span style="color: #ff7700;font-weight:bold;">import</span> Column
<span style="color: #ff7700;font-weight:bold;">from</span> sqlalchemy.<span style="color: #dc143c;">types</span> <span style="color: #ff7700;font-weight:bold;">import</span> String, Integer
&nbsp;
<span style="color: #808080; font-style: italic;"># Helper to map and register a Python class a db table</span>
Base = declarative_base<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> Message<span style="color: black;">&#40;</span>Base<span style="color: black;">&#41;</span>:
    __tablename__ = <span style="color: #483d8b;">'message'</span>
    <span style="color: #008000;">id</span> = Column<span style="color: black;">&#40;</span>Integer, primary_key=<span style="color: #008000;">True</span><span style="color: black;">&#41;</span>
    value =  Column<span style="color: black;">&#40;</span>String<span style="color: black;">&#41;</span>
&nbsp;
    <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>, message<span style="color: black;">&#41;</span>:
        Base.<span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">value</span> = message
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__str__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">self</span>.<span style="color: black;">value</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: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__unicode__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">self</span>.<span style="color: black;">value</span>
&nbsp;
    @<span style="color: #008000;">staticmethod</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #008000;">list</span><span style="color: black;">&#40;</span>session<span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> session.<span style="color: black;">query</span><span style="color: black;">&#40;</span>Message<span style="color: black;">&#41;</span>.<span style="color: #008000;">all</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> SAEnginePlugin<span style="color: black;">&#40;</span>plugins.<span style="color: black;">SimplePlugin</span><span style="color: black;">&#41;</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>, bus<span style="color: black;">&#41;</span>:
        <span style="color: #483d8b;">&quot;&quot;&quot;
        The plugin is registered to the CherryPy engine and therefore
        is part of the bus (the engine *is* a bus) registery.
&nbsp;
        We use this plugin to create the SA engine. At the same time,
        when the plugin starts we create the tables into the database
        using the mapped class of the global metadata.
&nbsp;
        Finally we create a new 'bind' channel that the SA tool
        will use to map a session to the SA engine at request time.
        &quot;&quot;&quot;</span>
        plugins.<span style="color: black;">SimplePlugin</span>.<span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, bus<span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">sa_engine</span> = <span style="color: #008000;">None</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">bus</span>.<span style="color: black;">subscribe</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;bind&quot;</span>, <span style="color: #008000;">self</span>.<span style="color: black;">bind</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> start<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        db_path = <span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">abspath</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">os</span>.<span style="color: black;">curdir</span>, <span style="color: #483d8b;">'my.db'</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">sa_engine</span> = create_engine<span style="color: black;">&#40;</span><span style="color: #483d8b;">'sqlite:///%s'</span> <span style="color: #66cc66;">%</span> db_path, echo=<span style="color: #008000;">True</span><span style="color: black;">&#41;</span>
        Base.<span style="color: black;">metadata</span>.<span style="color: black;">create_all</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">sa_engine</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> stop<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #008000;">self</span>.<span style="color: black;">sa_engine</span>:
            <span style="color: #008000;">self</span>.<span style="color: black;">sa_engine</span>.<span style="color: black;">dispose</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
            <span style="color: #008000;">self</span>.<span style="color: black;">sa_engine</span> = <span style="color: #008000;">None</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> bind<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, session<span style="color: black;">&#41;</span>:
        session.<span style="color: black;">configure</span><span style="color: black;">&#40;</span>bind=<span style="color: #008000;">self</span>.<span style="color: black;">sa_engine</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> SATool<span style="color: black;">&#40;</span>cherrypy.<span style="color: black;">Tool</span><span style="color: black;">&#41;</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><span style="color: black;">&#41;</span>:
        <span style="color: #483d8b;">&quot;&quot;&quot;
        The SA tool is responsible for associating a SA session
        to the SA engine and attaching it to the current request.
        Since we are running in a multithreaded application,
        we use the scoped_session that will create a session
        on a per thread basis so that you don't worry about
        concurrency on the session object itself.
&nbsp;
        This tools binds a session to the engine each time
        a requests starts and commits/rollbacks whenever
        the request terminates.
        &quot;&quot;&quot;</span>
        cherrypy.<span style="color: black;">Tool</span>.<span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, <span style="color: #483d8b;">'on_start_resource'</span>,
                               <span style="color: #008000;">self</span>.<span style="color: black;">bind_session</span>,
                               priority=<span style="color: #ff4500;">20</span><span style="color: black;">&#41;</span>
&nbsp;
        <span style="color: #008000;">self</span>.<span style="color: black;">session</span> = scoped_session<span style="color: black;">&#40;</span>sessionmaker<span style="color: black;">&#40;</span>autoflush=<span style="color: #008000;">True</span>,
                                                  autocommit=<span style="color: #008000;">False</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> _setup<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        cherrypy.<span style="color: black;">Tool</span>._setup<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>
        cherrypy.<span style="color: black;">request</span>.<span style="color: black;">hooks</span>.<span style="color: black;">attach</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'on_end_resource'</span>,
                                      <span style="color: #008000;">self</span>.<span style="color: black;">commit_transaction</span>,
                                      priority=<span style="color: #ff4500;">80</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> bind_session<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        cherrypy.<span style="color: black;">engine</span>.<span style="color: black;">publish</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'bind'</span>, <span style="color: #008000;">self</span>.<span style="color: black;">session</span><span style="color: black;">&#41;</span>
        cherrypy.<span style="color: black;">request</span>.<span style="color: black;">db</span> = <span style="color: #008000;">self</span>.<span style="color: black;">session</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> commit_transaction<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        cherrypy.<span style="color: black;">request</span>.<span style="color: black;">db</span> = <span style="color: #008000;">None</span>
        <span style="color: #ff7700;font-weight:bold;">try</span>:
            <span style="color: #008000;">self</span>.<span style="color: black;">session</span>.<span style="color: black;">commit</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">except</span>:
            <span style="color: #008000;">self</span>.<span style="color: black;">session</span>.<span style="color: black;">rollback</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>  
            <span style="color: #ff7700;font-weight:bold;">raise</span>
        <span style="color: #ff7700;font-weight:bold;">finally</span>:
            <span style="color: #008000;">self</span>.<span style="color: black;">session</span>.<span style="color: black;">remove</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
&nbsp;
&nbsp;
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> Root<span style="color: black;">&#40;</span><span style="color: #008000;">object</span><span style="color: black;">&#41;</span>:
    @cherrypy.<span style="color: black;">expose</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> index<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #808080; font-style: italic;"># print all the recorded messages so far</span>
        msgs = <span style="color: black;">&#91;</span><span style="color: #008000;">str</span><span style="color: black;">&#40;</span>msg<span style="color: black;">&#41;</span> <span style="color: #ff7700;font-weight:bold;">for</span> msg <span style="color: #ff7700;font-weight:bold;">in</span> Message.<span style="color: #008000;">list</span><span style="color: black;">&#40;</span>cherrypy.<span style="color: black;">request</span>.<span style="color: black;">db</span><span style="color: black;">&#41;</span><span style="color: black;">&#93;</span>
        cherrypy.<span style="color: black;">response</span>.<span style="color: black;">headers</span><span style="color: black;">&#91;</span><span style="color: #483d8b;">'content-type'</span><span style="color: black;">&#93;</span> = <span style="color: #483d8b;">'text/plain'</span>
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #483d8b;">&quot;Here are your list of messages: %s&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: #483d8b;">'<span style="color: #000099; font-weight: bold;">\n</span>'</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span>msgs<span style="color: black;">&#41;</span>
&nbsp;
    @cherrypy.<span style="color: black;">expose</span>
    <span style="color: #ff7700;font-weight:bold;">def</span> record<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, msg<span style="color: black;">&#41;</span>:
        <span style="color: #808080; font-style: italic;"># go to /record?msg=hello world to record a &quot;hello world&quot; message</span>
        m = Message<span style="color: black;">&#40;</span>msg<span style="color: black;">&#41;</span>
        cherrypy.<span style="color: black;">request</span>.<span style="color: black;">db</span>.<span style="color: black;">add</span><span style="color: black;">&#40;</span>m<span style="color: black;">&#41;</span>
        cherrypy.<span style="color: black;">response</span>.<span style="color: black;">headers</span><span style="color: black;">&#91;</span><span style="color: #483d8b;">'content-type'</span><span style="color: black;">&#93;</span> = <span style="color: #483d8b;">'text/plain'</span>
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #483d8b;">&quot;Recorded: %s&quot;</span> <span style="color: #66cc66;">%</span> m
&nbsp;
<span style="color: #ff7700;font-weight:bold;">if</span> __name__ == <span style="color: #483d8b;">'__main__'</span>:
    SAEnginePlugin<span style="color: black;">&#40;</span>cherrypy.<span style="color: black;">engine</span><span style="color: black;">&#41;</span>.<span style="color: black;">subscribe</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    cherrypy.<span style="color: black;">tools</span>.<span style="color: black;">db</span> = SATool<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    cherrypy.<span style="color: black;">tree</span>.<span style="color: black;">mount</span><span style="color: black;">&#40;</span>Root<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>, <span style="color: #483d8b;">'/'</span>, <span style="color: black;">&#123;</span><span style="color: #483d8b;">'/'</span>: <span style="color: black;">&#123;</span><span style="color: #483d8b;">'tools.db.on'</span>: <span style="color: #008000;">True</span><span style="color: black;">&#125;</span><span style="color: black;">&#125;</span><span style="color: black;">&#41;</span>
    cherrypy.<span style="color: black;">engine</span>.<span style="color: black;">start</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    cherrypy.<span style="color: black;">engine</span>.<span style="color: black;">block</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>The general idea is to use the plugin mechanism to register functions on an engine basis and enable a tool that will provide an access to the SQLAlchemy session at request time.</p>

]]></content:encoded>
			<wfw:commentRss>http://www.defuze.org/archives/222-integrating-sqlalchemy-into-a-cherrypy-application.html/feed</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Using Jython as a CLI frontend to HBase</title>
		<link>http://www.defuze.org/archives/216-using-jython-as-a-cli-frontend-to-hbase.html</link>
		<comments>http://www.defuze.org/archives/216-using-jython-as-a-cli-frontend-to-hbase.html#comments</comments>
		<pubDate>Sun, 27 Jun 2010 13:39:58 +0000</pubDate>
		<dc:creator>Sylvain Hellegouarch</dc:creator>
				<category><![CDATA[hbase]]></category>
		<category><![CDATA[headstock]]></category>
		<category><![CDATA[jython]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[cherrypy]]></category>

		<guid isPermaLink="false">http://www.defuze.org/?p=216</guid>
		<description><![CDATA[HBase, the well known non-relational distributed database, comes with a console program to perform various operations on a HBase cluster. I&#8217;ve personally found this tool to be a bit limited and I&#8217;ve toyed around the idea of writing my own. &#8230; <a href="http://www.defuze.org/archives/216-using-jython-as-a-cli-frontend-to-hbase.html">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[
<p><a href="http://hbase.apache.org/">HBase</a>, the well known non-relational distributed database, comes with a console program to perform various operations on a HBase cluster. I&#8217;ve personally found this tool to be a bit limited and I&#8217;ve toyed around the idea of writing my own. Since HBase only comes with a Java driver for direct access and the various RPC interfaces such as <a href="http://wiki.apache.org/hadoop/Hbase/ThriftApi">Thrift</a> don&#8217;t offer the full set of functions over HBase, I decided to go for <a href="http://www.jython.org/">Jython</a> and to directly use the Java API. This article will show a mock-up of such a tool.</p>
<p>The idea is to provide a simple Python API over the HBase one and couple it with a Python interpreter. This means, it offers the possibility to perform any Python (well Jython) operations whilst operating on HBase itself with an easier API than the Java one.</p>
<p>Note also that the tool uses the WSPBus already described in <a href="http://www.defuze.org/archives/198-managing-your-process-with-the-cherrypy-bus.html">an earlier article</a> to control the process itself. You will therefore need <a href="http://www.cherrypy.org">CherryPy&#8217;s latest revision</a>.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #808080; font-style: italic;"># -*- coding: utf-8 -*-</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">sys</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">os</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">code</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">readline</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">rlcompleter</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">from</span> org.<span style="color: black;">apache</span>.<span style="color: black;">hadoop</span>.<span style="color: black;">hbase</span> <span style="color: #ff7700;font-weight:bold;">import</span> HBaseConfiguration, \
     HTableDescriptor, HColumnDescriptor
<span style="color: #ff7700;font-weight:bold;">from</span> org.<span style="color: black;">apache</span>.<span style="color: black;">hadoop</span>.<span style="color: black;">hbase</span>.<span style="color: black;">client</span> <span style="color: #ff7700;font-weight:bold;">import</span> HBaseAdmin, \
     HTable, Put, Get, Scan
&nbsp;
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">logging</span>
<span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">logging</span> <span style="color: #ff7700;font-weight:bold;">import</span> handlers
&nbsp;
<span style="color: #ff7700;font-weight:bold;">from</span> cherrypy.<span style="color: black;">process</span> <span style="color: #ff7700;font-weight:bold;">import</span> wspbus
<span style="color: #ff7700;font-weight:bold;">from</span> cherrypy.<span style="color: black;">process</span> <span style="color: #ff7700;font-weight:bold;">import</span> plugins
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> StaveBus<span style="color: black;">&#40;</span>wspbus.<span style="color: black;">Bus</span><span style="color: black;">&#41;</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><span style="color: black;">&#41;</span>:
        wspbus.<span style="color: black;">Bus</span>.<span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">open_logger</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">subscribe</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;log&quot;</span>, <span style="color: #008000;">self</span>._log<span style="color: black;">&#41;</span>
&nbsp;
        sig = plugins.<span style="color: black;">SignalHandler</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #dc143c;">sys</span>.<span style="color: #dc143c;">platform</span><span style="color: black;">&#91;</span>:<span style="color: #ff4500;">4</span><span style="color: black;">&#93;</span> == <span style="color: #483d8b;">'java'</span>:
            <span style="color: #ff7700;font-weight:bold;">del</span> sig.<span style="color: black;">handlers</span><span style="color: black;">&#91;</span><span style="color: #483d8b;">'SIGUSR1'</span><span style="color: black;">&#93;</span>
            sig.<span style="color: black;">handlers</span><span style="color: black;">&#91;</span><span style="color: #483d8b;">'SIGUSR2'</span><span style="color: black;">&#93;</span> = <span style="color: #008000;">self</span>.<span style="color: black;">graceful</span>
            <span style="color: #008000;">self</span>.<span style="color: black;">log</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;SIGUSR1 cannot be set on the JVM platform. Using SIGUSR2 instead.&quot;</span><span style="color: black;">&#41;</span>
&nbsp;
            <span style="color: #808080; font-style: italic;"># See http://bugs.jython.org/issue1313</span>
            sig.<span style="color: black;">handlers</span><span style="color: black;">&#91;</span><span style="color: #483d8b;">'SIGINT'</span><span style="color: black;">&#93;</span> = <span style="color: #008000;">self</span>._jython_handle_SIGINT
        sig.<span style="color: black;">subscribe</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> exit<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        wspbus.<span style="color: black;">Bus</span>.<span style="color: black;">exit</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">close_logger</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> open_logger<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, name=<span style="color: #483d8b;">&quot;&quot;</span><span style="color: black;">&#41;</span>:
        logger = <span style="color: #dc143c;">logging</span>.<span style="color: black;">getLogger</span><span style="color: black;">&#40;</span>name<span style="color: black;">&#41;</span>
        logger.<span style="color: black;">setLevel</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">logging</span>.<span style="color: black;">INFO</span><span style="color: black;">&#41;</span>
        h = <span style="color: #dc143c;">logging</span>.<span style="color: black;">StreamHandler</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">sys</span>.<span style="color: black;">stdout</span><span style="color: black;">&#41;</span>
        h.<span style="color: black;">setLevel</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">logging</span>.<span style="color: black;">INFO</span><span style="color: black;">&#41;</span>
        h.<span style="color: black;">setFormatter</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">logging</span>.<span style="color: black;">Formatter</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;[%(asctime)s] %(name)s - %(levelname)s - %(message)s&quot;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
        logger.<span style="color: black;">addHandler</span><span style="color: black;">&#40;</span>h<span style="color: black;">&#41;</span>
&nbsp;
        <span style="color: #008000;">self</span>.<span style="color: black;">logger</span> = logger
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> close_logger<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">for</span> handler <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">self</span>.<span style="color: black;">logger</span>.<span style="color: black;">handlers</span>:
            handler.<span style="color: black;">flush</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
            handler.<span style="color: black;">close</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> _log<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, msg=<span style="color: #483d8b;">&quot;&quot;</span>, level=<span style="color: #dc143c;">logging</span>.<span style="color: black;">INFO</span><span style="color: black;">&#41;</span>:
        <span style="color: #008000;">self</span>.<span style="color: black;">logger</span>.<span style="color: black;">log</span><span style="color: black;">&#40;</span>level, msg<span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> _jython_handle_SIGINT<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, signum=<span style="color: #008000;">None</span>, frame=<span style="color: #008000;">None</span><span style="color: black;">&#41;</span>:
        <span style="color: #808080; font-style: italic;"># See http://bugs.jython.org/issue1313</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">log</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'Keyboard Interrupt: shutting down bus'</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">exit</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> HbaseConsolePlugin<span style="color: black;">&#40;</span>plugins.<span style="color: black;">SimplePlugin</span><span style="color: black;">&#41;</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>, bus<span style="color: black;">&#41;</span>:
        plugins.<span style="color: black;">SimplePlugin</span>.<span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, bus<span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">console</span> = HbaseConsole<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> start<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #008000;">self</span>.<span style="color: black;">console</span>.<span style="color: black;">setup</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">console</span>.<span style="color: black;">run</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> HbaseConsole<span style="color: black;">&#40;</span><span style="color: #008000;">object</span><span style="color: black;">&#41;</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><span style="color: black;">&#41;</span>:
        <span style="color: #808080; font-style: italic;"># we provide this instance to the underlying interpreter</span>
        <span style="color: #808080; font-style: italic;"># as the interface to operate on HBase</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">namespace</span> = <span style="color: black;">&#123;</span><span style="color: #483d8b;">'c'</span>: HbaseCommand<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><span style="color: black;">&#125;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> setup<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #dc143c;">readline</span>.<span style="color: black;">set_completer</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">rlcompleter</span>.<span style="color: black;">Completer</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">namespace</span><span style="color: black;">&#41;</span>.<span style="color: black;">complete</span><span style="color: black;">&#41;</span>
        <span style="color: #dc143c;">readline</span>.<span style="color: black;">parse_and_bind</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;tab:complete&quot;</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">user</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> run<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #dc143c;">code</span>.<span style="color: black;">interact</span><span style="color: black;">&#40;</span>local=<span style="color: #008000;">self</span>.<span style="color: black;">namespace</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> HbaseCommand<span style="color: black;">&#40;</span><span style="color: #008000;">object</span><span style="color: black;">&#41;</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>, conf=<span style="color: #008000;">None</span>, admin=<span style="color: #008000;">None</span><span style="color: black;">&#41;</span>:
        <span style="color: #008000;">self</span>.<span style="color: black;">conf</span> = conf
        <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff7700;font-weight:bold;">not</span> conf:
            <span style="color: #008000;">self</span>.<span style="color: black;">conf</span> = HBaseConfiguration<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">admin</span> = admin
        <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff7700;font-weight:bold;">not</span> admin:
            <span style="color: #008000;">self</span>.<span style="color: black;">admin</span> = HBaseAdmin<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">conf</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> table<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, name<span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> HTableCommand<span style="color: black;">&#40;</span>name, <span style="color: #008000;">self</span>.<span style="color: black;">conf</span>, <span style="color: #008000;">self</span>.<span style="color: black;">admin</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> list_tables<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">self</span>.<span style="color: black;">admin</span>.<span style="color: black;">listTables</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>.<span style="color: black;">tolist</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> HTableCommand<span style="color: black;">&#40;</span><span style="color: #008000;">object</span><span style="color: black;">&#41;</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>, name, conf, admin<span style="color: black;">&#41;</span>:
        <span style="color: #008000;">self</span>.<span style="color: black;">conf</span> = conf
        <span style="color: #008000;">self</span>.<span style="color: black;">admin</span> = admin
        <span style="color: #008000;">self</span>.<span style="color: black;">name</span> = name
        <span style="color: #008000;">self</span>._table = <span style="color: #008000;">None</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> row<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, name<span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff7700;font-weight:bold;">not</span> <span style="color: #008000;">self</span>._table:
            <span style="color: #008000;">self</span>._table = HTable<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">conf</span>, <span style="color: #008000;">self</span>.<span style="color: black;">name</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">return</span> HRowCommand<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>._table, name<span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> create<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, families=<span style="color: #008000;">None</span><span style="color: black;">&#41;</span>:
        desc = HTableDescriptor<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">name</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">if</span> families:
            <span style="color: #ff7700;font-weight:bold;">for</span> family <span style="color: #ff7700;font-weight:bold;">in</span> families:
                desc.<span style="color: black;">addFamily</span><span style="color: black;">&#40;</span>HColumnDescriptor<span style="color: black;">&#40;</span>family<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">admin</span>.<span style="color: black;">createTable</span><span style="color: black;">&#40;</span>desc<span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>._table = HTable<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">conf</span>, <span style="color: #008000;">self</span>.<span style="color: black;">name</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">self</span>._table
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> scan<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, start_row=<span style="color: #008000;">None</span>, end_row=<span style="color: #008000;">None</span>, <span style="color: #008000;">filter</span>=<span style="color: #008000;">None</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">if</span> <span style="color: #ff7700;font-weight:bold;">not</span> <span style="color: #008000;">self</span>._table:
            <span style="color: #008000;">self</span>._table = HTable<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">conf</span>, <span style="color: #008000;">self</span>.<span style="color: black;">name</span><span style="color: black;">&#41;</span>
&nbsp;
        sc = <span style="color: #008000;">None</span>
        <span style="color: #ff7700;font-weight:bold;">if</span> start_row <span style="color: #ff7700;font-weight:bold;">and</span> <span style="color: #008000;">filter</span>:
            sc = Scan<span style="color: black;">&#40;</span>start_row, <span style="color: #008000;">filter</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">elif</span> start_row <span style="color: #ff7700;font-weight:bold;">and</span> end_row:
            sc = Scan<span style="color: black;">&#40;</span>start_row, end_row<span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">elif</span> start_row:
            sc = Scan<span style="color: black;">&#40;</span>start_row<span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">else</span>:
            sc = Scan<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        s = <span style="color: #008000;">self</span>._table.<span style="color: black;">getScanner</span><span style="color: black;">&#40;</span>sc<span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">while</span> <span style="color: #008000;">True</span>:
            r = s.<span style="color: black;">next</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
            <span style="color: #ff7700;font-weight:bold;">if</span> r <span style="color: #ff7700;font-weight:bold;">is</span> <span style="color: #008000;">None</span>:
                <span style="color: #ff7700;font-weight:bold;">raise</span> <span style="color: #008000;">StopIteration</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
            <span style="color: #ff7700;font-weight:bold;">yield</span> r
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> delete<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #008000;">self</span>.<span style="color: black;">disable</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">admin</span>.<span style="color: black;">deleteTable</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">name</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> disable<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #008000;">self</span>.<span style="color: black;">admin</span>.<span style="color: black;">disableTable</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">name</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> enable<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #008000;">self</span>.<span style="color: black;">admin</span>.<span style="color: black;">enableTable</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">name</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> exists<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">self</span>.<span style="color: black;">admin</span>.<span style="color: black;">tableExists</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">name</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> list_families<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        desc = HTableDescriptor<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">name</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">return</span> desc.<span style="color: black;">getColumnFamilies</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> HRowCommand<span style="color: black;">&#40;</span><span style="color: #008000;">object</span><span style="color: black;">&#41;</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>, table, rowname<span style="color: black;">&#41;</span>:
        <span style="color: #008000;">self</span>.<span style="color: black;">table</span> = table
        <span style="color: #008000;">self</span>.<span style="color: black;">rowname</span> = rowname
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> put<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, family, column, value<span style="color: black;">&#41;</span>:
        p = Put<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">rowname</span><span style="color: black;">&#41;</span>
        p.<span style="color: black;">add</span><span style="color: black;">&#40;</span>family, column, value<span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">table</span>.<span style="color: black;">put</span><span style="color: black;">&#40;</span>p<span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> get<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, family, column<span style="color: black;">&#41;</span>:
        r = <span style="color: #008000;">self</span>.<span style="color: black;">table</span>.<span style="color: black;">get</span><span style="color: black;">&#40;</span>Get<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">rowname</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
        v = r.<span style="color: black;">getValue</span><span style="color: black;">&#40;</span>family, column<span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">if</span> v <span style="color: #ff7700;font-weight:bold;">is</span> <span style="color: #ff7700;font-weight:bold;">not</span> <span style="color: #008000;">None</span>:
            <span style="color: #ff7700;font-weight:bold;">return</span> v.<span style="color: black;">tostring</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
&nbsp;
<span style="color: #ff7700;font-weight:bold;">if</span> __name__ == <span style="color: #483d8b;">'__main__'</span>:
    bus = StaveBus<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    HbaseConsolePlugin<span style="color: black;">&#40;</span>bus<span style="color: black;">&#41;</span>.<span style="color: black;">subscribe</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    bus.<span style="color: black;">start</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    bus.<span style="color: black;">block</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>To test the tool, you can simply grab the latest copy of HBase and run:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">hbase-0.20.4$ .<span style="color: #000000; font-weight: bold;">/</span>bin<span style="color: #000000; font-weight: bold;">/</span>start-hbase.sh</pre></div></div>

<p>Then you need to configure your classpath so that it includes all the HBase dependencies. To determine them:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #c20cb9; font-weight: bold;">ps</span> auwx<span style="color: #000000; font-weight: bold;">|</span><span style="color: #c20cb9; font-weight: bold;">grep</span> java<span style="color: #000000; font-weight: bold;">|</span><span style="color: #c20cb9; font-weight: bold;">grep</span> org.apache.hadoop.hbase.master.HMaster<span style="color: #000000; font-weight: bold;">|</span><span style="color: #c20cb9; font-weight: bold;">perl</span> <span style="color: #660033;">-pi</span> <span style="color: #660033;">-e</span> <span style="color: #ff0000;">&quot;s/.*classpath //&quot;</span></pre></div></div>

<p>Copy the full list of jars and export CLASSPATH with it. (This is from the HBase wiki on <a href="http://wiki.apache.org/hadoop/Hbase/Jython">Jython and HBase</a>).</p>
<p>Next you have to add an extra jar to the classpath so that Jython supports readline:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ <span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">CLASSPATH</span>=<span style="color: #007800;">$CLASSPATH</span>:<span style="color: #007800;">$HOME</span><span style="color: #000000; font-weight: bold;">/</span>jython2.5.1<span style="color: #000000; font-weight: bold;">/</span>extlibs<span style="color: #000000; font-weight: bold;">/</span>libreadline-java-<span style="color: #000000;">0.8</span>.jar</pre></div></div>

<p>Make sure you&#8217;ll install libreadline-java as well.</p>
<p>Now, that your environment is setup, save the code above under a script named stave.py and run it as follow:</p>

<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;">$ jython stave.py
Python 2.5.1 <span style="color: #7a0874; font-weight: bold;">&#40;</span>Release_2_5_1:<span style="color: #000000;">6813</span>, Sep <span style="color: #000000;">26</span> <span style="color: #000000;">2009</span>, <span style="color: #000000;">13</span>:<span style="color: #000000;">47</span>:<span style="color: #000000;">54</span><span style="color: #7a0874; font-weight: bold;">&#41;</span> 
<span style="color: #7a0874; font-weight: bold;">&#91;</span>Java HotSpot<span style="color: #7a0874; font-weight: bold;">&#40;</span>TM<span style="color: #7a0874; font-weight: bold;">&#41;</span> Server VM <span style="color: #7a0874; font-weight: bold;">&#40;</span>Sun Microsystems Inc.<span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color: #7a0874; font-weight: bold;">&#93;</span> on java1.6.0_20
Type <span style="color: #ff0000;">&quot;help&quot;</span>, <span style="color: #ff0000;">&quot;copyright&quot;</span>, <span style="color: #ff0000;">&quot;credits&quot;</span> or <span style="color: #ff0000;">&quot;license&quot;</span> <span style="color: #000000; font-weight: bold;">for</span> <span style="color: #c20cb9; font-weight: bold;">more</span> information.
<span style="color: #7a0874; font-weight: bold;">&#40;</span>InteractiveConsole<span style="color: #7a0874; font-weight: bold;">&#41;</span>
<span style="color: #000000; font-weight: bold;">&gt;&gt;&gt;</span> c.table<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #ff0000;">'myTable'</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>.create<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #007800;">families</span>=<span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #ff0000;">'aFamily:'</span><span style="color: #7a0874; font-weight: bold;">&#93;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
<span style="color: #000000; font-weight: bold;">&gt;&gt;&gt;</span> c.table<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #ff0000;">'myTable'</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>.list_families<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
array<span style="color: #7a0874; font-weight: bold;">&#40;</span>org.apache.hadoop.hbase.HColumnDescriptor<span style="color: #7a0874; font-weight: bold;">&#41;</span>
<span style="color: #000000; font-weight: bold;">&gt;&gt;&gt;</span> c.table<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #ff0000;">'myTable'</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>.row<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #ff0000;">'aRow'</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>.put<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #ff0000;">'aFamily'</span>, <span style="color: #ff0000;">'aColumn'</span>, <span style="color: #ff0000;">'hello world!'</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
<span style="color: #000000; font-weight: bold;">&gt;&gt;&gt;</span> c.table<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #ff0000;">'myTable'</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>.row<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #ff0000;">'aRow'</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>.get<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #ff0000;">'aFamily'</span>, <span style="color: #ff0000;">'aColumn'</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
<span style="color: #ff0000;">'hello world!'</span>
<span style="color: #000000; font-weight: bold;">&gt;&gt;&gt;</span> list<span style="color: #7a0874; font-weight: bold;">&#40;</span>c.table<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #ff0000;">'myTable'</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>.scan<span style="color: #7a0874; font-weight: bold;">&#40;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span><span style="color: #7a0874; font-weight: bold;">&#41;</span>
<span style="color: #7a0874; font-weight: bold;">&#91;</span><span style="color: #007800;">keyvalues</span>=<span style="color: #7a0874; font-weight: bold;">&#123;</span>aRow<span style="color: #000000; font-weight: bold;">/</span>aFamily:aColumn<span style="color: #000000; font-weight: bold;">/</span><span style="color: #000000;">1277645421824</span><span style="color: #000000; font-weight: bold;">/</span>Put<span style="color: #000000; font-weight: bold;">/</span><span style="color: #007800;">vlen</span>=<span style="color: #000000;">12</span><span style="color: #7a0874; font-weight: bold;">&#125;</span><span style="color: #7a0874; font-weight: bold;">&#93;</span></pre></div></div>

<p>You can import any Python module available to your Jython environment as well of course.</p>
<p>I will probably extend this tool over time but in the meantime I hope you&#8217;ll find it a useful canvas to operate HBase.</p>

]]></content:encoded>
			<wfw:commentRss>http://www.defuze.org/archives/216-using-jython-as-a-cli-frontend-to-hbase.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A quick chat WebSockets/AMQP client</title>
		<link>http://www.defuze.org/archives/213-a-quick-chat-websocketsamqp-client.html</link>
		<comments>http://www.defuze.org/archives/213-a-quick-chat-websocketsamqp-client.html#comments</comments>
		<pubDate>Mon, 14 Jun 2010 18:39:22 +0000</pubDate>
		<dc:creator>Sylvain Hellegouarch</dc:creator>
				<category><![CDATA[amqp]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[websockets]]></category>

		<guid isPermaLink="false">http://www.defuze.org/?p=213</guid>
		<description><![CDATA[In my previous article I described how to plug WebSockets into AMQP using Tornado and pika. As a follow-up, I&#8217;ll show you how this can be used to write the simplest chat client. First we create a web handler for &#8230; <a href="http://www.defuze.org/archives/213-a-quick-chat-websocketsamqp-client.html">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[
<p>In my <a href="http://www.defuze.org/archives/207-plugging-amqp-and-websocket.html">previous article</a> I described how to plug WebSockets into AMQP using Tornado and pika. As a follow-up, I&#8217;ll show you how this can be used to write the simplest chat client.</p>
<p>First we create a web handler for Tornado that will return a web page containing the Javascript code that will connect and converse with our WebSockets endpoint following the <a href="http://dev.w3.org/html5/websockets/">WebSockets API</a>.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">class</span> MainHandler<span style="color: black;">&#40;</span>tornado.<span style="color: black;">web</span>.<span style="color: black;">RequestHandler</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">def</span> get<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        username = <span style="color: #483d8b;">&quot;User%d&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: #dc143c;">random</span>.<span style="color: black;">randint</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span>, <span style="color: #ff4500;">100</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">write</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;&quot;&quot;&lt;html&gt;
        &lt;head&gt;
          &lt;script type='application/javascript' src='/static/jquery-1.4.2.min.js'&gt; &lt;/script&gt;
          &lt;script type='application/javascript'&gt;
            $(document).ready(function() {
              var ws = new WebSocket('ws://localdomain.dom:8888/ws');
              ws.onmessage = function (evt) {
                 $('#chat').val($('#chat').val() + evt.data + '<span style="color: #000099; font-weight: bold;">\\</span>n');                  
              };
              $('#chatform').submit(function() {
                 ws.send('%(username)s: ' + $('#message').val());
                 $('#message').val(&quot;&quot;);
                 return false;
              });
            });
          &lt;/script&gt;
        &lt;/head&gt;
        &lt;body&gt;
        &lt;form action='/ws' id='chatform' method='post'&gt;
          &lt;textarea id='chat' cols='35' rows='10'&gt;&lt;/textarea&gt;
          &lt;br /&gt;
          &lt;label for='message'&gt;%(username)s: &lt;/label&gt;&lt;input type='text' id='message' /&gt;
          &lt;input type='submit' value='Send' /&gt;
          &lt;/form&gt;
        &lt;/body&gt;
        &lt;/html&gt;
        &quot;&quot;&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#123;</span><span style="color: #483d8b;">'username'</span>: username<span style="color: black;">&#125;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>Every time, the user enters a message and submits it too our WebSockets endpoint which, in return, will forward any messages back to the client. These will be appended to the textarea.</p>
<p>Internally, each client gets notified of any message through AMQP and the bus. Indeed the WebSockets handler are subscribed to a channel that will be notified every time the AMQP server pushes data to the consumer. A side effect of this is that the Javascript code above doesn&#8217;t update the textarea when it sends the message the user has entered, but when the server sends it back. </p>
<p>Let&#8217;s see how we had to change the Tornado application to support that handler as well as the serving of <a href="http://jQuery">jQuery</a> as a static resource (you need the jQuery toolkit in the same directory as the Python module).</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;">&nbsp;
<span style="color: #ff7700;font-weight:bold;">if</span> __name__ == <span style="color: #483d8b;">'__main__'</span>:
    application = tornado.<span style="color: black;">web</span>.<span style="color: black;">Application</span><span style="color: black;">&#40;</span><span style="color: black;">&#91;</span>
        <span style="color: black;">&#40;</span>r<span style="color: #483d8b;">&quot;/&quot;</span>, MainHandler<span style="color: black;">&#41;</span>,
        <span style="color: black;">&#40;</span>r<span style="color: #483d8b;">&quot;/ws&quot;</span>, WebSocket2AMQP<span style="color: black;">&#41;</span>,
        <span style="color: black;">&#93;</span>, static_path=<span style="color: #483d8b;">&quot;.&quot;</span>, bus=bus<span style="color: black;">&#41;</span>
&nbsp;
    http_server = tornado.<span style="color: black;">httpserver</span>.<span style="color: black;">HTTPServer</span><span style="color: black;">&#40;</span>application<span style="color: black;">&#41;</span>
    http_server.<span style="color: black;">listen</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">8888</span><span style="color: black;">&#41;</span>
&nbsp;
    bus.<span style="color: black;">subscribe</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;main&quot;</span>, poll<span style="color: black;">&#41;</span>
    WS2AMQPPlugin<span style="color: black;">&#40;</span>bus<span style="color: black;">&#41;</span>.<span style="color: black;">subscribe</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    bus.<span style="color: black;">start</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    bus.<span style="color: black;">block</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>The code is <a href="http://pastebin.ca/1883073">here</a>.</p>
<p>Once the server is running, open two browser windows and access http://localhost:8888/. You should be able to type messages in one and see them appears in both windows.</p>
<p><strong>Note:</strong></p>
<p>This has been tested against the latest <a href="http://www.google.com/chrome">Chrome</a> release. You will need to either set the &#8220;localdomain.dom&#8221; or provide the IP address of your network interface in the Javascript above since Chrome doesn&#8217;t allow for localhost nor 127.0.0.1.</p>

]]></content:encoded>
			<wfw:commentRss>http://www.defuze.org/archives/213-a-quick-chat-websocketsamqp-client.html/feed</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
	</channel>
</rss>

