Skip to content

cli

cli

allowed_extensions = ('qgz', 'qgs') module-attribute

allowed_output_formats = ('json', 'xml') module-attribute

qgs = QgsApplication([], False) module-attribute

export(project: str, unify_layer_names_by_group: bool = False, output_format: str | None = None) -> None

Source code in src/qgis_server_light/exporter/cli.py
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
@click.option("--project", help="Absolute path to the QGIS project.")
@click.option(
    "--unify_layer_names_by_group",
    default=False,
    help="Use the full tree path to unify job_layer_definition names.",
)
@click.option(
    "--output_format",
    default="json",
    help=f"The desired output format. Allowed are {'|'.join(allowed_output_formats)}.",
)
@main.command(
    "export",
    context_settings={"max_content_width": 120},
    help=f"""
    Export a QGIS project ({"|".join(allowed_extensions)}) (1st argument) file
    to {"|".join(allowed_output_formats)} format.

    Layers referencing a pg_service need the PGSERVICEFILE environment variable so
    that QGIS can open them during the export. The cli might be called with:

      PGSERVICEFILE=<absolute-path-to-pg_service.conf> python -m qgis_server_light.exporter.cli ...
    """,
)
def export(
    project: str,
    unify_layer_names_by_group: bool = False,
    output_format: str | None = None,
) -> None:
    logging.getLogger().setLevel(logging.DEBUG)
    serializer_config = SerializerConfig(indent="  ")
    output_format = "json" if output_format is None else output_format.lower()
    if not project.lower().endswith(allowed_extensions):
        raise click.BadParameter(
            f"Allowed qgis project file extensions "
            f"are: {'|'.join(allowed_extensions)} not => {project}",
            param_hint="--project",
        )
    if output_format not in allowed_output_formats:
        raise click.BadParameter(
            f"Allowed output formats are: {'|'.join(allowed_output_formats)} "
            f"not => {output_format}",
            param_hint="--output_format",
        )
    if not os.path.isfile(project):
        raise click.BadParameter(
            f"Project file '{project}' does not exist",
            param_hint="--project",
        )

    exporter = Exporter(
        project,
        unify_layer_names_by_group=bool(unify_layer_names_by_group),
    )

    if output_format == "json":
        serializer = JsonSerializer(config=serializer_config)
    elif output_format == "xml":
        serializer = XmlSerializer(config=serializer_config)
    else:
        raise RuntimeError(f"Unexpected output format {output_format}")

    try:
        config = exporter.run()
    except ExportFailure as err:
        click.echo(serializer.render(err.get_result()))
        raise click.exceptions.Exit(code=err.EXIT_CODE) from err
    else:
        click.echo(serializer.render(config))

main() -> None

Just the central cli entry command. Currently, we don't use it, but its here for future content.

Source code in src/qgis_server_light/exporter/cli.py
24
25
26
27
28
29
30
@click.group
def main() -> None:
    """
    Just the central cli entry command. Currently, we don't use it, but its here
    for future content.

    """