-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcheck_names.py
70 lines (60 loc) · 2.39 KB
/
check_names.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
"""Checks module and package names for pep8 compliance."""
import argparse
import re
from enum import IntEnum
from pathlib import Path
try:
from exit_codes.exit_codes import ExitCode # pyright: ignore[reportAssignmentType]
except ImportError:
class ExitCode(IntEnum): # type: ignore[no-redef]
"""Redefine in case ExitCode is not installed."""
OS_FILE = 1
DATA_ERR = 2
OK = 0
SHORT_NAME_LIMIT = 30
def main() -> int:
"""Check the file."""
parser = argparse.ArgumentParser()
parser.add_argument("files", nargs="+")
args = parser.parse_args()
for file_to_check in args.files:
# verify file exists
file_path = Path(file_to_check)
if not file_path.exists():
print("ERROR: the file doesn't exist")
return ExitCode.OS_FILE
module_name = file_path.stem
package_name = file_path.parent.name
# check length for module and package name
if len(module_name) > SHORT_NAME_LIMIT:
print(f"ERROR: '{module_name}' is longer than {SHORT_NAME_LIMIT}")
return ExitCode.DATA_ERR
if len(package_name) > SHORT_NAME_LIMIT:
print(f"ERROR: '{package_name}' is longer than {SHORT_NAME_LIMIT}")
return ExitCode.DATA_ERR
# check module name
if not re.fullmatch("[A-Za-z_]+", module_name):
if re.fullmatch("[A-Za-z0-9_]+", module_name):
print(
f"WARNING: '{module_name}' has numbers - allowing but note this is"
" not 'strictly' to pep 8 best practices",
)
else:
print(f"ERROR: '{module_name}' is not all lowercase with underscores")
return ExitCode.DATA_ERR
# check package if exists
# check package name
if package_name.strip() != "" and not re.fullmatch("[A-Za-z]+", package_name):
if re.fullmatch("[A-Za-z0-9]+", package_name):
print(
f"WARNING: '{package_name}' has numbers - allowing but note"
" this is not 'strictly' to pep 8 best practices",
)
else:
print(
f"ERROR: '{package_name}' is not all lowercase with no underscores",
)
return ExitCode.DATA_ERR
return ExitCode.OK
if __name__ == "__main__":
raise SystemExit(main())