summaryrefslogtreecommitdiffstats
path: root/src/common/options/y2c.py
blob: 0b64bec58ec3a26696c1ef9bc3bba7fac9cbd62f (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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
#!/usr/bin/env python3

import yaml
import argparse
import math
import os
import sys

# flake8: noqa: E127

def type_to_cxx(t):
    return f'Option::TYPE_{t.upper()}'


def level_to_cxx(lv):
    return f'Option::LEVEL_{lv.upper()}'


def eval_str(v):
    if v == "":
        return v
    v = v.strip('"').replace('"', '\\"')
    return f'"{v}"'


def eval_value(v, typ):
    try:
        if typ == 'str':
            return eval_str(v)
        if typ == 'float':
            return float(v)
        if typ in ('uint', 'int', 'size', 'secs', 'millisecs'):
            return int(v)
        if typ == 'bool':
            return 'true' if v else 'false'
        else:
            return f'"{v}"'
    except ValueError:
        times = dict(_min=60,
                     _hr=60*60,
                     _day=24*60*60,
                     _K=1 << 10,
                     _M=1 << 20,
                     _G=1 << 30,
                     _T=1 << 40)
        for unit, m in times.items():
            if v.endswith(unit):
                int(v[:-len(unit)])
                # user defined literals
                return v
        raise ValueError(f'unknown value: {v}')


def set_default(default, typ):
    if default is None:
        return ''
    v = eval_value(default, typ)
    return f'.set_default({v})\n'


def set_daemon_default(default, typ):
    if default is None:
        return ''
    v = eval_value(default, typ)
    return f'.set_daemon_default({v})\n'


def add_tags(tags):
    if tags is None:
        return ''
    cxx = ''
    for tag in tags:
        v = eval_str(tag)
        cxx += f'.add_tag({v})\n'
    return cxx


def add_services(services):
    if services is None:
        return ''
    if len(services) == 1:
        return f'.add_service("{services[0]}")\n'
    else:
        param = ', '.join(f'"{s}"' for s in services)
        return f'.add_service({{{param}}})\n'


def add_see_also(see_also):
    if see_also is None:
        return ''
    param = ', '.join(f'"{v}"' for v in see_also)
    return f'.add_see_also({{{param}}})\n'


def set_desc(desc):
    if desc is None:
        return ''
    v = eval_str(desc)
    return f'.set_description({v})\n'


def set_long_desc(desc):
    if desc is None:
        return ''
    v = eval_str(desc)
    return f'.set_long_description({v})\n'


def set_min_max(mi, ma, typ):
    if mi is None and ma is None:
        return ''
    if mi is not None and ma is not None:
        min_v = eval_value(mi, typ)
        max_v = eval_value(ma, typ)
        if isinstance(min_v, str) and isinstance(max_v, int):
            return f'.set_min_max({min_v}, {max_v}ULL)\n'
        elif isinstance(min_v, int) and isinstance(max_v, str):
            return f'.set_min_max({min_v}ULL, {max_v})\n'
        else:
            return f'.set_min_max({min_v}, {max_v})\n'
    if mi is not None:
        min_v = eval_value(mi, typ)
        return f'.set_min({min_v})\n'
    raise ValueError('set_max() is not implemented')


def set_enum_allowed(values):
    if values is None:
        return ''
    param = ', '.join(f'"{v}"' for v in values)
    return f'.set_enum_allowed({{{param}}})\n'


def add_flags(flags):
    if flags is None:
        return ''
    cxx = ''
    for flag in flags:
        cxx += f'.set_flag(Option::FLAG_{flag.upper()})\n'
    return cxx


def set_validator(validator):
    if validator is None:
        return ''
    validator = validator.rstrip()
    return f'.set_validator({validator})\n'


def add_verbatim(verbatim):
    if verbatim is None:
        return ''
    return verbatim + '\n'


def yaml_to_cxx(opt, indent):
    name = opt['name']
    typ = opt['type']
    ctyp = type_to_cxx(typ)
    level = level_to_cxx(opt['level'])
    cxx = f'Option("{name}", {ctyp}, {level})\n'
    cxx += set_desc(opt.get('desc'))
    cxx += set_long_desc(opt.get('long_desc'))
    cxx += set_default(opt.get('default'), typ)
    cxx += set_daemon_default(opt.get('daemon_default'), typ)
    cxx += set_min_max(opt.get('min'), opt.get('max'), typ)
    cxx += set_enum_allowed(opt.get('enum_values'))
    cxx += set_validator(opt.get('validator'))
    cxx += add_flags(opt.get('flags'))
    cxx += add_services(opt.get('services'))
    cxx += add_tags(opt.get('tags'))
    cxx += add_see_also(opt.get('see_also'))
    verbatim = add_verbatim(opt.get('verbatim'))
    cxx += verbatim
    if verbatim:
        cxx += '\n'
    else:
        cxx = cxx.rstrip()
    cxx += ',\n'
    if indent > 0:
        indented = []
        for line in cxx.split('\n'):
            if line:
                indented.append(' ' * indent + line + '\n')
        cxx = ''.join(indented)
    return cxx


def type_to_h(t):
    if t == 'uint':
        return 'OPT_U32'
    return f'OPT_{t.upper()}'


def yaml_to_h(opt):
    if opt.get('with_legacy', False):
        name = opt['name']
        typ = opt['type']
        htyp = type_to_h(typ)
        return f'OPTION({name}, {htyp})'
    else:
        return ''


TEMPLATE_CC = '''#include "common/options.h"
{headers}

std::vector<Option> get_{name}_options() {{
  return std::vector<Option>({{
@body@
  }});
}}
'''


# PyYAML doesn't check for duplicates even though the YAML spec says
# that mapping keys must be unique and that duplicates must be treated
# as an error.  See https://github.com/yaml/pyyaml/issues/165.
#
# This workaround breaks merge keys -- in "<<: *xyz", duplicate keys
# from xyz mapping raise an error instead of being discarded.
class UniqueKeySafeLoader(yaml.SafeLoader):
    def construct_mapping(self, node, deep=False):
        mapping = super().construct_mapping(node, deep)
        keys = set()
        for key_node, _ in node.value:
            key = self.construct_object(key_node, deep=deep)
            if key in keys:
                raise yaml.constructor.ConstructorError(None, None,
                                                        "found duplicate key",
                                                        key_node.start_mark)
            keys.add(key)
        return mapping


def translate(opts):
    if opts.raw:
        prelude, epilogue = '', ''
    else:
        prelude, epilogue = TEMPLATE_CC.split('@body@')

    if opts.name:
        name = opts.name
    else:
        name = os.path.split(opts.input)[-1]
        name = name.rsplit('.', 1)[0]
    name = name.replace('-', '_')
    # noqa: E127
    with open(opts.input) as infile, \
         open(opts.output, 'w') as cc_file, \
         open(opts.legacy, 'w') as h_file:
        yml = yaml.load(infile, Loader=UniqueKeySafeLoader)
        headers = yml.get('headers', '')
        cc_file.write(prelude.format(name=name, headers=headers))
        options = yml['options']
        for option in options:
            try:
                cc_file.write(yaml_to_cxx(option, opts.indent) + '\n')
                if option.get('with_legacy', False):
                    h_file.write(yaml_to_h(option) + '\n')
            except ValueError as e:
                print(f'failed to translate option "{name}": {e}',
                      file=sys.stderr)
                return 1
        cc_file.write(epilogue.replace("}}", "}"))


def readable_size(value, typ):
    times = dict(T=1 << 40,
                 G=1 << 30,
                 M=1 << 20,
                 K=1 << 10)
    if isinstance(value, str):
        value = value.strip('"')
    try:
        v = int(value)
        if v == 0:
            return 0
        for unit, m in times.items():
            if v % m == 0:
                v = int(v / m)
                return f'{v}_{unit}'
        return v
    except ValueError:
        return value


def readable_duration(value, typ):
    times = dict(day=24*60*60,
                 hr=60*60,
                 min=60)
    if isinstance(value, str):
        value = value.strip('"')
    try:
        v = float(value)
        if math.floor(v) != v:
            return v
        v = int(v)
        if v == 0:
            return 0
        for unit, m in times.items():
            if v % m == 0:
                v = int(v / m)
                return f'{v}_{unit}'
        return v
    except ValueError:
        return value


def readable_millisecs(value, typ):
    return int(value)


def readable(opts):
    with open(opts.input) as infile, open(opts.output, 'w') as outfile:
        yml = yaml.load(infile, Loader=UniqueKeySafeLoader)
        options = yml['options']
        for option in options:
            typ = option['type']
            if typ in ('size', 'uint'):
                do_readable = readable_size
            elif typ in ('float', 'int', 'secs'):
                do_readable = readable_duration
            elif typ == 'millisecs':
                do_readable = readable_millisecs
            else:
                continue
            for field in ['default', 'min', 'max', 'daemon_default']:
                v = option.get(field)
                if v is not None:
                    option[field] = do_readable(v, typ)
        yml['options'] = options
        yaml.dump(yml, outfile, sort_keys=False, indent=2)


def main():
    parser = argparse.ArgumentParser(
        formatter_class=argparse.ArgumentDefaultsHelpFormatter)
    parser.add_argument('-i', '--input', dest='input',
                        default='options.yaml',
                        help='the YAML file to be processed')
    parser.add_argument('-o', '--output', dest='output',
                        default='options',
                        help='the path to the generated .cc file')
    parser.add_argument('--legacy', dest='legacy',
                        default='legacy_options',
                        help='the path to the generated legacy .h file')
    parser.add_argument('--indent', type=int,
                        default=4,
                        help='the number of spaces added before each line')
    parser.add_argument('--name',
                        help='the name of the option group')
    parser.add_argument('--raw', action='store_true',
                        help='output the array without the full function')
    parser.add_argument('--op', choices=('readable', 'translate'),
                        default='translate',
                        help='operation to perform.')
    opts = parser.parse_args(sys.argv[1:])
    if opts.op == 'translate':
        translate(opts)
    else:
        readable(opts)


if __name__ == '__main__':
    main()