forked from ENCODE-DCC/submission_sample_scripts
-
Notifications
You must be signed in to change notification settings - Fork 0
/
patch.py
54 lines (45 loc) · 1.88 KB
/
patch.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
import os
import sys
import csv
import json
import requests
import argparse
def get_settings(key_file,server_name):
key_list = open(key_file)
keys = csv.DictReader(key_list,delimiter = '\t')
settings = {}
for key in keys:
if key.get('Server') == server_name:
settings['USER'] = key.get('User')
settings['SERVER'] = ('http://' + key.get('Server') + '.encodedcc.org')
settings['AUTHID'] = key.get('ID')
settings['AUTHPW'] = key.get('PW')
key_list.close()
return settings
def read_objects(object_file):
json_file = open(object_file)
json_objects = json.load(json_file)
json_file.close()
return json_objects
def filter_object(json_object,key):
json_duplicate = json_object
json_duplicate.pop(key)
return json.dumps(json_duplicate)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='Script to patch json objects with only fields you require.')
parser.add_argument('key_file', help='A tab delimited file that contains keys.')
parser.add_argument('server', help='The server you want to patch objects to.')
parser.add_argument('json_file', help='The JSON file that contains a list of objects you want to patch.')
args = parser.parse_args()
# set headers. UNCLEAR IF THIS IS USED PROPERLY
HEADERS = {'content-type': 'application/json'}
settings = get_settings(args.key_file,str(args.server))
patch_objects = read_objects(args.json_file)
for i in range(0,len(patch_objects)):
object_id = patch_objects[i]['@id']
patch_object = filter_object(patch_objects[i], '@id')
url = (settings.get('SERVER') + '/' + str(object_id))
authid = settings.get('AUTHID')
authpw = settings.get('AUTHPW')
response = requests.patch(url, auth=(authid, authpw), headers=HEADERS, data=patch_object)
print response.text