예제 #1
0
    def run(self, attrs: dict, simulate: bool) -> Path:
        path = attrs["path"]
        expanded_name = self.fill_template_tags(self.name, attrs)
        new_path = path.parent / expanded_name

        # handle filename collisions
        new_path_exists = new_path.exists()
        new_path_samefile = new_path_exists and new_path.samefile(path)
        if new_path_exists and not new_path_samefile:
            if self.overwrite:
                self.print("File already exists")
                Trash().run({"path": new_path}, simulate=simulate)
            else:
                new_path = find_unused_filename(
                    path=new_path, separator=self.counter_separator)

        # do nothing if the new name is equal to the old name and the file is
        # the same
        if new_path_samefile and new_path == path:
            self.print("Keep name")
        else:
            self.print('New name: "%s"' % new_path.name)
            if not simulate:
                self.log.info('Renaming "%s" to "%s".', path, new_path)
                path.rename(new_path)
        return new_path
예제 #2
0
    def run(self, attrs: dict, simulate: bool):
        path = attrs['path']
        basedir = attrs['basedir']

        expanded_dest = self.fill_template_tags(self.dest, attrs)
        # if only a folder path is given we append the filename to have the full
        # path. We use os.path for that because pathlib removes trailing slashes
        if expanded_dest.endswith(('\\', '/')):
            expanded_dest = os.path.join(expanded_dest, path.name)

        new_path = fullpath(expanded_dest)
        new_path_exists = new_path.exists()
        new_path_samefile = new_path_exists and new_path.samefile(path)
        if new_path_exists and not new_path_samefile:
            if self.overwrite:
                self.print('File already exists')
                Trash().run({'path': new_path}, simulate=simulate)
            else:
                new_path = find_unused_filename(new_path)

        if new_path_samefile and new_path == path:
            self.print('Keep location')
        else:
            self.print('Move to "%s"' % new_path)
            if not simulate:
                self.log.info('Creating folder if not exists: %s',
                              new_path.parent)
                new_path.parent.mkdir(parents=True, exist_ok=True)
                self.log.info('Moving "%s" to "%s"', path, new_path)
                shutil.move(src=str(path), dst=str(new_path))
        return new_path
예제 #3
0
    def pipeline(self, args: Mapping) -> None:
        path = args["path"]
        simulate = args["simulate"]

        expanded_dest = self.fill_template_tags(self.dest, args)
        # if only a folder path is given we append the filename to have the full
        # path. We use os.path for that because pathlib removes trailing slashes
        if expanded_dest.endswith(("\\", "/")):
            expanded_dest = os.path.join(expanded_dest, path.name)

        new_path = fullpath(expanded_dest)
        if new_path.exists() and not new_path.samefile(path):
            if self.overwrite:
                self.print("File already exists")
                Trash().run(path=new_path, simulate=simulate)
            else:
                new_path = find_unused_filename(
                    path=new_path, separator=self.counter_separator
                )

        self.print('Copy to "%s"' % new_path)
        if not simulate:
            logger.info("Creating folder if not exists: %s", new_path.parent)
            new_path.parent.mkdir(parents=True, exist_ok=True)
            logger.info('Copying "%s" to "%s"', path, new_path)
            shutil.copy2(src=str(path), dst=str(new_path))

        # the next actions should handle the original file
        return None
예제 #4
0
    def pipeline(self, args: Mapping) -> Mapping[str, Path]:
        path = args["path"]  # type: Path
        simulate = args["simulate"]
        expanded_name = self.fill_template_tags(self.name, args)
        new_path = path.parent / expanded_name

        # handle filename collisions
        new_path_exists = new_path.exists()
        new_path_samefile = new_path_exists and new_path.samefile(path)
        if new_path_exists and not new_path_samefile:
            if self.overwrite:
                self.print("File already exists")
                Trash().run(path=new_path, simulate=simulate)
            else:
                new_path = find_unused_filename(
                    path=new_path, separator=self.counter_separator)

        # do nothing if the new name is equal to the old name and the file is
        # the same
        if new_path_samefile and new_path == path:
            self.print("Keep name")
        else:
            self.print('New name: "%s"' % new_path.name)
            if not simulate:
                logger.info('Renaming "%s" to "%s".', path, new_path)
                path.rename(new_path)

        return {"path": new_path}
