-
Notifications
You must be signed in to change notification settings - Fork 129
Expand file tree
/
Copy pathrule_decorators.py
More file actions
131 lines (99 loc) · 5.22 KB
/
rule_decorators.py
File metadata and controls
131 lines (99 loc) · 5.22 KB
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
# -----------------------------------------------------------------------------
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License. See License.txt in the project root for
# license information.
# -----------------------------------------------------------------------------
# pylint: disable=line-too-long
from knack.util import CLIError
from .linter import RuleError, LinterSeverity
class BaseRule:
def __init__(self, severity):
if severity not in LinterSeverity:
raise CLIError("A {} rule has an invalid severity. Received {}; expected one of: {}"
.format(str(self.__class__), severity, list(LinterSeverity)))
self.severity = severity
# command_test_rule run once
class CommandCoverageRule(BaseRule):
def __call__(self, func):
def add_to_linter(linter_manager):
def wrapper():
linter = linter_manager.linter
try:
func(linter)
except RuleError as ex:
linter_manager.mark_rule_failure(self.severity)
yield (_create_violation_msg(ex, 'Repo: {}, Src Branch: {}, Target Branch: {}',
linter.git_repo, linter.git_source, linter.git_target),
(linter.git_source, linter.git_target),
func.__name__)
linter_manager.add_rule('command_test_coverage', func.__name__, wrapper, self.severity)
add_to_linter.linter_rule = True
return add_to_linter
# help_file_entry_rule
class HelpFileEntryRule(BaseRule):
def __call__(self, func):
return _get_decorator(func, 'help_file_entries', 'Help-Entry: `{}`', self.severity)
# command_rule
class CommandRule(BaseRule):
def __call__(self, func):
return _get_decorator(func, 'commands', 'Command: `{}`', self.severity)
# command_group_rule
class CommandGroupRule(BaseRule):
def __call__(self, func):
return _get_decorator(func, 'command_groups', 'Command-Group: `{}`', self.severity)
# parameter_rule
class ParameterRule(BaseRule):
def __call__(self, func):
def add_to_linter(linter_manager):
def wrapper():
linter = linter_manager.linter
for command_name in linter.commands:
for parameter_name in linter.get_command_parameters(command_name):
exclusion_parameters = linter_manager.exclusions.get(command_name, {}).get('parameters', {})
exclusions = exclusion_parameters.get(parameter_name, {}).get('rule_exclusions', [])
if func.__name__ not in exclusions:
try:
func(linter, command_name, parameter_name)
except RuleError as ex:
linter_manager.mark_rule_failure(self.severity)
yield (_create_violation_msg(ex, 'Parameter: {}, `{}`', command_name, parameter_name),
(command_name, parameter_name),
func.__name__)
linter_manager.add_rule('params', func.__name__, wrapper, self.severity)
add_to_linter.linter_rule = True
return add_to_linter
class ExtraCliLinterRule(BaseRule):
def __call__(self, func):
def add_to_linter(linter_manager):
def wrapper():
linter = linter_manager.linter
try:
func(linter)
except RuleError as ex:
linter_manager.mark_rule_failure(self.severity)
yield (_create_violation_msg(ex, 'Repo: {}, Src Branch: {}, Target Branch: {}, base meta path: {}, diff meta path: {} \n',
linter.git_repo, linter.git_source, linter.git_target, linter.base_meta_path, linter.diff_meta_path),
(linter.base_meta_path, linter.diff_meta_path),
func.__name__)
linter_manager.add_rule('extra_cli_linter', func.__name__, wrapper, self.severity)
add_to_linter.linter_rule = True
return add_to_linter
def _get_decorator(func, rule_group, print_format, severity):
def add_to_linter(linter_manager):
def wrapper():
linter = linter_manager.linter
# print('enter add to linter', len(getattr(linter, rule_group)))
for iter_entity in getattr(linter, rule_group):
exclusions = linter_manager.exclusions.get(iter_entity, {}).get('rule_exclusions', [])
if func.__name__ not in exclusions:
try:
func(linter, iter_entity)
except RuleError as ex:
linter_manager.mark_rule_failure(severity)
yield (_create_violation_msg(ex, print_format, iter_entity), iter_entity, func.__name__)
linter_manager.add_rule(rule_group, func.__name__, wrapper, severity)
add_to_linter.linter_rule = True
return add_to_linter
def _create_violation_msg(ex, format_string, *format_args):
violation_string = format_string.format(*format_args)
return ' {} - {}'.format(violation_string, ex)