-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathremoveCollection.py
86 lines (71 loc) · 2.93 KB
/
removeCollection.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
"""
removeCollection.py
Author(s): Abby Gleason, Josh Seibel
Description: Remove specified "collection" from existing json file
Dependencies: Python 3.x
"""
import io
import os
import json
import argparse
import sys
from collections import OrderedDict
class CollectionRemove(object):
def __init__(self, FOLDER, COLLECTION):
self.FOLDER = FOLDER
self.COLLECTION = COLLECTION
# Check for trailing backslash and remove
def formatPath(self, folderPath):
if folderPath[-1] == "\\":
folderReady = folderPath[:-1]
else:
folderReady = folderPath
return folderReady
# Creates an output sub-folder if one does not already exist
def createOutput(self, folderPath, collectionName):
folderPath = self.formatPath(folderPath) + "\\output"
# Create folder if doesn't exist
if os.path.exists(folderPath):
print("Output folder already exists.")
if not os.path.exists(folderPath):
os.makedirs(folderPath)
print("Output folder created.")
# Read through each json file and remove the specific collection
def removeCollection(self, folderPath, collectionName):
folderPath = self.formatPath(folderPath)
for filename in os.listdir(folderPath):
if filename.endswith(".json"):
jsonFile = json.load(io.open(folderPath + "\\" + filename, "rt", encoding="utf8"), object_pairs_hook=OrderedDict)
self.checkExistingCollection(collectionName, filename, jsonFile)
newFile = folderPath + "\\output\\" + filename
with open (newFile, 'w') as outfile:
json.dump(jsonFile, outfile)
print("Collection removed.")
# Remove the collection name if it exists in file
def checkExistingCollection(self, collectionName, filename, jsonFile):
if collectionName in jsonFile["dct_isPartOf_sm"]:
while collectionName in jsonFile["dct_isPartOf_sm"]:
jsonFile["dct_isPartOf_sm"].remove(collectionName)
else:
print("Warning: Collection does not exist in file " + filename)
return
def main():
parser = argparse.ArgumentParser()
parser.add_argument(
"-f",
"--folderPath",
help="Indicate path to folder to run script.")
group = parser.add_mutually_exclusive_group(required=True)
group.add_argument(
"-c",
"--collectionName",
help="Remove specified collection from dct_isPartOf_sm.")
args = parser.parse_args()
interface = CollectionRemove(FOLDER=args.folderPath, COLLECTION=args.collectionName)
if (args.folderPath) and (args.collectionName):
interface.createOutput(args.folderPath, args.collectionName)
interface.removeCollection(args.folderPath, args.collectionName)
else:
sys.exit(parser.print_help())
if __name__ == "__main__":
sys.exit(main())