Skip to content

Commit 3c63401

Browse files
Merge pull request #707 from richvdh/fix_acs_fixup
Fix automatic inversion of attribute map files
2 parents cf6fc80 + d05a7ff commit 3c63401

File tree

1 file changed

+36
-21
lines changed

1 file changed

+36
-21
lines changed

src/saml2/attribute_converter.py

Lines changed: 36 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -40,11 +40,8 @@ def load_maps(dirspec):
4040
for fil in os.listdir(dirspec):
4141
if fil.endswith(".py"):
4242
mod = import_module(fil[:-3])
43-
for key, item in mod.__dict__.items():
44-
if key.startswith("__"):
45-
continue
46-
if isinstance(item, dict) and "to" in item and "fro" in item:
47-
mapd[item["identifier"]] = item
43+
for item in _find_maps_in_module(mod):
44+
mapd[item["identifier"]] = item
4845

4946
return mapd
5047

@@ -54,7 +51,7 @@ def ac_factory(path=""):
5451
5552
:param path: The path to a directory where the attribute maps are expected
5653
to reside.
57-
:return: A AttributeConverter instance
54+
:return: A list of AttributeConverter instances
5855
"""
5956
acs = []
6057

@@ -65,30 +62,48 @@ def ac_factory(path=""):
6562
for fil in sorted(os.listdir(path)):
6663
if fil.endswith(".py"):
6764
mod = import_module(fil[:-3])
68-
for key, item in mod.__dict__.items():
69-
if key.startswith("__"):
70-
continue
71-
if isinstance(item,
72-
dict) and "to" in item and "fro" in item:
73-
atco = AttributeConverter(item["identifier"])
74-
atco.from_dict(item)
75-
acs.append(atco)
65+
acs.extend(_attribute_map_module_to_acs(mod))
7666
else:
7767
from saml2 import attributemaps
7868

7969
for typ in attributemaps.__all__:
8070
mod = import_module(".%s" % typ, "saml2.attributemaps")
81-
for key, item in mod.__dict__.items():
82-
if key.startswith("__"):
83-
continue
84-
if isinstance(item, dict) and "to" in item and "fro" in item:
85-
atco = AttributeConverter(item["identifier"])
86-
atco.from_dict(item)
87-
acs.append(atco)
71+
acs.extend(_attribute_map_module_to_acs(mod))
8872

8973
return acs
9074

9175

76+
def _attribute_map_module_to_acs(module):
77+
"""Scan an attribute map module and return any attribute maps defined
78+
79+
:param: module: the python map module
80+
:type: types.ModuleType
81+
:return: a generator yielding AttributeConverter defintions
82+
:rtype: typing.Iterable[AttributeConverter]
83+
"""
84+
for item in _find_maps_in_module(module):
85+
atco = AttributeConverter(item["identifier"])
86+
atco.from_dict(item)
87+
yield atco
88+
89+
90+
def _find_maps_in_module(module):
91+
"""Find attribute map dictionaries in a map file
92+
93+
:param: module: the python map module
94+
:type: types.ModuleType
95+
:return: a generator yielding dict objects which have the right shape
96+
:rtype: typing.Iterable[dict]
97+
"""
98+
for key, item in module.__dict__.items():
99+
if key.startswith("__"):
100+
continue
101+
if isinstance(item, dict) and "identifier" in item and (
102+
"to" in item or "fro" in item
103+
):
104+
yield item
105+
106+
92107
def to_local(acs, statement, allow_unknown_attributes=False):
93108
""" Replaces the attribute names in a attribute value assertion with the
94109
equivalent name from a local name format.

0 commit comments

Comments
 (0)