Exemple #1
0
def _get_incremental_transform(stub, shell, session_id, wstl_args, cell):
    """Invokes, throughs a gRPC request, an incremental Whistle transform.

  Args:
    stub: gRPC client stub library.
    shell: an instance of the iPython shell that invoked the magic command.
    session_id: the incremental transformation session id.
    wstl_args: the arguments to the wstl magic command.
    cell: the contents of the cell, containing whistle.

  Returns:
    TransformResponse or a grpc.RpcError
  """
    req = wstlservice_pb2.IncrementalTransformRequest()
    req.session_id = session_id
    req.wstl = cell
    if wstl_args.library_config:
        library_configs = _location.parse_location(
            shell,
            wstl_args.library_config,
            file_ext=_constants.WSTL_FILE_EXT,
            load_contents=False)
        if library_configs:
            req.library_config.extend(library_configs)

    if wstl_args.code_config:
        code_configs = _location.parse_location(
            shell,
            wstl_args.code_config,
            file_ext=_constants.JSON_FILE_EXT,
            load_contents=False)
        if code_configs:
            req.code_config.extend(code_configs)

    if wstl_args.unit_config:
        unit_config = _location.parse_location(
            shell,
            wstl_args.unit_config,
            file_ext=_constants.TEXTPROTO_FILE_EXT,
            load_contents=False)
        if unit_config:
            req.unit_config = unit_config[0]

    if wstl_args.input:
        inputs = _location.parse_location(shell,
                                          wstl_args.input,
                                          file_ext=_constants.JSON_FILE_EXT,
                                          load_contents=True)
        if inputs:
            req.input.extend(inputs)
        else:
            return None, "no inputs matching arguement {}".format(
                wstl_args.input)

    try:
        resp = stub.GetIncrementalTransform(req, timeout=_GRPC_TIMEOUT)
    except grpc.RpcError as rpc_error:
        return None, rpc_error
    else:
        return resp, None
    def test_parse_location_gs_prefix_wildcard_unsupported_ext(
            self, mock_bucket, mock_client):
        class Item(object):
            def __init__(self, bucket, name):
                self.bucket = bucket
                self.name = name

        class FakeBucket(object):
            def __init__(self, bucket_name):
                self.name = bucket_name

        bucket = FakeBucket("dummy_bucket")
        items = [
            Item(bucket, "file1.txt"),
            Item(bucket, "lib_folder/file2.wstl"),
            Item(bucket, "lib_folder/file3.wstl"),
            Item(bucket, "lib_folder/file4.json"),
            Item(bucket, "input.json")
        ]
        mock_bucket.list_blobs.return_value = iter(items)
        mock_client.return_value.get_bucket.return_value = mock_bucket

        shell = mock.MagicMock()
        input_wstl_arg = "gs://dummy_bucket/*.txt"
        locations = _location.parse_location(shell,
                                             input_wstl_arg,
                                             file_ext=_constants.WSTL_FILE_EXT,
                                             load_contents=False)
        self.assertEmpty(locations)
