def get_destination(self, root, directory_format, filename_format): # First we need to clean up the elements of directory_format to make # sure that we don't have any bad characters (including /) in the # folder names directory = root for item in directory_format: try: subdir = utils.recursive_replace_tokens(item, self) subdir = utils.clean_directory_name(subdir) except AttributeError: subdir = 'UNKNOWN' directory = os.path.join(directory, subdir) # Maximum recursion is 5 so we don't end up with any infinite loop # situations try: filename = utils.recursive_replace_tokens(filename_format, self) filename = utils.clean_path(filename) out = os.path.join(directory, filename) except AttributeError: # Now just use the initial filename origname = os.path.split(self.filename)[1] out = os.path.join(directory, origname) return out
def test_with_invalid_parts(self): dirname = 'prefix:*?|"<>suffix' path = os.path.join('tmp', 'dir', dirname, dirname) expected = os.path.join('tmp', 'dir', 'prefix_suffix', 'prefix_suffix') assert utils.clean_path(path) == expected
def test_no_invalid_parts(self): path = os.path.join('/tmp/dir/ok') assert utils.clean_path(path) == path