def _FromString()

in ez_wsi_dicomweb/ml_toolkit/dicom_path.py [0:0]


def _FromString(path_str: str) -> Path:
  """Parses the string and returns the Path object or raises ValueError if failed."""
  match_err_str = f'Error parsing the path. Path: {path_str}'
  prased_url = urllib.parse.urlparse(path_str)
  if not prased_url.scheme:
    base_address = _HEALTHCARE_API_URL
    healthcare_api_version = _DEFAULT_HEALTHCARE_API_VERSION
  else:
    # check if full url has been provided
    if prased_url.scheme.lower() not in ('http', 'https'):
      raise ValueError(match_err_str)
    if not prased_url.netloc:
      raise ValueError(match_err_str)
    base_address = f'{prased_url.scheme}://{prased_url.netloc}'
    path_str = prased_url.path
    if base_address.lower() != _HEALTHCARE_API_URL:
      healthcare_api_version = ''
    else:
      path_str = path_str.strip('/')
      path_str_parts = path_str.split('/')
      healthcare_api_version = path_str_parts[0]
      if not healthcare_api_version:
        raise ValueError(match_err_str)
      path_str = '/'.join(path_str_parts[1:])
  path_str = path_str.strip('/')
  is_healthcare_api_url = base_address.lower() == _HEALTHCARE_API_URL
  if is_healthcare_api_url:
    store_match = _MatchRegex(_REGEX_STORE, path_str, match_err_str)
    project_id = store_match.group(1)
    location = store_match.group(2)
    dataset_id = store_match.group(3)
    store_id = store_match.group(4)
    store_path_suffix = store_match.group(5)
    study_prefix = 'dicomWeb'
  else:
    project_id = ''
    location = ''
    dataset_id = ''
    store_id = ''
    study_prefix = ''
    store_path_suffix = path_str
  if not store_path_suffix:
    return Path(
        base_address,
        healthcare_api_version,
        project_id,
        location,
        dataset_id,
        store_id,
        study_prefix,
        '',
        '',
        '',
    )
  try:
    studies_match = _MatchRegex(
        _REGEX_STUDIES, store_path_suffix, match_err_str
    )
  except ValueError:
    store_path_suffix = store_path_suffix.strip().strip('/')
    if store_path_suffix:
      study_prefix = store_path_suffix
    if is_healthcare_api_url and study_prefix != 'dicomWeb':
      raise
    return Path(
        base_address,
        healthcare_api_version,
        project_id,
        location,
        dataset_id,
        store_id,
        study_prefix,
        '',
        '',
        '',
    )
  study_prefix = studies_match.group(2)
  if study_prefix is None:
    study_prefix = ''
  if study_prefix:
    study_prefix = study_prefix.strip('/')
  if is_healthcare_api_url and study_prefix != 'dicomWeb':
    raise ValueError(match_err_str)
  study_uid = studies_match.group(3)
  study_path_suffix = studies_match.group(4)

  if not study_path_suffix:
    return Path(
        base_address,
        healthcare_api_version,
        project_id,
        location,
        dataset_id,
        store_id,
        study_prefix,
        study_uid,
        '',
        '',
    )
  study_path_suffix = study_path_suffix.strip('/')
  series_match = _MatchRegex(_REGEX_SERIES, study_path_suffix, match_err_str)
  series_uid = series_match.group(1)
  series_path_suffix = series_match.group(2)
  series_path_suffix = series_path_suffix.strip('/')
  if not series_path_suffix:
    return Path(
        base_address,
        healthcare_api_version,
        project_id,
        location,
        dataset_id,
        store_id,
        study_prefix,
        study_uid,
        series_uid,
        '',
    )

  instance_match = _MatchRegex(
      _REGEX_INSTANCE, series_path_suffix, match_err_str
  )
  instance_uid = instance_match.group(1)

  return Path(
      base_address,
      healthcare_api_version,
      project_id,
      location,
      dataset_id,
      store_id,
      study_prefix,
      study_uid,
      series_uid,
      instance_uid,
  )