Make a branch to make krunner Good Enough For Aaron™.
[kdebase/uwolfer.git] / workspace / kscreensaver / libkscreensaver / kscreensaver-kde3to4-porting.py
blobeddea23bfd8ceb2bfee0784ebcfbbe2c5be0a4e2
1 #! /usr/bin/env python
3 # Copyright David Faure <faure@kde.org>, License LGPL v2
5 # This script converts a kde3 screensaver to kde4, adapting it to the change
6 # of API in libkscreensaver.
8 # If the input file could be parsed correctly (i.e. it had extern "C" and kss_* symbols),
9 # then this script will *overwrite* it with the result of the conversion.
10 # Make backups first, if you don't use a version control system.
12 # Usage: change those two lines before running the script
14 filename = 'pendulum.cpp'
15 savername = 'KPendulumSaver' # Interface is appended to this string.
17 import string,re,os
18 f = file(filename, 'r')
19 data = f.read()
20 lines = data.split('\n')
21 externCRe = re.compile('^\s*extern "C"')
22 appNameRe = re.compile('.*kss_applicationName\s*=\s*(".*")')
23 descriptionRe = re.compile('.*kss_description\s*=\s*(.*);')
24 versionRe = re.compile('.*kss_version\s*=\s*(.*);')
25 inExternC = 0
26 braceLevel = 0
27 aboutDataWritten = 0
28 appName = ''
29 description = ''
30 version = ''
31 outputlines = []
32 for line in lines:
33 if (line[0:2] == '//'):
34 outputlines.append(line)
35 continue
36 if not inExternC:
37 if (externCRe.match(line)):
38 inExternC = 1
39 if (line.find('{') >= 0):
40 braceLevel = braceLevel+1
41 outputlines.append(line.replace('extern "C"', 'class ' + savername + 'Interface : public KScreenSaverInterface'))
42 else:
43 outputlines.append(line)
44 else:
45 if (line.find('{') >= 0):
46 braceLevel = braceLevel+1
47 if (line.find('}') >= 0):
48 braceLevel = braceLevel-1
49 match = appNameRe.match(line)
50 if match:
51 appName = match.group(1)
52 line = ''
53 match = descriptionRe.match(line)
54 if match:
55 description = match.group(1)
56 line = ''
57 match = versionRe.match(line)
58 if match:
59 version = match.group(1)
60 line = ''
61 if appName and description and version and not aboutDataWritten:
62 outputlines.append( "public:" )
63 outputlines.append( " virtual KAboutData* aboutData() {" )
64 outputlines.append( " return new KAboutData( " + appName + ", " + description + ", " + version + ", " + description + " );" )
65 outputlines.append( " }" )
66 aboutDataWritten = 1
68 line = re.sub('KDE_EXPORT\s*','',line)
69 line = re.sub('KScreenSaver\s*\*\s*kss_create','virtual KScreenSaver* create',line)
70 line = re.sub('QDialog\s*\*\s*kss_setup','virtual QDialog* setup',line)
71 if braceLevel == 0:
72 outputlines.append( '};' )
73 outputlines.append( '' )
74 outputlines.append( 'int main( int argc, char *argv[] )' )
75 outputlines.append( '{' )
76 outputlines.append( ' ' + savername + 'Interface kss;' )
77 outputlines.append( ' return kScreenSaverMain( argc, argv, kss );' )
78 outputlines.append( '}' )
79 inExternC = 0
80 else:
81 outputlines.append( line )
83 if not aboutDataWritten:
84 print "PARSE ERROR"
85 print 'appName=' + appName
86 print 'description=' + description
87 print 'version=' + version
88 else:
89 open(filename,"w").write(string.join(outputlines, '\n'))
90 os.system('svn di ' + filename)