From f28a25fc314adee739991d68c6747b471ee6e1fe Mon Sep 17 00:00:00 2001 From: Drew Erickson Date: Thu, 12 Sep 2013 08:47:43 -0700 Subject: [PATCH] json.validate usage / delimiter change The validation by json.validate is now properly used in a try/except/else function. The delimiter for object files was changed, since '-' is present in uuids. --- ...54HG007010.json => award;U54HG007010.json} | 0 ...ck@uw.edu.json => user;msbuck@uw.edu.json} | 0 update.py | 29 +++++++++++-------- 3 files changed, 17 insertions(+), 12 deletions(-) rename objects/{award-U54HG007010.json => award;U54HG007010.json} (100%) rename objects/{user-msbuck@uw.edu.json => user;msbuck@uw.edu.json} (100%) diff --git a/objects/award-U54HG007010.json b/objects/award;U54HG007010.json similarity index 100% rename from objects/award-U54HG007010.json rename to objects/award;U54HG007010.json diff --git a/objects/user-msbuck@uw.edu.json b/objects/user;msbuck@uw.edu.json similarity index 100% rename from objects/user-msbuck@uw.edu.json rename to objects/user;msbuck@uw.edu.json diff --git a/update.py b/update.py index d00ef65..5e59f8f 100644 --- a/update.py +++ b/update.py @@ -76,18 +76,19 @@ def WriteJSON(new_object,object_file): # run for each object in objects folder for object_filename in object_filenames: - # define object parameters - object_type,object_name = object_filename.strip('.json').split('-') - object_file = ('objects/' + object_type + '-' + object_name + '.json') + # define object parameters. SHOULD NOT RELY ON FILENAME. NEED WAY TO IDENTIFY OBJECT TYPE/NAME BY REVIEWING DATA + object_type,object_name = object_filename.strip('.json').split(';') + object_file = ('objects/' + object_type + ';' + object_name + '.json') object_id = ('/' + object_type + 's/' + object_name + '/') # load object json_object = open(object_file) new_object = json.load(json_object) json_object.close() - - - #check to see if object already exists + + # check to see if object already exists + # PROBLEM: SHOULD CHECK UUID AND NOT USE ANY SHORTCUT METADATA THAT MIGHT NEED TO CHANGE + # BUT CAN'T USE UUID IF NEW... HENCE PROBLEM old_object = get_ENCODE(object_id) # if object is not found, verify and post it @@ -96,18 +97,22 @@ def WriteJSON(new_object,object_file): # get relevant schema object_schema = get_ENCODE(('/profiles/' + object_type + '.json')) - # test the new object. SHOULD HANDLE ERRORS GRACEFULLY - if jsonschema.validate(new_object,object_schema): - + # test the new object. SHOULD HANDLE ERRORS GRACEFULLY + try: + jsonschema.validate(new_object,object_schema) + # did not validate + except Exception as e: + print('Validation of ' + object_id + ' failed.') + print(e) + + # did validate + else: # inform the user of the success print('Validation of ' + object_id + ' succeeded.') # post the new object(s). SHOULD HANDLE ERRORS GRACEFULLY response = new_ENCODE('/users/',new_object) - # inform the user of the failure. - else: - print('Validation of ' + object_id + ' failed.') # if object is found, check for differences and patch it if needed. else: