Opened 6 years ago

Closed 6 years ago

#5253 closed defect (fixed)

Fix SVG generation for Python3

Reported by: Henning Kiel Owned by: Martin Sjölund
Priority: normal Milestone: 1.13.0
Component: Documentation Version: v1.13.0-dev.beta1
Keywords: Cc: Dietmar Winkler

Description

Dietmar reported a bug in SVG generation for the library documentation script when run with Python3:

2018-12-19 07:05:51,534 - generate_icons.py - INFO - Exporting: Modelica.Blocks.Interaction.Show.BooleanValue
2018-12-19 07:05:51,691 - generate_icons.py - CRITICAL - Failed to generate icons for Modelica after 11.6 seconds: 'rgb(166.4, 166.4, 166.4)' is not a valid value for attribute 'stop-color' at svg-element <stop>.
Traceback (most recent call last):
  File "generate_icons.py", line 1334, in <module>
    sys.exit(main())
  File "generate_icons.py", line 1299, in main
    dwg = exportIcon(modelica_class, base_classes, includeInvisibleText, warn_duplicates, with_json)
  File "generate_icons.py", line 1179, in exportIcon
    dwg = generateSvg(os.path.join(output_dir, classToFileName(modelicaClass) + ".svg"), iconGraphics, includeInvisbleText, warn_duplicates)
  File "generate_icons.py", line 1121, in generateSvg
    svgShape = getSvgFromGraphics(dwg, graphics, minX, maxY, includeInvisibleText)
  File "generate_icons.py", line 1001, in getSvgFromGraphics
    gradient.add_stop_color(offset=stopValue, color='rgb({0}, {1}, {2})'.format(colors[idx][0], colors[idx][1], colors[idx][2]), opacity=1)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/svgwrite/gradients.py", line 60, in add_stop_color
    self.add(_GradientStop(offset, color, opacity, factory=self))
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/svgwrite/gradients.py", line 29, in __init__
    self['stop-color'] = color
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/svgwrite/base.py", line 166, in __setitem__
    self.validator.check_svg_attribute_value(self.elementname, key, value)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/svgwrite/validator2.py", line 62, in check_svg_attribute_value
    self._check_svg_value(elementname, attributename, value)
  File "/opt/local/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/svgwrite/validator2.py", line 79, in _check_svg_value
    raise TypeError("'%s' is not a valid value for attribute '%s' at svg-element <%s>." % (value, attributename, elementname))
TypeError: 'rgb(166.4, 166.4, 166.4)' is not a valid value for attribute 'stop-color' at svg-element <stop>.

I also encountered a second one:

2018-12-19 08:09:19,008 - generate_icons.py - INFO - Exporting: Modelica.Fluid.Dissipation.Utilities.Icons.HeatTransfer.Channel_i
2018-12-19 08:09:19,045 - generate_icons.py - CRITICAL - Failed to generate icons for Modelica after 130.3 seconds: can only concatenate str (not "bytes") to str
Traceback (most recent call last):
  File "generate_icons.py", line 1334, in <module>
    sys.exit(main())
  File "generate_icons.py", line 1299, in main
    dwg = exportIcon(modelica_class, base_classes, includeInvisibleText, warn_duplicates, with_json)
  File "generate_icons.py", line 1171, in exportIcon
    graphics = getGraphicsWithPortsForClass(modelicaClass)
  File "generate_icons.py", line 323, in getGraphicsWithPortsForClass
    graphics = getGraphicsForClass(modelicaClass)
  File "generate_icons.py", line 298, in getGraphicsForClass
    graphicsObj['href'] = "data:image;base64,"+base64.b64encode(f_p.read())
TypeError: can only concatenate str (not "bytes") to str

Change History (2)

comment:1 by Henning Kiel, 6 years ago

I fixed the script by adding a few more explicit casts to satisfy type checking:

  • colors seem to be accepted as integer 0-255, but not float 0-255
  • the binary bitmap-image-files need to be opened in binary mode. read() returns an array of bytes then and not a string, which is then converted useing base64. The result needs to be concatenated with a string, so the base64 converted bytes array needs to be cast to s atring.

Fixed in PR2839

comment:2 by Dietmar Winkler, 6 years ago

Resolution: fixed
Status: newclosed

Tested and I confirm this fixes the issue.

Note: See TracTickets for help on using tickets.