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

<channel>
	<title>brool &#187; python</title>
	<atom:link href="http://www.brool.com/index.php/category/coding/python-coding/feed" rel="self" type="application/rss+xml" />
	<link>http://www.brool.com</link>
	<description>brool \brool\ (n.) : a low roar; a deep murmur or humming</description>
	<lastBuildDate>Fri, 20 Jan 2012 07:58:59 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>Using Markdown with Mutt</title>
		<link>http://www.brool.com/index.php/using-markdown-with-mutt</link>
		<comments>http://www.brool.com/index.php/using-markdown-with-mutt#comments</comments>
		<pubDate>Wed, 24 Aug 2011 08:28:19 +0000</pubDate>
		<dc:creator>tim</dc:creator>
				<category><![CDATA[coding]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://www.brool.com/?p=584</guid>
		<description><![CDATA[This took me a while to figure out, so in the hopes that I can save someone some time, here&#8217;s how to use markdown with Mutt: Step 1: Install msmtp (or any other program) On OS X you can do this with &#8220;brew install msmtp&#8221;. (You can use sendmail or whatever, but msmtp was easy [...]]]></description>
			<content:encoded><![CDATA[<p>This took me a while to figure out, so in the hopes that I can save someone some time, here&#8217;s how to use markdown with Mutt:</p>
<h2>Step 1: Install msmtp (or any other program)</h2>
<p>On OS X you can do this with &#8220;brew install msmtp&#8221;.  (You can use sendmail or whatever, but msmtp was easy to set up.).</p>
<h2>Step 2: Set up msmtp</h2>
<p>Set up the appropriate configuration file.  I ran into a problem wherein the program would just hang;  turning on debug showed that it hung after &#8220;Reading recipients from command line.&#8221;  This turned out to be an incorrect SSL configuration &#8212; I needed tls_starttls to be off. </p>
<p>My particular .msmtprc configuration:</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="bash"><pre class="de1">defaults
tls on
tls_starttls off
tls_certcheck off
&nbsp;
account Work
host mail.host.com
domain domain.com
auth on
port <span class="nu0">465</span>
protocol smtp
from from-name
user user-name
password password</pre></div></div></div></div></div></div></div>



<h2>Step 3: install sendmail replacement</h2>
<p>For me, this was two things:   a small shell script and a Python program that will convert any plaintext e-mail into a plaintext + HTML e-mail.</p>
<p>Shell script mark_send:</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="bash"><pre class="de1"><span class="co0">#!/bin/sh</span>
<span class="sy0">/</span>Users<span class="sy0">/</span>tim<span class="sy0">/</span>mutt<span class="sy0">/</span>mark_and_send.py <span class="st0">&quot;/usr/local/bin/pandoc -s&quot;</span> <span class="sy0">|</span> <span class="sy0">/</span>usr<span class="sy0">/</span>local<span class="sy0">/</span>bin<span class="sy0">/</span>msmtp <span class="re5">-a</span> Work $<span class="sy0">@</span></pre></div></div></div></div></div></div></div>



<p>Python script mark_and_send.py:</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="python"><pre class="de1"><span class="co1">#!/usr/bin/python</span>
<span class="kw1">import</span> <span class="kw3">os</span>
<span class="kw1">import</span> <span class="kw3">sys</span>
<span class="kw1">import</span> <span class="kw3">subprocess</span>
<span class="kw1">import</span> <span class="kw3">email</span>
<span class="kw1">import</span> <span class="kw3">email</span>.<span class="kw3">parser</span>
<span class="kw1">import</span> <span class="kw3">tempfile</span>
<span class="kw1">from</span> <span class="kw3">email</span>.<span class="me1">mime</span>.<span class="me1">multipart</span> <span class="kw1">import</span> MIMEMultipart
<span class="kw1">from</span> <span class="kw3">email</span>.<span class="me1">mime</span>.<span class="me1">text</span> <span class="kw1">import</span> MIMEText
&nbsp;
<span class="kw1">if</span> <span class="kw2">len</span><span class="br0">&#40;</span><span class="kw3">sys</span>.<span class="me1">argv</span><span class="br0">&#41;</span> <span class="sy0">==</span> <span class="nu0">1</span>:
    <span class="kw1">print</span> <span class="st0">&quot;usage: mark_and_send.py markdown_program [markdown_flag]&quot;</span>
    <span class="kw1">print</span> <span class="st0">&quot;where:&quot;</span>
    <span class="kw1">print</span> <span class="st0">&quot;    markdown_program is the name of the program to translate markdown.&quot;</span>
    <span class="kw1">print</span> <span class="st0">&quot;    markdown_flag is an optional indicator in the first line that determines whether to run the markdown program&quot;</span>
    <span class="kw3">sys</span>.<span class="me1">exit</span><span class="br0">&#40;</span><span class="nu0">0</span><span class="br0">&#41;</span>
&nbsp;
markdown_program <span class="sy0">=</span> <span class="kw3">sys</span>.<span class="me1">argv</span><span class="br0">&#91;</span><span class="nu0">1</span><span class="br0">&#93;</span>
&nbsp;
p <span class="sy0">=</span> <span class="kw3">email</span>.<span class="kw3">parser</span>.<span class="me1">Parser</span><span class="br0">&#40;</span><span class="br0">&#41;</span>
m <span class="sy0">=</span> p.<span class="me1">parsestr</span><span class="br0">&#40;</span><span class="kw3">sys</span>.<span class="me1">stdin</span>.<span class="me1">read</span><span class="br0">&#40;</span><span class="br0">&#41;</span><span class="br0">&#41;</span>
&nbsp;
<span class="co1"># is this a single header?</span>
do_markdown <span class="sy0">=</span> <span class="kw2">False</span>
<span class="kw1">if</span> <span class="kw1">not</span> m.<span class="me1">is_multipart</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="kw1">and</span> m.<span class="me1">get_content_maintype</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="sy0">==</span> <span class="st0">'text'</span> <span class="kw1">and</span> m.<span class="me1">get_content_subtype</span><span class="br0">&#40;</span><span class="br0">&#41;</span> <span class="sy0">==</span> <span class="st0">'plain'</span>:
    do_markdown <span class="sy0">=</span> <span class="kw2">True</span>
    payload <span class="sy0">=</span> m.<span class="me1">get_payload</span><span class="br0">&#40;</span><span class="br0">&#41;</span>
    <span class="co1"># optionally check the markdown flag</span>
    <span class="kw1">if</span> <span class="kw2">len</span><span class="br0">&#40;</span><span class="kw3">sys</span>.<span class="me1">argv</span><span class="br0">&#41;</span> <span class="sy0">==</span> <span class="nu0">3</span>:
        lines <span class="sy0">=</span> payload.<span class="me1">split</span><span class="br0">&#40;</span><span class="st0">&quot;<span class="es0">\n</span>&quot;</span><span class="br0">&#41;</span>
        do_markdown <span class="sy0">=</span> lines<span class="br0">&#91;</span><span class="nu0">0</span><span class="br0">&#93;</span> <span class="sy0">==</span> <span class="kw3">sys</span>.<span class="me1">argv</span><span class="br0">&#91;</span><span class="nu0">2</span><span class="br0">&#93;</span>
        <span class="kw1">if</span> do_markdown:
            payload <span class="sy0">=</span> <span class="st0">&quot;<span class="es0">\n</span>&quot;</span>.<span class="me1">join</span><span class="br0">&#40;</span>lines<span class="br0">&#91;</span><span class="nu0">1</span>:<span class="br0">&#93;</span><span class="br0">&#41;</span>
&nbsp;
<span class="kw1">if</span> do_markdown:
    tf <span class="sy0">=</span> <span class="kw3">tempfile</span>.<span class="me1">NamedTemporaryFile</span><span class="br0">&#40;</span>delete<span class="sy0">=</span><span class="kw2">False</span><span class="br0">&#41;</span>
    tf.<span class="me1">write</span><span class="br0">&#40;</span>payload<span class="br0">&#41;</span>
    tf.<span class="me1">close</span><span class="br0">&#40;</span><span class="br0">&#41;</span>
&nbsp;
    <span class="br0">&#40;</span>markdown<span class="sy0">,</span> error<span class="br0">&#41;</span> <span class="sy0">=</span> <span class="kw3">subprocess</span>.<span class="me1">Popen</span><span class="br0">&#40;</span>markdown_program.<span class="me1">split</span><span class="br0">&#40;</span><span class="br0">&#41;</span> + <span class="br0">&#91;</span>tf.<span class="me1">name</span><span class="br0">&#93;</span><span class="sy0">,</span> stdout<span class="sy0">=</span><span class="kw3">subprocess</span>.<span class="me1">PIPE</span><span class="br0">&#41;</span>.<span class="me1">communicate</span><span class="br0">&#40;</span><span class="br0">&#41;</span>
&nbsp;
    <span class="co1"># create a new message with the exact same headers</span>
    new_message <span class="sy0">=</span> MIMEMultipart<span class="br0">&#40;</span><span class="st0">'alternative'</span><span class="br0">&#41;</span>
    <span class="kw1">for</span> <span class="br0">&#40;</span>k<span class="sy0">,</span>v<span class="br0">&#41;</span> <span class="kw1">in</span> m._headers:
        new_message<span class="br0">&#91;</span>k<span class="br0">&#93;</span> <span class="sy0">=</span> v
&nbsp;
    text_plain <span class="sy0">=</span> MIMEText<span class="br0">&#40;</span>payload<span class="sy0">,</span> <span class="st0">'plain'</span><span class="br0">&#41;</span>
    new_message.<span class="me1">attach</span><span class="br0">&#40;</span>text_plain<span class="br0">&#41;</span>
&nbsp;
    text_html <span class="sy0">=</span> MIMEText<span class="br0">&#40;</span>markdown<span class="sy0">,</span> <span class="st0">'html'</span><span class="br0">&#41;</span>
    new_message.<span class="me1">attach</span><span class="br0">&#40;</span>text_html<span class="br0">&#41;</span>
&nbsp;
    <span class="kw1">print</span> new_message.<span class="me1">as_string</span><span class="br0">&#40;</span><span class="br0">&#41;</span>
    <span class="kw3">os</span>.<span class="me1">unlink</span><span class="br0">&#40;</span>tf.<span class="me1">name</span><span class="br0">&#41;</span>
<span class="kw1">else</span>:
    <span class="kw1">print</span> m.<span class="me1">as_string</span><span class="br0">&#40;</span><span class="br0">&#41;</span></pre></div></div></div></div></div></div></div>



<p>The mark_and_send.py program takes one required argument (the name of the program to convert from markdown to HTML, possibly quoted) an an optional indicator of markdown.  If the first line of the message is the indicator, then it is stripped and the rest of the message is converted to HTML.

<h2>Step 3: set up mutt</h2>
<p>You need to tell mutt to use your shell script instead of sendmail.  I have it inside a folder hook:</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="bash"><pre class="de1">folder-hook +work<span class="sy0">/</span>.<span class="sy0">*</span> <span class="st_h">'set sendmail=&quot;/bin/sh /Users/tim/mutt/mark_send&quot;'</span></pre></div></div></div></div></div></div></div>



<p>Now, whenever you send a plain-text email, mutt will automatically run markdown on it and attach it as the HTML view of the message.</p>

<h3>Results</h3>
So, after running with this for a few days, here are my impressions:

<ul>
<li>Markdown is not ideal for e-mail, at least, not without some work.  For example: more e-mails that are quoted by default will not work properly in markdown, because there are not blank lines in the message to set off the blockquote.  This could possibly be improved by post-processing the file that Mutt generates on a reply, but I don&#8217;t think there&#8217;s a hook for that.</li>
<li>Code coloring is nice</li>
<li>Math formulas, if you need them</li>
</ul>

<p>Example is show below, as rendered by Zimbra. It isn&#8217;t perfect. Google will unfortunately strip the colors from the code coloring, and Zimbra loses leading indents, and both readers will require the user to load images. Nonetheless, in a pinch&#8230;</p>

<div style="border: 1px solid black !important; margin: 10px"><img src="http://images.brool.com/blog/coding/mail-example.png"/></div>]]></content:encoded>
			<wfw:commentRss>http://www.brool.com/index.php/using-markdown-with-mutt/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using Google Authenticator For Your Website</title>
		<link>http://www.brool.com/index.php/using-google-authenticator-for-your-website</link>
		<comments>http://www.brool.com/index.php/using-google-authenticator-for-your-website#comments</comments>
		<pubDate>Sat, 26 Feb 2011 18:41:44 +0000</pubDate>
		<dc:creator>tim</dc:creator>
				<category><![CDATA[coding]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://www.brool.com/?p=465</guid>
		<description><![CDATA[Google has started offering two-factor authentication for Google logins, using Google Authenticator. They have applications available for iPhone, Android, and Blackberry that give time-based passwords based on the proposed TOTP (Time-based One Time Password) draft standard. The Google code provides a command line program that can generate secret keys as well as a PAM module, [...]]]></description>
			<content:encoded><![CDATA[<p>Google has started offering two-factor authentication for Google logins, using <a href="https://code.google.com/p/google-authenticator/">Google Authenticator</a>.  They have applications available for iPhone, Android, and Blackberry that give time-based passwords based on the proposed <a href="http://tools.ietf.org/html/draft-mraihi-totp-timebased">TOTP (Time-based One Time Password)</a> draft standard.</p>

<p>The Google code provides a command line program that can generate secret keys as well as a PAM module, but it turns out to be very little code to authenticate a TOTP, thereby providing two-factor authentication to your website very easily.</p>

<p>To give the user the key, you&#8217;ll need to generate a cryptographically-secure 10 byte random key, presented to the user as a base32 16-character string.  They can either enter this string directly, or you can use Google charts to provide a barcode that they can scan into the Google Authenticator application:</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="python"><pre class="de1"><span class="kw1">def</span> get_barcode_image<span class="br0">&#40;</span>username<span class="sy0">,</span> domain<span class="sy0">,</span> secretkey<span class="br0">&#41;</span>:
    url <span class="sy0">=</span> <span class="st0">&quot;https://www.google.com/chart&quot;</span>
    url +<span class="sy0">=</span> <span class="st0">&quot;?chs=200x200&amp;chld=M|0&amp;cht=qr&amp;chl=otpauth://totp/&quot;</span>
    url +<span class="sy0">=</span> username + <span class="st0">&quot;@&quot;</span> + domain + <span class="st0">&quot;%3Fsecret%3D&quot;</span> + secretkey
    <span class="kw1">return</span> url</pre></div></div></div></div></div></div></div>




<p>For an example of what a code looks like, <a href="https://www.google.com/chart?chs=200x200&#038;chld=M|0&#038;cht=qr&#038;chl=otpauth://totp/me@brool.com%3Fsecret%3DZVMDU4NOTXEJGGET">click here</a>, or, look below:</p>

<img src="http://images.brool.com/blog/coding/totp_code.png"/>

<p>After the user has a secret key from you and has entered it into Google Authenticator either by typing it in directly or scanning in the barcode, you have to be able to verify the key during login (for example).  The code to authenticate is only a few lines in Python:</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="python"><pre class="de1"><span class="kw1">import</span> <span class="kw3">time</span>
<span class="kw1">import</span> <span class="kw3">struct</span>
<span class="kw1">import</span> <span class="kw3">hmac</span>
<span class="kw1">import</span> hashlib
<span class="kw1">import</span> <span class="kw3">base64</span>
&nbsp;
<span class="kw1">def</span> authenticate<span class="br0">&#40;</span>secretkey<span class="sy0">,</span> code_attempt<span class="br0">&#41;</span>:
    tm <span class="sy0">=</span> <span class="kw2">int</span><span class="br0">&#40;</span><span class="kw3">time</span>.<span class="kw3">time</span><span class="br0">&#40;</span><span class="br0">&#41;</span> / <span class="nu0">30</span><span class="br0">&#41;</span>
&nbsp;
    secretkey <span class="sy0">=</span> <span class="kw3">base64</span>.<span class="me1">b32decode</span><span class="br0">&#40;</span>secretkey<span class="br0">&#41;</span>
&nbsp;
    <span class="co1"># try 30 seconds behind and ahead as well</span>
    <span class="kw1">for</span> ix <span class="kw1">in</span> <span class="br0">&#91;</span>-<span class="nu0">1</span><span class="sy0">,</span> <span class="nu0">0</span><span class="sy0">,</span> <span class="nu0">1</span><span class="br0">&#93;</span>:
        <span class="co1"># convert timestamp to raw bytes</span>
        b <span class="sy0">=</span> <span class="kw3">struct</span>.<span class="me1">pack</span><span class="br0">&#40;</span><span class="st0">&quot;&gt;q&quot;</span><span class="sy0">,</span> tm + ix<span class="br0">&#41;</span>
&nbsp;
        <span class="co1"># generate HMAC-SHA1 from timestamp based on secret key</span>
        hm <span class="sy0">=</span> <span class="kw3">hmac</span>.<span class="me1">HMAC</span><span class="br0">&#40;</span>secretkey<span class="sy0">,</span> b<span class="sy0">,</span> hashlib.<span class="me1">sha1</span><span class="br0">&#41;</span>.<span class="me1">digest</span><span class="br0">&#40;</span><span class="br0">&#41;</span>
&nbsp;
        <span class="co1"># extract 4 bytes from digest based on LSB</span>
        offset <span class="sy0">=</span> <span class="kw2">ord</span><span class="br0">&#40;</span>hm<span class="br0">&#91;</span>-<span class="nu0">1</span><span class="br0">&#93;</span><span class="br0">&#41;</span> &amp; <span class="nu0">0x0F</span>
        truncatedHash <span class="sy0">=</span> hm<span class="br0">&#91;</span>offset:offset+<span class="nu0">4</span><span class="br0">&#93;</span>
&nbsp;
        <span class="co1"># get the code from it</span>
        <span class="kw3">code</span> <span class="sy0">=</span> <span class="kw3">struct</span>.<span class="me1">unpack</span><span class="br0">&#40;</span><span class="st0">&quot;&gt;L&quot;</span><span class="sy0">,</span> truncatedHash<span class="br0">&#41;</span><span class="br0">&#91;</span><span class="nu0">0</span><span class="br0">&#93;</span>
        <span class="kw3">code</span> &amp;<span class="sy0">=</span> <span class="nu0">0x7FFFFFFF</span><span class="sy0">;</span>
        <span class="kw3">code</span> %<span class="sy0">=</span> <span class="nu0">1000000</span><span class="sy0">;</span>
&nbsp;
        <span class="kw1">if</span> <span class="br0">&#40;</span><span class="st0">&quot;%06d&quot;</span> % <span class="kw3">code</span><span class="br0">&#41;</span> <span class="sy0">==</span> <span class="kw2">str</span><span class="br0">&#40;</span>code_attempt<span class="br0">&#41;</span>:
            <span class="kw1">return</span> <span class="kw2">True</span>
&nbsp;
    <span class="kw1">return</span> <span class="kw2">False</span></pre></div></div></div></div></div></div></div>


]]></content:encoded>
			<wfw:commentRss>http://www.brool.com/index.php/using-google-authenticator-for-your-website/feed</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
		<item>
		<title>Posting To WordPress From Git</title>
		<link>http://www.brool.com/index.php/posting-to-wordpress-from-git</link>
		<comments>http://www.brool.com/index.php/posting-to-wordpress-from-git#comments</comments>
		<pubDate>Mon, 27 Jul 2009 20:57:20 +0000</pubDate>
		<dc:creator>tim</dc:creator>
				<category><![CDATA[coding]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[git]]></category>
		<category><![CDATA[wordpress]]></category>

		<guid isPermaLink="false">http://www.brool.com/?p=302</guid>
		<description><![CDATA[I&#8217;ve found WordPress to be pretty decent, aside from the security updates every other week, but for me writing is a very spur of the moment thing; I prefer to be able to go into Emacs and just immediately type anything without having to log into my blog, create a new post, and then suffer [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve found WordPress to be pretty decent, aside from the security updates every other week, but for me writing is a very spur of the moment thing;  I prefer to be able to go into Emacs and just immediately type anything without having to log into my blog, create a new post, and then suffer through a web editor.  Basically, I want to do it in Emacs!  Now!  And maybe I&#8217;m offline!</p>

<p>So, I wrote a tiny little program to help facilitate using git and WordPress together through the XMLRPC API, and it&#8217;s helped me write&#8230; well, if not <i>more</i> than at least <i>less painfully</i>.</p>

<h3>Setting Up</h3>
<p>Assuming that your blog is set up at http://www.yourblog.com (you can create an account at wordpress.com to test this out), all you&#8217;ll need to do is:</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="bash"><pre class="de1"><span class="re5">--</span> <span class="kw2">make</span> a directory <span class="kw1">for</span> the blog
<span class="kw2">mkdir</span> blog
chdir blog
&nbsp;
<span class="re5">--</span> download everything
python wp.py download <span class="re5">--user</span>=yourname <span class="re5">--password</span>=yourpass <span class="re5">--url</span>=http:<span class="sy0">//</span>www.yourblog.com<span class="sy0">/</span>xmlrpc.php
<span class="br0">&#40;</span><span class="kw3">wait</span> a bit<span class="br0">&#41;</span>
<span class="kw2">git init</span>
<span class="kw2">git add</span> .
<span class="kw2">git commit</span> <span class="re5">-m</span> <span class="st0">&quot;first version&quot;</span>
&nbsp;
<span class="re5">--</span> now <span class="kw1">set</span> up so we don<span class="st_h">'t have to specify --user, --password, and --url every time (optional)
git wp config wp.url http://www.yourblog.com/xmlrpc.php
git wp config wp.user yourname
git wp config wp.password yourpass</span></pre></div></div></div></div></div></div></div>




<p>The files are downloaded in the appropriate YYYY/MM directories, with the draft directory being used for all of your unpublished drafts.</p>

<p>All the drafts are stored in plain text, but you&#8217;ll see some lines starting with periods &#8212; these are various WordPress variables that are associated with the file.  You can change them, as well;  for example, to change the title of the post, just change the line that begins with &#8220;.title&#8221;. </p>

<h3>Seeing What&#8217;s Different</h3>
<p>You can use the status command to see differences between the local file system and your blog.</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="bash"><pre class="de1">python wp.py status</pre></div></div></div></div></div></div></div>




<h3>Updating From The Blog</h3>
<p>If you&#8217;ve made changes through the web interface and you&#8217;d like to bring them down, you don&#8217;t have to download everything again, but can instead just update.</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="bash"><pre class="de1">python wp.py update</pre></div></div></div></div></div></div></div>




<h3>Posting/Editing</h3>
<p>If you&#8217;d like to edit a post, just edit it, and then use wp post to push it back to the blog:</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="bash"><pre class="de1">python wp.py post changed-file</pre></div></div></div></div></div></div></div>




<p>To create a new post, just create a new file in the draft folder &#8212; I like to specify the .title and .wp_slug parameters, as well &#8212; and then post it.  You can also publish it by changing the .post_status line from draft to publish.

<h3>Gotchas</h3>

<p>While this program <i>requires</i> git, it doesn&#8217;t automatically check anything in &#8212; so you&#8217;ll need to make sure you do git add / git commit or whatnot as necessary.</p>

<p>There are also some gotchas due to the fact that the filename can change on you.  There are cases where the filename that will be brought down is different then the one that you send up:</p>
<ul>
<li>You post a file without a .title or .wp_slug line</li>
<li>You post a file with a different file name than the slug that is generated (i.e., &#8220;my-first-draft&#8221; when the title is actually &#8220;my final draft&#8221;)</li>
<li>Something moves from &#8220;draft&#8221; to &#8220;publish&#8221;
</ul>

<p>When you see a message of the form &#8220;changed: fn1 -> fn2&#8243;, it means that a rename has occurred, and you&#8217;ll need to do the git rm/git add or git mv by hand.</p>

<h3>Source</h3>
<a href="http://github.com/brool/git-wordpress/tree/master">Source is available on github</a>.]]></content:encoded>
			<wfw:commentRss>http://www.brool.com/index.php/posting-to-wordpress-from-git/feed</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Using MissingPy</title>
		<link>http://www.brool.com/index.php/using-missingpy</link>
		<comments>http://www.brool.com/index.php/using-missingpy#comments</comments>
		<pubDate>Fri, 20 Mar 2009 02:15:00 +0000</pubDate>
		<dc:creator>tim</dc:creator>
				<category><![CDATA[coding]]></category>
		<category><![CDATA[haskell]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[imap]]></category>
		<category><![CDATA[missingpy]]></category>
		<category><![CDATA[tutorial]]></category>

		<guid isPermaLink="false">http://www.brool.com/?p=209</guid>
		<description><![CDATA[For comparison purposes, I was rewriting the silly little e-mail digest program in Haskell, using Python libraries for the IMAP interface (it&#8217;s available on Github). It&#8217;s hard to beat Python&#8217;s amazing collection of libraries. Cabal / hackage isn&#8217;t bad, but it doesn&#8217;t yet approach Python&#8217;s &#8220;batteries included&#8221; philosophy and ease of use. Anyway, I couldn&#8217;t [...]]]></description>
			<content:encoded><![CDATA[<p>For comparison purposes, I was rewriting the <a href="http://www.brool.com/index.php/creating-mail-digests-with-python-and-imap">silly little e-mail digest program</a> in Haskell, using Python libraries for the IMAP interface (it&#8217;s <a href="http://github.com/brool/digest/tree/master">available on Github</a>). It&#8217;s hard to beat Python&#8217;s amazing collection of libraries.  Cabal / hackage isn&#8217;t bad, but it doesn&#8217;t yet approach Python&#8217;s &#8220;batteries included&#8221; philosophy and ease of use.  Anyway, I couldn&#8217;t find an IMAP library for Haskell, so decided instead to try <a href="http://hackage.haskell.org/cgi-bin/hackage-scripts/package/MissingPy">MissingPy</a> to interface into the Python IMAP library.  A quick overview/tutorial:</p>

<h3>Installing MissingPy</h3>
<p>I had an issue using Cabal to install MissingPy into my GHC 6.10 installation (Mac OS X).  I went into the directory that had been unpacked and modified Setup.hs:</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="haskell"><pre class="de1"><span class="kw1">import</span> Distribution<span class="sy0">.</span>PackageDescription
<span class="kw1">import</span> Distribution<span class="sy0">.</span>PackageDescription<span class="sy0">.</span>Parse  <span class="co1">-- added this line</span>
<span class="kw1">import</span> Distribution<span class="sy0">.</span>Simple</pre></div></div></div></div></div></div></div>




<p>A cabal configure, build, and install of the package then installed everything.</p>

<h3>Basic Usage</h3>
<p>It&#8217;s easiest to play around with MissingPy in ghci.  Before you do anything you&#8217;ll need to initialize the interpreter with py_initialize and import any necessary modules with pyImport.  You can test by using pyRun_SimpleString:</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="haskell"><pre class="de1"># :m Python<span class="sy0">.</span>Interpreter Python<span class="sy0">.</span>Objects Python<span class="sy0">.</span>Utils Python<span class="sy0">.</span>Exceptions
# py<span class="sy0">_</span>initialize
# pyRun<span class="sy0">_</span>SimpleString <span class="st0">&quot;print 'hello'&quot;</span>
hello</pre></div></div></div></div></div></div></div>




<p>The simplest useful way to execute python code is to use pyRun_String.  For example:</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="haskell"><pre class="de1"><span class="co1">-- add 1 and 2 in Python</span>
<span class="co1">-- can also use Py_file_input or Py_single_input for different handling of the context</span>
addem <span class="sy0">=</span> pyRun<span class="sy0">_</span>String <span class="st0">&quot;1+2&quot;</span> Py<span class="sy0">_</span>eval<span class="sy0">_</span>input <span class="br0">&#91;</span><span class="br0">&#93;</span> <span class="sy0">&gt;&gt;=</span> fromPyObject <span class="sy0">::</span> <span class="kw4">IO</span> <span class="kw4">Integer</span></pre></div></div></div></div></div></div></div>




<p>There are also the callByName and pyObject_Call methods for more complicated scenarios.</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="haskell"><pre class="de1">callByName <span class="sy0">::</span> <span class="kw4">String</span>            <span class="co1">-- ^ Object\/function name</span>
           <span class="sy0">-&gt;</span> <span class="br0">&#91;</span>PyObject<span class="br0">&#93;</span>        <span class="co1">-- ^ List of non-keyword parameters</span>
           <span class="sy0">-&gt;</span> <span class="br0">&#91;</span><span class="br0">&#40;</span><span class="kw4">String</span><span class="sy0">,</span> PyObject<span class="br0">&#41;</span><span class="br0">&#93;</span> <span class="co1">-- ^ List of keyword parameters</span>
           <span class="sy0">-&gt;</span> <span class="kw4">IO</span> PyObject
&nbsp;
pyObject<span class="sy0">_</span>Call <span class="sy0">::</span> PyObject       <span class="co1">-- ^ Object to call</span>
              <span class="sy0">-&gt;</span> <span class="br0">&#91;</span>PyObject<span class="br0">&#93;</span>     <span class="co1">-- ^ List of non-keyword parameters (may be empty)</span>
              <span class="sy0">-&gt;</span> <span class="br0">&#91;</span><span class="br0">&#40;</span><span class="kw4">String</span><span class="sy0">,</span> PyObject<span class="br0">&#41;</span><span class="br0">&#93;</span> <span class="co1">-- ^ List of keyword parameters (may be empty)</span>
              <span class="sy0">-&gt;</span> <span class="kw4">IO</span> PyObject    <span class="co1">-- ^ Return value</span>
&nbsp;
<span class="co1">-- example 1: create an IMAP instance</span>
imap <span class="sy0">&lt;-</span> callByName <span class="st0">&quot;imaplib.IMAP4_SSL&quot;</span> <span class="br0">&#91;</span>imap<span class="sy0">_</span>hostname<span class="br0">&#93;</span> <span class="br0">&#91;</span><span class="br0">&#93;</span>
&nbsp;
<span class="co1">-- call &quot;foo.bar(p1,p2)&quot;</span>
bar <span class="sy0">&lt;-</span> getattr foo <span class="st0">&quot;bar&quot;</span>
result <span class="sy0">&lt;-</span> pyObject<span class="sy0">_</span>Call bar <span class="br0">&#91;</span>p1<span class="sy0">,</span>p2<span class="br0">&#93;</span> <span class="br0">&#91;</span><span class="br0">&#93;</span></pre></div></div></div></div></div></div></div>




<h3>Manipulating Python Objects</h3>
<p>The fromPyObject and toPyObject handle marshaling of objects from Haskell to Python and vice versa.  There are some handy functions for doing common operations on Python objects that are especially useful from the REPL: reprOf (like Python repr), strOf (Python str), showPyObject (shows the type and repr), and dirPyObject (like the Python dir()).</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="haskell"><pre class="de1"># g <span class="sy0">&lt;-</span> pyRun<span class="sy0">_</span>String <span class="st0">&quot;(1,2,'hello')&quot;</span> Py<span class="sy0">_</span>eval<span class="sy0">_</span>input <span class="br0">&#91;</span><span class="br0">&#93;</span>
# showPyObject g
<span class="st0">&quot;&lt;type 'tuple'&gt;: (1, 2, 'hello')&quot;</span>
# reprOf g
<span class="st0">&quot;(1, 2, 'hello')&quot;</span>
# strOf g
<span class="st0">&quot;(1, 2, 'hello')&quot;</span>
# dirPyObject g
<span class="br0">&#91;</span><span class="st0">&quot;__add__&quot;</span><span class="sy0">,</span><span class="st0">&quot;__class__&quot;</span><span class="sy0">,</span><span class="st0">&quot;__contains__&quot;</span><span class="sy0">,</span><span class="st0">&quot;__delattr__&quot;</span><span class="sy0">,</span><span class="st0">&quot;__doc__&quot;</span><span class="sy0">,</span><span class="st0">&quot;__eq__&quot;</span><span class="sy0">,</span><span class="st0">&quot;__ge__&quot;</span><span class="sy0">,</span>
<span class="st0">&quot;__getattribute__&quot;</span><span class="sy0">,</span><span class="st0">&quot;__getitem__&quot;</span><span class="sy0">,</span><span class="st0">&quot;__getnewargs__&quot;</span><span class="sy0">,</span><span class="st0">&quot;__getslice__&quot;</span><span class="sy0">,</span><span class="st0">&quot;__gt__&quot;</span><span class="sy0">,</span>
<span class="st0">&quot;__hash__&quot;</span><span class="sy0">,</span><span class="st0">&quot;__init__&quot;</span><span class="sy0">,</span><span class="st0">&quot;__iter__&quot;</span><span class="sy0">,</span><span class="st0">&quot;__le__&quot;</span><span class="sy0">,</span><span class="st0">&quot;__len__&quot;</span><span class="sy0">,</span><span class="st0">&quot;__lt__&quot;</span><span class="sy0">,</span><span class="st0">&quot;__mul__&quot;</span><span class="sy0">,</span><span class="st0">&quot;__ne__&quot;</span><span class="sy0">,</span>
<span class="st0">&quot;__new__&quot;</span><span class="sy0">,</span><span class="st0">&quot;__reduce__&quot;</span><span class="sy0">,</span><span class="st0">&quot;__reduce_ex__&quot;</span><span class="sy0">,</span><span class="st0">&quot;__repr__&quot;</span><span class="sy0">,</span><span class="st0">&quot;__rmul__&quot;</span><span class="sy0">,</span><span class="st0">&quot;__setattr__&quot;</span><span class="sy0">,</span><span class="st0">&quot;__str__&quot;</span><span class="br0">&#93;</span></pre></div></div></div></div></div></div></div>




<h3>Dealing with Data</h3>
<p>Tuples look like lists when they are returned.  There is no conversion of tuples in the toPyObject call, so you&#8217;ll need to just convert a list and then convert it to a tuple with pyList_AsTuple.  Passing &#8220;None,&#8221; &#8220;True,&#8221; or &#8220;False&#8221; is trickier; the only way I found to do it was create an instance of the value and use it in subsequent calls.</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="haskell"><pre class="de1"># a
<span class="br0">&#91;</span><span class="nu0">1</span><span class="sy0">,</span><span class="nu0">2</span><span class="br0">&#93;</span>
# b <span class="sy0">&lt;-</span> toPyObject a
# reprOf b
<span class="st0">&quot;[1L, 2L]&quot;</span>
# pyList<span class="sy0">_</span>AsTuple b <span class="sy0">&gt;&gt;=</span> reprOf
<span class="st0">&quot;(1L, 2L)&quot;</span>
&nbsp;
<span class="co1">-- get &quot;True,&quot; &quot;False,&quot; and &quot;None&quot;</span>
none <span class="sy0">&lt;-</span> pyRun<span class="sy0">_</span>String <span class="st0">&quot;None&quot;</span> Py<span class="sy0">_</span>eval<span class="sy0">_</span>input <span class="br0">&#91;</span><span class="br0">&#93;</span>
pyTrue <span class="sy0">&lt;-</span> pyRun<span class="sy0">_</span>String <span class="st0">&quot;True&quot;</span> Py<span class="sy0">_</span>eval<span class="sy0">_</span>input <span class="br0">&#91;</span><span class="br0">&#93;</span>
pyFalse <span class="sy0">&lt;-</span> pyRun<span class="sy0">_</span>String <span class="st0">&quot;False&quot;</span> Py<span class="sy0">_</span>eval<span class="sy0">_</span>input <span class="br0">&#91;</span><span class="br0">&#93;</span>
&nbsp;
<span class="co1">-- try it out</span>
# r <span class="sy0">&lt;-</span> pyRun<span class="sy0">_</span>String <span class="st0">&quot;1==1&quot;</span> Py<span class="sy0">_</span>eval<span class="sy0">_</span>input <span class="br0">&#91;</span><span class="br0">&#93;</span>
# r <span class="sy0">==</span> pyTrue
True</pre></div></div></div></div></div></div></div>




<h3>Passing Haskell Objects</h3>
<p>If you want to call into functions without having to deal with Python objects, there are a couple of calls that automatically marshal the objects back and forth for you (all suffixed with &#8220;Hs&#8221;), although they turn out to be less than useful in most situations, since a) the parameters passed in must all be the same type, and b) extracting most return objects from Python is painful.  </p>

<table border="1px solid" width="100%">
<tr><th>call</th><th>invoked upon</th><th>returns</th></tr>
<tr><td>callByNameHs</td><td>Object/function name</td><td>Haskell</td></tr>
<tr><td>pyObject_CallHs</td><td>PyObject</td><td>Haskell</td></tr>
<tr><td>pyObject_Hs</td><td>PyObject</td><td>Python</td></tr>
<tr><td>pyObject_RunHs</td><td>PyObject</td><td>nothing</td></tr>
<tr><td>callMethodHs</td><td>PyObject + method name</td><td>Haskell</td></tr>
<tr><td>runMethodHs</td><td>PyObject + method name</td><td>nothing</td></tr>
</table>
<br/>

<h3>Exceptions</h3>
<p>Python exceptions can be caught and handled;  probably the easiest default way to do this is to use handlePy in conjunction with the ex2ioerr handler, which will automatically print the exception and fail.</p>


<div class="wp-geshi-highlight-wrap5"><div class="wp-geshi-highlight-wrap4"><div class="wp-geshi-highlight-wrap3"><div class="wp-geshi-highlight-wrap2"><div class="wp-geshi-highlight-wrap"><div class="wp-geshi-highlight"><div class="haskell"><pre class="de1">addem <span class="sy0">=</span> handlePy exc2ioerror <span class="sy0">$</span> 
           <span class="kw1">do</span> r <span class="sy0">&lt;-</span> pyRun<span class="sy0">_</span>String <span class="st0">&quot;1/0&quot;</span> Py<span class="sy0">_</span>eval<span class="sy0">_</span>input <span class="br0">&#91;</span><span class="br0">&#93;</span>
              fromPyObject r <span class="sy0">::</span> <span class="kw4">IO</span> <span class="kw4">Integer</span>
&nbsp;
# addem
<span class="sy0">***</span> Exception: user <span class="kw3">error</span> <span class="br0">&#40;</span>Python <span class="sy0">&lt;</span><span class="kw1">type</span> 'exceptions<span class="sy0">.</span>ZeroDivisionError'<span class="sy0">&gt;</span>: integer division <span class="kw3">or</span> modulo by zero<span class="br0">&#41;</span></pre></div></div></div></div></div></div></div>


]]></content:encoded>
			<wfw:commentRss>http://www.brool.com/index.php/using-missingpy/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

