-
Notifications
You must be signed in to change notification settings - Fork 2
/
CheckPackageNamespace.py
108 lines (86 loc) · 3.43 KB
/
CheckPackageNamespace.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# Copyright (C) 2023 tracetronic GmbH
#
# SPDX-License-Identifier: MIT
# -*- coding: utf-8 -*-
import re
from typing import List
from .api.CheckResult import CheckResult
from .api.AbstractPackageCheck import AbstractPackageCheck
from .helper.CheckType import CheckType
from .helper.ConfigKeys import ParameterKeys as pk
try:
from tts.core.logging import SPrint, WPrint, EPrint
from tts.core.api.internalApi.Api import Api
api = Api()
except:
from logging import info as SPrint, warning as WPrint, error as EPrint
# module type: mandatory
MODULE_TYPE = CheckType.PACKAGE.value
# keys declared in "parameters" in config.yaml:
# ParameterKeys.REGEX_PATTERN
# ParameterKeys.CUSTOM_MESSAGE
class CheckPackageNamespace(AbstractPackageCheck):
"""
Check Package Namespace
========================
Description
-----------
Checks whether the package file name is valid.
Instructions:
-----------
Specify the pattern of the checked packages in the YAML configuration file as Regex.
The parameter for this check is called <RegexPackageName> in the config.yaml.
Return messages:
---------------------
- "Please save the package <package_name>. Could not find folder location!"
- "<RegexPattern> is not a valid pattern!"
- "<PackageName> does not follow name pattern. <CustomMessage>"
- "<PackageName> does not follow name pattern: <Regex>"
Limitations
-----------
...
"""
def __init__(self, internalApi): # pylint: disable=W0613
"""
Constructor to load the check parameters from config.yaml
"""
super().__init__()
def GetName(self) -> str:
"""
Name to be shown in UI and used in the config.yaml
"""
return type(self).__name__
def check(self, test_item, parameters) -> List:
"""
Checks if package name matches regex pattern
"""
# init clean check result list
checkResults = []
package_name = test_item.GetName()
# Determine package type based on file location
if test_item.GetFilename() is None:
checkResults.append(
CheckResult(f'Please save the package "{package_name}". Could not find folder '
f'location!'))
else:
try:
regex = parameters[pk.REGEX_PATTERN]
re.compile(regex)
except KeyError:
checkResults.append(CheckResult(f'No pattern configuration provided. '
f'Please check "{self.config.config_rel_path}"!'))
except re.error:
checkResults.append(CheckResult(
f'"{parameters[pk.REGEX_PATTERN]}" is not a valid pattern. Check '
f'"{self.config.config_rel_path}"!'))
else:
# if re.compile is successful the package name check will be performed
if not re.match(regex, package_name):
# check if message for pattern should be more specific
if pk.CUSTOM_MESSAGE in parameters:
msg = f'{package_name} does not follow name pattern. ' \
f'{parameters.get(pk.CUSTOM_MESSAGE)}'
else:
msg = f'{package_name} does not follow name pattern: "{regex}"'
checkResults.append(CheckResult(msg))
return checkResults