File: description_to_json.py

package info (click to toggle)
qgis 3.40.10%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,183,672 kB
  • sloc: cpp: 1,595,771; python: 372,544; xml: 23,474; sh: 3,761; perl: 3,664; ansic: 2,257; sql: 2,137; yacc: 1,068; lex: 577; javascript: 540; lisp: 411; makefile: 161
file content (58 lines) | stat: -rw-r--r-- 2,033 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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
"""
***************************************************************************
*                                                                         *
*   This program is free software; you can redistribute it and/or modify  *
*   it under the terms of the GNU General Public License as published by  *
*   the Free Software Foundation; either version 2 of the License, or     *
*   (at your option) any later version.                                   *
*                                                                         *
***************************************************************************

Parses .txt algorithm description files and builds aggregated .json
description
"""

import argparse
import json
import os
from pathlib import Path


def main(description_folder: str, output_file: str):
    from .parsed_description import ParsedDescription

    algorithms = []
    folder = Path(description_folder)
    for description_file in folder.glob("*.txt"):

        description = ParsedDescription.parse_description_file(
            description_file, translate=False
        )

        ext_path = description_file.parents[1].joinpath(
            "ext", description.name.replace(".", "_") + ".py"
        )
        if ext_path.exists():
            description.ext_path = description.name.replace(".", "_")

        algorithms.append(description.as_dict())

    Path(output_file).parent.mkdir(parents=True, exist_ok=True)
    with open(output_file, "w", encoding="utf8") as f_out:
        f_out.write(json.dumps(algorithms, indent=2))


parser = argparse.ArgumentParser(
    description="Parses GRASS .txt algorithm "
    "description files and builds "
    "aggregated .json description"
)

parser.add_argument("input", help="Path to the description directory")
parser.add_argument("output", help="Path to the output algorithms.json file")
args = parser.parse_args()

if not os.path.isdir(args.input):
    raise ValueError(f"Input directory '{args.input}' is not a directory.")

main(args.input, args.output)