Esempio n. 1
0
def makerc(qrc_files, recursive, verbose):
    """Generate python module for corresponding given qrc files.

    Args:
        qrc_files (tuple): Paths to qrc files that need to generate its
            corresponding rc files.
        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")
        else:
            generate_rc(recursive_qrc_files, verbose)

    # Process given files or warns user if none
    if qrc_files:
        generate_rc(qrc_files, verbose)
    elif not recursive:
        v.warning("No qrc files was given to process.")
Esempio n. 2
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. 3
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)
Esempio n. 4
0
def test_addqres_in_non_project_qrc(config, test_resources):
    runner = CliRunner()

    QRCTestFile("res").add_qresource("/").add_file("test.txt").build()

    result = runner.invoke(pyqtcli, ["addqres", "res.qrc", "resources"])
    assert format_msg(result.output) == v.error("res.qrc isn't part of the project.\nAborted!\n")
Esempio n. 5
0
def qrc(config, path, res_folder, verbose):
    """Create a new qrc file.

    Args:
        config (:class:`PyqtcliConfig`): PyqtcliConfig object representing
            project config file.
        path (str): Path where create the new qrc file.
        res_folder (str): Path to the folder of resources .
        verbose (bool): Boolean determining if messages will be displayed.

    """
    file_path, name = os.path.split(path)

    qrc_file = QRCFile(name, file_path)

    # Verify qrc file doesn't already exists
    if name in config.get_qrcs():
        v.error("A qrc file named \'{}\' already exists".format(name))
        raise click.Abort()

    # Add a new section for the created qrc file
    config.cparser.add_section(name)
    config.cparser.set(name, "path", qrc_file.path)

    if res_folder:
        generate_qrc(qrc_file, res_folder, build=False)

        # Get the relative path to the folder of resources from project
        # directory to add its sub dirs to the dirs variable in the
        # corresponding qrc section of config file
        rel_path = os.path.relpath(res_folder, config.dir_path)
        resources = os.listdir(rel_path)

        # If there is resources that are not in folder add the res_folder in the
        # dirs key of the config file.
        config.add_dirs(name, rel_path, commit=False)

        for d in resources:
            if os.path.isdir(os.path.join(rel_path, d)):
                config.add_dirs(name, os.path.join(rel_path, d), commit=False)

    qrc_file.build()
    config.save()

    v.info("Qrc file \'{}\' has been created.".format(path), verbose)
Esempio n. 6
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()