提交 7579c650 编写于 作者: C Cleber Rosa

YAML to Mux: unwind the normalize_path utility function

The function where this utility method lives is really big and
complex.  Let's break it down for the sake of code readability,
while adding better docstrings.  At the same time, this allows
us to write tests for those smaller units.

I've timed the performance changes caused by this, and on all
trials they presented slightly better (but overall insignifcant)
improvements.  This is just mentioned to assert that there are
no performance downsides to this change.
Signed-off-by: NCleber Rosa <crosa@redhat.com>
上级 f35378a8
......@@ -73,20 +73,28 @@ class ListOfNodeObjects(list): # Few methods pylint: disable=R0903
"""
def _normalize_path(path):
"""
End the path with single '/'
:param path: original path
:type path: str
:returns: path with trailing '/', or None when empty path
:rtype: str or None
"""
if not path:
return
if path[-1] != '/':
path += '/'
return path
def _create_from_yaml(path, cls_node=mux.MuxTreeNode):
"""Create tree structure from yaml stream"""
def tree_node_from_values(name, values):
"""Create `name` node and add values"""
def handle_control_tag(node, value):
"""Handling of YAML tags (except of !using)"""
def normalize_path(path):
"""End the path with single '/', None when empty path"""
if not path:
return
if path[-1] != '/':
path += '/'
return path
if value[0].code == YAML_INCLUDE:
# Include file
ypath = value[1]
......@@ -105,11 +113,11 @@ def _create_from_yaml(path, cls_node=mux.MuxTreeNode):
elif value[0].code == YAML_MUX:
node.multiplex = True
elif value[0].code == YAML_FILTER_ONLY:
new_value = normalize_path(value[1])
new_value = _normalize_path(value[1])
if new_value:
node.filters[0].append(new_value)
elif value[0].code == YAML_FILTER_OUT:
new_value = normalize_path(value[1])
new_value = _normalize_path(value[1])
if new_value:
node.filters[1].append(new_value)
......
......@@ -535,6 +535,13 @@ class TestPathParent(unittest.TestCase):
self.assertNotEqual(mux.path_parent('/os/linux'), '/')
class TestCreateFromYaml(unittest.TestCase):
def test_normalize_path(self):
self.assertEqual(yaml_to_mux._normalize_path(''), None)
self.assertEqual(yaml_to_mux._normalize_path('path'), 'path/')
class TestFingerprint(unittest.TestCase):
def test_fingerprint(self):
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册