]> git.llucax.com Git - software/bife/byfe.git/commitdiff
Added an experimental implementation of BIFE in python. master svn_import
authorLeandro Lucarella <llucax@gmail.com>
Mon, 11 Aug 2003 16:16:55 +0000 (16:16 +0000)
committerLeandro Lucarella <llucax@gmail.com>
Mon, 11 Aug 2003 16:16:55 +0000 (16:16 +0000)
byfe/__init__.py [new file with mode: 0644]
byfe/core.py [new file with mode: 0644]
byfe/hit.py [new file with mode: 0644]
byfe/parser.py [new file with mode: 0644]
index.xbf [new file with mode: 0644]
test.tpl [new file with mode: 0644]
test2.tpl [new file with mode: 0644]
tests/parser_test.py [new file with mode: 0755]
tests/sqlite.py [new file with mode: 0755]

diff --git a/byfe/__init__.py b/byfe/__init__.py
new file mode 100644 (file)
index 0000000..043414a
--- /dev/null
@@ -0,0 +1,75 @@
+# 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) + ')'
+
diff --git a/byfe/core.py b/byfe/core.py
new file mode 100644 (file)
index 0000000..043414a
--- /dev/null
@@ -0,0 +1,75 @@
+# 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) + ')'
+
diff --git a/byfe/hit.py b/byfe/hit.py
new file mode 100644 (file)
index 0000000..a4527d1
--- /dev/null
@@ -0,0 +1,56 @@
+# 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()
+
diff --git a/byfe/parser.py b/byfe/parser.py
new file mode 100644 (file)
index 0000000..eda9872
--- /dev/null
@@ -0,0 +1,56 @@
+# 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)
diff --git a/index.xbf b/index.xbf
new file mode 100644 (file)
index 0000000..3e846c1
--- /dev/null
+++ b/index.xbf
@@ -0,0 +1,15 @@
+<?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>
diff --git a/test.tpl b/test.tpl
new file mode 100644 (file)
index 0000000..5adc97d
--- /dev/null
+++ b/test.tpl
@@ -0,0 +1,6 @@
+Este es un template. Todos los valores como este: {VALOR}
+van a ser reemplazados.
+También podemos poner algo anidado:
+{ANIDADO}
+
+Chau.
diff --git a/test2.tpl b/test2.tpl
new file mode 100644 (file)
index 0000000..f31f2f6
--- /dev/null
+++ b/test2.tpl
@@ -0,0 +1,3 @@
+Esto está contenido en otro lado.
+La hora es {HORA}
+Fin de texto incluido desde {VALOR}
diff --git a/tests/parser_test.py b/tests/parser_test.py
new file mode 100755 (executable)
index 0000000..29f3f68
--- /dev/null
@@ -0,0 +1,32 @@
+#!/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])
diff --git a/tests/sqlite.py b/tests/sqlite.py
new file mode 100755 (executable)
index 0000000..079d876
--- /dev/null
@@ -0,0 +1,14 @@
+#!/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
+