|
| 1 | +#!/usr/bin/env python3 |
| 2 | + |
| 3 | +import random |
| 4 | +import string |
| 5 | +import argparse |
| 6 | + |
| 7 | +SIMPLE_TYPES = [ |
| 8 | + "bool", |
| 9 | + "uint8_t", |
| 10 | + "uint16_t", |
| 11 | + "uint32_t", |
| 12 | + "uint64_t", |
| 13 | + "int8_t", |
| 14 | + "int16_t", |
| 15 | + "int32_t", |
| 16 | + "int64_t", |
| 17 | + "std::string", |
| 18 | +] |
| 19 | + |
| 20 | +COMPLEX_TYPES = [ |
| 21 | + "std::vector", |
| 22 | + "std::map", |
| 23 | + "boost::optional", |
| 24 | + # "std::array", # not supported yet |
| 25 | +] |
| 26 | + |
| 27 | +ALL_TYPES = SIMPLE_TYPES + COMPLEX_TYPES |
| 28 | + |
| 29 | +def generate_random_type(): |
| 30 | + def _tvector(): |
| 31 | + inner = random.choice(SIMPLE_TYPES) |
| 32 | + return f"std::vector<{inner}>" |
| 33 | + |
| 34 | + def _tmap(): |
| 35 | + inner = random.choice(SIMPLE_TYPES) |
| 36 | + return f"std::map<std::string, {inner}>" |
| 37 | + |
| 38 | + def _toptional(): |
| 39 | + inner = random.choice(SIMPLE_TYPES) |
| 40 | + return f"boost::optional<{inner}>" |
| 41 | + |
| 42 | + def _tarray(): |
| 43 | + inner = random.choice(SIMPLE_TYPES) |
| 44 | + length = random.randint(2, 24) |
| 45 | + return f"std::array<{inner}, {length}>" |
| 46 | + |
| 47 | + mapper = { |
| 48 | + "std::vector": _tvector, |
| 49 | + "std::map": _tmap, |
| 50 | + "boost::optional": _toptional, |
| 51 | + } |
| 52 | + |
| 53 | + result = random.choice(ALL_TYPES) |
| 54 | + if result in COMPLEX_TYPES: |
| 55 | + return mapper[result]() |
| 56 | + return result |
| 57 | + |
| 58 | +def generate_random_word(): |
| 59 | + letters = string.ascii_letters |
| 60 | + return ''.join(random.choice(letters) for _ in range(20)) |
| 61 | + |
| 62 | +TEMPLATE = string.Template(""" |
| 63 | +#pragma once |
| 64 | +
|
| 65 | +struct Large : public fable::Confable { |
| 66 | + $variable_lines |
| 67 | +
|
| 68 | + CONFABLE_SCHEMA(Large) { |
| 69 | + using namespace fable; |
| 70 | + return Schema{ |
| 71 | + $schema_lines |
| 72 | + }; |
| 73 | + } |
| 74 | +}; |
| 75 | +""") |
| 76 | + |
| 77 | +def generate(count) -> str: |
| 78 | + variable_lines = [] |
| 79 | + schema_lines = [] |
| 80 | + for i in range(count): |
| 81 | + random_word = generate_random_word() |
| 82 | + variable_lines.append(f"{generate_random_type()} v{i};") |
| 83 | + schema_lines.append('{{"{0}", make_schema(&v{1}, "")}},'.format(random_word, i)) |
| 84 | + result = TEMPLATE.substitute({ |
| 85 | + "variable_lines": "\n ".join(variable_lines), |
| 86 | + "schema_lines": "\n ".join(schema_lines), |
| 87 | + }) |
| 88 | + return result |
| 89 | + |
| 90 | +def main(): |
| 91 | + parser = argparse.ArgumentParser() |
| 92 | + parser.add_argument("count", default=10, type=int) |
| 93 | + parser.add_argument("output", default="-", type=str) |
| 94 | + args = parser.parse_args() |
| 95 | + |
| 96 | + result = generate(args.count) |
| 97 | + if args.output == "-": |
| 98 | + print(result) |
| 99 | + else: |
| 100 | + with open(args.output, "w") as file: |
| 101 | + file.write(result) |
| 102 | + |
| 103 | +if __name__ == "__main__": |
| 104 | + main() |
0 commit comments