@@ -40,11 +40,8 @@ def load_maps(dirspec):
40
40
for fil in os .listdir (dirspec ):
41
41
if fil .endswith (".py" ):
42
42
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
48
45
49
46
return mapd
50
47
@@ -54,7 +51,7 @@ def ac_factory(path=""):
54
51
55
52
:param path: The path to a directory where the attribute maps are expected
56
53
to reside.
57
- :return: A AttributeConverter instance
54
+ :return: A list of AttributeConverter instances
58
55
"""
59
56
acs = []
60
57
@@ -65,30 +62,46 @@ def ac_factory(path=""):
65
62
for fil in sorted (os .listdir (path )):
66
63
if fil .endswith (".py" ):
67
64
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 ))
76
66
else :
77
67
from saml2 import attributemaps
78
68
79
69
for typ in attributemaps .__all__ :
80
70
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 ))
88
72
89
73
return acs
90
74
91
75
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 "to" in item and "fro" in item :
102
+ yield item
103
+
104
+
92
105
def to_local (acs , statement , allow_unknown_attributes = False ):
93
106
""" Replaces the attribute names in a attribute value assertion with the
94
107
equivalent name from a local name format.
0 commit comments