|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +help = """Script helps to convert C/C++ sources to C/C++ -like Python sources. |
| 4 | +
|
| 5 | +It does few edit operations, for convert C/C++ to Python. |
| 6 | +After it you must edit code manually, but, probably you would spend less time for it. |
| 7 | +
|
| 8 | +Utility Will make mistaces and Will not generate ready for use code, so, it won't help you, |
| 9 | +if you don't know C/C++ and Python |
| 10 | +
|
| 11 | +For better result, it is recomented to format your code to ANSI style before do conversion. |
| 12 | +
|
| 13 | +NOTE: NO ANY BACKUPS CREATED. YOU MAY PERMANENTLY CORRUPT YOR SOURCES!!! |
| 14 | +
|
| 15 | +Usage: |
| 16 | + cpptopython.py DIR|FILE |
| 17 | + cpptopython.py -v|--version|-h|--help |
| 18 | +
|
| 19 | +When directory given - tries to find source files by C/C++ suffixes, when file name given - processes given file |
| 20 | +
|
| 21 | +Author: Andrei Kopats <[email protected]> |
| 22 | +License: GPL |
| 23 | +""" |
| 24 | + |
| 25 | +import sys |
| 26 | +import os.path |
| 27 | +import re |
| 28 | + |
| 29 | +def is_source(filename): |
| 30 | + suffixes = ('.cpp', '.c', '.cxx', '.c++', '.cc', '.h', '.hpp', '.hxx', '.h++') |
| 31 | + for s in suffixes: |
| 32 | + if filename.endswith(s): |
| 33 | + return True |
| 34 | + return False |
| 35 | + |
| 36 | +def process_line(line): |
| 37 | + |
| 38 | + """ remove semicolons |
| 39 | + |
| 40 | + codecode(param, param); |
| 41 | + V |
| 42 | + codecode(param, param) |
| 43 | + """ |
| 44 | + line = re.sub(';$', '', line) # remove semicolon from the end of line |
| 45 | + |
| 46 | + """ remove strings containing opening bracket |
| 47 | + |
| 48 | + if (blabla) |
| 49 | + { |
| 50 | + codecode |
| 51 | + V |
| 52 | + if (blabla) |
| 53 | + codecode |
| 54 | + """ |
| 55 | + line = re.sub('\s*{\n$', '', line) |
| 56 | + |
| 57 | + """ remove closing brackets. Empty line preserved |
| 58 | + |
| 59 | + if (blabla) |
| 60 | + { |
| 61 | + codecode |
| 62 | + V |
| 63 | + if (blabla) |
| 64 | + codecode |
| 65 | + """ |
| 66 | + line = re.sub('\s*}$', '', line) |
| 67 | + |
| 68 | + """ replace inline comment sign |
| 69 | + |
| 70 | + // here is comment |
| 71 | + V |
| 72 | + # here is comment |
| 73 | + """ |
| 74 | + line = re.sub('//', '#', line) |
| 75 | + |
| 76 | + """ replace /* comment sign |
| 77 | + |
| 78 | + /* here is comment |
| 79 | + V |
| 80 | + ''' here is comment |
| 81 | + """ |
| 82 | + line = re.sub('/\*', "'''", line) |
| 83 | + |
| 84 | + """ replace */ comment sign |
| 85 | + |
| 86 | + here is comment */ |
| 87 | + V |
| 88 | + here is comment ''' |
| 89 | + """ |
| 90 | + line = re.sub('\*/', "'''", line) |
| 91 | + |
| 92 | + """ replace '||' with 'or' |
| 93 | + |
| 94 | + boolvar || anotherboolvar |
| 95 | + V |
| 96 | + boolvar or anotherboolvar |
| 97 | + """ |
| 98 | + line = re.sub('\|\|', 'or', line) |
| 99 | + |
| 100 | + """ replace '&&' with 'and' |
| 101 | + |
| 102 | + boolvar && anotherboolvar |
| 103 | + V |
| 104 | + boolvar and anotherboolvar |
| 105 | + """ |
| 106 | + line = re.sub('&&', 'and', line) |
| 107 | + |
| 108 | + """ replace '!' with 'not ' |
| 109 | + |
| 110 | + if !boolvar |
| 111 | + V |
| 112 | + if not boolvar |
| 113 | + """ |
| 114 | + line = re.sub('!([^=\n])', 'not \\1', line) |
| 115 | + |
| 116 | + """ replace '->' with '.' |
| 117 | + |
| 118 | + object->method() |
| 119 | + V |
| 120 | + object.method() |
| 121 | + """ |
| 122 | + line = re.sub('->', '.', line) |
| 123 | + |
| 124 | + """ replace 'false' with 'False' |
| 125 | + |
| 126 | + b = false |
| 127 | + V |
| 128 | + b = False |
| 129 | + """ |
| 130 | + line = re.sub('false', 'False', line) |
| 131 | + |
| 132 | + """ replace 'true' with 'True' |
| 133 | + |
| 134 | + b = true |
| 135 | + V |
| 136 | + b = True |
| 137 | + """ |
| 138 | + line = re.sub('true', 'True', line) |
| 139 | + |
| 140 | + """ remove "const" word from the middle of string |
| 141 | + |
| 142 | + const int result = a.exec(); |
| 143 | + V |
| 144 | + int result = a.exec(); |
| 145 | + """ |
| 146 | + line = re.sub('const ', ' ', line) |
| 147 | + |
| 148 | + """ remove "const" word from the end of string |
| 149 | + |
| 150 | + const int result = a.exec(); |
| 151 | + V |
| 152 | + int result = a.exec(); |
| 153 | + """ |
| 154 | + line = re.sub(' const$', '', line) |
| 155 | + |
| 156 | + """ remove brackets around if statement and add colon |
| 157 | + |
| 158 | + if (i = 4) |
| 159 | + V |
| 160 | + if i = 4: |
| 161 | + """ |
| 162 | + line = re.sub('if\s*\((.*)\)$', 'if \\1:', line) |
| 163 | + |
| 164 | + """ remove brackets around if statement and add colon |
| 165 | + |
| 166 | + if (i = 4) |
| 167 | + V |
| 168 | + if i = 4: |
| 169 | + """ |
| 170 | + line = re.sub('if\s*\((.*)\)$', 'if \\1:', line) |
| 171 | + #return line |
| 172 | + |
| 173 | + """ remove type from method definition and add a colon and "def" |
| 174 | + |
| 175 | + -bool pMonkeyStudio::isSameFile( const QString& left, const QString& right ) |
| 176 | + +pMonkeyStudio::isSameFile( const QString& left, const QString& right ): |
| 177 | + """ |
| 178 | + line = re.sub('^[\w:&<>\*]+\s+([\w:]+)\(([^\)]*\))$', 'def \\1(self, \\2:', line) |
| 179 | + |
| 180 | + """ after previous replacement fix "(self, )" to "(self)" |
| 181 | + |
| 182 | + -def internal_projectCustomActionTriggered(self, ): |
| 183 | + +def internal_projectCustomActionTriggered(self): |
| 184 | + """ |
| 185 | + line = re.sub('\(\s*self,\s*\)', '(self)', line) |
| 186 | + |
| 187 | + """ remove type name from function parameters (second and farther) |
| 188 | + |
| 189 | + -def internal_currentProjectChanged(self, XUPProjectItem* currentProject, XUPProjectItem* previousProject ): |
| 190 | + +def internal_currentProjectChanged(self, currentProject, previousProject ): |
| 191 | + """ |
| 192 | + line = re.sub(',\s*[\w\d:&\*<>]+\s+([\w\d:&\*]+)', ', \\1', line) |
| 193 | + |
| 194 | + """ remove type name from variable declaration and initialisation |
| 195 | + -pAbstractChild* document = currentDocument() |
| 196 | + +document = currentDocument() |
| 197 | + """ |
| 198 | + line = re.sub('[\w\d:&\*]+\s+([\w\d]+)\s*= ', '\\1 = ', line) |
| 199 | + |
| 200 | + """ remove class name from method definition |
| 201 | + |
| 202 | + -pMonkeyStudio::isSameFile( const QString& left, const QString& right ): |
| 203 | + +pMonkeyStudio::isSameFile( const QString& left, const QString& right ): |
| 204 | + """ |
| 205 | + line = re.sub('^def [\w\d]+::([\w\d]+\([^\)]*\):)$', 'def \\1', line) |
| 206 | + |
| 207 | + """ replace '::' with '.' |
| 208 | + |
| 209 | + YourNameSpace::YourFunction(bla, bla) |
| 210 | + V |
| 211 | + YourNameSpace.YourFunction(bla, bla) |
| 212 | + """ |
| 213 | + line = re.sub('::', '.', line) |
| 214 | + |
| 215 | + """ replace 'else if' with 'elif' |
| 216 | + |
| 217 | + else if (blabla) |
| 218 | + V |
| 219 | + elif (blabla) |
| 220 | + """ |
| 221 | + line = re.sub('else\s+if', 'elif', line) |
| 222 | + |
| 223 | + """ replace 'else' with 'else:' |
| 224 | + if blabala: |
| 225 | + pass |
| 226 | + else |
| 227 | + pass |
| 228 | + V |
| 229 | + if blabala: |
| 230 | + pass |
| 231 | + else: |
| 232 | + pass |
| 233 | + """ |
| 234 | + line = re.sub('else\s*$', 'else:\n', line) |
| 235 | + |
| 236 | + """ Remove "new" keyword |
| 237 | + -i = new Class |
| 238 | + +i = Class |
| 239 | + """ |
| 240 | + line = re.sub(' new ', ' ', line) |
| 241 | + |
| 242 | + """ Replace "this" with "self" |
| 243 | + -p = SomeClass(this) |
| 244 | + +p = SomeClass(self) |
| 245 | + """ |
| 246 | + line = re.sub('([^\w])this([^\w])', '\\1self\\2', line) |
| 247 | + |
| 248 | + """ Replace Qt foreach macro with Python for |
| 249 | + -foreach ( QMdiSubWindow* window, a.subWindowList() ) |
| 250 | + +foreach ( QMdiSubWindow* window, a.subWindowList() ) |
| 251 | + """ |
| 252 | + line = re.sub('foreach\s*\(\s*[\w\d:&\*]+\s+([\w\d]+)\s*,\s*([\w\d\.\(\)]+)\s*\)', 'for \\1 in \\2:', line) |
| 253 | + |
| 254 | + """ Replace Qt signal emit statement |
| 255 | + -emit signalName(param, param) |
| 256 | + +signalName.emit(param, param) |
| 257 | + """ |
| 258 | + line = re.sub('emit ([\w\d]+)', '\\1.emit', line) |
| 259 | + |
| 260 | + """ Replace Qt connect call |
| 261 | + -connect( combo, SIGNAL( activated( int ) ), self, SLOT( comboBox_activated( int ) ) ) |
| 262 | + +combo.activated.connect(self.comboBox_activated) |
| 263 | + """ |
| 264 | + line = re.sub('connect\s*\(\s*([^,]+)\s*,\s*' + \ |
| 265 | + 'SIGNAL\s*\(\s*([\w\d]+)[^\)]+\)\s*\)\s*,'+ \ |
| 266 | + '\s*([^,]+)\s*,\s*' + \ |
| 267 | + 'S[A-Z]+\s*\(\s*([\w\d]+)[^\)]+\)\s*\)\s*\)', |
| 268 | + '\\1.\\2.connect(\\3.\\4)', line) |
| 269 | + |
| 270 | + return line |
| 271 | + |
| 272 | +def process_file(filename): |
| 273 | + with open(filename, 'rw+') as file: |
| 274 | + lines = file.readlines() # probably would die on sources more than 100 000 lines :D |
| 275 | + file.seek(0) |
| 276 | + file.truncate(0) |
| 277 | + for line in lines: |
| 278 | + file.write(process_line(line)) |
| 279 | + |
| 280 | + |
| 281 | +if __name__ == '__main__': |
| 282 | + if '--help' in sys.argv or \ |
| 283 | + '-h' in sys.argv or \ |
| 284 | + '--version' in sys.argv or \ |
| 285 | + '-v' in sys.argv: |
| 286 | + print help |
| 287 | + sys.exit(0) |
| 288 | + if len (sys.argv) != 2: |
| 289 | + print >> sys.stderr, 'Invalid parameters count. Must be 1' |
| 290 | + print help |
| 291 | + sys.exit(-1) |
| 292 | + if os.path.isdir(sys.argv[1]): |
| 293 | + for root, dirs, files in os.walk(sys.argv[1]): |
| 294 | + for file in files: |
| 295 | + filename = root + '/' + file |
| 296 | + if is_source(filename): |
| 297 | + process_file(filename) |
| 298 | + elif os.path.isfile(sys.argv[1]): |
| 299 | + process_file(sys.argv[1]) |
| 300 | + else: |
| 301 | + print >> sys.stderr, 'Not a file or directory', sys.argv[1] |
| 302 | + sys.exit(-1) |
0 commit comments