|
| 1 | +__author__ = 'swissbib' |
| 2 | + |
| 3 | + |
| 4 | + |
| 5 | +import os |
| 6 | +from argparse import ArgumentParser |
| 7 | +from swissbibHarvestingConfigs import HarvestingReadConfigs |
| 8 | +from Context import ApplicationContext |
| 9 | +from swissbibMongoHarvesting import MongoDBHarvestingWrapper |
| 10 | +import zlib |
| 11 | + |
| 12 | + |
| 13 | +#Kurzdoku |
| 14 | +#Aufruf |
| 15 | +#deleteRecords.py --config=localStuff/config.sbudb6/config.readMongo.idsbb.xml --file=[file with IDS] --networkPrefix=IDSBB |
| 16 | + |
| 17 | + |
| 18 | + |
| 19 | +class MongoDelete(MongoDBHarvestingWrapper): |
| 20 | + |
| 21 | + def __init__(self,applicationContext=None): |
| 22 | + MongoDBHarvestingWrapper.__init__(self,applicationContext=applicationContext) |
| 23 | + self.collection = self.dbConnections['nativeSources']['collections']['sourceDB'] |
| 24 | + self.numbers = 0 |
| 25 | + |
| 26 | + def deleteId(self, id): |
| 27 | + delete_query = { "_id": id} |
| 28 | + delete_result = self.collection.delete_one(delete_query) |
| 29 | + print(delete_result) |
| 30 | + |
| 31 | + |
| 32 | + def readId(self, id): |
| 33 | + document = self.collection.find_one({"_id": id}) |
| 34 | + if document: |
| 35 | + self.numbers += 1 |
| 36 | + print (zlib.decompress(document["record"])) |
| 37 | + |
| 38 | + |
| 39 | + |
| 40 | +if __name__ == '__main__': |
| 41 | + |
| 42 | + oParser = None |
| 43 | + args = None |
| 44 | + sConfigs = None |
| 45 | + deleteMongo = None |
| 46 | + |
| 47 | + |
| 48 | + try: |
| 49 | + |
| 50 | + oParser = ArgumentParser() |
| 51 | + oParser.add_argument("-c", "--config", dest="confFile", required=True) |
| 52 | + oParser.add_argument("-f", "--file", dest="fileWithIds", required=True) |
| 53 | + oParser.add_argument("-p", "--networkPrefix", dest="prefix", required=True) |
| 54 | + |
| 55 | + args = oParser.parse_args() |
| 56 | + sConfigs = HarvestingReadConfigs(args.confFile) |
| 57 | + sConfigs.setApplicationDir(os.getcwd()) |
| 58 | + |
| 59 | + appContext = ApplicationContext() |
| 60 | + appContext.setConfiguration(sConfigs) |
| 61 | + |
| 62 | + deleteMongo = MongoDelete(appContext) |
| 63 | + |
| 64 | + idFile = open(args.fileWithIds,"r") |
| 65 | + for line in idFile: |
| 66 | + deleteMongo.deleteId("".join(['(',args.prefix,')',line]).strip('\n')) |
| 67 | + #deleteMongo.readId("".join(['(', args.prefix, ')', line]).strip('\n')) |
| 68 | + |
| 69 | + |
| 70 | + |
| 71 | + except Exception as pythonBaseException: |
| 72 | + print str(pythonBaseException) |
| 73 | + |
| 74 | + finally: |
| 75 | + if not deleteMongo is None: |
| 76 | + deleteMongo.closeResources() |
| 77 | + print("records found: " + str(deleteMongo.numbers)) |
0 commit comments