def _check_for_uri_param(param, value): if isinstance(value, list) and len(value) == 1: value = value[0] try: return get_paramfile(value) except ResourceLoadingError as e: raise ParamError(param.cli_name, six.text_type(e))
def test_text_file(self): contents = 'This is a test' filename = self.files.create_file('foo', contents) prefixed_filename = 'file://' + filename data = get_paramfile(prefixed_filename) self.assertEqual(data, contents) self.assertIsInstance(data, six.string_types)
def test_binary_file(self): contents = "This is a test" filename = self.files.create_file("foo", contents) prefixed_filename = "fileb://" + filename data = get_paramfile(prefixed_filename) self.assertEqual(data, b"This is a test") self.assertIsInstance(data, six.binary_type)
def _check_for_uri_param(param, value): if isinstance(value, list) and len(value) == 1: value = value[0] try: return get_paramfile(value) except ResourceLoadingError as e: raise ParamError(param, str(e))
def test_text_file(self): contents = "This is a test" filename = self.files.create_file("foo", contents) prefixed_filename = "file://" + filename data = get_paramfile(prefixed_filename) self.assertEqual(data, contents) self.assertIsInstance(data, six.string_types)
def test_binary_file(self): contents = 'This is a test' filename = self.files.create_file('foo', contents) prefixed_filename = 'fileb://' + filename data = get_paramfile(prefixed_filename) self.assertEqual(data, b'This is a test') self.assertIsInstance(data, six.binary_type)
def _handle_param_file(self, value): session = self.operation_object.service.session if isinstance(value, list) and len(value) == 1: temp = value[0] else: temp = value temp = get_paramfile(session, temp) if temp: value = temp return value
def _handle_param_file(self, value): session = self.operation_object.service.session # If the arg is suppose to be a list type, just # get the first element in the list, as it may # refer to a file:// (or http/https) type. potential_param_value = value if isinstance(value, list) and len(value) == 1: potential_param_value = value[0] try: actual_value = get_paramfile(session, potential_param_value) except ResourceLoadingError as e: raise BadArgumentError("Bad value for argument '%s': %s" % (self.cli_name, e)) if actual_value is not None: value = actual_value return value
def _handle_param_file(self, value): session = self.operation_object.service.session # If the arg is suppose to be a list type, just # get the first element in the list, as it may # refer to a file:// (or http/https) type. potential_param_value = value if isinstance(value, list) and len(value) == 1: potential_param_value = value[0] try: actual_value = get_paramfile(session, potential_param_value) except ResourceLoadingError as e: raise BadArgumentError("Bad value for argument '%s': %s" % (self.cli_name, e)) if actual_value is not None: value = actual_value return value
def add_to_call_parameters(self, call_parameters, parsed_args, parsed_globals, **kwargs): # Check if ``--cli-input-json`` was specified in the command line. input_json = getattr(parsed_args, "cli_input_json", None) if input_json is not None: # Retrieve the JSON from the file if needed. retrieved_json = get_paramfile(input_json) # Nothing was retrieved from the file. So assume the argument # is already a JSON string. if retrieved_json is None: retrieved_json = input_json try: # Try to load the JSON string into a python dictionary input_data = json.loads(retrieved_json) except ValueError as e: raise ParamError(self.name, "Invalid JSON: %s\nJSON received: %s" % (e, retrieved_json)) # Add the members from the input JSON to the call parameters. self._update_call_parameters(call_parameters, input_data)
def _get_arg_value(self, parsed_args): arg_value = getattr(parsed_args, self.py_name, None) if arg_value is None: return cli_input_args = [ k for k, v in vars(parsed_args).items() if v is not None and k.startswith('cli_input') ] if len(cli_input_args) != 1: raise ParamSyntaxError( 'Only one --cli-input- parameter may be specified.') # If the value starts with file:// or fileb://, return the contents # from the file. paramfile_data = get_paramfile(arg_value, LOCAL_PREFIX_MAP) if paramfile_data is not None: return paramfile_data return arg_value
def add_to_call_parameters(self, call_parameters, parsed_args, parsed_globals, **kwargs): # Check if ``--cli-input-json`` was specified in the command line. input_json = getattr(parsed_args, 'cli_input_json', None) if input_json is not None: # Retrieve the JSON from the file if needed. retrieved_json = get_paramfile(input_json, LOCAL_PREFIX_MAP) # Nothing was retrieved from the file. So assume the argument # is already a JSON string. if retrieved_json is None: retrieved_json = input_json try: # Try to load the JSON string into a python dictionary input_data = json.loads(retrieved_json) except ValueError as e: raise ParamError( self.name, "Invalid JSON: %s\nJSON received: %s" % (e, retrieved_json)) # Add the members from the input JSON to the call parameters. self._update_call_parameters(call_parameters, input_data)
def test_non_string_type_returns_none(self): self.assertIsNone(get_paramfile(100))
def get_paramfile(self, path): return get_paramfile(path, REMOTE_PREFIX_MAP.copy())
def get_paramfile(self, path): return get_paramfile(path, LOCAL_PREFIX_MAP.copy())
def test_no_match_uris_returns_none(self): self.assertIsNone(get_paramfile('foobar://somewhere.bar'))
def test_non_string_type_returns_none(self): self.assertIsNone(get_paramfile(100))
def test_resource_from_http(self): self.response.text = 'http contents' loaded = get_paramfile('http://foo.bar.baz') self.assertEqual(loaded, 'http contents') self.requests_mock.get.assert_called_with('http://foo.bar.baz')
def test_connection_error_raises_error(self): self.requests_mock.get.side_effect = Exception("Connection error.") with self.assertRaisesRegexp(ResourceLoadingError, "foo\.bar\.baz"): get_paramfile("https://foo.bar.baz")
def test_resource_from_https(self): self.response.text = 'http contents' loaded = get_paramfile('https://foo.bar.baz') self.assertEqual(loaded, 'http contents') self.requests_mock.get.assert_called_with('https://foo.bar.baz')
def test_resource_from_https(self): self.response.text = "http contents" loaded = get_paramfile("https://foo.bar.baz") self.assertEqual(loaded, "http contents") self.requests_mock.get.assert_called_with("https://foo.bar.baz")
def test_non_200_raises_error(self): self.response.status_code = 500 with self.assertRaisesRegexp(ResourceLoadingError, "foo\.bar\.baz"): get_paramfile("https://foo.bar.baz")
def test_file_does_not_exist_raises_error(self): with self.assertRaises(ResourceLoadingError): get_paramfile('file://file/does/not/existsasdf.txt')
def test_non_200_raises_error(self): self.response.status_code = 500 with self.assertRaisesRegexp(ResourceLoadingError, 'foo\.bar\.baz'): get_paramfile('https://foo.bar.baz')
def test_file_does_not_exist_raises_error(self): with self.assertRaises(ResourceLoadingError): get_paramfile("file://file/does/not/existsasdf.txt")
def test_cannot_load_text_file(self): contents = b"\xbfX\xac\xbe" filename = self.files.create_file("foo", contents, mode="wb") prefixed_filename = "file://" + filename with self.assertRaises(ResourceLoadingError): get_paramfile(prefixed_filename)
def test_connection_error_raises_error(self): self.requests_mock.get.side_effect = Exception("Connection error.") with self.assertRaisesRegexp(ResourceLoadingError, 'foo\.bar\.baz'): get_paramfile('https://foo.bar.baz')
def test_no_match_uris_returns_none(self): self.assertIsNone(get_paramfile("foobar://somewhere.bar"))
def test_cannot_load_text_file(self): contents = b'\xbfX\xac\xbe' filename = self.files.create_file('foo', contents, mode='wb') prefixed_filename = 'file://' + filename with self.assertRaises(ResourceLoadingError): get_paramfile(prefixed_filename)