--- /dev/null
+# vim: set expandtab tabstop=4 shiftwidth=4:
+
+class widget:
+ """Base widget class."""
+
+ def __init__(self, attrs = None):
+ if attrs is None:
+ attrs = {}
+ self.attrs = attrs
+
+ def __repr__(self):
+ return 'widget(attrs=' + str(self.attrs) + ')'
+
+ def render(self, userdata = None):
+ return str(self)
+
+
+class container(widget):
+ """Base container widget class."""
+
+ def __init__(self, attrs = None, content = None):
+ widget.__init__(self, attrs)
+ self.content = []
+ if content:
+ self.content.append(content)
+
+ def __repr__(self):
+ return 'container(attrs=' + str(self.attrs) + ', content=' \
+ + str(self.content) + ')'
+
+ def __len__(self):
+ return len(self.content)
+
+ def __getitem__(self, item):
+ return self.content[item]
+
+ def __setitem__(self, item, value):
+ self.content[item] = value
+
+ def __delitem__(self, item):
+ del self.content[item]
+
+ def __contains__(self, item):
+ return item in self.content
+
+ def __iter__(self):
+ for i in self.content: yield i
+
+ def append(self, content):
+ self.content.append(content)
+
+ def extend(self, content):
+ self.content.extend(content)
+
+ def renderContent(self, userdata = None):
+ out = ''
+ for content in self.content:
+ if isinstance(content, widget):
+ out += content.render(userdata)
+ else:
+ out += str(content);
+ return out;
+
+
+class fallback(container):
+ """Fallback widget to use when no specific widget is implemented."""
+
+ def __init__(self, name, attrs = None, content = None):
+ container.__init__(self, attrs, content)
+ self.name = name
+
+ def __repr__(self):
+ return 'fallback(name=\'' + str(self.name) + '\', attrs=' \
+ + str(self.attrs) + ', content=' + str(self.content) + ')'
+
--- /dev/null
+# vim: set expandtab tabstop=4 shiftwidth=4:
+
+class widget:
+ """Base widget class."""
+
+ def __init__(self, attrs = None):
+ if attrs is None:
+ attrs = {}
+ self.attrs = attrs
+
+ def __repr__(self):
+ return 'widget(attrs=' + str(self.attrs) + ')'
+
+ def render(self, userdata = None):
+ return str(self)
+
+
+class container(widget):
+ """Base container widget class."""
+
+ def __init__(self, attrs = None, content = None):
+ widget.__init__(self, attrs)
+ self.content = []
+ if content:
+ self.content.append(content)
+
+ def __repr__(self):
+ return 'container(attrs=' + str(self.attrs) + ', content=' \
+ + str(self.content) + ')'
+
+ def __len__(self):
+ return len(self.content)
+
+ def __getitem__(self, item):
+ return self.content[item]
+
+ def __setitem__(self, item, value):
+ self.content[item] = value
+
+ def __delitem__(self, item):
+ del self.content[item]
+
+ def __contains__(self, item):
+ return item in self.content
+
+ def __iter__(self):
+ for i in self.content: yield i
+
+ def append(self, content):
+ self.content.append(content)
+
+ def extend(self, content):
+ self.content.extend(content)
+
+ def renderContent(self, userdata = None):
+ out = ''
+ for content in self.content:
+ if isinstance(content, widget):
+ out += content.render(userdata)
+ else:
+ out += str(content);
+ return out;
+
+
+class fallback(container):
+ """Fallback widget to use when no specific widget is implemented."""
+
+ def __init__(self, name, attrs = None, content = None):
+ container.__init__(self, attrs, content)
+ self.name = name
+
+ def __repr__(self):
+ return 'fallback(name=\'' + str(self.name) + '\', attrs=' \
+ + str(self.attrs) + ', content=' + str(self.content) + ')'
+
--- /dev/null
+# vim: set expandtab tabstop=4 shiftwidth=4:
+
+import os
+
+class HIT:
+ """Hooks vs IT template engine."""
+ root = '.'
+ use_path = False
+ group = ''
+ postfix = '.tpl.html'
+
+ def __init__(self, root = root, postfix = postfix, use_path = use_path, group = group):
+ """Constructor."""
+ self.root = root
+ self.use_path = use_path
+ self.postfix = postfix
+ self.group = []
+ self.cache = {}
+ self.buffer = {}
+ self.group.append(group)
+
+ def parse(self, name, vars = {}, **more_vars):
+ """Parses a block of code."""
+ cache_key = self.group[len(self.group)-1] + os.sep + name + self.postfix
+ filename = self.root + os.sep + cache_key
+ if not self.cache.has_key(cache_key):
+ self.cache[cache_key] = file(filename).read()
+ out = self.cache[cache_key]
+ for key, val in vars.items():
+ out = out.replace('{' + key + '}', val)
+ for key, val in more_vars.items():
+ out = out.replace('{' + key + '}', val)
+ return out
+
+ def parseBuffered(self, name, vars = {}, **more_vars):
+ """Parses a block of code leaving the result in a temporary buffer."""
+ self.buffer[self.group + os.sep + name] += self.parse(name, vars);
+
+ def get_buffer(name):
+ """Gets a parsed buffer."""
+ return self.buffer[self.group + os.sep + name]
+
+ def pop_buffer(name):
+ """Pops a parsed buffer."""
+ buff = self.buffer[self.group + os.sep + name]
+ del self.buffer[self.group + os.sep + name]
+ return buff
+
+ def push_group(group = ''):
+ """Pushes a group to work on."""
+ self.group.append(group)
+
+ def pop_group():
+ """Pops the last group worked on."""
+ return self.group.pop()
+
--- /dev/null
+# vim: set expandtab tabstop=4 shiftwidth=4:
+
+from xml.sax import saxutils, make_parser
+from os import sep
+
+class Parser(xml.sax.saxutils.DefaultHandler):
+ """ByFE XML Parser."""
+
+ def __init__(self, fallback = None, cache = os.sep + 'tmp'):
+ """Constructor."""
+ self.fallback = fallback
+ self.cache = cache
+ self.parser = xml.sax.make_parser()
+ self.parser.setFeature(xml.sax.handler.feature_namespaces, 0)
+ self.parser.setContentHandler(self)
+
+ def startElement(self, name, attrs): # FIXME - arreglar a partir de aca.
+ """Start element handler."""
+ mods = name.lower().split('.')
+ classname = path.pop()
+ modulename = 'bife.' + mods.join('.')
+ try:
+ module = __import__(modulename, None, None, True)
+ classobj = getattr(module, classname)
+ obj = classobj(attrs)
+ except:
+ #if self.fallback:
+ obj = self.fallback(name, attrs)
+ #else:
+ # raise "Class not found '$class'."
+ self.stack[] = obj
+
+ def endElement(self, name):
+ """End element handler."""
+ end(self.stack)
+ current = self.stack[key(self.stack)]
+ self.stack.pop()
+ end(self.stack)
+ parent = self.stack[key(self.stack)]
+ if parent:
+ parent.addContents(current)
+ else:
+ self.root = current
+
+ def characters(self, data):
+ """Caracter data handler."""
+ end(self.stack)
+ current = self.stack[key(self.stack)]
+ current.addContents(data)
+
+ def parse(self, file):
+ # TODO - cache
+ if !self.parser.parse(self.parser, file)
+
+ def parseString(self, string):
+ if !self.parser.parseString(self.parser, string, self)
--- /dev/null
+<?xml version="1.0" encoding="UTF-8"?>
+<Page title="Hello world!">
+ <Title>BIFE is working!</Title>
+ <H3>Translate Fallback is working too!</H3>
+ <P>This is a very bad use for BIFE, because I'm writing HTML :-P</P>
+ <Title>A little of 'real' use</Title>
+ <P>Here's a <Link url="link.xbf">link</Link> to another BIFE file</P>
+ <Title>Links to subdirectories</Title>
+ <P>Here's a <Link url="dir/">file in a directory</Link>.</P>
+ <P>And a <Link url="dir/subdir/">file in a subdirectory</Link>.</P>
+ <P>Finally a <Link url="otherdir/">file in a another directory</Link>.</P>
+ <HR noshade="noshade" size="2"/>
+ <Link data-s="1" data-b="1">View BIFE (xbf) source</Link> |
+ <Link data-s="1">View PHP source</Link>
+</Page>
--- /dev/null
+Este es un template. Todos los valores como este: {VALOR}
+van a ser reemplazados.
+También podemos poner algo anidado:
+{ANIDADO}
+
+Chau.
--- /dev/null
+Esto está contenido en otro lado.
+La hora es {HORA}
+Fin de texto incluido desde {VALOR}
--- /dev/null
+#!/usr/bin/env python
+# vim: set et ts=4 sw=4:
+
+from xml.sax import saxutils, make_parser
+from xml.sax.handler import feature_namespaces
+from sys import argv
+
+class Parser(saxutils.DefaultHandler):
+ def __init__(self):
+ self.level = 0
+ self.parser = make_parser()
+ self.parser.setFeature(feature_namespaces, 0)
+ self.parser.setContentHandler(self)
+
+ def startElement(self, name, attrs):
+ print self.level * ' ', 'Inicio:', name
+ self.level += 1
+ for key, val in attrs.items():
+ print self.level * ' ', key, '=', val
+
+ def endElement(self, name):
+ self.level -= 1
+ print self.level * ' ', 'Fin:', name
+
+ def characters(self, data):
+ print self.level * ' ', 'Datos:', data
+
+ def parse(self, file):
+ self.parser.parse(file)
+
+if __name__ == '__main__':
+ Parser().parse(argv[1])
--- /dev/null
+#!/usr/bin/env python
+# vim: set et ts=4 sw=4 tw=0 fdm=syntax fileencoding=iso-8859-1:
+
+import sqlite
+
+def printall(db, table='test'):
+ """Funcion que imprime todos los elementos de una tabla de sqlite.
+ """
+ dbh = sqlite.connect(db)
+ dbr = dbh.cursor()
+ dbr.execute('SELECT * FROM ' + table)
+ for row in dbr.fetchall():
+ for k, v in row: print k, '=', v
+