def FromString()

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


def FromString(path_str: str, path_type: Optional[Type] = None) -> Path:
  """Parses the string and returns the Path object or raises ValueError if failed.

  Args:
    path_str: The string containing the path.
    path_type: The expected type of the path or None if no specific type is
      expected.

  Returns:
    The newly constructed Path object.
  Raises:
    ValueError if the path cannot be parsed or the actual path type doesn't
      match the specified expected type.
  """
  path = _FromString(path_str)

  # Validate that the path is of the right type of the type is specified.
  if path_type is not None and path.type != path_type:
    raise ValueError(
        f'Unexpected path type. Expected: {path_type}, actual: {path.type}.'
        f' Path: {path_str}'
    )

  return path