Beispiel #1
0
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))
Beispiel #2
0
 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)
Beispiel #3
0
 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)
Beispiel #4
0
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))
Beispiel #5
0
 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)
Beispiel #6
0
 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)
Beispiel #7
0
 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
Beispiel #8
0
 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
Beispiel #9
0
 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)
Beispiel #11
0
    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
Beispiel #12
0
    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)
Beispiel #13
0
 def test_non_string_type_returns_none(self):
     self.assertIsNone(get_paramfile(100))
Beispiel #14
0
 def get_paramfile(self, path):
     return get_paramfile(path, REMOTE_PREFIX_MAP.copy())
Beispiel #15
0
 def get_paramfile(self, path):
     return get_paramfile(path, LOCAL_PREFIX_MAP.copy())
Beispiel #16
0
 def test_no_match_uris_returns_none(self):
     self.assertIsNone(get_paramfile('foobar://somewhere.bar'))
Beispiel #17
0
 def test_non_string_type_returns_none(self):
     self.assertIsNone(get_paramfile(100))
Beispiel #18
0
 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')
Beispiel #19
0
 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")
Beispiel #20
0
 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')
Beispiel #21
0
 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")
Beispiel #22
0
 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")
Beispiel #23
0
 def test_file_does_not_exist_raises_error(self):
     with self.assertRaises(ResourceLoadingError):
         get_paramfile('file://file/does/not/existsasdf.txt')
Beispiel #24
0
 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')
Beispiel #25
0
 def test_file_does_not_exist_raises_error(self):
     with self.assertRaises(ResourceLoadingError):
         get_paramfile("file://file/does/not/existsasdf.txt")
Beispiel #26
0
 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)
Beispiel #27
0
 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')
Beispiel #28
0
 def test_no_match_uris_returns_none(self):
     self.assertIsNone(get_paramfile("foobar://somewhere.bar"))
Beispiel #29
0
 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)