Refactoring flatten_paths() into flatten_all_paths()
parent
3512f86968
commit
f5a7fe77d1
|
@ -74,10 +74,11 @@ CONVERSIONS = {'path': path2pathd,
|
||||||
'rect': rect2pathd}
|
'rect': rect2pathd}
|
||||||
|
|
||||||
|
|
||||||
def flatten_paths(group, return_attribs=False,
|
def flatten_all_paths(
|
||||||
group_filter=lambda x: True,
|
group,
|
||||||
path_filter=lambda x: True,
|
group_filter=lambda x: True,
|
||||||
path_conversions=CONVERSIONS):
|
path_filter=lambda x: True,
|
||||||
|
path_conversions=CONVERSIONS):
|
||||||
"""Returns the paths inside a group (recursively), expressing the paths in the root coordinates.
|
"""Returns the paths inside a group (recursively), expressing the paths in the root coordinates.
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
|
@ -108,8 +109,8 @@ def flatten_paths(group, return_attribs=False,
|
||||||
|
|
||||||
stack = [new_stack_element(group, np.identity(3))]
|
stack = [new_stack_element(group, np.identity(3))]
|
||||||
|
|
||||||
|
FlattenedPath = collections.namedtuple('FlattenedPath', ['path', 'attributes', 'transform'])
|
||||||
paths = []
|
paths = []
|
||||||
path_attribs = []
|
|
||||||
|
|
||||||
while stack:
|
while stack:
|
||||||
top = stack.pop()
|
top = stack.pop()
|
||||||
|
@ -118,16 +119,13 @@ def flatten_paths(group, return_attribs=False,
|
||||||
# the path_filter accepts it.
|
# the path_filter accepts it.
|
||||||
for key, converter in path_conversions:
|
for key, converter in path_conversions:
|
||||||
for path_elem in filter(path_filter, top.group.iterfind(key)):
|
for path_elem in filter(path_filter, top.group.iterfind(key)):
|
||||||
paths.append(transform(parse_path(converter(path_elem)), top.transform))
|
path_tf = top.transform * parse_transform(path_elem.get('transform'))
|
||||||
if return_attribs:
|
path = transform(parse_path(converter(path_elem)), path_tf)
|
||||||
path_attribs.append(path_elem.attrib)
|
paths.append(FlattenedPath(path, path_elem.attrib, path_tf))
|
||||||
|
|
||||||
stack.extend(get_relevant_children(top.group, top.transform))
|
stack.extend(get_relevant_children(top.group, top.transform))
|
||||||
|
|
||||||
if return_attribs:
|
return paths
|
||||||
return paths, path_attribs
|
|
||||||
else:
|
|
||||||
return paths
|
|
||||||
|
|
||||||
|
|
||||||
class Document:
|
class Document:
|
||||||
|
@ -163,35 +161,11 @@ class Document:
|
||||||
self._prefix = ''
|
self._prefix = ''
|
||||||
# etree.register_namespace('', prefix)
|
# etree.register_namespace('', prefix)
|
||||||
|
|
||||||
def flatten_paths(self, return_attribs=False,
|
def flatten_all_paths(self,
|
||||||
group_filter=lambda x: True,
|
group_filter=lambda x: True,
|
||||||
path_filter=lambda x: True,
|
path_filter=lambda x: True,
|
||||||
path_conversions=CONVERSIONS):
|
path_conversions=CONVERSIONS):
|
||||||
paths = []
|
return flatten_all_paths(self.tree.getroot(), group_filter, path_filter, path_conversions)
|
||||||
path_attribs = []
|
|
||||||
|
|
||||||
# We don't need to worry about transforming any paths that lack a group.
|
|
||||||
# We can just append them to the list of paths and grab their attributes.
|
|
||||||
for key, converter in path_conversions:
|
|
||||||
for path_elem in filter(path_filter, self.tree.getroot().iterfind(key)):
|
|
||||||
paths.append(parse_path(converter(path_elem)))
|
|
||||||
if return_attribs:
|
|
||||||
path_attribs.append(path_elem.attrib)
|
|
||||||
|
|
||||||
for group_elem in filter(group_filter, self.tree.getroot().iterfind('g')):
|
|
||||||
if return_attribs:
|
|
||||||
new_paths, new_attribs = flatten_paths(group_elem, return_attribs,
|
|
||||||
group_filter, path_filter, path_conversions)
|
|
||||||
path_attribs.extend(new_attribs)
|
|
||||||
else:
|
|
||||||
new_paths = flatten_paths(group_elem, return_attribs,
|
|
||||||
group_filter, path_filter, path_conversions)
|
|
||||||
paths.extend(new_paths)
|
|
||||||
|
|
||||||
if return_attribs:
|
|
||||||
return new_paths, new_attribs
|
|
||||||
else:
|
|
||||||
return new_paths
|
|
||||||
|
|
||||||
def get_elements_by_tag(self, tag):
|
def get_elements_by_tag(self, tag):
|
||||||
"""Returns a generator of all elements with the given tag.
|
"""Returns a generator of all elements with the given tag.
|
||||||
|
|
|
@ -219,7 +219,7 @@ def _parse_transform_substr(transform_substr):
|
||||||
if 'matrix' in type_str:
|
if 'matrix' in type_str:
|
||||||
_check_num_parsed_values(values, 6)
|
_check_num_parsed_values(values, 6)
|
||||||
|
|
||||||
transform[0:2, 0:3] = np.matrix([values[0:3], values[3:6]])
|
transform[0:2, 0:3] = np.matrix([values[0:6:2], values[1:6:2]])
|
||||||
|
|
||||||
elif 'translate' in transform_substr:
|
elif 'translate' in transform_substr:
|
||||||
_check_num_parsed_values(values, [1, 2])
|
_check_num_parsed_values(values, [1, 2])
|
||||||
|
|
Loading…
Reference in New Issue