aboutsummaryrefslogtreecommitdiff
path: root/scripts/properties.py
blob: 790127c41b4b38a240d25ff0803be979525c0dc6 (plain)
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#!/usr/bin/env python3
import os
import re
import argparse
from collections import defaultdict


style_properties_type = {
    "LV_STYLE_BG_COLOR": "LV_PROPERTY_TYPE_COLOR",
    "LV_STYLE_BG_GRAD_COLOR": "LV_PROPERTY_TYPE_COLOR",
    "LV_STYLE_BG_IMAGE_SRC": "LV_PROPERTY_TYPE_IMGSRC",
    "LV_STYLE_BG_IMAGE_RECOLOR": "LV_PROPERTY_TYPE_COLOR",
    "LV_STYLE_BORDER_COLOR": "LV_PROPERTY_TYPE_COLOR",
    "LV_STYLE_OUTLINE_COLOR": "LV_PROPERTY_TYPE_COLOR",
    "LV_STYLE_SHADOW_COLOR": "LV_PROPERTY_TYPE_COLOR",
    "LV_STYLE_IMAGE_RECOLOR": "LV_PROPERTY_TYPE_COLOR",
    "LV_STYLE_ARCH_IMAGE_SRC": "LV_PROPERTY_TYPE_IMGSRC",
    "LV_STYLE_ARCH_COLOR": "LV_PROPERTY_TYPE_COLOR",
    "LV_STYLE_TEXT_COLOR": "LV_PROPERTY_TYPE_COLOR",
    "LV_STYLE_TEXT_FONT": "LV_PROPERTY_TYPE_FONT",
    "LV_STYLE_LINE_COLOR": "LV_PROPERTY_TYPE_COLOR",
}


class Property:
    def __init__(self, widget, name, type, index, id):
        self.widget = widget
        self.name = name
        self.type = type
        self.index = index
        self.id = id


def find_headers(directory):
    if os.path.isfile(directory):
        yield directory
        return

    for root, dirs, files in os.walk(directory):
        for file in files:
            if file.endswith('.h'):
                yield os.path.join(root, file)


def read_widget_properties(directory):

    def match_properties(file_path):
        pattern = r'^\s*LV_PROPERTY_ID\((\w+),\s*(\w+),\s*(\w+),\s*(\d+)\)'
        with open(file_path, 'r') as file:
            for line in file.readlines():
                match = re.match(pattern, line)
                if match:
                    id = f"LV_PROPERTY_{match.group(1).upper()}_{match.group(2).upper()}"
                    yield Property(
                        match.group(1).lower(),
                        match.group(2).lower(), match.group(3), match.group(4),
                        id)

    def match_styles(file_path):
        pattern = r'^\s+LV_STYLE_(\w+)\s*=\s*(\d+),'
        with open(file_path, 'r') as file:
            for line in file.readlines():
                match = re.match(pattern, line)
                if match:
                    name = match.group(1).upper()
                    id = f"LV_PROPERTY_STYLE_{name}"
                    yield Property("style",
                                   match.group(1).lower(), "style",
                                   match.group(2), id)

    properties_by_widget = defaultdict(list)
    for file_path in find_headers(directory):
        for property in match_properties(file_path):
            properties_by_widget[property.widget].append(property)

        for property in match_styles(file_path):
            properties_by_widget[property.widget].append(property)

        for widget, properties in properties_by_widget.items():
            # sort properties by property name
            properties.sort(key=lambda x: x.name)
            properties_by_widget[widget] = properties

    return properties_by_widget


def write_widget_properties(output, properties_by_widget):
    # Open header file for update.
    with open(f'{output}/lv_obj_property_names.h', "w") as header:
        header.write(f'''
/**
 * @file lv_obj_property_names.h
 * GENERATED FILE, DO NOT EDIT IT!
 */
#ifndef LV_OBJ_PROPERTY_NAMES_H
#define LV_OBJ_PROPERTY_NAMES_H

#include "../../misc/lv_types.h"

#if LV_USE_OBJ_PROPERTY && LV_USE_OBJ_PROPERTY_NAME

''')

        for widget in sorted(properties_by_widget.keys()):
            properties = properties_by_widget[widget]
            file_name = f'lv_{widget}_properties.c'
            output_file = f'{output}/{file_name}'

            count = len(properties)
            if widget == 'style':
                include = "lv_style_properties.h"
                guard = None
            elif widget == "obj":
                include = "../../core/lv_obj.h"
                guard = None
            else:
                include = f'../{widget}/lv_{widget}.h'
                guard = f"#if LV_USE_{widget.upper()}"

            with open(output_file, 'w') as f:
                f.write(f'''
/**
 * GENERATED FILE, DO NOT EDIT IT!
 * @file {file_name}
 */

#include "{include}"

#if LV_USE_OBJ_PROPERTY && LV_USE_OBJ_PROPERTY_NAME

{guard if guard else ""}
/**
 * {widget.capitalize() + ' widget' if widget != 'style' else 'Style'} property names, name must be in order.
 * Generated code from properties.py
 */
/* *INDENT-OFF* */
const lv_property_name_t lv_{widget}_property_names[{count}] = {{
''')

                for property in properties:
                    name = property.name
                    name_str = '"' + name + '",'
                    f.write(f"    {{{name_str :25} {property.id},}},\n")

                f.write('};\n')
                if guard:
                    f.write(f"#endif /*LV_USE_{widget.upper()}*/\n\n")
                f.write("/* *INDENT-ON* */\n")
                f.write('#endif\n')
            header.write(
                f'    extern const lv_property_name_t lv_{widget}_property_names[{count}];\n'
            )
        header.write('#endif\n')
        header.write('#endif\n')


def write_style_header(output, properties_by_widget):
    properties = properties_by_widget['style']

    output_file = f'{output}/lv_style_properties.h'

    with open(output_file, 'w') as f:
        f.write(f'''
/**
 * GENERATED FILE, DO NOT EDIT IT!
 * @file lv_style_properties.h
 */
#ifndef LV_STYLE_PROPERTIES_H
#define LV_STYLE_PROPERTIES_H

#include "../../core/lv_obj_property.h"
#if LV_USE_OBJ_PROPERTY


/* *INDENT-OFF* */
enum {{
''')

        for property in properties:
            name = property.name
            id_type = style_properties_type.get(f"LV_STYLE_{name.upper()}",
                                                "LV_PROPERTY_TYPE_INT")
            f.write(
                f"    LV_PROPERTY_ID(STYLE, {name.upper() + ',' :25} {id_type+',' :28} LV_STYLE_{name.upper()}),\n"
            )

        f.write('};\n\n')
        f.write('#endif\n')
        f.write('#endif\n')


def main(directory, output):
    property = read_widget_properties(directory)
    write_widget_properties(output, property)
    write_style_header(output, property)


if __name__ == "__main__":
    parser = argparse.ArgumentParser(
        description='Search files and filter lines.')
    parser.add_argument('-d', '--directory',
                        help='Directory to lvgl root path')
    parser.add_argument(
        '-o', '--output', help='Folders to write generated properties for all widgets.')
    args = parser.parse_args()

    # default directory is the lvgl root path of where this script sits
    if args.directory is None:
        args.directory = os.path.join(os.path.dirname(__file__), "../")

    if args.output is None:
        args.output = os.path.join(args.directory, "src/widgets/property/")

    # create output directory if it doesn't exist
    os.makedirs(args.output, exist_ok=True)

    main(args.directory, args.output)