11"""Utilities for DICOMweb URI manipulation."""
2- import dataclasses
32import enum
43import re
54from typing import Optional , Sequence , Tuple
@@ -26,23 +25,6 @@ class URISuffix(enum.Enum):
2625_MAX_UID_LENGTH = 64
2726_REGEX_UID = re .compile (r'[0-9]+([.][0-9]+)*' )
2827_REGEX_PERMISSIVE_UID = re .compile (r'[^/@]+' )
29- # Used for Project ID and Location validation in `GoogleCloudHealthcareURL`.
30- _REGEX_ID_1 = re .compile (r'[\w-]+' )
31- # Used for Dataset ID and DICOM Store ID validation in
32- # `GoogleCloudHealthcareURL`.
33- _REGEX_ID_2 = re .compile (r'[\w.-]+' )
34- # Regex for the DICOM Store suffix for the Google Cloud Healthcare API endpoint.
35- _STORE_REGEX = re .compile (
36- (r'projects/(%s)/locations/(%s)/datasets/(%s)/'
37- r'dicomStores/(%s)/dicomWeb$' ) % (_REGEX_ID_1 .pattern ,
38- _REGEX_ID_1 .pattern ,
39- _REGEX_ID_2 .pattern ,
40- _REGEX_ID_2 .pattern ))
41- # The URL for the Google Cloud Healthcare API endpoint.
42- _CHC_API_URL = 'https://healthcare.googleapis.com/v1'
43- # Cloud Healthcare validation error.
44- _CHC_API_ERROR_TMPL = ('`{attribute}` must match regex {regex}. Actual value: '
45- '{value!r}' )
4628
4729
4830class URI :
@@ -484,94 +466,6 @@ def from_string(cls,
484466 return uri
485467
486468
487- @dataclasses .dataclass (eq = True , frozen = True )
488- class GoogleCloudHealthcareURL :
489- """Base URL container for DICOM Stores under the `Google Cloud Healthcare API`_.
490-
491- This class facilitates the parsing and creation of :py:attr:`URI.base_url`
492- corresponding to DICOMweb API Service URLs under the v1_ API. The URLs are
493- of the form:
494- ``https://healthcare.googleapis.com/v1/projects/{project_id}/locations/{location}/datasets/{dataset_id}/dicomStores/{dicom_store_id}/dicomWeb``
495-
496- .. _Google Cloud Healthcare API: https://cloud.google.com/healthcare
497- .. _v1: https://cloud.google.com/healthcare/docs/how-tos/transition-guide
498-
499- Attributes:
500- project_id: str
501- The ID of the `GCP Project
502- <https://cloud.google.com/healthcare/docs/concepts/projects-datasets-data-stores#projects>`_
503- that contains the DICOM Store.
504- location: str
505- The `Region name
506- <https://cloud.google.com/healthcare/docs/concepts/regions>`_ of the
507- geographic location configured for the Dataset that contains the
508- DICOM Store.
509- dataset_id: str
510- The ID of the `Dataset
511- <https://cloud.google.com/healthcare/docs/concepts/projects-datasets-data-stores#datasets_and_data_stores>`_
512- that contains the DICOM Store.
513- dicom_store_id: str
514- The ID of the `DICOM Store
515- <https://cloud.google.com/healthcare/docs/concepts/dicom#dicom_stores>`_.
516- """
517- project_id : str
518- location : str
519- dataset_id : str
520- dicom_store_id : str
521-
522- def __post_init__ (self ) -> None :
523- """Performs input sanity checks."""
524- for regex , attribute , value in (
525- (_REGEX_ID_1 , 'project_id' , self .project_id ),
526- (_REGEX_ID_1 , 'location' , self .location ),
527- (_REGEX_ID_2 , 'dataset_id' , self .dataset_id ),
528- (_REGEX_ID_2 , 'dicom_store_id' , self .dicom_store_id )):
529- if regex .fullmatch (value ) is None :
530- raise ValueError (_CHC_API_ERROR_TMPL .format (
531- attribute = attribute , regex = regex , value = value ))
532-
533- def __str__ (self ) -> str :
534- """Returns a string URL for use as :py:attr:`URI.base_url`.
535-
536- See class docstring for the returned URL format.
537- """
538- return (f'{ _CHC_API_URL } /'
539- f'projects/{ self .project_id } /'
540- f'locations/{ self .location } /'
541- f'datasets/{ self .dataset_id } /'
542- f'dicomStores/{ self .dicom_store_id } /dicomWeb' )
543-
544- @classmethod
545- def from_string (cls , base_url : str ) -> 'GoogleCloudHealthcareURL' :
546- """Creates an instance from ``base_url``.
547-
548- Parameters
549- ----------
550- base_url: str
551- The URL for the DICOMweb API Service endpoint corresponding to a
552- `CHC API DICOM Store
553- <https://cloud.google.com/healthcare/docs/concepts/dicom#dicom_stores>`_.
554- See class docstring for supported formats.
555-
556- Raises
557- ------
558- ValueError
559- If ``base_url`` does not match the specifications in the class
560- docstring.
561- """
562- if not base_url .startswith (f'{ _CHC_API_URL } /' ):
563- raise ValueError ('Invalid CHC API v1 URL: {base_url!r}' )
564- resource_suffix = base_url [len (_CHC_API_URL ) + 1 :]
565-
566- store_match = _STORE_REGEX .match (resource_suffix )
567- if store_match is None :
568- raise ValueError (
569- 'Invalid CHC API v1 DICOM Store name: {resource_suffix!r}' )
570-
571- return cls (store_match .group (1 ), store_match .group (2 ),
572- store_match .group (3 ), store_match .group (4 ))
573-
574-
575469def _validate_base_url (url : str ) -> None :
576470 """Validates the Base (DICOMweb service) URL supplied to `URI`."""
577471 parse_result = urlparse .urlparse (url )
0 commit comments