New custom timeline widget

Tired of looking for a timeline widget in PyGTK for Vazaar I’ve decided to develop it  by myself.

It’s very simple: 1 calendar and 3 treeviews.

The user clicks a day in the calendar and with the selected date Vazaar asks to the database for those resources created that day, the day before and the day after.

Here is a screenshot:

vazaar-timeline_0

You can find an example in the repository.



~~ · ~~


How to fix your sqlite database after corruption

If you get something like this:

Traceback (most recent call last):
  File "./bin/vazaar", line 79, in
    Vazaar(options, args)
  File "/home/t00m/dev/trunk/vazaarlib/main.py", line 61, in __init__
    self.store = StoreManager(self)
  File "/home/t00m/dev/trunk/vazaarlib/store.py", line 54, in __init__
    self.__get_graph()
  File "/home/t00m/dev/trunk/vazaarlib/store.py", line 85, in __get_graph
    self.graph.store.open(self.options)
  File "/usr/lib/pymodules/python2.6/rdflib/store/Sleepycat.py", line 77, in open
    index.open(index_name, dbname, dbtype, dbopenflags|db.DB_CREATE, dbmode)

MemoryError: (12, 'Cannot allocate memory -- Lock table is out of available locker entries')

Just go to database directory and delete all files similar to __db.

Reason:
This problem occurs when Vazaar accesses the Berkeley database files, it makes temporary locker entries within the tables while it searches for data. If Vazaar crashes often, this issue will occur much sooner because the locks are never cleared.

Tip found at http://www.linuxmaza.com/system-administration/solved-lock-table-is-out-of-available-locker-entries-yum-install-yum-upgrade-3/



~~ · ~~


PyGTK XDot Widget

XDot es un magnífico componente para representar grafos interactivos con Python, Graphviz y GTK.

De la web del autor:

xdot.py is an interactive viewer for graphs written in Graphviz’s dot language.

It uses internally the graphviz’s xdot output format as an intermediate format, and PyGTK and Cairo for rendering.
xdot.py can be used either as a standalone application from command line, or as a library embedded in your python application.

Features

  • Since it doesn’t use bitmaps it is fast and has a small memory footprint.
  • Arbitrary zoom.
  • Keyboard/mouse navigation.
  • Supports events on the nodes with URLs.
  • Animated jumping between nodes.
  • Highlights node/edge under mouse.

Este código ilustra como usar el widget en una aplicación. En la mía, genero dinámicamente el código dot según los nodos (recursos) que haya eligido el usuario. En los adjuntos puedes encontrar otro ejemplo mezclando rdflib y xdot:

Read the rest of this entry »



~~ · ~~


Question about UTF-8

Sometimes, I’ve noticed that my application doesn’t deal very well with UTF-8 input/output. UTF-8 is mandatory but it’s a headache when you have to spend your time guessing what is wrong and how display the right characteres. I’ve found this function to force encode any text into UTF-8.

