Making a command out of shutil

I love the creating that developers show in making unintended use of a tool to solve a problem. It doesn't matter if that use is a "good idea" or not. Regardless of whether it's a "good idea" or not. The ingenuity and problem solving demonstrated is usually quite fascinating. Like this little hack that my friend mjdorma shared with me that uses Begins' sub-commands.

import shutil
import begin

begin.subcommand(shutil.copy)
begin.subcommand(shutil.copy2)
begin.subcommand(shutil.copyfile)
begin.subcommand(shutil.copymode)
begin.subcommand(shutil.copystat)
begin.subcommand(shutil.copytree)
begin.subcommand(shutil.move)
begin.subcommand(shutil.rmtree)

# Patch in doc before the func is wrapped by begin.start
def patch_doc(doc):
    def decorate(func):
        func.__doc__ = doc
        return func
    return decorate

@begin.start
@patch_doc(shutil.__doc__)
def main():
    pass

This little script gives you access to a number of utility functions from Python's shutil module from the command line. Admittedly there are common Unix utilities that provide most of this functionality. But I still felt it was a nifty and entertaining use of functionality I had just written.

After using Begins for a few weeks this type of application has become a common pattern for Begins. Sub-commands excel at creating small administration programs that are a collection of related commands. Even more exciting is that others groups are successfully orchestrating much larger systems using Begins as a framework. Its terrific to know that Begins is meeting its goal of growing with the developer.

You can download this script from Michael's original GitHub Gist.


Rebooting TwoSlug

This week, like last week, I've been revamping an old project and learning some new tools. This time it's been TwoSlug receiving my attention.

TwoSlug is a fun little web tool to generate random two word phrases, or slug lines, using a randomly chosen verb and noun. Words are chosen from Princeton University's WordNet database. It was originally written almost year ago, in a single evening, and hosted on GitHub. Unfortunately it's been limited to a simple, static, HTML page until now.

Aside from the obligatory themeing, I've added two major new features to TwoSlug. The first is an API for requesting your own random slug lines. With an HTTP GET request you can ask for any combination of verbs, nouns, adjectives and adverbs. The API will return you random slug line in JSON format.

The second feature is word definitions using DuckDuckGo's excellent Instant Answer API. Definitions are displayed as pop ups on each word. The words themselves are links to the original definition source. The team at DuckDuckGo are extremely generous to offer their API with very restrictions.

Now, if I can just get ElasticSearch to use TwoSlug to generate a random node name each time a node starts.


reStructuredText Viewer

This week's project was implementing my own reStructuredText Viewer and editor for reStructuredText documents. In the past I have used the Online reStructuredText editor for quickly previewing and editing README files before being publishing on the Python Package Index. While I have been happy with the online editor, it recently had some stability issues. When I tried to deploy my own instance I was surprised to find a dependency on Redis.

Not wanting to deploy an instance of Redis as well as a web application, I decided it was a good opportunity to learn something about docutils. It's the basis for the Sphinx documentation generator, I tool I use regularity. My viewer is a web application built using the Flask web framework. The application acts as a docutils publisher, accepting reStructuredText documents and return rendered HTML. As with other little web applications I've, the viewer is deployed on Heroku. The source code is available from GitHub.

So feel free to give my reStructuredText Viewer a try. But please be kind to the aesthetics, web design is not my strong suite.