Exemplo n.º 1
0
    def rename_file(self, dest):
        """
        Rename downloaded file from random name to name identified in url.
        Filename will have incremental number at the front if filename exist.

        dest: destination path
        """
        filename = self.progress.get("filename")
        dirname = FileManager.get_dirname(dest)

        # Get filename to rename the file
        # Increment number at the front of filename if filename exist
        new_dest = FileManager.join_path(dirname, filename)
        if FileManager.is_path_exists(new_dest):
            new_dest = FileManager.generate_filepath(new_dest)

        FileManager.rename_file(dest, new_dest)

        # Notify success work
        self.progress["filename"] = FileManager.get_basename(new_dest)
        self.progress["state"] = "Success"
        self.progresses.put((self.i, self.progress.copy()))
    # Parse command line arguments
    parser = argparse.ArgumentParser(
        description=
        'Download files from different sources with different protocols.')
    parser.add_argument('--input',
                        required=True,
                        metavar="/path/to/input/",
                        help='Path to input .yml file')
    parser.add_argument('--config',
                        required=True,
                        metavar="/path/to/config/",
                        help='Path to configurate .yml file')
    args = parser.parse_args()

    try:
        if not FileManager.is_path_exists(args.input):
            raise FileNotFoundError(errno.ENOENT, os.strerror(errno.ENOENT),
                                    args.input)
        if not FileManager.is_path_exists(args.config):
            raise FileNotFoundError(errno.ENOENT, os.strerror(errno.ENOENT),
                                    args.config)

        inputs = get_input(args.input)
        config = get_config(args.config)

        # Quit if inputs is empty
        if not inputs:
            raise Exception("No inputs given")

        works = Queue(maxsize=0)
        progresses = Queue(maxsize=0)