Posts about programming (old posts, page 10)

2007-05-15 12:44

BartleBlog change: Mako Templates

Since the very beginning, BartleBlog has been using CherryTemplate for its output formatting needs. I like it, because it's very simple.

However, it had grown rather cumbersome.

Specifically, most pages in a blog are sort of a page template with a body template inside (the main content).

To do that on CherryTemplate, I used a two-pass approach: generate the body, then pass it as parameter to the page template.

Which is a pain in some cases because you end basically having to do a rendering function for each kind of page, or some crazy-evil function (what I did).

Exploring the different python template engines, I ran into Mako and decided to give it a whirl. It looks good.

The approach is a bit different, it is much more powerful, but you can still use it simply if you can.

And the main feature was template inheritance. Using that, no more inner and outer templates, baby!

Oh, and performance is better:

Cherry

real    31m44.732s
user    21m18.336s
sys     2m7.628s


Mako

real    24m54.472s
user    19m9.508s
sys     1m56.375s

This is for completely rerendering the whole 7 years, 574 posts, 40 static articles, 14 category blog, and there is tons of optimizations to be done.

BTW: this is how you rerender the whole blog:

from BartleBlog.backend.blog import Blog
Blog().renderFullBlog()

2007-05-11 12:01

New Bartleblog Feature: Menu Editor

Took a while to implement, but BartleBlog finally got a functional menu editor:

bartleblog12.png

Right now, it only works with the mootools-based menu gadget, but I will start working on the yahoo menu version in a moment.

The only thing not working is the preview button, because it needs more support on the backend side.

2007-05-11 08:59

Python Trick: Save anything in config files

The Python config objects are convenient and simple, but they have a problem: you can only save strings. That means you need to store numbers as strings and remember to use the getint()/getfloat() methods (or coerce by hand!), which is error prone and anti-pythonic. Storing a list is even uglier.

You could store ascii pickles, but those are pretty unpleasant to read in some cases.

Here's my solution: Encode it using a JSON encoder first! (I am using demjson)

Silly obvious code fragment:

def getValue(section,key,default=None):
    try:
        return JSON().decode(conf.get (section,key))
    except:
        return default

def setValue(section,key,value):
    value=JSON().encode(value)
    try:
        r=conf.set(section,key,value)
    except ConfigParser.NoSectionError:
        conf.add_section(section)
        r=conf.set(section,key,value)
    f=open(os.path.expanduser('~/.bartleblog/config'),'w')
    conf.write(f)
    return r

With just a little effort you can have a readable ascii typed python config file.

2007-05-10 14:07

Today's first hour of hacking...

... has been all about UI.

I have always had a problem when writing PyQt apps: stock icons.

Which ones should I use? Where are they?

I usually fished through the crystalsvg icon set until I found one that seemed to be what I needed, and then copied it to my app.

Sadly, that's annoying in several ways:

  1. Since those are PNG icons, you need to find the right size.
  2. Not all icons are there for all sizes!
  3. Because of 2, I need to check three or four folders to see all the icons.

So, I decided to cut my losses, and see what else could be done. And here it is:

bartleblog11.png

I am now using all SVG icons, from the reinhardt set that will look equally out of place in all OSs, but which I like (and I think look awesome with this relaxed Domino theme). And because they are all SVG, I don't care about sizes, and they are all in the same place, and all is good.

And whenever Oxygen is released, all I need to do is switch the files around and that's that. Which is nice, too.

Of course there is a catch... it does look out of place, and I expect many to find it ugly. So what, since I am the only user of this app! ;-)

2007-05-09 15:06

Today's two hours of hacking

  • Done with the main blog config dialog.
  • Fixed a dozen bugs
  • Generate the blog in a reasonable place
  • Fixed a lot of UI bugs (tab orders, sizes)

Still lots and lots of things to be done, tho.

2007-05-08 21:05

Making your QTextBrowser show remote images

It's remarkably easy to turn your QTextBrowser into a limited web browser, at least good enough to show images from the web.

Here's all the code:

