Skip to content

Commit 245f975

Browse files
committed
Update jinja2 settings generator to work with latest NM settings
Also add `--regex-filter` option
1 parent d162905 commit 245f975

File tree

1 file changed

+57
-11
lines changed

1 file changed

+57
-11
lines changed

tools/generate-settings-dataclasses-jinja.py

Lines changed: 57 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,28 @@
11
#!/usr/bin/env python
22
# SPDX-License-Identifier: LGPL-2.1-or-later
3+
4+
# Gerating nm-settings-docs-dbus.xml from NetworkManager source code
5+
# 1. meson setup \
6+
# -Dselinux=false \
7+
# -Dqt=false \
8+
# -Dintrospection=true \
9+
# -Ddocs=true \
10+
# build
11+
# 2. cd build
12+
# 3. ninja man/nm-settings-docs-dbus.xml
313
from __future__ import annotations
414
from argparse import ArgumentParser
515
from pathlib import Path
616
from xml.etree.ElementTree import parse, Element
717

818
from typing import List, Optional
19+
from re import compile as regex_compile
20+
from re import Pattern
921
from jinja2 import Environment
1022
import builtins
1123
import keyword
24+
from textwrap import fill
25+
1226

1327
dbus_to_python_extra_typing_imports = {
1428
"as": ("List", ),
@@ -43,8 +57,11 @@
4357
'array of array of uint32': 'aau',
4458
'array of byte array': 'aay',
4559
'array of legacy IPv6 address struct': 'a(ayuay)',
60+
'array of legacy IPv6 address struct (a(ayuay))': 'a(ayuay)',
4661
'array of legacy IPv6 route struct': 'a(ayuayu)',
62+
'array of legacy IPv6 route struct (a(ayuayu))': 'a(ayuayu)',
4763
'array of string': 'as',
64+
'array of strings': 'as',
4865
'array of uint32': 'au',
4966
'array of vardict': 'aa{sv}',
5067
"array of 'a{sv}'": 'aa{sv}', # wireguard.peers uses this, fix NM upstream
@@ -110,19 +127,32 @@ def __init__(self, name: str, description: str, name_upper: str,
110127
self.properties: List[NmSettingPropertyIntrospection] = []
111128

112129

130+
def extract_and_format_option_description(node: Element) -> str:
131+
paragraphs: list[str] = []
132+
for para in node.iter('para'):
133+
paragraphs.append(fill(para.text, width=72))
134+
135+
return '\n\n'.join(paragraphs)
136+
137+
113138
def convert_property(node: Element,
114139
parent: NmSettingsIntrospection
115-
) -> NmSettingPropertyIntrospection:
116-
options = node.attrib
140+
) -> Optional[NmSettingPropertyIntrospection]:
141+
options = node.attrib.copy()
142+
143+
try:
144+
unconverted_type = options.pop('type')
145+
except KeyError:
146+
return None
117147

118-
unconverted_type = options.pop('type')
119148
try:
120149
dbus_type = dbus_name_type_map[unconverted_type]
121150
except KeyError:
122151
dbus_type = dbus_name_type_map[unconverted_type.split('(')[1][:-1]]
123152

124153
options['dbus_type'] = dbus_type
125154
options['python_type'] = dbus_to_python_type_map[dbus_type]
155+
options['description'] = extract_and_format_option_description(node)
126156

127157
return NmSettingPropertyIntrospection(**options, parent=parent)
128158

@@ -131,9 +161,11 @@ def generate_introspection(root: Element) -> List[NmSettingsIntrospection]:
131161
settings_introspection: List[NmSettingsIntrospection] = []
132162
for setting_node in root:
133163
setting = NmSettingsIntrospection(**setting_node.attrib)
134-
setting.properties.extend(
135-
(convert_property(x, setting) for x in setting_node)
136-
)
164+
165+
for x in setting_node:
166+
new_property = convert_property(x, setting)
167+
if new_property is not None:
168+
setting.properties.append(new_property)
137169

138170
settings_introspection.append(setting)
139171

@@ -167,24 +199,38 @@ class {{ setting.python_class_name }}(NetworkManagerSettingsMixin):
167199
settings_template = jinja_env.from_string(setttngs_template_str)
168200

169201

170-
def main(settings_xml_path: Path) -> None:
202+
def main(
203+
settings_xml_path: Path,
204+
regex_filter: Optional[Pattern] = None,
205+
) -> None:
171206
tree = parse(settings_xml_path)
172207
introspection = generate_introspection(tree.getroot())
173208

174209
settings_dir = Path('sdbus_async/networkmanager/settings/')
175210
for setting in introspection:
176-
setting_py_file = settings_dir / (setting.name_upper.lower() + '.py')
211+
setting_sake_name = setting.name_upper.lower()
212+
213+
if regex_filter is not None:
214+
if not regex_filter.match(setting_sake_name):
215+
continue
216+
217+
setting_py_file = settings_dir / (setting_sake_name + '.py')
177218
with open(setting_py_file, mode='w') as f:
178219
f.write(settings_template.render(setting=setting))
179220

180221

181222
if __name__ == '__main__':
182223
arg_parser = ArgumentParser()
183224
arg_parser.add_argument(
184-
'nm_settings_xml',
225+
'settings_xml_path',
185226
type=Path,
186-
default=Path('man/nm-settings-docs-dbus.xml'),
227+
default=Path('./nm-settings-docs-dbus.xml'),
187228
)
229+
arg_parser.add_argument(
230+
'--regex-filter',
231+
type=regex_compile,
232+
)
233+
188234
args = arg_parser.parse_args()
189235

190-
main(args.nm_settings_xml)
236+
main(**vars(args))

0 commit comments

Comments
 (0)