2015-09-20 17:12:43 +02:00
|
|
|
import glob
|
2015-06-11 12:02:27 +10:00
|
|
|
import json
|
|
|
|
import os
|
|
|
|
import sys
|
2024-11-29 14:26:30 +01:00
|
|
|
|
2021-05-08 22:31:50 +02:00
|
|
|
from copy import deepcopy
|
2015-06-11 12:02:27 +10:00
|
|
|
|
2015-11-08 15:37:58 +00:00
|
|
|
sys.path.append(
|
|
|
|
os.path.join(os.path.dirname(os.path.realpath(__file__)), "../python/ext-libs")
|
|
|
|
)
|
|
|
|
|
2018-09-17 00:01:35 +02:00
|
|
|
cpp = open(sys.argv[1], "w", encoding="utf-8")
|
2015-09-20 17:12:43 +02:00
|
|
|
cpp.write(
|
|
|
|
'#include "qgsexpression.h"\n'
|
2019-11-01 17:07:55 +01:00
|
|
|
'#include "qgsexpression_p.h"\n'
|
2019-11-25 16:42:24 +10:00
|
|
|
"#include <mutex>\n"
|
2015-09-20 17:12:43 +02:00
|
|
|
"\n"
|
|
|
|
"void QgsExpression::initFunctionHelp()\n"
|
|
|
|
"{\n"
|
2019-11-25 16:42:24 +10:00
|
|
|
" static std::once_flag initialized;\n"
|
|
|
|
" std::call_once( initialized, []\n"
|
|
|
|
" {"
|
2015-09-20 17:12:43 +02:00
|
|
|
)
|
2015-06-11 12:02:27 +10:00
|
|
|
|
|
|
|
|
2015-09-20 17:12:43 +02:00
|
|
|
def quote(v):
|
|
|
|
if isinstance(v, dict):
|
|
|
|
for k in v:
|
|
|
|
v[k] = quote(v[k])
|
|
|
|
return v
|
|
|
|
elif isinstance(v, list):
|
|
|
|
return map(quote, v)
|
2015-06-11 12:02:27 +10:00
|
|
|
|
2018-09-17 00:01:35 +02:00
|
|
|
elif isinstance(v, str):
|
2015-09-20 17:12:43 +02:00
|
|
|
return v.replace('"', '\\"').replace("\n", "\\n")
|
|
|
|
|
|
|
|
elif isinstance(v, bool):
|
|
|
|
return v
|
|
|
|
|
|
|
|
else:
|
|
|
|
raise BaseException("unexpected type " + repr(v))
|
|
|
|
|
2017-03-04 19:41:23 +01:00
|
|
|
|
2015-09-20 17:12:43 +02:00
|
|
|
for f in sorted(glob.glob("resources/function_help/json/*")):
|
2018-09-17 00:01:35 +02:00
|
|
|
with open(f, encoding="utf-8") as function_file:
|
2015-09-20 17:12:43 +02:00
|
|
|
try:
|
|
|
|
json_params = json.load(function_file)
|
|
|
|
except:
|
2015-11-08 15:37:58 +00:00
|
|
|
print(f)
|
2015-09-20 17:12:43 +02:00
|
|
|
raise
|
|
|
|
|
|
|
|
json_params = quote(json_params)
|
|
|
|
|
|
|
|
for field in ["name", "type"]:
|
2017-03-04 19:41:23 +01:00
|
|
|
if field not in json_params:
|
2023-02-01 09:54:38 +01:00
|
|
|
raise BaseException(f"{f}: {field} missing")
|
2015-09-20 17:12:43 +02:00
|
|
|
|
|
|
|
if not json_params["type"] in [
|
|
|
|
"function",
|
|
|
|
"operator",
|
|
|
|
"value",
|
|
|
|
"expression",
|
|
|
|
"group",
|
|
|
|
]:
|
2023-02-01 09:54:38 +01:00
|
|
|
raise BaseException("{}: invalid type {} ".format(f, json_params["type"]))
|
2015-09-20 17:12:43 +02:00
|
|
|
|
2017-03-04 19:41:23 +01:00
|
|
|
if "variants" not in json_params:
|
2015-09-20 17:12:43 +02:00
|
|
|
# convert single variant shortcut to a expanded variant
|
|
|
|
v = {}
|
|
|
|
for i in json_params:
|
|
|
|
v[i] = json_params[i]
|
|
|
|
|
|
|
|
v["variant"] = json_params["name"]
|
|
|
|
v["variant_description"] = json_params["description"]
|
|
|
|
json_params["variants"] = [v]
|
|
|
|
|
2023-02-01 09:54:38 +01:00
|
|
|
name = '"{}"'.format(json_params["name"])
|
2015-09-20 17:12:43 +02:00
|
|
|
|
|
|
|
if json_params["type"] == "operator":
|
|
|
|
for v in json_params["variants"]:
|
2017-03-04 19:41:23 +01:00
|
|
|
if "arguments" not in v:
|
2021-05-07 23:52:59 +02:00
|
|
|
raise BaseException("%s: arguments expected for operator" % f)
|
2021-05-08 22:31:50 +02:00
|
|
|
a_list = list(deepcopy(v["arguments"]))
|
|
|
|
if not 1 <= len(a_list) <= 2:
|
|
|
|
raise BaseException(
|
|
|
|
"%s: 1 or 2 arguments expected for operator found %i"
|
|
|
|
% (f, len(a_list))
|
2024-11-29 14:26:30 +01:00
|
|
|
)
|
2015-09-20 17:12:43 +02:00
|
|
|
|
2023-04-11 11:10:54 +10:00
|
|
|
cpp.write(
|
|
|
|
'\n\n QgsExpression::functionHelpTexts().insert( QStringLiteral( {0} ),\n Help( QStringLiteral( {0} ), tr( "{1}" ), tr( "{2}" ),\n QList<HelpVariant>()'.format(
|
2015-09-20 17:12:43 +02:00
|
|
|
name, json_params["type"], json_params["description"]
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
for v in json_params["variants"]:
|
|
|
|
cpp.write(
|
2023-02-01 09:54:38 +01:00
|
|
|
'\n << HelpVariant( tr( "{}" ), tr( "{}" ),\n QList<HelpArg>()'.format(
|
|
|
|
v["variant"], v["variant_description"]
|
|
|
|
)
|
2024-11-29 14:26:30 +01:00
|
|
|
)
|
|
|
|
|
2015-09-20 17:12:43 +02:00
|
|
|
if "arguments" in v:
|
|
|
|
for a in v["arguments"]:
|
2023-02-01 09:54:38 +01:00
|
|
|
cpp.write(
|
|
|
|
'\n << HelpArg( QStringLiteral( "{}" ), tr( "{}" ), {}, {}, {}, {} )'.format(
|
2015-09-20 17:12:43 +02:00
|
|
|
a["arg"],
|
|
|
|
a.get("description", ""),
|
|
|
|
"true" if a.get("descOnly", False) else "false",
|
2016-03-29 16:22:04 +11:00
|
|
|
"true" if a.get("syntaxOnly", False) else "false",
|
|
|
|
"true" if a.get("optional", False) else "false",
|
2024-11-29 14:26:30 +01:00
|
|
|
(
|
2019-11-25 16:42:24 +10:00
|
|
|
'QStringLiteral( "{}" )'.format(a.get("default", ""))
|
|
|
|
if a.get("default", "")
|
|
|
|
else "QString()"
|
2024-11-29 14:26:30 +01:00
|
|
|
),
|
2018-05-10 19:59:12 +02:00
|
|
|
)
|
2015-09-20 17:12:43 +02:00
|
|
|
)
|
|
|
|
|
2023-02-01 09:54:38 +01:00
|
|
|
cpp.write(
|
|
|
|
",\n /* variableLenArguments */ {}".format(
|
2015-09-20 17:12:43 +02:00
|
|
|
"true" if v.get("variableLenArguments", False) else "false"
|
|
|
|
)
|
2024-11-29 14:26:30 +01:00
|
|
|
)
|
2019-11-25 16:42:24 +10:00
|
|
|
cpp.write(",\n QList<HelpExample>()")
|
2015-09-20 17:12:43 +02:00
|
|
|
|
|
|
|
if "examples" in v:
|
|
|
|
for e in v["examples"]:
|
2023-02-01 09:54:38 +01:00
|
|
|
cpp.write(
|
|
|
|
'\n << HelpExample( tr( "{}" ), tr( "{}" ), tr( "{}" ) )'.format(
|
2015-09-20 17:12:43 +02:00
|
|
|
e["expression"], e["returns"], e.get("note", "")
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
if "notes" in v:
|
2023-02-01 09:54:38 +01:00
|
|
|
cpp.write(',\n tr( "{}" )'.format(v["notes"]))
|
2019-12-03 14:48:59 +01:00
|
|
|
else:
|
|
|
|
cpp.write(",\n QString()")
|
|
|
|
|
2022-02-25 18:24:53 +01:00
|
|
|
cpp.write(",\n QStringList()")
|
2019-12-03 14:48:59 +01:00
|
|
|
if "tags" in v:
|
2023-02-01 09:54:38 +01:00
|
|
|
cpp.write('\n << tr( "{}" )'.format(",".join(v["tags"])))
|
2015-09-20 17:12:43 +02:00
|
|
|
|
2019-11-25 16:42:24 +10:00
|
|
|
cpp.write("\n )")
|
2015-09-20 17:12:43 +02:00
|
|
|
|
2019-11-25 16:42:24 +10:00
|
|
|
cpp.write("\n )")
|
|
|
|
cpp.write("\n );")
|
2015-09-20 17:12:43 +02:00
|
|
|
|
|
|
|
for f in sorted(glob.glob("resources/function_help/text/*")):
|
|
|
|
n = os.path.basename(f)
|
|
|
|
|
|
|
|
with open(f) as content:
|
2023-04-11 11:10:54 +10:00
|
|
|
cpp.write(
|
|
|
|
'\n\n QgsExpression::functionHelpTexts().insert( "{0}",\n Help( tr( "{0}" ), tr( "group" ), tr( "{1}" ), QList<HelpVariant>() ) );\n'.format(
|
2015-09-20 17:12:43 +02:00
|
|
|
n,
|
|
|
|
content.read()
|
|
|
|
.replace("\\", "\")
|
|
|
|
.replace("\\", "\\\\")
|
|
|
|
.replace('"', '\\"')
|
|
|
|
.replace("\n", "\\n"),
|
2024-11-29 14:26:30 +01:00
|
|
|
)
|
|
|
|
)
|
2015-09-20 17:12:43 +02:00
|
|
|
|
2019-11-25 16:42:24 +10:00
|
|
|
cpp.write("\n } );\n}\n")
|
2015-09-29 17:01:11 +02:00
|
|
|
cpp.close()
|