def link_meta_data_record(input, output): #sys.exit(1) try: os.mkdir(os.path.split(output)[0]) except: pass try: os.mkdir(TEMPORARY_FOLDER_FOLDER) except: pass if os.path.exists(output): os.unlink(output) ################################################################ # Parse meta data and update person record ############# db_connection = sqlite3.connect(options.database_file) db_pointer = db_connection.cursor() image_path_name = input[:-len("also_meta")]+"jpg" meta_dict = path_to_meta_data_parser(image_path_name) #print image_path_name, "\n", meta_dict, "\n", input, "\n\n\n" #print meta_dict["patient_id"] while True: #try: meta_id_record = [str(x[0]) for x in db_pointer.execute("select id from meta where patient_id = ?", (meta_dict["patient_id"],))] try: assert len(meta_id_record) == 1 except: print meta_id_record, meta_dict["patient_id"] sys.exit("meta_id_record larger than 1") #print record break #except: print "re-ping sqlite database" with tempfile.NamedTemporaryFile(suffix = ".request", dir = TEMPORARY_FOLDER_FOLDER) as temp_request_file: while True: if not os.path.exists(options.database_file+".queue"): time.sleep(0.5) else: #print "checking...", open(options.database_file+".queue","r").readline().strip("\n"), os.path.split(temp_request_file.name),open(options.database_file+".queue","r").readline().strip("\n") == os.path.split(temp_request_file.name)[1] if open(options.database_file+".queue","r").readline().strip("\n") == os.path.split(temp_request_file.name)[1]: #print "accepted!" break try: db_pointer.execute("UPDATE processing SET meta_id = ? WHERE image_path_name = ?", (meta_id_record[0], image_path_name)) except: print "failed", "UPDATE processing SET meta_id = ? WHERE image_path_name = ?", meta_id_record[0], image_path_name db_pointer.execute("UPDATE processing SET meta_id = ? WHERE image_path_name = ?", (meta_id_record[0], image_path_name)) sys.exit(1) db_connection.commit() db_connection.close() fh_out = open(output, "w") fh_out.close()
def create_meta_data_record(input, output): try: os.mkdir(os.path.split(output)[0]) except: pass try: os.mkdir(TEMPORARY_FOLDER_FOLDER) except: pass if os.path.exists(output): os.unlink(output) ################################################################ # Parse meta data and update person record ############# db_connection = sqlite3.connect(options.database_file) db_pointer = db_connection.cursor() meta_dict = path_to_meta_data_parser(input) with tempfile.NamedTemporaryFile(suffix = ".request", dir = TEMPORARY_FOLDER_FOLDER) as temp_request_file: while True: if not os.path.exists(options.database_file+".queue"): time.sleep(0.5) else: #print "checking...", open(options.database_file+".queue","r").readline().strip("\n"), os.path.split(temp_request_file.name),open(options.database_file+".queue","r").readline().strip("\n") == os.path.split(temp_request_file.name)[1] if open(options.database_file+".queue","r").readline().strip("\n") == os.path.split(temp_request_file.name)[1]: #print "accepted!" break ################ # Must be with db locking else duplicated meta records appear while True: #try: record = [x for x in db_pointer.execute("select * from meta where patient_id = ?", (meta_dict["patient_id"],))] break #except: print "re-ping sqlite database" sqlite_cmd = "nope!" if record: sqlite_cmd = "".join(["UPDATE meta SET"] + [" "+str(x)+' = "'+str(y)+'",' for x,y in meta_dict.iteritems()]) sqlite_cmd = sqlite_cmd[:-1]+'where patient_id = "%s"' % str(meta_dict["patient_id"]) else: sqlite_cmd = "INSERT INTO meta (%s) VALUES (%s)" % (",".join([str(x) for x in meta_dict.keys()]) , ",".join(['"'+str(x)+'"' for x in meta_dict.values()])) try: db_pointer.execute(sqlite_cmd) except: print "failed", sqlite_cmd db_pointer.execute(sqlite_cmd) sys.exit(1) db_connection.commit() db_connection.close() fh_out = open(output, "w") fh_out.close()