Exemplo n.º 1
0
    def test_no_subdir(self):
        """get_random_filename_in_dir should not include subdirectories if [include_subdir] is False"""

        folder = "folder"

        with patch('os.path.exists', return_value=True):
            with patch('os.walk',
                       return_value=[("root", "dir", "file")]) as walk_mock:
                with patch('os.listdir', return_value=[folder, folder]):
                    with patch('os.path.isfile', return_value=True):
                        utils.get_random_filename_in_dir("some/path")
                        self.assertEqual(walk_mock.called, False)
Exemplo n.º 2
0
def get_preview_file_name(path,
                          add_prefix=False,
                          prefix="",
                          add_suffix=False,
                          suffix="",
                          change_ext=False,
                          new_ext="",
                          rename=False,
                          old_name="",
                          new_name="",
                          get_new=False,
                          include_subdir=False):
    """
    Returns preview filename. When called for the first time it uses a random filename in
    the provided path and cache it. Every time after that it will use the cached filename
    unless [get_new] argument is set to True.

    If path is invalid, it returns an empty string.

    If there are no files in the provided path it returns "No files found"
    """

    global cached_filename

    norm_path = os.path.normpath(path)

    if not os.path.exists(norm_path):
        return "Invalid directory"

    if not get_new and cached_filename != "":
        return full_rename(cached_filename,
                           add_prefix=add_prefix,
                           prefix=prefix,
                           add_suffix=add_suffix,
                           suffix=suffix,
                           change_ext=change_ext,
                           new_ext=new_ext,
                           rename=rename,
                           old_name=old_name,
                           new_name=new_name)

    rand_filename = get_random_filename_in_dir(norm_path,
                                               include_subdir=include_subdir)

    if rand_filename is None:
        return "No files found"

    filename = full_rename(rand_filename,
                           add_prefix=add_prefix,
                           prefix=prefix,
                           add_suffix=add_suffix,
                           suffix=suffix,
                           change_ext=change_ext,
                           new_ext=new_ext,
                           rename=rename,
                           old_name=old_name,
                           new_name=new_name)
    cached_filename = filename

    return filename
Exemplo n.º 3
0
    def test_valid(self):
        """get_random_filename_in_dir should return a filename with extension if path is valid"""

        filename = "file.png"

        with patch('os.path.exists', return_value=True):
            with patch('os.walk', return_value=[("root", "dir", "file")]):
                with patch('os.listdir', return_value=[filename, filename]):
                    with patch('os.path.isfile', return_value=True):
                        value = utils.get_random_filename_in_dir("some/path")
                        self.assertEqual(filename, value)
Exemplo n.º 4
0
    def test_invalid(self):
        """get_random_filename_in_dir should return an empty string if path is invalid"""

        with patch('os.path.exists', return_value=False):
            value = utils.get_random_filename_in_dir("some/path")
            self.assertEqual("", value)