Esempio n. 1
0
def makealias(qrc_files, recursive, verbose):
    """Command to generate aliases for each resources contained in qrc files.

    Args:
        qrc_files (tuple): Paths to qrc files that need to generate alias.
        recursive (bool): If True, search recursively qrc filed from launching
            directory.
        verbose (bool): Boolean determining if messages will be displayed.

    """
    # Check all qrc files recursively
    if recursive:
        recursive_qrc_files = recursive_file_search("qrc")

        # Check if recursive option find qrc files
        if not recursive_qrc_files:
            v.error("Could not find any qrc files.")
            raise click.Abort()
        else:
            write_alias(recursive_qrc_files, verbose)

    # Process given files or warns user if none
    if qrc_files:
        write_alias(qrc_files, verbose)
    elif not recursive:
        v.warning("No qrc files was given to process.")
Esempio n. 2
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)