예제 #5
0
    def pipeline(self, args: DotDict) -> Mapping[str, Path]:
        path = args["path"]
        simulate = args["simulate"]

        expanded_dest = self.fill_template_tags(self.dest, args)
        # if only a folder path is given we append the filename to have the full
        # path. We use os.path for that because pathlib removes trailing slashes
        if expanded_dest.endswith(("\\", "/")):
            expanded_dest = os.path.join(expanded_dest, path.name)

        new_path = fullpath(expanded_dest)
        new_path_exists = new_path.exists()
        new_path_samefile = new_path_exists and new_path.samefile(path)
        if new_path_exists and not new_path_samefile:
            if self.overwrite:
                self.print("File already exists")
                Trash().run(path=new_path, simulate=simulate)
            else:
                new_path = find_unused_filename(
                    path=new_path, separator=self.counter_separator)

        if new_path_samefile and new_path == path:
            self.print("Keep location")
        else:
            self.print('Move to "%s"' % new_path)
            if not simulate:
                logger.info("Creating folder if not exists: %s",
                            new_path.parent)
                new_path.parent.mkdir(parents=True, exist_ok=True)
                logger.info('Moving "%s" to "%s"', path, new_path)
                shutil.move(src=str(path), dst=str(new_path))

        return {"path": new_path}
예제 #6
0
    def run(self, basedir: Path, path: Path, attrs: dict, simulate: bool):
        full_path = fullpath(path)

        expanded_dest = self.fill_template_tags(self.dest, basedir, path,
                                                attrs)
        # if only a folder path is given we append the filename to have the full
        # path. We use os.path for that because pathlib removes trailing slashes
        if expanded_dest.endswith(('\\', '/')):
            expanded_dest = os.path.join(expanded_dest, path.name)

        new_path = fullpath(expanded_dest)
        if new_path.exists() and not new_path.samefile(full_path):
            if self.overwrite:
                self.print('File already exists')
                Trash().run(basedir,
                            path=new_path,
                            attrs=attrs,
                            simulate=simulate)
            else:
                new_path = find_unused_filename(new_path)

        self.print('Copy to "%s"' % new_path)
        if not simulate:
            self.log.info('Creating folder if not exists: %s', new_path.parent)
            new_path.parent.mkdir(parents=True, exist_ok=True)
            self.log.info('Copying "%s" to "%s"', full_path, new_path)
            shutil.copy2(src=str(full_path), dst=str(new_path))

        # the next actions should handle the original file
        return None
예제 #7
0
def test_unused_filename_increase_digit(mock_exists):
    mock_exists.side_effect = [True, False]
    assert find_unused_filename(
        Path('7.gif')) == Path('7 3.gif')
예제 #8
0
def test_unused_filename_increase(mock_exists):
    mock_exists.side_effect = [True, False]
    assert find_unused_filename(
        Path('somefile 7.jpg')) == Path('somefile 9.jpg')
예제 #9
0
def test_unused_filename_multiple(mock_exists):
    mock_exists.side_effect = [True, True, False]
    assert find_unused_filename(Path('somefile.jpg')) == Path('somefile 4.jpg')
예제 #10
0
def test_unused_filename_basic(mock_exists):
    mock_exists.return_value = False
    assert find_unused_filename(Path('somefile.jpg')) == Path('somefile 2.jpg')
예제 #11
0
def test_unused_filename_separator(mock_exists):
    mock_exists.return_value = False
    assert find_unused_filename(Path("somefile.jpg"), separator="_") == Path(
        "somefile_2.jpg"
    )