libpurple/dbus-analyze-types.py

Thu, 01 Sep 2016 23:42:21 -0400

author
Elliott Sales de Andrade <qulogic@pidgin.im>
date
Thu, 01 Sep 2016 23:42:21 -0400
changeset 37983
7d134a4a87b2
parent 37936
65e477a02dd6
child 37984
6a5ca046a90d
permissions
-rw-r--r--

Ensure all Python scripts are Py3k compatible.

# This program takes a C header/source as the input and produces
#
# with --keyword=enum: the list of all enums
# with --keyword=struct: the list of all structs
#
# the output styles:
#
# --enum    DBUS_POINTER_NAME1,
#           DBUS_POINTER_NAME2,
#           DBUS_POINTER_NAME3,
#
# --list    NAME1
#           NAME2
#           NAME3
#

from __future__ import absolute_import, division, print_function

import argparse
import fileinput
import re
import sys

def toprint(match, line):
    if args.verbatim:
        return line
    else:
        return args.pattern % match

parser = argparse.ArgumentParser()
parser.add_argument('input', nargs='*',
                    help='Input files (or stdin if not specified)')
parser.add_argument('-o', '--output', type=argparse.FileType('w'),
                    help='Output to file instead of stdout')
parser.add_argument('--keyword', default='struct',
                    help='What keyword to search')
parser.add_argument('--pattern', default='%s',
                    help='String pattern used to print matches')
parser.add_argument('--verbatim', action='store_true',
                    help='Return full line of match instead of match itself')
args = parser.parse_args()

structregexp1 = re.compile(r"^(typedef\s+)?%s\s+\w+\s+(\w+)\s*;" % args.keyword)
structregexp2 = re.compile(r"^(typedef\s+)?%s" % args.keyword)
structregexp3 = re.compile(r"^}\s+(\w+)\s*;")

print("/* Generated by %s.  Do not edit! */" % sys.argv[0],
      file=args.output)

myinput = fileinput.input(args.input)

for line in myinput:
    match = structregexp1.match(line)
    if match is not None:
        print(toprint(match.group(2), line), file=args.output)
        continue

    match = structregexp2.match(line)
    if match is not None:
        while True:
            if args.verbatim:
                print(line.rstrip(), file=args.output)
            line = next(myinput)
            match = structregexp3.match(line)
            if match is not None:
                print(toprint(match.group(1), line), file=args.output)
                break
            if line[0] not in [" ", "\t", "{", "\n"]:
                if args.verbatim:
                    print(line, file=args.output)
                break

mercurial