summaryrefslogtreecommitdiff
path: root/tools/generate-docs-nm-property-infos.py
diff options
context:
space:
mode:
authorWen Liang <liangwen12year@gmail.com>2021-04-22 18:41:50 -0400
committerThomas Haller <thaller@redhat.com>2021-05-05 15:28:17 +0200
commit86dcb31ab36dafbf512c45c800f22b7db8c13440 (patch)
tree282e8c60407ecef34bae40a6a014ed00a91e38ef /tools/generate-docs-nm-property-infos.py
parent8232c3473f93a87e2eb736bf4b63049001a20926 (diff)
downloadNetworkManager-86dcb31ab36dafbf512c45c800f22b7db8c13440.tar.gz
build: replace `./tools/generate-docs-nm-property-infos.pl` with python script
In order to have more structured settings in man page and make it more manageable to generate the docbook, it is recommended to use python script to replace `./tools/generate-docs-nm-property-infos.pl` (this tool is used to parse the comment section starting with `---nmcli---`, `---dbus---`, `---keyfile---`, `---ifcfg-rh---`). Signed-off-by: Wen Liang <liangwen12year@gmail.com> https://gitlab.freedesktop.org/NetworkManager/NetworkManager/-/merge_requests/824
Diffstat (limited to 'tools/generate-docs-nm-property-infos.py')
-rwxr-xr-xtools/generate-docs-nm-property-infos.py146
1 files changed, 146 insertions, 0 deletions
diff --git a/tools/generate-docs-nm-property-infos.py b/tools/generate-docs-nm-property-infos.py
new file mode 100755
index 0000000000..90f757728e
--- /dev/null
+++ b/tools/generate-docs-nm-property-infos.py
@@ -0,0 +1,146 @@
+#!/usr/bin/env python
+# SPDX-License-Identifier: LGPL-2.1-or-later
+
+import os, re, sys
+
+
+def get_setting_name(one_file):
+ setting_name = ""
+ assert re.match(r".*/libnm-core-impl/nm-setting-.*\.c$", one_file)
+ header_path = one_file.replace("libnm-core-impl", "libnm-core-public")
+ header_path = header_path.replace(".c", ".h")
+ try:
+ header_reader = open(header_path, "r")
+ except OSError:
+ print("Can not open header file: %s" % (header_path))
+ exit(1)
+
+ line = header_reader.readline()
+ while line != "":
+ setting_name_found = re.search(r"NM_SETTING_.+SETTING_NAME\s+\"(\S+)\"", line)
+ if setting_name_found:
+ setting_name = setting_name_found.group(1)
+ break
+ line = header_reader.readline()
+ header_reader.close()
+ return setting_name
+
+
+def scan_doc_comments(plugin, outfile, file, start_tag, end_tag):
+ data = []
+ push_flag = 0
+ try:
+ file_reader = open(file, "r")
+ except OSError:
+ print("Can not open file: %s" % (file))
+ exit(1)
+
+ line = file_reader.readline()
+ while line != "":
+ if start_tag in line:
+ push_flag = 1
+ elif end_tag in line and push_flag == 1:
+ push_flag = 0
+ parsed_data = process_data(data)
+ if parsed_data:
+ write_data(outfile, parsed_data)
+ data = []
+ elif push_flag == 1:
+ data.append(line)
+ line = file_reader.readline()
+ file_reader.close()
+ return
+
+
+def process_data(data):
+ parsed_data = {}
+ if not data:
+ return parsed_data
+ keywords = [
+ "property",
+ "variable",
+ "format",
+ "values",
+ "default",
+ "example",
+ "description",
+ ]
+ kwd_pat = "|".join(keywords)
+ keyword = ""
+ for line in data:
+ kwd_first_line_found = re.search(
+ r"^\s*\**\s+({}):\s+(.*?)\s*$".format(kwd_pat), line
+ )
+ kwd_more_line_found = re.search(r"^\s*\**\s+(.*?)\s*$", line)
+ if kwd_first_line_found:
+ keyword = kwd_first_line_found.group(1)
+ value = kwd_first_line_found.group(2) + "\n"
+ parsed_data[keyword] = escape_xml_char(value)
+ elif kwd_more_line_found:
+ if not keyword:
+ print("Extra mess in a comment: %s" % (line))
+ exit(1)
+ else:
+ value = kwd_more_line_found.group(1) + "\n"
+ parsed_data[keyword] += escape_xml_char(value)
+ for keyword in keywords:
+ if keyword == "variable" and keyword not in parsed_data:
+ parsed_data[keyword] = parsed_data["property"]
+ elif keyword not in parsed_data:
+ parsed_data[keyword] = ""
+ for key in parsed_data.keys():
+ parsed_data[key] = parsed_data[key].rstrip()
+ return parsed_data
+
+
+def write_data(outfile, parsed_data):
+ outfile.write(
+ '<property name="{0}" variable="{1}" format="{2}" values="{3}" default="{4}" example="{5}" description="{6}"/>\n'.format(
+ parsed_data["property"],
+ parsed_data["variable"],
+ parsed_data["format"],
+ parsed_data["values"],
+ parsed_data["default"],
+ parsed_data["example"],
+ parsed_data["description"],
+ )
+ )
+
+
+def escape_xml_char(text):
+ text = text.replace("&", "&amp;")
+ text = text.replace("<", "&lt;")
+ text = text.replace(">", "&gt;")
+ text = text.replace('"', "&quot;")
+ text = text.replace("'", "&apos;")
+
+ return text
+
+
+if len(sys.argv) < 4:
+ print("Usage: %s [plugin] [output-xml-file] [srcfiles]" % (sys.argv[0]))
+ exit(1)
+
+argv = list(sys.argv[1:])
+plugin, output, source_files = argv[0], argv[1], argv[2:]
+start_tag = "---" + plugin + "---"
+end_tag = "---end---"
+outfile = open(output, mode="w")
+
+# write XML header
+outfile.write("<nm-setting-docs>\n")
+outfile.write(" ")
+
+for one_file in source_files:
+ setting_name = get_setting_name(one_file)
+ if setting_name:
+ outfile.write('<setting name="' + setting_name + '">\n')
+ scan_doc_comments(plugin, outfile, one_file, start_tag, end_tag)
+ outfile.write("</setting>\n")
+
+
+# write XML footer
+outfile.write("</nm-setting-docs>")
+
+# close output file
+outfile.close()