in src/cfnlint/decode/__init__.py [0:0]
def decode(filename):
"""
Decode filename into an object
"""
template = None
matches = []
try:
template = cfn_yaml.load(filename)
except IOError as e:
if e.errno == 2:
LOGGER.error('Template file not found: %s', filename)
matches.append(create_match_file_error(
filename, 'Template file not found: %s' % filename))
elif e.errno == 21:
LOGGER.error('Template references a directory, not a file: %s',
filename)
matches.append(create_match_file_error(
filename,
'Template references a directory, not a file: %s' % filename))
elif e.errno == 13:
LOGGER.error('Permission denied when accessing template file: %s',
filename)
matches.append(create_match_file_error(
filename,
'Permission denied when accessing template file: %s' % filename))
if matches:
return(None, matches)
except UnicodeDecodeError as err:
LOGGER.error('Cannot read file contents: %s', filename)
matches.append(create_match_file_error(
filename, 'Cannot read file contents: %s' % filename))
except cfn_yaml.CfnParseError as err:
matches = err.matches
except ParserError as err:
matches = [create_match_yaml_parser_error(err, filename)]
except ScannerError as err:
if err.problem in [
'found character \'\\t\' that cannot start any token',
'found unknown escape character'] or err.problem.startswith(
'found unknown escape character'):
try:
template = cfn_json.load(filename)
except cfn_json.JSONDecodeError as json_err:
for e in json_err.matches:
e.filename = filename
matches = json_err.matches
except JSONDecodeError as json_err:
if hasattr(json_err, 'message'):
if json_err.message == 'No JSON object could be decoded': # pylint: disable=no-member
matches = [create_match_yaml_parser_error(err, filename)]
else:
matches = [create_match_json_parser_error(json_err, filename)]
if hasattr(json_err, 'msg'):
if json_err.msg == 'Expecting value': # pylint: disable=no-member
matches = [create_match_yaml_parser_error(err, filename)]
else:
matches = [create_match_json_parser_error(json_err, filename)]
except Exception as json_err: # pylint: disable=W0703
LOGGER.error(
'Template %s is malformed: %s', filename, err.problem)
LOGGER.error('Tried to parse %s as JSON but got error: %s',
filename, str(json_err))
return (None, [create_match_file_error(
filename,
'Tried to parse %s as JSON but got error: %s' % (
filename, str(json_err)))])
else:
matches = [create_match_yaml_parser_error(err, filename)]
except YAMLError as err:
matches = [create_match_file_error(filename, err)]
if not isinstance(template, dict) and not matches:
# Template isn't a dict which means nearly nothing will work
matches = [Match(1, 1, 1, 1, filename, ParseError(),
message='Template needs to be an object.')]
return (template, matches)