<disclaimer>
Men, this is pure love, I’m a geek and if you aren’t too, don’t read further.
</disclaimer>
In those days in my spare time I’m working on Matt Goodall’s Twisted Pastebin to learn using the framework and Nevow. The original version is very good but needed some enhancements so I put my hands on it and start working around. I knew nothing about Nevow and very little but with the help of the #twisted.web guys on IRC and with some tips given me by my friend Valentino “dialtone” Volonghi I coded a better pastebin featuring persistent cookies, syntax highlighting for about 30 languages, navigation UI (needs to be improved, it’s on my TODO list), export as text of pastings, a yet-to-be-coded archives interface (and perhaps a very simple backend to allow deletion and something like that). Ah! I forgot the main change (the real reason why I started the improving process): Atop, the Python OODB (based on Twisted obviously) developed by the Divmod team. So in a couple of hours I replaced the pickle based backend to use it.
But what excited me most is the thing that I’ve done two minutes ago. I replaced this bad bad code (I was too lazy to read again the manual, shame on me):
def _get_process_output(text, language):
lang = languagesmap.get(language)
if not lang:
return text
params = ARGS % lang
cmd = "%s %s" % (PATH, params)
stdin, stdout_err = os.popen2(cmd, params)
stdin.write(text)
stdin.close()
return stdout_err.read()
def colouriser(text, language):
text = "\n".join(text.splitlines())
d = threads.deferToThread(_get_process_output, text, language)
d.addCallbacks(lambda data: data, lambda data: data)
return d
with this wonderful piece of code enclosing all the Twisted Matrix Power:
def colouriser(text, language):
text = "\n".join(text.splitlines())
lang = languagesmap.get(language)
if not lang:
lang = "txt"
params = ARGS % lang
params = params.split()
params.insert(0, PROCESS_NAME)
d = defer.Deferred()
hlpp = HLProcessProtocol(text, d)
reactor.spawnProcess(hlpp, PATH, params)
return d
class HLProcessProtocol(protocol.ProcessProtocol):
def __init__(self, text, d):
self.text = text
self.d = d
self.buffer = []
def connectionMade(self):
self.transport.write(self.text)
self.transport.closeStdin()
def outReceived(self, data):
self.buffer.append(data)
def outConnectionLost(self):
data = "".join(self.buffer)
self.receiveColourisedText(data)
def receiveColourisedText(self, data):
self.d.callback(data)
again… shame on me, shame on me
- Twisted Matrix is an event driven framework developed in Python with tons of ready to use protocols; if you want to do networking use Twisted, drop everything else
- a pastebin is a web application used to share code, more here: http://en.wikipedia.org/wiki/Pastebin
- Nevow is a web application templating system built upon Twisted Matrix
- Atop is a OODB mainly used as a backend for Quotient, the Divmod’s messaging oriented application server
- Pickle is the Python builtin serialization mechanism.
The online demo is here http://rhymes.dyndns.org:8083.
I will not guarantee that it will be up 24hours a day because it’s my 1Ghz laptop, so please, don’t flood it and since I’m still working on the codebase it will be shutted down on my own needs.
Bye, share the love.
update: the archives interface is there, needs only better colors. The backend is useless so I’ll not waste my time coding an interface used only to delete pastings. I noticed that (as usual) the site with IE sucks so I gotta take some time tweaking the css files (perhaps I could go without css at all for IE users <g>).

