<s> import sys <EOL> import os . path <EOL> import sqlite3 as sqlite <EOL> TYPE_TABLES = [ "<STR_LIT>" , "<STR_LIT>" , "<STR_LIT>" ] <EOL> NON_EMPTY_TABLES = set ( [ "<STR_LIT>" ] ) <EOL> def argparser ( ) : <EOL> import argparse <EOL> ap = argparse . ArgumentParser ( description = "<STR_LIT>" ) <EOL> ap . add_argument ( "<STR_LIT>" , "<STR_LIT>" , default = False , action = "<STR_LIT:store_true>" , help = "<STR_LIT>" ) <EOL> ap . add_argument ( "<STR_LIT>" , "<STR_LIT>" , default = False , action = "<STR_LIT:store_true>" , help = "<STR_LIT>" ) <EOL> ap . add_argument ( "<STR_LIT>" , metavar = "<STR_LIT>" , help = "<STR_LIT>" ) <EOL> return ap <EOL> def string_norm_form ( s ) : <EOL> return s . lower ( ) . strip ( ) . replace ( '<STR_LIT:->' , '<STR_LIT:U+0020>' ) <EOL> def datas_by_ids ( cursor , ids ) : <EOL> responses = { } <EOL> for table in TYPE_TABLES : <EOL> command = '''<STR_LIT>''' % ( table , '<STR_LIT:U+002C>' . join ( [ '<STR_LIT:?>' for i in ids ] ) ) <EOL> cursor . execute ( command , list ( ids ) ) <EOL> response = cursor . fetchall ( ) <EOL> for id_ , label , value in response : <EOL> if id_ not in responses : <EOL> responses [ id_ ] = { } <EOL> if table not in responses [ id_ ] : <EOL> responses [ id_ ] [ table ] = [ ] <EOL> responses [ id_ ] [ table ] . append ( [ label , value ] ) <EOL> if ( table in NON_EMPTY_TABLES and <EOL> len ( [ i for i in responses if responses [ i ] [ table ] == <NUM_LIT:0> ] ) != <NUM_LIT:0> ) : <EOL> return None <EOL> for id_ in responses : <EOL> for t in NON_EMPTY_TABLES : <EOL> if len ( responses [ id_ ] [ t ] ) == <NUM_LIT:0> : <EOL> return None <EOL> datas = { } <EOL> for id_ in responses : <EOL> datas [ id_ ] = [ ] <EOL> for t in TYPE_TABLES : <EOL> datas [ id_ ] . append ( responses [ id_ ] . get ( t , [ ] ) ) <EOL> return datas <EOL> def ids_by_name ( cursor , name , exactmatch = False , return_match = False ) : <EOL> return ids_by_names ( cursor , [ name ] , exactmatch , return_match ) <EOL> def ids_by_names ( cursor , names , exactmatch = False , return_match = False ) : <EOL> if not return_match : <EOL> command = '<STR_LIT>' <EOL> else : <EOL> command = '<STR_LIT>' <EOL> command += '''<STR_LIT>''' <EOL> if exactmatch : <EOL> command += '<STR_LIT>' % '<STR_LIT:U+002C>' . join ( [ '<STR_LIT:?>' for n in names ] ) <EOL> else : <EOL> command += '<STR_LIT>' % '<STR_LIT:U+002C>' . join ( [ '<STR_LIT:?>' for n in names ] ) <EOL> names = [ string_norm_form ( n ) for n in names ] <EOL> cursor . execute ( command , names ) <EOL> responses = cursor . fetchall ( ) <EOL> if not return_match : <EOL> return [ r [ <NUM_LIT:0> ] for r in responses ] <EOL> else : <EOL> return [ ( r [ <NUM_LIT:0> ] , r [ <NUM_LIT:1> ] ) for r in responses ] <EOL> def main ( argv ) : <EOL> arg = argparser ( ) . parse_args ( argv [ <NUM_LIT:1> : ] ) <EOL> dbn = arg . database <EOL> dbpaths = [ dbn , os . path . join ( '<STR_LIT>' , dbn ) , os . path . join ( '<STR_LIT>' , dbn ) + '<STR_LIT>' ] <EOL> dbfn = None <EOL> for p in dbpaths : <EOL> if os . path . exists ( p ) : <EOL> dbfn = p <EOL> break <EOL> if dbfn is None : <EOL> print >> sys . stderr , "<STR_LIT>" % dbfn <EOL> return <NUM_LIT:1> <EOL> try : <EOL> connection = sqlite . connect ( dbfn ) <EOL> except sqlite . OperationalError , e : <EOL> print >> sys . stderr , "<STR_LIT>" % dbfn , e <EOL> return <NUM_LIT:1> <EOL> cursor = connection . cursor ( ) <EOL> while True : <EOL> if not arg . no_prompt : <EOL> print "<STR_LIT>" , <EOL> l = sys . stdin . readline ( ) <EOL> if not l : <EOL> break <EOL> l = l . rstrip ( ) <EOL> try : <EOL> r = ids_by_name ( cursor , l ) <EOL> if len ( r ) != <NUM_LIT:0> : <EOL> d = datas_by_ids ( cursor , r ) <EOL> for i in d : <EOL> print i + '<STR_LIT:\t>' , '<STR_LIT:\t>' . join ( [ '<STR_LIT:U+0020>' . join ( [ "<STR_LIT>" % ( k , v ) for k , v in a ] ) for a in d [ i ] ] ) <EOL> elif l == '<STR_LIT>' : <EOL> print "<STR_LIT>" <EOL> else : <EOL> print "<STR_LIT>" % l <EOL> except Exception , e : <EOL> print >> sys . stderr , "<STR_LIT>" , e <EOL> return <NUM_LIT:1> <EOL> return <NUM_LIT:0> <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> sys . exit ( main ( sys . argv ) ) </s>
