Beispiel #1
0
def _decode_personname(components, encodings):
    """Return a list of decoded person name components.

    Parameters
    ----------
    components : list of byte string
        The list of the up to three encoded person name components
    encodings : list of str
        The Python encodings uses to decode `components`.

    Returns
    -------
    text type
        The unicode string representing the person name.
        If the decoding of some component parts is not possible using the
        given encodings, they are decoded with the first encoding using
        replacement characters for bytes that cannot be decoded.
    """
    from pydicom.charset import decode_bytes

    if isinstance(components[0], str):
        comps = components
    else:
        comps = [
            decode_bytes(comp, encodings, PN_DELIMS) for comp in components
        ]
    # Remove empty elements from the end to avoid trailing '='
    while len(comps) and not comps[-1]:
        comps.pop()
    return tuple(comps)
Beispiel #2
0
def convert_single_string(
    byte_string: bytes,
    encodings: Optional[List[str]] = None,
    vr: str = None,
) -> str:
    """Return decoded text, ignoring backslashes and trailing spaces.

    Parameters
    ----------
    byte_string : bytes
        The encoded string.
    encodings : list of str, optional
        A list of the character encoding schemes used to encode the text.
    vr : str
        The value representation of the element. Needed for validation.

    Returns
    -------
    str
        The decoded text.
    """
    if vr is not None:
        validate_value(vr, byte_string,
                       config.settings.reading_validation_mode)
    encodings = encodings or [default_encoding]
    value = decode_bytes(byte_string, encodings, TEXT_VR_DELIMS)
    return value.rstrip('\0 ')
Beispiel #3
0
def convert_single_string(byte_string: bytes,
                          encodings: Optional[List[str]] = None) -> str:
    """Return decoded text, ignoring backslashes and trailing spaces.

    Parameters
    ----------
    byte_string : bytes
        The encoded string.
    encodings : list of str, optional
        A list of the character encoding schemes used to encode the text.

    Returns
    -------
    str
        The decoded text.
    """
    encodings = encodings or [default_encoding]
    value = decode_bytes(byte_string, encodings, TEXT_VR_DELIMS)
    return value.rstrip('\0 ')
Beispiel #4
0
 def dec(s: bytes) -> str:
     return decode_bytes(s, encodings or [default_encoding], [])