Ejemplo n.º 1
0
def test_add_file_to_nonexistent_qresource():
    with pytest.raises(QresourceError) as e:
        qrc = QRCFile("res.qrc")
        qrc.add_file("test.txt", '/')
        qrc.build()
    assert str(e.value) == ("Error: No <qresource> node corresponding to '/' "
                            "prefix")
Ejemplo n.º 2
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)