def __init__()

in pathology/transformation_pipeline/ingestion_lib/dicom_gen/uid_generator.py [0:0]


  def __init__(self, seed: Optional[int] = None):
    random.seed(seed)  # Seed with system time or os methods

    # Internal counter for UID Allocation
    self._counter = random.randint(
        _MIN_UID_COUNTER_VALUE, _MAX_UID_COUNTER_VALUE
    )

    # Random component of UID. Initialized once.
    self._rand_fraction = ''

    # Length of last reported time string. Used to check if the time string
    # length changes. If string length changes we recompute the random
    # component of the UID to avoid exceeding the UID length limits.
    self._last_time_str_len: int = -1

    prefix = ingest_flags.DICOM_GUID_PREFIX_FLG.value.strip().rstrip('.')
    if not prefix:
      _log_and_raise('DICOM UID prefix flag is undefined.')
    # Validates UID prefix starts with DPAS UID
    if not prefix.startswith(ingest_const.DPAS_UID_PREFIX):
      _log_and_raise(
          'DICOM uid prefix flag must start with'
          f' "{ingest_const.DPAS_UID_PREFIX}". The prefix is defined as'
          f' "{prefix}"'
      )
    # Verify that UID defines no more than 7 characters after DPAS UID and that
    # the UID matchs DICOM UID formatting requirements.
    # https://dicom.nema.org/dicom/2013/output/chtml/part05/chapter_9.html
    uid_parts = prefix.split('.')
    base_dpas_uid_block_length = len(ingest_const.DPAS_UID_PREFIX.split('.'))
    customer_uid_len = len(uid_parts)
    for uid_block_index in range(base_dpas_uid_block_length, customer_uid_len):
      # Check that blocks conforms to DICOM spec requirements
      if not is_uid_block_correctly_formatted(uid_parts[uid_block_index]):
        _log_and_raise('DICOM UID suffix is incorrectly formatted.')
    # Not DICOM requirement, but one for DPAS, make sure total size of customer
    # block is small, no less than 7 characters to retain space for other
    # components of the UID.
    customer_ext = '.'.join(uid_parts[base_dpas_uid_block_length:])
    if len(customer_ext) > 7:
      _log_and_raise(
          'DICOM UID customer prefix following the DPAS UID must be <= 7 chars'
          f' in length. DPAS UID Prefix: {prefix}; '
          f'Customer prefix: {customer_ext} exceeds 7 chars.'
      )
    self._prefix = prefix

    # Representation of embedding of hostname in UID.
    hostname = cloud_logging_client.POD_HOSTNAME_FLG.value
    if hostname is None or not hostname:
      self._hostname_uid = ''
    else:
      parts = hostname.strip().split('-')
      hostname = parts[-1]
      hostname_hex = hostname.encode('utf-8').hex()
      self._hostname_uid = str(int(hostname_hex, 16))[
          :_MAX_HOSTNAME_COMPONENT_LENGTH
      ]
    self._init_random_fraction()