From bff4f65cd2d5ef2da14523316994bf985c504c8b Mon Sep 17 00:00:00 2001 From: Sandra Hoffmann Date: Sun, 1 Mar 2020 12:29:42 +0100 Subject: [PATCH] STCC-219 fixed svg-to-png conversion error with xml register_namespace --- config/default.ini | 2 +- .../converter/mediaconverter.py | 4 +++- .../converter/test_converter.py | 6 ++++-- src/scratchtocatrobat/tools/svgtopng.py | 13 +++++++++---- src/scratchtocatrobat/tools/test_svgtopng.py | 17 ++++++++++------- 5 files changed, 27 insertions(+), 15 deletions(-) diff --git a/config/default.ini b/config/default.ini index 7e2fa3e6..60ef92d2 100755 --- a/config/default.ini +++ b/config/default.ini @@ -4,7 +4,7 @@ name: Scratch2Catrobat Converter short_name: S2CC version: 0.10.0 build_name: Aegean cat -build_number: 1002 +build_number: 1007 build_type: S2CC ;------------------------------------------------------------------------------- diff --git a/src/scratchtocatrobat/converter/mediaconverter.py b/src/scratchtocatrobat/converter/mediaconverter.py index 6c8bc514..31495626 100644 --- a/src/scratchtocatrobat/converter/mediaconverter.py +++ b/src/scratchtocatrobat/converter/mediaconverter.py @@ -22,6 +22,7 @@ import os import shutil from threading import Thread +from threading import Lock from java.awt import Color from scratchtocatrobat.tools import logger @@ -37,6 +38,7 @@ MAX_CONCURRENT_THREADS = int(helpers.config.get("MEDIA_CONVERTER", "max_concurrent_threads")) log = logger.log +ns_registry_lock = Lock() class MediaType(object): @@ -73,7 +75,7 @@ def run(self): if media_type == MediaType.UNCONVERTED_SVG: # converting svg to png -> new md5 and filename - new_src_path = svgtopng.convert(old_src_path, info["rotationCenterX"], info["rotationCenterY"]) + new_src_path = svgtopng.convert(old_src_path, info["rotationCenterX"], info["rotationCenterY"], ns_registry_lock) elif media_type == MediaType.UNCONVERTED_WAV: # converting Android-incompatible wav to compatible wav new_src_path = wavconverter.convert_to_android_compatible_wav(old_src_path) diff --git a/src/scratchtocatrobat/converter/test_converter.py b/src/scratchtocatrobat/converter/test_converter.py index a8ae94a1..2f949771 100644 --- a/src/scratchtocatrobat/converter/test_converter.py +++ b/src/scratchtocatrobat/converter/test_converter.py @@ -21,6 +21,7 @@ import os import unittest import re +from threading import Lock import org.catrobat.catroid.common as catcommon import org.catrobat.catroid.content as catbase @@ -40,6 +41,7 @@ BACKGROUND_LOCALIZED_GERMAN_NAME = "Hintergrund" BACKGROUND_ORIGINAL_NAME = "Stage" +ns_registry_lock = Lock() def create_catrobat_sprite_stub(name=None): @@ -2643,7 +2645,7 @@ def test_can_rewrite_svg_matrix(self): if re.search('.*}g', child.tag) != None: if 'transform' in child.attrib: assert(child.attrib['transform'] == "matrix(1.5902323722839355, 0, 0, 1.5902323722839355, -0.5, 0.5)") - svgtopng._parse_and_rewrite_svg_file("test/res/scratch/Wizard_Spells/3.svg","test/res/scratch/Wizard_Spells/3_changed.svg") + svgtopng._parse_and_rewrite_svg_file("test/res/scratch/Wizard_Spells/3.svg","test/res/scratch/Wizard_Spells/3_changed.svg", ns_registry_lock) tree = ET.parse("test/res/scratch/Wizard_Spells/3_changed.svg") root = tree.getroot() for child in root: @@ -2658,7 +2660,7 @@ def test_can_rewrite_svg_text_position(self): if re.search('.*}text', child.tag) != None: assert(child.attrib['x'] == '147.5') assert(child.attrib['y'] == '146.1') - svgtopng._parse_and_rewrite_svg_file("test/res/scratch/Wizard_Spells/6.svg","test/res/scratch/Wizard_Spells/6_changed.svg") + svgtopng._parse_and_rewrite_svg_file("test/res/scratch/Wizard_Spells/6.svg","test/res/scratch/Wizard_Spells/6_changed.svg", ns_registry_lock) tree = ET.parse("test/res/scratch/Wizard_Spells/6_changed.svg") root = tree.getroot() for child in root: diff --git a/src/scratchtocatrobat/tools/svgtopng.py b/src/scratchtocatrobat/tools/svgtopng.py index e3f30383..41a55be7 100644 --- a/src/scratchtocatrobat/tools/svgtopng.py +++ b/src/scratchtocatrobat/tools/svgtopng.py @@ -35,6 +35,7 @@ import java.awt.Color import xml.etree.cElementTree as ET import subprocess +from threading import Lock _BATIK_CLI_JAR = "batik-rasterizer.jar" _log = logging.getLogger(__name__) @@ -51,7 +52,7 @@ def _checked_batik_jar_path(): return _batik_jar_path -def convert(input_svg_path, rotation_x, rotation_y): +def convert(input_svg_path, rotation_x, rotation_y, ns_registry_lock): assert isinstance(input_svg_path, (str, unicode)) assert os.path.splitext(input_svg_path)[1] == ".svg" @@ -74,7 +75,7 @@ def convert(input_svg_path, rotation_x, rotation_y): png_ostream = None error = None try: - _parse_and_rewrite_svg_file(input_svg_path, output_svg_path) + _parse_and_rewrite_svg_file(input_svg_path, output_svg_path, ns_registry_lock) command = "svg2png" out = subprocess.check_output([command, output_svg_path, "-o", output_png_path]) _log.info(" converting '%s' to Pocket Code compatible png '%s'", @@ -196,12 +197,16 @@ def _create_buffered_image(image): -def _parse_and_rewrite_svg_file(svg_input_path, svg_output_path): +def _parse_and_rewrite_svg_file(svg_input_path, svg_output_path, ns_registry_lock): tree = ET.parse(svg_input_path) namespaces = dict([node for _, node in ET.iterparse(svg_input_path,events=['start-ns'])]) for prefix, uri in namespaces.items(): - ET.register_namespace(prefix, uri) + ns_registry_lock.acquire() + try: + ET.register_namespace(prefix, uri) + finally: + ns_registry_lock.release() root = tree.getroot() #exception is thrown if height or width is less or equal zero diff --git a/src/scratchtocatrobat/tools/test_svgtopng.py b/src/scratchtocatrobat/tools/test_svgtopng.py index 31026d5b..496881b0 100644 --- a/src/scratchtocatrobat/tools/test_svgtopng.py +++ b/src/scratchtocatrobat/tools/test_svgtopng.py @@ -22,11 +22,14 @@ import os import shutil import unittest +from threading import Lock from scratchtocatrobat.tools import common_testing from scratchtocatrobat.tools import svgtopng from scratchtocatrobat.tools import helpers +ns_registry_lock = Lock() + class SvgToPngTest(common_testing.BaseTestCase): #def test_can_convert_file_from_svg_to_png(self): @@ -49,7 +52,7 @@ def test_parse_svgfile_and_convert_to_png_cape(self): rotation_x, rotation_y = 36, 67 - output_png_path = svgtopng.convert(input_svg_path, rotation_x, rotation_y) + output_png_path = svgtopng.convert(input_svg_path, rotation_x, rotation_y, ns_registry_lock) from javax.imageio import ImageIO from java.io import File @@ -76,7 +79,7 @@ def test_parse_svgfile_and_convert_to_png_background(self): rotation_x, rotation_y = 255, 180 - output_png_path = svgtopng.convert(input_svg_path, rotation_x, rotation_y) + output_png_path = svgtopng.convert(input_svg_path, rotation_x, rotation_y, ns_registry_lock) from javax.imageio import ImageIO from java.io import File @@ -103,7 +106,7 @@ def test_parse_svgfile_and_convert_to_png_antenna(self): rotation_x, rotation_y = 53, 43 - output_png_path = svgtopng.convert(input_svg_path, rotation_x, rotation_y) + output_png_path = svgtopng.convert(input_svg_path, rotation_x, rotation_y, ns_registry_lock) from javax.imageio import ImageIO from java.io import File @@ -133,7 +136,7 @@ def test_parse_svgfile_and_convert_to_png_hat_q1(self): str(rotation_x) + "_rotY_" + str(rotation_y) + ".png") - output_png_path = svgtopng.convert(input_svg_path, rotation_x, rotation_y) + output_png_path = svgtopng.convert(input_svg_path, rotation_x, rotation_y, ns_registry_lock) from javax.imageio import ImageIO from java.io import File @@ -163,7 +166,7 @@ def test_parse_svgfile_and_convert_to_png_hat_q2(self): str(rotation_x) + "_rotY_" + str(rotation_y) + ".png") - output_png_path = svgtopng.convert(input_svg_path, rotation_x, rotation_y) + output_png_path = svgtopng.convert(input_svg_path, rotation_x, rotation_y, ns_registry_lock) from javax.imageio import ImageIO from java.io import File @@ -193,7 +196,7 @@ def test_parse_svgfile_and_convert_to_png_hat_q3(self): str(rotation_x) + "_rotY_" + str(rotation_y) + ".png") - output_png_path = svgtopng.convert(input_svg_path, rotation_x, rotation_y) + output_png_path = svgtopng.convert(input_svg_path, rotation_x, rotation_y, ns_registry_lock) from javax.imageio import ImageIO from java.io import File @@ -223,7 +226,7 @@ def test_parse_svgfile_and_convert_to_png_hat_q4(self): str(rotation_x) + "_rotY_" + str(rotation_y) + ".png") - output_png_path = svgtopng.convert(input_svg_path, rotation_x, rotation_y) + output_png_path = svgtopng.convert(input_svg_path, rotation_x, rotation_y, ns_registry_lock) from javax.imageio import ImageIO from java.io import File