from PyQt4 import QtCore,QtGui
import urllib, os, md5

class PBrowser(QtGui.QTextBrowser):
    def loadResource(self, type, name):
        url=unicode(name.toString())
        ret=QtCore.QVariant()
        if url.startswith('http://'):
            dn=os.path.expanduser('~/.bartleblog/cache/')
            if not os.path.isdir(dn):
                os.mkdir(dn)
            m=md5.new()
            m.update(url)
            fn=os.path.join(dn,m.hexdigest())
            if not os.path.isfile(fn):
                urllib.urlretrieve(url, fn)
            ret=QtGui.QTextBrowser.loadResource(self, type, QtCore.QUrl(fn))
        else:
            ret=QtGui.QTextBrowser.loadResource(self, type, name)
        return ret

And here's bartleblog taking advantage of it:

bartleblog10.png

It even has a primitive cache and everything ;-)

2007-05-08 17:44

PHP is for Lucre (II)

I probably should have charged more.

Things I learned in this project:

  • If they tell you the design is important, it doesn't matter that said design includes:

    • Fixed pixel-height text inputs (so when you resize fonts you can't see what you write)
    • Misaligned fields and labels

    So, I needed to use Smarty to generate the exact same HTML, working just as bad as the original. And it is thrice the work of doing it right.

  • If deployment is on Windows, regardless of having the same version of PHP, you are going to get in trouble one way or another.

    In order to do it quick & cheap & good, I used PEAR modules extensively. And now I have to make sure all of them are in the server... where there is no remote admin solution usable. So I have to be onsite to deploy. I can't even remember the last time I had to do such a thing :-(

So, I was cheaper than the other proposals, and maybe I was a bit too cheap. Live and learn :-)

2007-05-05 18:29

Giving BartleBlog the push it needs

I found a couple of hours to hack, and decided to spend them on BartleBlog.

Here's what got done:

  • UI for blog preferences (still need to do maybe ten of these little modules).
  • Search for files in reasonable places (instead of using the DB in the current folder, for example).
  • Started work on a first-use wizard
  • Started working on a original and simple help system (resource files, yay! ;-).
  • Implemented (lamely) posting of "stories" which are static pages that are not part of the blog's flow.

I intend to spend a couple of hours on it every day for the next ten days or so. After that, who knows, maybe a release will be reasonable.

2007-05-04 15:03

PHP is for lucre. Filthy, filthy lucre.

Got comissioned a small job for a customer:

  • Take a form done in ancient HTML using dreamweaver.
  • Redo it in PHP+MySQL
  • Make it work.
  • Add validation.
  • Make it look the same.

Ok, it's not terribly hard, but it's not terribly simple, either, specially if you are not all that up-to-date on PHP.

But hey, using HTML_QuickForms and Renderer_Tableless, it's kinda done.

But boy, do I now remember how I don't like PHP!

2007-05-01 19:39

What I am plotting...

...nothing.

Ok, here's what I have been doing:

  • Daddy work. Lots of it. Fun!
  • Actual work. Lots of it. Fun, but not so much!
  • Worked a little more in RA-Flip. Play/Stop/Pause is done. The editing mode and opening/saving files is pretty much done, so it will be fully functional as soon as I can spend an afternoon in it.
  • Tobogan (My slideshow software). It works nicely. There are many rough spots and it needs a few features:
    • Some way to edit the CSS for individual slides in a nice manner. Like setting backgrounds and colours (and not much else)
    • A nice way to save the slideshow so it ends all in a single folder.
    • Many UI details
    • Docs
    • A site
  • BartleBlog: it has worked well for me for a long while, since I have been posting this blog using it. And really, it's a nice blog, isn't it? ;-) But of course, there is stuff to be done:
    • Many UI details
    • Decent flickr and openomy uploaders/managers
    • Some extra gadgets I have in mind
    • Docs
    • A site

And that's all I have in mind right now. Except maybe a graphical Befunge interpreter, but that's not really important ;-)

Contents © 2000-2018 Roberto Alsina