How to use the yamllint.parser.token_or_comment_generator 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 / test_parser.py View on Github external
'- 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'))
        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'
github adrienverge / yamllint / tests / test_parser.py View on Github external
'- 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'))
        self.assertEqual(e[1], Comment(2, 1, '# trailing spaces    ', 0))

        e = [c for c in
             token_or_comment_generator('# block\n'
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[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'
                                        '- [ l, ist\n'
                                        ']   # inline comment\n'
github adrienverge / yamllint / tests / test_parser.py View on Github external
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'
                                        '- [ l, ist\n'
                                        ']   # inline comment\n'
                                        '- { m: ap\n'
                                        '}   # inline comment\n'
                                        '# block comment\n'
                                        '- data   # inline comment\n')
github adrienverge / yamllint / tests / test_parser.py View on Github external
e = list(token_or_comment_generator(''))
        self.assertEqual(len(e), 2)
        self.assertEqual(e[0].prev, None)
        self.assertIsInstance(e[0].curr, yaml.Token)
        self.assertIsInstance(e[0].next, yaml.Token)
        self.assertEqual(e[1].prev, e[0].curr)
        self.assertEqual(e[1].curr, e[0].next)
        self.assertEqual(e[1].next, None)

        e = list(token_or_comment_generator('---\n'
                                            'k: v\n'))
        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))
github adrienverge / yamllint / tests / test_parser.py View on Github external
def test_token_or_comment_generator(self):
        e = list(token_or_comment_generator(''))
        self.assertEqual(len(e), 2)
        self.assertEqual(e[0].prev, None)
        self.assertIsInstance(e[0].curr, yaml.Token)
        self.assertIsInstance(e[0].next, yaml.Token)
        self.assertEqual(e[1].prev, e[0].curr)
        self.assertEqual(e[1].curr, e[0].next)
        self.assertEqual(e[1].next, None)

        e = list(token_or_comment_generator('---\n'
                                            'k: v\n'))
        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'
github adrienverge / yamllint / tests / test_parser.py View on Github external
'# 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)
        self.assertFalse(e[0].is_inline())
        self.assertFalse(e[1].is_inline())
        self.assertTrue(e[2].is_inline())
github adrienverge / yamllint / tests / test_parser.py View on Github external
def test_token_or_comment_generator(self):
        e = list(token_or_comment_generator(''))
        self.assertEqual(len(e), 2)
        self.assertEqual(e[0].prev, None)
        self.assertIsInstance(e[0].curr, yaml.Token)
        self.assertIsInstance(e[0].next, yaml.Token)
        self.assertEqual(e[1].prev, e[0].curr)
        self.assertEqual(e[1].curr, e[0].next)
        self.assertEqual(e[1].next, None)

        e = list(token_or_comment_generator('---\n'
                                            'k: v\n'))
        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))