Skip to content

cli

cli

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

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

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

cli() -> 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
21
22
23
24
25
26
27
28
@click.group
def cli() -> None:
    """
    Just the central cli entry command. Currently, we don't use it, but its here
    for future content.

    """
    pass

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

Source code in src/qgis_server_light/exporter/cli.py
31
32
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
@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)}.",
)
@click.option(
    "--pg_service_conf",
    default=None,
    help="Absolute path to a pg_service.conf file to take connection information from.",
)
@cli.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.

    It takes into account the PGSERVICEFILE environment variable. The cli might be called with:

      PGSERVICEFILE=<absolute-path-to-pg_service.conf> python -m qgis_server_light.exporter.cli ...

    The pg_service.conf absolute path can be passed with parameter too. If this is done, the one out of
    environment will be joined with the passed one. The passed one overwrites values of the environment one.
    """,
)
def export(
    project: str,
    unify_layer_names_by_group: bool = False,
    output_format: str | None = None,
    pg_service_conf: str | None = None,
) -> None:
    logging.getLogger().setLevel(logging.DEBUG)
    serializer_config = SerializerConfig(indent="  ")
    if output_format is None:
        output_format = "json"
    if not project.lower().endswith(allowed_extensions):
        raise NotImplementedError(
            f"Allowed qgis project file extensions are: {'|'.join(allowed_extensions)} not => {project}"
        )
    if output_format.lower() not in allowed_output_formats:
        raise NotImplementedError(
            f"Allowed output formats are: {'|'.join(allowed_output_formats)} not => {output_format}"
        )
    full_pg_service_config = create_full_pg_service_conf(pg_service_conf)
    if os.path.isfile(project):
        exporter = Exporter(
            project,
            unify_layer_names_by_group=bool(unify_layer_names_by_group),
            pg_service_configs=full_pg_service_config,
        )
        config = exporter.run()
        if output_format == "json":
            click.echo(JsonSerializer(config=serializer_config).render(config))
        elif output_format == "xml":
            click.echo(XmlSerializer(config=serializer_config).render(config))

    else:
        raise AttributeError("Project file does not exist")