Ir al contenido principal

Ralsina.Me — El sitio web de Roberto Alsina

Publicaciones sobre programming (publicaciones antiguas, página 20)

BartleBlog change: Mako Templates

Since the very be­gin­ning, Bartle­Blog has been us­ing Cher­ry­Tem­plate for its out­put for­mat­ting need­s. I like it, be­cause it's very sim­ple.

How­ev­er, it had grown rather cum­ber­some.

Specif­i­cal­ly, most pages in a blog are sort of a page tem­plate with a body tem­plate in­side (the main con­tent).

To do that on Cher­ry­Tem­plate, I used a two-­pass ap­proach: gen­er­ate the body, then pass it as pa­ram­e­ter to the page tem­plate.

Which is a pain in some cas­es be­cause you end ba­si­cal­ly hav­ing to do a ren­der­ing func­tion for each kind of page, or some crazy-evil func­tion (what I did).

Ex­plor­ing the dif­fer­ent python tem­plate en­gi­nes, I ran in­to Mako and de­cid­ed to give it a whirl. It looks good.

The ap­proach is a bit dif­fer­en­t, it is much more pow­er­ful, but you can still use it sim­ply if you can.

And the main fea­ture was tem­plate in­her­i­tance. Us­ing that, no more in­ner and out­er tem­plates, baby!

Oh, and per­for­mance is bet­ter:

Cherry

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


Mako

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

This is for com­plete­ly reren­der­ing the whole 7 years, 574 post­s, 40 stat­ic ar­ti­cles, 14 cat­e­go­ry blog, and there is tons of op­ti­miza­tions to be done.

BTW: this is how you reren­der the whole blog:

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

New Bartleblog Feature: Menu Editor

Took a while to im­ple­men­t, but Bartle­Blog fi­nal­ly got a func­tion­al menu ed­i­tor:

bartleblog12.png

Right now, it on­ly works with the mootool­s-based menu gad­get, but I will start work­ing on the ya­hoo menu ver­sion in a mo­men­t.

The on­ly thing not work­ing is the pre­view but­ton, be­cause it needs more sup­port on the back­end side.

Python Trick: Save anything in config files

The Python con­fig ob­jects are con­ve­nient and sim­ple, but they have a prob­lem: you can on­ly save strings. That means you need to store num­bers as strings and re­mem­ber to use the get­int()/get­float() meth­ods (or co­erce by hand!), which is er­ror prone and an­ti-python­ic. Stor­ing a list is even ugli­er.

You could store ascii pick­les, but those are pret­ty un­pleas­ant to read in some cas­es.

Here's my so­lu­tion: En­code it us­ing a JSON en­coder first! (I am us­ing demj­son)

Sil­ly ob­vi­ous code frag­men­t:

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 lit­tle ef­fort you can have a read­able ascii typed python con­fig file.

Today's first hour of hacking...

... has been all about UI.

I have al­ways had a prob­lem when writ­ing PyQt app­s: stock icon­s.

Which ones should I use? Where are they?

I usu­al­ly fished through the crys­talsvg icon set un­til I found one that seemed to be what I need­ed, and then copied it to my ap­p.

Sad­ly, that's an­noy­ing in sev­er­al ways:

  1. Since those are PNG icon­s, you need to find the right size.

  2. Not all icons are there for all sizes!

  3. Be­­cause of 2, I need to check three or four fold­ers to see all the icon­s.

So, I de­cid­ed to cut my loss­es, and see what else could be done. And here it is:

bartleblog11.png

I am now us­ing all SVG icon­s, from the rein­hardt set that will look equal­ly out of place in all OS­s, but which I like (and I think look awe­some with this re­laxed Domi­no the­me). And be­cause they are all SVG, I don't care about sizes, and they are all in the same place, and all is good.

And when­ev­er Oxy­gen is re­leased, 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 ex­pect many to find it ug­ly. So what, since I am the on­ly us­er of this ap­p! ;-)

Today's two hours of hacking

  • Done with the main blog con­­fig di­a­log.

  • Fixed a dozen bugs

  • Gen­er­ate the blog in a rea­­son­able place

  • Fixed a lot of UI bugs (tab or­der­s, sizes)

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


Contents © 2000-2024 Roberto Alsina