keydict = {
    ‘zwnj’: , ‘aring’: \xc3\xa5′, ‘gt’: ‘>’, ‘yen’: \xc2\xa5′,
    ‘ograve’: \xc3\xb2′, ‘Chi’: \xce\xa7′, ‘delta’: \xce\xb4′,
    ‘rang’: \xe3\x80\x89′, ‘sup’: \xe2\x8a\x83′,
    ‘trade’: \xe2\x84\xa2′, ‘ge’: \xe2\x89\xa5′, ‘xi’: \xce\xbe’,
    ‘upsih’: \xcf\x92′, ‘nbsp’: , ‘Atilde’: \xc3\x83′,
    ‘radic’: \xe2\x88\x9a’, ‘otimes’: \xe2\x8a\x97′,
    ‘aelig’: \xc3\xa6′, ‘oelig’: \xc5\x93′, ‘equiv’: \xe2\x89\xa1′,
    ‘ni’: \xe2\x88\x8b’, ‘le’: \xe2\x89\xa4′, ‘auml’: \xc3\xa4′,
    ‘cup’: \xe2\x88\xaa’, ‘Epsilon’: \xce\x95′, ‘otilde’: \xc3\xb5′,
    ‘lt’: ‘< ‘, ‘Icirc’: \xc3\x8e’, ‘shy’: , ‘Eacute’: \xc3\x89′,
    ‘ndash’: \xe2\x80\x93′, ‘yacute’: \xc3\xbd’,
    ‘Prime’: \xe2\x80\xb3′, ‘prime’: \xe2\x80\xb2′, ‘psi’: \xcf\x88′,
    ‘Kappa’: \xce\x9a’, ‘rsaquo’: \xe2\x80\xba’, ‘Tau’: \xce\xa4′,
    ‘alefsym’: \xe2\x84\xb5′, ‘ocirc’: \xc3\xb4′, ‘lrm’: ,
    ‘zwj’: , ‘cedil’: \xc2\xb8′, ‘Ecirc’: \xc3\x8a’,
    ‘not’: \xc2\xac’, ‘Dagger’: \xe2\x80\xa1′, ‘AElig’: \xc3\x86′,
    ‘oslash’: \xc3\xb8′, ‘acute’: \xc2\xb4′, ‘lceil’: \xe2\x8c\x88′,
    ‘iquest’: \xc2\xbf’, ‘uacute’: \xc3\xba’, ‘laquo’: \xc2\xab’,
    ‘dArr’: \xe2\x87\x93′, ‘rdquo’: \xe2\x80\x9d’,
    ‘Ntilde’: \xc3\x91′, ‘Igrave’: \xc3\x8c’, ‘nu’: \xce\xbd’,
    ‘lsquo’: \xe2\x80\x98′, ‘lsaquo’: \xe2\x80\xb9′,
    ‘sube’: \xe2\x8a\x86′, ‘rarr’: \xe2\x86\x92′,
    ‘sdot’: \xe2\x8b\x85′, ‘supe’: \xe2\x8a\x87′, ‘Yacute’: \xc3\x9d’,
    ‘lfloor’: \xe2\x8c\x8a’, ‘lArr’: \xe2\x87\x90′, ‘Auml’: \xc3\x84′,
    ‘asymp’: \xe2\x89\x88′, ‘Otilde’: \xc3\x95′, ‘szlig’: \xc3\x9f’,
    ‘clubs’: \xe2\x99\xa3′, ‘agrave’: \xc3\xa0′, ‘Ocirc’: \xc3\x94′,
    ‘Theta’: \xce\x98′, ‘Pi’: \xce\xa0′, ‘harr’: \xe2\x86\x94′,
    ‘Scaron’: \xc5\xa0′, ‘frac14′: \xc2\xbc’, ‘egrave’: \xc3\xa8′,
    ‘sub’: \xe2\x8a\x82′, ‘iexcl’: \xc2\xa1′, ‘frac12′: \xc2\xbd’,
    ‘ordf’: \xc2\xaa’, ‘rlm’: , ‘prop’: \xe2\x88\x9d’,
    ‘circ’: \xcb\x86′, ‘ntilde’: \xc3\xb1′, ‘apos’: "’",
    ‘atilde’: \xc3\xa3′, ‘THORN’: \xc3\x9e’, ‘theta’: \xce\xb8′,
    ‘prod’: \xe2\x88\x8f’, ‘nsub’: \xe2\x8a\x84′,
    ‘hArr’: \xe2\x87\x94′, ‘rArr’: \xe2\x87\x92′, ‘Oslash’: \xc3\x98′,
    ‘omicron’: \xce\xbf’, ‘infin’: \xe2\x88\x9e’, ‘yuml’: \xc3\xbf’,
    ‘Mu’: \xce\x9c’, ‘Psi’: \xce\xa8′, ‘thinsp’: \xe2\x80\x89′,
    ‘ecirc’: \xc3\xaa’, ‘bdquo’: \xe2\x80\x9e’, ‘Sigma’: \xce\xa3′,
    ‘kappa’: \xce\xba’, ‘Aring’: \xc3\x85′, ’tilde’: \xcb\x9c’,
    ‘emsp’: \xe2\x80\x83′, ‘mdash’: \xe2\x80\x94′,
    ‘uarr’: \xe2\x86\x91′, ‘permil’: \xe2\x80\xb0′, ‘tau’: \xcf\x84′,
    ‘Ugrave’: \xc3\x99′, ‘fnof’: \xc6\x92′, ‘Agrave’: \xc3\x80′,
    ‘sup1′: \xc2\xb9′, ‘forall’: \xe2\x88\x80′, ‘eth’: \xc3\xb0′,
    ‘rceil’: \xe2\x8c\x89′, ‘iuml’: \xc3\xaf’, ‘gamma’: \xce\xb3′,
    ‘lambda’: \xce\xbb’, ‘reg’: \xc2\xae’, ‘Egrave’: \xc3\x88′,
    ‘sup3′: \xc2\xb3′, ‘dagger’: \xe2\x80\xa0′, ‘divide’: \xc3\xb7′,
    ‘Ouml’: \xc3\x96′, ‘image’: \xe2\x84\x91′, ‘hellip’: \xe2\x80\xa6′,
    ‘igrave’: \xc3\xac’, ‘Yuml’: \xc5\xb8′, ‘Oacute’: \xc3\x93′,
    ‘pound’: \xc2\xa3′, ‘eacute’: \xc3\xa9′, ‘frasl’: \xe2\x81\x84′,
    ‘ETH’: \xc3\x90′, ‘lowast’: \xe2\x88\x97′, ‘Nu’: \xce\x9d’,
    ‘plusmn’: \xc2\xb1′, ‘chi’: \xcf\x87′, ‘sup2′: \xc2\xb2′,
    ‘frac34′: \xc2\xbe’, ‘Aacute’: \xc3\x81′, ‘cent’: \xc2\xa2′,
    ‘oline’: \xe2\x80\xbe’, ‘Beta’: \xce\x92′, ‘perp’: \xe2\x8a\xa5′,
    ‘Delta’: \xce\x94′, ‘loz’: \xe2\x97\x8a’, ‘pi’: \xcf\x80′,
    ‘iota’: \xce\xb9′, ‘empty’: \xe2\x88\x85′, ‘euml’: \xc3\xab’,
    ‘brvbar’: \xc2\xa6′, ‘iacute’: \xc3\xad’, ‘para’: \xc2\xb6′,
    ‘ordm’: \xc2\xba’, ‘epsilon’: \xce\xb5′, ‘uuml’: \xc3\xbc’,
    ‘OElig’: \xc5\x92′, ‘there4′: \xe2\x88\xb4′,
    ‘part’: \xe2\x88\x82′, ‘icirc’: \xc3\xae’, ‘bull’: \xe2\x80\xa2′,
    ‘upsilon’: \xcf\x85′, ‘copy’: \xc2\xa9′, ‘Iuml’: \xc3\x8f’,
    ‘Lambda’: \xce\x9b’, ‘Xi’: \xce\x9e’, ‘ensp’: \xe2\x80\x82′,
    ‘ccedil’: \xc3\xa7′, ‘Ucirc’: \xc3\x9b’, ‘cap’: \xe2\x88\xa9′,
    ‘Upsilon’: \xce\xa5′, ‘mu’: \xce\xbc’, ‘sigmaf’: \xcf\x82′,
    ‘scaron’: \xc5\xa1′, ‘nabla’: \xe2\x88\x87′,
    ‘isin’: \xe2\x88\x88′, ‘Zeta’: \xce\x96′, ‘minus’: \xe2\x88\x92′,
    ‘deg’: \xc2\xb0′, ‘and’: \xe2\x88\xa7′, ‘real’: \xe2\x84\x9c’,
    ‘ang’: \xe2\x88\xa0′, ‘curren’: \xc2\xa4′, ‘int’: \xe2\x88\xab’,
    ‘ucirc’: \xc3\xbb’, ‘rfloor’: \xe2\x8c\x8b’, ‘crarr’: \xe2\x86\xb5′,
    ‘ugrave’: \xc3\xb9′, ‘notin’: \xe2\x88\x89′, ‘exist’: \xe2\x88\x83′,
    ‘cong’: \xe2\x89\x85′, ‘Uuml’: \xc3\x9c’, ‘oplus’: \xe2\x8a\x95′,
    ‘times’: \xc3\x97′, ‘Acirc’: \xc3\x82′, ‘piv’: \xcf\x96′,
    ‘Euml’: \xc3\x8b’, ‘Phi’: \xce\xa6′, ‘raquo’: \xc2\xbb’,
    ‘Iacute’: \xc3\x8d’, ‘quot’: , ‘Uacute’: \xc3\x9a’,
    ‘Omicron’: \xce\x9f’, ‘ne’: \xe2\x89\xa0′, ‘Iota’: \xce\x99′,
    ‘eta’: \xce\xb7′, ‘sbquo’: \xe2\x80\x9a’, ‘Rho’: \xce\xa1′,
    ‘darr’: \xe2\x86\x93′, ‘Alpha’: \xce\x91′, ‘zeta’: \xce\xb6′,
    ‘Omega’: \xce\xa9′, ‘acirc’: \xc3\xa2′, ‘sim’: \xe2\x88\xbc’,
    ‘phi’: \xcf\x86′, ‘diams’: \xe2\x99\xa6′, ‘macr’: \xc2\xaf’,
    ‘larr’: \xe2\x86\x90′, ‘Ccedil’: \xc3\x87′, ‘aacute’: \xc3\xa1′,
    ‘uArr’: \xe2\x87\x91′, ‘beta’: \xce\xb2′, ‘Eta’: \xce\x97′,
    ‘weierp’: \xe2\x84\x98′, ‘rho’: \xcf\x81′, ‘micro’: \xc2\xb5′,
    ‘alpha’: \xce\xb1′, ‘omega’: \xcf\x89′, ‘middot’: \xc2\xb7′,
    ‘Gamma’: \xce\x93′, ‘euro’: \xe2\x82\xac’, ‘lang’: \xe3\x80\x88′,
    ‘spades’: \xe2\x99\xa0′, ‘amp’: ‘and’, ‘rsquo’: \xe2\x80\x99′,
    ‘uml’: \xc2\xa8′, ‘thorn’: \xc3\xbe’, ‘ouml’: \xc3\xb6′,
    ‘thetasym’: \xcf\x91′, ‘or’: \xe2\x88\xa8′, ‘Ograve’: \xc3\x92′,
    ‘sect’: \xc2\xa7′, ‘ldquo’: \xe2\x80\x9c’,
    ‘hearts’: \xe2\x99\xa5′, ‘sigma’: \xcf\x83′,‘sum’: \xe2\x88\x91′,
    ‘oacute’: \xc3\xb3′
    }

