summaryrefslogtreecommitdiff
path: root/convert-enum.py
blob: 0a1cae05998d0f436456b1c1d311b0469db02b59 (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
#!/usr/bin/env python3
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
#
#
# This is a simple script to convert C enum from documentation to the
# Dict that is used in the Python code
#
# Example input:
# ---------------
# typedef enum
# {
# BLACKONWHITE = 0x0001,
# WHITEONBLACK = 0x0002,
# COLORONCOLOR = 0x0003,
# HALFTONE = 0x0004
# } StretchMode;
# ---------------
#
# Example output:
# ---------------
# StretchMode = {
#     0x0001: "BLACKONWHITE",
#     0x0002: "WHITEONBLACK",
#     0x0003: "COLORONCOLOR",
#     0x0004: "HALFTONE",
# }
# ---------------

import sys

result = ""
for line in sys.stdin:
    word = line.split()
    if len(word) == 0 or word[0] == "{" or word[0] == "enum":
        continue
    elif word[0] == "}":
        name = word[1].replace(";", "")
        result = name + " = {\n" + result
    elif len(word) == 3:
        enum = word[2].replace(",", "")
        result += ("    " + enum + ": \"" + word[0] + "\",\n")
result += "}"
print(result)