<s> import os <EOL> import sys <EOL> import logging <EOL> from rxnorm_link import runImport <EOL> class DocHandler ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> self . documents = [ ] <EOL> def addDocument ( self , doc ) : <EOL> if doc is not None : <EOL> self . documents . append ( doc ) <EOL> def finalize ( self ) : <EOL> pass <EOL> class DebugDocHandler ( DocHandler ) : <EOL> """<STR_LIT>""" <EOL> def addDocument ( self , doc ) : <EOL> print ( doc ) <EOL> def __str__ ( self ) : <EOL> return "<STR_LIT>" <EOL> class SQLiteDocHandler ( DocHandler ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> super ( ) . __init__ ( ) <EOL> from sqlite import SQLite <EOL> absolute = os . path . dirname ( os . path . realpath ( __file__ ) ) <EOL> db_file = os . environ . get ( '<STR_LIT>' ) <EOL> db_file = db_file if db_file else os . path . join ( absolute , '<STR_LIT>' ) <EOL> self . db_file = db_file <EOL> self . handled = <NUM_LIT:0> <EOL> self . sqlite = SQLite . get ( self . db_file ) <EOL> self . sqlite . execute ( '<STR_LIT>' ) <EOL> self . sqlite . execute ( '''<STR_LIT>''' ) <EOL> self . sqlite . execute ( '<STR_LIT>' ) <EOL> self . sqlite . execute ( '<STR_LIT>' ) <EOL> self . sqlite . execute ( '''<STR_LIT>''' ) <EOL> self . sqlite . execute ( '<STR_LIT>' ) <EOL> self . sqlite . execute ( '''<STR_LIT>''' ) <EOL> self . sqlite . execute ( '<STR_LIT>' ) <EOL> self . sqlite . execute ( '''<STR_LIT>''' ) <EOL> def addDocument ( self , doc ) : <EOL> rxcui = doc . get ( '<STR_LIT>' , '<STR_LIT:0>' ) <EOL> fields = { <EOL> '<STR_LIT>' : doc . get ( '<STR_LIT>' , None ) , <EOL> '<STR_LIT>' : doc . get ( '<STR_LIT>' , None ) , <EOL> '<STR_LIT:label>' : doc . get ( '<STR_LIT:label>' , None ) , <EOL> '<STR_LIT>' : doc . get ( '<STR_LIT>' , None ) , <EOL> '<STR_LIT>' : doc . get ( '<STR_LIT>' , None ) , <EOL> '<STR_LIT>' : doc . get ( '<STR_LIT>' , None ) <EOL> } <EOL> for k , v in fields . items ( ) : <EOL> if not v : continue <EOL> v = v if isinstance ( v , list ) else [ v ] <EOL> for vv in v : <EOL> self . sqlite . execute ( <EOL> '<STR_LIT>' , <EOL> ( rxcui , k , vv ) ) <EOL> self . handled += <NUM_LIT:1> <EOL> if ( self . handled % <NUM_LIT:50> == <NUM_LIT:0> ) : self . sqlite . commit ( ) <EOL> def finalize ( self ) : <EOL> self . sqlite . commit ( ) <EOL> def __str__ ( self ) : <EOL> return "<STR_LIT>" . format ( self . db_file ) <EOL> class MongoDocHandler ( DocHandler ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> super ( ) . __init__ ( ) <EOL> db_host = os . environ . get ( '<STR_LIT>' ) <EOL> db_host = db_host if db_host else '<STR_LIT:localhost>' <EOL> db_port = int ( os . environ . get ( '<STR_LIT>' ) ) <EOL> db_port = db_port if db_port else <NUM_LIT> <EOL> db_name = os . environ . get ( '<STR_LIT>' ) <EOL> db_name = db_name if db_name else '<STR_LIT:default>' <EOL> db_bucket = os . environ . get ( '<STR_LIT>' ) <EOL> db_bucket = db_bucket if db_bucket else '<STR_LIT>' <EOL> import pymongo <EOL> conn = pymongo . MongoClient ( host = db_host , port = db_port ) <EOL> db = conn [ db_name ] <EOL> db_user = os . environ . get ( '<STR_LIT>' ) <EOL> db_pass = os . environ . get ( '<STR_LIT>' ) <EOL> if db_user and db_pass : <EOL> db . authenticate ( db_user , db_pass ) <EOL> self . mng = db [ db_bucket ] <EOL> self . mng . ensure_index ( '<STR_LIT>' ) <EOL> self . mng . ensure_index ( '<STR_LIT:label>' , text = pymongo . TEXT ) <EOL> def addDocument ( self , doc ) : <EOL> lbl = doc . get ( '<STR_LIT:label>' ) <EOL> if lbl and len ( lbl ) > <NUM_LIT> : <EOL> doc [ '<STR_LIT>' ] = lbl <EOL> doc [ '<STR_LIT:label>' ] = lbl [ : <NUM_LIT> ] <EOL> super ( ) . addDocument ( doc ) <EOL> if len ( self . documents ) > <NUM_LIT:50> : <EOL> self . _insertAndClear ( ) <EOL> def finalize ( self ) : <EOL> self . _insertAndClear ( ) <EOL> def _insertAndClear ( self ) : <EOL> if len ( self . documents ) > <NUM_LIT:0> : <EOL> self . mng . insert ( self . documents ) <EOL> self . documents . clear ( ) <EOL> def __str__ ( self ) : <EOL> return "<STR_LIT>" . format ( self . mng ) <EOL> class CSVHandler ( DocHandler ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> super ( ) . __init__ ( ) <EOL> self . csv_file = '<STR_LIT>' <EOL> self . csv_handle = open ( self . csv_file , '<STR_LIT:w>' ) <EOL> self . csv_handle . write ( "<STR_LIT>" ) <EOL> def addDocument ( self , doc ) : <EOL> self . csv_handle . write ( '<STR_LIT>' . format ( <EOL> doc . get ( '<STR_LIT>' , '<STR_LIT:0>' ) , <EOL> doc . get ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> doc . get ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> doc . get ( '<STR_LIT:label>' , '<STR_LIT>' ) , <EOL> '<STR_LIT:;>' . join ( doc . get ( '<STR_LIT>' ) or [ ] ) , <EOL> '<STR_LIT:;>' . join ( doc . get ( '<STR_LIT>' ) or [ ] ) , <EOL> '<STR_LIT:;>' . join ( doc . get ( '<STR_LIT>' ) or [ ] ) , <EOL> "<STR_LIT:\n>" <EOL> ) ) <EOL> def __str__ ( self ) : <EOL> return '<STR_LIT>' . format ( self . csv_file ) <EOL> def runLinking ( ex_type ) : <EOL> """<STR_LIT>""" <EOL> handler = DebugDocHandler ( ) <EOL> if ex_type is not None and len ( ex_type ) > <NUM_LIT:0> : <EOL> try : <EOL> if '<STR_LIT>' == ex_type : <EOL> handler = MongoDocHandler ( ) <EOL> elif '<STR_LIT>' == ex_type : <EOL> raise Exception ( '<STR_LIT>' ) <EOL> elif '<STR_LIT>' == ex_type : <EOL> handler = CSVHandler ( ) <EOL> elif '<STR_LIT>' == ex_type : <EOL> handler = SQLiteDocHandler ( ) <EOL> else : <EOL> raise Exception ( '<STR_LIT>' . format ( ex_type ) ) <EOL> except Exception as e : <EOL> logging . error ( e ) <EOL> sys . exit ( <NUM_LIT:1> ) <EOL> print ( '<STR_LIT>' . format ( handler ) ) <EOL> runImport ( doc_handler = handler ) <EOL> if '<STR_LIT:__main__>' == __name__ : <EOL> logging . basicConfig ( level = logging . INFO ) <EOL> cmd_arg = sys . argv [ <NUM_LIT:1> ] if len ( sys . argv ) > <NUM_LIT:1> else None <EOL> ex_type = os . environ . get ( '<STR_LIT>' ) or cmd_arg <EOL> runLinking ( ex_type ) </s>