def decode_unicode_references(data):
    try:
        text = data
        pos1 = text.find(‘&’)
        pos2 = text.find(‘;’)
        if (((pos1 != -1) or (pos2 != -1)) and (pos2 – pos1 < 15)):
            entity = text[pos1+1: pos2]
            a = text[pos1:pos2+1]
            return text.replace(a, keydict[entity])
        else:
            return data
    except:
        return data

def str2utf8(data):
    data = decode_unicode_references(data)
    guess_list=[‘utf-8′,‘iso8859-1′,‘iso8859-2′, \
                        ‘iso8859-15′,‘ascii’]
    encoding = ‘iso8859-1′
    text = None
    string = None
    for best_enc in guess_list:
        try:
            encoding = best_enc
            text = data.decode(encoding).encode(‘utf-8′)
            break
        except Exception, error:
            #print "ERROR:", error
            pass

    return text

How do you deal with UTF-8, python and gtk? Am I doing right? Do you know a better way?



~~ · ~~


Reports with Cheetah templates

Start Here Report I’ve found Cheetah template engine very useful to build custom reports. With a very small set of syntactic rules you can mix html and python to generate dynamic content.

This is a small example extracted from my project:

filename default.html:

#import time
#from vazaarlib.i18n import _
#from vazaarlib.utils import fuzzy_date, str2utf8, get_resource_contents
#from vazaarlib.utils import get_beautiful_rtype
#from vazaarlib.namespaces import *
#from vazaarlib.constants import GPATH, LPATH

