Ejemplo n.º 1
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)
        new_path_exists = new_path.exists()
        new_path_samefile = new_path_exists and new_path.samefile(full_path)
        if new_path_exists and not new_path_samefile:
            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)

        if new_path_samefile and new_path == full_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"', full_path, new_path)
                shutil.move(src=str(full_path), dst=str(new_path))
        return new_path
Ejemplo n.º 2
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
Ejemplo n.º 3
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(
                    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:
                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
Ejemplo n.º 4
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)
        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:
            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"', path, new_path)
            shutil.copy2(src=str(path), dst=str(new_path))

        # the next actions should handle the original file
        return None
Ejemplo n.º 5
0
 def run(self, basedir: Path, path: Path, attrs: dict, simulate: bool):
     from send2trash import send2trash
     self.print('Trash "%s"' % path)
     if not simulate:
         full_path = fullpath(path)
         self.log.info('Moving file %s into trash.', full_path)
         send2trash(str(full_path))
Ejemplo n.º 6
0
 def pipeline(self, args):
     file_size = fullpath(args.path).stat().st_size
     if self.matches(file_size):
         return {"filesize": {"bytes": file_size}}
Ejemplo n.º 7
0
 def pipeline(self, args):
     return self.matches(str(fullpath(args["path"])))
Ejemplo n.º 8
0
 def pipeline(self, args: DotDict) -> Optional[Dict[str, Dict[str, int]]]:
     file_size = fullpath(args.path).stat().st_size
     if self.matches(file_size):
         return {"filesize": {"bytes": file_size}}
     return None