예제 #1
0
def move_from_to(root_path: str, destination_path: str) -> bool:
    """Move files from the root to the destination path.

    Parameters
    ----------
    root_path : str
        The root path
    destination_path : str
        The destination path

    Returns
    -------
    bool

    Obs: raises exceptions on the log file.
    """

    if not exists(path=destination_path):
        try:
            makedirs(name=destination_path)
        except Exception as e:
            write_log_msg(msg=str(e) + '(move_from_to function)')
            return False

    try:
        move(src=root_path, dst=destination_path)
    except Exception as e:
        write_log_msg(msg=str(e) + '(move_from_to function)')
        return False
    else:
        return True
예제 #2
0
def scan_dir(path: str) -> Generator[tuple, None, None]:
    """Directory tree generator.

    For each directory in the directory tree, yields a 3-tuple

        dirpath, dirnames, filenames

    Parameters
    ----------
    path : str
        The path name (directory) to be scanned

    Returns
    -------
    generator

    Obs: raises exceptions on the log file.
    """

    try:
        scanned_dir = walk(abspath(path), topdown=True)
    except Exception as e:
        write_log_msg(msg=str(e) + '(scan_dir funtion)')
    else:
        return scanned_dir
예제 #3
0
def read_category_names(
        file_name: str = COMMON_PATH + 'category_names.json') -> dict:
    """Loads a json file containing folder names by category.

    Parameters
    ----------
    file_name : str (default folder_names.json)
        The name of the file containing folder names by category

    Returns
    -------
    dictionary

    Obs: raises exceptions on the log file.
    """

    try:
        with open(file=file_name, mode='r') as file_handle:
            folder_names_dict = loads(file_handle.read())
    except Exception as e:
        write_log_msg(msg=str(e) + f'({file_name})')
    else:
        return folder_names_dict
예제 #4
0
def read_file_extensions(file_name: str = abspath(
    COMMON_PATH + 'file_extensions.json')) -> dict:
    """Loads information about file extensions.

    Parameters
    ----------
    file_name : str (default file_extensions.json)
        The name of the file containing the categories and description for each extension

    Returns
    -------
    dictionary

    Obs: raises exceptions on the log file
    """

    try:
        with open(file=file_name, mode='r') as file_handle:
            file_ext_dict = loads(file_handle.read())
    except Exception as e:
        write_log_msg(msg=str(e))
    else:
        return file_ext_dict
예제 #5
0
def read_config(file_name: str = PATH + FILE_NAME) -> dict:
    """Loads the configuration file and validates the parameters.

    Parameters
    ----------
    file_name : str (default config.json)
        The name of the configuration file

    Returns
    -------
    dictionary

    Obs: raises exceptions on the log file.
    """
    try:
        with open(file_name, 'r') as file_handle:
            config_dict = loads(file_handle.read())
    except Exception as e:
        write_log_msg(msg=f'Problem reading {file_name}. ({e})')
    else:
        parameters = (('root_path', str), \
                      ('destination_path', str), \
                      ('organize_files_per_category', str), \
                      ('organize_files_per_type', str), \
                      ('organize_files_inside_subfolders', str), \
                      ('do_not_move', list)) # Parameters name

        bad_configs = [
        ]  # Holds keys of the config dict with wrong names or bad values

        for config in config_dict:  # This block of code will validate each key and value of the config dict
            for parameter, parameter_type in parameters:
                # Check if the parameter exist
                if parameter not in config_dict[config].keys():
                    write_log_msg(
                        msg=
                        f'The parameter {parameter} was not found in {config} ({file_name})'
                    )
                    if config not in bad_configs:
                        bad_configs.append(config)
                else:
                    # Check if the value of the parameter is string
                    if type(config_dict[config]
                            [parameter]) is not parameter_type:
                        write_log_msg(
                            msg=
                            f'The parameter {parameter} in {config} must be {parameter_type}. ({file_name})'
                        )
                        if config not in bad_configs:
                            bad_configs.append(config)
                    else:
                        # Check if root and destination path exist
                        if parameter in ('root_path', 'destination_path'):
                            if not exists(
                                    abspath(config_dict[config][parameter])):
                                write_log_msg(
                                    msg=
                                    f'The following path does not exist: {config_dict[config][parameter]} ({file_name})'
                                )
                                if config not in bad_configs:
                                    bad_configs.append(config)
                            else:
                                config_dict[config][parameter] = abspath(
                                    config_dict[config][parameter])
                        elif parameter == 'do_not_move':
                            for value in config_dict[config][parameter]:
                                if type(value) is not str:
                                    write_log_msg(
                                        msg=
                                        f'The values of the parameter {parameter} in {config} must be str. ({file_name})'
                                    )
                                elif len(value.strip()) == 0:
                                    config_dict[config][parameter].remove(
                                        value)

                        # Check if the parameter has the right value (yes/no)
                        else:
                            if config_dict[config][parameter].lower() not in (
                                    'yes', 'no'):
                                write_log_msg(
                                    msg=
                                    f'The value of {parameter} in {config} is wrong: yes/no expected ({file_name})'
                                )
                                if config not in bad_configs:
                                    bad_configs.append(config)
                            else:
                                config_dict[config][parameter] = config_dict[
                                    config][parameter].lower()

        if len(bad_configs) > 0:  # Remove bad configs
            for config in bad_configs:
                del config_dict[config]

        return config_dict if len(config_dict) > 0 else None