Skip to content

Commit

Permalink
Return SVG string when path not specified. Closes #39.
Browse files Browse the repository at this point in the history
  • Loading branch information
onyxfish committed May 19, 2016
1 parent eb8cb78 commit 76f5c6a
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 46 deletions.
41 changes: 21 additions & 20 deletions leather/chart.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,7 @@ def to_svg_group(self, width, height):

return root_group

def to_svg(self, path, width=None, height=None):
def to_svg(self, path=None, width=None, height=None):
"""
Render this chart to an SVG document.
Expand All @@ -243,7 +243,8 @@ def to_svg(self, path, width=None, height=None):
as though they were pixels.
:param path:
Filepath or file-like object to write to.
Filepath or file-like object to write to. If not specified then
the SVG will be returned as a string.
:param width:
The output width, in SVG user units.
:param height:
Expand All @@ -262,26 +263,26 @@ def to_svg(self, path, width=None, height=None):
group = self.to_svg_group(width, height)
root.append(group)

svg_text = svg.stringify(root)
close = True

try:
if hasattr(path, 'write'):
f = path
close = False
else:
dirpath = os.path.dirname(path)

if dirpath and not os.path.exists(dirpath):
os.makedirs(dirpath)
if path:
try:
if hasattr(path, 'write'):
f = path
close = False
else:
dirpath = os.path.dirname(path)

f = open(path, 'w')
if dirpath and not os.path.exists(dirpath):
os.makedirs(dirpath)

f.write(svg.HEADER)
f = open(path, 'w')

if six.PY3:
f.write(ET.tostring(root, encoding='unicode'))
else:
f.write(ET.tostring(root, encoding='utf-8'))
finally:
if close and f is not None:
f.close()
f.write(svg.HEADER)
f.write(svg_text)
finally:
if close and f is not None:
f.close()
else:
return svg_text
41 changes: 22 additions & 19 deletions leather/grid.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def add_chart(self, chart):
"""
self._charts.append(chart)

def to_svg(self, path, width=None, height=None):
def to_svg(self, path=None, width=None, height=None):
"""
Render the grid to an SVG.
"""
Expand Down Expand Up @@ -61,23 +61,26 @@ def to_svg(self, path, width=None, height=None):

root.append(group)

svg_text = svg.stringify(root)
close = True

try:
if hasattr(path, 'write'):
f = path
close = False
else:
dirpath = os.path.dirname(path)

if dirpath and not os.path.exists(dirpath):
os.makedirs(dirpath)

f = open(path, 'w')

f.write(svg.HEADER)
f.write(ET.tostring(root, encoding='unicode'))
f.close()
finally:
if close and f is not None:
f.close()
if path:
try:
if hasattr(path, 'write'):
f = path
close = False
else:
dirpath = os.path.dirname(path)

if dirpath and not os.path.exists(dirpath):
os.makedirs(dirpath)

f = open(path, 'w')

f.write(svg.HEADER)
f.write(svg_text)
finally:
if close and f is not None:
f.close()
else:
return svg_text
24 changes: 24 additions & 0 deletions leather/svg.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,33 @@
Helpers for working with SVG.
"""

import xml.etree.ElementTree as ET

import six

HEADER = '<?xml version="1.0" standalone="no"?>\n' + \
'<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN"\n' + \
'"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd">\n'


def stringify(root):
"""
Convert an SVG XML tree to a unicode string.
"""
if six.PY3:
return ET.tostring(root, encoding='unicode')
else:
return ET.tostring(root, encoding='utf-8')

def save(f, root):
"""
Save an SVG XML tree to a file.
"""
f.write(HEADER)
f.write(stringify(root))

def translate(x, y):
"""
Generate an SVG transform statement representing a simple translation.
"""
return 'translate(%i %i)' % (x, y)
10 changes: 3 additions & 7 deletions tests/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
import unittest

from lxml import etree
import six


class XMLTest(unittest.TestCase):
Expand All @@ -17,10 +16,7 @@ def render_chart(self, chart):
"""
Verify the column names in the given table match what is expected.
"""
output = six.StringIO()
chart.to_svg(output)
text = output.getvalue()

text = chart.to_svg()
text = text.replace(' xmlns="http://www.w3.org/2000/svg"', '')

return etree.fromstring(text)
Expand All @@ -29,6 +25,6 @@ def assertElementCount(self, svg, selector, count):
series = svg.cssselect(selector)
self.assertEqual(len(series), count)

def assertTickLabels(self, svg, orient, cmp):
def assertTickLabels(self, svg, orient, compare):
ticks = [t.text for t in svg.cssselect('.%s .tick text' % orient)]
self.assertSequenceEqual(ticks, cmp)
self.assertSequenceEqual(ticks, compare)

0 comments on commit 76f5c6a

Please sign in to comment.