def test_instantiate_with_credentials_config_override(self): """Test instantiation of client: No exception if password wrong.""" # Test variables credentials = MagicMock() config_from_cred = {} valuefoo = 'foo/foo/foo/' config_from_cred['REST_API_url_extension'] = valuefoo credentials.get_config = MagicMock(return_value=config_from_cred) credentials.get_username = MagicMock(return_value=self.user) credentials.get_password = MagicMock(return_value=self.randompassword) credentials.get_server_URL = MagicMock(return_value=self.url) self.assertEqual(credentials.get_config()['REST_API_url_extension'],valuefoo, 'Config: '+str(credentials.get_config())) # Run code to be tested # Create instance with credentials inst = EUDATHandleClient.instantiate_with_credentials( credentials, HTTPS_verify=self.https_verify, REST_API_url_extension='api/handles') # If this raises an exception, it is because /foo/foo from the # credentials config was used as path. /foo/foo should be overridden # by the standard stuff. # Check desired outcomes self.assertIsInstance(inst, EUDATHandleClient) val = self.inst.get_value_from_handle(self.handle, 'test1') self.assertEqual(val, 'val1', 'Retrieving "test1" should lead to "val1", but it lead to: '+str(val))
def read(args): """perform read action""" try: # load credentials credentials = PIDClientCredentials.load_from_JSON(args.credpath) except CredentialsFormatError: sys.stdout.write('error') return except HandleSyntaxError: sys.stdout.write('error') return # retrieve and set extra values extra_config = {} try: # setup connection to handle server client = EUDATHandleClient.instantiate_with_credentials( credentials, **extra_config) except HandleNotFoundException: sys.stdout.write('error') return if args.key is None: result = read_execution(client, args.handle) else: result = read_execution(client, args.handle, args.key) sys.stdout.write(result)
def test_instantiate_with_credentials_config(self): """Test instantiation of client: No exception if password wrong.""" # Test variables credentials = mock.MagicMock() config_from_cred = {} valuefoo = 'foo/foo/foo/' config_from_cred['REST_API_url_extension'] = valuefoo credentials = b2handle.clientcredentials.PIDClientCredentials( handle_server_url=self.url, username=self.user, password=self.randompassword, handleowner=self.user, REST_API_url_extension=valuefoo) self.assertEqual(credentials.get_config()['REST_API_url_extension'], valuefoo, 'Config: ' + str(credentials.get_config())) # foo/foo/ from the credentials should override default api/handles/ # Run code to be tested - we expect an exception, as it will try to do a GET on the bogus rest api: with self.assertRaises(GenericHandleError): inst = EUDATHandleClient.instantiate_with_credentials( credentials, HTTPS_verify=self.https_verify) # So this code can only be reached if something went wrong: self.assertIsInstance(inst, EUDATHandleClient) # Check if foo/foo instead of api/handles was stored as path! serverconn = inst._EUDATHandleClient__handlesystemconnector self.assertIn( '/foo/', serverconn._HandleSystemConnector__REST_API_url_extension) self.assertEquals( serverconn._HandleSystemConnector__REST_API_url_extension, valuefoo)
def register_handle(self, location): """Register a handle.""" pid = '' try: credential = PIDClientCredentials.load_from_JSON( self.credential_path) client = EUDATHandleClient.instantiate_with_credentials(credential) pid = credential.get_prefix() + '/' \ + "{:010d}".format(int(location.split('/records/')[1])) handle = client.register_handle(pid, location) current_app.logger.info( 'Registered successfully handle {}'.format(pid)) return handle except (FileNotFoundError, CredentialsFormatError, HandleAuthenticationError, GenericHandleError) as e: current_app.logger.error('Registration failed of handle {}. {} in ' 'HandleClient.register_handle'.format( pid, e)) return None except AttributeError: current_app.logger.error('Missing Private Key!') return None except Exception as e: current_app.logger.error(e) return None
def test_instantiate_with_credentials_config(self): """Test instantiation of client: No exception if password wrong.""" # Test variables credentials = MagicMock() config_from_cred = {} valuefoo = "foo/foo/foo/" config_from_cred["REST_API_url_extension"] = valuefoo credentials.get_config = MagicMock(return_value=config_from_cred) credentials.get_username = MagicMock(return_value=self.user) credentials.get_password = MagicMock(return_value=self.randompassword) credentials.get_server_URL = MagicMock(return_value=self.url) credentials.get_handleowner = MagicMock(return_value=None) credentials.get_path_to_private_key = MagicMock(return_value=None) credentials.get_path_to_file_certificate_only = MagicMock(return_value=None) credentials.get_path_to_file_certificate_and_key = MagicMock(return_value=None) self.assertEqual( credentials.get_config()["REST_API_url_extension"], valuefoo, "Config: " + str(credentials.get_config()) ) # Run code to be tested # Create instance with credentials with self.assertRaises(GenericHandleError): inst = EUDATHandleClient.instantiate_with_credentials(credentials, HTTPS_verify=self.https_verify)
def create(args): """perform create action""" try: # load credentials credentials = PIDClientCredentials.load_from_JSON(args.credpath) except CredentialsFormatError: sys.stdout.write('error') return except HandleSyntaxError: sys.stdout.write('error') return # retrieve and set extra values extra_config = {} # create a handle to put. Concate the prefix with a new generated suffix prefix = str(credentials.get_prefix()) uid = uuid.uuid1() suffix = str(uid) handle = prefix+"/"+suffix try: # setup connection to handle server client = EUDATHandleClient.instantiate_with_credentials( credentials, **extra_config) except HandleNotFoundException: sys.stdout.write('error') return overwrite=False result = create_execution(client, handle, args.location, overwrite, args.checksum, args.loc10320, args.extratype) sys.stdout.write(result)
def modify(args): """perform modify action""" # load credentials credentials = PIDClientCredentials.load_from_JSON(args.credpath) # retrieve and set extra values extra_config = {} # setup connection to handle server client = EUDATHandleClient.instantiate_with_credentials( credentials, **extra_config) kvpairs = dict([(args.key, args.value)]) # modify key/value pairs result = client.modify_handle_value(args.handle, ttl=None, add_if_not_exist=True, **kvpairs) output_result = str(result) if output_result == 'None': output_result = 'True' sys.stdout.write(output_result)
def test_instantiate_with_credentials_config_override(self): """Test instantiation of client: No exception if password wrong.""" # Test variables credentials = MagicMock() config_from_cred = {} valuefoo = 'foo/foo/foo/' config_from_cred['REST_API_url_extension'] = valuefoo credentials.get_config = MagicMock(return_value=config_from_cred) credentials.get_username = MagicMock(return_value=self.user) credentials.get_password = MagicMock(return_value=self.randompassword) credentials.get_server_URL = MagicMock(return_value=self.url) self.assertEqual(credentials.get_config()['REST_API_url_extension'], valuefoo, 'Config: ' + str(credentials.get_config())) # Run code to be tested # Create instance with credentials inst = EUDATHandleClient.instantiate_with_credentials( credentials, HTTPS_verify=self.https_verify, REST_API_url_extension='api/handles') # If this raises an exception, it is because /foo/foo from the # credentials config was used as path. /foo/foo should be overridden # by the standard stuff. # Check desired outcomes self.assertIsInstance(inst, EUDATHandleClient) val = self.inst.get_value_from_handle(self.handle, 'test1') self.assertEqual( val, 'val1', 'Retrieving "test1" should lead to "val1", but it lead to: ' + str(val))
def connect_client(self, force_no_credentials=False, disable_logs=False): if getattr(self, '_handle_client', None) is None: if disable_logs: import logging logging.getLogger('b2handle').setLevel(logging.WARNING) # With credentials if force_no_credentials: self._handle_client = b2handle.instantiate_for_read_access() log.debug("HANDLE client connected [w/out credentials]") else: found = False file = os.environ.get('HANDLE_CREDENTIALS', None) if file is not None: from utilities import path credentials_path = path.build(file) found = path.file_exists_and_nonzero(credentials_path) if not found: log.warning("B2HANDLE credentials file not found %s", file) if found: self._handle_client = \ b2handle.instantiate_with_credentials( credentials.load_from_JSON(file) ) log.debug("HANDLE client connected [w/ credentials]") return self._handle_client, True return self._handle_client, False
def create(args): """perform create action""" try: # load credentials credentials = PIDClientCredentials.load_from_JSON(args.credpath) except CredentialsFormatError: sys.stdout.write('error') return except HandleSyntaxError: sys.stdout.write('error') return # retrieve and set extra values extra_config = {} # create a handle to put. Concate the prefix with a new generated suffix prefix = str(credentials.get_prefix()) uid = uuid.uuid1() suffix = str(uid) handle = prefix + "/" + suffix try: # setup connection to handle server client = EUDATHandleClient.instantiate_with_credentials( credentials, **extra_config) except HandleNotFoundException: sys.stdout.write('error') return overwrite = False result = create_execution(client, handle, args.location, overwrite, args.checksum, args.loc10320, args.extratype) sys.stdout.write(result)
def conn_handle(credentials='cred_21.T12995.json'): cred = PIDClientCredentials.load_from_JSON(credentials) print GREEN, "DEBUG" print('PID prefix ' + cred.get_prefix()) print('Server ' + cred.get_server_URL()) print DEFAULT ec = EUDATHandleClient.instantiate_with_credentials(cred) return ec, cred
def retrieve_handle(self, handle): """Retrieve a handle.""" try: credential = PIDClientCredentials.load_from_JSON( self.credential_path) client = EUDATHandleClient.instantiate_with_credentials(credential) handle_record_json = client.retrieve_handle_record_json(handle) return jsonify(handle_record_json) except (CredentialsFormatError, FileNotFoundError) as e: current_app.logger.error( '{} in HandleClient.retrieve_handle_record_json({})'.format( e, handle))
def test_instantiate_with_credentials_inexistentuser(self): """Test instantiation of client: Exception if username does not exist.""" # Test variables testusername_inexistent = '100:' + self.inexistent_handle credentials = b2handle.clientcredentials.PIDClientCredentials( self.url, testusername_inexistent, self.randompassword) # Run code to be tested + check exception: # Create instance with credentials with self.assertRaises(HandleNotFoundException): inst = EUDATHandleClient.instantiate_with_credentials( credentials, HTTPS_verify=self.https_verify)
def test_instantiate_with_credentials_inexistentuser(self): """Test instantiation of client: Exception if username does not exist.""" # Test variables testusername_inexistent = "100:" + self.inexistent_handle credentials = b2handle.clientcredentials.PIDClientCredentials( handle_server_url=self.url, username=testusername_inexistent, password=self.randompassword ) # Run code to be tested + check exception: # Create instance with credentials with self.assertRaises(HandleNotFoundException): inst = EUDATHandleClient.instantiate_with_credentials(credentials, HTTPS_verify=self.https_verify)
def read(args): """perform read action""" try: # load credentials credentials = PIDClientCredentials.load_from_JSON(args.credpath) except CredentialsFormatError: sys.stdout.write('error') return except HandleSyntaxError: sys.stdout.write('error') return # retrieve and set extra values extra_config = {} try: # setup connection to handle server client = EUDATHandleClient.instantiate_with_credentials( credentials, **extra_config) except HandleNotFoundException: sys.stdout.write('error') return # set default return value json_result = "None" if args.key is None: try: # retrieve whole handle result = client.retrieve_handle_record_json(args.handle) except HandleSyntaxError: json_result = 'error' if result is not None: json_result = json.dumps(result["values"]) else: try: # retrieve single value from a handle result = client.get_value_from_handle(args.handle, args.key) except HandleSyntaxError: json_result = 'error' if result is not None: json_result = json.dumps(result) # remove starting and finishing quotes. json_result = json_result.lstrip('"') json_result = json_result.rstrip('"') sys.stdout.write(json_result)
def main(): # parse command line options and arguments: modes = [ 'x', 'xmlfiles', 'j', 'jsonfiles', 'c', 'ckandatasets', 'p', 'pids', 'x-p', 'x-j', 'j-c', 'j-p' ] p = options_parser(modes) options, arguments = p.parse_args() # check option 'mode' and generate process list: (mode, pstat) = pstat_init(p, modes, options.mode, options.source, options.host) # check for quiet mode if (options.quiet): qmsg = 'would be' mainmode = 'check' else: qmsg = 'is' mainmode = 'deletion' if options.host: print "\tCKAN HOST:\t%s" % (options.host) if options.handle_check: print "\tCREDENTIAL:\t%s" % (options.handle_check) print '=' * 90 # make jobdir now = time.strftime("%Y-%m-%d %H:%M:%S") jid = os.getpid() print "\tStart of processing:\t%s" % (now) global logger OUT = Output(pstat, now, jid, options) ##HEW-D logger = log.getLogger() ## logger logger = OUT.setup_custom_logger('root', options.verbose) # create credentials if required if (options.handle_check): try: cred = PIDClientCredentials.load_from_JSON('credentials_11098') except Exception, err: logger.critical( "[CRITICAL] %s Could not create credentials from credstore %s" % (err, options.handle_check)) p.print_help() sys.exit(-1) else: logger.debug("Create EUDATHandleClient instance") HandleClient = EUDATHandleClient.instantiate_with_credentials( cred, HTTPS_verify=True)
def test_instantiate_with_credentials(self): """Test instantiation of client: No exception if password wrong.""" # Test variables credentials = b2handle.clientcredentials.PIDClientCredentials( handle_server_url=self.url, username=self.user, password=self.randompassword ) # Run code to be tested # Create instance with credentials inst = EUDATHandleClient.instantiate_with_credentials(credentials, HTTPS_verify=self.https_verify) # Check desired outcomes self.assertIsInstance(inst, EUDATHandleClient)
def test_instantiate_with_credentials(self): """Test instantiation of client: No exception if password wrong.""" # Test variables credentials = b2handle.clientcredentials.PIDClientCredentials( self.url, self.user, self.randompassword) # Run code to be tested # Create instance with credentials inst = EUDATHandleClient.instantiate_with_credentials( credentials, HTTPS_verify=self.https_verify) # Check desired outcomes self.assertIsInstance(inst, EUDATHandleClient)
def delete(args): """perform delete action""" try: # load credentials credentials = PIDClientCredentials.load_from_JSON(args.credpath) except CredentialsFormatError: sys.stdout.write('error') return except HandleSyntaxError: sys.stdout.write('error') return # retrieve and set extra values extra_config = {} try: # setup connection to handle server client = EUDATHandleClient.instantiate_with_credentials( credentials, **extra_config) except HandleNotFoundException: sys.stdout.write('error') return result = 'True' if args.key is None: # delete whole handle try: client.delete_handle(args.handle) except HandleAuthenticationError: result = 'error' except HandleNotFoundException: result = 'False' except HandleSyntaxError: result = 'error' else: # delete value try: client.delete_handle_value(args.handle, args.key) except HandleAuthenticationError: result = 'error' except HandleNotFoundException: result = 'False' except HandleSyntaxError: result = 'error' sys.stdout.write(result)
def test_instantiate_with_credentials_inexistentuser(self, getpatch): """Test instantiation of client: Exception if username does not exist.""" # Define the replacement for the patched method: mock_response = MockResponse(notfound=True) getpatch.return_value = mock_response # Test variables testusername_inexistent = '100:john/doe' credentials = b2handle.clientcredentials.PIDClientCredentials( 'some/url', testusername_inexistent, 'some_password') # Run code to be tested + check exception: # Create instance with credentials with self.assertRaises(HandleNotFoundException): inst = EUDATHandleClient.instantiate_with_credentials(credentials)
def relation(args): """perform the relation action""" # load credentials credentials = PIDClientCredentials.load_from_JSON(args.credpath) # retrieve and set extra values extra_config = {} # setup connection to handle server client = EUDATHandleClient.instantiate_with_credentials( credentials, **extra_config) # add relation to 10320/LOC result = client.add_additional_URL(args.ppid, args.cpid) sys.stdout.write(str(result))
def test_instantiate_with_credentials_config(self, checkpatch): """Test instantiation of client: No exception if password wrong.""" # Define the replacement for the patched method: mock_response = MockResponse(success=True) checkpatch.return_value = mock_response # Test variables credentials = MockCredentials(restapi='foobar') self.assertEqual(credentials.get_config()['REST_API_url_extension'], 'foobar', 'Config: ' + str(credentials.get_config())) # Run code to be tested # Create instance with credentials inst = EUDATHandleClient.instantiate_with_credentials(credentials) self.assertIsInstance(inst, EUDATHandleClient)
def test_instantiate_with_credentials_inexistentuser(self, getpatch): """Test instantiation of client: Exception if username does not exist.""" # Define the replacement for the patched method: mock_response = MockResponse(notfound=True) getpatch.return_value = mock_response # Test variables testusername_inexistent = "100:john/doe" credentials = b2handle.clientcredentials.PIDClientCredentials( "some/url", testusername_inexistent, "some_password" ) # Run code to be tested + check exception: # Create instance with credentials with self.assertRaises(HandleNotFoundException): inst = EUDATHandleClient.instantiate_with_credentials(credentials)
def test_instantiate_with_credentials_config(self, checkpatch): """Test instantiation of client: No exception if password wrong.""" # Define the replacement for the patched method: mock_response = MockResponse(success=True) checkpatch.return_value = mock_response # Test variables credentials = MockCredentials(restapi='foobar') self.assertEqual(credentials.get_config()['REST_API_url_extension'],'foobar', 'Config: '+str(credentials.get_config())) # Run code to be tested # Create instance with credentials inst = EUDATHandleClient.instantiate_with_credentials(credentials) self.assertIsInstance(inst, EUDATHandleClient)
def test_instantiate_with_credentials(self, getpatch): """Test instantiation of client: No exception if password wrong.""" # Define the replacement for the patched method: mock_response = MockResponse(success=True) getpatch.return_value = mock_response # Test variables credentials = b2handle.clientcredentials.PIDClientCredentials( 'some/url', '100:my/testhandle', 'some_password_123') # Run code to be tested # Create instance with credentials inst = EUDATHandleClient.instantiate_with_credentials(credentials) # Check desired outcomes self.assertIsInstance(inst, EUDATHandleClient)
def test_instantiate_with_credentials(self, getpatch): """Test instantiation of client: No exception if password wrong.""" # Define the replacement for the patched method: mock_response = MockResponse(success=True) getpatch.return_value = mock_response # Test variables credentials = b2handle.clientcredentials.PIDClientCredentials( "some/url", "100:my/testhandle", "some_password_123" ) # Run code to be tested # Create instance with credentials inst = EUDATHandleClient.instantiate_with_credentials(credentials) # Check desired outcomes self.assertIsInstance(inst, EUDATHandleClient)
def modify(args): """perform modify action""" try: # load credentials credentials = PIDClientCredentials.load_from_JSON(args.credpath) except CredentialsFormatError: sys.stdout.write('error') return except HandleSyntaxError: sys.stdout.write('error') return # retrieve and set extra values extra_config = {} try: # setup connection to handle server client = EUDATHandleClient.instantiate_with_credentials( credentials, **extra_config) except HandleNotFoundException: sys.stdout.write('error') return kvpairs = dict([(args.key, args.value)]) result = 'True' try: # modify key/value pairs client.modify_handle_value( args.handle, ttl=None, add_if_not_exist=True, **kvpairs) except HandleAuthenticationError: result = 'error' except HandleNotFoundException: result = 'False' except HandleSyntaxError: result = 'error' sys.stdout.write(result)
def main(): # parse command line options and arguments: modes=['x','xmlfiles','j','jsonfiles','c','ckandatasets','p','pids','x-p', 'x-j', 'j-c','j-p'] p = options_parser(modes) options,arguments = p.parse_args() # check option 'mode' and generate process list: (mode, pstat) = pstat_init(p,modes,options.mode,options.source,options.host) # check for quiet mode if (options.quiet): qmsg='would be' mainmode='check' else: qmsg='is' mainmode='deletion' if options.host : print "\tCKAN HOST:\t%s" % (options.host) if options.handle_check : print "\tCREDENTIAL:\t%s" % (options.handle_check) print '='*90 # make jobdir now = time.strftime("%Y-%m-%d %H:%M:%S") jid = os.getpid() print "\tStart of processing:\t%s" % (now) global logger OUT = Output(pstat,now,jid,options) ##HEW-D logger = log.getLogger() ## logger logger = OUT.setup_custom_logger('root',options.verbose) # create credentials if required if (options.handle_check): try: cred = PIDClientCredentials.load_from_JSON('credentials_11098') except Exception, err: logger.critical("[CRITICAL] %s Could not create credentials from credstore %s" % (err,options.handle_check)) p.print_help() sys.exit(-1) else: logger.debug("Create EUDATHandleClient instance") HandleClient = EUDATHandleClient.instantiate_with_credentials(cred,HTTPS_verify=True)
def create(args): """perform create action""" # load credentials credentials = PIDClientCredentials.load_from_JSON(args.credpath) # retrieve and set extra values extra_config = {} # create a handle to put. Concate the prefix with a new generated suffix prefix = str(credentials.get_prefix()) uid = uuid.uuid1() suffix = str(uid) handle = prefix + "/" + suffix # setup connection to handle server client = EUDATHandleClient.instantiate_with_credentials( credentials, **extra_config) # pre-process the input parameters for the handle api extype = {} if args.extratype is not None: exlist = args.extratype.split(';') for item in exlist: key = item.split('=')[0] extype[key] = item.split('=')[1] if args.loc10320 is not None: l10320 = args.loc10320.split(';') else: l10320 = None # create the new handle result = client.register_handle(handle, location=args.location, checksum=args.checksum, additional_URLs=l10320, overwrite=False, **extype) if result is None: sys.stdout.write("error") else: sys.stdout.write(result)
def process_upload(UP, rlist): ##HEW-D-ec credentials,ec = None,None def print_extra(key,jsondata): for v in jsondata['extras']: if v['key'] == key: print ' Key : %s | Value : %s |' % (v['key'],v['value']) # create credentials and handle client if required if (options.handle_check): try: cred = PIDClientCredentials.load_from_JSON('credentials_11098') except Exception, err: logger.critical("[CRITICAL %s ] : Could not create credentials from credstore %s" % (err,options.handle_check)) ##p.print_help() sys.exit(-1) else: logger.debug("Create EUDATHandleClient instance") client = EUDATHandleClient.instantiate_with_credentials(cred)
def search(args): """perform search action""" try: # load credentials credentials = PIDClientCredentials.load_from_JSON(args.credpath) except CredentialsFormatError: sys.stdout.write('error') return except HandleSyntaxError: sys.stdout.write('error') return # retrieve and set extra values extra_config = {} try: # setup connection to handle server client = EUDATHandleClient.instantiate_with_credentials( credentials, **extra_config) except HandleNotFoundException: sys.stdout.write('error') return kvpairs = dict([(args.key, str(''.join(args.value)))]) try: # search for handle result = client.search_handle(**kvpairs) except ReverseLookupException: result = '{error}' json_result = str(json.dumps(result)) if json_result == '[]': json_result = 'empty' elif json_result == '{error}': json_result = 'error' sys.stdout.write(json_result)
def test_instantiate_with_credentials_config(self): """Test instantiation of client: No exception if password wrong.""" # Test variables credentials = MagicMock() config_from_cred = {} valuefoo = 'foo/foo/foo/' config_from_cred['REST_API_url_extension'] = valuefoo credentials.get_config = MagicMock(return_value=config_from_cred) credentials.get_username = MagicMock(return_value=self.user) credentials.get_password = MagicMock(return_value=self.randompassword) credentials.get_server_URL = MagicMock(return_value=self.url) self.assertEqual(credentials.get_config()['REST_API_url_extension'], valuefoo, 'Config: ' + str(credentials.get_config())) # Run code to be tested # Create instance with credentials with self.assertRaises(GenericHandleError): inst = EUDATHandleClient.instantiate_with_credentials( credentials, HTTPS_verify=self.https_verify)
def delete(args): """perform delete action""" # load credentials credentials = PIDClientCredentials.load_from_JSON(args.credpath) # retrieve and set extra values extra_config = {} # setup connection to handle server client = EUDATHandleClient.instantiate_with_credentials( credentials, **extra_config) if args.key is None: # delete whole handle result = client.delete_handle(args.handle) else: # delete value result = client.delete_handle_value(args.handle, args.key) sys.stdout.write(str(result))
def __init__(self): # Load config parameters for ckanext-handle self.development = config.get("ckanext.handle.development") self.resource_field = config.get("ckanext.handle.resource_field") self.package_field = config.get("ckanext.handle.package_field") self.proxy = config.get("ckanext.handle.proxy") self.prefix = config.get("ckanext.handle.prefix") if not self.development: self.handle_server_url = config.get( "ckanext.handle.handle_server_url") self.private_key = config.get("ckanext.handle.private_key") self.certificate_only = config.get( "ckanext.handle.certificate_only") # Create credentials and client for handle interaction self.cred = PIDClientCredentials( handle_server_url=self.handle_server_url, private_key=self.private_key, certificate_only=self.certificate_only) self.client = EUDATHandleClient.instantiate_with_credentials( self.cred)
def test_instantiate_with_credentials_config_override(self, getpatch, checkpatch): """Test instantiation of client: We pass a config value in the credentials and also as an arg in the instantiation. We want the latter to override the first one. """ # Define the replacement for the patched method: # We pretend the username exists! mock_response = MockResponse(success=True) checkpatch.return_value = mock_response # Define the replacement for the patched GET: cont = {"responseCode":1,"handle":"my/testhandle","values":[{"index":111,"type":"TEST1","data":{"format":"string","value":"val1"},"ttl":86400,"timestamp":"2015-09-30T15:08:49Z"},{"index":2222,"type":"TEST2","data":{"format":"string","value":"val2"},"ttl":86400,"timestamp":"2015-09-30T15:08:49Z"},{"index":333,"type":"TEST3","data":{"format":"string","value":"val3"},"ttl":86400,"timestamp":"2015-09-30T15:08:49Z"},{"index":4,"type":"TEST4","data":{"format":"string","value":"val4"},"ttl":86400,"timestamp":"2015-09-30T15:08:49Z"}]} mock_response = MockResponse(success=True, content=json.dumps(cont)) getpatch.return_value = mock_response # Test variables # Passing mock credentials, give them the value "foobar", which # should be overridden! credentials = MockCredentials(restapi='foobar') self.assertEqual(credentials.get_config()['REST_API_url_extension'],'foobar', 'Config: '+str(credentials.get_config())) # Run code to be tested # Create instance with credentials. It gets the "REST_API_url_extention" # from both the credentials and as a param. inst = EUDATHandleClient.instantiate_with_credentials( credentials, REST_API_url_extension='bar/bar/bar') self.assertIsInstance(inst, EUDATHandleClient) # How to know now which one was used? # Call a read and check its url! Did it get foobar or barbarbar appended? inst.get_value_from_handle('my/testhandle', 'key') positional_args_passed = getpatch.call_args_list[len(getpatch.call_args_list)-1][0] passed_url = positional_args_passed[0] # Compare with expected URL: self.assertIn('bar/bar/bar',passed_url, 'bar/bar/bar is not specified in the URL '+passed_url)
def test_instantiate_with_credentials_config(self): """Test instantiation of client: No exception if password wrong.""" # Test variables credentials = MagicMock() config_from_cred = {} valuefoo = 'foo/foo/foo/' config_from_cred['REST_API_url_extension'] = valuefoo credentials.get_config = MagicMock(return_value=config_from_cred) credentials.get_username = MagicMock(return_value=self.user) credentials.get_password = MagicMock(return_value=self.randompassword) credentials.get_server_URL = MagicMock(return_value=self.url) self.assertEqual(credentials.get_config()['REST_API_url_extension'],valuefoo, 'Config: '+str(credentials.get_config())) # Run code to be tested # Create instance with credentials with self.assertRaises(GenericHandleError): inst = EUDATHandleClient.instantiate_with_credentials( credentials, HTTPS_verify=self.https_verify)
def relation(args): """perform the relation action""" try: # load credentials credentials = PIDClientCredentials.load_from_JSON(args.credpath) except CredentialsFormatError: sys.stdout.write('error') return except HandleSyntaxError: sys.stdout.write('error') return # retrieve and set extra values extra_config = {} try: # setup connection to handle server client = EUDATHandleClient.instantiate_with_credentials( credentials, **extra_config) except HandleNotFoundException: sys.stdout.write('error') return result = 'None' # add relation to 10320/LOC try: client.add_additional_URL(args.ppid, args.cpid) except HandleAuthenticationError: result = 'error' except HandleNotFoundException: result = 'False' except HandleSyntaxError: result = 'error' sys.stdout.write(result)
def connect_client(self, force_no_credentials=False): found = False # With credentials if not force_no_credentials: file = os.environ.get('HANDLE_CREDENTIALS', None) if file is not None: from utilities import path credentials_path = path.build(file) found = path.file_exists_and_nonzero(credentials_path) if not found: log.warning("B2HANDLE credentials file not found %s", file) if found: client = b2handle.instantiate_with_credentials( credentials.load_from_JSON(file)) log.info("PID client connected: w/ credentials") return client, True client = b2handle.instantiate_for_read_access() log.warning("PID client connected: NO credentials") return client, False
def create(args): """perform create action""" try: # load credentials credentials = PIDClientCredentials.load_from_JSON(args.credpath) except CredentialsFormatError: sys.stdout.write('error') return except HandleSyntaxError: sys.stdout.write('error') return # retrieve and set extra values extra_config = {} # create a handle to put. Concate the prefix with a new generated suffix prefix = str(credentials.get_prefix()) uid = uuid.uuid1() suffix = str(uid) handle = prefix+"/"+suffix try: # setup connection to handle server client = EUDATHandleClient.instantiate_with_credentials( credentials, **extra_config) except HandleNotFoundException: sys.stdout.write('error') return # pre-process the input parameters for the handle api extype = {} if args.extratype is not None: exlist = args.extratype.split(';') for item in exlist: key = item.split('=')[0] value = item.split('=')[1] extype[key] = value if args.loc10320 is not None: l10320 = args.loc10320.split(';') else: l10320 = None # replace "EUDAT/ROR=pid" with "EUDAT/ROR=handle" key = 'EUDAT/ROR' if key in extype: if extype[key].lower() == 'pid': extype[key] = handle result = '' try: # create the new handle result = client.register_handle( handle, location=args.location, checksum=args.checksum, additional_URLs=l10320, overwrite=False, **extype) except HandleAlreadyExistsException: result = 'False' except HandleAuthenticationError: result = 'error' except HandleSyntaxError: result = 'error' sys.stdout.write(result)
def process(options, pstat, OUT): ## process (options,pstat,OUT) - function # Starts processing as specified in pstat['tbd'] and # according the request list given bey the options # # Parameters: # ----------- # 1. options (OptionsParser object) # 2. pstat (process status dict) # # set list of request lsits for single or multi mode: mode = None procOptions = [ 'community', 'source', 'verb', 'mdprefix', 'mdsubset', 'target_mdschema' ] if (options.source): mode = 'single' mandParams = ['community', 'verb', 'mdprefix'] # mandatory processing params for param in mandParams: if not getattr(options, param): logger.critical( "Processing parameter %s is required in single mode" % param) sys.exit(-1) reqlist = [[ options.community, options.source, options.verb, options.mdprefix, options.mdsubset, options.ckan_check, options.handle_check, options.target_mdschema ]] elif (options.list): mode = 'multi' logger.debug(' |- Joblist: \t%s' % options.list) reqlist = parse_list_file(options) logger.debug(' |- Requestlist: \t%s' % reqlist) ## check job request (processing) options logger.debug('|- Command line options') for opt in procOptions: if hasattr(options, opt): logger.debug(' |- %s:\t%s' % (opt.upper(), getattr(options, opt))) ## HARVESTING mode: if (pstat['status']['h'] == 'tbd'): logger.info('\n|- Harvesting started : %s' % time.strftime("%Y-%m-%d %H:%M:%S")) HV = Harvester(OUT, pstat, options.outdir, options.fromdate) process_harvest(HV, reqlist) ## MAPPINING - Mode: if (pstat['status']['m'] == 'tbd'): logger.info('\n|- Mapping started : %s' % time.strftime("%Y-%m-%d %H:%M:%S")) MP = Mapper(OUT, options.outdir, options.fromdate) process_map(MP, reqlist) ## VALIDATING - Mode: if (pstat['status']['v'] == 'tbd'): logger.info(' |- Validating started : %s' % time.strftime("%Y-%m-%d %H:%M:%S")) MP = Mapper(OUT, options.outdir, options.fromdate) process_validate(MP, reqlist) ## OAI-CONVERTING - Mode: if (pstat['status']['o'] == 'tbd'): logger.info('\n|- OAI-Converting started : %s' % time.strftime("%Y-%m-%d %H:%M:%S")) MP = Mapper(OUT, options.outdir, options.fromdate) process_oaiconvert(MP, reqlist) ## UPLOADING - Mode: if (pstat['status']['u'] == 'tbd'): logger.info('\n|- Uploading started : %s' % time.strftime("%Y-%m-%d %H:%M:%S")) # create CKAN object CKAN = CKAN_CLIENT(options.iphost, options.auth) # create credentials and handle client if required if (options.handle_check): try: cred = PIDClientCredentials.load_from_JSON('credentials_11098') except Exception as err: logger.critical( "%s : Could not create credentials from credstore %s" % (err, options.handle_check)) ##p.print_help() sys.exit(-1) else: logger.debug("Create EUDATHandleClient instance") HandleClient = EUDATHandleClient.instantiate_with_credentials( cred) else: cred = None HandleClient = None UP = Uploader(CKAN, options.ckan_check, HandleClient, cred, OUT, options.outdir, options.fromdate, options.iphost) logger.info(' |- Host: \t%s' % CKAN.ip_host) process_upload(UP, reqlist) ## DELETING - Mode: if (pstat['status']['d'] == 'tbd'): # start the process deleting: logger.info('\n|- Deleting started : %s' % time.strftime("%Y-%m-%d %H:%M:%S")) if mode is 'multi': dir = options.outdir + '/delete' if os.path.exists(dir): process_delete(OUT, dir, options) else: logger.error( '[ERROR] The directory "%s" does not exist! No files for deleting are found!' % (dir)) else: logger.critical( "[CRITICAL] Deleting mode only supported in 'multi mode' and an explicitly deleting script given !" )
def bulk(args): """perform the bulk actions""" try: # open input file bulk_input_file = open(args.input, "r") except: sys.stdout.write('error opening: ' + args.input) return try: # open result file bulk_result_file = open(args.result, "w") except: sys.stdout.write('error opening: ' + args.result) return try: # load credentials credentials = PIDClientCredentials.load_from_JSON(args.credpath) except CredentialsFormatError: sys.stdout.write('error') return except HandleSyntaxError: sys.stdout.write('error') return # retrieve and set extra values extra_config = {} try: # setup connection to handle server client = EUDATHandleClient.instantiate_with_credentials( credentials, **extra_config) except HandleNotFoundException: sys.stdout.write('error') return for line in bulk_input_file: bulk_array = line.split() if bulk_array[0] == 'SEARCH': # search key value # search handle which matches criteria search_key = bulk_array[1] search_value = bulk_array[2] result = search_execution(client, search_key, search_value) bulk_result_file.write('search handle key: ' + search_key + ' value: ' + search_value + ' result: ' + result + '\n') if bulk_array[0] == 'READ': # READ handle # read whole handle # READ handle key # read key/value pair from handle read_handle = bulk_array[1] if len(bulk_array) >= 3: read_key = bulk_array[2] result = read_execution(client, read_handle, read_key) bulk_result_file.write('read handle: ' + read_handle + ' key: ' + read_key + ' result: ' + result + '\n') else: result = read_execution(client, read_handle) bulk_result_file.write('read handle: ' + read_handle + ' result: ' + result + '\n') if bulk_array[0] == 'CREATE': # CREATE prefix/uuid URL # create handle, use uuid for suffix # CREATE prefix/suffix URL # create handle, use suffix for handle, no check before if handle exists # CREATE prefix/uuid URL CHECKSUM # create handle, use uuid for suffix, add checksum # CREATE prefix/uuid URL CHECKSUM 10320/LOC # create handle, use uuid for suffix, add checksum, add 10320/LOC # CREATE prefix/uuid URL CHECKSUM 10320/LOC EXTRATYPE # create handle, use uuid for suffix, add checksum, add 10320/LOC, add extratypes overwrite = False checksum = None loc10320 = None extratype = None # create a handle to put. prefix = str(credentials.get_prefix()) create_prefix = bulk_array[1].split("/")[0] create_suffix = bulk_array[1].split("/")[1] if create_suffix == 'uuid': uid = uuid.uuid1() suffix = str(uid) handle = prefix + "/" + suffix else: handle = prefix + "/" + create_suffix overwrite = True if len(bulk_array) == 3: result = create_execution(client, handle, bulk_array[2], overwrite) else: if len(bulk_array) >= 4 and bulk_array[3].lower() != 'none': checksum = bulk_array[3] if len(bulk_array) >= 5 and bulk_array[4].lower() != 'none': loc10320 = bulk_array[4] if len(bulk_array) >= 6 and bulk_array[5].lower() != 'none': extratype = bulk_array[5] result = create_execution(client, handle, bulk_array[2], overwrite, checksum, loc10320, extratype) bulk_result_file.write('create handle: ' + bulk_array[1] + ' result: ' + result + '\n') if bulk_array[0] == 'DELETE': # DELETE handle # delete whole handle # DELETE handle key # delete key/value pair from handle delete_handle = bulk_array[1] if len(bulk_array) >= 3: delete_key = bulk_array[2] result = delete_execution(client, delete_handle, delete_key) bulk_result_file.write('delete handle: ' + delete_handle + ' key: ' + delete_key + ' result: ' + result + '\n') else: result = delete_execution(client, delete_handle) bulk_result_file.write('delete handle: ' + delete_handle + ' result: ' + result + '\n') if bulk_array[0] == 'MODIFY': # MODIFY handle key value # modify key/value pair in handle if len(bulk_array) == 4: modify_handle = bulk_array[1] modify_key = bulk_array[2] modify_value = bulk_array[3] result = modify_execution(client, modify_handle, modify_key, modify_value) bulk_result_file.write('modify handle: ' + modify_handle + ' key: ' + modify_key + ' value: ' + modify_value + ' result: ' + result + '\n') if bulk_array[0] == 'REPLACE': # REPLACE handle key data1 data2 # replace data1 with data2 in value part of key/value pair in handle if len(bulk_array) == 5: replace_handle = bulk_array[1] replace_key = bulk_array[2] replace_data1 = bulk_array[3] replace_data2 = bulk_array[4] result = read_execution(client, replace_handle, replace_key) if result != "None" and result != 'error': new_value = result.replace(replace_data1, replace_data2) if result != new_value: result = modify_execution(client, replace_handle, replace_key, new_value) else: result = "None" bulk_result_file.write('replace handle: ' + replace_handle + ' key: ' + replace_key + ' data1: ' + replace_data1 + ' data2: ' + replace_data2 + ' result: ' + result + '\n') bulk_input_file.close() bulk_result_file.close()
# Verbose information about the data if args.verbose > 0 : print(' The dataset to upload:\n\t%s' % dataset_dict) # Use the json module to dump the dictionary to a string for posting. data_string = urllib.quote(json.dumps(dataset_dict)) # By default package_create function is used to create a new dataset. request = urllib2.Request( 'http://'+args.ipadress+'/api/action/package_create') # Create a handle client and check handle if required handlestatus="unknown" if (args.handle_check): try: cred = PIDClientCredentials.load_from_JSON(args.handle_check) client = EUDATHandleClient.instantiate_with_credentials(cred) except Exception as err: print ("%s : Could not create credentials from credstore %s" % (err,args.handle_check)) sys.exit(-1) else: if args.verbose > 1 : print ("HandleClient created") pidRecord=dict() try: pid = cred.get_prefix() + '/eudat-jmd_' + dataset_dict['name'] rec = client.retrieve_handle_record_json(pid) except Exception as err : print ("ERROR : %s in client.retrieve_handle_record_json()" % (err)) else: if args.verbose > 0 : print(" Retrieved Handle %s with\n |%-12s\t|%-30s\t|%-30s|\n %s" % (pid,'Attribute','Value','Changed value',80*'-'))
def test_instantiate_with_credentials_config_override( self, getpatch, checkpatch): """Test instantiation of client: We pass a config value in the credentials and also as an arg in the instantiation. We want the latter to override the first one. """ # Define the replacement for the patched method: # We pretend the username exists! mock_response = MockResponse(success=True) checkpatch.return_value = mock_response # Define the replacement for the patched GET: cont = { "responseCode": 1, "handle": "my/testhandle", "values": [{ "index": 111, "type": "TEST1", "data": { "format": "string", "value": "val1" }, "ttl": 86400, "timestamp": "2015-09-30T15:08:49Z" }, { "index": 2222, "type": "TEST2", "data": { "format": "string", "value": "val2" }, "ttl": 86400, "timestamp": "2015-09-30T15:08:49Z" }, { "index": 333, "type": "TEST3", "data": { "format": "string", "value": "val3" }, "ttl": 86400, "timestamp": "2015-09-30T15:08:49Z" }, { "index": 4, "type": "TEST4", "data": { "format": "string", "value": "val4" }, "ttl": 86400, "timestamp": "2015-09-30T15:08:49Z" }] } mock_response = MockResponse(success=True, content=json.dumps(cont)) getpatch.return_value = mock_response # Test variables # Passing mock credentials, give them the value "foobar", which # should be overridden! credentials = MockCredentials(restapi='foobar') self.assertEqual(credentials.get_config()['REST_API_url_extension'], 'foobar', 'Config: ' + str(credentials.get_config())) # Run code to be tested # Create instance with credentials. It gets the "REST_API_url_extention" # from both the credentials and as a param. inst = EUDATHandleClient.instantiate_with_credentials( credentials, REST_API_url_extension='bar/bar/bar') self.assertIsInstance(inst, EUDATHandleClient) # How to know now which one was used? # Call a read and check its url! Did it get foobar or barbarbar appended? inst.get_value_from_handle('my/testhandle', 'key') positional_args_passed = getpatch.call_args_list[ len(getpatch.call_args_list) - 1][0] passed_url = positional_args_passed[0] # Compare with expected URL: self.assertIn('bar/bar/bar', passed_url, 'bar/bar/bar is not specified in the URL ' + passed_url)
def process(options,pstat,OUT): ## process (options,pstat,OUT) - function # Starts processing as specified in pstat['tbd'] and # according the request list given bey the options # # Parameters: # ----------- # 1. options (OptionsParser object) # 2. pstat (process status dict) # # set list of request lsits for single or multi mode: mode = None procOptions=['community','source','verb','mdprefix','mdsubset','target_mdschema'] if(options.source): mode = 'single' mandParams=['community','verb','mdprefix'] # mandatory processing params for param in mandParams : if not getattr(options,param) : logger.critical("Processing parameter %s is required in single mode" % param) sys.exit(-1) reqlist=[[ options.community, options.source, options.verb, options.mdprefix, options.mdsubset, options.ckan_check, options.handle_check, options.target_mdschema ]] elif(options.list): mode = 'multi' logger.debug(' |- Joblist: \t%s' % options.list) reqlist=parse_list_file(options) logger.debug(' |- Requestlist: \t%s' % reqlist) ## check job request (processing) options logger.debug('|- Command line options') for opt in procOptions : if hasattr(options,opt) : logger.debug(' |- %s:\t%s' % (opt.upper(),getattr(options,opt))) ## GENERATION mode: if (pstat['status']['g'] == 'tbd'): logger.info('\n|- Generation started : %s' % time.strftime("%Y-%m-%d %H:%M:%S")) GEN = Generator(OUT,pstat,options.outdir) process_generate(GEN,reqlist) ## HARVESTING mode: if (pstat['status']['h'] == 'tbd'): logger.info('\n|- Harvesting started : %s' % time.strftime("%Y-%m-%d %H:%M:%S")) HV = Harvester(OUT,pstat,options.outdir,options.fromdate) process_harvest(HV,reqlist) ## MAPPINING - Mode: if (pstat['status']['m'] == 'tbd'): logger.info('\n|- Mapping started : %s' % time.strftime("%Y-%m-%d %H:%M:%S")) MP = Mapper(OUT,options.outdir,options.fromdate) process_map(MP,reqlist) ## VALIDATING - Mode: if (pstat['status']['v'] == 'tbd'): logger.info(' |- Validating started : %s' % time.strftime("%Y-%m-%d %H:%M:%S")) VD = Validator(OUT,options.outdir,options.fromdate) process_validate(VD,reqlist) ## CONVERTING - Mode: if (pstat['status']['c'] == 'tbd'): logger.info('\n|- Converting started : %s' % time.strftime("%Y-%m-%d %H:%M:%S")) CV = Converter(OUT,options.outdir,options.fromdate) process_convert(CV, reqlist) ## UPLOADING - Mode: if (pstat['status']['u'] == 'tbd'): logger.info('\n|- Uploading started : %s' % time.strftime("%Y-%m-%d %H:%M:%S")) # create CKAN object CKAN = CKAN_CLIENT(options.iphost,options.auth) # create credentials and handle client if required if (options.handle_check): try: cred = PIDClientCredentials.load_from_JSON('credentials_11098') except Exception as err: logger.critical("%s : Could not create credentials from credstore %s" % (err,options.handle_check)) ##p.print_help() sys.exit(-1) else: logger.debug("Create EUDATHandleClient instance") HandleClient = EUDATHandleClient.instantiate_with_credentials(cred) else: cred=None HandleClient=None UP = Uploader(CKAN,options.ckan_check,HandleClient,cred,OUT,options.outdir,options.fromdate,options.iphost) logger.info(' |- Host: \t%s' % CKAN.ip_host ) process_upload(UP, reqlist) ## DELETING - Mode: if (pstat['status']['d'] == 'tbd'): # start the process deleting: logger.info('\n|- Deleting started : %s' % time.strftime("%Y-%m-%d %H:%M:%S")) if mode is 'multi': dir = options.outdir+'/delete' if os.path.exists(dir): process_delete(OUT, dir, options) else: logger.error('[ERROR] The directory "%s" does not exist! No files for deleting are found!' % (dir)) else: logger.critical("[CRITICAL] Deleting mode only supported in 'multi mode' and an explicitly deleting script given !")
def bulk(args): """perform the bulk actions""" try: # open input file bulk_input_file = open(args.input, "r") except: sys.stdout.write('error opening: '+args.input) return try: # open result file bulk_result_file = open(args.result, "w") except: sys.stdout.write('error opening: '+args.result) return try: # load credentials credentials = PIDClientCredentials.load_from_JSON(args.credpath) except CredentialsFormatError: sys.stdout.write('error') return except HandleSyntaxError: sys.stdout.write('error') return # retrieve and set extra values extra_config = {} try: # setup connection to handle server client = EUDATHandleClient.instantiate_with_credentials( credentials, **extra_config) except HandleNotFoundException: sys.stdout.write('error') return for line in bulk_input_file: bulk_array = line.split() if bulk_array[0] == 'SEARCH': # search key value # search handle which matches criteria search_key = bulk_array[1] search_value = bulk_array[2] result = search_execution(client, search_key, search_value) bulk_result_file.write('search handle key: '+search_key+' value: '+search_value+' result: '+result+'\n') if bulk_array[0] == 'READ': # READ handle # read whole handle # READ handle key # read key/value pair from handle read_handle = bulk_array[1] if len(bulk_array) >= 3: read_key = bulk_array[2] result = read_execution(client, read_handle, read_key) bulk_result_file.write('read handle: '+read_handle+' key: '+read_key+' result: '+result+'\n') else: result = read_execution(client, read_handle) bulk_result_file.write('read handle: '+read_handle+' result: '+result+'\n') if bulk_array[0] == 'CREATE': # CREATE prefix/uuid URL # create handle, use uuid for suffix # CREATE prefix/suffix URL # create handle, use suffix for handle, no check before if handle exists # CREATE prefix/uuid URL CHECKSUM # create handle, use uuid for suffix, add checksum # CREATE prefix/uuid URL CHECKSUM 10320/LOC # create handle, use uuid for suffix, add checksum, add 10320/LOC # CREATE prefix/uuid URL CHECKSUM 10320/LOC EXTRATYPE # create handle, use uuid for suffix, add checksum, add 10320/LOC, add extratypes overwrite=False checksum = None loc10320 = None extratype = None # create a handle to put. prefix = str(credentials.get_prefix()) create_prefix = bulk_array[1].split("/")[0] create_suffix = bulk_array[1].split("/")[1] if create_suffix == 'uuid': uid = uuid.uuid1() suffix = str(uid) handle = prefix+"/"+suffix else: handle = prefix+"/"+create_suffix overwrite=True if len(bulk_array) == 3: result = create_execution(client, handle, bulk_array[2], overwrite) else: if len(bulk_array) >= 4 and bulk_array[3].lower() != 'none': checksum = bulk_array[3] if len(bulk_array) >= 5 and bulk_array[4].lower() != 'none': loc10320 = bulk_array[4] if len(bulk_array) >= 6 and bulk_array[5].lower() != 'none': extratype = bulk_array[5] result = create_execution(client, handle, bulk_array[2], overwrite, checksum, loc10320, extratype) bulk_result_file.write('create handle: '+bulk_array[1]+' result: '+result+'\n') if bulk_array[0] == 'DELETE': # DELETE handle # delete whole handle # DELETE handle key # delete key/value pair from handle delete_handle = bulk_array[1] if len(bulk_array) >= 3: delete_key = bulk_array[2] result = delete_execution(client, delete_handle, delete_key) bulk_result_file.write('delete handle: '+delete_handle+' key: '+delete_key+' result: '+result+'\n') else: result = delete_execution(client, delete_handle) bulk_result_file.write('delete handle: '+delete_handle+' result: '+result+'\n') if bulk_array[0] == 'MODIFY': # MODIFY handle key value # modify key/value pair in handle if len(bulk_array) == 4: modify_handle = bulk_array[1] modify_key = bulk_array[2] modify_value = bulk_array[3] result = modify_execution(client, modify_handle, modify_key, modify_value) bulk_result_file.write('modify handle: '+modify_handle+' key: '+modify_key+' value: '+modify_value+' result: '+result+'\n') if bulk_array[0] == 'REPLACE': # REPLACE handle key data1 data2 # replace data1 with data2 in value part of key/value pair in handle if len(bulk_array) == 5: replace_handle = bulk_array[1] replace_key = bulk_array[2] replace_data1 = bulk_array[3] replace_data2 = bulk_array[4] result = read_execution(client, replace_handle, replace_key) if result != "None" and result != 'error': new_value = result.replace(replace_data1, replace_data2) if result != new_value: result = modify_execution(client, replace_handle, replace_key, new_value) else: result = "None" bulk_result_file.write('replace handle: '+replace_handle+' key: '+replace_key+' data1: '+replace_data1+' data2: '+replace_data2+' result: '+result+'\n') bulk_input_file.close() bulk_result_file.close()