<s> from smart . triplestore import TripleStore <EOL> from smart . models . record_object import api_types , Record , RecordObject <EOL> from smart . common . rdf_tools . util import parse_rdf , serialize_rdf , remap_node , bound_graph , URIRef , Literal , BNode , sp , rdf <EOL> from django . conf import settings <EOL> import sys <EOL> """<STR_LIT>""" <EOL> class RecordImporter ( object ) : <EOL> def __init__ ( self , filename , target_id = None ) : <EOL> self . target_id = target_id <EOL> self . data = parse_rdf ( open ( filename ) . read ( ) ) <EOL> var_bindings = { '<STR_LIT>' : self . target_id } <EOL> self . ro = RecordObject [ sp . Statement ] <EOL> self . ro . prepare_graph ( self . data , None , var_bindings ) <EOL> print "<STR_LIT>" , len ( self . data . default_context ) <EOL> record_node = list ( self . data . triples ( ( None , rdf . type , sp . MedicalRecord ) ) ) <EOL> assert len ( record_node ) == <NUM_LIT:1> , "<STR_LIT>" % record_node <EOL> record_node = record_node [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] <EOL> self . record_node = record_node <EOL> self . ro . segregate_nodes ( self . data , record_node ) <EOL> self . data . remove_context ( self . data . default_context ) <EOL> self . write_to_record ( ) <EOL> print self . data . default_context . identifier . n3 ( ) <EOL> def write_to_record ( self ) : <EOL> rconn = TripleStore ( ) <EOL> rconn . transaction_begin ( ) <EOL> r , created = Record . objects . get_or_create ( id = self . target_id ) <EOL> print "<STR_LIT>" , self . record_node . n3 ( ) <EOL> try : <EOL> rconn . destroy_context_and_neighbors ( self . record_node ) <EOL> except : <EOL> pass <EOL> rconn . replace_conjunctive_graph ( self . data ) <EOL> print "<STR_LIT>" , len ( self . data ) <EOL> rconn . transaction_commit ( ) <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> import string <EOL> for v in sys . argv [ <NUM_LIT:1> : ] : <EOL> rid = filter ( str . isdigit , v . split ( "<STR_LIT:/>" ) [ - <NUM_LIT:1> ] . split ( "<STR_LIT:.>" ) [ <NUM_LIT:0> ] ) <EOL> print "<STR_LIT>" % rid <EOL> RecordImporter ( v , rid ) </s>
