Skip to content

Commit ff90829

Browse files
committed
Add option to delete files and fix uploading of file that already exists
1 parent ff0d32f commit ff90829

File tree

2 files changed

+39
-24
lines changed

2 files changed

+39
-24
lines changed

caltechdata_api/caltechdata_edit.py

Lines changed: 31 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -81,33 +81,44 @@ def caltechdata_edit(
8181
for idv in ids:
8282
metadata["id"] = idv
8383

84-
if files:
85-
# Files to delete
84+
if files or delete:
85+
# Files to add or delete
8686
fjson = {}
8787
c = session()
88-
existing = c.get(api_url + idv)
89-
file_info = existing.json()["metadata"]
88+
existing_meta = c.get(api_url + idv)
89+
file_info = existing_meta.json()["metadata"]
90+
existing = {}
91+
if "electronic_location_and_access" in file_info:
92+
for ex in file_info["electronic_location_and_access"]:
93+
name = ex["electronic_name"][0]
94+
fu = ex["uniform_resource_identifier"].split("/")[-2]
95+
existing[name] = fu
96+
97+
# File ids to delete
9098
fids = []
91-
for f in files: # Check if new files match existing
92-
if "electronic_location_and_access" in file_info:
93-
for ex in file_info["electronic_location_and_access"]:
94-
name = ex["electronic_name"][0]
95-
fu = ex["uniform_resource_identifier"].split("/")[-2]
96-
if name == f:
99+
100+
if files:
101+
for f in files: # Check if new files match existing
102+
# In case we have a path
103+
fname = f.split("/")[-1]
104+
if fname in existing:
105+
fids.append(existing[fname])
106+
# Now upload new files
107+
fileinfo = [send_s3(f, token, production) for f in files]
108+
fjson["new"] = fileinfo
109+
110+
if delete:
111+
for d in delete:
112+
if d in existing:
113+
fids.append(existing[d])
114+
# Also check if we are deleting by extenson
115+
for e in existing.keys():
116+
if e.split(".")[-1] == d:
97117
fids.append(fu)
98-
for d in delete:
99-
if name == d:
100-
fids.append(fu)
101-
if name.split(".")[-1] == d:
102-
fids.append(fu)
118+
103119
if len(fids) > 0:
104120
fjson = {"delete": fids}
105121

106-
# upload new
107-
print(files)
108-
fileinfo = [send_s3(f, token, production) for f in files]
109-
110-
fjson["new"] = fileinfo
111122
metadata["files"] = fjson
112123

113124
dat = json.dumps({"record": metadata})

edit.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,24 @@
44
parser = argparse.ArgumentParser(description=\
55
"Write files and a DataCite 4 standard json record\
66
to CaltechDATA repository")
7-
parser.add_argument('json_file', nargs=1, help=\
7+
parser.add_argument('json_file', nargs='?', default=None, help=\
88
'file name for json DataCite metadata file')
99
parser.add_argument('-ids', nargs='*', help='CaltechDATA IDs')
1010
parser.add_argument('-fnames', nargs='*', help='New Files')
1111
parser.add_argument('-schema', default="40", help="Metadata Schema")
12+
parser.add_argument('-delete', nargs='*', default="{}", help="Filename or extension to delete")
1213
args = parser.parse_args()
1314

1415
#Get access token from TIND set as environment variable with source token.bash
1516
token = os.environ['TINDTOK']
1617

17-
metaf = open(args.json_file[0], 'r')
18-
metadata = json.load(metaf)
18+
if args.json_file:
19+
metaf = open(args.json_file, 'r')
20+
metadata = json.load(metaf)
21+
else:
22+
metadata={}
1923

2024
production = True
2125

22-
response = caltechdata_edit(token, args.ids, metadata, args.fnames, {'pdf'}, production, args.schema)
26+
response = caltechdata_edit(token, args.ids, metadata, args.fnames, args.delete, production, args.schema)
2327
print(response)

0 commit comments

Comments
 (0)