Skip to content

Commit 6a5fa68

Browse files
committed
Apply f-string format
1 parent f458594 commit 6a5fa68

File tree

2 files changed

+43
-43
lines changed

2 files changed

+43
-43
lines changed

src/fosslight_reuse/_add.py

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ def get_licenses_from_json():
4848
with open(file_withpath, 'r') as f:
4949
licenses = json.load(f)
5050
except Exception as ex:
51-
print_error('Error to get license from json file :' + str(ex))
51+
print_error(f"Error to get license from json file : {ex}")
5252

5353
return licenses
5454

@@ -61,11 +61,11 @@ def check_file_extension(file_list):
6161
try:
6262
file_extension = os.path.splitext(file)[1].lower()
6363
if file_extension == "":
64-
logger.info(" No extension file(s) : " + file)
64+
logger.info(f" No extension file(s) : {file}")
6565
if file_extension in EXTENSION_COMMENT_STYLE_MAP_LOWERCASE:
6666
files_filtered.append(file)
6767
except Exception as ex:
68-
print_error("Error - Unknown error to check file extension - " + str(ex))
68+
print_error(f"Error - Unknown error to check file extension: {ex}")
6969

7070
return files_filtered
7171

@@ -90,7 +90,7 @@ def check_license_and_copyright(path_to_find, all_files, missing_license, missin
9090

9191
def convert_to_spdx_style(input_string):
9292
input_string = input_string.replace(" ", "-")
93-
input_converted = "LicenseRef-" + input_string
93+
input_converted = f"LicenseRef-{input_string}"
9494
return input_converted
9595

9696

@@ -103,7 +103,7 @@ def check_input_license_format(input_license):
103103

104104
licensesfromJson = get_licenses_from_json()
105105
if licensesfromJson == "":
106-
print_error(" Error - Return Value to get license from Json is none ")
106+
print_error(" Error - Return Value to get license from Json is none")
107107

108108
try:
109109
# Get frequetly used license from json file
@@ -161,13 +161,13 @@ def set_missing_license_copyright(missing_license_filtered, missing_copyright_fi
161161
try:
162162
main_parser = reuse_arg_parser()
163163
except Exception as ex:
164-
print_error('Error_get_arg_parser :' + str(ex))
164+
print_error(f"Error_get_arg_parser : {ex}")
165165

166166
# Print missing License
167167
if missing_license_filtered is not None and len(missing_license_filtered) > 0:
168168
missing_license_list = []
169169

170-
logger.info("# Missing license File(s) ")
170+
logger.info("# Missing license File(s)")
171171
for lic_file in sorted(missing_license_filtered):
172172
logger.info(f" * {lic_file}")
173173
missing_license_list.append(lic_file)
@@ -184,7 +184,7 @@ def set_missing_license_copyright(missing_license_filtered, missing_copyright_fi
184184
try:
185185
reuse_header(parsed_args, project)
186186
except Exception as ex:
187-
print_error('Error_call_run_in_license :' + str(ex))
187+
print_error(f"Error_call_run_in_license : {ex}")
188188
else:
189189
logger.info("# There is no missing license file\n")
190190

@@ -203,20 +203,20 @@ def set_missing_license_copyright(missing_license_filtered, missing_copyright_fi
203203
input_copyright = copyright
204204

205205
if input_copyright != "":
206-
input_copyright = 'Copyright ' + input_copyright
206+
input_copyright = f"Copyright {input_copyright}"
207207

208208
input_ok = check_input_copyright_format(input_copyright)
209209
if input_ok is False:
210210
return
211211

212212
logger.warning(f" * Your input Copyright : {input_copyright}")
213213
parsed_args = main_parser.parse_args(['addheader', '--copyright',
214-
'SPDX-FileCopyrightText: ' + str(input_copyright),
214+
f'SPDX-FileCopyrightText: {input_copyright}',
215215
'--exclude-year'] + missing_copyright_list)
216216
try:
217217
reuse_header(parsed_args, project)
218218
except Exception as ex:
219-
print_error('Error_call_run_in_copyright :' + str(ex))
219+
print_error(f"Error_call_run_in_copyright : {ex}")
220220
else:
221221
logger.info("\n# There is no missing copyright file\n")
222222

@@ -236,7 +236,7 @@ def get_allfiles_list(path):
236236
file_rel_path = os.path.relpath(file_abs_path, path)
237237
all_files.append(file_rel_path)
238238
except Exception as ex:
239-
print_error('Error_Get_AllFiles : ' + str(ex))
239+
print_error(f"Error_Get_AllFiles : {ex}")
240240

241241
return all_files
242242

@@ -246,17 +246,17 @@ def save_result_log():
246246
_str_final_result_log = safe_dump(_result_log, allow_unicode=True, sort_keys=True)
247247
logger.info(_str_final_result_log)
248248
except Exception as ex:
249-
logger.warning("Failed to print add result log. " + str(ex))
249+
logger.warning(f"Failed to print add result log. : {ex}")
250250

251251

252252
def copy_to_root(input_license):
253-
lic_file = str(input_license) + '.txt'
253+
lic_file = f"{input_license}.txt"
254254
try:
255255
source = os.path.join('LICENSES', f'{lic_file}')
256256
destination = 'LICENSE'
257257
shutil.copyfile(source, destination)
258258
except Exception as ex:
259-
print_error("Error - Can't copy license file: " + str(ex))
259+
print_error(f"Error - Can't copy license file: {ex}")
260260

261261

262262
def find_representative_license(path_to_find, input_license):
@@ -284,7 +284,7 @@ def find_representative_license(path_to_find, input_license):
284284
input_license = check_input_license_format(input_license)
285285

286286
logger.info(f" Input License : {input_license}")
287-
parsed_args = main_parser.parse_args(['download', str(input_license)])
287+
parsed_args = main_parser.parse_args(['download', f"{input_license}"])
288288

289289
try:
290290
logger.warning(" # There is no representative license file")
@@ -294,7 +294,7 @@ def find_representative_license(path_to_find, input_license):
294294
logger.warning(f" # Created Representative License File : {input_license}.txt")
295295

296296
except Exception as ex:
297-
print_error('Error - download representative license text:' + str(ex))
297+
print_error(f"Error - download representative license text: {ex}")
298298

299299

300300
def is_exclude_dir(dir_path):
@@ -338,7 +338,7 @@ def download_oss_info_license(base_path, input_license=""):
338338
try:
339339
reuse_download(parsed_args, prj)
340340
except Exception as ex:
341-
print_error('Error - download license text in OSS-pkg-info.yml :' + str(ex))
341+
print_error(f"Error - download license text in OSS-pkg-info.yml : {ex}")
342342
else:
343343
logger.info(" # There is no license in the path \n")
344344

@@ -356,7 +356,7 @@ def add_content(path_to_find="", file="", input_license="", input_copyright=""):
356356

357357
now = datetime.now().strftime('%Y%m%d_%H-%M-%S')
358358
output_dir = os.getcwd()
359-
logger, _result_log = init_log(os.path.join(output_dir, "fosslight_reuse_add_log_"+now+".txt"),
359+
logger, _result_log = init_log(os.path.join(output_dir, f"fosslight_reuse_add_log_{now}.txt"),
360360
True, logging.INFO, logging.DEBUG, PKG_NAME, path_to_find)
361361

362362
if file != "":
@@ -367,7 +367,7 @@ def add_content(path_to_find="", file="", input_license="", input_copyright=""):
367367
try:
368368
success, error_msg, licenses = get_spdx_licenses_json()
369369
if success is False:
370-
print_error('Error to get SPDX Licesens : ' + str(error_msg))
370+
print_error(f"Error to get SPDX Licesens : {error_msg}")
371371

372372
licenseInfo = licenses.get("licenses")
373373
for info in licenseInfo:
@@ -376,7 +376,7 @@ def add_content(path_to_find="", file="", input_license="", input_copyright=""):
376376
if isDeprecated is False:
377377
spdx_licenses.append(shortID)
378378
except Exception as ex:
379-
print_error('Error access to get_spdx_licenses_json : ' + str(ex))
379+
print_error(f"Error access to get_spdx_licenses_json : {ex}")
380380

381381
if input_license != "":
382382
find_representative_license(path_to_find, input_license)
@@ -396,11 +396,11 @@ def add_content(path_to_find="", file="", input_license="", input_copyright=""):
396396
if input_license != "":
397397
converted_license = check_input_license_format(input_license)
398398
logger.warning(f" * Your input license : {converted_license}")
399-
parsed_args = main_parser.parse_args(['addheader', '--license', str(converted_license)] + missing_license_list)
399+
parsed_args = main_parser.parse_args(['addheader', '--license', f"{converted_license}"] + missing_license_list)
400400
try:
401401
reuse_header(parsed_args, project)
402402
except Exception as ex:
403-
print_error('Error_call_run_in_license_file_only :' + str(ex))
403+
print_error(f"Error_call_run_in_license_file_only : {ex}")
404404
else:
405405
logger.info("# There is no missing license file")
406406

@@ -409,20 +409,20 @@ def add_content(path_to_find="", file="", input_license="", input_copyright=""):
409409
input_copyright = input_copyright_while_running()
410410

411411
if input_copyright != "":
412-
input_copyright = 'Copyright ' + input_copyright
412+
input_copyright = f"Copyright {input_copyright}"
413413

414414
input_ok = check_input_copyright_format(input_copyright)
415415
if input_ok is False:
416416
return
417417

418418
logger.warning(f" * Your input Copyright : {input_copyright}")
419419
parsed_args = main_parser.parse_args(['addheader', '--copyright',
420-
'SPDX-FileCopyrightText: ' + str(input_copyright),
420+
f"SPDX-FileCopyrightText: {input_copyright}",
421421
'--exclude-year'] + missing_copyright_list)
422422
try:
423423
reuse_header(parsed_args, project)
424424
except Exception as ex:
425-
print_error('Error_call_run_in_copyright_file_only :' + str(ex))
425+
print_error(f"Error_call_run_in_copyright_file_only : {ex}")
426426
else:
427427
logger.info("# There is no missing copyright file\n")
428428
# Path mode (-p option)

src/fosslight_reuse/_fosslight_reuse.py

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ def find_oss_pkg_info(path):
6161
_DEFAULT_EXCLUDE_EXTENSION_FILES.append(file_rel_path)
6262

6363
except Exception as ex:
64-
print_error('Error_FIND_OSS_PKG :' + str(ex))
64+
print_error(f"Error_FIND_OSS_PKG : {ex}")
6565

6666
return oss_pkg_info
6767

@@ -84,20 +84,20 @@ def create_reuse_dep5_file(path):
8484
else:
8585
dir_to_remove = ""
8686
if os.path.exists(reuse_config_file):
87-
file_to_remove = reuse_config_file + "_" + _start_time + ".bk"
87+
file_to_remove = f"{reuse_config_file}_{_start_time}.bk"
8888
shutil.copy2(reuse_config_file, file_to_remove)
8989
need_rollback = True
9090

9191
_DEFAULT_EXCLUDE_EXTENSION_FILES.extend(_DEFAULT_EXCLUDE_FOLDERS)
9292
for file_to_exclude in _DEFAULT_EXCLUDE_EXTENSION_FILES:
93-
str_contents += "\nFiles: " + file_to_exclude + "\nCopyright: -\nLicense: -\n"
93+
str_contents += f"\nFiles: {file_to_exclude} \nCopyright: -\nLicense: -\n"
9494

9595
with open(reuse_config_file, "a") as f:
9696
if not need_rollback:
9797
f.write(_DEFAULT_CONFIG_PREFIX)
9898
f.write(str_contents)
9999
except Exception as ex:
100-
print_error('Error_Create_Dep5 :' + str(ex))
100+
print_error(f"Error_Create_Dep5 : {ex}")
101101

102102
return need_rollback, file_to_remove, dir_to_remove
103103

@@ -114,7 +114,7 @@ def remove_reuse_dep5_file(rollback, file_to_remove, temp_dir_name):
114114
os.rmdir(temp_dir_name)
115115

116116
except Exception as ex:
117-
print_error('Error_Remove_Dep5 :' + str(ex))
117+
print_error(f"Error_Remove_Dep5 : {ex}")
118118

119119

120120
def reuse_for_files(path, files):
@@ -137,22 +137,22 @@ def reuse_for_files(path, files):
137137
if extension in _DEFAULT_EXCLUDE_EXTENSION:
138138
_DEFAULT_EXCLUDE_EXTENSION_FILES.append(file)
139139
else:
140-
logger.info("# " + file)
140+
logger.info(f"# {file}")
141141
rep = report.FileReport.generate(prj, file_abs_path)
142142

143-
logger.info("* License: " + ", ".join(rep.spdxfile.licenses_in_file))
144-
logger.info("* Copyright: " + rep.spdxfile.copyright + "\n")
143+
logger.info(f"* License: {', '.join(rep.spdxfile.licenses_in_file)}")
144+
logger.info(f"* Copyright: {rep.spdxfile.copyright}\n")
145145

146146
if rep.spdxfile.licenses_in_file is None or len(rep.spdxfile.licenses_in_file) == 0:
147147
missing_license_list.append(file)
148148
if rep.spdxfile.copyright is None or len(rep.spdxfile.copyright) == 0:
149149
missing_copyright_list.append(file)
150150

151151
except Exception as ex:
152-
print_error('Error_Reuse_for_file_to_read :' + str(ex))
152+
print_error(f"Error_Reuse_for_file_to_read : {ex}")
153153

154154
except Exception as ex:
155-
print_error('Error_Reuse_for_file :' + str(ex))
155+
print_error(f"Error_Reuse_for_file : {ex}")
156156
error_occurred = True
157157

158158
return missing_license_list, missing_copyright_list, error_occurred, prj
@@ -236,15 +236,15 @@ def result_for_summary(str_lint_result, oss_pkg_info, path, msg_missing_files):
236236
reuse_compliant = True
237237
str_oss_pkg += ", ".join(oss_pkg_info)
238238
except Exception as ex:
239-
print_error('Error_Print_OSS_PKG_INFO:' + str(ex))
239+
print_error(f"Error_Print_OSS_PKG_INFO: {ex}")
240240

241241
if msg_missing_files == "":
242242
reuse_compliant = True
243243

244244
# Add Summary Comment
245245
_SUMMARY_PREFIX = '# SUMMARY\n'
246246
_SUMMARY_SUFFIX = '\n\n' + _MSG_REFERENCE
247-
str_summary = _SUMMARY_PREFIX + str_oss_pkg + '\n' + str_lint_result + _SUMMARY_SUFFIX
247+
str_summary = f"{_SUMMARY_PREFIX}{str_oss_pkg}\n{str_lint_result}{_SUMMARY_SUFFIX}"
248248
items = ET.Element('error')
249249
items.set('id', 'rule_key_osc_checker_01')
250250
items.set('line', '0')
@@ -275,7 +275,7 @@ def result_for_missing_license_and_copyright_files(files_without_license, copyri
275275
items.set('msg', _MSG_FOLLOW_LIC_TXT)
276276
if _check_only_file_mode:
277277
_root_xml_item.append(items)
278-
str_missing_lic_files += ("* " + file_name + "\n")
278+
str_missing_lic_files += (f"* {file_name}\n")
279279

280280
for file_name in copyright_without_files:
281281
items = ET.Element('error')
@@ -285,7 +285,7 @@ def result_for_missing_license_and_copyright_files(files_without_license, copyri
285285
items.set('msg', _MSG_FOLLOW_LIC_TXT)
286286
if _check_only_file_mode:
287287
_root_xml_item.append(items)
288-
str_missing_cop_files += ("* " + file_name + "\n")
288+
str_missing_cop_files += (f"* {file_name}\n")
289289

290290
if _check_only_file_mode and _DEFAULT_EXCLUDE_EXTENSION_FILES is not None and len(
291291
_DEFAULT_EXCLUDE_EXTENSION_FILES) > 0:
@@ -311,13 +311,13 @@ def write_xml_and_exit(result_file: str, exit_code: int) -> None:
311311
for xml_item in error_items:
312312
logger.warning(xml_item.text)
313313
except Exception as ex:
314-
logger.error('Error_to_write_xml:', ex)
314+
logger.error(f"Error_to_write_xml: {ex}")
315315
exit_code = os.EX_IOERR
316316
try:
317317
_str_final_result_log = safe_dump(_result_log, allow_unicode=True, sort_keys=True)
318318
logger.info(_str_final_result_log)
319319
except Exception as ex:
320-
logger.warning("Failed to print result log. " + str(ex))
320+
logger.warning(f"Failed to print result log. {ex}")
321321
sys.exit(exit_code)
322322

323323

@@ -326,7 +326,7 @@ def init(path_to_find, result_file, file_list):
326326

327327
_start_time = datetime.now().strftime('%Y%m%d_%H-%M-%S')
328328
output_dir = os.path.dirname(os.path.abspath(result_file))
329-
logger, _result_log = init_log(os.path.join(output_dir, "fosslight_reuse_log_"+_start_time+".txt"),
329+
logger, _result_log = init_log(os.path.join(output_dir, f"fosslight_reuse_log_{_start_time}.txt"),
330330
True, logging.INFO, logging.DEBUG, _PKG_NAME, path_to_find)
331331
if file_list:
332332
_result_log["File list to check"] = file_list

0 commit comments

Comments
 (0)