Files
create/src/Tools/PythonToCPP.py
Chris Hennes 62bfb44a0d [Tools] Fix static analyis issues
This commit addresses issues identified by LGTM for the various
independent (and mostly-independent) files in the Tools subdirectory.
The vast majority of the issues are trivial, and are things like unused
imports or catching BaseException. There was one true bug identified, a
global variable being changed in a function where it was not marked
global, but it only affected output quantity (the variable is named
"VERBOSE"). A couple of other issues identified variables that appear to
represent no-longer-existing options in the code. The options were left,
but a deprecation printout replaces the variable in the event the option
is provided.
2021-02-28 17:53:04 +01:00

54 lines
1.2 KiB
Python

#!/usr/bin/python
# -*- coding: utf-8 -*-
# (c) 2004 Werner Mayer LGPL
import os,sys
#os.chdir("E:\\Develop\\FreeCADWin\\scripts")
try:
file = open(sys.argv[1],encoding="utf-8")
except TypeError:
file = open(sys.argv[1])
if(len(sys.argv) > 4):
sys.stderr.write("Wrong Parameter\n Usage:\n PythonToCPP Infile.py [Outfile][Variable]\n")
if(len(sys.argv) > 2):
try:
out = open(sys.argv[2],"w",encoding="utf-8");
except TypeError:
out = open(sys.argv[2],"w");
else:
out = sys.stdout
if(len(sys.argv) > 3):
identifier = sys.argv[3]
else:
identifier = os.path.basename(sys.argv[1])
identifier = identifier[:-3]
lines = file.readlines()
# We want to use this script for files in another directory, so we extract the actual file name
out.write("const char " + identifier + "[] =")
for line in lines:
# remove new line
line2 = line.rstrip()
# replace special chars
line2 = line2.replace('\\','\\\\')
line2 = line2.replace('\"','\\\"')
line2 = line2.replace("\'","\\\'")
# output
#out.write(line)
out.write( '\"' + line2 + '\\n\"\n')
out.write(";\n\n\n");