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
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
94
95
96
97
98
|
#!/usr/bin/env python3
# Reads all .ui files and creates builder.pyi
# Excecute this script from the repo root dir
from io import TextIOWrapper
from pathlib import Path
from xml.etree import ElementTree as ET
cwd = Path.cwd()
if cwd.name != 'gajim':
exit('Script needs to be excecuted from gajim repository root directory')
in_path = cwd / 'gajim' / 'data' / 'gui'
out_path = cwd / 'gajim' / 'gtk' / 'builder.pyi'
paths = list(in_path.iterdir())
paths.sort()
IMPORTS = '''
from typing import Literal
from typing import overload
from gi.repository import Atk
from gi.repository import Gtk
from gi.repository import GtkSource
class Builder(Gtk.Builder): ...
'''
CLASS_DEF = '\nclass %s(Builder):'
ATTR = '\n %s: %s'
GET_BUILDER_OVERLOAD = '''
@overload
def get_builder(file_name: Literal['%s'], widgets: list[str] = ...) -> %s: ...''' # noqa
GET_BUILDER = '''
def get_builder(file_name: str, widgets: list[str] = ...) -> Builder: ...'''
def make_class_name(path: Path) -> str:
name = path.name.removesuffix('.ui')
names = name.split('_')
names = map(lambda x: x.capitalize(), names)
return ''.join(names) + 'Builder'
def parse(path: Path, file: TextIOWrapper) -> str:
print('read', path)
lines: list[str] = []
tree = ET.parse(path)
for node in tree.iter(tag='object'):
id_ = node.attrib.get('id')
if id_ is None:
continue
klass = node.attrib['class']
if klass.startswith('GtkSource'):
klass = f'GtkSource.{klass.removeprefix("GtkSource")}'
elif klass.startswith('Atk'):
klass = f'Atk.{klass.removeprefix("Atk")}'
else:
klass = f'Gtk.{klass.removeprefix("Gtk")}'
lines.append(ATTR % (id_.replace('-', '_'), klass))
klass_name = make_class_name(path)
file.write(CLASS_DEF % klass_name)
if not lines:
file.write('\n pass')
else:
for line in lines:
file.write(line)
file.write('\n\n')
return klass_name
builder_names: list[tuple[str, str]] = []
with out_path.open(mode='w', encoding='utf8') as file:
file.write(IMPORTS)
for path in paths:
if path.name.endswith('~'):
continue
if path.name.startswith('#'):
continue
name = parse(path, file)
builder_names.append((name, path.name))
for name, file_name in builder_names:
file.write(GET_BUILDER_OVERLOAD % (file_name, name))
file.write(GET_BUILDER)
file.write('\n')
|