Example #1
0
def addqres(config, qrc_path, res_folders, alias, verbose):
    """
    Add <qresource> element with a prefix attribute set to the base name of
    the given folder of resources. All resources contained in this folder are
    recorded in qresource as <file> subelement.

    Args:
        config (:class:`PyqtcliConfig`): PyqtcliConfig object representing
            project config file.
        qrc_path (str): Path to the qrc file that need to add the qresource
            nodes corresponding to `res_folders`.
        res_folders (tuple): Paths to folders of resources to record.
        alias (bool): If True, aliases will be generated while resources are
            recorded.
        verbose (bool): Boolean determining if messages will be displayed.
    """
    qrc_file = read_qrc(qrc_path)
    recorded_dirs = config.get_dirs(qrc_file.name)

    # Remove duplication in res_folders with a set
    res_folders = set(res_folders)

    # Process each resource folders passed
    for folder in res_folders:
        # rel_path => relative path of folder from project directory
        rel_path = os.path.relpath(folder, config.dir_path)

        # Check if given resources folder has already been recorded
        if rel_path in recorded_dirs:
            v.warning("You have already added \'{}\' to {}.".format(
                    folder, qrc_file.name))
            continue

        # Add folder to dirs variable in the config file
        try:
            config.add_dirs(qrc_file.name, rel_path, commit=False)
        except PyqtcliConfigError:
            v.error("{} isn't part of the project.".format(qrc_path))
            raise click.Abort()

        # Add qresource to qrc file
        prefix = get_prefix(folder)
        qrc_file.add_qresource(prefix)
        fill_qresource(qrc_file, folder, prefix)

        v.info("qresource with prefix: \'{}\' has been recorded in {}.".format(
                prefix, qrc_path), verbose)

    qrc_file.build()
    config.save()

    if alias:
        write_alias([qrc_file.path], verbose)
Example #2
0
def rmqres(config, qrc_path, res_folders, verbose):
    """
    Remove a <qresource> element with a prefix attribute set to the base name
    of the given folder of resources. All <file> subelements are removed too.

    Args:
        config (:class:`PyqtcliConfig`): PyqtcliConfig object representing
            project config file.
        qrc_path (str): Path to the qrc file that need to remove the qresource
            nodes corresponding to `res_folders`.
        res_folders (tuple): Paths to folders of resources to remove.
        verbose (bool): Boolean determining if messages will be displayed.
    """
    qrcfile = read_qrc(qrc_path)

    # Remove duplication in res_folders with a set
    res_folders = set(res_folders)

    folders = [os.path.relpath(f, config.dir_path) for f in res_folders]

    # remove folder to dirs variable in the config file
    try:
        config.rm_dirs(qrcfile.name, folders, commit=False)
    except PyqtcliConfigError:
        v.error("{} isn't part of the project.".format(qrc_path))
        raise click.Abort()

    for folder in folders:
        # Remove qresource to qrc file
        prefix = get_prefix(folder)
        qrcfile.remove_qresource(prefix)

        v.info("Resources folder: \'{}\' has been removed in {}.".format(
                folder, qrc_path), verbose)

    config.save()
    qrcfile.build()