Exemple #3
0
def _get_validation(stub, shell, version, input_arg):
    """Validates the input JSON resource(s) against the FHIR version.

  Args:
    stub: gRPC client stub library.
    shell: an instance of the iPython shell that invoked the magic command.
    version: the FHIR version to be used for validation.
    input_arg: the FHIR resource to be validated against the specified version.

  Returns:
    The ValidationResponse containing the validation results of the resources
    or an error.

  """
    req = wstlservice_pb2.ValidationRequest(
        input=_location.parse_location(shell, input_arg))
    if version.lower() == "r4":
        req.fhir_version = wstlservice_pb2.ValidationRequest.FhirVersion.R4
    elif version.lower() == "stu3":
        req.fhir_version = wstlservice_pb2.ValidationRequest.FhirVersion.STU3
    else:
        return None, ValueError(
            """FHIR version {} is incorrect or not supported,
{} are supported versions""".format(
                version, wstlservice_pb2.ValidationRequest.FhirVersion.keys()))
    try:
        resp = stub.FhirValidate(req)
    except grpc.RpcError as rpc_error:
        return None, rpc_error
    return resp, None
    def test_parse_location_gs_prefix_success(self, mock_bucket, mock_client):
        class Item(object):
            def __init__(self, bucket_name, name):
                self.bucket = bucket_name
                self.name = name

        class FakeBucket(object):
            def __init__(self, bucket_name):
                self.name = bucket_name

        bucket = FakeBucket("fake_bucket")
        items = [
            Item(bucket, "file1.wstl"),
            Item(bucket, "lib_folder/file2.wstl"),
            Item(bucket, "lib_folder/file3.txt"),
            Item(bucket, "input.json")
        ]

        mock_bucket.list_blobs.return_value = iter(items)
        mock_client.return_value.get_bucket.return_value = mock_bucket

        shell = mock.MagicMock()
        input_wstl_arg = "gs://fake_bucket/input.json"
        locations = _location.parse_location(shell,
                                             input_wstl_arg,
                                             file_ext=_constants.JSON_FILE_EXT)
        self.assertLen(locations, 1)
        self.assertTrue(locations[0].HasField("gcs_location"))
        self.assertEqual(locations[0].gcs_location, input_wstl_arg)
    def test_parse_location_gs_prefix_wildcard_success(self, mock_bucket,
                                                       mock_client):
        class Item(object):
            def __init__(self, bucket, name):
                self.bucket = bucket
                self.name = name

        class FakeBucket(object):
            def __init__(self, bucket_name):
                self.name = bucket_name

        bucket = FakeBucket("dummy_bucket")
        items = [
            Item(bucket, "file1.txt"),
            Item(bucket, "lib_folder/file2.wstl"),
            Item(bucket, "lib_folder/file3.wstl"),
            Item(bucket, "lib_folder/file4.json"),
            Item(bucket, "input.json")
        ]
        mock_bucket.list_blobs.return_value = iter(items)
        mock_client.return_value.get_bucket.return_value = mock_bucket

        shell = mock.MagicMock()
        input_wstl_arg = "gs://dummy_bucket/lib_folder/*"
        locations = _location.parse_location(shell,
                                             input_wstl_arg,
                                             file_ext=_constants.WSTL_FILE_EXT,
                                             load_contents=False)
        self.assertLen(locations, 2)
        self.assertTrue(locations[0].HasField("gcs_location"))
        self.assertEqual(locations[0].gcs_location,
                         "gs://dummy_bucket/lib_folder/file2.wstl")
        self.assertTrue(locations[1].HasField("gcs_location"))
        self.assertEqual(locations[1].gcs_location,
                         "gs://dummy_bucket/lib_folder/file3.wstl")
 def test_parse_location_file_prefix_invalid_path(self):
     shell = mock.MagicMock()
     content = """{"hello": "world"}"""
     tmp_file = self.create_tempfile(content=content, mode="w")
     input_wstl_arg = "file://invalid-{}".format(tmp_file.full_path)
     locations = _location.parse_location(shell,
                                          input_wstl_arg,
                                          file_ext=_constants.JSON_FILE_EXT)
     self.assertEmpty(locations)
 def test_parse_location_json_prefix_object_success(self):
     shell = mock.MagicMock()
     input_wstl_arg = """json://{"hello":"world"}"""
     locations = _location.parse_location(shell,
                                          input_wstl_arg,
                                          file_ext=None)
     self.assertLen(locations, 1)
     self.assertTrue(locations[0].HasField("inline_json"))
     self.assertEqual(locations[0].inline_json, "{\"hello\":\"world\"}")
 def test_parse_location_json_prefix_list_success(self):
     shell = mock.MagicMock()
     input_wstl_arg = """json://[{"first": "world"},{"second": "world"}]"""
     locations = _location.parse_location(shell,
                                          input_wstl_arg,
                                          file_ext=None)
     self.assertLen(locations, 1)
     self.assertTrue(locations[0].HasField("inline_json"))
     self.assertEqual(locations[0].inline_json,
                      """[{"first": "world"},{"second": "world"}]""")
 def test_parse_location_python_prefix_string_success(self):
     str_content = """{"hello": "world"}"""
     _ip.push("str_content")
     input_wstl_arg = "py://str_content"
     locations = _location.parse_location(_ip,
                                          input_wstl_arg,
                                          file_ext=None)
     self.assertLen(locations, 1)
     self.assertTrue(locations[0].HasField("inline_json"))
     self.assertEqual(locations[0].inline_json, str_content)
 def test_parse_location_python_prefix_dict_success(self):
     dict_content = {"hello": "world"}
     _ip.push("dict_content")
     input_wstl_arg = "py://dict_content"
     locations = _location.parse_location(_ip,
                                          input_wstl_arg,
                                          file_ext=None)
     self.assertLen(locations, 1)
     self.assertTrue(locations[0].HasField("inline_json"))
     self.assertEqual(locations[0].inline_json, json.dumps(dict_content))
 def test_parse_location_python_prefix_list_success(self):
     list_content = [{"first": "item"}, {"second": "item"}]
     _ip.push("list_content")
     input_wstl_arg = "py://list_content"
     locations = _location.parse_location(_ip,
                                          input_wstl_arg,
                                          file_ext=None)
     self.assertLen(locations, 1)
     self.assertTrue(locations[0].HasField("inline_json"))
     self.assertEqual(locations[0].inline_json,
                      json.dumps(list_content, sort_keys=True))
 def test_parse_location_file_prefix_file_exists_success(self):
     shell = mock.MagicMock()
     content = """{"hello": "world"}"""
     tmp_file = self.create_tempfile(file_path="dummy.json",
                                     content=content,
                                     mode="w")
     input_wstl_arg = "file://{}".format(tmp_file.full_path)
     locations = _location.parse_location(shell,
                                          input_wstl_arg,
                                          file_ext=_constants.JSON_FILE_EXT)
     self.assertTrue(locations[0].HasField("inline_json"))
     self.assertEqual(locations[0].inline_json, content)
 def test_parse_location_file_prefix_wstl_suffix_success(self):
     shell = mock.MagicMock()
     content = """Result: $ToUpper("a")"""
     tmp_file = self.create_tempfile(file_path="dummy.wstl",
                                     content=content,
                                     mode="w")
     input_wstl_arg = "file://{}".format(tmp_file.full_path)
     locations = _location.parse_location(shell,
                                          input_wstl_arg,
                                          file_ext=_constants.WSTL_FILE_EXT,
                                          load_contents=False)
     self.assertTrue(locations[0].HasField("local_path"))
     self.assertEqual(locations[0].local_path, tmp_file.full_path)
 def test_parse_location_file_prefix_textproto_suffix_success(self):
     shell = mock.MagicMock()
     content = """fake_field: true"""
     tmp_file = self.create_tempfile(file_path="fake.textproto",
                                     content=content,
                                     mode="w")
     input_wstl_arg = "file://{}".format(tmp_file.full_path)
     locations = _location.parse_location(
         shell,
         input_wstl_arg,
         file_ext=_constants.TEXTPROTO_FILE_EXT,
         load_contents=False)
     self.assertTrue(locations[0].HasField("local_path"))
     self.assertEqual(locations[0].local_path, tmp_file.full_path)
 def test_parse_location_file_prefix_no_load_content_success(self):
     shell = mock.MagicMock()
     content = """{"hello": "world"}"""
     tmp_file = self.create_tempfile(file_path="dummy.json",
                                     content=content,
                                     mode="w")
     input_wstl_arg = "file://{}/*".format(path.dirname(tmp_file.full_path))
     locations = _location.parse_location(shell,
                                          input_wstl_arg,
                                          file_ext=_constants.JSON_FILE_EXT,
                                          load_contents=False)
     self.assertLen(locations, 1)
     self.assertTrue(locations[0].HasField("local_path"))
     self.assertEqual(locations[0].local_path, tmp_file.full_path)
 def test_parse_location_file_suffix_ndjson_success(self):
     shell = mock.MagicMock()
     content = """{"first": "item"}\n{"second": "item"}"""
     tmp_file = self.create_tempfile(file_path="dummy.ndjson",
                                     content=content,
                                     mode="w")
     input_wstl_arg = "file://{}".format(tmp_file.full_path)
     locations = _location.parse_location(shell,
                                          input_wstl_arg,
                                          file_ext=_constants.JSON_FILE_EXT)
     self.assertLen(locations, 2)
     self.assertTrue(locations[0].HasField("inline_json"))
     self.assertEqual(locations[0].inline_json, "{\"first\": \"item\"}")
     self.assertTrue(locations[1].HasField("inline_json"))
     self.assertEqual(locations[1].inline_json, "{\"second\": \"item\"}")
 def test_parse_location_unknown_prefix_failure(self):
     shell = mock.MagicMock()
     input_wstl_arg = "invalid://blah"
     with self.assertRaises(ValueError):
         _location.parse_location(shell, input_wstl_arg, file_ext=None)
 def test_parse_location_file_prefix_missing_extension(self):
     shell = mock.MagicMock()
     input_wstl_arg = "file://placeholder.json"
     with self.assertRaises(ValueError):
         _location.parse_location(shell, input_wstl_arg, file_ext=None)