Debian Codegraph @

demos/gtk-demo/geninclude.py

raw
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3
4 import sys
5 import re
6 import os
7 from collections import *
8
9 out_file = sys.argv[1]
10 in_files = sys.argv[2:]
11
12
13 file_output = """
14 typedef GtkWidget *(*GDoDemoFunc) (GtkWidget *do_widget);
15
16 typedef struct _Demo Demo;
17
18 struct _Demo
19 {
20 gchar *name;
21 gchar *title;
22 gchar *filename;
23 GDoDemoFunc func;
24 Demo *children;
25 };
26
27 """
28
29 # Demo = namedtuple('Demo', ['name', 'title', 'file', 'func'])
30
31 demos = []
32
33 for demo_file in in_files:
34 filename = demo_file[demo_file.rfind('/')+1:]
35 demo_name = filename.replace(".c", "")
36 with open(demo_file, 'r', encoding='utf-8') as f:
37 title = f.readline().replace("/*", "").strip()
38
39
40 file_output += "GtkWidget *do_" + demo_name + " (GtkWidget *do_widget);\n"
41 # demos += Demo(name = demo_name,
42 # title = title,
43 # file = demo_file,
44 # func = "do_" + title)
45 demos.append((demo_name, title, filename, "do_" + demo_name, -1))
46
47 # Generate a List of "Parent names"
48 parents = []
49 parent_ids = []
50 parent_index = 0
51 for demo in demos:
52 if "/" in demo[1]:
53 slash_index = demo[1].index('/')
54 parent_name = demo[1][:slash_index]
55 do_break = False
56
57 # Check for duplicates
58 if not parent_name in parents:
59 parents.append(parent_name)
60 parent_ids.append(parent_index)
61 demos.append(("NULL", parent_name, "NULL", "NULL", parent_index))
62
63 parent_index = parent_index + 1
64
65
66 # For every child with a parent, generate a list of child demos
67 i = 0
68 for parent in parents:
69 id = parent_ids[i]
70 file_output += "\nDemo child" + str(id) + "[] = {\n"
71 # iterate over all demos and check if the name starts with the given parent name
72 for child in demos:
73 if child[1].startswith(parent + "/"):
74 title = child[1][child[1].rfind('/') + 1:]
75 file_output += " { \"" + child[0] + "\", \"" + title + "\", \"" + child[2] + "\", " + child[3] + ", NULL },\n"
76
77
78 file_output += " { NULL }\n};\n"
79 i = i + 1
80
81
82 # Sort demos by title
83 demos = sorted(demos, key=lambda x: x[1])
84
85 file_output += "\nDemo gtk_demos[] = {\n"
86 for demo in demos:
87 # Do not generate one of these for demos with a parent demo
88 if "/" not in demo[1]:
89 child_array = "NULL"
90 name = demo[0];
91 title = demo[1];
92 file = demo[2]
93 if name != "NULL":
94 name = "\"" + name + "\""
95 if title != "NULL":
96 title = "\"" + title + "\""
97 if file != "NULL":
98 file = "\"" + file + "\""
99
100 if demo[4] != -1:
101 child_array = "child" + str(demo[4])
102 file_output += " { " + name + ", " + title + ", " + file + ", " + demo[3] + ", " + child_array + " },\n"
103
104 file_output += " { NULL }\n};\n"
105
106 ofile = open(out_file, "w", encoding="utf-8")
107 ofile.write(file_output)
108 ofile.close()