fix: replace codecs.open with io.open to fix yamllint (#528)

This commit is contained in:
Robert Kaussow 2023-01-16 21:56:06 +01:00 committed by GitHub
parent 1130a168d0
commit 129e6bab0f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,9 +1,9 @@
"""Standard definition.""" """Standard definition."""
import codecs
import copy import copy
import importlib import importlib
import inspect import inspect
import io
import os import os
import pathlib import pathlib
import re import re
@ -65,7 +65,7 @@ class StandardBase(object, metaclass=StandardExtendedMeta):
if not candidate.faulty: if not candidate.faulty:
try: try:
with codecs.open(candidate.path, mode="rb", encoding="utf-8") as f: with io.open(candidate.path, mode="r", encoding="utf-8") as f:
yamllines = parse_yaml_linenumbers(f, candidate.path) yamllines = parse_yaml_linenumbers(f, candidate.path)
except LaterError as ex: except LaterError as ex:
e = ex.original e = ex.original
@ -86,7 +86,7 @@ class StandardBase(object, metaclass=StandardExtendedMeta):
if not candidate.faulty: if not candidate.faulty:
try: try:
with codecs.open(candidate.path, mode="rb", encoding="utf-8") as f: with io.open(candidate.path, mode="r", encoding="utf-8") as f:
yamllines = parse_yaml_linenumbers(f, candidate.path) yamllines = parse_yaml_linenumbers(f, candidate.path)
if yamllines: if yamllines:
@ -132,7 +132,7 @@ class StandardBase(object, metaclass=StandardExtendedMeta):
if not candidate.faulty: if not candidate.faulty:
try: try:
with codecs.open(candidate.path, mode="rb", encoding="utf-8") as f: with io.open(candidate.path, mode="r", encoding="utf-8") as f:
yamllines = parse_yaml_linenumbers(f, candidate.path) yamllines = parse_yaml_linenumbers(f, candidate.path)
if yamllines: if yamllines:
@ -201,7 +201,7 @@ class StandardBase(object, metaclass=StandardExtendedMeta):
if not candidate.faulty: if not candidate.faulty:
try: try:
with codecs.open(candidate.path, mode="rb", encoding="utf-8") as f: with io.open(candidate.path, mode="r", encoding="utf-8") as f:
yaml.add_constructor( yaml.add_constructor(
UnsafeTag.yaml_tag, UnsafeTag.yaml_constructor, Loader=yaml.SafeLoader UnsafeTag.yaml_tag, UnsafeTag.yaml_constructor, Loader=yaml.SafeLoader
) )
@ -223,7 +223,7 @@ class StandardBase(object, metaclass=StandardExtendedMeta):
if not candidate.faulty: if not candidate.faulty:
try: try:
with codecs.open(candidate.path, mode="rb", encoding="utf-8") as f: with io.open(candidate.path, mode="r", encoding="utf-8") as f:
for problem in linter.run(f, YamlLintConfig(options)): for problem in linter.run(f, YamlLintConfig(options)):
errors.append(StandardBase.Error(problem.line, problem.desc)) errors.append(StandardBase.Error(problem.line, problem.desc))
except yaml.YAMLError as e: except yaml.YAMLError as e:
@ -231,6 +231,9 @@ class StandardBase(object, metaclass=StandardExtendedMeta):
StandardBase.Error(e.problem_mark.line + 1, f"syntax error: {e.problem}") StandardBase.Error(e.problem_mark.line + 1, f"syntax error: {e.problem}")
) )
candidate.faulty = True candidate.faulty = True
except (TypeError, ValueError) as e:
errors.append(StandardBase.Error(None, f"yamllint error: {e}"))
candidate.faulty = True
return errors return errors