From 3bc4b5a9f2bcac3d1e554de0ff5b03653a5ec19c Mon Sep 17 00:00:00 2001 From: Andrei Kopats Date: Sun, 6 Apr 2014 08:53:47 +0300 Subject: [PATCH 01/19] Better semicolon removing code --- cpp2python.py | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/cpp2python.py b/cpp2python.py index da409fb..76e1e99 100755 --- a/cpp2python.py +++ b/cpp2python.py @@ -40,8 +40,9 @@ def process_line(line): V codecode(param, param) """ - line = re.sub(';$', '', line) # remove semicolon from the end of line - + line = re.sub(';([\r\n]?)$', '\\1', line) # remove semicolon from the end of line + + """ remove strings containing opening bracket if (blabla) From 5e0eaf8c41ea4d136b3d3c0cc95844593f867c1d Mon Sep 17 00:00:00 2001 From: Andrei Kopats Date: Sun, 6 Apr 2014 08:54:06 +0300 Subject: [PATCH 02/19] Strip trailing whitespace --- cpp2python.py | 132 +++++++++++++++++++++++++------------------------- 1 file changed, 66 insertions(+), 66 deletions(-) diff --git a/cpp2python.py b/cpp2python.py index 76e1e99..d10c291 100755 --- a/cpp2python.py +++ b/cpp2python.py @@ -11,7 +11,7 @@ NO ANY BACKUPS ARE CREATED. YOU MAY PERMANENTLY CORRUPT YOR SOURCES Usage: - + cpp2python.py DIR|FILE cpp2python.py -v|--version|-h|--help @@ -33,9 +33,9 @@ def is_source(filename): return False def process_line(line): - + """ remove semicolons - + codecode(param, param); V codecode(param, param) @@ -44,7 +44,7 @@ def process_line(line): """ remove strings containing opening bracket - + if (blabla) { codecode @@ -52,10 +52,10 @@ def process_line(line): if (blabla) codecode """ - line = re.sub('\s*{\n$', '', line) - + line = re.sub('\s*{\n$', '', line) + """ remove closing brackets. Empty line preserved - + if (blabla) { codecode @@ -63,163 +63,163 @@ def process_line(line): if (blabla) codecode """ - line = re.sub('\s*}$', '', line) - + line = re.sub('\s*}$', '', line) + """ replace inline comment sign - + // here is comment V # here is comment """ - line = re.sub('//', '#', line) - + line = re.sub('//', '#', line) + """ replace /* comment sign - + /* here is comment V ''' here is comment """ - line = re.sub('/\*', "'''", line) - + line = re.sub('/\*', "'''", line) + """ replace */ comment sign - + here is comment */ V here is comment ''' """ - line = re.sub('\*/', "'''", line) - + line = re.sub('\*/', "'''", line) + """ replace '||' with 'or' - + boolvar || anotherboolvar V boolvar or anotherboolvar """ - line = re.sub('\|\|', 'or', line) - + line = re.sub('\|\|', 'or', line) + """ replace '&&' with 'and' - + boolvar && anotherboolvar V boolvar and anotherboolvar """ - line = re.sub('&&', 'and', line) - + line = re.sub('&&', 'and', line) + """ replace '!' with 'not ' - + if !boolvar V if not boolvar """ - line = re.sub('!([^=\n])', 'not \\1', line) - + line = re.sub('!([^=\n])', 'not \\1', line) + """ replace '->' with '.' - + object->method() V object.method() """ - line = re.sub('->', '.', line) - + line = re.sub('->', '.', line) + """ replace 'false' with 'False' - + b = false V b = False """ - line = re.sub('false', 'False', line) - + line = re.sub('false', 'False', line) + """ replace 'true' with 'True' - + b = true V b = True """ - line = re.sub('true', 'True', line) - + line = re.sub('true', 'True', line) + """ remove "const" word from the middle of string - + const int result = a.exec(); V int result = a.exec(); """ line = re.sub('const ', ' ', line) - + """ remove "const" word from the end of string - + const int result = a.exec(); V int result = a.exec(); """ line = re.sub(' const$', '', line) - + """ remove brackets around if statement and add colon - + if (i = 4) V if i = 4: """ line = re.sub('if\s*\((.*)\)$', 'if \\1:', line) - + """ remove brackets around if statement and add colon - + if (i = 4) V if i = 4: """ line = re.sub('if\s*\((.*)\)$', 'if \\1:', line) #return line - + """ remove type from method definition and add a colon and "def" - + -bool pMonkeyStudio::isSameFile( const QString& left, const QString& right ) +pMonkeyStudio::isSameFile( const QString& left, const QString& right ): """ line = re.sub('^[\w:&<>\*]+\s+([\w:]+)\(([^\)]*\))$', 'def \\1(self, \\2:', line) - + """ after previous replacement fix "(self, )" to "(self)" - + -def internal_projectCustomActionTriggered(self, ): +def internal_projectCustomActionTriggered(self): """ line = re.sub('\(\s*self,\s*\)', '(self)', line) - + """ remove type name from function parameters (second and farther) - + -def internal_currentProjectChanged(self, XUPProjectItem* currentProject, XUPProjectItem* previousProject ): +def internal_currentProjectChanged(self, currentProject, previousProject ): """ line = re.sub(',\s*[\w\d:&\*<>]+\s+([\w\d:&\*]+)', ', \\1', line) - + """ remove type name from variable declaration and initialisation -pAbstractChild* document = currentDocument() +document = currentDocument() """ line = re.sub('[\w\d:&\*]+\s+([\w\d]+)\s*= ', '\\1 = ', line) - + """ remove class name from method definition - + -pMonkeyStudio::isSameFile( const QString& left, const QString& right ): +pMonkeyStudio::isSameFile( const QString& left, const QString& right ): """ line = re.sub('^def [\w\d]+::([\w\d]+\([^\)]*\):)$', 'def \\1', line) - + """ replace '::' with '.' - + YourNameSpace::YourFunction(bla, bla) V YourNameSpace.YourFunction(bla, bla) """ - line = re.sub('::', '.', line) - + line = re.sub('::', '.', line) + """ replace 'else if' with 'elif' - + else if (blabla) V elif (blabla) """ - line = re.sub('else\s+if', 'elif', line) - + line = re.sub('else\s+if', 'elif', line) + """ replace 'else' with 'else:' if blabala: pass @@ -232,31 +232,31 @@ def process_line(line): pass """ line = re.sub('else\s*$', 'else:\n', line) - + """ Remove "new" keyword -i = new Class +i = Class """ line = re.sub(' new ', ' ', line) - + """ Replace "this" with "self" -p = SomeClass(this) +p = SomeClass(self) """ line = re.sub('([^\w])this([^\w])', '\\1self\\2', line) - + """ Replace Qt foreach macro with Python for -foreach ( QMdiSubWindow* window, a.subWindowList() ) +foreach ( QMdiSubWindow* window, a.subWindowList() ) """ line = re.sub('foreach\s*\(\s*[\w\d:&\*]+\s+([\w\d]+)\s*,\s*([\w\d\.\(\)]+)\s*\)', 'for \\1 in \\2:', line) - + """ Replace Qt signal emit statement -emit signalName(param, param) +signalName.emit(param, param) """ line = re.sub('emit ([\w\d]+)', '\\1.emit', line) - + """ Replace Qt connect call -connect( combo, SIGNAL( activated( int ) ), self, SLOT( comboBox_activated( int ) ) ) +combo.activated.connect(self.comboBox_activated) @@ -266,12 +266,12 @@ def process_line(line): '\s*([^,]+)\s*,\s*' + \ 'S[A-Z]+\s*\(\s*([\w\d]+)[^\)]+\)\s*\)\s*\)', '\\1.\\2.connect(\\3.\\4)', line) - + return line def process_file(filename): with open(filename, 'rw+') as file: - lines = file.readlines() # probably would die on sources more than 100 000 lines :D + lines = file.readlines() # probably would die on sources more than 100 000 lines :D file.seek(0) file.truncate(0) for line in lines: From 99af62520ae8c1475f60cc55780f897dcdbaf67a Mon Sep 17 00:00:00 2001 From: Stuart Axon Date: Sun, 26 Jul 2015 20:02:50 +0800 Subject: [PATCH 03/19] Add setup.py --- setup.py | 107 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 107 insertions(+) create mode 100644 setup.py diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..5d7ff9c --- /dev/null +++ b/setup.py @@ -0,0 +1,107 @@ +import os +import subprocess +import sys +import pkg_resources + +import subprocess +import sys + +from glob import glob +from os.path import abspath, basename, dirname, join, normpath, relpath +from shutil import rmtree +from textwrap import dedent + +from setuptools import setup +from setuptools import Command + +here = normpath(abspath(dirname(__file__))) + +class CleanCommand(Command): + """Custom clean command to tidy up the project root.""" + CLEAN_FILES = './build ./dist ./*.pyc ./*.tgz ./*.egg-info ./__pycache__'.split(' ') + + user_options = [] + + def initialize_options(self): + pass + def finalize_options(self): + pass + def run(self): + global here + + for path_spec in self.CLEAN_FILES: + # Make paths absolute and relative to this path + abs_paths = glob(normpath(join(here, path_spec))) + for path in [str(p) for p in abs_paths]: + if not path.startswith(here): + # Die if path in CLEAN_FILES is absolute + outside this directory + raise ValueError("%s is not a path inside %s" % (path, here)) + print('removing %s' % relpath(path)) + try: + rmtree(path) + except: + pass + +long_description=dedent(""" + cpp2python + + Script helps to convert C/C++ sources to C/C++ -like Python sources. + + It does few simple edit operations, like removing semicolons and type declarations. After it you must edit code manually, but you'll spend less time to do it. + + Utility will make mistaces and will not generate ready for use code, so, it won't help you, unless if you know either C/C++ and Python + + For better result, it is recomented to format your code to ANSI style before doing conversion. + + NO ANY BACKUPS ARE CREATED. YOU MAY PERMANENTLY CORRUPT YOR SOURCES + + Usage: + + cpp2python.py DIR|FILE + cpp2python.py -v|--version|-h|--help + + When directory name is given - tries to find source files by C/C++ suffixes, when file name is given - processes given file + + Author: Andrei Kopats hlamer@tut.by License: GPL +""") + +setup( + cmdclass={ + 'clean': CleanCommand, + }, + + name='cpp2python', + version='0.1.0', + + description='Helps to convert C/C++ sources to C/C++ -like Python sources.', + long_description=long_description, + url='https://github.com/hlamer/cpp2python', + author='Andrei Kopats', + author_email='hlamer@tut.by', + license='GPL', + # See https://pypi.python.org/pypi?%3Aaction=list_classifiers + classifiers=[ + # How mature is this project? Common values are + # 3 - Alpha + # 4 - Beta + # 5 - Production/Stable + 'Development Status :: 3 - Alpha', + + # Indicate who your project is intended for + 'Intended Audience :: Developers', + 'Topic :: Software Development', + + # Pick your license as you wish (should match "license" above) + 'License :: OSI Approved :: GNU General Public License v2 or later (GPLv2+)', + + 'Programming Language :: Python :: 2.7', + 'Programming Language :: Python :: 3.4', + ], + keywords='cpp python', + + entry_points = { + 'console_scripts': [ + 'cpp2python = cpp2python:main' + ] + }, +) From 6175f4915df60a120ffc5faacbdc2e4141fc6d23 Mon Sep 17 00:00:00 2001 From: Stuart Axon Date: Sun, 26 Jul 2015 20:02:57 +0800 Subject: [PATCH 04/19] Add entry point for setup --- cpp2python.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/cpp2python.py b/cpp2python.py index d10c291..72680e9 100755 --- a/cpp2python.py +++ b/cpp2python.py @@ -278,7 +278,7 @@ def process_file(filename): file.write(process_line(line)) -if __name__ == '__main__': +def main(): if '--help' in sys.argv or \ '-h' in sys.argv or \ '--version' in sys.argv or \ @@ -300,3 +300,6 @@ def process_file(filename): else: print >> sys.stderr, 'Not a file or directory', sys.argv[1] sys.exit(-1) + +if __name__ == '__main__': + main() From ef5a1a185a02fc0eec7006e14891a2b9b41924ae Mon Sep 17 00:00:00 2001 From: Stuart Axon Date: Sun, 26 Jul 2015 20:12:58 +0800 Subject: [PATCH 05/19] Don't overwrite the original files. --- cpp2python.py | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/cpp2python.py b/cpp2python.py index 72680e9..9adc872 100755 --- a/cpp2python.py +++ b/cpp2python.py @@ -269,11 +269,13 @@ def process_line(line): return line -def process_file(filename): - with open(filename, 'rw+') as file: +def process_file(in_filename, out_filename): + """ + generator - outputs processed file + """ + with open(in_filename, 'r') as file: lines = file.readlines() # probably would die on sources more than 100 000 lines :D - file.seek(0) - file.truncate(0) + with open(out_filename, 'w+') as file: for line in lines: file.write(process_line(line)) @@ -292,11 +294,12 @@ def main(): if os.path.isdir(sys.argv[1]): for root, dirs, files in os.walk(sys.argv[1]): for file in files: - filename = root + '/' + file - if is_source(filename): - process_file(filename) + in_filename = root + '/' + file + if is_source(in_filename): + out_filename = in_filename + '.py' # not ideal + process_file(in_filename, out_filename) elif os.path.isfile(sys.argv[1]): - process_file(sys.argv[1]) + process_file(sys.argv[1], sys.argv[1] + '.py') else: print >> sys.stderr, 'Not a file or directory', sys.argv[1] sys.exit(-1) From 1105f2d1553aabfb1da99016c4598231aa5ce225 Mon Sep 17 00:00:00 2001 From: Andrei Kopats Date: Wed, 29 Jul 2015 22:17:43 +0300 Subject: [PATCH 06/19] README update --- README.md | 39 +++++++++++++++++++++++++++------------ cpp2python.py | 27 ++++++++++++++++++--------- 2 files changed, 45 insertions(+), 21 deletions(-) diff --git a/README.md b/README.md index 66e5127..41753ab 100644 --- a/README.md +++ b/README.md @@ -1,20 +1,35 @@ # cpp2python -Script helps to convert C/C++ sources to C/C++ -like Python sources. -It does few simple edit operations, like removing semicolons and type declarations. After it you must edit code manually, but you'll spend less time to do it. +The script helps to convert C/C++ sources to C/C++ -like Python sources. -Utility **will** make mistaces and **will not** generate ready for use code, so, it won't help you, unless if you know either C/C++ and Python +It does some simple edit operations like removing semicolons and type declarations. After it you must edit code manually, but you'll probably spend less time doing it. -For better result, it is recomented to format your code to ANSI style before doing conversion. +Example: +``` +if (a && b) --> if a and b: +{ --> object.method() + object->method(); --> +} --> +``` -**NO ANY BACKUPS ARE CREATED. YOU MAY PERMANENTLY CORRUPT YOR SOURCES** +The utility **will** make mistakes and **will not** generate ready for use code, therefore it won't be useful for you unless you know both C/C++ and Python. -Usage: - - cpp2python.py DIR|FILE - cpp2python.py -v|--version|-h|--help +For better result, it is recommended to format your code to ANSI style before performing conversion. -When directory name is given - tries to find source files by C/C++ suffixes, when file name is given - processes given file +``` +astyle --style=ansi your.cpp source.cpp files.cpp +``` -Author: Andrei Kopats -License: GPL +### Usage + + cpp2python.py DIR Find C/C++ files in the directory by suffix and process. + cpp2python.py FILE Process the file + cpp2python.py -v|--version|-h|--help Display the help message + + +### Author +Andrei Kopats +setup.py and improvements by Stuart Axon + +### License +GPL diff --git a/cpp2python.py b/cpp2python.py index d10c291..71411fb 100755 --- a/cpp2python.py +++ b/cpp2python.py @@ -1,21 +1,30 @@ #!/usr/bin/env python -help = """Script helps to convert C/C++ sources to C/C++ -like Python sources. +help = """The script helps to convert C/C++ sources to C/C++ -like Python sources. -It does few simple edit operations, like removing semicolons and type declarations. After it you must edit code manually, but you'll spend less time to do it. +It does some simple edit operations like removing semicolons and type declarations. +After it you must edit code manually, but you'll probably spend less time doing it. +Example: -Utility Will make mistaces and Will not generate ready for use code, so, it won't help you, unless if you know either C/C++ and Python + if (a && b) --> if a and b: + { --> object.method() + object->method(); --> + } --> -For better result, it is recomented to format your code to ANSI style before doing conversion. +The utility **will** make mistakes and **will not** generate ready for use code, +therefore it won't be useful for you unless you know both C/C++ and Python. -NO ANY BACKUPS ARE CREATED. YOU MAY PERMANENTLY CORRUPT YOR SOURCES +For better result, it is recomented to format your code to ANSI style +before doing conversion. -Usage: + astyle --style=ansi your.cpp source.cpp files.cpp - cpp2python.py DIR|FILE - cpp2python.py -v|--version|-h|--help +Usage: -When directory name is given - tries to find source files by C/C++ suffixes, when file name is given - processes given file + cpp2python.py DIR Find C/C++ files in the directory + by suffix and process. + cpp2python.py FILE Process the file + cpp2python.py -v|--version|-h|--help Display the help message Author: Andrei Kopats License: GPL From 5d7b534d394b33b4a994fa18b7b8d267bfa8168c Mon Sep 17 00:00:00 2001 From: Andrei Kopats Date: Wed, 29 Jul 2015 22:29:24 +0300 Subject: [PATCH 07/19] setup.py: remove not necessary command and duplicating description --- setup.py | 61 ++++---------------------------------------------------- 1 file changed, 4 insertions(+), 57 deletions(-) diff --git a/setup.py b/setup.py index 5d7ff9c..a589e70 100644 --- a/setup.py +++ b/setup.py @@ -13,68 +13,15 @@ from setuptools import setup from setuptools import Command +import cpp2python -here = normpath(abspath(dirname(__file__))) - -class CleanCommand(Command): - """Custom clean command to tidy up the project root.""" - CLEAN_FILES = './build ./dist ./*.pyc ./*.tgz ./*.egg-info ./__pycache__'.split(' ') - - user_options = [] - - def initialize_options(self): - pass - def finalize_options(self): - pass - def run(self): - global here - - for path_spec in self.CLEAN_FILES: - # Make paths absolute and relative to this path - abs_paths = glob(normpath(join(here, path_spec))) - for path in [str(p) for p in abs_paths]: - if not path.startswith(here): - # Die if path in CLEAN_FILES is absolute + outside this directory - raise ValueError("%s is not a path inside %s" % (path, here)) - print('removing %s' % relpath(path)) - try: - rmtree(path) - except: - pass - -long_description=dedent(""" - cpp2python - - Script helps to convert C/C++ sources to C/C++ -like Python sources. - - It does few simple edit operations, like removing semicolons and type declarations. After it you must edit code manually, but you'll spend less time to do it. - - Utility will make mistaces and will not generate ready for use code, so, it won't help you, unless if you know either C/C++ and Python - - For better result, it is recomented to format your code to ANSI style before doing conversion. - - NO ANY BACKUPS ARE CREATED. YOU MAY PERMANENTLY CORRUPT YOR SOURCES - - Usage: - - cpp2python.py DIR|FILE - cpp2python.py -v|--version|-h|--help - - When directory name is given - tries to find source files by C/C++ suffixes, when file name is given - processes given file - - Author: Andrei Kopats hlamer@tut.by License: GPL -""") +here = normpath(abspath(dirname(__file__))) setup( - cmdclass={ - 'clean': CleanCommand, - }, - name='cpp2python', - version='0.1.0', - + version='0.2.0', description='Helps to convert C/C++ sources to C/C++ -like Python sources.', - long_description=long_description, + long_description=cpp2python.help, url='https://github.com/hlamer/cpp2python', author='Andrei Kopats', author_email='hlamer@tut.by', From 76b91d8a69368dce658aa5f1d65104c00107b9a3 Mon Sep 17 00:00:00 2001 From: Andrei Kopats Date: Wed, 29 Jul 2015 22:31:55 +0300 Subject: [PATCH 08/19] suffix documentation --- README.md | 2 ++ cpp2python.py | 9 ++++++--- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 41753ab..88689d2 100644 --- a/README.md +++ b/README.md @@ -26,6 +26,8 @@ astyle --style=ansi your.cpp source.cpp files.cpp cpp2python.py FILE Process the file cpp2python.py -v|--version|-h|--help Display the help message +After the processing new file is created. +File name is {old file name with suffix}.py. i.e. main.cpp.py ### Author Andrei Kopats diff --git a/cpp2python.py b/cpp2python.py index 80cc27c..e412d4e 100755 --- a/cpp2python.py +++ b/cpp2python.py @@ -23,8 +23,11 @@ cpp2python.py DIR Find C/C++ files in the directory by suffix and process. - cpp2python.py FILE Process the file - cpp2python.py -v|--version|-h|--help Display the help message + cpp2python.py FILE Process the file. + cpp2python.py -v|--version|-h|--help Display the help message. + +After the processing new file is created. +File name is {old file name with suffix}.py. i.e. main.cpp.py Author: Andrei Kopats License: GPL @@ -303,7 +306,7 @@ def main(): if os.path.isdir(sys.argv[1]): for root, dirs, files in os.walk(sys.argv[1]): for file in files: - in_filename = root + '/' + file + in_filename = root + '/' + file if is_source(in_filename): out_filename = in_filename + '.py' # not ideal process_file(in_filename, out_filename) From a863ee5aef9d1a9699e55c002f00e9a434e241d5 Mon Sep 17 00:00:00 2001 From: Andrei Kopats Date: Wed, 29 Jul 2015 22:33:08 +0300 Subject: [PATCH 09/19] readme: break long lines --- README.md | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 88689d2..527a557 100644 --- a/README.md +++ b/README.md @@ -2,7 +2,8 @@ The script helps to convert C/C++ sources to C/C++ -like Python sources. -It does some simple edit operations like removing semicolons and type declarations. After it you must edit code manually, but you'll probably spend less time doing it. +It does some simple edit operations like removing semicolons and type declarations. +After it you must edit code manually, but you'll probably spend less time doing it. Example: ``` @@ -12,9 +13,11 @@ if (a && b) --> if a and b: } --> ``` -The utility **will** make mistakes and **will not** generate ready for use code, therefore it won't be useful for you unless you know both C/C++ and Python. +The utility **will** make mistakes and **will not** generate ready for use code, +therefore it won't be useful for you unless you know both C/C++ and Python. -For better result, it is recommended to format your code to ANSI style before performing conversion. +For better result, it is recommended to format your code to ANSI style +before performing conversion. ``` astyle --style=ansi your.cpp source.cpp files.cpp @@ -22,7 +25,8 @@ astyle --style=ansi your.cpp source.cpp files.cpp ### Usage - cpp2python.py DIR Find C/C++ files in the directory by suffix and process. + cpp2python.py DIR Find C/C++ files in the directory + by suffix and process. cpp2python.py FILE Process the file cpp2python.py -v|--version|-h|--help Display the help message From a64705efd58777305094b2ebb3f3922d0b6a3bd1 Mon Sep 17 00:00:00 2001 From: Andrei Kopats Date: Wed, 29 Jul 2015 22:33:49 +0300 Subject: [PATCH 10/19] readme: better formatting --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 527a557..1006c1b 100644 --- a/README.md +++ b/README.md @@ -35,6 +35,7 @@ File name is {old file name with suffix}.py. i.e. main.cpp.py ### Author Andrei Kopats + setup.py and improvements by Stuart Axon ### License From fc7fbcaad61080a084e4967d328bafd6750d274e Mon Sep 17 00:00:00 2001 From: Andrei Kopats Date: Sun, 23 Aug 2015 21:00:32 +0300 Subject: [PATCH 11/19] gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..0d20b64 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +*.pyc From e3a7daccdd32007bea5c56d66c13ca21372442a4 Mon Sep 17 00:00:00 2001 From: Andrei Kopats Date: Mon, 31 Aug 2015 23:04:33 +0300 Subject: [PATCH 12/19] setup.py: simplify and fix #5 --- setup.py | 24 ++---------------------- 1 file changed, 2 insertions(+), 22 deletions(-) diff --git a/setup.py b/setup.py index a589e70..979a5af 100644 --- a/setup.py +++ b/setup.py @@ -1,22 +1,6 @@ -import os -import subprocess -import sys -import pkg_resources - -import subprocess -import sys - -from glob import glob -from os.path import abspath, basename, dirname, join, normpath, relpath -from shutil import rmtree -from textwrap import dedent - -from setuptools import setup -from setuptools import Command +from distutils.core import setup import cpp2python -here = normpath(abspath(dirname(__file__))) - setup( name='cpp2python', version='0.2.0', @@ -46,9 +30,5 @@ ], keywords='cpp python', - entry_points = { - 'console_scripts': [ - 'cpp2python = cpp2python:main' - ] - }, + scripts=['cpp2python.py'] ) From 37d6e3f4df07afda1ba9138736370c30b9bb21d8 Mon Sep 17 00:00:00 2001 From: Andrei Kopats Date: Mon, 31 Aug 2015 23:06:48 +0300 Subject: [PATCH 13/19] gitignore build dir --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 0d20b64..0fe2c40 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,2 @@ *.pyc +build/ From 9bcbbe75187d3ff96e48aa92bb684286c90a4b34 Mon Sep 17 00:00:00 2001 From: wittrup Date: Mon, 4 Jan 2016 13:09:34 +0100 Subject: [PATCH 14/19] Python 3.5.1 Wrote cpp2python running on Python 3.5.1 (v3.5.1:37a07cee5969, Dec 6 2015, 01:54:25) [MSC v.1900 64 bit (AMD64)] on win32 Included .gitignore for JetBrains PyCharm --- .gitignore | 2 ++ cpp2python.py | 8 ++++---- setup.py | 2 +- 3 files changed, 7 insertions(+), 5 deletions(-) diff --git a/.gitignore b/.gitignore index 0fe2c40..a429c83 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,4 @@ *.pyc build/ +.idea/ +cpp2python.egg-info/ diff --git a/cpp2python.py b/cpp2python.py index e412d4e..5888b27 100755 --- a/cpp2python.py +++ b/cpp2python.py @@ -297,11 +297,11 @@ def main(): '-h' in sys.argv or \ '--version' in sys.argv or \ '-v' in sys.argv: - print help + print(help) sys.exit(0) if len (sys.argv) != 2: - print >> sys.stderr, 'Invalid parameters count. Must be 1' - print help + print('Invalid parameters count. Must be 1', file=sys.stderr) + print(help) sys.exit(-1) if os.path.isdir(sys.argv[1]): for root, dirs, files in os.walk(sys.argv[1]): @@ -313,7 +313,7 @@ def main(): elif os.path.isfile(sys.argv[1]): process_file(sys.argv[1], sys.argv[1] + '.py') else: - print >> sys.stderr, 'Not a file or directory', sys.argv[1] + print('Not a file or directory', sys.argv[1], file=sys.stderr) sys.exit(-1) if __name__ == '__main__': diff --git a/setup.py b/setup.py index 979a5af..42bb132 100644 --- a/setup.py +++ b/setup.py @@ -1,4 +1,4 @@ -from distutils.core import setup +from setuptools import setup import cpp2python setup( From 90427f6e11ef2b6369192608109e2d2906229b03 Mon Sep 17 00:00:00 2001 From: Andrei Kopats Date: Tue, 22 Mar 2016 10:41:17 +0300 Subject: [PATCH 15/19] Use python3 --- cpp2python.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cpp2python.py b/cpp2python.py index 5888b27..d0e13f1 100755 --- a/cpp2python.py +++ b/cpp2python.py @@ -1,4 +1,4 @@ -#!/usr/bin/env python +#!/usr/bin/env python3 help = """The script helps to convert C/C++ sources to C/C++ -like Python sources. From 3f95fccc120bbf1f74e712869f7681da4dc87c99 Mon Sep 17 00:00:00 2001 From: Andrei Kopats Date: Tue, 22 Mar 2016 10:42:55 +0300 Subject: [PATCH 16/19] Update README.md --- README.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/README.md b/README.md index 1006c1b..7e81e94 100644 --- a/README.md +++ b/README.md @@ -30,6 +30,12 @@ astyle --style=ansi your.cpp source.cpp files.cpp cpp2python.py FILE Process the file cpp2python.py -v|--version|-h|--help Display the help message +### Installation +(Optional, the script can be used from the source tree) + + python3 setup.py install + + After the processing new file is created. File name is {old file name with suffix}.py. i.e. main.cpp.py From 73ac8111882da435ba77ba9b48dd7ecf049ebc35 Mon Sep 17 00:00:00 2001 From: Andrei Kopats Date: Tue, 22 Mar 2016 10:43:27 +0300 Subject: [PATCH 17/19] Update README.md --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 7e81e94..a1eb00d 100644 --- a/README.md +++ b/README.md @@ -30,15 +30,15 @@ astyle --style=ansi your.cpp source.cpp files.cpp cpp2python.py FILE Process the file cpp2python.py -v|--version|-h|--help Display the help message +After the processing new file is created. +File name is `{old file name with suffix}.py`. i.e. `main.cpp.py` + ### Installation (Optional, the script can be used from the source tree) python3 setup.py install -After the processing new file is created. -File name is {old file name with suffix}.py. i.e. main.cpp.py - ### Author Andrei Kopats From 11e039b65545c37a6d4232b38720489b6dbd98c1 Mon Sep 17 00:00:00 2001 From: danerlt <1598552894@qq.com> Date: Thu, 21 Nov 2019 21:51:17 +0800 Subject: [PATCH 18/19] fix open file encode error fix read utf-8 file in windows10 read to gbk encode. --- cpp2python.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cpp2python.py b/cpp2python.py index d0e13f1..27d8ccf 100755 --- a/cpp2python.py +++ b/cpp2python.py @@ -285,7 +285,7 @@ def process_file(in_filename, out_filename): """ generator - outputs processed file """ - with open(in_filename, 'r') as file: + with open(in_filename, 'r', encoding='utf-8') as file: lines = file.readlines() # probably would die on sources more than 100 000 lines :D with open(out_filename, 'w+') as file: for line in lines: From 5a5b4fd33d6b61114117ddc2d5554f4001ef0d7c Mon Sep 17 00:00:00 2001 From: danerlt <1598552894@qq.com> Date: Thu, 21 Nov 2019 23:43:08 +0800 Subject: [PATCH 19/19] fix write file encode error fix write file to gbk encode error --- cpp2python.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cpp2python.py b/cpp2python.py index 27d8ccf..c98e98c 100755 --- a/cpp2python.py +++ b/cpp2python.py @@ -287,7 +287,7 @@ def process_file(in_filename, out_filename): """ with open(in_filename, 'r', encoding='utf-8') as file: lines = file.readlines() # probably would die on sources more than 100 000 lines :D - with open(out_filename, 'w+') as file: + with open(out_filename, 'w+', encoding='utf-8') as file: for line in lines: file.write(process_line(line))