#set $user_owner_pim_uri = $self.gui.app.cfgmgr.get_value(‘PIMO’, ‘PIM’)
#set $user_owner_uri = $self.gui.ask.get_owner($user_owner_pim_uri)
#set $user_owner_email_uri = $self.gui.ask.get_owner_email($user_owner_uri)
#set $uemail = $self.gui.app.vstore.get_property($user_owner_email_uri)
#set $uowner = $self.gui.app.vstore.get_property($user_owner_uri)
#set $totres = $self.gui.ask.get_total_resources()
#set $typefreq = $self.gui.ask.get_qname_frequency($RDF.type)
#set $medate = time.strftime("%A, %d %b %Y", $time.localtime(time.time()))
#set $metime = time.strftime("%H:%M", $time.localtime(time.time()))
#set $css = $open($GPATH['CSS'] + ‘/default/style.css’, ‘r’).read()

< !DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">

Vazaar
<!–
$css
–>
<div>
<div><strong>$medate @ $metime</strong></div>
$_("Hi") <strong><em>$uowner</em></strong>, your Knowledge Database has <strong>$totres</strong> resources

#for $rtype in $typefreq:
    #set $title = get_beautiful_rtype($rtype)
    #set $freq = $typefreq[$rtype]

#end for
<table style="font-size: 10pt;" cellspacing="3px" cellpadding="3px" width="100%">
<tbody>
<tr id="row">
<td style="border-bottom: 1px solid gray;" align="left"><strong>$title</strong></td>
<td style="border-bottom: 0px dotted gray;" align="right">$freq</td>
</tr>
<tr>
<td style="border-top: 0px solid black; color: black;" align="right">$_("<strong>Total resources</strong>: ")</td>
<td style="border-top: 1px solid black; color: black;" align="right"><strong>$totres</strong></td>
</tr>
</tbody>
</table>
</div>

filename reports.py:

#!/usr/bin/python
# -*- coding: utf-8 -*-
# File: reports.py
# Author: Tomás Vírseda
# License: GPL v3
# Description: Report builder for Vazaar

[]
from Cheetah.Template import Template

class Report():
    def __init__(self, gui):
        …
        …

    def build_starthere_report(self, params):
        template = GPATH[‘TEMPLATES’] + ‘/default.html’
        report = Template(file=template)
        report.css_content = self.css_content
        report.author = ‘Tomás Vírseda García’
        report.date = ’14/12/2010′
        report.params = params

        return str(report)
[]

Check the code at vazaarlib/ui/reports.py in the source project repository.



~~ · ~~