]> git.ipfire.org Git - thirdparty/jinja.git/commitdiff
Refactor _prepare_attribute_parts() to use in both make_attrgetter() and make_multi_a... 1013/head
authormayur-srivastava <mayur.p.srivastava@gmail.com>
Sun, 9 Jun 2019 13:35:29 +0000 (09:35 -0400)
committermayur-srivastava <mayur.p.srivastava@gmail.com>
Sun, 9 Jun 2019 13:35:29 +0000 (09:35 -0400)
jinja2/filters.py

index 2245742241154eb253094ae19e94a857c42ecea6..8504c7b9c4aa5093c2c89434b400e8488e20222b 100644 (file)
@@ -65,12 +65,7 @@ def make_attrgetter(environment, attribute, postprocess=None):
     to access attributes of attributes.  Integer parts in paths are
     looked up as integers.
     """
-    if attribute is None:
-        attribute = []
-    elif isinstance(attribute, string_types):
-        attribute = [int(x) if x.isdigit() else x for x in attribute.split('.')]
-    else:
-        attribute = [attribute]
+    attribute = _prepare_attribute_parts(attribute)
 
     def attrgetter(item):
         for part in attribute:
@@ -95,14 +90,6 @@ def make_multi_attrgetter(environment, attribute, postprocess=None):
 
     Examples of attribute: "attr1,attr2", "attr1.inner1.0,attr2.inner2.0", etc.
     """
-    def _prepare_attribute_parts(attr):
-        if attr is None:
-            return []
-        elif isinstance(attribute, string_types):
-            return [int(x) if x.isdigit() else x for x in attr.split('.')]
-        else:
-            return [attr]
-
     attribute_parts = attribute.split(',') if isinstance(attribute, string_types) else [attribute]
     attribute = [_prepare_attribute_parts(attribute_part) for attribute_part in attribute_parts]
 
@@ -122,6 +109,15 @@ def make_multi_attrgetter(environment, attribute, postprocess=None):
     return attrgetter
 
 
+def _prepare_attribute_parts(attr):
+    if attr is None:
+        return []
+    elif isinstance(attr, string_types):
+        return [int(x) if x.isdigit() else x for x in attr.split('.')]
+    else:
+        return [attr]
+
+
 def do_forceescape(value):
     """Enforce HTML escaping.  This will probably double escape variables."""
     if hasattr(value, '__html__'):