Skip to content

Commit

Permalink
Merge pull request #6 from fedorov/update-add-unsafe
Browse files Browse the repository at this point in the history
General fixes and unsafe mode added
  • Loading branch information
pieper committed Jul 4, 2018
2 parents f621373 + 595e3a3 commit a65b6ef
Showing 1 changed file with 19 additions and 6 deletions.
25 changes: 19 additions & 6 deletions dicomsort.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,14 @@
import zipfile

# special public packages
import dicom
from dicom.filereader import InvalidDicomError
try:
import dicom
from dicom.filereader import InvalidDicomError
except ImportError:
import pydicom
from pydicom.filereader import InvalidDicomError
dicom = pydicom


# }}}

Expand Down Expand Up @@ -58,6 +64,8 @@ def __init__(self):
'--keepGoing': 'keepGoing',
'-t': 'test',
'--test': 'test',
'-u': 'unsafe',
'--unsafe': 'unsafe'
}

self.defaultOptions = {
Expand All @@ -69,6 +77,7 @@ def __init__(self):
'keepGoing': False,
'verbose': False,
'test': False,
'unsafe': False
}

self.requiredOptions = [ 'sourceDir', 'targetPattern', ]
Expand Down Expand Up @@ -107,7 +116,7 @@ def safeFileName(self,fileName):
safeName += c
return safeName

def pathFromDatasetPattern(self,ds):
def pathFromDatasetPattern(self,ds,safe=True):
"""Given a dicom dataset, use the targetPattern option
to define a file path"""
replacements = {}
Expand All @@ -119,7 +128,10 @@ def pathFromDatasetPattern(self,ds):
value = ""
if value == "":
value = "Unknown%s" % key
replacements[key] = self.safeFileName(str(value))
if safe:
replacements[key] = self.safeFileName(str(value))
else:
replacements[key] = str(value)
return fmt % replacements

def formatFromPattern(self):
Expand Down Expand Up @@ -183,7 +195,7 @@ def renameFile(self,file):
# needed for issue with pydicom 0.9.9 and some dicomdir files
return False
# check for valid path - abort program to avoid overwrite
path = self.pathFromDatasetPattern(ds)
path = self.pathFromDatasetPattern(ds, safe=(not sorter.options['unsafe']))
if os.path.exists(path):
print('\nSource file: %s' % file)
print('Target file: %s' % path)
Expand Down Expand Up @@ -290,11 +302,12 @@ def usage():
print(" [-k,--keepGoing] - report but ignore dupicate target files")
print(" [-v,--verbose] - print diagnostics while processing")
print(" [-t,--test] - run the built in self test (requires internet)")
print(" [-u,--unsafe] - do not replace unsafe characters with '_' in the path")
print(" [--help] - print this message")
print("\n <patterns...> is a string defining the output file and directory")
print("names based on the dicom tags in the file.")
print("\n Examples:")
print("\n dicomsort data sorted/%PatientName/%StudyDate/%SeriesDescription-%InstanceUID.dcm")
print("\n dicomsort data sorted/%PatientName/%StudyDate/%SeriesDescription-%SOPInstanceUID.dcm")
print("\n could create a folder structure like:")
print("\n sorted/JohnDoe/2013-40-18/FLAIR-2.dcm")
print("\nIf patterns are not specified, the following default is used:")
Expand Down

0 comments on commit a65b6ef

Please sign in to comment.