<?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"
	>

<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>
	<pubDate>Mon, 23 Aug 2010 20:05:12 +0000</pubDate>
	<generator>http://wordpress.org/?v=2.6.2</generator>
	<language>en</language>
			<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 pizza delivery guy rung the door.
How to get on about it? Well that&#8217;s the surprise, [...]]]></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>
		</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 to the recipes&#8217; fault, many people don&#8217;t necessarily know about CherryPy tools and plugins at [...]]]></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>
		</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. Since HBase only comes with a Java driver for direct access and the various RPC [...]]]></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-0.8.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>
		</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 Tornado that will return a web page containing the Javascript code that will connect and converse [...]]]></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>
		</item>
		<item>
		<title>Plugging AMQP and WebSockets</title>
		<link>http://www.defuze.org/archives/207-plugging-amqp-and-websocket.html</link>
		<comments>http://www.defuze.org/archives/207-plugging-amqp-and-websocket.html#comments</comments>
		<pubDate>Sat, 12 Jun 2010 17:46:28 +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=207</guid>
		<description><![CDATA[In my last article, I discussed the way the WSPBus could help your management of Python processes. This time, I&#8217;ll show how the bus can help plugging in heterogeneous frameworks and manage them properly too.
The following example will plug the WebSockets and AMQP together in order to channel data in and out of a WebSockets [...]]]></description>
			<content:encoded><![CDATA[<p>In my <a href="http://www.defuze.org/archives/198-managing-your-process-with-the-cherrypy-bus.html" target="_blank">last article</a>, I discussed the way the <a href="http://www.cherrypy.org/wiki/WSPBSpec" target="_blank">WSPBus</a> could help your management of Python processes. This time, I&#8217;ll show how the bus can help plugging in heterogeneous frameworks and manage them properly too.</p>
<p>The following example will plug the <a href="http://en.wikipedia.org/wiki/WebSockets">WebSockets</a> and <a href="http://www.amqp.org">AMQP</a> together in order to channel data in and out of a WebSockets channel into AMQP exchanges and queues. For this, we&#8217;ll be using the <a href="http://github.com/facebook/tornado">Tornado</a> web framework to handle the WebSockets side and <a href="http://github.com/tonyg/pika/">pika</a> for the AMQP one.</p>
<p>pika uses the Python built-in asyncore module to perform the non-blocking socket operations whilst Tornado comes with its own main loop on top of select or poll. Since Tornado doesn&#8217;t offer a single function call to iterate once, we&#8217;ll be directly using their main loop to block the process and therefore won&#8217;t be using the bus&#8217; own block method.</p>
<p>Let&#8217;s see how the bus looks like</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"> <span style="color: #ff7700;font-weight:bold;">class</span> MyBus<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>, name=<span style="color: #483d8b;">&quot;&quot;</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>name<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;
        <span style="color: #008000;">self</span>.<span style="color: black;">ioloop</span> = tornado.<span style="color: black;">ioloop</span>.<span style="color: black;">IOLoop</span>.<span style="color: black;">instance</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">ioloop</span>.<span style="color: black;">add_callback</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">call_main</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> call_main<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;">publish</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'main'</span><span style="color: black;">&#41;</span>
        <span style="color: #dc143c;">time</span>.<span style="color: black;">sleep</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">0.1</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">ioloop</span>.<span style="color: black;">add_callback</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">call_main</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> block<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        ioloop = tornado.<span style="color: black;">ioloop</span>.<span style="color: black;">IOLoop</span>.<span style="color: black;">instance</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">try</span>:
            ioloop.<span style="color: black;">start</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;">KeyboardInterrupt</span>:
            ioloop.<span style="color: black;">stop</span><span style="color: black;">&#40;</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;">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></pre></div></div>

<p>Next we create a plugin that will subscribe to the bus and which will be in charge for the AMQP communication.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">class</span> WS2AMQPPlugin<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;">conn</span> = pika.<span style="color: black;">AsyncoreConnection</span><span style="color: black;">&#40;</span>pika.<span style="color: black;">ConnectionParameters</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">'localhost'</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">channel</span> = <span style="color: #008000;">self</span>.<span style="color: black;">conn</span>.<span style="color: black;">channel</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">channel</span>.<span style="color: black;">exchange_declare</span><span style="color: black;">&#40;</span>exchange=<span style="color: #483d8b;">&quot;X&quot;</span>, <span style="color: #008000;">type</span>=<span style="color: #483d8b;">&quot;direct&quot;</span>, durable=<span style="color: #008000;">False</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">channel</span>.<span style="color: black;">queue_declare</span><span style="color: black;">&#40;</span>queue=<span style="color: #483d8b;">&quot;Q&quot;</span>, durable=<span style="color: #008000;">False</span>, exclusive=<span style="color: #008000;">False</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">channel</span>.<span style="color: black;">queue_bind</span><span style="color: black;">&#40;</span>queue=<span style="color: #483d8b;">&quot;Q&quot;</span>, exchange=<span style="color: #483d8b;">&quot;X&quot;</span>, routing_key=<span style="color: #483d8b;">&quot;&quot;</span><span style="color: black;">&#41;</span>
&nbsp;
        <span style="color: #008000;">self</span>.<span style="color: black;">channel</span>.<span style="color: black;">basic_consume</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">amqp2ws</span>, queue=<span style="color: #483d8b;">&quot;Q&quot;</span><span style="color: black;">&#41;</span>
&nbsp;
        <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;ws2amqp&quot;</span>, <span style="color: #008000;">self</span>.<span style="color: black;">ws2amqp</span><span style="color: black;">&#41;</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;stop&quot;</span>, <span style="color: #008000;">self</span>.<span style="color: black;">cleanup</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> cleanup<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;">unsubscribe</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;ws2amqp&quot;</span>, <span style="color: #008000;">self</span>.<span style="color: black;">ws2amqp</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">bus</span>.<span style="color: black;">unsubscribe</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;stop&quot;</span>, <span style="color: #008000;">self</span>.<span style="color: black;">cleanup</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">channel</span>.<span style="color: black;">queue_delete</span><span style="color: black;">&#40;</span>queue=<span style="color: #483d8b;">&quot;Q&quot;</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">channel</span>.<span style="color: black;">exchange_delete</span><span style="color: black;">&#40;</span>exchange=<span style="color: #483d8b;">&quot;X&quot;</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">conn</span>.<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> amqp2ws<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, ch, method, header, body<span style="color: black;">&#41;</span>:
        <span style="color: #008000;">self</span>.<span style="color: black;">bus</span>.<span style="color: black;">publish</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;amqp2ws&quot;</span>, body<span style="color: black;">&#41;</span>
        ch.<span style="color: black;">basic_ack</span><span style="color: black;">&#40;</span>delivery_tag=method.<span style="color: black;">delivery_tag</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> ws2amqp<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, message<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;Publishing to AMQP: %s&quot;</span> <span style="color: #66cc66;">%</span> message<span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">channel</span>.<span style="color: black;">basic_publish</span><span style="color: black;">&#40;</span>exchange=<span style="color: #483d8b;">&quot;X&quot;</span>, routing_key=<span style="color: #483d8b;">&quot;&quot;</span>, body=message<span style="color: black;">&#41;</span></pre></div></div>

<p>The interesting bits are the amqp2ws and ws2amqp methods. The former is called anytime the AMQP broker pushes data to our AMQP consumer, we then use the bus to publish the message to any interested subscribers. The latter publishes to AMQP messages that come from the WebSockets channel.</p>
<p>Next let&#8217;s see the Tornado WebSockets handler.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">class</span> WebSocket2AMQP<span style="color: black;">&#40;</span>websocket.<span style="color: black;">WebSocketHandler</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: #66cc66;">*</span>args, <span style="color: #66cc66;">**</span>kwargs<span style="color: black;">&#41;</span>:
        websocket.<span style="color: black;">WebSocketHandler</span>.<span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, <span style="color: #66cc66;">*</span>args, <span style="color: #66cc66;">**</span>kwargs<span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">settings</span><span style="color: black;">&#91;</span><span style="color: #483d8b;">'bus'</span><span style="color: black;">&#93;</span>.<span style="color: black;">subscribe</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;amqp2ws&quot;</span>, <span style="color: #008000;">self</span>.<span style="color: black;">push_message</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #008000;">open</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;">receive_message</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">on_message</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> on_message<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, message<span style="color: black;">&#41;</span>:
        <span style="color: #008000;">self</span>.<span style="color: black;">settings</span><span style="color: black;">&#91;</span><span style="color: #483d8b;">'bus'</span><span style="color: black;">&#93;</span>.<span style="color: black;">publish</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;ws2amqp&quot;</span>, message<span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">write_message</span><span style="color: black;">&#40;</span>message<span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">receive_message</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>.<span style="color: black;">on_message</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> on_connection_close<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;">settings</span><span style="color: black;">&#91;</span><span style="color: #483d8b;">'bus'</span><span style="color: black;">&#93;</span>.<span style="color: black;">unsubscribe</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;amqp2ws&quot;</span>, <span style="color: #008000;">self</span>.<span style="color: black;">push_message</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> push_message<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, message<span style="color: black;">&#41;</span>:
        <span style="color: #008000;">self</span>.<span style="color: black;">write_message</span><span style="color: black;">&#40;</span>message<span style="color: black;">&#41;</span></pre></div></div>

<p>The on_message method is called whenever data is received from the client, the push_message is used to push data to the client.</p>
<p>Finally, we setup the plug everything together:</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><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;/ws&quot;</span>, WebSocket2AMQP<span style="color: black;">&#41;</span>,
        <span style="color: black;">&#93;</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>Notice the fact we subscribe the asyncore poll function to the main channel of the bus so that pika works properly as if we had called asyncore.loop()</p>
<p>The code can be found <a href="http://pastebin.ca/1881774">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.defuze.org/archives/207-plugging-amqp-and-websocket.html/feed</wfw:commentRss>
		</item>
		<item>
		<title>Managing your process with the CherryPy&#8217;s bus</title>
		<link>http://www.defuze.org/archives/198-managing-your-process-with-the-cherrypy-bus.html</link>
		<comments>http://www.defuze.org/archives/198-managing-your-process-with-the-cherrypy-bus.html#comments</comments>
		<pubDate>Wed, 09 Jun 2010 20:00:50 +0000</pubDate>
		<dc:creator>Sylvain Hellegouarch</dc:creator>
		
		<category><![CDATA[Uncategorized]]></category>

		<category><![CDATA[python]]></category>

		<category><![CDATA[cherrypy]]></category>

		<guid isPermaLink="false">http://www.defuze.org/?p=198</guid>
		<description><![CDATA[CherryPy is a successful small web framework which over the years has built up its performances as well as its stability. To do so, Robert Brewer, the main CherryPy&#8217;s architect has introduced what is called the Web Site Process Bus (WSPBus). The idea is to manage a Python process by providing it with a bus [...]]]></description>
			<content:encoded><![CDATA[<p>CherryPy is a successful small web framework which over the years has built up its performances as well as its stability. To do so, Robert Brewer, the main CherryPy&#8217;s architect has introduced what is called the <a href="http://www.cherrypy.org/wiki/WSPBSpec" target="_blank">Web Site Process Bus</a> (WSPBus). The idea is to manage a Python process by providing it with a bus to which one can publish or subscribe for events. CherryPy&#8217;s implementation of the bus comes with a set of pubsub handlers for very basic operations such as responding to system signals, handle thread creation and deletion, drop process privileges and handle PID files. The bus mechanism can help your handling of sub-processes so that they start, run and terminates gracefully. Let&#8217;s see how.</p>
<h2>Create your bus</h2>
<p>First, you need to create a bus instance. This could be as simple as this.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><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
bus = wspbus.<span style="color: black;">Bus</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></td></tr></table></div>

<p>If you want to log through the bus, you will need further work since the bus doesn&#8217;t create a logger by default. Let&#8217;s see an example.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><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;">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
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> MyBus<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>, name=<span style="color: #483d8b;">&quot;&quot;</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>name<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;
    <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></pre></td></tr></table></div>

<p>Not much, just creating a logger and subscribing the bus log channel to an instance method.</p>
<h2>Associate the bus with the main process</h2>
<p>Before we move on to the management of sub-process, let&#8217;s see how we can manage the main Python process already with our bus above.</p>
<p>For this, let&#8217;s imagine a bank placing stock orders, those orders will be handled by a broker running in a sub-process.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">random</span>
<span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">string</span>
<span style="color: #ff7700;font-weight:bold;">from</span> multiprocessing <span style="color: #ff7700;font-weight:bold;">import</span> Process
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> Bank<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>, queue<span style="color: black;">&#41;</span>:
        <span style="color: #008000;">self</span>.<span style="color: black;">bus</span> = MyBus<span style="color: black;">&#40;</span>Bank.__name__<span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">queue</span> = queue
        <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;main&quot;</span>, <span style="color: #008000;">self</span>.<span style="color: black;">randomly_place_order</span><span style="color: black;">&#41;</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;exit&quot;</span>, <span style="color: #008000;">self</span>.<span style="color: black;">terminate</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> randomly_place_order<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
        order = <span style="color: #dc143c;">random</span>.<span style="color: black;">sample</span><span style="color: black;">&#40;</span><span style="color: black;">&#91;</span><span style="color: #483d8b;">'BUY'</span>, <span style="color: #483d8b;">'SELL'</span><span style="color: black;">&#93;</span>, <span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: black;">&#93;</span>
        <span style="color: #dc143c;">code</span> = <span style="color: #dc143c;">random</span>.<span style="color: black;">sample</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">string</span>.<span style="color: black;">ascii_uppercase</span>, <span style="color: #ff4500;">4</span><span style="color: black;">&#41;</span>
        amount = <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>
&nbsp;
        message = <span style="color: #483d8b;">&quot;%s %s %d&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>order, <span style="color: #483d8b;">''</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span><span style="color: #dc143c;">code</span><span style="color: black;">&#41;</span>, amount<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;Placing order: %s&quot;</span> <span style="color: #66cc66;">%</span> message<span style="color: black;">&#41;</span>
&nbsp;
        <span style="color: #008000;">self</span>.<span style="color: black;">queue</span>.<span style="color: black;">put</span><span style="color: black;">&#40;</span>message<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>:
        <span style="color: #008000;">self</span>.<span style="color: black;">bus</span>.<span style="color: black;">start</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">bus</span>.<span style="color: black;">block</span><span style="color: black;">&#40;</span>interval=<span style="color: #ff4500;">0.01</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> terminate<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;">unsubscribe</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;main&quot;</span>, <span style="color: #008000;">self</span>.<span style="color: black;">randomly_place_order</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">bus</span>.<span style="color: black;">unsubscribe</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;exit&quot;</span>, <span style="color: #008000;">self</span>.<span style="color: black;">terminate</span><span style="color: black;">&#41;</span></pre></td></tr></table></div>

<p>As you can see, not much again here, we simply associate a bus with the bank object. We also register to the exit channel of the bus so that when we terminated, we can do some cleanup. It&#8217;s good use to unregister from the bus.</p>
<p>We don&#8217;t actually care where those orders come from so we randomly generate them. The orders are placed every time the bus iterates its loop. This is done by attaching to the main channel of the bus.</p>
<p>We use a <a href="http://docs.python.org/library/multiprocessing.html#pipes-and-queues" target="_blank">process queue</a> to communicate with the broker&#8217;s sub-process.</p>
<h2>Associate the bus with a sub-process</h2>
<p>Handling the sub-process is actually similar to handling the main process. Let&#8217;s see the broker implementation for example.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">from</span> <span style="color: #dc143c;">Queue</span> <span style="color: #ff7700;font-weight:bold;">import</span> Empty
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> Broker<span style="color: black;">&#40;</span>Process<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>, queue<span style="color: black;">&#41;</span>:
        Process.<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;">queue</span> = queue
        <span style="color: #008000;">self</span>.<span style="color: black;">bus</span> = MyBus<span style="color: black;">&#40;</span>Broker.__name__<span style="color: black;">&#41;</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;main&quot;</span>, <span style="color: #008000;">self</span>.<span style="color: black;">check</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> check<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;">try</span>:
            message = <span style="color: #008000;">self</span>.<span style="color: black;">queue</span>.<span style="color: black;">get_nowait</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">except</span> Empty:
            <span style="color: #ff7700;font-weight:bold;">return</span>
&nbsp;
        <span style="color: #ff7700;font-weight:bold;">if</span> message == <span style="color: #483d8b;">&quot;stop&quot;</span>:
            <span style="color: #008000;">self</span>.<span style="color: black;">bus</span>.<span style="color: black;">unsubscribe</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;main&quot;</span>, <span style="color: #008000;">self</span>.<span style="color: black;">check</span><span style="color: black;">&#41;</span>
            <span style="color: #008000;">self</span>.<span style="color: black;">bus</span>.<span style="color: black;">exit</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">elif</span> message.<span style="color: black;">startswith</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;BUY&quot;</span><span style="color: black;">&#41;</span>:
            <span style="color: #008000;">self</span>.<span style="color: black;">buy</span><span style="color: black;">&#40;</span><span style="color: #66cc66;">*</span>message.<span style="color: black;">split</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">' '</span>, <span style="color: #ff4500;">2</span><span style="color: black;">&#41;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span>:<span style="color: black;">&#93;</span><span style="color: black;">&#41;</span>
        <span style="color: #ff7700;font-weight:bold;">elif</span> message.<span style="color: black;">startswith</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;SELL&quot;</span><span style="color: black;">&#41;</span>:
            <span style="color: #008000;">self</span>.<span style="color: black;">sell</span><span style="color: black;">&#40;</span><span style="color: #66cc66;">*</span>message.<span style="color: black;">split</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">' '</span>, <span style="color: #ff4500;">2</span><span style="color: black;">&#41;</span><span style="color: black;">&#91;</span><span style="color: #ff4500;">1</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: #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;">start</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">bus</span>.<span style="color: black;">block</span><span style="color: black;">&#40;</span>interval=<span style="color: #ff4500;">0.01</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: #008000;">self</span>.<span style="color: black;">queue</span>.<span style="color: black;">put</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;stop&quot;</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> buy<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, <span style="color: #dc143c;">code</span>, amount<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;BUY order placed for %s %s&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>amount, <span style="color: #dc143c;">code</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">def</span> sell<span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, <span style="color: #dc143c;">code</span>, amount<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;SELL order placed for %s %s&quot;</span> <span style="color: #66cc66;">%</span> <span style="color: black;">&#40;</span>amount, <span style="color: #dc143c;">code</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span></pre></td></tr></table></div>

<p>Several things are to be noticed. First we register once again to the bus&#8217; main channel a method that checks the shared queue for incoming data. Whenever the incoming message is &#8220;stop&#8221;, we exit the bus altogether, thus leaving the sub-process, since it was blocked on the bus loop.</p>
<p>Note that the stop method could be called by the parent process if you needed to programatically stop the sub-process.</p>
<h2>Put it all together</h2>
<p>Run the code above as follow:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">if</span> __name__ == <span style="color: #483d8b;">'__main__'</span>:
    <span style="color: #ff7700;font-weight:bold;">from</span> multiprocessing <span style="color: #ff7700;font-weight:bold;">import</span> <span style="color: #dc143c;">Queue</span>
    queue = <span style="color: #dc143c;">Queue</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
    broker = Broker<span style="color: black;">&#40;</span>queue<span style="color: black;">&#41;</span>
    broker.<span style="color: black;">start</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
&nbsp;
    bank = Bank<span style="color: black;">&#40;</span>queue<span style="color: black;">&#41;</span>
    bank.<span style="color: black;">run</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></td></tr></table></div>

<p>This creates the shared queue, starts the sub-process that runs the broker and finally starts the bank within the main process.</p>
<p>You should see a bunch of messages in the console and if you hit Ctrl-C, this will stop both processes cleanly.</p>
<p>And here we are, we now manage processes and sub-processes with a clean solution. The CherryPy process bus is an elegant add-on to your toolbox that I can only highly advise to consider in the future. The WSPBus implementation is part of the main CherryPy package (CherryPy 3.x), so you&#8217;ll have to install it all, even if you don&#8217;t require the HTTP framework. But don&#8217;t let that hold you back since the HTTP framework isn&#8217;t required for the bus to be used.</p>
<p>Happy coding!</p>
<p>The code is <a href="http://pastebin.ca/1879986" target="_blank">here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.defuze.org/archives/198-managing-your-process-with-the-cherrypy-bus.html/feed</wfw:commentRss>
		</item>
		<item>
		<title>Beyond superfeedr awesomeness</title>
		<link>http://www.defuze.org/archives/195-beyond-superfeedr-awesomeness.html</link>
		<comments>http://www.defuze.org/archives/195-beyond-superfeedr-awesomeness.html#comments</comments>
		<pubDate>Wed, 09 Dec 2009 21:04:34 +0000</pubDate>
		<dc:creator>Sylvain Hellegouarch</dc:creator>
		
		<category><![CDATA[atom]]></category>

		<category><![CDATA[xmpp]]></category>

		<category><![CDATA[superfeedr xmpp atom]]></category>

		<guid isPermaLink="false">http://www.defuze.org/?p=195</guid>
		<description><![CDATA[superfeedr is one of those great web services that has bridged what interests you on the web with your lazyness to go and check it for yourself. That&#8217;s right! More seriously, you don&#8217;t have to be lazy to be quickly overwhelmed by the huge quantity of information available to you and superfeedr offers you a [...]]]></description>
			<content:encoded><![CDATA[<p><a title="superfeedr is as good as chocolate" href="http://superfeedr.com/" target="_blank">superfeedr</a> is one of those great web services that has bridged what interests you on the web with your lazyness to go and check it for yourself. That&#8217;s right! More seriously, you don&#8217;t have to be lazy to be quickly overwhelmed by the huge quantity of information available to you and superfeedr offers you a fantastic service by delivering information at your door step.</p>
<p>But as it stands, superfeedr is just the tip of the iceberg. It could do much more for you.</p>
<h3>Resource sharing</h3>
<p>It would be probably be interesting if you could easily share with others (contacts, aggregators, social networks, etc.) links that you find interesting. This means you would become a source of information too. In other words, it&#8217;s as if you were generating a feed of links which others could subscribe too through superfeedr. But aside from the form itself, the possibility to automatically (re)publish interesting links is probably useful.</p>
<h3>Recommendation support</h3>
<p>Considering the sheer amount of feeds parsed by superfeedr and the fact they know the set of feeds you are interested in, it would be quite useful to add a recommendation mechanism in place. It means analysing feeds metadata but also giving weight to feeds based on the number of subscribers they have in the system or how long they retain subscribers. Similarly it would be relevant to publishers to get metrics to monitor how people consume their resources.</p>
<h3>A dedicated client</h3>
<p>XMPP clients suck. They all do when you want to go beyond pure instant messaging. PubSub is just barely supported and when it is, it&#8217;s just not user friendly. Therefore, following what the guys at <a href="http://seesmic.com/" target="_blank">seesmic</a> have done, if you want your users to enjoy your service, you need a dedicated client. They care naught for your protocol. They want information displayed in the fashion du jour. These days, you gotta look like <a title="My twitter profile. So sexy!" href="http://twitter.com/lawouach" target="_self">Twitter</a>. Maybe <a href="http://www.process-one.net/en/blogs/article/onechannel_a_desktop_publish_subscribe_client_for_instant_news_delivery/" target="_blank">Process One OneChannel</a> could be a lead.</p>
<h3><strong>Integrate with Wave</strong></h3>
<p>That one is probably just for the fun of it but <a href="https://wave.google.com/" target="_blank">Wave</a> is quite fancy-able these days and being able to be notified via a Wave could be a very cool.</p>
<h3>Have a mobile presence</h3>
<p>Mobiles will be where the market is in the next coming years. New generation mobile phones can support really well HTTP and even XMPP as <a href="http://www.buddymob.com/" target="_blank">BuddyMob</a> has shown for quite some time with Android. Being able to broadcast to mobile phones is a must.</p>
<hr />Are those overall changes critical to superfeedr? It&#8217;s hard to tell. Creeping featurism is never a good idea and finding the right balance between what would make the service richer without making it obscure and without focus is always difficult. In any case, it&#8217;s a very handy service that deserves more attention. Happy feeding.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.defuze.org/archives/195-beyond-superfeedr-awesomeness.html/feed</wfw:commentRss>
		</item>
		<item>
		<title>Atom and AtomPub were failed</title>
		<link>http://www.defuze.org/archives/171-atom-and-atompub-were-failed.html</link>
		<comments>http://www.defuze.org/archives/171-atom-and-atompub-were-failed.html#comments</comments>
		<pubDate>Sun, 19 Apr 2009 13:23:48 +0000</pubDate>
		<dc:creator>Sylvain Hellegouarch</dc:creator>
		
		<category><![CDATA[atom]]></category>

		<category><![CDATA[atompub]]></category>

		<category><![CDATA[xmpp]]></category>

		<category><![CDATA[atom atompub pubsub xmpp]]></category>

		<guid isPermaLink="false">http://www.defuze.org/?p=171</guid>
		<description><![CDATA[Joe Gregorio&#8217;s AtomPub is a failure note is probably not as provocative as its title may make it sound. Joe knows more than the average lunatic out there about AtomPub considering he was co-editor of RFC 5023 and his work at Google so when he says AtomPub may have failed, one is forced to pay [...]]]></description>
			<content:encoded><![CDATA[<p><a title="AtomPub is a failure by Joe Gregorio" href="http://bitworking.org/news/425/atompub-is-a-failure" target="_blank">Joe Gregorio&#8217;s AtomPub is a failure</a> note is probably not as provocative as its title may make it sound. Joe knows more than the average lunatic out there about AtomPub considering he was co-editor of <a title="Atom Publishing Protocol RFC" href="http://www.ietf.org/rfc/rfc5023.txt" target="_blank">RFC 5023</a> and his work at Google so when he says AtomPub may have failed, one is forced to pay attention.</p>
<p>Joe argues that even though AtomPub hasn&#8217;t failed as technology, it hasn&#8217;t swept the inter-tubes world away as hoped probably because the rationale for creating such a protocol years back doesn&#8217;t really pan out any longer.</p>
<p>I share Joe&#8217;s slight disappointment at not seeing AtomPub more spread on the web but I don&#8217;t entirely agree with the reasons he puts forward:</p>
<blockquote><p>So why hasn&#8217;t AtomPub stormed the world to become the one true protocol? Well, there are three answers:</p>
<ul>
<li>Browsers</li>
<li>Browsers</li>
<li>Browsers</li>
</ul>
<p>The world is a different place then it was when Atom and AtomPub started back in 2002, browsers are much more powerful, Javascript compatibility is increasing among them, there are more libraries to smooth over the differences, and connectivity is on the rise.</p></blockquote>
<p>I&#8217;m kind of puzzled, browsers did exist back in 2002, didn&#8217;t they? Sure they are more standardized, Javascript is not a shameful programing language like it used to be back in the DHTML days but nonetheless, browsers haven&#8217;t fundamentally changed since then. Moreover, through all the years required to release AtomPub nothing prevented the WG in charge of it to reach out for browser vendors. I don&#8217;t remember seeing much of them during the long discussions that took place. I find therefore rather harsh to complain at browsers for the fact AtomPub isn&#8217;t spread enough. (Note to the <a href="https://www.ietf.org/mailman/listinfo/hybi" target="_blank">Hybi list</a>, don&#8217;t make the same potential mistake).</p>
<p>Joe also suggests that XML was overcome by the couple: HTML+JSON. That is very true and Joe acknowledges that he didn&#8217;t think this would happen but when he says:</p>
<blockquote><p><strong>All of the advances in browsers and connectivity have conspired to keep AtomPub from reaching the widespread adoption</strong> that I had envisioned when work started on the protocol, but that doesn&#8217;t mean it&#8217;s a failure.</p></blockquote>
<p>Conspired? Come on Joe. XMPP certainly suffers from the priority browser vendors have made in the past years but XMPP isn&#8217;t built atop HTTP when AtomPub is. I&#8217;m sorry Joe but, if anything, the fact Javascript has now so many fantastic libraries make it even simpler to interface with any protocol based on HTTP. Browsers have everything one needs to build an application conversing through AtomPub.</p>
<p>I disagree with Joe&#8217;s reasoning and personally I think there is a much bigger issue here to explain the protocol&#8217;s difficulties to be a premium choice for public web services.</p>
<p>Let&#8217;s review some social network platforms and the way they expose their data.</p>
<p><strong>Wikipedia</strong></p>
<p><em>Atom:</em> Only used to view global recent changes and utter failure at actually using the format for what it&#8217;s good at. In a nutshell, everything is serialized to HTML and packed into the atom:summary element.</p>
<p><em>API:</em> MediaWiki offers an <a href="http://www.mediawiki.org/wiki/API" target="_blank">API</a> to expose and manipulate data which isn&#8217;t based on AtomPub but follows quite obviously the same design considering the document&#8217;s nature of a wiki. Note that the API supports several output formats but none of which is Atom.</p>
<p><strong>Facebook</strong></p>
<p><em>Atom:</em> As far as I can tell, Facebook doesn&#8217;t offer any support for Atom.</p>
<p><em>API:</em> Facebook has its own <a href="http://wiki.developers.facebook.com/index.php/API" target="_blank">API</a> which doesn&#8217;t really map well to AtomPub even though it&#8217;s quite often simple CRUD operations.</p>
<p><strong>Twitter</strong></p>
<p><em>Atom:</em> Twitter exposes friend&#8217;s timeline as Atom feeds and does it well. Their API doesn&#8217;t support it however.</p>
<p><em>API:</em> Twitter&#8217;s <a href="http://apiwiki.twitter.com/Twitter-API-Documentation" target="_blank">API</a> is also not based on AtomPub.</p>
<p><strong>Last.fm</strong></p>
<p><em>Atom:</em> No support for Atom.</p>
<p><em>API:</em> Last.fm&#8217;s <a href="http://www.lastfm.fr/api" target="_blank">API</a> doesn&#8217;t use AtomPub, well considering they don&#8217;t even use Atom itself, it kind of make sense.</p>
<p><strong>Orkut</strong></p>
<p><em>Atom:</em> Available but not officially supported as per the documentation.</p>
<p><em>API:</em> Orkut&#8217;s <a href="http://code.google.com/intl/en-US/apis/orkut/docs/index.html" target="_blank">API</a> is based on OpenSocial which if I&#8217;m not mistaken isn&#8217;t based on AtomPub.</p>
<p><strong>MySpace</strong></p>
<p><em>Atom:</em> Not from  the public website as far as I could tell.</p>
<p><em>API:</em> MySpace&#8217;s <a href="http://developer.myspace.com/community/" target="_blank">API</a> is based on OpenSocial too, thus not based on AtomPub.</p>
<p><strong>Flickr</strong></p>
<p><em>Atom:</em> Offers Atom feeds for a wide range of their data from their website.</p>
<p><em>API:</em> Flickr has its own API not based on AtomPub and doesn&#8217;t output to Atom.</p>
<p><strong>Google&#8217;s various API</strong></p>
<p><em>Atom:</em> Extensive support for it.</p>
<p><em>API:</em> Extensively based on AtomPub even though no public support for the AtomPub service document which kind of limits service discovery.</p>
<p>I&#8217;m not trying to point fingers at those services, most of them came to life way before AtomPub became seriously defined (and maybe Tim Bray was right to warn the WG it was taken too long) so it&#8217;s not really a surprise it&#8217;s not used. In most cases though exposed APIs could be mapped to the protocol as well as the Atom format but obviously that would require major and fundamental architectural refactorings which make little economical sense.</p>
<p>It&#8217;s worth noting that some services have hard times even exposing data without degrading their values. Atom is a well-defined protocol that offers a simple view on web semantics. Used well, it offers a fantastic ground for mashup of organized and structured data. This is added value I&#8217;m wondering if mentioned social network platforms are willing to propose. In some respects <a href="http://pipes.yahoo.com/pipes/" target="_blank">Yahoo Pipes</a> might not have to exist if Atom and AtomPub were properly used. Atom is so much more than pure serialization format. Atom can be serialized to JSON and still be Atom. Atom offers a real path to build powerful and meaningful social graph of distributed data.</p>
<p>Ultimately, data is money and I assume it&#8217;s a technical issue as much as an economical one. It&#8217;s easy to help building the piping for interoperability but it seems harder to actually open up the tap of data.</p>
<p>That being said, as Joe says:</p>
<blockquote><p>There is still plenty of uses for AtomPub and it has quietly appeared in many places.</p></blockquote>
<p>Atom and AtomPub aren&#8217;t dead but are probably less visible than one might have hoped, used in more traditional places and I suggest you <a href="http://twitter.com/pkeane" target="_blank">follow</a> <a href="http://blogs.law.harvard.edu/pkeane/" target="_blank">Peter Keane</a> to get a feel for it.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.defuze.org/archives/171-atom-and-atompub-were-failed.html/feed</wfw:commentRss>
		</item>
		<item>
		<title>Ubikod: Here I come</title>
		<link>http://www.defuze.org/archives/169-ubikod-here-i-come.html</link>
		<comments>http://www.defuze.org/archives/169-ubikod-here-i-come.html#comments</comments>
		<pubDate>Fri, 27 Feb 2009 13:55:27 +0000</pubDate>
		<dc:creator>Sylvain Hellegouarch</dc:creator>
		
		<category><![CDATA[android]]></category>

		<category><![CDATA[xmpp]]></category>

		<category><![CDATA[ubikod]]></category>

		<guid isPermaLink="false">http://www.defuze.org/?p=169</guid>
		<description><![CDATA[Today is my last day at Ipsis as I&#8217;m joining Ubikod starting from Monday to become a senior software developer.
Ubikod is a young but enthusiastic company that is geared toward the future as they&#8217;re leading the way of social network for the Google Android platform through BuddyMob.
The team is passionate and has gathered quite a [...]]]></description>
			<content:encoded><![CDATA[<p>Today is my last day at Ipsis as I&#8217;m joining <a href="http://www.ubikod.com/" target="_blank">Ubikod</a> starting from Monday to become a senior software developer.</p>
<p>Ubikod is a young but enthusiastic company that is geared toward the future as they&#8217;re leading the way of social network for the Google Android platform through <a href="http://www.buddymob.com/" target="_blank">BuddyMob</a>.</p>
<p>The team is passionate and has gathered quite a great knowledge of the platform. On top of that they&#8217;re using foxy technologies like XMPP for their platform and I&#8217;m excited about getting my hands on.</p>
<p>Things are looking great.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.defuze.org/archives/169-ubikod-here-i-come.html/feed</wfw:commentRss>
		</item>
		<item>
		<title>Is XMPP for the web ready?</title>
		<link>http://www.defuze.org/archives/163-is-xmpp-for-the-web-ready.html</link>
		<comments>http://www.defuze.org/archives/163-is-xmpp-for-the-web-ready.html#comments</comments>
		<pubDate>Wed, 25 Feb 2009 16:49:23 +0000</pubDate>
		<dc:creator>Sylvain Hellegouarch</dc:creator>
		
		<category><![CDATA[atompub]]></category>

		<category><![CDATA[python]]></category>

		<category><![CDATA[xmpp]]></category>

		<category><![CDATA[bosh]]></category>

		<category><![CDATA[facebook]]></category>

		<guid isPermaLink="false">http://www.defuze.org/?p=163</guid>
		<description><![CDATA[A discussion started recently on the Jabber social mailing-list about the current state of XMPP support at Facebook. If you don&#8217;t remember the idea of a XMPP network for Facebook I&#8217;d say it&#8217;s rather normal considering they talked about it back in May 2008 and nothing has really happened since then. The discussion has quickly [...]]]></description>
			<content:encoded><![CDATA[<p>A <a href="http://mail.jabber.org/pipermail/social/2009-February/000470.html" target="_blank">discussion</a> started recently on the Jabber social mailing-list about the current state of XMPP support at Facebook. If you don&#8217;t remember the idea of a XMPP network for Facebook I&#8217;d say it&#8217;s rather normal considering they talked about it <a href="http://www.defuze.org/archives/46-facebook-goes-xmpp.html" target="_blank">back in May 2008</a> and <a href="http://bugs.developers.facebook.com/show_bug.cgi?id=3152" target="_blank">nothing has really happened since then</a>. The discussion has quickly drifted toward the difficulty XMPP knows to really make it big and you can sense some despair within the community.</p>
<p>It seems there are several reasons for this.</p>
<p>First and foremost, XMPP is rather poorly integrated within browsers. Today you have to be part of the browser if you want to succeed. Perhaps the response will come from the rather brilliant <a href="http://code.stanziq.com/strophe/" target="_blank">lib Strophe</a>. Strophe uses <a href="http://xmpp.org/extensions/xep-0206.html" target="_blank">BOSH</a> to connect to XMPP services since Javascript doesn&#8217;t offer much access to the low level socket connection object. I hope in the future browsers will offer a built-in XMPP API that can be accessed from Javascript allowing to avoid using BOSH.</p>
<p>Second of all, we need big players to start accepting to let go and start entering Jabber federations so that interoperability actually works. I assume companies have yet to find a way on how they can exercise control over the data they might expose through XMPP whilst finding a way to monetize them.</p>
<p>Third I&#8217;d say there is a critical lack of PR around XMPP as an added value to the business. To be honest there is the same issue with AtomPub in my opinion. Both are fantastic technologies but they don&#8217;t sell by themselves and I find there is a lack of support from companies to support them. SOAP might have been a crap technology but it was backed up by large businesses that were competing with each other (for the worse one might argue).</p>
<p>Finally, and to me this is the most critical reason, XMPP doesn&#8217;t integrate well with the web in general. From the browser side as I suggested above to the fact that it&#8217;s rather hard to combine web application with jabber ones. Its definitely doable but requires some careful attention to your architecture. Jack Moffitt <a href="http://metajack.im/2008/09/03/twitters-failures-are-not-xmpps-failures/" target="_blank">argues it&#8217;s not XMPP&#8217;s fault</a>. It&#8217;s quite true as a protocol but I believe it&#8217;s not really the right way to invite web developers to push it one step further if you blame them for doing it wrong.</p>
<p>I believe XMPP has tremendous potential but it&#8217;s still has some way to go before it finds its place.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.defuze.org/archives/163-is-xmpp-for-the-web-ready.html/feed</wfw:commentRss>
		</item>
	</channel>
</rss>
