Merged pidgin/main into default
[pidgin-git.git] / libpurple / dbus-analyze-types.py
blob54f0b0774460f79393b155624d96917f7485f1ee
1 # This program takes a C header/source as the input and produces
3 # with --keyword=enum: the list of all enums
4 # with --keyword=struct: the list of all structs
6 # the output styles:
8 # --enum DBUS_POINTER_NAME1,
9 # DBUS_POINTER_NAME2,
10 # DBUS_POINTER_NAME3,
12 # --list NAME1
13 # NAME2
14 # NAME3
17 from __future__ import absolute_import, division, print_function
19 import argparse
20 import fileinput
21 import re
22 import sys
25 def toprint(match, line):
26 if args.verbatim:
27 return line
28 else:
29 return args.pattern % match
32 parser = argparse.ArgumentParser()
33 parser.add_argument('input', nargs='*',
34 help='Input files (or stdin if not specified)')
35 parser.add_argument('-o', '--output', type=argparse.FileType('w'),
36 help='Output to file instead of stdout')
37 parser.add_argument('--keyword', default='struct',
38 help='What keyword to search')
39 parser.add_argument('--pattern', default='%s',
40 help='String pattern used to print matches')
41 parser.add_argument('--verbatim', action='store_true',
42 help='Return full line of match instead of match itself')
43 args = parser.parse_args()
45 structregexp1 = re.compile(
46 r"^(typedef\s+)?%s\s+\w+\s+(\w+)\s*;" % (args.keyword, ))
47 structregexp2 = re.compile(r"^(typedef\s+)?%s" % (args.keyword, ))
48 structregexp3 = re.compile(r"^}\s+(\w+)\s*;")
50 print("/* Generated by %s. Do not edit! */" % (sys.argv[0], ),
51 file=args.output)
53 myinput = fileinput.input(args.input)
55 for line in myinput:
56 match = structregexp1.match(line)
57 if match is not None:
58 print(toprint(match.group(2), line), file=args.output)
59 continue
61 match = structregexp2.match(line)
62 if match is not None:
63 while True:
64 if args.verbatim:
65 print(line.rstrip(), file=args.output)
66 line = next(myinput)
67 match = structregexp3.match(line)
68 if match is not None:
69 print(toprint(match.group(1), line), file=args.output)
70 break
71 if line[0] not in " \t{\n":
72 if args.verbatim:
73 print(line, file=args.output)
74 break