Esempio n. 1
0
    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
Esempio n. 2
0
    def test_max_nested_tokens(self):
        format_string = 'prefix_%(Key1)s_suffix'
        # Make the values recurse
        values = {
            'Key1': '%(Key2)s',
            'Key2': '%(Key3)s',
            'Key3': '%(Key4)s',
            'Key4': '%(Key5)s',
            'Key5': '%(Key6)s',
            'Key6': 'blah',
        }

        # Will only perform replacements 5 times
        output = utils.recursive_replace_tokens(format_string, values)

        assert output == 'prefix_%(Key6)s_suffix'
Esempio n. 3
0
    def test_no_tokens(self):
        format_string = 'no_token_string'
        output = utils.recursive_replace_tokens(format_string, {})

        assert output == format_string
Esempio n. 4
0
    def test_nested_tokens(self):
        format_string = 'prefix_%(Key1)s_suffix'
        values = {'Key1': '%(Key2)s', 'Key2': 'value'}
        output = utils.recursive_replace_tokens(format_string, values)

        assert output == 'prefix_value_suffix'