<s> """<STR_LIT>""" <EOL> import os , re <EOL> from types import StringType <EOL> __author__ = "<STR_LIT>" <EOL> __version__ = "<STR_LIT>" [ <NUM_LIT:11> : - <NUM_LIT:2> ] <EOL> __OS__ = os . name <EOL> choose = lambda bool , a , b : ( bool and [ a ] or [ b ] ) [ <NUM_LIT:0> ] <EOL> def console ( rows , headers = ( ) ) : <EOL> """<STR_LIT>""" <EOL> output = [ ] <EOL> headers = map ( lambda header : header . upper ( ) , list ( map ( lambda x : x or "<STR_LIT>" , headers ) ) ) <EOL> collen = map ( len , headers ) <EOL> output . append ( headers ) <EOL> if rows and len ( rows ) > <NUM_LIT:0> : <EOL> for row in rows : <EOL> row = map ( lambda x : str ( x ) , row ) <EOL> for i in range ( len ( row ) ) : <EOL> entry = row [ i ] <EOL> if collen [ i ] < len ( entry ) : <EOL> collen [ i ] = len ( entry ) <EOL> output . append ( row ) <EOL> if len ( output ) == <NUM_LIT:1> : <EOL> affected = "<STR_LIT>" <EOL> elif len ( output ) == <NUM_LIT:2> : <EOL> affected = "<STR_LIT>" <EOL> else : <EOL> affected = "<STR_LIT>" % ( len ( output ) - <NUM_LIT:1> ) <EOL> for i in range ( len ( output ) ) : <EOL> row = output [ i ] <EOL> l = [ ] <EOL> for j in range ( len ( row ) ) : <EOL> l . append ( '<STR_LIT>' % ( collen [ j ] , row [ j ] ) ) <EOL> output [ i ] = "<STR_LIT>" . join ( l ) <EOL> totallen = len ( output [ <NUM_LIT:0> ] ) <EOL> output [ <NUM_LIT:1> : <NUM_LIT:1> ] = [ "<STR_LIT:->" * ( totallen / len ( "<STR_LIT:->" ) ) ] <EOL> output . append ( "<STR_LIT:\n>" + affected ) <EOL> return output <EOL> def html ( rows , headers = ( ) ) : <EOL> output = [ ] <EOL> output . append ( '<STR_LIT>' ) <EOL> output . append ( '<STR_LIT>' ) <EOL> headers = map ( lambda x : '<STR_LIT>' % ( x . upper ( ) ) , list ( headers ) ) <EOL> map ( output . append , headers ) <EOL> output . append ( '<STR_LIT>' ) <EOL> if rows and len ( rows ) > <NUM_LIT:0> : <EOL> for row in rows : <EOL> output . append ( '<STR_LIT>' ) <EOL> row = map ( lambda x : '<STR_LIT>' % ( x ) , row ) <EOL> map ( output . append , row ) <EOL> output . append ( '<STR_LIT>' ) <EOL> output . append ( '<STR_LIT>' ) <EOL> return output <EOL> comments = lambda x : re . compile ( "<STR_LIT>" , re . S ) . sub ( "<STR_LIT>" , x , <NUM_LIT:0> ) <EOL> class mxODBCProxy : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , c ) : <EOL> self . c = c <EOL> def __getattr__ ( self , name ) : <EOL> if name == "<STR_LIT>" : <EOL> return self . execute <EOL> elif name == "<STR_LIT>" : <EOL> return self . gettypeinfo <EOL> else : <EOL> return getattr ( self . c , name ) <EOL> def execute ( self , sql , params = None , bindings = None , maxrows = None ) : <EOL> if params : <EOL> self . c . execute ( sql , params ) <EOL> else : <EOL> self . c . execute ( sql ) <EOL> def gettypeinfo ( self , typeid = None ) : <EOL> if typeid : <EOL> self . c . gettypeinfo ( typeid ) <EOL> class executor : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , table , cols ) : <EOL> self . cols = cols <EOL> self . table = table <EOL> if self . cols : <EOL> self . sql = "<STR_LIT>" % ( table , "<STR_LIT:U+002C>" . join ( self . cols ) , "<STR_LIT:U+002C>" . join ( ( "<STR_LIT:?>" , ) * len ( self . cols ) ) ) <EOL> else : <EOL> self . sql = "<STR_LIT>" % ( table ) <EOL> def execute ( self , db , rows , bindings ) : <EOL> assert rows and len ( rows ) > <NUM_LIT:0> , "<STR_LIT>" <EOL> if self . cols : <EOL> sql = self . sql <EOL> else : <EOL> sql = self . sql % ( "<STR_LIT:U+002C>" . join ( ( "<STR_LIT:?>" , ) * len ( rows [ <NUM_LIT:0> ] ) ) ) <EOL> db . raw ( sql , rows , bindings ) <EOL> def connect ( dbname ) : <EOL> return dbexts ( dbname ) <EOL> def lookup ( dbname ) : <EOL> return dbexts ( jndiname = dbname ) <EOL> class dbexts : <EOL> def __init__ ( self , dbname = None , cfg = None , formatter = console , autocommit = <NUM_LIT:0> , jndiname = None , out = None ) : <EOL> self . verbose = <NUM_LIT:1> <EOL> self . results = [ ] <EOL> self . headers = [ ] <EOL> self . autocommit = autocommit <EOL> self . formatter = formatter <EOL> self . out = out <EOL> self . lastrowid = None <EOL> self . updatecount = None <EOL> if not jndiname : <EOL> if cfg == None : <EOL> fn = os . path . join ( os . path . split ( __file__ ) [ <NUM_LIT:0> ] , "<STR_LIT>" ) <EOL> if not os . path . exists ( fn ) : <EOL> fn = os . path . join ( os . environ [ '<STR_LIT>' ] , "<STR_LIT>" ) <EOL> self . dbs = IniParser ( fn ) <EOL> elif isinstance ( cfg , IniParser ) : <EOL> self . dbs = cfg <EOL> else : <EOL> self . dbs = IniParser ( cfg ) <EOL> if dbname == None : dbname = self . dbs [ ( "<STR_LIT:default>" , "<STR_LIT:name>" ) ] <EOL> if __OS__ == '<STR_LIT>' : <EOL> from com . ziclix . python . sql import zxJDBC <EOL> database = zxJDBC <EOL> if not jndiname : <EOL> t = self . dbs [ ( "<STR_LIT>" , dbname ) ] <EOL> self . dburl , dbuser , dbpwd , jdbcdriver = t [ '<STR_LIT:url>' ] , t [ '<STR_LIT:user>' ] , t [ '<STR_LIT>' ] , t [ '<STR_LIT>' ] <EOL> if t . has_key ( '<STR_LIT>' ) : <EOL> self . datahandler = [ ] <EOL> for dh in t [ '<STR_LIT>' ] . split ( '<STR_LIT:U+002C>' ) : <EOL> classname = dh . split ( "<STR_LIT:.>" ) [ - <NUM_LIT:1> ] <EOL> datahandlerclass = __import__ ( dh , globals ( ) , locals ( ) , classname ) <EOL> self . datahandler . append ( datahandlerclass ) <EOL> keys = [ x for x in t . keys ( ) if x not in [ '<STR_LIT:url>' , '<STR_LIT:user>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:name>' ] ] <EOL> props = { } <EOL> for a in keys : <EOL> props [ a ] = t [ a ] <EOL> self . db = apply ( database . connect , ( self . dburl , dbuser , dbpwd , jdbcdriver ) , props ) <EOL> else : <EOL> self . db = database . lookup ( jndiname ) <EOL> self . db . autocommit = self . autocommit <EOL> elif __OS__ == '<STR_LIT>' : <EOL> for modname in [ "<STR_LIT>" , "<STR_LIT>" ] : <EOL> try : <EOL> database = __import__ ( modname , globals ( ) , locals ( ) , "<STR_LIT>" ) <EOL> break <EOL> except : <EOL> continue <EOL> else : <EOL> raise ImportError ( "<STR_LIT>" ) <EOL> t = self . dbs [ ( "<STR_LIT>" , dbname ) ] <EOL> self . dburl , dbuser , dbpwd = t [ '<STR_LIT:url>' ] , t [ '<STR_LIT:user>' ] , t [ '<STR_LIT>' ] <EOL> self . db = database . Connect ( self . dburl , dbuser , dbpwd , clear_auto_commit = <NUM_LIT:1> ) <EOL> self . dbname = dbname <EOL> for a in database . sqltype . keys ( ) : <EOL> setattr ( self , database . sqltype [ a ] , a ) <EOL> for a in dir ( database ) : <EOL> try : <EOL> p = getattr ( database , a ) <EOL> if issubclass ( p , Exception ) : <EOL> setattr ( self , a , p ) <EOL> except : <EOL> continue <EOL> del database <EOL> def __str__ ( self ) : <EOL> return self . dburl <EOL> def __repr__ ( self ) : <EOL> return self . dburl <EOL> def __getattr__ ( self , name ) : <EOL> if "<STR_LIT>" == name : <EOL> return self . dbs . cfg <EOL> raise AttributeError ( "<STR_LIT>" % ( name ) ) <EOL> def close ( self ) : <EOL> """<STR_LIT>""" <EOL> self . db . close ( ) <EOL> def begin ( self , style = None ) : <EOL> """<STR_LIT>""" <EOL> self . headers , self . results = [ ] , [ ] <EOL> if style : <EOL> c = self . db . cursor ( style ) <EOL> else : <EOL> c = self . db . cursor ( ) <EOL> if __OS__ == '<STR_LIT>' : <EOL> if hasattr ( self , '<STR_LIT>' ) : <EOL> for dh in self . datahandler : <EOL> c . datahandler = dh ( c . datahandler ) <EOL> else : <EOL> c = mxODBCProxy ( c ) <EOL> return c <EOL> def commit ( self , cursor = None , close = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> if cursor and cursor . description : <EOL> self . headers = cursor . description <EOL> self . results = cursor . fetchall ( ) <EOL> if hasattr ( cursor , "<STR_LIT>" ) : <EOL> s = cursor . nextset ( ) <EOL> while s : <EOL> self . results += cursor . fetchall ( ) <EOL> s = cursor . nextset ( ) <EOL> if hasattr ( cursor , "<STR_LIT>" ) : <EOL> self . lastrowid = cursor . lastrowid <EOL> if hasattr ( cursor , "<STR_LIT>" ) : <EOL> self . updatecount = cursor . updatecount <EOL> if not self . autocommit or cursor is None : <EOL> if not self . db . autocommit : <EOL> self . db . commit ( ) <EOL> if cursor and close : cursor . close ( ) <EOL> def rollback ( self ) : <EOL> """<STR_LIT>""" <EOL> self . db . rollback ( ) <EOL> def prepare ( self , sql ) : <EOL> """<STR_LIT>""" <EOL> cur = self . begin ( ) <EOL> try : <EOL> return cur . prepare ( sql ) <EOL> finally : <EOL> self . commit ( cur ) <EOL> def display ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . formatter and self . verbose > <NUM_LIT:0> : <EOL> res = self . results <EOL> if res : <EOL> print >> self . out , "<STR_LIT>" <EOL> for a in self . formatter ( res , map ( lambda x : x [ <NUM_LIT:0> ] , self . headers ) ) : <EOL> print >> self . out , a <EOL> print >> self . out , "<STR_LIT>" <EOL> def __execute__ ( self , sql , params = None , bindings = None , maxrows = None ) : <EOL> """<STR_LIT>""" <EOL> cur = self . begin ( ) <EOL> try : <EOL> if bindings : <EOL> cur . execute ( sql , params , bindings , maxrows = maxrows ) <EOL> elif params : <EOL> cur . execute ( sql , params , maxrows = maxrows ) <EOL> else : <EOL> cur . execute ( sql , maxrows = maxrows ) <EOL> finally : <EOL> self . commit ( cur , close = isinstance ( sql , StringType ) ) <EOL> def isql ( self , sql , params = None , bindings = None , maxrows = None ) : <EOL> """<STR_LIT>""" <EOL> self . raw ( sql , params , bindings , maxrows = maxrows ) <EOL> self . display ( ) <EOL> def raw ( self , sql , params = None , bindings = None , delim = None , comments = comments , maxrows = None ) : <EOL> """<STR_LIT>""" <EOL> if delim : <EOL> headers = [ ] <EOL> results = [ ] <EOL> if type ( sql ) == type ( StringType ) : <EOL> if comments : sql = comments ( sql ) <EOL> statements = filter ( lambda x : len ( x ) > <NUM_LIT:0> , <EOL> map ( lambda statement : statement . strip ( ) , sql . split ( delim ) ) ) <EOL> else : <EOL> statements = [ sql ] <EOL> for a in statements : <EOL> self . __execute__ ( a , params , bindings , maxrows = maxrows ) <EOL> headers . append ( self . headers ) <EOL> results . append ( self . results ) <EOL> self . headers = headers <EOL> self . results = results <EOL> else : <EOL> self . __execute__ ( sql , params , bindings , maxrows = maxrows ) <EOL> return ( self . headers , self . results ) <EOL> def callproc ( self , procname , params = None , bindings = None , maxrows = None ) : <EOL> """<STR_LIT>""" <EOL> cur = self . begin ( ) <EOL> try : <EOL> cur . callproc ( procname , params = params , bindings = bindings , maxrows = maxrows ) <EOL> finally : <EOL> self . commit ( cur ) <EOL> self . display ( ) <EOL> def pk ( self , table , owner = None , schema = None ) : <EOL> """<STR_LIT>""" <EOL> cur = self . begin ( ) <EOL> cur . primarykeys ( schema , owner , table ) <EOL> self . commit ( cur ) <EOL> self . display ( ) <EOL> def fk ( self , primary_table = None , foreign_table = None , owner = None , schema = None ) : <EOL> """<STR_LIT>""" <EOL> cur = self . begin ( ) <EOL> if primary_table and foreign_table : <EOL> cur . foreignkeys ( schema , owner , primary_table , schema , owner , foreign_table ) <EOL> elif primary_table : <EOL> cur . foreignkeys ( schema , owner , primary_table , schema , owner , None ) <EOL> elif foreign_table : <EOL> cur . foreignkeys ( schema , owner , None , schema , owner , foreign_table ) <EOL> self . commit ( cur ) <EOL> self . display ( ) <EOL> def table ( self , table = None , types = ( "<STR_LIT>" , ) , owner = None , schema = None ) : <EOL> """<STR_LIT>""" <EOL> cur = self . begin ( ) <EOL> if table : <EOL> cur . columns ( schema , owner , table , None ) <EOL> else : <EOL> cur . tables ( schema , owner , None , types ) <EOL> self . commit ( cur ) <EOL> self . display ( ) <EOL> def proc ( self , proc = None , owner = None , schema = None ) : <EOL> """<STR_LIT>""" <EOL> cur = self . begin ( ) <EOL> if proc : <EOL> cur . procedurecolumns ( schema , owner , proc , None ) <EOL> else : <EOL> cur . procedures ( schema , owner , None ) <EOL> self . commit ( cur ) <EOL> self . display ( ) <EOL> def stat ( self , table , qualifier = None , owner = None , unique = <NUM_LIT:0> , accuracy = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> cur = self . begin ( ) <EOL> cur . statistics ( qualifier , owner , table , unique , accuracy ) <EOL> self . commit ( cur ) <EOL> self . display ( ) <EOL> def typeinfo ( self , sqltype = None ) : <EOL> """<STR_LIT>""" <EOL> cur = self . begin ( ) <EOL> cur . gettypeinfo ( sqltype ) <EOL> self . commit ( cur ) <EOL> self . display ( ) <EOL> def tabletypeinfo ( self ) : <EOL> """<STR_LIT>""" <EOL> cur = self . begin ( ) <EOL> cur . gettabletypeinfo ( ) <EOL> self . commit ( cur ) <EOL> self . display ( ) <EOL> def schema ( self , table , full = <NUM_LIT:0> , sort = <NUM_LIT:1> , owner = None ) : <EOL> """<STR_LIT>""" <EOL> print >> self . out , str ( Schema ( self , table , owner , full , sort ) ) <EOL> def bulkcopy ( self , dst , table , include = [ ] , exclude = [ ] , autobatch = <NUM_LIT:0> , executor = executor ) : <EOL> """<STR_LIT>""" <EOL> if type ( dst ) == type ( "<STR_LIT>" ) : <EOL> dst = dbexts ( dst , cfg = self . dbs ) <EOL> bcp = Bulkcopy ( dst , table , include = include , exclude = exclude , autobatch = autobatch , executor = executor ) <EOL> return bcp <EOL> def bcp ( self , src , table , where = '<STR_LIT>' , params = [ ] , include = [ ] , exclude = [ ] , autobatch = <NUM_LIT:0> , executor = executor ) : <EOL> """<STR_LIT>""" <EOL> if type ( src ) == type ( "<STR_LIT>" ) : <EOL> src = dbexts ( src , cfg = self . dbs ) <EOL> bcp = self . bulkcopy ( self , table , include , exclude , autobatch , executor ) <EOL> num = bcp . transfer ( src , where , params ) <EOL> return num <EOL> def unload ( self , filename , sql , delimiter = "<STR_LIT:U+002C>" , includeheaders = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> u = Unload ( self , filename , delimiter , includeheaders ) <EOL> u . unload ( sql ) <EOL> class Bulkcopy : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , dst , table , include = [ ] , exclude = [ ] , autobatch = <NUM_LIT:0> , executor = executor ) : <EOL> self . dst = dst <EOL> self . table = table <EOL> self . total = <NUM_LIT:0> <EOL> self . rows = [ ] <EOL> self . autobatch = autobatch <EOL> self . bindings = { } <EOL> include = map ( lambda x : x . lower ( ) , include ) <EOL> exclude = map ( lambda x : x . lower ( ) , exclude ) <EOL> _verbose = self . dst . verbose <EOL> self . dst . verbose = <NUM_LIT:0> <EOL> try : <EOL> self . dst . table ( self . table ) <EOL> if self . dst . results : <EOL> colmap = { } <EOL> for a in self . dst . results : <EOL> colmap [ a [ <NUM_LIT:3> ] . lower ( ) ] = a [ <NUM_LIT:4> ] <EOL> cols = self . __filter__ ( colmap . keys ( ) , include , exclude ) <EOL> for a in zip ( range ( len ( cols ) ) , cols ) : <EOL> self . bindings [ a [ <NUM_LIT:0> ] ] = colmap [ a [ <NUM_LIT:1> ] ] <EOL> colmap = None <EOL> else : <EOL> cols = self . __filter__ ( include , include , exclude ) <EOL> finally : <EOL> self . dst . verbose = _verbose <EOL> self . executor = executor ( table , cols ) <EOL> def __str__ ( self ) : <EOL> return "<STR_LIT>" % ( self . dst , self . table ) <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" % ( self . dst , self . table ) <EOL> def __getattr__ ( self , name ) : <EOL> if name == '<STR_LIT>' : <EOL> return self . executor . cols <EOL> def __filter__ ( self , values , include , exclude ) : <EOL> cols = map ( lambda col : col . lower ( ) , values ) <EOL> if exclude : <EOL> cols = filter ( lambda x , ex = exclude : x not in ex , cols ) <EOL> if include : <EOL> cols = filter ( lambda x , inc = include : x in inc , cols ) <EOL> return cols <EOL> def format ( self , column , type ) : <EOL> self . bindings [ column ] = type <EOL> def done ( self ) : <EOL> if len ( self . rows ) > <NUM_LIT:0> : <EOL> return self . batch ( ) <EOL> return <NUM_LIT:0> <EOL> def batch ( self ) : <EOL> self . executor . execute ( self . dst , self . rows , self . bindings ) <EOL> cnt = len ( self . rows ) <EOL> self . total += cnt <EOL> self . rows = [ ] <EOL> return cnt <EOL> def rowxfer ( self , line ) : <EOL> self . rows . append ( line ) <EOL> if self . autobatch : self . batch ( ) <EOL> def transfer ( self , src , where = "<STR_LIT>" , params = [ ] ) : <EOL> sql = "<STR_LIT>" % ( "<STR_LIT:U+002CU+0020>" . join ( self . columns ) , self . table , where ) <EOL> h , d = src . raw ( sql , params ) <EOL> if d : <EOL> map ( self . rowxfer , d ) <EOL> return self . done ( ) <EOL> return <NUM_LIT:0> <EOL> class Unload : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , db , filename , delimiter = "<STR_LIT:U+002C>" , includeheaders = <NUM_LIT:1> ) : <EOL> self . db = db <EOL> self . filename = filename <EOL> self . delimiter = delimiter <EOL> self . includeheaders = includeheaders <EOL> self . formatters = { } <EOL> def format ( self , o ) : <EOL> if not o : <EOL> return "<STR_LIT>" <EOL> o = str ( o ) <EOL> if o . find ( "<STR_LIT:U+002C>" ) != - <NUM_LIT:1> : <EOL> o = "<STR_LIT>" % ( o ) <EOL> return o <EOL> def unload ( self , sql , mode = "<STR_LIT:w>" ) : <EOL> headers , results = self . db . raw ( sql ) <EOL> w = open ( self . filename , mode ) <EOL> if self . includeheaders : <EOL> w . write ( "<STR_LIT>" % ( self . delimiter . join ( map ( lambda x : x [ <NUM_LIT:0> ] , headers ) ) ) ) <EOL> if results : <EOL> for a in results : <EOL> w . write ( "<STR_LIT>" % ( self . delimiter . join ( map ( self . format , a ) ) ) ) <EOL> w . flush ( ) <EOL> w . close ( ) <EOL> class Schema : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , db , table , owner = None , full = <NUM_LIT:0> , sort = <NUM_LIT:1> ) : <EOL> self . db = db <EOL> self . table = table <EOL> self . owner = owner <EOL> self . full = full <EOL> self . sort = sort <EOL> _verbose = self . db . verbose <EOL> self . db . verbose = <NUM_LIT:0> <EOL> try : <EOL> if table : self . computeschema ( ) <EOL> finally : <EOL> self . db . verbose = _verbose <EOL> def computeschema ( self ) : <EOL> self . db . table ( self . table , owner = self . owner ) <EOL> self . columns = [ ] <EOL> if self . db . results : <EOL> self . columns = map ( lambda x : ( x [ <NUM_LIT:3> ] , x [ <NUM_LIT:5> ] , x [ <NUM_LIT:6> ] , x [ <NUM_LIT:10> ] ) , self . db . results ) <EOL> if self . sort : self . columns . sort ( lambda x , y : cmp ( x [ <NUM_LIT:0> ] , y [ <NUM_LIT:0> ] ) ) <EOL> self . db . fk ( None , self . table ) <EOL> self . imported = [ ] <EOL> if self . db . results : <EOL> self . imported = map ( lambda x : ( x [ <NUM_LIT:2> ] , x [ <NUM_LIT:3> ] , x [ <NUM_LIT:7> ] , x [ <NUM_LIT:11> ] , x [ <NUM_LIT:12> ] ) , self . db . results ) <EOL> if self . sort : self . imported . sort ( lambda x , y : cmp ( x [ <NUM_LIT:2> ] , y [ <NUM_LIT:2> ] ) ) <EOL> self . exported = [ ] <EOL> if self . full : <EOL> self . db . fk ( self . table , None ) <EOL> if self . db . results : <EOL> self . exported = map ( lambda x : ( x [ <NUM_LIT:3> ] , x [ <NUM_LIT:6> ] , x [ <NUM_LIT:7> ] , x [ <NUM_LIT:11> ] , x [ <NUM_LIT:12> ] ) , self . db . results ) <EOL> if self . sort : self . exported . sort ( lambda x , y : cmp ( x [ <NUM_LIT:1> ] , y [ <NUM_LIT:1> ] ) ) <EOL> self . db . pk ( self . table ) <EOL> self . primarykeys = [ ] <EOL> if self . db . results : <EOL> self . primarykeys = map ( lambda x : ( x [ <NUM_LIT:3> ] , x [ <NUM_LIT:4> ] , x [ <NUM_LIT:5> ] ) , self . db . results ) <EOL> if self . sort : self . primarykeys . sort ( lambda x , y : cmp ( x [ <NUM_LIT:1> ] , y [ <NUM_LIT:1> ] ) ) <EOL> try : <EOL> self . indices = None <EOL> self . db . stat ( self . table ) <EOL> self . indices = [ ] <EOL> if self . db . results : <EOL> idxdict = { } <EOL> idx = map ( lambda x : ( x [ <NUM_LIT:3> ] , x [ <NUM_LIT:5> ] . strip ( ) , x [ <NUM_LIT:6> ] , x [ <NUM_LIT:7> ] , x [ <NUM_LIT:8> ] ) , filter ( lambda x : x [ <NUM_LIT:5> ] , self . db . results ) ) <EOL> def cckmp ( x , y ) : <EOL> c = cmp ( x [ <NUM_LIT:1> ] , y [ <NUM_LIT:1> ] ) <EOL> if c == <NUM_LIT:0> : c = cmp ( x [ <NUM_LIT:3> ] , y [ <NUM_LIT:3> ] ) <EOL> return c <EOL> idx . sort ( cckmp ) <EOL> for a in idx : <EOL> if not idxdict . has_key ( a [ <NUM_LIT:1> ] ) : <EOL> idxdict [ a [ <NUM_LIT:1> ] ] = [ ] <EOL> idxdict [ a [ <NUM_LIT:1> ] ] . append ( a ) <EOL> self . indices = idxdict . values ( ) <EOL> if self . sort : self . indices . sort ( lambda x , y : cmp ( x [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] , y [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] ) ) <EOL> except : <EOL> pass <EOL> def __str__ ( self ) : <EOL> d = [ ] <EOL> d . append ( "<STR_LIT>" ) <EOL> d . append ( "<STR_LIT:U+0020>" + self . table ) <EOL> d . append ( "<STR_LIT>" ) <EOL> for a in self . primarykeys : <EOL> d . append ( "<STR_LIT>" % ( a [ <NUM_LIT:0> ] , a [ <NUM_LIT:2> ] ) ) <EOL> d . append ( "<STR_LIT>" ) <EOL> for a in self . imported : <EOL> d . append ( "<STR_LIT>" % ( a [ <NUM_LIT:2> ] , a [ <NUM_LIT:0> ] , a [ <NUM_LIT:1> ] , a [ <NUM_LIT:3> ] ) ) <EOL> if self . full : <EOL> d . append ( "<STR_LIT>" ) <EOL> for a in self . exported : <EOL> d . append ( "<STR_LIT>" % ( a [ <NUM_LIT:0> ] , a [ <NUM_LIT:1> ] , a [ <NUM_LIT:2> ] , a [ <NUM_LIT:3> ] ) ) <EOL> d . append ( "<STR_LIT>" ) <EOL> for a in self . columns : <EOL> nullable = choose ( a [ <NUM_LIT:3> ] , "<STR_LIT>" , "<STR_LIT>" ) <EOL> d . append ( "<STR_LIT>" % ( a [ <NUM_LIT:0> ] , a [ <NUM_LIT:1> ] , a [ <NUM_LIT:2> ] , nullable ) ) <EOL> d . append ( "<STR_LIT>" ) <EOL> if self . indices is None : <EOL> d . append ( "<STR_LIT>" ) <EOL> else : <EOL> for a in self . indices : <EOL> unique = choose ( a [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] , "<STR_LIT>" , "<STR_LIT>" ) <EOL> cname = "<STR_LIT:U+002CU+0020>" . join ( map ( lambda x : x [ <NUM_LIT:4> ] , a ) ) <EOL> d . append ( "<STR_LIT>" % ( unique , a [ <NUM_LIT:0> ] [ <NUM_LIT:1> ] , cname ) ) <EOL> return "<STR_LIT:\n>" . join ( d ) <EOL> class IniParser : <EOL> def __init__ ( self , cfg , key = '<STR_LIT:name>' ) : <EOL> self . key = key <EOL> self . records = { } <EOL> self . ctypeRE = re . compile ( "<STR_LIT>" ) <EOL> self . entryRE = re . compile ( "<STR_LIT>" ) <EOL> self . cfg = cfg <EOL> self . parse ( ) <EOL> def parse ( self ) : <EOL> fp = open ( self . cfg , "<STR_LIT:r>" ) <EOL> data = fp . readlines ( ) <EOL> fp . close ( ) <EOL> lines = filter ( lambda x : len ( x ) > <NUM_LIT:0> and x [ <NUM_LIT:0> ] not in [ '<STR_LIT:#>' , '<STR_LIT:;>' ] , map ( lambda x : x . strip ( ) , data ) ) <EOL> current = None <EOL> for i in range ( len ( lines ) ) : <EOL> line = lines [ i ] <EOL> g = self . ctypeRE . match ( line ) <EOL> if g : <EOL> current = { } <EOL> if not self . records . has_key ( g . group ( <NUM_LIT:1> ) ) : <EOL> self . records [ g . group ( <NUM_LIT:1> ) ] = [ ] <EOL> self . records [ g . group ( <NUM_LIT:1> ) ] . append ( current ) <EOL> else : <EOL> g = self . entryRE . match ( line ) <EOL> if g : <EOL> current [ g . group ( <NUM_LIT:1> ) ] = g . group ( <NUM_LIT:2> ) <EOL> def __getitem__ ( self , ( ctype , skey ) ) : <EOL> if skey == self . key : return self . records [ ctype ] [ <NUM_LIT:0> ] [ skey ] <EOL> t = filter ( lambda x , p = self . key , s = skey : x [ p ] == s , self . records [ ctype ] ) <EOL> if not t or len ( t ) > <NUM_LIT:1> : <EOL> raise KeyError , "<STR_LIT>" % ( ctype , skey ) <EOL> return t [ <NUM_LIT:0> ] <EOL> def random_table_name ( prefix , num_chars ) : <EOL> import random <EOL> d = [ prefix , '<STR_LIT:_>' ] <EOL> i = <NUM_LIT:0> <EOL> while i < num_chars : <EOL> d . append ( chr ( int ( <NUM_LIT:100> * random . random ( ) ) % <NUM_LIT> + ord ( '<STR_LIT:A>' ) ) ) <EOL> i += <NUM_LIT:1> <EOL> return "<STR_LIT>" . join ( d ) <EOL> class ResultSetRow : <EOL> def __init__ ( self , rs , row ) : <EOL> self . row = row <EOL> self . rs = rs <EOL> def __getitem__ ( self , i ) : <EOL> if type ( i ) == type ( "<STR_LIT>" ) : <EOL> i = self . rs . index ( i ) <EOL> return self . row [ i ] <EOL> def __getslice__ ( self , i , j ) : <EOL> if type ( i ) == type ( "<STR_LIT>" ) : i = self . rs . index ( i ) <EOL> if type ( j ) == type ( "<STR_LIT>" ) : j = self . rs . index ( j ) <EOL> return self . row [ i : j ] <EOL> def __len__ ( self ) : <EOL> return len ( self . row ) <EOL> def __repr__ ( self ) : <EOL> return str ( self . row ) <EOL> class ResultSet : <EOL> def __init__ ( self , headers , results = [ ] ) : <EOL> self . headers = map ( lambda x : x . upper ( ) , headers ) <EOL> self . results = results <EOL> def index ( self , i ) : <EOL> return self . headers . index ( i . upper ( ) ) <EOL> def __getitem__ ( self , i ) : <EOL> return ResultSetRow ( self , self . results [ i ] ) <EOL> def __getslice__ ( self , i , j ) : <EOL> return map ( lambda x , rs = self : ResultSetRow ( rs , x ) , self . results [ i : j ] ) <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" % ( self . __class__ , len ( self . headers ) , len ( self . results ) , id ( self ) ) </s>
