Skip to content

Commit 0dce60c

Browse files
authored
Add type hints to each function (#159)
Signed-off-by: ParkSangsin <[email protected]>
1 parent 82c4a3f commit 0dce60c

File tree

9 files changed

+128
-48
lines changed

9 files changed

+128
-48
lines changed

setup.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,11 @@
5151
"Programming Language :: Python :: 3.8",
5252
"Programming Language :: Python :: 3.9"],
5353
install_requires=required,
54-
package_data={_PACKAGE_NAME: [os.path.join('resources', 'convert_license.json'), os.path.join(_LICENSE_DIR, '*'), os.path.join('templates', '*')]},
54+
package_data={
55+
_PACKAGE_NAME: [
56+
os.path.join('resources', 'convert_license.json'), os.path.join(_LICENSE_DIR, '*'), os.path.join('templates', '*')
57+
]
58+
},
5559
include_package_data=True,
5660
entry_points={
5761
"console_scripts": [

src/fosslight_oss_pkg/_convert.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,12 @@
1919
logger = logging.getLogger(LOGGER_NAME)
2020

2121

22-
def convert_report(base_path, output_name, format, need_log_file=True):
22+
def convert_report(
23+
base_path: str,
24+
output_name: str,
25+
format: str,
26+
need_log_file: bool = True
27+
) -> None:
2328
oss_yaml_files = []
2429
file_option_on = False
2530
output_report = ""

src/fosslight_oss_pkg/_parsing_excel.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
# SPDX-License-Identifier: GPL-3.0-only
55
import logging
66
import os
7+
from typing import Dict, List
78
from fosslight_util.constant import LOGGER_NAME
89
from fosslight_util.parsing_yaml import parsing_yml
910
from fosslight_util.output_format import write_output_file
@@ -17,7 +18,7 @@
1718
MAX_SHEET_NAME_LEN = 31
1819

1920

20-
def get_sheet_name(yaml_file, sheet_list):
21+
def get_sheet_name(yaml_file: str, sheet_list: Dict[str, List]) -> str:
2122
if len(yaml_file) > MAX_SHEET_NAME_LEN:
2223
yaml_file = yaml_file[0:MAX_SHEET_NAME_LEN]
2324

@@ -33,7 +34,12 @@ def get_sheet_name(yaml_file, sheet_list):
3334
return yaml_file
3435

3536

36-
def convert_yml_to_excel(oss_yaml_files, output_file, file_option_on, base_path):
37+
def convert_yml_to_excel(
38+
oss_yaml_files: List[str],
39+
output_file: str,
40+
file_option_on: bool,
41+
base_path: str
42+
) -> None:
3743
sheet_list = {}
3844
header = {}
3945

src/fosslight_prechecker/_add.py

Lines changed: 54 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import sys
1010
import fosslight_util.constant as constant
1111
import urllib.request
12+
import argparse
1213
from yaml import safe_dump
1314
from fosslight_util.set_log import init_log
1415
from fosslight_util.spdx_licenses import get_spdx_licenses_json, get_license_from_nick
@@ -24,6 +25,7 @@
2425
from reuse.project import Project
2526
from bs4 import BeautifulSoup
2627
from pathlib import Path
28+
from typing import List, Optional
2729

2830

2931
PKG_NAME = "fosslight_prechecker"
@@ -37,13 +39,13 @@
3739
logger = logging.getLogger(constant.LOGGER_NAME)
3840

3941

40-
def convert_to_spdx_style(input_string):
42+
def convert_to_spdx_style(input_string: str) -> str:
4143
input_string = input_string.replace(" ", "-")
4244
input_converted = f"LicenseRef-{input_string}"
4345
return input_converted
4446

4547

46-
def check_input_license_format(input_license):
48+
def check_input_license_format(input_license: str) -> str:
4749
for spdx in spdx_licenses:
4850
if input_license.casefold() == spdx.casefold():
4951
return spdx
@@ -66,7 +68,7 @@ def check_input_license_format(input_license):
6668
return converted_license
6769

6870

69-
def check_input_copyright_format(input_copyright):
71+
def check_input_copyright_format(input_copyright: str) -> bool:
7072
regex = re.compile(r'Copyright(\s)+(\(c\)\s)?\s*\d{4}(-\d{4})*(\s)+(\S)+')
7173
check_ok = True
7274

@@ -77,7 +79,7 @@ def check_input_copyright_format(input_copyright):
7779
return check_ok
7880

7981

80-
def input_license_while_running():
82+
def input_license_while_running() -> str:
8183
input_license = ""
8284

8385
logger.info("# Select a license to write in the license missing files ")
@@ -95,7 +97,7 @@ def input_license_while_running():
9597
return input_license
9698

9799

98-
def input_copyright_while_running():
100+
def input_copyright_while_running() -> Optional[str]:
99101
input_copyright = ""
100102
input_copyright = input("# Input Copyright to write in the copyright missing files (ex, <year> <name>): ")
101103
if input_copyright == 'Quit' or input_copyright == 'quit' or input_copyright == 'Q':
@@ -104,7 +106,7 @@ def input_copyright_while_running():
104106
return input_copyright
105107

106108

107-
def input_dl_url_while_running():
109+
def input_dl_url_while_running() -> Optional[str]:
108110
input_dl_url = ""
109111
input_dl_url = input("# Input Download URL to write to missing files (ex, https://github.com/fosslight/fosslight-prechecker): ")
110112
if input_dl_url == 'Quit' or input_dl_url == 'quit' or input_dl_url == 'Q':
@@ -113,7 +115,13 @@ def input_dl_url_while_running():
113115
return input_dl_url
114116

115117

116-
def add_dl_url_into_file(main_parser, project, path_to_find, input_dl_url, file_list):
118+
def add_dl_url_into_file(
119+
main_parser: argparse.ArgumentParser,
120+
project: Project,
121+
path_to_find: str,
122+
input_dl_url: str,
123+
file_list: List[str]
124+
) -> None:
117125
logger.info("\n# Adding Download Location into your files")
118126
logger.warning(f" * Your input DownloadLocation : {input_dl_url}")
119127
add_dl_url_list = [os.path.join(path_to_find, file) for file in file_list]
@@ -125,7 +133,12 @@ def add_dl_url_into_file(main_parser, project, path_to_find, input_dl_url, file_
125133
dump_error_msg(f"Error_to_add_url : {ex}")
126134

127135

128-
def add_license_into_file(main_parser, project, input_license, file_list):
136+
def add_license_into_file(
137+
main_parser: argparse.ArgumentParser,
138+
project: Project,
139+
input_license: str,
140+
file_list: List[str]
141+
) -> None:
129142
converted_license = check_input_license_format(input_license)
130143
logger.warning(f" * Your input license : {converted_license}")
131144
parsed_args = main_parser.parse_args(['addheader', '--license', str(converted_license)] + file_list)
@@ -135,7 +148,12 @@ def add_license_into_file(main_parser, project, input_license, file_list):
135148
dump_error_msg(f"Error_call_run_in_license : {ex}")
136149

137150

138-
def add_copyright_into_file(main_parser, project, input_copyright, file_list):
151+
def add_copyright_into_file(
152+
main_parser: argparse.ArgumentParser,
153+
project: Project,
154+
input_copyright: str,
155+
file_list: List[str]
156+
) -> None:
139157
input_copyright = f"Copyright {input_copyright}"
140158

141159
input_ok = check_input_copyright_format(input_copyright)
@@ -152,8 +170,16 @@ def add_copyright_into_file(main_parser, project, input_copyright, file_list):
152170
dump_error_msg(f"Error_call_run_in_copyright : {ex}")
153171

154172

155-
def set_missing_license_copyright(missing_license_filtered, missing_copyright_filtered, project,
156-
path_to_find, license, copyright, total_files_excluded, input_dl_url):
173+
def set_missing_license_copyright(
174+
missing_license_filtered: Optional[List[str]],
175+
missing_copyright_filtered: Optional[List[str]],
176+
project: Project,
177+
path_to_find: str,
178+
license: str,
179+
copyright: str,
180+
total_files_excluded: List[str],
181+
input_dl_url: str
182+
) -> None:
157183
input_license = ""
158184
input_copyright = ""
159185

@@ -218,15 +244,15 @@ def get_allfiles_list(path):
218244
dump_error_msg(f"Error - get all files list : {ex}")
219245

220246

221-
def save_result_log():
247+
def save_result_log() -> None:
222248
try:
223249
_str_final_result_log = safe_dump(_result_log, allow_unicode=True, sort_keys=True)
224250
logger.info(_str_final_result_log)
225251
except Exception as ex:
226252
logger.warning(f"Failed to print add result log. : {ex}")
227253

228254

229-
def copy_to_root(path_to_find, input_license):
255+
def copy_to_root(path_to_find: str, input_license: str) -> None:
230256
lic_file = f"{input_license}.txt"
231257
try:
232258
source = os.path.join(path_to_find, 'LICENSES', f'{lic_file}')
@@ -236,7 +262,7 @@ def copy_to_root(path_to_find, input_license):
236262
dump_error_msg(f"Error - Can't copy license file: {ex}")
237263

238264

239-
def lge_lic_download(path_to_find, input_license):
265+
def lge_lic_download(path_to_find: str, input_license: str) -> bool:
240266
success = False
241267

242268
input_license_url = input_license.replace(' ', '_').replace('/', '_').replace('LicenseRef-', '').replace('-', '_')
@@ -269,7 +295,7 @@ def lge_lic_download(path_to_find, input_license):
269295
return success
270296

271297

272-
def present_license_file(path_to_find, lic):
298+
def present_license_file(path_to_find: str, lic: str) -> bool:
273299
present = False
274300
lic_file_path = os.path.join(os.getcwd(), path_to_find, 'LICENSES')
275301
file_name = f"{lic}.txt"
@@ -278,7 +304,7 @@ def present_license_file(path_to_find, lic):
278304
return present
279305

280306

281-
def find_representative_license(path_to_find, input_license):
307+
def find_representative_license(path_to_find: str, input_license: str) -> None:
282308
files = []
283309
found_file = []
284310
found_license_file = False
@@ -324,7 +350,7 @@ def find_representative_license(path_to_find, input_license):
324350
dump_error_msg(f"Error - download representative license text: {ex}")
325351

326352

327-
def is_exclude_dir(dir_path):
353+
def is_exclude_dir(dir_path: str) -> Optional[bool]:
328354
if dir_path != "":
329355
dir_path = dir_path.lower()
330356
dir_path = dir_path if dir_path.endswith(
@@ -335,7 +361,7 @@ def is_exclude_dir(dir_path):
335361
return
336362

337363

338-
def download_oss_info_license(base_path, input_license=""):
364+
def download_oss_info_license(base_path: str, input_license: str = "") -> None:
339365
license_list = []
340366
converted_lic_list = []
341367
oss_yaml_files = []
@@ -370,7 +396,14 @@ def download_oss_info_license(base_path, input_license=""):
370396
logger.info(" # There is no license in the path \n")
371397

372398

373-
def add_content(target_path="", input_license="", input_copyright="", input_dl_url="", output_path="", need_log_file=True):
399+
def add_content(
400+
target_path: str = "",
401+
input_license: str = "",
402+
input_copyright: str = "",
403+
input_dl_url: str = "",
404+
output_path: str = "",
405+
need_log_file: bool = True
406+
) -> None:
374407
global _result_log, spdx_licenses
375408
_check_only_file_mode = False
376409
file_to_check_list = []
@@ -450,7 +483,8 @@ def add_content(target_path="", input_license="", input_copyright="", input_dl_u
450483
logger.info(f"\n# File list that have both license and copyright : {len(skip_files)} / {len(total_files_excluded)}")
451484

452485
# Filter by file extension
453-
total_files_excluded = [file for file in total_files_excluded if os.path.splitext(file)[1].lower() in EXTENSION_COMMENT_STYLE_MAP_LOWERCASE]
486+
total_files_excluded = [file for file in total_files_excluded
487+
if os.path.splitext(file)[1].lower() in EXTENSION_COMMENT_STYLE_MAP_LOWERCASE]
454488
missing_license = [file for file in missing_license if os.path.splitext(file)[1].lower() in EXTENSION_COMMENT_STYLE_MAP_LOWERCASE]
455489
missing_copyright = [file for file in missing_copyright if os.path.splitext(file)[1].lower() in EXTENSION_COMMENT_STYLE_MAP_LOWERCASE]
456490

src/fosslight_prechecker/_help.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,6 @@
4040
-u <dl_location>\t Download Location to add(ex, "https://github.com/fosslight/fosslight_prechecker")"""
4141

4242

43-
def print_help_msg(exitOpt=True):
43+
def print_help_msg(exitOpt: bool = True) -> None:
4444
helpMsg = PrintHelpMsg(_HELP_MESSAGE_PRECHECKER)
4545
helpMsg.print_help_msg(exitOpt)

0 commit comments

Comments
 (0)