summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRichard van der Hoff <richard@matrix.org>2020-08-10 19:03:43 +0100
committerRichard van der Hoff <richard@matrix.org>2020-08-10 19:16:30 +0100
commitf8aca945103ce587f9082c6cfa9b18ab73a3daf1 (patch)
treeb86d0f14c508c32b7bfcbcde9872c5742cb13bbd
parente5d0b4f0760144430d885165d41d777b59ef5d6a (diff)
downloadpysaml2-f8aca945103ce587f9082c6cfa9b18ab73a3daf1.tar.gz
Factor out common codepaths in attribute_converter
We have three copies of the code that looks for attribute map definitions in a python module: let's factor them out.
-rw-r--r--src/saml2/attribute_converter.py55
1 files changed, 34 insertions, 21 deletions
diff --git a/src/saml2/attribute_converter.py b/src/saml2/attribute_converter.py
index adfe659d..43c345ff 100644
--- a/src/saml2/attribute_converter.py
+++ b/src/saml2/attribute_converter.py
@@ -40,11 +40,8 @@ def load_maps(dirspec):
for fil in os.listdir(dirspec):
if fil.endswith(".py"):
mod = import_module(fil[:-3])
- for key, item in mod.__dict__.items():
- if key.startswith("__"):
- continue
- if isinstance(item, dict) and "to" in item and "fro" in item:
- mapd[item["identifier"]] = item
+ for item in _find_maps_in_module(mod):
+ mapd[item["identifier"]] = item
return mapd
@@ -54,7 +51,7 @@ def ac_factory(path=""):
:param path: The path to a directory where the attribute maps are expected
to reside.
- :return: A AttributeConverter instance
+ :return: A list of AttributeConverter instances
"""
acs = []
@@ -65,30 +62,46 @@ def ac_factory(path=""):
for fil in sorted(os.listdir(path)):
if fil.endswith(".py"):
mod = import_module(fil[:-3])
- for key, item in mod.__dict__.items():
- if key.startswith("__"):
- continue
- if isinstance(item,
- dict) and "to" in item and "fro" in item:
- atco = AttributeConverter(item["identifier"])
- atco.from_dict(item)
- acs.append(atco)
+ acs.extend(_attribute_map_module_to_acs(mod))
else:
from saml2 import attributemaps
for typ in attributemaps.__all__:
mod = import_module(".%s" % typ, "saml2.attributemaps")
- for key, item in mod.__dict__.items():
- if key.startswith("__"):
- continue
- if isinstance(item, dict) and "to" in item and "fro" in item:
- atco = AttributeConverter(item["identifier"])
- atco.from_dict(item)
- acs.append(atco)
+ acs.extend(_attribute_map_module_to_acs(mod))
return acs
+def _attribute_map_module_to_acs(module):
+ """Scan an attribute map module and return any attribute maps defined
+
+ :param: module: the python map module
+ :type: types.ModuleType
+ :return: a generator yielding AttributeConverter defintions
+ :rtype: typing.Iterable[AttributeConverter]
+ """
+ for item in _find_maps_in_module(module):
+ atco = AttributeConverter(item["identifier"])
+ atco.from_dict(item)
+ yield atco
+
+
+def _find_maps_in_module(module):
+ """Find attribute map dictionaries in a map file
+
+ :param: module: the python map module
+ :type: types.ModuleType
+ :return: a generator yielding dict objects which have the right shape
+ :rtype: typing.Iterable[dict]
+ """
+ for key, item in module.__dict__.items():
+ if key.startswith("__"):
+ continue
+ if isinstance(item, dict) and "to" in item and "fro" in item:
+ yield item
+
+
def to_local(acs, statement, allow_unknown_attributes=False):
""" Replaces the attribute names in a attribute value assertion with the
equivalent name from a local name format.