mirror of
https://github.com/wxWidgets/Phoenix.git
synced 2025-12-16 01:30:07 +01:00
Adding missing close for open and replaced "fid=open(filename) fid.close()"
statements with the safer "with open(filename) as fid:" blocks. Also removed unnecessary "try: ... finally: pass" statements and refactored code from img2py function into _write_image and _replace_non_alphanumeric_with_underscore Fixes #1574
This commit is contained in:
@@ -126,6 +126,41 @@ def convert(fileName, maskClr, outputDir, outputName, outType, outExt):
|
||||
return img2img.convert(fileName, maskClr, outputDir, outputName, outType, outExt)
|
||||
|
||||
|
||||
def _replace_non_alphanumeric_with_underscore(imgName):
|
||||
letters = [letter if letter.isalnum() else '_' for letter in imgName]
|
||||
|
||||
if not letters[0].isalpha() and letters[0] != '_':
|
||||
letters.insert(0, "_")
|
||||
varName = "".join(letters)
|
||||
return varName
|
||||
|
||||
|
||||
def _write_image(out, imgName, data, append, icon, catalog, functionCompatible, old_index):
|
||||
out.write("#" + "-" * 70 + "\n")
|
||||
if not append:
|
||||
out.write("# This file was generated by %s\n#\n" % sys.argv[0])
|
||||
out.write("from wx.lib.embeddedimage import PyEmbeddedImage\n\n")
|
||||
if catalog:
|
||||
out.write("catalog = {}\n")
|
||||
out.write("index = []\n\n")
|
||||
varName = _replace_non_alphanumeric_with_underscore(imgName)
|
||||
out.write("%s = PyEmbeddedImage(\n%s\n" % (varName, data))
|
||||
if catalog:
|
||||
if imgName in old_index:
|
||||
print("Warning: %s already in catalog." % imgName)
|
||||
print(" Only the last entry will be accessible.\n")
|
||||
old_index.append(imgName)
|
||||
out.write("index.append('%s')\n" % imgName)
|
||||
out.write("catalog['%s'] = %s\n" % (imgName, varName))
|
||||
if functionCompatible:
|
||||
out.write("get%sData = %s.GetData\n" % (varName, varName))
|
||||
out.write("get%sImage = %s.GetImage\n" % (varName, varName))
|
||||
out.write("get%sBitmap = %s.GetBitmap\n" % (varName, varName))
|
||||
if icon:
|
||||
out.write("get%sIcon = %s.GetIcon\n" % (varName, varName))
|
||||
out.write("\n")
|
||||
|
||||
|
||||
def img2py(image_file, python_file,
|
||||
append=DEFAULT_APPEND,
|
||||
compressed=DEFAULT_COMPRESSED,
|
||||
@@ -186,91 +221,44 @@ def img2py(image_file, python_file,
|
||||
append_catalog = True
|
||||
|
||||
with open(python_file, "r") as sourcePy:
|
||||
try:
|
||||
for line in sourcePy:
|
||||
|
||||
if line == "catalog = {}\n":
|
||||
append_catalog = False
|
||||
else:
|
||||
lineMatcher = indexPattern.match(line)
|
||||
if lineMatcher:
|
||||
old_index.append(lineMatcher.groups()[0])
|
||||
finally:
|
||||
pass
|
||||
for line in sourcePy:
|
||||
if line == "catalog = {}\n":
|
||||
append_catalog = False
|
||||
else:
|
||||
lineMatcher = indexPattern.match(line)
|
||||
if lineMatcher:
|
||||
old_index.append(lineMatcher.groups()[0])
|
||||
|
||||
if append_catalog:
|
||||
with open(python_file, "a") as out:
|
||||
try:
|
||||
out.write("\n# ***************** Catalog starts here *******************")
|
||||
out.write("\n\ncatalog = {}\n")
|
||||
out.write("index = []\n\n")
|
||||
finally:
|
||||
pass
|
||||
out.write("\n# ***************** Catalog starts here *******************")
|
||||
out.write("\n\ncatalog = {}\n")
|
||||
out.write("index = []\n\n")
|
||||
|
||||
if not imgName:
|
||||
imgPath, imgFile = os.path.split(image_file)
|
||||
imgName = os.path.splitext(imgFile)[0]
|
||||
print("\nWarning: -n not specified. Using filename (%s) for name of image and/or catalog entry." % imgName)
|
||||
|
||||
if python_file == '-':
|
||||
out = sys.stdout
|
||||
elif append:
|
||||
out = open(python_file, "a")
|
||||
_write_image(out, imgName, data, append, icon, catalog, functionCompatible, old_index)
|
||||
else:
|
||||
out = open(python_file, "w")
|
||||
mode = "a" if append else "w"
|
||||
with open(python_file, mode) as out:
|
||||
_write_image(out, imgName, data, append, icon, catalog, functionCompatible, old_index)
|
||||
|
||||
try:
|
||||
imgPath, imgFile = os.path.split(image_file)
|
||||
if not imgName:
|
||||
imgName = os.path.splitext(imgFile)[0]
|
||||
print("\nWarning: -n not specified. Using filename (%s) for name of image and/or catalog entry." % imgName)
|
||||
if imgName:
|
||||
n_msg = ' using "%s"' % imgName
|
||||
else:
|
||||
n_msg = ""
|
||||
|
||||
out.write("#" + "-" * 70 + "\n")
|
||||
if not append:
|
||||
out.write("# This file was generated by %s\n#\n" % sys.argv[0])
|
||||
out.write("from wx.lib.embeddedimage import PyEmbeddedImage\n\n")
|
||||
if catalog:
|
||||
out.write("catalog = {}\n")
|
||||
out.write("index = []\n\n")
|
||||
|
||||
letters = []
|
||||
for letter in imgName:
|
||||
if not letter.isalnum():
|
||||
letter = "_"
|
||||
letters.append(letter)
|
||||
if not letters[0].isalpha() and letters[0] != '_':
|
||||
letters.insert(0, "_")
|
||||
varName = "".join(letters)
|
||||
|
||||
out.write("%s = PyEmbeddedImage(\n%s\n" % (varName, data))
|
||||
|
||||
if catalog:
|
||||
if imgName in old_index:
|
||||
print("Warning: %s already in catalog." % imgName)
|
||||
print(" Only the last entry will be accessible.\n")
|
||||
old_index.append(imgName)
|
||||
out.write("index.append('%s')\n" % imgName)
|
||||
out.write("catalog['%s'] = %s\n" % (imgName, varName))
|
||||
|
||||
if functionCompatible:
|
||||
out.write("get%sData = %s.GetData\n" % (varName, varName))
|
||||
out.write("get%sImage = %s.GetImage\n" % (varName, varName))
|
||||
out.write("get%sBitmap = %s.GetBitmap\n" % (varName, varName))
|
||||
if icon:
|
||||
out.write("get%sIcon = %s.GetIcon\n" % (varName, varName))
|
||||
|
||||
out.write("\n")
|
||||
|
||||
if imgName:
|
||||
n_msg = ' using "%s"' % imgName
|
||||
else:
|
||||
n_msg = ""
|
||||
|
||||
if maskClr:
|
||||
m_msg = " with mask %s" % maskClr
|
||||
else:
|
||||
m_msg = ""
|
||||
|
||||
print("Embedded %s%s into %s%s" % (image_file, n_msg, python_file, m_msg))
|
||||
finally:
|
||||
if python_file != '-':
|
||||
out.close()
|
||||
if maskClr:
|
||||
m_msg = " with mask %s" % maskClr
|
||||
else:
|
||||
m_msg = ""
|
||||
|
||||
print("Embedded %s%s into %s%s" % (image_file, n_msg, python_file, m_msg))
|
||||
|
||||
|
||||
def main(args=None):
|
||||
@@ -319,5 +307,6 @@ def main(args=None):
|
||||
img2py(image_file, python_file,
|
||||
append, compressed, maskClr, imgName, icon, catalog, compatible)
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main(sys.argv[1:])
|
||||
|
||||
Reference in New Issue
Block a user