def sort(self, root, directory_fields, filename_string, test=False, rootdir=None, keep_original=True): # If we want to sort in place if directory_fields is None: destination = os.path.relpath(self.filename, rootdir[0]) destination = os.path.join(root, destination) else: destination = self.get_destination(root, directory_fields, filename_string) if test: print(destination) return utils.mkdir(os.path.dirname(destination)) # Check if destination exists while os.path.exists(destination): destination = destination + '.copy' if self.is_anonymous(): # Actually write the anonymous data # write everything in anonymization_lookup -> Parse it so we can # have dynamic fields for key in self.anonymization_lookup.keys(): replacement_value = self.anonymization_lookup[key] % self try: self.dicom.data_element(key).value = replacement_value except KeyError: continue self.dicom.save_as(destination) if keep_original is False: os.remove(self.filename) else: if keep_original: shutil.copy(self.filename, destination) else: shutil.move(self.filename, destination)
def test_new_nested_directory(self, tmpdir): new_dir = str(tmpdir.join('new_dir').join('nested_dir')) utils.mkdir(new_dir) assert os.path.exists(new_dir)
def test_directory_exists(self, tmpdir): utils.mkdir(str(tmpdir))