Try to follow FT coding style

This commit is contained in:
Rod Sheeter 2019-02-12 11:53:27 -08:00
parent 199aa9e24b
commit 416da67fdd
3 changed files with 96 additions and 96 deletions

View File

@ -54,7 +54,7 @@ class SVGPath(object):
pb = PathBuilder() pb = PathBuilder()
# xpath | doesn't seem to reliable work so just walk it # xpath | doesn't seem to reliable work so just walk it
for el in self.root.iter(): for el in self.root.iter():
pb.AddPathFromElement(el) pb.add_path_from_element(el)
for path in pb.pathes: for path in pb.pathes:
parse_path(path, pen) parse_path(path, pen)

View File

@ -1,4 +1,4 @@
def _PreferNonZero(*args): def _prefer_non_zero(*args):
for arg in args: for arg in args:
if arg != 0: if arg != 0:
return arg return arg
@ -12,13 +12,13 @@ class PathBuilder(object):
def __init__(self): def __init__(self):
self.pathes = [] self.pathes = []
def StartPath(self, initial_path=''): def _start_path(self, initial_path=''):
self.pathes.append(initial_path) self.pathes.append(initial_path)
def EndPath(self): def _end_path(self):
self._Add('z') self._add('z')
def _Add(self, path_snippet): def _add(self, path_snippet):
path = self.pathes[-1] path = self.pathes[-1]
if path: if path:
path += ' ' + path_snippet path += ' ' + path_snippet
@ -27,7 +27,7 @@ class PathBuilder(object):
self.pathes[-1] = path self.pathes[-1] = path
def _move(self, c, x, y): def _move(self, c, x, y):
self._Add('%s%s,%s' % (c, _ntos(x), _ntos(y))) self._add('%s%s,%s' % (c, _ntos(x), _ntos(y)))
def M(self, x, y): def M(self, x, y):
self._move('M', x, y) self._move('M', x, y)
@ -36,7 +36,7 @@ class PathBuilder(object):
self._move('m', x, y) self._move('m', x, y)
def _arc(self, c, rx, ry, x, y, large_arc): def _arc(self, c, rx, ry, x, y, large_arc):
self._Add('%s%s,%s 0 %d 1 %s,%s' % (c, _ntos(rx), _ntos(ry), large_arc, self._add('%s%s,%s 0 %d 1 %s,%s' % (c, _ntos(rx), _ntos(ry), large_arc,
_ntos(x), _ntos(y))) _ntos(x), _ntos(y)))
def A(self, rx, ry, x, y, large_arc = 0): def A(self, rx, ry, x, y, large_arc = 0):
@ -46,7 +46,7 @@ class PathBuilder(object):
self._arc('a', rx, ry, x, y, large_arc) self._arc('a', rx, ry, x, y, large_arc)
def _vhline(self, c, x): def _vhline(self, c, x):
self._Add('%s%s' % (c, _ntos(x))) self._add('%s%s' % (c, _ntos(x)))
def H(self, x): def H(self, x):
self._vhline('H', x) self._vhline('H', x)
@ -60,7 +60,7 @@ class PathBuilder(object):
def v(self, x): def v(self, x):
self._vhline('v', y) self._vhline('v', y)
def _ParseRect(self, rect): def _parse_rect(self, rect):
x = float(rect.attrib.get('x', 0)) x = float(rect.attrib.get('x', 0))
y = float(rect.attrib.get('y', 0)) y = float(rect.attrib.get('y', 0))
w = float(rect.attrib.get('width')) w = float(rect.attrib.get('width'))
@ -68,11 +68,11 @@ class PathBuilder(object):
rx = float(rect.attrib.get('rx', 0)) rx = float(rect.attrib.get('rx', 0))
ry = float(rect.attrib.get('ry', 0)) ry = float(rect.attrib.get('ry', 0))
rx = _PreferNonZero(rx, ry) rx = _prefer_non_zero(rx, ry)
ry = _PreferNonZero(ry, rx) ry = _prefer_non_zero(ry, rx)
# TODO there are more rules for adjusting rx, ry # TODO there are more rules for adjusting rx, ry
self.StartPath() self._start_path()
self.M(x + rx, y) self.M(x + rx, y)
self.H(x + w -rx) self.H(x + w -rx)
if rx > 0: if rx > 0:
@ -86,33 +86,33 @@ class PathBuilder(object):
self.V(y + ry) self.V(y + ry)
if rx > 0: if rx > 0:
self.A(rx, ry, x + rx, y) self.A(rx, ry, x + rx, y)
self.EndPath() self._end_path()
def _ParsePath(self, path): def _parse_path(self, path):
if 'd' in path.attrib: if 'd' in path.attrib:
self.StartPath(initial_path=path.attrib['d']) self._start_path(initial_path=path.attrib['d'])
def _ParsePolygon(self, poly): def _parse_polygon(self, poly):
if 'points' in poly.attrib: if 'points' in poly.attrib:
self.StartPath('M' + poly.attrib['points']) self._start_path('M' + poly.attrib['points'])
self.EndPath() self._end_path()
def _ParseCircle(self, circle): def _parse_circle(self, circle):
cx = float(circle.attrib.get('cx', 0)) cx = float(circle.attrib.get('cx', 0))
cy = float(circle.attrib.get('cy', 0)) cy = float(circle.attrib.get('cy', 0))
r = float(circle.attrib.get('r')) r = float(circle.attrib.get('r'))
# arc doesn't seem to like being a complete shape, draw two halves # arc doesn't seem to like being a complete shape, draw two halves
self.StartPath() self._start_path()
self.M(cx - r, cy) self.M(cx - r, cy)
self.A(r, r, cx + r, cy, large_arc=1) self.A(r, r, cx + r, cy, large_arc=1)
self.A(r, r, cx - r, cy, large_arc=1) self.A(r, r, cx - r, cy, large_arc=1)
def AddPathFromElement(self, el): def add_path_from_element(self, el):
tag = el.tag tag = el.tag
if '}' in el.tag: if '}' in el.tag:
tag = el.tag.split('}', 1)[1] # from https://bugs.python.org/issue18304 tag = el.tag.split('}', 1)[1] # from https://bugs.python.org/issue18304
parse_fn = getattr(self, '_Parse%s' % tag.lower().capitalize(), None) parse_fn = getattr(self, '_parse_%s' % tag.lower(), None)
if not callable(parse_fn): if not callable(parse_fn):
return False return False
parse_fn(el) parse_fn(el)

View File

@ -60,7 +60,7 @@ import pytest
) )
def test_el_to_path(svg_xml, expected_path): def test_el_to_path(svg_xml, expected_path):
pb = shapes.PathBuilder() pb = shapes.PathBuilder()
pb.AddPathFromElement(etree.fromstring(svg_xml)) pb.add_path_from_element(etree.fromstring(svg_xml))
if expected_path: if expected_path:
expected = [expected_path] expected = [expected_path]
else: else: