From 05f1bed12bd25bf88d87bf9fcbc46bec52772309 Mon Sep 17 00:00:00 2001 From: Toshio Kuratomi Date: Wed, 1 Apr 2015 13:51:01 -0700 Subject: [PATCH] Use the node's start_mark to determine line and column. * Elminates a lot of logic in the AnsibleComposer class. * Update tests with new column offsets. The rule should now be consistently: Column is the start of the entry's value (so for strings, the first non-space after the entry beginning, for dicts, the first character of the first key) --- v2/ansible/parsing/yaml/composer.py | 33 ++---------------- v2/test/parsing/yaml/test_loader.py | 54 ++++++++++++++--------------- 2 files changed, 29 insertions(+), 58 deletions(-) diff --git a/v2/ansible/parsing/yaml/composer.py b/v2/ansible/parsing/yaml/composer.py index faf712253e..6bdee92fc3 100644 --- a/v2/ansible/parsing/yaml/composer.py +++ b/v2/ansible/parsing/yaml/composer.py @@ -24,42 +24,15 @@ from yaml.nodes import MappingNode, ScalarNode class AnsibleComposer(Composer): def __init__(self): - self.__mapping_starts = [] super(Composer, self).__init__() def compose_node(self, parent, index): # the line number where the previous token has ended (plus empty lines) node = Composer.compose_node(self, parent, index) - if isinstance(node, ScalarNode): - # Scalars are pretty easy -- assume they start on the current - # token's line (what about multiline strings? Perhaps we also - # need to use previous token ended + if isinstance(node, (ScalarNode, MappingNode)): node.__datasource__ = self.name node.__line__ = self.line - - # Need to investigate why this works... - if self.indents: - node.__column__ = self.indent + 1 - else: - node.__column__ = self.column +1 - elif isinstance(node, MappingNode): - node.__datasource__ = self.name - - # Need extra help to know where the mapping starts - try: - (cur_line, cur_column) = self.__mapping_starts.pop() - except: - cur_line = None - cur_column = None - node.__line__ = cur_line - node.__column__ = cur_column + node.__column__ = node.start_mark.column + 1 + node.__line__ = node.start_mark.line + 1 return node - - def compose_mapping_node(self, anchor): - # the column here will point at the position in the file immediately - # after the first key is found, which could be a space or a newline. - # We could back this up to find the beginning of the key, but this - # should be good enough to determine the error location. - self.__mapping_starts.append((self.line + 1, self.column + 1)) - return Composer.compose_mapping_node(self, anchor) diff --git a/v2/test/parsing/yaml/test_loader.py b/v2/test/parsing/yaml/test_loader.py index 4f08d8ea70..aba103d37f 100644 --- a/v2/test/parsing/yaml/test_loader.py +++ b/v2/test/parsing/yaml/test_loader.py @@ -83,18 +83,17 @@ class TestAnsibleLoaderBasic(unittest.TestCase): self.assertIsInstance(data.keys()[0], unicode) self.assertIsInstance(data.values()[0], unicode) - # Note: this is the beginning of the first value. - # May be changed in the future to beginning of the first key + # Beginning of the first key self.assertEqual(data._line_number, 2) - self.assertEqual(data._column_number, 25) + self.assertEqual(data._column_number, 17) self.assertEqual(data._data_source, 'myfile.yml') self.assertEqual(data[u'webster']._line_number, 2) - self.assertEqual(data[u'webster']._column_number, 17) + self.assertEqual(data[u'webster']._column_number, 26) self.assertEqual(data[u'webster']._data_source, 'myfile.yml') self.assertEqual(data[u'oed']._line_number, 3) - self.assertEqual(data[u'oed']._column_number, 17) + self.assertEqual(data[u'oed']._column_number, 22) self.assertEqual(data[u'oed']._data_source, 'myfile.yml') def test_parse_list(self): @@ -147,7 +146,6 @@ class TestAnsibleLoaderPlay(unittest.TestCase): pass def test_data_complete(self): - return self.assertEqual(len(self.data), 1) self.assertIsInstance(self.data, list) self.assertEqual(frozenset(self.data[0].keys()), frozenset((u'hosts', u'vars', u'tasks'))) @@ -198,23 +196,23 @@ class TestAnsibleLoaderPlay(unittest.TestCase): #self.assertEqual(self.data[0][u'vars'][u'number']._data_source, self.play_filename) self.assertEqual(self.data[0][u'vars'][u'string']._line_number, 5) - self.assertEqual(self.data[0][u'vars'][u'string']._column_number, 21) + self.assertEqual(self.data[0][u'vars'][u'string']._column_number, 29) self.assertEqual(self.data[0][u'vars'][u'string']._data_source, self.play_filename) self.assertEqual(self.data[0][u'vars'][u'utf8_string']._line_number, 6) - self.assertEqual(self.data[0][u'vars'][u'utf8_string']._column_number, 21) + self.assertEqual(self.data[0][u'vars'][u'utf8_string']._column_number, 34) self.assertEqual(self.data[0][u'vars'][u'utf8_string']._data_source, self.play_filename) self.assertEqual(self.data[0][u'vars'][u'dictionary']._line_number, 8) - self.assertEqual(self.data[0][u'vars'][u'dictionary']._column_number, 31) + self.assertEqual(self.data[0][u'vars'][u'dictionary']._column_number, 23) self.assertEqual(self.data[0][u'vars'][u'dictionary']._data_source, self.play_filename) self.assertEqual(self.data[0][u'vars'][u'dictionary'][u'webster']._line_number, 8) - self.assertEqual(self.data[0][u'vars'][u'dictionary'][u'webster']._column_number, 23) + self.assertEqual(self.data[0][u'vars'][u'dictionary'][u'webster']._column_number, 32) self.assertEqual(self.data[0][u'vars'][u'dictionary'][u'webster']._data_source, self.play_filename) self.assertEqual(self.data[0][u'vars'][u'dictionary'][u'oed']._line_number, 9) - self.assertEqual(self.data[0][u'vars'][u'dictionary'][u'oed']._column_number, 23) + self.assertEqual(self.data[0][u'vars'][u'dictionary'][u'oed']._column_number, 28) self.assertEqual(self.data[0][u'vars'][u'dictionary'][u'oed']._data_source, self.play_filename) # Lists don't yet have line/col information @@ -227,68 +225,68 @@ class TestAnsibleLoaderPlay(unittest.TestCase): # First Task # self.assertEqual(self.data[0][u'tasks'][0]._line_number, 16) - self.assertEqual(self.data[0][u'tasks'][0]._column_number, 28) + self.assertEqual(self.data[0][u'tasks'][0]._column_number, 23) self.assertEqual(self.data[0][u'tasks'][0]._data_source, self.play_filename) self.assertEqual(self.data[0][u'tasks'][0][u'name']._line_number, 16) - self.assertEqual(self.data[0][u'tasks'][0][u'name']._column_number, 23) + self.assertEqual(self.data[0][u'tasks'][0][u'name']._column_number, 29) self.assertEqual(self.data[0][u'tasks'][0][u'name']._data_source, self.play_filename) self.assertEqual(self.data[0][u'tasks'][0][u'ping']._line_number, 18) - self.assertEqual(self.data[0][u'tasks'][0][u'ping']._column_number, 30) + self.assertEqual(self.data[0][u'tasks'][0][u'ping']._column_number, 25) self.assertEqual(self.data[0][u'tasks'][0][u'ping']._data_source, self.play_filename) - #self.assertEqual(self.data[0][u'tasks'][0][u'ping'][u'data']._line_number, 18) - self.assertEqual(self.data[0][u'tasks'][0][u'ping'][u'data']._column_number, 25) + self.assertEqual(self.data[0][u'tasks'][0][u'ping'][u'data']._line_number, 18) + self.assertEqual(self.data[0][u'tasks'][0][u'ping'][u'data']._column_number, 31) self.assertEqual(self.data[0][u'tasks'][0][u'ping'][u'data']._data_source, self.play_filename) # # Second Task # self.assertEqual(self.data[0][u'tasks'][1]._line_number, 20) - self.assertEqual(self.data[0][u'tasks'][1]._column_number, 28) + self.assertEqual(self.data[0][u'tasks'][1]._column_number, 23) self.assertEqual(self.data[0][u'tasks'][1]._data_source, self.play_filename) self.assertEqual(self.data[0][u'tasks'][1][u'name']._line_number, 20) - self.assertEqual(self.data[0][u'tasks'][1][u'name']._column_number, 23) + self.assertEqual(self.data[0][u'tasks'][1][u'name']._column_number, 29) self.assertEqual(self.data[0][u'tasks'][1][u'name']._data_source, self.play_filename) self.assertEqual(self.data[0][u'tasks'][1][u'ping']._line_number, 22) - self.assertEqual(self.data[0][u'tasks'][1][u'ping']._column_number, 30) + self.assertEqual(self.data[0][u'tasks'][1][u'ping']._column_number, 25) self.assertEqual(self.data[0][u'tasks'][1][u'ping']._data_source, self.play_filename) - #self.assertEqual(self.data[0][u'tasks'][1][u'ping'][u'data']._line_number, 22) - self.assertEqual(self.data[0][u'tasks'][1][u'ping'][u'data']._column_number, 25) + self.assertEqual(self.data[0][u'tasks'][1][u'ping'][u'data']._line_number, 22) + self.assertEqual(self.data[0][u'tasks'][1][u'ping'][u'data']._column_number, 31) self.assertEqual(self.data[0][u'tasks'][1][u'ping'][u'data']._data_source, self.play_filename) # # Third Task # self.assertEqual(self.data[0][u'tasks'][2]._line_number, 24) - self.assertEqual(self.data[0][u'tasks'][2]._column_number, 28) + self.assertEqual(self.data[0][u'tasks'][2]._column_number, 23) self.assertEqual(self.data[0][u'tasks'][2]._data_source, self.play_filename) self.assertEqual(self.data[0][u'tasks'][2][u'name']._line_number, 24) - self.assertEqual(self.data[0][u'tasks'][2][u'name']._column_number, 23) + self.assertEqual(self.data[0][u'tasks'][2][u'name']._column_number, 29) self.assertEqual(self.data[0][u'tasks'][2][u'name']._data_source, self.play_filename) - #self.assertEqual(self.data[0][u'tasks'][2][u'command']._line_number, 25) - self.assertEqual(self.data[0][u'tasks'][2][u'command']._column_number, 23) + self.assertEqual(self.data[0][u'tasks'][2][u'command']._line_number, 25) + self.assertEqual(self.data[0][u'tasks'][2][u'command']._column_number, 32) self.assertEqual(self.data[0][u'tasks'][2][u'command']._data_source, self.play_filename) def test_line_numbers(self): # Check the line/column numbers are correct # Note: Remember, currently dicts begin at the start of their first entry's value self.assertEqual(self.data[0]._line_number, 2) - self.assertEqual(self.data[0]._column_number, 25) + self.assertEqual(self.data[0]._column_number, 19) self.assertEqual(self.data[0]._data_source, self.play_filename) self.assertEqual(self.data[0][u'hosts']._line_number, 2) - self.assertEqual(self.data[0][u'hosts']._column_number, 19) + self.assertEqual(self.data[0][u'hosts']._column_number, 26) self.assertEqual(self.data[0][u'hosts']._data_source, self.play_filename) self.assertEqual(self.data[0][u'vars']._line_number, 4) - self.assertEqual(self.data[0][u'vars']._column_number, 28) + self.assertEqual(self.data[0][u'vars']._column_number, 21) self.assertEqual(self.data[0][u'vars']._data_source, self.play_filename) self.check_vars()