This site is deprecated, please visit http://threebean.org/blog/

My blog is moving…

leave a comment »

I won’t be updating this blog anymore – please visit http://threebean.org/blog instead.

This wordpress site will be left in tact however, since there are a number of posts linked to from other sites.

Written by threebean

February 15, 2012 at 12:56 am

Posted in Uncategorized

Hacking tw2 resource injection (in pyramid)

leave a comment »

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 <head> 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:



    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 [
        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

Written by threebean

February 11, 2012 at 2:58 pm

Posted in Code, Pyramid, Python

Announcing — 24 hour tw2 bug sprint for PyCon

with one comment

(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:

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

Written by threebean

February 7, 2012 at 3:11 am

Posted in Code, Python

Tagged with

Hacking tw2 resource injection

leave a comment »

Tonight, VooDooNOFX was asking in IRC in #turbogears how to disable the injection of jquery.js by tw2.jquery into her/his TG2 app. Using the inject_resources=False middleware config value wouldn’t cut it, since she/he wanted tw2 to inject all other resources, they were loading jQuery via google CDN beforehand and tw2’s injection was clobbering their code.

I came up with the following hack to myapp/lib/base.py which will remove tw2.jquery.jquery_js from the list of resources tw2 would inject into each page served by a TG2.1 app.

At the top of myapp/lib/base.py import:

import tw2.core.core
import tw2.jquery

and then replace:

        return TGController.__call__(self, environ, start_response)

with the following:

        stream = TGController.__call__(self, environ, start_response)

        # Disable the injection of tw2.jquery
        offending_link = tw2.jquery.jquery_js.req().link
        local = tw2.core.core.request_local()
        local['resources'] = [
            r for r in local.get('resources', list()) if r.link != offending_link

        return stream

The two tricks to this are

  • Simply knowing that tw2 resources register themselves with the ‘request_local’ object and that during the return-phase of the WSGI pipeline, the tw2 middleware refers to that list when injecting resources
  • Figuring out where in a TG2 app’s request flow to place the call to alter that object after all widgets that might register jquery have declared their resources but before the resources list is injected into the output stream.

We came out of it with a bug filed in the tw2 issue tracker so we can take care of it properly in the future.

Written by threebean

December 13, 2011 at 5:44 am

Posted in Code, Python, Turbogears

skipping words in zsh

leave a comment »

About a half year ago I started using zsh after having been introduced to the GRML config. It boasts a ton of great features including remote tab-completion over ssh under the hood (when trying to remember a path for scp for instance). It’s been great, but the one thing I’ve been missing is that old word-skipping feature from bash; pressing ctrl-left and ctrl-right is really nice.

After finding (in multiple places) cake that was really a lie, I finally found it commented out at the bottom of a random .zshrc. I added it to my ~/.zshrc.local and am finally in business.

bindkey '^[[1;5D' emacs-backward-word
bindkey '^[[1;5C' emacs-forward-word

Written by threebean

November 21, 2011 at 4:50 pm

Posted in Uncategorized

“chaplin-ize” – Fact: every Cinema studies department website is better with Charlie Chaplin

leave a comment »

This past week, work sent me to Seattle for Supercomputing 2011.

While there, I crashed with my sister, Doctor J, drank wine, did some dancing, and had a blast! On Friday night after the wine had set in, I tried explaining what it is I do. It’s really just magic, but has to be described with words like python, and development. I showed off raptorizemw and soon enough, we were getting ready to chaplin-ize her department’s new website!

Check it out at http://depts.washington.edu/mirg. The effect only triggers once in every three page loads (on average) so if it doesn’t show, reload it a couple times.

Written by threebean

November 21, 2011 at 4:23 pm

Posted in Code, lulz

python ansi2html makes taskwarrior fantas(k)tic

with one comment

Over a year ago, I wrote a python module called ansi2html that takes colored ansi as input and produces colored, formatted html.

With it you can do something like the following:

$ ls --color=always | ansi2html > directory.html
$ sudo tail /var/log/messages | ccze -A | ansi2html > logs.html

Much more recently I discovered taskwarrior, a command line todo list manager that rocks. It features the ability to produce burndown charts of your todos over time.

I use the following in a cronjob to produce an online version of my burndown chart.

$ task burndown | ansi2html | ssh me@web "cat > ~/static/burndown.html"

My regularly-updated chart is hosted on threebean.org.

If you come up with any neat uses for ansi2html, please let me know. I’d love to hear about them.

Written by threebean

October 28, 2011 at 6:26 pm

Posted in Code, Python


Get every new post delivered to your Inbox.