/usr/share/pyshared/dicom/valuerep.py is in python-dicom 0.9.6-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 | # valuerep.py
"""Special classes for DICOM value representations (VR)"""
# Copyright (c) 2008 Darcy Mason
# This file is part of pydicom, released under a modified MIT license.
# See the file license.txt included with this distribution, also
# available at http://pydicom.googlecode.com
from sys import version_info
if version_info[0] < 3:
namebase = object
bytestring = str
else:
namebase = bytestring
def is_stringlike(name):
"""Return True if name is string-like."""
try:
name + ""
except TypeError:
return False
else:
return True
class MultiValue(list):
"""MutliValue is a special list, derived to overwrite the __str__ method
to display the multi-value list more nicely. Used for Dicom values of
multiplicity > 1, i.e. strings with the "\" delimiter inside.
"""
def __str__(self):
lines = [str(x) for x in self]
return "[" + ", ".join(lines) + "]"
__repr__ = __str__
def MultiString(val, valtype=str):
"""Split a string by delimiters if there are any
val -- DICOM string to split up
valtype -- default str, but can be e.g. UID to overwrite to a specific type
"""
# Remove trailing blank used to pad to even length
# 2005.05.25: also check for trailing 0, error made in PET files we are converting
if val and (val.endswith(' ') or val.endswith('\x00')):
val = val[:-1]
splitup = [valtype(x) for x in val.split("\\")]
if len(splitup) == 1:
return splitup[0]
else:
return MultiValue(splitup)
class PersonNameBase(namebase):
"""Base class for Person Name classes"""
def __init__(self, val):
"""Initialize the PN properties"""
# Note normally use __new__ on subclassing an immutable, but here we just want
# to do some pre-processing for properties
# PS 3.5-2008 section 6.2 (p.28) and 6.2.1 describes PN. Briefly:
# single-byte-characters=ideographic characters=phonetic-characters
# (each with?):
# family-name-complex^Given-name-complex^Middle-name^name-prefix^name-suffix
self.parse()
def formatted(self, format_str):
"""Return a formatted string according to the format pattern
Use "...%(property)...%(property)..." where property is one of
family_name, given_name, middle_name, name_prefix, name_suffix
"""
return format_str % self.__dict__
def parse(self):
"""Break down the components and name parts"""
self.components = self.split("=")
nComponents = len(self.components)
self.single_byte = self.components[0]
self.ideographic = ''
self.phonetic = ''
if nComponents > 1:
self.ideographic = self.components[1]
if nComponents > 2:
self.phonetic = self.components[2]
if self.single_byte:
name_string = self.single_byte+"^^^^" # in case missing trailing items are left out
parts = name_string.split("^")[:5]
(self.family_name, self.given_name, self.middle_name,
self.name_prefix, self.name_suffix) = parts
else:
(self.family_name, self.given_name, self.middle_name,
self.name_prefix, self.name_suffix) = ('', '', '', '', '')
class PersonName(PersonNameBase, str):
"""Human-friendly class to hold VR of Person Name (PN)
Name is parsed into the following properties:
single-byte, ideographic, and phonetic components (PS3.5-2008 6.2.1)
family_name,
given_name,
middle_name,
name_prefix,
name_suffix
"""
def __new__(cls, val):
"""Return instance of the new class"""
# Check if trying to convert a string that has already been converted
if isinstance(val, PersonName):
return val
return super(PersonName, cls).__new__(cls, val)
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, unicode):
"""Unicode version of Person Name"""
def __new__(cls, val, encodings):
"""Return unicode string after conversion of each part
val -- the PN value to store
encodings -- a list of python encodings, generally found
from dicom.charset.python_encodings mapping
of values in DICOM data element (0008,0005).
"""
# Make the possible three character encodings explicit:
if not isinstance(encodings, list):
encodings = [encodings]*3
if len(encodings) == 2:
encodings.append(encodings[1])
# print encodings
components = val.split("=")
unicomponents = [unicode(components[i],encodings[i])
for i, component in enumerate(components)]
new_val = u"=".join(unicomponents)
return unicode.__new__(cls, new_val)
def __init__(self, val, encodings):
self.encodings = encodings
PersonNameBase.__init__(self, val)
def family_comma_given(self):
"""Return name as 'Family-name, Given-name'"""
return self.formatted("%(family_name)u, %(given_name)u")
class OtherByte(bytestring):
pass
|