3030from attributecode .attrib_util import multi_sort
3131
3232DEFAULT_TEMPLATE_FILE = os .path .join (
33- os .path .dirname (os .path .realpath (__file__ )), 'templates' , 'default_html.template' )
33+ os .path .dirname (os .path .realpath (__file__ )), "templates" , "default_html.template"
34+ )
3435
3536DEFAULT_TEMPLATE_SCANCODE_FILE = os .path .join (
36- os .path .dirname (os .path .realpath (__file__ )), 'templates' , 'scancode_html.template' )
37+ os .path .dirname (os .path .realpath (__file__ )), "templates" , "scancode_html.template"
38+ )
3739
3840DEFAULT_LICENSE_SCORE = 100
3941
4042
41- def generate (abouts , is_about_input , license_dict , scancode , min_license_score , template = None , vartext = None ):
43+ def generate (
44+ abouts , is_about_input , license_dict , scancode , min_license_score , template = None , vartext = None
45+ ):
4246 """
4347 Generate an attribution text from an `abouts` list of About objects, a
4448 `template` template text and a `vartext` optional dict of extra
@@ -53,9 +57,7 @@ def generate(abouts, is_about_input, license_dict, scancode, min_license_score,
5357 if template_error :
5458 lineno , message = template_error
5559 error = Error (
56- CRITICAL ,
57- 'Template validation error at line: {lineno}: "{message}"' .format (
58- ** locals ())
60+ CRITICAL , 'Template validation error at line: {lineno}: "{message}"' .format (** locals ())
5961 )
6062 errors .append (error )
6163 return error , None
@@ -87,14 +89,13 @@ def generate(abouts, is_about_input, license_dict, scancode, min_license_score,
8789 filename = list (about .license_file .value .keys ())[index ]
8890 text = list (about .license_file .value .values ())[index ]
8991 else :
90- error = Error (
91- CRITICAL , 'No license file found for ' + name )
92+ error = Error (CRITICAL , "No license file found for " + name )
9293 errors .append (error )
9394 break
9495 if about .license_url .value :
9596 url = about .license_url .value [index ]
9697 else :
97- url = ''
98+ url = ""
9899 license_object = License (key , name , filename , url , text )
99100 licenses_list .append (license_object )
100101 index = index + 1
@@ -114,7 +115,8 @@ def generate(abouts, is_about_input, license_dict, scancode, min_license_score,
114115 # The process will update the license_key, license_name and license_score.
115116 if scancode :
116117 abouts , meet_score_licenses_list = generate_sctk_input (
117- abouts , min_license_score , license_dict )
118+ abouts , min_license_score , license_dict
119+ )
118120 # Remove the license object
119121 remove_list = []
120122 for lic in licenses_list :
@@ -126,7 +128,7 @@ def generate(abouts, is_about_input, license_dict, scancode, min_license_score,
126128
127129 for about in abouts :
128130 # Create a license expression with license name
129- lic_name_expression = ''
131+ lic_name_expression = ""
130132 lic_name_expression_list = []
131133 if about .license_expression .value :
132134 for segment in about .license_expression .value .split ():
@@ -139,12 +141,13 @@ def generate(abouts, is_about_input, license_dict, scancode, min_license_score,
139141 if not_lic :
140142 lic_name_expression_list .append (segment )
141143 # Join the license name expression into a single string
142- lic_name_expression = ' ' .join (lic_name_expression_list )
144+ lic_name_expression = " " .join (lic_name_expression_list )
143145
144146 # Add the license name expression string into the about object as a custom field
145147 custom_field = StringField (
146- name = 'license_name_expression' , value = lic_name_expression , present = True )
147- setattr (about , 'license_name_expression' , custom_field )
148+ name = "license_name_expression" , value = lic_name_expression , present = True
149+ )
150+ setattr (about , "license_name_expression" , custom_field )
148151
149152 # Sort the about objects by name
150153 abouts = sorted (abouts , key = lambda x : x .name .value .lower ())
@@ -158,7 +161,7 @@ def generate(abouts, is_about_input, license_dict, scancode, min_license_score,
158161 licenses_list = licenses_list ,
159162 utcnow = utcnow ,
160163 tkversion = __version__ ,
161- vartext = vartext
164+ vartext = vartext ,
162165 )
163166
164167 return errors , rendered
@@ -211,13 +214,11 @@ def generate_sctk_input(abouts, min_license_score, license_dict):
211214 previous_score , _name = updated_dict [key ]
212215 current_score = lic_score [index ]
213216 if current_score > previous_score :
214- updated_dict [key ] = (
215- lic_score [index ], lic_name [index ])
217+ updated_dict [key ] = (lic_score [index ], lic_name [index ])
216218 # Track the duplicated index
217219 removed_index .append (index )
218220 else :
219- updated_dict [key ] = (
220- lic_score [index ], lic_name [index ])
221+ updated_dict [key ] = (lic_score [index ], lic_name [index ])
221222 updated_lic_key_expression .append (key )
222223 # Remove the duplication
223224 for index , key in enumerate (about .license_key .value ):
@@ -231,8 +232,7 @@ def generate_sctk_input(abouts, min_license_score, license_dict):
231232 updated_lic_name = []
232233 updated_lic_score = []
233234 for index , lic in enumerate (updated_dict ):
234- _sp_char , lic_keys , _invalid_lic_exp = parse_license_expression (
235- lic )
235+ _sp_char , lic_keys , _invalid_lic_exp = parse_license_expression (lic )
236236 score , name = updated_dict [lic ]
237237 if score >= min_license_score :
238238 for lic_key in lic_keys :
@@ -259,10 +259,10 @@ def generate_sctk_input(abouts, min_license_score, license_dict):
259259
260260
261261def get_license_file_key (license_text_name ):
262- if license_text_name .endswith (' .LICENSE' ):
262+ if license_text_name .endswith (" .LICENSE" ):
263263 # See https://github.com/aboutcode-org/aboutcode-toolkit/issues/439
264264 # for why using split instead of strip
265- return license_text_name .rsplit ('.' , 1 )[0 ]
265+ return license_text_name .rsplit ("." , 1 )[0 ]
266266 else :
267267 return license_text_name
268268
@@ -273,13 +273,21 @@ def check_template(template_string):
273273 message) if the template is invalid or None if it is valid.
274274 """
275275 try :
276- jinja2 .filters .FILTERS [' multi_sort' ] = multi_sort
276+ jinja2 .filters .FILTERS [" multi_sort" ] = multi_sort
277277 jinja2 .Template (template_string )
278278 except (jinja2 .TemplateSyntaxError , jinja2 .TemplateAssertionError ) as e :
279279 return e .lineno , e .message
280280
281281
282- def generate_from_file (abouts , is_about_input , license_dict , scancode , min_license_score , template_loc = None , vartext = None ):
282+ def generate_from_file (
283+ abouts ,
284+ is_about_input ,
285+ license_dict ,
286+ scancode ,
287+ min_license_score ,
288+ template_loc = None ,
289+ vartext = None ,
290+ ):
283291 """
284292 Generate an attribution text from an `abouts` list of About objects, a
285293 `template_loc` template file location and a `vartext` optional
@@ -295,12 +303,29 @@ def generate_from_file(abouts, is_about_input, license_dict, scancode, min_licen
295303 template_loc = add_unc (DEFAULT_TEMPLATE_FILE )
296304 else :
297305 template_loc = add_unc (template_loc )
298- with open (template_loc , encoding = ' utf-8' , errors = ' replace' ) as tplf :
306+ with open (template_loc , encoding = " utf-8" , errors = " replace" ) as tplf :
299307 tpls = tplf .read ()
300- return generate (abouts , is_about_input , license_dict , scancode , min_license_score , template = tpls , vartext = vartext )
308+ return generate (
309+ abouts ,
310+ is_about_input ,
311+ license_dict ,
312+ scancode ,
313+ min_license_score ,
314+ template = tpls ,
315+ vartext = vartext ,
316+ )
301317
302318
303- def generate_and_save (abouts , is_about_input , license_dict , output_location , scancode = False , min_license_score = 0 , template_loc = None , vartext = None ):
319+ def generate_and_save (
320+ abouts ,
321+ is_about_input ,
322+ license_dict ,
323+ output_location ,
324+ scancode = False ,
325+ min_license_score = 0 ,
326+ template_loc = None ,
327+ vartext = None ,
328+ ):
304329 """
305330 Generate an attribution text from an `abouts` list of About objects, a
306331 `template_loc` template file location and a `vartext` optional
@@ -314,14 +339,15 @@ def generate_and_save(abouts, is_about_input, license_dict, output_location, sca
314339 if not about .license_expression .value :
315340 continue
316341 special_char_in_expression , lic_list , invalid_lic_exp = parse_license_expression (
317- about .license_expression .value )
342+ about .license_expression .value
343+ )
318344 if special_char_in_expression or invalid_lic_exp :
319345 if special_char_in_expression :
320- msg = (u"The following character(s) cannot be in the license_expression: " +
321- str (special_char_in_expression ))
346+ msg = "The following character(s) cannot be in the license_expression: " + str (
347+ special_char_in_expression
348+ )
322349 else :
323- msg = (u"This license_expression is invalid: " +
324- str (invalid_lic_exp ))
350+ msg = "This license_expression is invalid: " + str (invalid_lic_exp )
325351 errors .append (Error (ERROR , msg ))
326352
327353 rendering_error , rendered = generate_from_file (
@@ -339,7 +365,7 @@ def generate_and_save(abouts, is_about_input, license_dict, output_location, sca
339365
340366 if rendered :
341367 output_location = add_unc (output_location )
342- with open (output_location , 'w' , encoding = ' utf-8' , errors = ' replace' ) as of :
368+ with open (output_location , "w" , encoding = " utf-8" , errors = " replace" ) as of :
343369 of .write (rendered )
344370
345371 return errors , rendered
0 commit comments