Skip to content

Commit 7e03322

Browse files
kddejongChuck Meyer
authored andcommitted
Move Rules classes into the rules directory from init (aws-cloudformation#1098)
1 parent 24cf5da commit 7e03322

File tree

151 files changed

+912
-756
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

151 files changed

+912
-756
lines changed

docs/getting_started/rules.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,8 @@ Use the following skeleton code as a starting point of your new rule:
5656
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
5757
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
5858
"""
59-
from cfnlint import CloudFormationLintRule
60-
from cfnlint import RuleMatch
59+
from cfnlint.rules import CloudFormationLintRule
60+
from cfnlint.rules import RuleMatch
6161

6262

6363
class MyNewRule(CloudFormationLintRule):

examples/rules/PropertiesTagsIncluded.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
1515
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
1616
"""
17-
from cfnlint import CloudFormationLintRule
18-
from cfnlint import RuleMatch
17+
from cfnlint.rules import CloudFormationLintRule
18+
from cfnlint.rules import RuleMatch
1919
import cfnlint.helpers
2020

2121

examples/rules/PropertiesTagsRequired.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
1515
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
1616
"""
17-
from cfnlint import CloudFormationLintRule
18-
from cfnlint import RuleMatch
17+
from cfnlint.rules import CloudFormationLintRule
18+
from cfnlint.rules import RuleMatch
1919

2020

2121
class PropertiesTagsRequired(CloudFormationLintRule):

src/cfnlint/__init__.py

Lines changed: 62 additions & 418 deletions
Large diffs are not rendered by default.

src/cfnlint/core.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
import os
1919
import sys
2020
from jsonschema.exceptions import ValidationError
21-
from cfnlint import RulesCollection
21+
from cfnlint.rules import RulesCollection
2222
import cfnlint.config
2323
import cfnlint.formatters
2424
import cfnlint.decode

src/cfnlint/decode/__init__.py

Lines changed: 40 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@
2323
JSONDecodeError = ValueError
2424
from yaml.parser import ParserError, ScannerError
2525
from yaml import YAMLError
26-
import cfnlint.decode.cfn_yaml
27-
import cfnlint.decode.cfn_json
26+
from cfnlint.decode import cfn_yaml, cfn_json
27+
from cfnlint.rules import Match, ParseError
2828

2929

3030
LOGGER = logging.getLogger(__name__)
@@ -37,24 +37,32 @@ def decode(filename, ignore_bad_template):
3737
template = None
3838
matches = []
3939
try:
40-
template = cfnlint.decode.cfn_yaml.load(filename)
40+
template = cfn_yaml.load(filename)
4141
except IOError as e:
4242
if e.errno == 2:
4343
LOGGER.error('Template file not found: %s', filename)
44-
matches.append(create_match_file_error(filename, 'Template file not found: %s' % filename))
44+
matches.append(create_match_file_error(
45+
filename, 'Template file not found: %s' % filename))
4546
elif e.errno == 21:
46-
LOGGER.error('Template references a directory, not a file: %s', filename)
47-
matches.append(create_match_file_error(filename, 'Template references a directory, not a file: %s' % filename))
47+
LOGGER.error('Template references a directory, not a file: %s',
48+
filename)
49+
matches.append(create_match_file_error(
50+
filename,
51+
'Template references a directory, not a file: %s' % filename))
4852
elif e.errno == 13:
49-
LOGGER.error('Permission denied when accessing template file: %s', filename)
50-
matches.append(create_match_file_error(filename, 'Permission denied when accessing template file: %s' % filename))
53+
LOGGER.error('Permission denied when accessing template file: %s',
54+
filename)
55+
matches.append(create_match_file_error(
56+
filename,
57+
'Permission denied when accessing template file: %s' % filename))
5158

5259
if matches:
5360
return(None, matches)
5461
except UnicodeDecodeError as err:
5562
LOGGER.error('Cannot read file contents: %s', filename)
56-
matches.append(create_match_file_error(filename, 'Cannot read file contents: %s' % filename))
57-
except cfnlint.decode.cfn_yaml.CfnParseError as err:
63+
matches.append(create_match_file_error(
64+
filename, 'Cannot read file contents: %s' % filename))
65+
except cfn_yaml.CfnParseError as err:
5866
err.match.Filename = filename
5967
matches = [err.match]
6068
except ParserError as err:
@@ -64,28 +72,36 @@ def decode(filename, ignore_bad_template):
6472
'found character \'\\t\' that cannot start any token',
6573
'found unknown escape character']:
6674
try:
67-
template = cfnlint.decode.cfn_json.load(filename)
68-
except cfnlint.decode.cfn_json.JSONDecodeError as json_err:
75+
template = cfn_json.load(filename)
76+
except cfn_json.JSONDecodeError as json_err:
6977
json_err.match.filename = filename
7078
matches = [json_err.match]
7179
except JSONDecodeError as json_err:
7280
matches = [create_match_json_parser_error(json_err, filename)]
7381
except Exception as json_err: # pylint: disable=W0703
7482
if ignore_bad_template:
75-
LOGGER.info('Template %s is malformed: %s', filename, err.problem)
76-
LOGGER.info('Tried to parse %s as JSON but got error: %s', filename, str(json_err))
83+
LOGGER.info('Template %s is malformed: %s',
84+
filename, err.problem)
85+
LOGGER.info('Tried to parse %s as JSON but got error: %s',
86+
filename, str(json_err))
7787
else:
78-
LOGGER.error('Template %s is malformed: %s', filename, err.problem)
79-
LOGGER.error('Tried to parse %s as JSON but got error: %s', filename, str(json_err))
80-
return(None, [create_match_file_error(filename, 'Tried to parse %s as JSON but got error: %s' % (filename, str(json_err)))])
88+
LOGGER.error(
89+
'Template %s is malformed: %s', filename, err.problem)
90+
LOGGER.error('Tried to parse %s as JSON but got error: %s',
91+
filename, str(json_err))
92+
return (None, [create_match_file_error(
93+
filename,
94+
'Tried to parse %s as JSON but got error: %s' % (
95+
filename, str(json_err)))])
8196
else:
8297
matches = [create_match_yaml_parser_error(err, filename)]
8398
except YAMLError as err:
8499
matches = [create_match_file_error(filename, err)]
85100

86101
if not isinstance(template, dict) and not matches:
87102
# Template isn't a dict which means nearly nothing will work
88-
matches = [cfnlint.Match(1, 1, 1, 1, filename, cfnlint.ParseError(), message='Template needs to be an object.')]
103+
matches = [Match(1, 1, 1, 1, filename, ParseError(),
104+
message='Template needs to be an object.')]
89105
return (template, matches)
90106

91107

@@ -94,16 +110,16 @@ def create_match_yaml_parser_error(parser_error, filename):
94110
lineno = parser_error.problem_mark.line + 1
95111
colno = parser_error.problem_mark.column + 1
96112
msg = parser_error.problem
97-
return cfnlint.Match(
113+
return Match(
98114
lineno, colno, lineno, colno + 1, filename,
99-
cfnlint.ParseError(), message=msg)
115+
ParseError(), message=msg)
100116

101117

102118
def create_match_file_error(filename, msg):
103119
"""Create a Match for a parser error"""
104-
return cfnlint.Match(
120+
return Match(
105121
linenumber=1, columnnumber=1, linenumberend=1, columnnumberend=2,
106-
filename=filename, rule=cfnlint.ParseError(), message=msg)
122+
filename=filename, rule=ParseError(), message=msg)
107123

108124

109125
def create_match_json_parser_error(parser_error, filename):
@@ -116,5 +132,5 @@ def create_match_json_parser_error(parser_error, filename):
116132
lineno = 1
117133
colno = 1
118134
msg = parser_error.message
119-
return cfnlint.Match(
120-
lineno, colno, lineno, colno + 1, filename, cfnlint.ParseError(), message=msg)
135+
return Match(
136+
lineno, colno, lineno, colno + 1, filename, ParseError(), message=msg)

src/cfnlint/decode/cfn_json.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,9 @@ def __init__(self, msg, doc, pos, key=' '):
7474
self.pos = pos
7575
self.lineno = lineno
7676
self.colno = colno
77-
self.match = cfnlint.Match(
77+
self.match = cfnlint.rules.Match(
7878
lineno, colno + 1, lineno,
79-
colno + 1 + len(key), '', cfnlint.ParseError(), message=msg)
79+
colno + 1 + len(key), '', cfnlint.rules.ParseError(), message=msg)
8080

8181
def __reduce__(self):
8282
return self.__class__, (self.msg, self.doc, self.pos)

src/cfnlint/decode/cfn_yaml.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ class CfnParseError(ConstructorError):
4747
"""
4848
Error thrown when the template contains Cfn Error
4949
"""
50+
5051
def __init__(self, filename, message, line_number, column_number, key=' '):
5152

5253
# Call the base class constructor with the parameters it needs
@@ -57,9 +58,9 @@ def __init__(self, filename, message, line_number, column_number, key=' '):
5758
self.line_number = line_number
5859
self.column_number = column_number
5960
self.message = message
60-
self.match = cfnlint.Match(
61+
self.match = cfnlint.rules.Match(
6162
line_number + 1, column_number + 1, line_number + 1,
62-
column_number + 1 + len(key), filename, cfnlint.ParseError(), message=message)
63+
column_number + 1 + len(key), filename, cfnlint.rules.ParseError(), message=message)
6364

6465

6566
class NodeConstructor(SafeConstructor):
@@ -91,7 +92,8 @@ def construct_yaml_map(self, node):
9192
if key in mapping:
9293
raise CfnParseError(
9394
self.filename,
94-
'Duplicate resource found "{}" (line {})'.format(key, key_node.start_mark.line + 1),
95+
'Duplicate resource found "{}" (line {})'.format(
96+
key, key_node.start_mark.line + 1),
9597
key_node.start_mark.line, key_node.start_mark.column, key)
9698
mapping[key] = value
9799

@@ -112,7 +114,8 @@ def construct_yaml_null_error(self, node):
112114
"""Throw a null error"""
113115
raise CfnParseError(
114116
self.filename,
115-
'Null value at line {0} column {1}'.format(node.start_mark.line + 1, node.start_mark.column + 1),
117+
'Null value at line {0} column {1}'.format(
118+
node.start_mark.line + 1, node.start_mark.column + 1),
116119
node.start_mark.line, node.start_mark.column, ' ')
117120

118121

@@ -138,6 +141,7 @@ class MarkedLoader(Reader, Scanner, Parser, Composer, NodeConstructor, Resolver)
138141
Class for marked loading YAML
139142
"""
140143
# pylint: disable=non-parent-init-called,super-init-not-called
144+
141145
def __init__(self, stream, filename):
142146
Reader.__init__(self, stream)
143147
Scanner.__init__(self)

src/cfnlint/formatters/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@
1515
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
1616
"""
1717
import json
18-
from cfnlint import Match
18+
from cfnlint.rules import Match
19+
1920

2021
class BaseFormatter(object):
2122
"""Base Formatter class"""

src/cfnlint/helpers.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ def onerror(os_error):
292292
mod = imp.load_module(pluginname, fh, filename, desc)
293293
for _, clazz in inspect.getmembers(mod, inspect.isclass):
294294
method_resolution = inspect.getmro(clazz)
295-
if [clz for clz in method_resolution[1:] if clz.__module__ == 'cfnlint' and clz.__name__ == 'CloudFormationLintRule']:
295+
if [clz for clz in method_resolution[1:] if clz.__module__ in ('cfnlint', 'cfnlint.rules') and clz.__name__ == 'CloudFormationLintRule']:
296296
# create and instance of subclasses of CloudFormationLintRule
297297
obj = clazz()
298298
result.append(obj)

0 commit comments

Comments
 (0)