def write_text( fp: DicomIO, elem: DataElement, encodings: Optional[List[str]] = None ) -> None: """Write a single or multivalued text string.""" encodings = encodings or [default_encoding] val = elem.value if val is not None: if _is_multi_value(val): val = cast(Union[Sequence[bytes], Sequence[str]], val) if isinstance(val[0], str): val = cast(Sequence[str], val) val = b'\\'.join( [encode_string(val, encodings) for val in val] ) else: val = cast(Sequence[bytes], val) val = b'\\'.join([val for val in val]) else: val = cast(Union[bytes, str], val) if isinstance(val, str): val = encode_string(val, encodings) if len(val) % 2 != 0: val = val + b' ' # pad to even length fp.write(val)
def _encode_personname(components, encodings): """Encode a list of text string person name components. Parameters ---------- components : list of text type The list of the up to three unicode person name components encodings : list of str The Python encodings uses to encode `components`. Returns ------- byte string The byte string that can be written as a PN DICOM tag value. If the encoding of some component parts is not possible using the given encodings, they are encoded with the first encoding using replacement bytes for characters that cannot be encoded. """ from pydicom.charset import encode_string encoded_comps = [] for comp in components: groups = [encode_string(group, encodings) for group in comp.split('^')] encoded_comps.append(b'^'.join(groups)) # Remove empty elements from the end while len(encoded_comps) and not encoded_comps[-1]: encoded_comps.pop() return b'='.join(encoded_comps)
def write_text(fp, data_element, encodings=None): """Write a single or multivalued text string.""" val = data_element.value if val is not None: encodings = encodings or [default_encoding] if _is_multi_value(val): if val and isinstance(val[0], compat.text_type): val = b'\\'.join( [encode_string(val, encodings) for val in val]) else: val = b'\\'.join([val for val in val]) else: if isinstance(val, compat.text_type): val = encode_string(val, encodings) if len(val) % 2 != 0: val = val + b' ' # pad to even length fp.write(val)
def write_text(fp, data_element, encodings=None): """Write a single or multivalued text string.""" val = data_element.value if val is not None: encodings = encodings or [default_encoding] if _is_multi_value(val): if val and isinstance(val[0], compat.text_type): val = b'\\'.join([encode_string(val, encodings) for val in val]) else: val = b'\\'.join([val for val in val]) else: if isinstance(val, compat.text_type): val = encode_string(val, encodings) if len(val) % 2 != 0: val = val + b' ' # pad to even length fp.write(val)
def enc(s: str) -> bytes: return encode_string(s, encodings or [default_encoding])
def _encode_and_validate_string(vr: str, value: str, encodings: Sequence[str]) -> bytes: encoded = encode_string(value, encodings) validate_value(vr, encoded, config.settings.writing_validation_mode) return encoded