def add_transfer_syntax(self, syntax): """Append a transfer syntax to the presentation context. Parameters ---------- syntax : pydicom.uid.UID, bytes or str The transfer syntax to add to the presentation context. """ if isinstance(syntax, str): syntax = UID(syntax) elif isinstance(syntax, bytes): syntax = UID(syntax.decode('ascii')) else: LOGGER.error("Attempted to add an invalid transfer syntax") return if syntax is not None and not validate_uid(syntax): LOGGER.error("'transfer_syntax' contains an invalid UID") raise ValueError("'transfer_syntax' contains an invalid UID") if syntax and not syntax.is_valid: LOGGER.warning( "The Transfer Syntax Name '{}' is non-conformant".format( syntax)) # If the transfer syntax is rejected we may add an empty str if syntax not in self._transfer_syntax and syntax != '': if not syntax.is_valid: LOGGER.warning("A non-conformant UID has been added " "to 'transfer_syntax'") if not syntax.is_private and not syntax.is_transfer_syntax: LOGGER.warning("A UID has been added to 'transfer_syntax' " "that is not a transfer syntax") self._transfer_syntax.append(syntax)
def TransferSyntax(self, uid_list): """Set the Presentation Context's Transfer Syntax parameter. Parameters ---------- uid_list : list of str or bytes or pydicom.uid.UID The transfer syntax UIDs """ # pylint: disable=attribute-defined-outside-init self._transfer_syntax = [] if not isinstance(uid_list, list): raise TypeError("transfer_syntaxes must be a list.") for uid in uid_list: if isinstance(uid, bytes): uid = UID(uid.decode('utf-8')) elif isinstance(uid, UID): pass elif isinstance(uid, str): uid = UID(uid) else: raise ValueError("PresentationContext(): Invalid transfer " "syntax item") if not uid.is_valid: LOGGER.info('Presentation Context attempted to set an invalid ' 'transfer syntax UID') continue if uid.is_private: self._transfer_syntax.append(uid) elif uid.is_transfer_syntax: self._transfer_syntax.append(uid)
def transfer_syntax(self, syntaxes): """Set the presentation context's transfer syntaxes. Parameters ---------- syntaxes : list of (str or bytes or pydicom.uid.UID) The transfer syntax UIDs to add to the Presentation Context. """ if not isinstance(syntaxes, list): raise TypeError("'transfer_syntax' must be a list") self._transfer_syntax = [] for uid in syntaxes: if isinstance(uid, bytes): uid = UID(uid.decode('ascii')) elif isinstance(uid, str): uid = UID(uid) else: LOGGER.error("Attempted to add an invalid 'transfer_syntax'") continue if not uid.is_valid: LOGGER.warning("A non-conformant UID has been added " "to 'transfer_syntax'") self._transfer_syntax.append(uid)
def AffectedSOPClassUID(self, value): """ Sets the Affected SOP Class UID parameter Parameters ---------- value : pydicom.uid.UID, bytes or str The value for the Affected SOP Class UID """ if isinstance(value, UID): pass elif isinstance(value, str): value = UID(value) elif isinstance(value, bytes): value = UID(value.decode('utf-8')) elif value is None: pass else: raise TypeError("Affected SOP Class UID must be a " \ "pydicom.uid.UID, str or bytes") if value is not None: try: value.is_valid() except: logger.error("Affected SOP Class UID is an invalid UID") raise ValueError("Affected SOP Class UID is an invalid UID") self._affected_sop_class_uid = value
def add_transfer_syntax(self, transfer_syntax): """Append a transfer syntax to the Presentation Context. Parameters ---------- transfer_syntax : pydicom.uid.UID, bytes or str The transfer syntax to add to the Presentation Context. For Presentation contexts that are rejected the `transfer_syntax` may be an empty UID. """ # UID is a subclass of str if isinstance(transfer_syntax, str): transfer_syntax = UID(transfer_syntax) elif isinstance(transfer_syntax, bytes): transfer_syntax = UID(transfer_syntax.decode('utf-8')) else: raise TypeError('transfer_syntax must be a pydicom.uid.UID,' \ ' bytes or str') if transfer_syntax not in self.TransferSyntax and \ transfer_syntax != '': if not transfer_syntax.is_valid: raise ValueError('Presentation Context attempted to add a ' 'invalid UID') # Issue #62: private transfer syntaxes may be used if not transfer_syntax.is_private and \ not transfer_syntax.is_transfer_syntax: raise ValueError('Presentation Context attempted to add a ' 'non-transfer syntax UID') self.TransferSyntax.append(transfer_syntax)
def AbstractSyntax(self, uid): """Set the Presentation Context's Abstract Syntax parameter. Parameters ---------- uid : str or bytes or pydicom.uid.UID The abstract syntax UIDs """ # pylint: disable=attribute-defined-outside-init if uid is None: self._abstract_syntax = None return if isinstance(uid, bytes): uid = UID(uid.decode('utf-8')) elif isinstance(uid, UID): pass elif isinstance(uid, str): uid = UID(uid) else: raise TypeError("Presentation Context invalid type for abstract " "syntax") if not uid.is_valid: LOGGER.info('Presentation Context attempted to set an invalid ' 'abstract syntax UID') else: self._abstract_syntax = uid
def abstract_syntax(self, uid): """Set the presentation context's *Abstract Syntax*. Parameters ---------- uid : str or bytes or pydicom.uid.UID The abstract syntax UIDs """ if isinstance(uid, bytes): uid = UID(uid.decode('ascii')) elif isinstance(uid, str): uid = UID(uid) else: raise TypeError("'abstract_syntax' must be str or bytes or UID") if not validate_uid(uid): LOGGER.error("'abstract_syntax' is an invalid UID") raise ValueError("'abstract_syntax' is an invalid UID") if uid and not uid.is_valid: LOGGER.warning( "The Abstract Syntax Name '{}' is non-conformant".format(uid) ) self._abstract_syntax = uid
def TransferSyntax(self, value): """ `value` must be a list of pydicom.uid.UIDs, string UIDs or byte string UIDs """ self.__transfer_syntax = [] for ii in value: if isinstance(value, bytes): ii = UID(ii.decode("utf-8")) elif isinstance(ii, UID): pass elif isinstance(ii, str): ii = UID(ii) else: raise ValueError("PresentationContext(): Invalid transfer " "syntax item") self.__transfer_syntax.append(ii)
def AbstractSyntax(self, value): """ `value` must be a pydicom.uid.UID, a string UID or a byte string UID """ if isinstance(value, bytes): value = UID(value.decode("utf-8")) elif isinstance(value, UID): pass elif isinstance(value, str): value = UID(value) elif value is None: pass else: raise ValueError("PresentationContext(): Invalid abstract syntax") self.__abstract_syntax = value
def add_transfer_syntax(self, transfer_syntax): """ Parameters ---------- transfer_syntax - pydicom.uid.UID, bytes or str The transfer syntax to add to the Presentation Context """ if isinstance(transfer_syntax, str): transfer_syntax = UID(transfer_syntax) elif isinstance(transfer_syntax, bytes): transfer_syntax = UID(transfer_syntax.decode("utf-8")) else: raise ValueError("transfer_syntax must be a pydicom.uid.UID, bytes or str") if isinstance(transfer_syntax, UID): if transfer_syntax not in self.TransferSyntax: self.TransferSyntax.append(transfer_syntax)
def abstract_syntax(self, uid): """Set the presentation context's abstract syntax. Parameters ---------- uid : str or bytes or pydicom.uid.UID The abstract syntax UIDs """ if isinstance(uid, bytes): uid = UID(uid.decode('ascii')) elif isinstance(uid, str): uid = UID(uid) else: raise TypeError("'abstract_syntax' must be str or bytes or UID") if not uid.is_valid: LOGGER.warning("'abstract_syntax' set to a non-conformant UID") self._abstract_syntax = uid
def scp_supported_sop(self, sop_list): """ A valid SOP is either a str UID (ie '1.2.840.10008.1.1') or a valid pydicom.uid.UID object (UID.is_valid() shouldn't cause an exception) or a pynetdicom.SOPclass.ServiceClass subclass with a UID attribute(ie VerificationSOPClass) """ self._scp_supported_sop = [] try: for sop_class in sop_list: try: if isinstance(sop_class, str): sop_uid = UID(sop_class) elif isinstance(sop_class, UID): sop_uid = sop_class elif isinstance(sop_class, bytes): sop_uid = UID(sop_uid.decode('utf-8')) elif 'UID' in sop_class.__dict__.keys(): sop_uid = UID(sop_class.UID) else: raise ValueError("SCU SOP class must be a UID str, " "UID or ServiceClass subclass") sop_uid.is_valid() self._scp_supported_sop.append(sop_uid) except InvalidUID: raise ValueError("scp_sop_class must be a list of " "SOP Classes") except Exception as e: logger.warning("Invalid SCP SOP class '%s'" %sop_class) if sop_list != [] and self._scp_supported_sop == []: raise ValueError("No valid SCP SOP classes were supplied") except TypeError: raise ValueError("scp_sop_class must be a list") except: raise ValueError("scp_sop_class must be a list of SOP Classes")