def ParseMessage(message: pubsub_v1.types.PubsubMessage, path_type: dicom_path.Type) -> ParsedMessage: """Parses input Pub/Sub message into a ParsedMessage object. Args: message: Pub/Sub message to be parsed. Expected to contain a DICOMweb path starting from "projects/" and down to the level of a Study UID, Series UID, or Instance UID. path_type: indicates the expected type of the DICOM resource the path in the message points to. Returns: ParsedMessage object representing parsed Pub/Sub message data. Raises: exception.CustomExceptionError with status code INVALID_ARGUMENT if the input doesn't match expected format. """ input_path_str = message.data.decode() # Support both 'True' and 'true' for user convenience with manual invocation. test_attr = (message.attributes.get('test') in ['True', 'true']) conflict_attr = message.attributes.get('conflict') if conflict_attr and conflict_attr not in CONFLICT_MAP: raise exception.CustomExceptionError( 'Unexpected value for conflict attribute: %s. Must be one of the ' 'following values: %s' % (conflict_attr, CONFLICT_MAP.keys()), code_pb2.Code.INVALID_ARGUMENT) conflict = CONFLICT_MAP[ conflict_attr] if conflict_attr else DEFAULT_CONFLICT try: input_path = dicom_path.FromString(input_path_str, path_type) parsed_message = ParsedMessage(input_path=input_path, conflict=conflict, test=test_attr) # Set the output DICOM store path, if available. output_store_path_str = message.attributes.get( 'output_dicom_store_path') if output_store_path_str is not None: output_store_path = dicom_path.FromString(output_store_path_str, dicom_path.Type.STORE) parsed_message.output_dicom_store_path = output_store_path return parsed_message except ValueError as e: traceback.print_exc() raise exception.CustomExceptionError(str(e), code_pb2.Code.INVALID_ARGUMENT)
def testStorePath(self): """Store path is parsed correctly and behaves as expected.""" store_path = dicom_path.FromString(tdpu.STORE_PATH_STR) self._AssertStoreAttributes(store_path) self.assertIsNone(store_path.study_uid) self.assertIsNone(store_path.series_uid) self.assertIsNone(store_path.instance_uid) self.assertEqual(store_path.type, dicom_path.Type.STORE) self.assertEqual(store_path.dicomweb_path_str, tdpu.DICOMWEB_PATH_STR) self.assertEqual(str(store_path), tdpu.STORE_PATH_STR) self.assertEqual(str(store_path.GetStorePath()), tdpu.STORE_PATH_STR)
def testInstancePath(self): """Instance path is parsed correctly and behaves as expected.""" instance_path = dicom_path.FromString(tdpu.INSTANCE_PATH_STR) self._AssertStoreAttributes(instance_path) self.assertEqual(instance_path.study_uid, tdpu.STUDY_UID) self.assertEqual(instance_path.series_uid, tdpu.SERIES_UID) self.assertEqual(instance_path.instance_uid, tdpu.INSTANCE_UID) self.assertEqual(instance_path.type, dicom_path.Type.INSTANCE) self.assertEqual(instance_path.dicomweb_path_str, tdpu.DICOMWEB_PATH_STR) self.assertEqual(str(instance_path), tdpu.INSTANCE_PATH_STR) self.assertEqual(str(instance_path.GetStorePath()), tdpu.STORE_PATH_STR) self.assertEqual(str(instance_path.GetStudyPath()), tdpu.STUDY_PATH_STR) self.assertEqual(str(instance_path.GetSeriesPath()), tdpu.SERIES_PATH_STR)
def __init__(self, predictor, dicom_store_path): """Inits PubsubMessageHandler with args. Args: predictor: Object used to get prediction results. dicom_store_path: DICOM store used to store inference results. """ self._predictor = predictor # May raise exception if the path is invalid. if dicom_store_path: self._dicom_store_path = dicom_path.FromString( dicom_store_path, dicom_path.Type.STORE) else: self._dicom_store_path = None self._success_count = 0 self.publisher = pubsub_v1.PublisherClient()
def testPathToUrl(self): dicom_path_str = tdpu.STUDY_PATH_STR url = dicom_web.PathToUrl(dicom_path.FromString(dicom_path_str)) expected_url = posixpath.join(dicom_web.CLOUD_HEALTHCARE_API_URL, dicom_path_str) self.assertEqual(url, expected_url)