summaryrefslogtreecommitdiff
path: root/scripts/qdebug-odebug.py
blob: 7a417e1a849fccb86fa345c24b28cd5b6f2de050 (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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#!/usr/bin/env python

"""
qdebug-odebug.py (C) 2004 Michael 'Mickey' Lauer <mickey@Vanille.de>
This script helps to convert from the Qt debugging framework to the Opie debugging framework
Though it will take care of the most common usages, it may not recognize uncommon ones, hence
manual work may be needed after applying the script.
"""

import sys, sre

qDebugExpression = sre.compile( '(?P<head>.*)(?P<statement>qDebug)\(\s*(?P<content>.*)\s*\);(?P<tail>.*)' )
qWarningExpression = sre.compile( '(?P<head>.*)(?P<statement>qWarning)\(\s*(?P<content>.*)\s*\);(?P<tail>.*)' )
qErrorExpression = sre.compile( '(?P<head>.*)(?P<statement>qError)\(\s*(?P<content>.*)\s*\);(?P<tail>.*)' )
qFatalExpression = sre.compile( '(?P<head>.*)(?P<statement>qFatal)\(\s*(?P<content>.*)\s*\);(?P<tail>.*)' )
#printfExpression = sre.compile( '(?P<head>.*)(?![s\.])(?P<statement>printf)\(\s*(?P<content>.*)\s*\);(?P<tail>.*)' )

debugTable = { "qDebug" : "odebug",
               "qWarning" : "owarn",
               "qError" : "oerr",
               "qFatal" : "ofatal",
               "printf" : "odebug" }

allExpressions = ( qDebugExpression, qWarningExpression, qErrorExpression, qFatalExpression ) #, printfExpression )

####################################################################################################

def convert( fname ):
    print >>sys.stderr, "<NOTE>: Dealing with %s..." % fname

    for line in file( fname ):
        match = False
        for expr in allExpressions:
            m = expr.match( line )
            if m is None:
                continue
            else:
                match = True

                head = m.groupdict()["head"]
                debug = m.groupdict()["statement"]
                content = m.groupdict()["content"]
                tail = m.groupdict()["tail"]

                print >>sys.stderr, "<NOTE>: Groups = ", m.groups()
                sys.stdout.write( head ) # don't strip() here, because we want to keep indentation
                sys.stdout.write( debugTable[debug.strip()] )
                sys.stdout.write( " << " )
                sys.stdout.write( transform( content ).strip() )
                sys.stdout.write( " << oendl; " )
                sys.stdout.write( tail )
                if not tail.endswith( "\n" ): sys.stdout.write( "\n" )
                continue
        # nothing applies
        if not match:
            sys.stdout.write( line )

####################################################################################################

def transform( s ):
    print >>sys.stderr, "<NOTE>: Transforming '%s'..." % s

    # check if there is one or more comma's outside of strings
    groups = []
    i = 0
    instring = False
    group = ""
    while i < len( s ):
        if s[i] == '"':
            instring = not instring
        elif s[i] == "," and not instring:
            groups.append( group.strip() )
            group = ""
            i += 1
            continue
        group += s[i]
        i += 1
        continue

    groups.append( group.strip() )

    # check for easy case
    if len( groups ) < 2: return s.replace( "%%", "%" )

    # damn. it gets complicated...
    print >>sys.stderr, "<NOTE>: Comma's outside of strings = %d" % ( len( groups ) -1 )

    formatstring, substitutions = groups[0], groups[1:]
    result = ""

    # iterator over formatstring and substitute format directives (e.g. '%d') with the substitutions
    subst = 0
    i = 0
    indirective = False
    while i < len( formatstring ):
        if formatstring[i] != "%":
            result += formatstring[i]
            i += 1
            continue
        else: # % in formatstring
            indirective = True
            i += 1
            while i < len( formatstring ) and formatstring[i] not in "%dDiouxXfFegEscpn":
                i += 1
            if formatstring[i] == "%":
                result += "%"
            else:
                result += '" << %s << "' % substitutions[0].replace( "(const char*)", "" ).replace( ".latin1()", "" )
                del substitutions[0]
            indirective = False
            i += 1

    print >>sys.stderr, "<NOTE>: Result seems to be '%s'" % result
    return result.replace( "%%", "%" )

####################################################################################################

if __name__ == "__main__":
    try:
        fname = sys.argv[1]
    except:
        print >>sys.stderr, "Usage: %s <filename>\n" % sys.argv[0]
    else:
        convert( fname )