How to use the yamllint.parser.Comment function in yamllint

To help you get started, we’ve selected a few yamllint examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github adrienverge / yamllint / tests / rules / test_indentation.py View on Github external
def full_stack(self, source):
        conf = {'spaces': 2, 'indent-sequences': True,
                'check-multi-line-strings': False}
        context = {}
        output = ''
        for elem in [t for t in token_or_comment_generator(source)
                     if not isinstance(t, Comment)]:
            list(check(conf, elem.curr, elem.prev, elem.next, elem.nextnext,
                       context))

            token_type = (elem.curr.__class__.__name__
                          .replace('Token', '')
                          .replace('Block', 'B').replace('Flow', 'F')
                          .replace('Sequence', 'Seq')
                          .replace('Mapping', 'Map'))
            if token_type in ('StreamStart', 'StreamEnd'):
                continue
            output += '{:>9} {}\n'.format(token_type,
                                          self.format_stack(context['stack']))
        return output
github adrienverge / yamllint / tests / test_parser.py View on Github external
self.assertIsInstance(e[3].curr, yaml.KeyToken)
        self.assertIsInstance(e[5].curr, yaml.ValueToken)

        e = list(token_or_comment_generator('# start comment\n'
                                            '- a\n'
                                            '- key: val  # key=val\n'
                                            '# this is\n'
                                            '# a block     \n'
                                            '# comment\n'
                                            '- c\n'
                                            '# end comment\n'))
        self.assertEqual(len(e), 21)
        self.assertIsInstance(e[1], Comment)
        self.assertEqual(e[1], Comment(1, 1, '# start comment', 0))
        self.assertEqual(e[11], Comment(3, 13, '# key=val', 0))
        self.assertEqual(e[12], Comment(4, 1, '# this is', 0))
        self.assertEqual(e[13], Comment(5, 1, '# a block     ', 0))
        self.assertEqual(e[14], Comment(6, 1, '# comment', 0))
        self.assertEqual(e[18], Comment(8, 1, '# end comment', 0))

        e = list(token_or_comment_generator('---\n'
                                            '# no newline char'))
        self.assertEqual(e[2], Comment(2, 1, '# no newline char', 0))

        e = list(token_or_comment_generator('# just comment'))
        self.assertEqual(e[1], Comment(1, 1, '# just comment', 0))

        e = list(token_or_comment_generator('\n'
                                            '   # indented comment\n'))
        self.assertEqual(e[1], Comment(2, 4, '# indented comment', 0))

        e = list(token_or_comment_generator('\n'
github adrienverge / yamllint / tests / test_parser.py View on Github external
self.assertIsInstance(e[5].curr, yaml.ValueToken)

        e = list(token_or_comment_generator('# start comment\n'
                                            '- a\n'
                                            '- key: val  # key=val\n'
                                            '# this is\n'
                                            '# a block     \n'
                                            '# comment\n'
                                            '- c\n'
                                            '# end comment\n'))
        self.assertEqual(len(e), 21)
        self.assertIsInstance(e[1], Comment)
        self.assertEqual(e[1], Comment(1, 1, '# start comment', 0))
        self.assertEqual(e[11], Comment(3, 13, '# key=val', 0))
        self.assertEqual(e[12], Comment(4, 1, '# this is', 0))
        self.assertEqual(e[13], Comment(5, 1, '# a block     ', 0))
        self.assertEqual(e[14], Comment(6, 1, '# comment', 0))
        self.assertEqual(e[18], Comment(8, 1, '# end comment', 0))

        e = list(token_or_comment_generator('---\n'
                                            '# no newline char'))
        self.assertEqual(e[2], Comment(2, 1, '# no newline char', 0))

        e = list(token_or_comment_generator('# just comment'))
        self.assertEqual(e[1], Comment(1, 1, '# just comment', 0))

        e = list(token_or_comment_generator('\n'
                                            '   # indented comment\n'))
        self.assertEqual(e[1], Comment(2, 4, '# indented comment', 0))

        e = list(token_or_comment_generator('\n'
                                            '# trailing spaces    \n'))
github adrienverge / yamllint / tests / test_parser.py View on Github external
'# end comment\n'))
        self.assertEqual(len(e), 21)
        self.assertIsInstance(e[1], Comment)
        self.assertEqual(e[1], Comment(1, 1, '# start comment', 0))
        self.assertEqual(e[11], Comment(3, 13, '# key=val', 0))
        self.assertEqual(e[12], Comment(4, 1, '# this is', 0))
        self.assertEqual(e[13], Comment(5, 1, '# a block     ', 0))
        self.assertEqual(e[14], Comment(6, 1, '# comment', 0))
        self.assertEqual(e[18], Comment(8, 1, '# end comment', 0))

        e = list(token_or_comment_generator('---\n'
                                            '# no newline char'))
        self.assertEqual(e[2], Comment(2, 1, '# no newline char', 0))

        e = list(token_or_comment_generator('# just comment'))
        self.assertEqual(e[1], Comment(1, 1, '# just comment', 0))

        e = list(token_or_comment_generator('\n'
                                            '   # indented comment\n'))
        self.assertEqual(e[1], Comment(2, 4, '# indented comment', 0))

        e = list(token_or_comment_generator('\n'
                                            '# trailing spaces    \n'))
        self.assertEqual(e[1], Comment(2, 1, '# trailing spaces    ', 0))

        e = [c for c in
             token_or_comment_generator('# block\n'
                                        '# comment\n'
                                        '- data   # inline comment\n'
                                        '# block\n'
                                        '# comment\n'
                                        '- k: v   # inline comment\n'
github adrienverge / yamllint / tests / test_parser.py View on Github external
self.assertEqual(len(e), 9)
        self.assertIsInstance(e[3].curr, yaml.KeyToken)
        self.assertIsInstance(e[5].curr, yaml.ValueToken)

        e = list(token_or_comment_generator('# start comment\n'
                                            '- a\n'
                                            '- key: val  # key=val\n'
                                            '# this is\n'
                                            '# a block     \n'
                                            '# comment\n'
                                            '- c\n'
                                            '# end comment\n'))
        self.assertEqual(len(e), 21)
        self.assertIsInstance(e[1], Comment)
        self.assertEqual(e[1], Comment(1, 1, '# start comment', 0))
        self.assertEqual(e[11], Comment(3, 13, '# key=val', 0))
        self.assertEqual(e[12], Comment(4, 1, '# this is', 0))
        self.assertEqual(e[13], Comment(5, 1, '# a block     ', 0))
        self.assertEqual(e[14], Comment(6, 1, '# comment', 0))
        self.assertEqual(e[18], Comment(8, 1, '# end comment', 0))

        e = list(token_or_comment_generator('---\n'
                                            '# no newline char'))
        self.assertEqual(e[2], Comment(2, 1, '# no newline char', 0))

        e = list(token_or_comment_generator('# just comment'))
        self.assertEqual(e[1], Comment(1, 1, '# just comment', 0))

        e = list(token_or_comment_generator('\n'
                                            '   # indented comment\n'))
        self.assertEqual(e[1], Comment(2, 4, '# indented comment', 0))
github adrienverge / yamllint / tests / test_parser.py View on Github external
self.assertEqual(e[18], Comment(8, 1, '# end comment', 0))

        e = list(token_or_comment_generator('---\n'
                                            '# no newline char'))
        self.assertEqual(e[2], Comment(2, 1, '# no newline char', 0))

        e = list(token_or_comment_generator('# just comment'))
        self.assertEqual(e[1], Comment(1, 1, '# just comment', 0))

        e = list(token_or_comment_generator('\n'
                                            '   # indented comment\n'))
        self.assertEqual(e[1], Comment(2, 4, '# indented comment', 0))

        e = list(token_or_comment_generator('\n'
                                            '# trailing spaces    \n'))
        self.assertEqual(e[1], Comment(2, 1, '# trailing spaces    ', 0))

        e = [c for c in
             token_or_comment_generator('# block\n'
                                        '# comment\n'
                                        '- data   # inline comment\n'
                                        '# block\n'
                                        '# comment\n'
                                        '- k: v   # inline comment\n'
                                        '- [ l, ist\n'
                                        ']   # inline comment\n'
                                        '- { m: ap\n'
                                        '}   # inline comment\n'
                                        '# block comment\n'
                                        '- data   # inline comment\n')
             if isinstance(c, Comment)]
        self.assertEqual(len(e), 10)
github adrienverge / yamllint / yamllint / parser.py View on Github external
not isinstance(token1, yaml.StreamStartToken) and
          not isinstance(token2, yaml.StreamEndToken)):
        return
    else:
        buf = token1.end_mark.buffer[token1.end_mark.pointer:
                                     token2.start_mark.pointer]

    line_no = token1.end_mark.line + 1
    column_no = token1.end_mark.column + 1
    pointer = token1.end_mark.pointer

    comment_before = None
    for line in buf.split('\n'):
        pos = line.find('#')
        if pos != -1:
            comment = Comment(line_no, column_no + pos,
                              token1.end_mark.buffer, pointer + pos,
                              token1, token2, comment_before)
            yield comment

            comment_before = comment

        pointer += len(line) + 1
        line_no += 1
        column_no = 1
github adrienverge / yamllint / yamllint / linter.py View on Github external
disabled = DisableDirective()
    disabled_for_line = DisableLineDirective()
    disabled_for_next_line = DisableLineDirective()

    for elem in parser.token_or_comment_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,
                                          elem.nextnext,
                                          context[rule.ID]):
                    problem.rule = rule.ID
                    problem.level = rule_conf['level']
                    cache.append(problem)
        elif isinstance(elem, parser.Comment):
            for rule in comment_rules:
                rule_conf = conf.rules[rule.ID]
                for problem in rule.check(rule_conf, elem):
                    problem.rule = rule.ID
                    problem.level = rule_conf['level']
                    cache.append(problem)

            disabled.process_comment(elem)
            if elem.is_inline():
                disabled_for_line.process_comment(elem)
            else:
                disabled_for_next_line.process_comment(elem)
        elif isinstance(elem, parser.Line):
            for rule in line_rules:
                rule_conf = conf.rules[rule.ID]
                for problem in rule.check(rule_conf, elem):
github nzoschke / gofaas / vendor / pip / yamllint / linter.py View on Github external
disabled = DisableDirective()
    disabled_for_line = DisableLineDirective()
    disabled_for_next_line = DisableLineDirective()

    for elem in parser.token_or_comment_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,
                                          elem.nextnext,
                                          context[rule.ID]):
                    problem.rule = rule.ID
                    problem.level = rule_conf['level']
                    cache.append(problem)
        elif isinstance(elem, parser.Comment):
            for rule in comment_rules:
                rule_conf = conf.rules[rule.ID]
                for problem in rule.check(rule_conf, elem):
                    problem.rule = rule.ID
                    problem.level = rule_conf['level']
                    cache.append(problem)

            disabled.process_comment(elem)
            if elem.is_inline():
                disabled_for_line.process_comment(elem)
            else:
                disabled_for_next_line.process_comment(elem)
        elif isinstance(elem, parser.Line):
            for rule in line_rules:
                rule_conf = conf.rules[rule.ID]
                for problem in rule.check(rule_conf, elem):