libpurple/dbus-analyze-types.py

changeset 37936
65e477a02dd6
parent 33932
c49bfe98716d
child 37983
7d134a4a87b2
equal deleted inserted replaced
37935:fe98e3815bc4 37936:65e477a02dd6
13 # NAME2 13 # NAME2
14 # NAME3 14 # NAME3
15 # 15 #
16 16
17 from __future__ import print_function 17 from __future__ import print_function
18
19 import argparse
20 import fileinput
18 import re 21 import re
19 import sys 22 import sys
20 23
21 options = {}
22
23 def toprint(match, line): 24 def toprint(match, line):
24 if verbatim: 25 if args.verbatim:
25 return line 26 return line
26 else: 27 else:
27 return pattern % match 28 return args.pattern % match
28 29
29 for arg in sys.argv[1:]: 30 parser = argparse.ArgumentParser()
30 if arg[0:2] == "--": 31 parser.add_argument('input', nargs='*',
31 mylist = arg[2:].split("=",1) 32 help='Input files (or stdin if not specified)')
32 command = mylist[0] 33 parser.add_argument('-o', '--output', type=argparse.FileType('w'),
33 if len(mylist) > 1: 34 help='Output to file instead of stdout')
34 options[command] = mylist[1] 35 parser.add_argument('--keyword', default='struct',
35 else: 36 help='What keyword to search')
36 options[command] = None 37 parser.add_argument('--pattern', default='%s',
38 help='String pattern used to print matches')
39 parser.add_argument('--verbatim', action='store_true',
40 help='Return full line of match instead of match itself')
41 args = parser.parse_args()
37 42
38 keyword = options.get("keyword", "struct") 43 structregexp1 = re.compile(r"^(typedef\s+)?%s\s+\w+\s+(\w+)\s*;" % args.keyword)
39 pattern = options.get("pattern", "%s") 44 structregexp2 = re.compile(r"^(typedef\s+)?%s" % args.keyword)
40 verbatim = "verbatim" in options
41
42 structregexp1 = re.compile(r"^(typedef\s+)?%s\s+\w+\s+(\w+)\s*;" % keyword)
43 structregexp2 = re.compile(r"^(typedef\s+)?%s" % keyword)
44 structregexp3 = re.compile(r"^}\s+(\w+)\s*;") 45 structregexp3 = re.compile(r"^}\s+(\w+)\s*;")
45 46
46 print("/* Generated by %s. Do not edit! */" % sys.argv[0]) 47 print("/* Generated by %s. Do not edit! */" % sys.argv[0],
48 file=args.output)
47 49
48 myinput = iter(sys.stdin) 50 myinput = fileinput.input(args.input)
49 51
50 for line in myinput: 52 for line in myinput:
51 match = structregexp1.match(line) 53 match = structregexp1.match(line)
52 if match is not None: 54 if match is not None:
53 print(toprint(match.group(2), line)) 55 print(toprint(match.group(2), line), file=args.output)
54 continue 56 continue
55 57
56 match = structregexp2.match(line) 58 match = structregexp2.match(line)
57 if match is not None: 59 if match is not None:
58 while True: 60 while True:
59 if verbatim: 61 if args.verbatim:
60 print(line.rstrip()) 62 print(line.rstrip(), file=args.output)
61 line = next(myinput) 63 line = next(myinput)
62 match = structregexp3.match(line) 64 match = structregexp3.match(line)
63 if match is not None: 65 if match is not None:
64 print(toprint(match.group(1), line)) 66 print(toprint(match.group(1), line), file=args.output)
65 break 67 break
66 if line[0] not in [" ", "\t", "{", "\n"]: 68 if line[0] not in [" ", "\t", "{", "\n"]:
67 if verbatim: 69 if args.verbatim:
68 print(line) 70 print(line, file=args.output)
69 break 71 break

mercurial