Changeset 4396:ef01dc6c9393 in livinglogic.python.xist

Show
Ignore:
Timestamp:
03/04/11 15:18:55 (9 years ago)
Author:
Walter Doerwald <walter@…>
Branch:
default
Message:

Change db2ul4 variables.

Replace connect with oracle, sqlite and mysql.

Files:
3 modified

Legend:

Unmodified
Added
Removed
  • MIGRATION.rst

    r4350 r4396  
     1Migrating to version 3.18 
     2========================= 
     3 
     4Changes to ``db2ul4`` 
     5--------------------- 
     6 
     7*   The variables available in UL4 templates used by ``db2ul4`` have changed. 
     8    Instead of a ``connect`` object, there are now three objects for each 
     9    supported database (i.e. ``oracle``, ``sqlite`` and ``mysql``). To update 
     10    your template replace:: 
     11 
     12        connect["oracle:user/pwd@db"] 
     13 
     14    with:: 
     15 
     16        oracle["user/pwd@db"] 
     17 
     18 
    119Migrating to version 3.17 
    220========================= 
    321 
    422Changes to :mod:`ll.misc` 
    5 ------------------------------ 
     23------------------------- 
    624 
    725*   :func:`ll.misc.javastring` has been renamed to :func:`ll.misc.javaexpr`. 
  • src/ll/scripts/__init__.py

    r3930 r4396  
    77## 
    88## See ll/__init__.py for the license 
     9 
     10""" 
     11This package contains the following scripts: 
     12 
     13    ``db2ul4`` 
     14        ``db2ul4`` renders an UL4 template. The available template variables 
     15        allow system command and database access. Supported databases are Oracle, 
     16        sqllite and MySQL. 
     17 
     18    ``uls`` 
     19        ``uls`` is an URL-enabled version of the ``ls`` command for listing 
     20        directory content. It uses :mod:`ll.url` and supports ``ssh`` and 
     21        ``oracle`` URLs (via :mod:`ll.orasql`). 
     22 
     23    ``ucp`` 
     24        ``ucp`` is an URL-enabled version of the ``cp`` command for copying files 
     25        (and file-like objects). It supports ``ssh`` and ``oracle`` URLs for both 
     26        source and target files. 
     27 
     28    ``ucat`` 
     29        ``ucat`` is an URL-enabled version of the ``cat`` command for printing 
     30        files (and file-like objects). 
     31 
     32These scripts can either be called via Pythons :option:`-m` option:: 
     33 
     34    python -mll.scripts.db2ul4 --help 
     35 
     36or as a simple script installed in the search path:: 
     37 
     38    db2ul4 --help 
     39""" 
     40 
     41 
     42__docformat__ = "reStructuredText" 
  • src/ll/scripts/db2ul4.py

    r4392 r4396  
    11#!/usr/bin/env python 
    22# -*- coding: utf-8 -*- 
     3 
     4""" 
     5``db2ul4`` is a script that can be used for rendering database content into 
     6a UL4 template. An example template that connects to an Oracle database and 
     7outputs the content of a ``person`` table into an XML file looks like this:: 
     8 
     9    <?xml version='1.0' encoding='<?print encoding?>'?> 
     10    <?code db = oracle["user/pwd@db"]?> 
     11    <persons> 
     12        <?for p in db.iter["select id, firstname, lastname from person"]?> 
     13            <person id="<?printx p.id?>"> 
     14                <firstname><?printx p.firstname?></firstname> 
     15                <lastname><?printx p.lastname?></lastname> 
     16            </person> 
     17        <?end for?> 
     18    </persons> 
     19""" 
     20 
    321 
    422import sys, os, argparse, codecs 
    523 
    624from ll import ul4c 
     25 
     26 
     27__docformat__ = "reStructuredText" 
    728 
    829 
     
    3253 
    3354 
    34 class DB(object): 
     55class Connection(object): 
     56    def __init__(self, connection): 
     57        self.connection = connection 
     58 
    3559    def __getitem__(self, key): 
    3660        if key == "iter": 
     
    4266 
    4367 
    44 class Oracle(DB): 
    45     def __init__(self, connectstring): 
     68class Oracle(object): 
     69    def __getitem__(self, connectstring): 
    4670        from ll import orasql 
    47         self.connection = orasql.connect(connectstring, readlobs=True) 
     71        return Connection(orasql.connect(connectstring, readlobs=True)) 
    4872 
    4973 
    50 class SQLite(DB): 
    51     def __init__(self, connectstring): 
     74class SQLite(object): 
     75    def __getitem__(self, connectstring): 
    5276        import sqlite3 
    5377        connection = sqlite3.connect(connectstring) 
     
    5882                return sqlite3.Row.__getitem__(self, key) 
    5983        connection.row_factory = Row 
    60         self.connection = connection 
     84        return Connection(connection) 
    6185 
    6286 
    63 class MySQL(DB): 
    64     def __init__(self, connectstring): 
     87class MySQL(object): 
     88    def __getitem__(self, connectstring): 
    6589        import MySQLdb 
    6690        from MySQLdb import cursors 
     
    6892        (user, passwd) = user.split("/") 
    6993        (host, db) = host.split("/") 
    70         self.connection = MySQLdb.connect(user=user, passwd=passwd, host=host, db=db, use_unicode=True, cursorclass=cursors.DictCursor) 
    71  
    72  
    73 class Connect(object): 
    74     def __getitem__(self, connectstring): 
    75         (type, sep, connectstring) = connectstring.partition(":") 
    76         if type == "oracle": 
    77             return Oracle(connectstring) 
    78         elif type == "sqlite": 
    79             return SQLite(connectstring) 
    80         elif type == "mysql": 
    81             return MySQL(connectstring) 
    82         else: 
    83             raise KeyError(connectstring) 
     94        return Connection(MySQLdb.connect(user=user, passwd=passwd, host=host, db=db, use_unicode=True, cursorclass=cursors.DictCursor)) 
    8495 
    8596 
     
    109120        templates[templatename] = template 
    110121 
    111     vars = dict(connect=Connect(), system=System(), encoding=args.outputencoding, templates=templates) 
     122    vars = dict( 
     123        oracle=Oracle(), 
     124        sqlite=SQLite(), 
     125        mysql=MySQL(), 
     126        system=System(), 
     127        encoding=args.outputencoding, 
     128        templates=templates, 
     129    ) 
    112130    for part in codecs.iterencode(maintemplate.render(**vars), args.outputencoding): 
    113131        sys.stdout.write(part)