summaryrefslogtreecommitdiff
path: root/tools/grilo-inspect/generate_core_keys.py
blob: 867a55a4df73bf67f19268796d39b560daea3d7c (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
#
# generate_core_keys.py
#
# Author: Juan A. Suarez Romero <jasuarez@igalia.com>
#
# Copyright (C) 2016 Igalia S.L.
#
# Generates a .c file containing an array with the core keys
#

import re
import sys


COMMENT_RE = re.compile(
    r'''
    //.*?$ | /\*.*?\*/ | \'(?:\\. | [^\\\'])*\' | "(?:\\. | [^\\"])*"
    ''',
    re.DOTALL | re.MULTILINE)


# From https://stackoverflow.com/a/241506
def comment_remover(text):
    def replacer(match):
        s = match.group(0)
        if s.startswith('/'):
            return " "  # note: a space and not an empty string
        else:
            return s
    return re.sub(COMMENT_RE, replacer, text)


if len(sys.argv) != 3:
    print("Usage: " + sys.argv[0] + " <input> <output>")
    exit(1)

with open(sys.argv[1], "r") as finput:
    input_data = finput.read()
    input_data = comment_remover(input_data).split('\n')

with open(sys.argv[2], "w") as foutput:
    foutput.write("gchar *grl_core_keys[] = {\n")

    output_keys = False
    for line in input_data:
        if re.search("GRL_METADATA_KEY_ALBUM", line):
            output_keys = True
        if re.search("G_BEGIN_DECLS", line):
            output_keys = False
        m = re.search("GRL_METADATA_KEY_[^ ]+", line)
        if output_keys and m:
            foutput.write('"' + m.group(0) + '",\n')

    foutput.write("};")