Refactor project layout to import yamllint alone
Currently importing yamllint recursively imports its submodules, which finally requires having pyyaml installed. This is a problem when you just want to import APP_VERSION from yamllint. For instance, setup.py imports yamllint to know the version, but doesn't know yet that pyyaml is to be installed, because it is stated in setup.py itself. To solve this, yamllint/__init__.py will only contain constants. The linting functions will be in yamllint/linter.py.
This commit is contained in:
@@ -14,12 +14,6 @@
|
||||
# You should have received a copy of the GNU General Public License
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
import yaml
|
||||
|
||||
from yamllint.errors import LintProblem
|
||||
from yamllint import parser
|
||||
|
||||
|
||||
APP_NAME = 'yamllint'
|
||||
APP_VERSION = '0.5.1'
|
||||
APP_DESCRIPTION = 'A linter for YAML files.'
|
||||
@@ -28,89 +22,3 @@ __author__ = u'Adrien Vergé'
|
||||
__copyright__ = u'Copyright 2016, Adrien Vergé'
|
||||
__license__ = 'GPLv3'
|
||||
__version__ = APP_VERSION
|
||||
|
||||
|
||||
def get_costemic_problems(buffer, conf):
|
||||
rules = conf.enabled_rules()
|
||||
|
||||
# Split token rules from line rules
|
||||
token_rules = [r for r in rules if r.TYPE == 'token']
|
||||
line_rules = [r for r in rules if r.TYPE == 'line']
|
||||
|
||||
context = {}
|
||||
for rule in token_rules:
|
||||
context[rule.ID] = {}
|
||||
|
||||
for elem in parser.token_or_line_generator(buffer):
|
||||
if isinstance(elem, parser.Token):
|
||||
for rule in token_rules:
|
||||
rule_conf = conf.rules[rule.ID]
|
||||
for problem in rule.check(rule_conf,
|
||||
elem.curr, elem.prev, elem.next,
|
||||
context[rule.ID]):
|
||||
problem.rule = rule.ID
|
||||
problem.level = rule_conf['level']
|
||||
yield problem
|
||||
elif isinstance(elem, parser.Line):
|
||||
for rule in line_rules:
|
||||
rule_conf = conf.rules[rule.ID]
|
||||
for problem in rule.check(rule_conf, elem):
|
||||
problem.rule = rule.ID
|
||||
problem.level = rule_conf['level']
|
||||
yield problem
|
||||
|
||||
|
||||
def get_syntax_error(buffer):
|
||||
try:
|
||||
list(yaml.parse(buffer, Loader=yaml.BaseLoader))
|
||||
except yaml.error.MarkedYAMLError as e:
|
||||
problem = LintProblem(e.problem_mark.line + 1,
|
||||
e.problem_mark.column + 1,
|
||||
'syntax error: ' + e.problem)
|
||||
problem.level = 'error'
|
||||
return problem
|
||||
|
||||
|
||||
def _lint(buffer, conf):
|
||||
# If the document contains a syntax error, save it and yield it at the
|
||||
# right line
|
||||
syntax_error = get_syntax_error(buffer)
|
||||
|
||||
for problem in get_costemic_problems(buffer, conf):
|
||||
# Insert the syntax error (if any) at the right place...
|
||||
if (syntax_error and syntax_error.line <= problem.line and
|
||||
syntax_error.column <= problem.column):
|
||||
yield syntax_error
|
||||
|
||||
# If there is already a yamllint error at the same place, discard
|
||||
# it as it is probably redundant (and maybe it's just a 'warning',
|
||||
# in which case the script won't even exit with a failure status).
|
||||
if (syntax_error.line == problem.line and
|
||||
syntax_error.column == problem.column):
|
||||
syntax_error = None
|
||||
continue
|
||||
|
||||
syntax_error = None
|
||||
|
||||
yield problem
|
||||
|
||||
if syntax_error:
|
||||
yield syntax_error
|
||||
|
||||
|
||||
def lint(input, conf):
|
||||
"""Lints a YAML source.
|
||||
|
||||
Returns a generator of LintProblem objects.
|
||||
|
||||
:param input: buffer, string or stream to read from
|
||||
:param conf: yamllint configuration object
|
||||
"""
|
||||
if type(input) == str:
|
||||
return _lint(input, conf)
|
||||
elif hasattr(input, 'read'): # Python 2's file or Python 3's io.IOBase
|
||||
# We need to have everything in memory to parse correctly
|
||||
content = input.read()
|
||||
return _lint(content, conf)
|
||||
else:
|
||||
raise TypeError('input should be a string or a stream')
|
||||
|
||||
Reference in New Issue
Block a user