@@ -14,9 +14,9 @@ def scope(s):
1414 :param s: string to extract scope from (filtered string in mako template)
1515 :return: the scope
1616 """
17- if '@' not in s :
17+ if "@" not in s :
1818 raise ValueError ("Unscoped string" )
19- (local_part , _ , domain_part ) = s .partition ('@' )
19+ (local_part , _ , domain_part ) = s .partition ("@" )
2020 return domain_part
2121
2222
@@ -31,16 +31,22 @@ def __init__(self, internal_attributes):
3131 :param internal_attributes: A map of how to convert the attributes
3232 (dict[internal_name, dict[attribute_profile, external_name]])
3333 """
34- self .separator = "." # separator for nested attribute values, e.g. address.street_address
35- self .multivalue_separator = ";" # separates multiple values, e.g. when using templates
34+ self .separator = (
35+ "." # separator for nested attribute values, e.g. address.street_address
36+ )
37+ self .multivalue_separator = (
38+ ";" # separates multiple values, e.g. when using templates
39+ )
3640 self .from_internal_attributes = internal_attributes ["attributes" ]
3741 self .template_attributes = internal_attributes .get ("template_attributes" , None )
3842
3943 self .to_internal_attributes = defaultdict (dict )
4044 for internal_attribute_name , mappings in self .from_internal_attributes .items ():
4145 for profile , external_attribute_names in mappings .items ():
4246 for external_attribute_name in external_attribute_names :
43- self .to_internal_attributes [profile ][external_attribute_name ] = internal_attribute_name
47+ self .to_internal_attributes [profile ][
48+ external_attribute_name
49+ ] = internal_attribute_name
4450
4551 def to_internal_filter (self , attribute_profile , external_attribute_names ):
4652 """
@@ -59,7 +65,11 @@ def to_internal_filter(self, attribute_profile, external_attribute_names):
5965 try :
6066 profile_mapping = self .to_internal_attributes [attribute_profile ]
6167 except KeyError :
62- logline = "no attribute mapping found for the given attribute profile {}" .format (attribute_profile )
68+ logline = (
69+ "no attribute mapping found for the given attribute profile {}" .format (
70+ attribute_profile
71+ )
72+ )
6373 logger .warn (logline )
6474 # no attributes since the given profile is not configured
6575 return []
@@ -103,7 +113,9 @@ def to_internal(self, attribute_profile, external_dict):
103113 )
104114 if attribute_values : # Only insert key if it has some values
105115 logline = "backend attribute {external} mapped to {internal} ({value})" .format (
106- external = external_attribute_name , internal = internal_attribute_name , value = attribute_values
116+ external = external_attribute_name ,
117+ internal = internal_attribute_name ,
118+ value = attribute_values ,
107119 )
108120 logger .debug (logline )
109121 internal_dict [internal_attribute_name ] = attribute_values
@@ -112,7 +124,9 @@ def to_internal(self, attribute_profile, external_dict):
112124 external_attribute_name
113125 )
114126 logger .debug (logline )
115- internal_dict = self ._handle_template_attributes (attribute_profile , internal_dict )
127+ internal_dict = self ._handle_template_attributes (
128+ attribute_profile , internal_dict
129+ )
116130 return internal_dict
117131
118132 def _collate_attribute_values_by_priority_order (self , attribute_names , data ):
@@ -128,7 +142,11 @@ def _collate_attribute_values_by_priority_order(self, attribute_names, data):
128142 return result
129143
130144 def _render_attribute_template (self , template , data ):
131- t = Template (template , cache_enabled = True , imports = ["from satosa.attribute_mapping import scope" ])
145+ t = Template (
146+ template ,
147+ cache_enabled = True ,
148+ imports = ["from satosa.attribute_mapping import scope" ],
149+ )
132150 try :
133151 return t .render (** data ).split (self .multivalue_separator )
134152 except (NameError , TypeError ):
@@ -144,11 +162,19 @@ def _handle_template_attributes(self, attribute_profile, internal_dict):
144162 continue
145163
146164 external_attribute_name = mapping [attribute_profile ]
147- templates = [t for t in external_attribute_name if "$" in t ] # these looks like templates...
148- template_attribute_values = [self ._render_attribute_template (template , internal_dict ) for template in
149- templates ]
150- flattened_attribute_values = list (chain .from_iterable (template_attribute_values ))
151- attribute_values = flattened_attribute_values or internal_dict .get (internal_attribute_name , None )
165+ templates = [
166+ t for t in external_attribute_name if "$" in t
167+ ] # these looks like templates...
168+ template_attribute_values = [
169+ self ._render_attribute_template (template , internal_dict )
170+ for template in templates
171+ ]
172+ flattened_attribute_values = list (
173+ chain .from_iterable (template_attribute_values )
174+ )
175+ attribute_values = flattened_attribute_values or internal_dict .get (
176+ internal_attribute_name , None
177+ )
152178 if attribute_values : # only insert key if it has some values
153179 internal_dict [internal_attribute_name ] = attribute_values
154180
@@ -172,7 +198,9 @@ def _create_nested_attribute_value(self, nested_attribute_names, value):
172198 return {nested_attribute_names [0 ]: value }
173199
174200 # keep digging further into the nested attribute names
175- child_dict = self ._create_nested_attribute_value (nested_attribute_names [1 :], value )
201+ child_dict = self ._create_nested_attribute_value (
202+ nested_attribute_names [1 :], value
203+ )
176204 return {nested_attribute_names [0 ]: child_dict }
177205
178206 def from_internal (self , attribute_profile , internal_dict ):
@@ -190,10 +218,14 @@ def from_internal(self, attribute_profile, internal_dict):
190218 external_dict = {}
191219 for internal_attribute_name in internal_dict :
192220 try :
193- attribute_mapping = self .from_internal_attributes [internal_attribute_name ]
194- except KeyError :
195- logline = "no attribute mapping found for the internal attribute {}" .format (
221+ attribute_mapping = self .from_internal_attributes [
196222 internal_attribute_name
223+ ]
224+ except KeyError :
225+ logline = (
226+ "no attribute mapping found for the internal attribute {}" .format (
227+ internal_attribute_name
228+ )
197229 )
198230 logger .debug (logline )
199231 continue
@@ -206,20 +238,29 @@ def from_internal(self, attribute_profile, internal_dict):
206238 logger .debug (logline )
207239 continue
208240
209- external_attribute_names = self .from_internal_attributes [internal_attribute_name ][attribute_profile ]
241+ external_attribute_names = self .from_internal_attributes [
242+ internal_attribute_name
243+ ][attribute_profile ]
210244 # select the first attribute name
211245 external_attribute_name = external_attribute_names [0 ]
212- logline = "frontend attribute {external} mapped from {internal} ({value})" .format (
213- external = external_attribute_name , internal = internal_attribute_name , value = internal_dict [internal_attribute_name ]
246+ logline = (
247+ "frontend attribute {external} mapped from {internal} ({value})" .format (
248+ external = external_attribute_name ,
249+ internal = internal_attribute_name ,
250+ value = internal_dict [internal_attribute_name ],
251+ )
214252 )
215253 logger .debug (logline )
216254
217255 if self .separator in external_attribute_name :
218256 nested_attribute_names = external_attribute_name .split (self .separator )
219- nested_dict = self ._create_nested_attribute_value (nested_attribute_names [1 :],
220- internal_dict [internal_attribute_name ])
257+ nested_dict = self ._create_nested_attribute_value (
258+ nested_attribute_names [1 :], internal_dict [internal_attribute_name ]
259+ )
221260 external_dict [nested_attribute_names [0 ]] = nested_dict
222261 else :
223- external_dict [external_attribute_name ] = internal_dict [internal_attribute_name ]
262+ external_dict [external_attribute_name ] = internal_dict [
263+ internal_attribute_name
264+ ]
224265
225266 return external_dict
0 commit comments