Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
# This greatly simplifies the write process so all objects have the
# "encode" method
return self
def family_comma_given(self):
"""Return name as 'Family-name, Given-name'"""
return self.formatted("%(family_name)s, %(given_name)s")
# def __str__(self):
# return str(self.byte_string)
# XXX need to process the ideographic or phonetic components?
# def __len__(self):
# return len(self.byte_string)
class PersonNameUnicode(PersonNameBase, compat.text_type):
"""Unicode version of Person Name"""
def __new__(cls, val, encodings):
"""Return unicode string after conversion of each part
Parameters
----------
val : bytes or str
The PN value to store
encodings : list of str
A list of python encodings, generally found from
``pydicom.charset.python_encodings`` mapping of values in DICOM
data element (0008,0005) *Specific Character Set*.
"""
encodings = _verify_encodings(encodings)
comps = _decode_personname(val.split(b"="), encodings)
element_struct = Struct(endian_chr + "HH2sH")
extra_length_struct = Struct(endian_chr + "L") # for special VRs
extra_length_unpack = extra_length_struct.unpack # for lookup speed
# Make local variables so have faster lookup
fp_read = fp.read
fp_tell = fp.tell
logger_debug = logger.debug
debugging = config.debugging
element_struct_unpack = element_struct.unpack
defer_size = size_in_bytes(defer_size)
tag_set = set()
if specific_tags is not None:
for tag in specific_tags:
if isinstance(tag, (str, compat.text_type)):
tag = Tag(tag_for_keyword(tag))
if isinstance(tag, BaseTag):
tag_set.add(tag)
tag_set.add(Tag(0x08, 0x05))
has_tag_set = len(tag_set) > 0
while True:
# Read tag, VR, length, get ready to read value
bytes_read = fp_read(8)
if len(bytes_read) < 8:
return # at end of file
if debugging:
debug_msg = "{0:08x}: {1}".format(fp.tell() - 8,
bytes2hex(bytes_read))
if is_implicit_VR:
def recurse_tree(tree, dataset, parent, hide=False):
# order the dicom tags
for data_element in dataset:
node_id = parent + "." + hex(id(data_element))
if isinstance(data_element.value, compat.text_type):
tree.hlist.add(node_id, text=compat.text_type(data_element))
else:
tree.hlist.add(node_id, text=str(data_element))
if hide:
tree.hlist.hide_entry(node_id)
if data_element.VR == "SQ": # a sequence
for i, dataset in enumerate(data_element.value):
item_id = node_id + "." + str(i + 1)
sq_item_description = data_element.name.replace(
" Sequence", "") # XXX not i18n
item_text = "{0:s} {1:d}".format(sq_item_description, i + 1)
tree.hlist.add(item_id, text=item_text)
tree.hlist.hide_entry(item_id)
recurse_tree(tree, dataset, item_id, hide=True)
def recurse_tree(self, ds, parent, hide=False):
""" order the dicom tags """
for data_element in ds:
if isinstance(data_element.value, compat.text_type):
text = compat.text_type(data_element)
ip = self.dsTreeView.AppendItem(parent, text=text)
else:
ip = self.dsTreeView.AppendItem(parent, text=str(data_element))
if data_element.VR == "SQ":
for i, ds in enumerate(data_element.value):
item_describe = data_element.name.replace(" Sequence", "")
item_text = "%s %d" % (item_describe, i + 1)
rjust = item_text.rjust(128)
parentNodeID = self.dsTreeView.AppendItem(ip, text=rjust)
self.recurse_tree(ds, parentNodeID)
def write_string(fp, data_element, padding=' ', encoding=default_encoding):
"""Write a single or multivalued string."""
val = multi_string(data_element.value)
if len(val) % 2 != 0:
val = val + padding # pad to even length
if isinstance(val, compat.text_type):
val = val.encode(encoding)
fp.write(val)
def __unicode__(self):
"""Return unicode representation of the element."""
if isinstance(self.value, compat.text_type):
# start with the string rep then replace the value part
# with the unicode
strVal = str(self)
strVal = strVal.replace(self.repval, "")
uniVal = compat.text_type(strVal) + self.value
return uniVal
else:
return compat.text_type(str(self))
data_element.value = [
val.decode(encodings) for val in data_element.value
]
else:
if data_element.VM <= 1:
data_element.value = PersonNameUnicode(data_element.value,
encodings)
else:
data_element.value = [
PersonNameUnicode(value, encodings)
for value in data_element.value
]
if data_element.VR in text_VRs:
# You can't re-decode unicode (string literals in py3)
if data_element.VM == 1:
if isinstance(data_element.value, compat.text_type):
return
data_element.value = decode_string(data_element.value, encodings,
TEXT_VR_DELIMS)
else:
output = list()
for value in data_element.value:
if isinstance(value, compat.text_type):
output.append(value)
else:
output.append(decode_string(value, encodings,
TEXT_VR_DELIMS))
data_element.value = output
if valid:
arg = (int(arg[0], 16), int(arg[1], 16))
elif isinstance(arg[0], compat.number_types):
valid = isinstance(arg[1], compat.number_types)
if not valid:
raise ValueError("Both arguments for Tag must be the same type, "
"either string or int.")
if arg[0] > 0xFFFF or arg[1] > 0xFFFF:
raise OverflowError("Groups and elements of tags must each "
"be <=2 byte integers")
long_value = (arg[0] << 16) | arg[1]
# Single str parameter
elif isinstance(arg, (str, compat.text_type)):
try:
long_value = int(arg, 16)
if long_value > 0xFFFFFFFF:
raise OverflowError("Tags are limited to 32-bit length; "
"tag {0!r}"
.format(long_value))
except ValueError:
# Try a DICOM keyword
from pydicom.datadict import tag_for_keyword
long_value = tag_for_keyword(arg)
if long_value is None:
raise ValueError("'{}' is not a valid int or DICOM keyword"
.format(arg))
# Single int parameter
else:
long_value = arg
def recurse_tree(self, ds, parent, hide=False):
""" order the dicom tags """
for data_element in ds:
if isinstance(data_element.value, compat.text_type):
text = compat.text_type(data_element)
ip = self.dsTreeView.AppendItem(parent, text=text)
else:
ip = self.dsTreeView.AppendItem(parent, text=str(data_element))
if data_element.VR == "SQ":
for i, ds in enumerate(data_element.value):
item_describe = data_element.name.replace(" Sequence", "")
item_text = "%s %d" % (item_describe, i + 1)
rjust = item_text.rjust(128)
parentNodeID = self.dsTreeView.AppendItem(ip, text=rjust)
self.recurse_tree(ds, parentNodeID)