from codecs import open from getopt import getopt import sys from pprint import pprint, pformat from lap.store import Lafdb from lap.utils import infomsg options, args = getopt(sys.argv[1:], '', ['purge', 'all', 'dump', 'output=']) options = {key: value for key, value in options} if len(args) < 1: output = "Usage:\n" output += "\t%s --purge RCPT" % sys.argv[0] + "\n" output += "\t%s --dump RCPT" % sys.argv[0] sys.exit(output) infile = open(args[0], 'r', 'utf-8') db = Lafdb(data=infile.read()) if '--purge' in options: if '--all' in options: for media_type, receipt in db.info['media'].items(): db.remove(receipt, media=True) for receipt in db.info['annotators'].values(): db.remove(receipt, media=False) else: if db.info['annotators']: infomsg("Removing: %s" % db.info['receipt_origin']) db.remove(db.info['annotators'][db.info['receipt_origin']], media=False) else: for media_type, receipt in db.info['media'].items(): db.remove(receipt, media=True) quit() # def print_to_sysout(collection, documents, annotator=None): # output = '' # if annotator is None: # output += '=== Media: ' + str(collection) + ' ===\n' # else: # output += '\n=== Annotator: ' + annotator + ' (' + collection + ') ===\n\n' # if len(documents) > 1: # output += pformat(documents) + '\n' # else: # output += pformat(documents[0]) + '\n' # output += '-------------------------------\n' # return output # # # def print_to_file(file_name, collection, documents, annotator=None): # # if annotator is None: # file_name = file_name + '.media.' + collection # else: # file_name = file_name + '.' + annotator + '.' + collection # # output = open(file_name, 'w') # if len(documents) > 1: # output.write(pformat(documents)) # else: # output.write(pformat(documents[0])) # output.close() # return "Saved "+file_name+"\n" # # # def handle_collection(file_name, collection, cursor, annotator=None): # output = '' # documents = [] # for document in cursor: # documents.append(document) # if file_name is None: # output += print_to_sysout(collection, documents, annotator) # else: # output += print_to_file(file_name, collection, documents, annotator) # return output # # # if '--dump' in options: # # output = '\n' # # file_name = None # # if '--output' in options: # file_name = options['--output'] # # for media_type, collection in db.info['media'].items(): # cursor = db.db[db.info["media"][media_type]][media_type].find() # output += handle_collection(file_name, collection, cursor) # # for annotator in db.info['annotators']: # collection = db.info['annotators'][annotator] # cursor = db.db[collection]['graph'].find() # output += handle_collection(file_name, collection, cursor, annotator) # print output # quit() output = "Usage:\n" output += "\t%s --purge RCPT" % sys.argv[0] + "\n" output += "\t%s --dump RCPT" % sys.argv[0] sys.exit(output)