[three]Bean

TW2 Bugsprint; the Final Countdown

Mar 02, 2012 | categories: python, toscawidgets, pyramid, turbogears View Comments

Tomorrow marks the start of the toscawidgets2 24 hour bugsprint and I couldn't be more stoked. We haven't even started yet but tw2 development pushed ahead anyways.

We have:

Goal: (solidly) close as many bugs as possible and push as many libraries from beta to release as we can.

Here's to winning that game.

View Comments

New toscawidgets.org frontpage

Feb 26, 2012 | categories: python, toscawidgets View Comments

The toscawidgets2 24 hour bugsprint is this coming Saturday! In order to get you excited and to curry all of your favors, I decided to give http://toscawidgets.org a facelift.

The old http://toscawidgets.org site was confusing! 80% of the frontpage was about toscawidgets 1 which, while stable, has a diminishing number of maintainers and no active development. 20% of the page was a "news" bar on the right that pointed users to "Alpha releases" and "new documentation" for tw2, timestamped back in 2010!

/blog/static/images/old-tw2-frontpage.png

I revamped it to more clearly differentiate between the two versions of the framework and to give it a more modern look (if we're building python widget libraries, we better be on top of CSS3 on our project page, right?)

/blog/static/images/new-tw2-frontpage.png
View Comments

Migrating from wordpress

Feb 14, 2012 | categories: python, blogofile View Comments

I got fed up with wordpress and have moved to the python tool blogofile. I've been intermittently tending to my old blog over at http://threebean.wordpress.com/ and getting traffic I'm happy with. A lot of my old tutorials on tw2 regularly get referred to by google and my new posts get picked up by readers of planet python and occasionally by reddit.

WordPress has become unbearable:

  • I have to edit my posts in a clumsy WSIWYG editor.
  • My posts appear in planet aggregators without proper links back to my blog and half-truncated.
  • My control of styling and javascript is limited.
  • I can't embed github gists.

So I'm trying out blogofile:

  • It supports both reStructuredText and markdown out of the box.
  • My styling and layout are handled by mako templates which I use already elsewhere.
  • I can keep my posts in git.
  • I can host everything in one place on http://threebean.org

Along the way, I picked up this handy hack from zzzeek on getting syntax highlighting working in reST posts.

I also had to script something up with feedparser to parse my exported WordPress blog's and reimport it to blogofile. Here's what I wrote (It's based heavily on this example from the blogofile github repo:

#!/usr/bin/env python
""" WordPress (xml) -> blogofile.

Picks up an exported WordPress.com blog in XML format and builds a blogofile
``_posts`` directory out of it.
"""

import codecs
import feedparser
import os
import re
import yaml


def get_published_posts(filename):
    """ Generator over the posts in a wordpress.com XML dump. """

    d = feedparser.parse(filename)

    for entry in d['entries']:
        if entry.get('wp_attachment_url', None):
            pass
        elif entry.get('title', None) == 'About':
            pass
        else:
            yield entry


if __name__ == '__main__':
    #Output textile files in ./_posts
    if os.path.isdir("_posts"):
        print "There's already a _posts directory here, "\
                "I'm not going to overwrite it."
        sys.exit(1)
    else:
        os.mkdir("_posts")

    old_prefix = "http://threebean.wordpress.com/"
    new_prefix = "http://threebean.org/blog/"
    prepend_links = True

    post_num = 1
    for post in get_published_posts('wordpress-export.xml'):
        yaml_data = {
            "title": post['title'],
            "date": post['wp_post_date'].replace('-', '/'),
            "permalink": post['link'].replace(old_prefix, new_prefix),
            "categories": ", ".join([
                tag['term'].lower() for tag in post['tags']
            ]),
            "tags": ", ".join([
                tag['term'].lower() for tag in post['tags']
            ]),
            }
        fn = u"{0}-{1}.html".format(
                str(post_num).zfill(4),
                re.sub(
                    r'[/!:?\-,\']',
                    '',
                    post['title'].strip().lower().replace(' ', '_')
                )
        )

        content = post['content'][0]['value'].replace(u"\r\n", u"\n")

        if prepend_links:
            content = "<p>Originally posted at <a href='%s'>%s</a>.</p>" % (
                post['link'], post['link']) + content

        print "writing " + fn
        f = codecs.open(os.path.join("_posts", fn), "w", "utf-8")
        f.write("---\n")
        f.write(
            yaml.safe_dump(
                yaml_data,
                default_flow_style=False,
                allow_unicode=True
            ).decode("utf-8")
        )
        f.write("---\n")
        f.write(content)
        f.close()
        post_num += 1
View Comments

Hacking tw2 resource injection (in pyramid)

Feb 11, 2012 | categories: python, toscawidgets, pyramid View Comments

In #toscawidgets this morning, zykes- was asking about how to do the jquery removal hack I wrote about a month back but in Pyramid instead of TurboGears2. First I'll summarize the problem, then show you our solution.

toscawidgets2 tw2.* provides lots of handy web ui widgets including a wrapper around jquery-ui, jqgrid, and lots of fancy data widgets. One of the problems it solves for you is de-duplicating resources. Say you're including two fancy jquery-related widgets. Both of them require that jquery.js be included on the page. tw2 includes a piece of middleware that tracks all the resources required by the widgets being rendered on a given request, reduces that list to a set with no duplicates, orders it by dependency, and injects it into the tag.

Usually this is fine, but in some cases you want an exception to the rule. Say, like in my previous post, you want to include jquery.js yourself manually and you'd rather not have tw2 stomping all over your javascript namespace. You could disable tw2 injection of all resources, but you want all the others included -- just not jquery.

There is no automatic detection and filtration flag implemented in tw2 and it would be tough to do in the general case. tw2 can't guarantee that it's own jquery.js and your jquery.js are the same, or different, or included, or not included, or anything -- it doesn't even try.

To get tw2 to not do what you don't want, you need to un-register tw2's jquery resource from the middleware yourself (on each request). Previously we came up with a working hack that does this in the context of a TurboGears2 app. Here's the same concept applied to a Pyramid app using a Pyramid "tween".

import tw2.core.core
import tw2.jquery

def remove_jq_factory(handler, registry):
    """ Remove tw2 jquery_js from tw2's middleware registry.

    In order to use this, you need to add the following to your
    myapp/__init__.py file:

        config.add_tween('myapp.tween.remove_jq_factory')

    """

    def remove_jq_tween(request):
        # Send the request on through to other tweens and to our app
        response = handler(request)

        # Before the response is modified by the tw2 middleware, let's remove
        # jquery_js from its registry.
        offending_links = [r.req().link for r in [
            tw2.jquery.jquery_js,
        ]]
        local = tw2.core.core.request_local()
        local['resources'] = [
            r for r in local.get('resources', list())
            if r.link not in offending_links
        ]

        return response

    return remove_jq_tween
View Comments

Announcing -- 24 hour tw2 bug sprint for PyCon

Feb 07, 2012 | categories: python, toscawidgets View Comments

(First published to the toscawidgets-discuss google group)

It came up in IRC the other day that tw2 has been through many, many beta iterations.  So many in fact, that a full on tw2 2.0 release is painfully overdue.

That said, there are still plenty of out-standing issues in our issue tracker:
    http://bitbucket.org/paj/tw2core

Before we tag and name the 2.0 final product, I'm proposing -- no, announcing -- a 24 hour tw2 sprint the weekend before PyCon.  We'll write docs, we'll automate tests, we'll settle the dispatch dispute and wax philosophic.

I invite you to join me Saturday, March 3rd at 18:00:00 UTC through Sunday, March 4th 18:00:00 UTC.  Hop into #toscawidgets on freenode or join me in a hangout on google plus.  We'll nail every last bug on deck and still have time for a beer.

Yours in widgetry-

 - threebean

View Comments

« Previous Page -- Next Page »