Revision: 3326
Author: janne.t.harkonen
Date: Wed May 19 03:09:49 2010
Log: Added support for comments in imports and metadata
http://code.google.com/p/robotframework/source/detail?r=3326

Modified:
 /trunk/src/robot/parsing/newmodel.py
 /trunk/src/robot/parsing/populator.py
 /trunk/src/robot/parsing/settings.py
 /trunk/utest/parsing/test_populator.py

=======================================
--- /trunk/src/robot/parsing/newmodel.py        Wed May 19 01:37:06 2010
+++ /trunk/src/robot/parsing/newmodel.py        Wed May 19 03:09:49 2010
@@ -85,20 +85,20 @@
         self.metadata = []
         self.imports = []

-    def add_metadata(self, name, value):
-        self.metadata.append(Metadata(name, value))
+    def add_metadata(self, name, value, comment=None):
+        self.metadata.append(Metadata(name, value, comment))
         return self.metadata[-1]

-    def add_library(self, name, args=None):
-        self.imports.append(Library(name, args))
+    def add_library(self, name, args=None, comment=None):
+        self.imports.append(Library(name, args, comment=comment))
         return self.imports[-1]

-    def add_resource(self, name, invalid_args=None):
-        self.imports.append(Resource(name, invalid_args))
+    def add_resource(self, name, invalid_args=None, comment=None):
+        self.imports.append(Resource(name, invalid_args, comment=comment))
         return self.imports[-1]

-    def add_variables(self, name, args=None):
-        self.imports.append(Variables(name, args))
+    def add_variables(self, name, args=None, comment=None):
+        self.imports.append(Variables(name, args, comment=comment))
         return self.imports[-1]

     def __iter__(self):
@@ -114,7 +114,7 @@
     def __init__(self):
         self.variables = []

-    def add(self, name, value):
+    def add(self, name, value, comment=None):
         self.variables.append(Variable(name, value))

     def __iter__(self):
=======================================
--- /trunk/src/robot/parsing/populator.py       Wed May 19 01:26:55 2010
+++ /trunk/src/robot/parsing/populator.py       Wed May 19 03:09:49 2010
@@ -36,17 +36,17 @@

     def __init__(self, datafile):
         self._table = self._get_table(datafile)
-        self._populator = None
+        self._populator = NullPopulator()
         self._comments = []

     def add(self, row):
-        if row.is_comment():
+        if row.is_commented():
             self._comments.append(row)
             return
         if not self._is_continuing(row):
-            self.populate()
+            self._populator.populate()
             self._populator = self._get_populator(row)
-            self._add_cached_comments_to(self._populator)
+        self._add_cached_comments_to(self._populator)
         self._populator.add(row)

     def _add_cached_comments_to(self, populator):
@@ -55,8 +55,8 @@
         self._comments = []

     def populate(self):
-        if self._populator:
-            self._populator.populate()
+        self._add_cached_comments_to(self._populator)
+        self._populator.populate()

     def _is_continuing(self, row):
         return row.is_continuing()
@@ -245,44 +245,48 @@


 class _PropertyPopulator(Populator):
+    comments = property(lambda self: ''.join(self._comments))

     def __init__(self, setter):
         self._setter = setter
         self._value = []
         self._comments = []

+    def add(self, row):
+        if not row.is_commented():
+            self._add(row)
+        self._comments.extend(row.comments)
+

 class NameAndValuePropertyPopulator(_PropertyPopulator):

-    def add(self, row):
+    def _add(self, row):
         self._value.extend(row.all)

     def populate(self):
         name, value = self._value[0], self._value[1:]
-        self._setter(name, value)
+        self._setter(name, value, self.comments)


 class SettingPopulator(_PropertyPopulator):

-    def add(self, row):
-        if not row.is_comment():
-            self._value.extend(row.tail)
-        self._comments.extend(row.comments)
+    def _add(self, row):
+        self._value.extend(row.tail)

     def populate(self):
-        self._setter(self._value, self._comments)
+        self._setter(self._value, self.comments)


 class SettingTableNameValuePopulator(NameAndValuePropertyPopulator):

-    def add(self, row):
+    def _add(self, row):
         self._value.extend(row.tail)


 class OldStyleMetadataPopulator(NameAndValuePropertyPopulator):
     olde_metadata_prefix = 'meta:'

-    def add(self, row):
+    def _add(self, row):
         if self._is_metadata_with_olde_prefix(row.head):
values = self._extract_name_from_olde_style_meta_cell(row.head) + row.tail
         else:
@@ -390,7 +394,7 @@
     def is_continuing(self):
         return self.head == self._row_continuation_marker

-    def is_comment(self):
+    def is_commented(self):
         return not self.cells and self.comments

     def _parse(self, row):
=======================================
--- /trunk/src/robot/parsing/settings.py        Wed May 19 00:55:08 2010
+++ /trunk/src/robot/parsing/settings.py        Wed May 19 03:09:49 2010
@@ -19,9 +19,9 @@
         self.value = []
         self.comment = ''

-    def set(self, value, comment=''):
+    def set(self, value, comment=None):
         self._set(value)
-        self.comment = self._string_value(comment)
+        self.comment = comment

     def _set(self, value):
         self.value = value
@@ -75,25 +75,27 @@

 class Metadata(Setting):

-    def __init__(self, name, value):
+    def __init__(self, name, value, comment):
         self.name = name
         self.value = self._string_value(value)
+        self.comment = comment


 class Import(Setting):

-    def __init__(self, name, args=None, alias=None):
+    def __init__(self, name, args=None, alias=None, comment=None):
         self.name = name
         self.args = args or []
         self.alias = alias
+        self.comment = comment


 class Library(Import):

-    def __init__(self, name, args=None, alias=None):
+    def __init__(self, name, args=None, alias=None, comment=None):
         if args and not alias:
             args, alias = self._split_alias(args)
-        Import.__init__(self, name, args, alias)
+        Import.__init__(self, name, args, alias, comment)

     def _split_alias(self, args):
         if len(args) >= 2 and args[-2].upper() == 'WITH NAME':
@@ -103,13 +105,13 @@

 class Resource(Import):

-    def __init__(self, name, invalid_args=None):
+    def __init__(self, name, invalid_args=None, comment=None):
         if invalid_args:
             name += ' ' + ' '.join(invalid_args)
-        Import.__init__(self, name)
+        Import.__init__(self, name, comment=comment)


 class Variables(Import):

-    def __init__(self, name, args=None):
-        Import.__init__(self, name, args)
+    def __init__(self, name, args=None, comment=None):
+        Import.__init__(self, name, args, comment=comment)
=======================================
--- /trunk/utest/parsing/test_populator.py      Wed May 19 01:26:46 2010
+++ /trunk/utest/parsing/test_populator.py      Wed May 19 03:09:49 2010
@@ -33,10 +33,34 @@
         if eof:
             self._populator.eof()

-    def _assert_setting(self, setting_name, exp_value, exp_comment=None):
-        assert_equals(self._setting_with(setting_name).value, exp_value)
+    def _assert_setting(self, name, exp_value, exp_comment=None):
+        setting = self._setting_with(name)
+        assert_equals(setting.value, exp_value)
+        self._assert_comment(setting, exp_comment)
+
+ def _assert_fixture(self, fixture_name, exp_name, exp_args, exp_comment=None):
+        fixture = self._setting_with(fixture_name)
+        self._assert_name_and_args(fixture, exp_name, exp_args)
+        self._assert_comment(fixture, exp_comment)
+
+    def _assert_import(self, index, exp_name, exp_args, exp_comment=None):
+        imp = self._datafile.setting_table.imports[index]
+        self._assert_name_and_args(imp, exp_name, exp_args)
+        self._assert_comment(imp, exp_comment)
+
+    def _assert_name_and_args(self, item, exp_name, exp_args):
+        assert_equals(item.name, exp_name)
+        assert_equals(item.args, exp_args)
+
+    def _assert_meta(self, index, exp_name, exp_value, exp_comment=None):
+        meta = self._setting_with('metadata')[index]
+        assert_equals(meta.name, exp_name)
+        assert_equals(meta.value, exp_value)
+        self._assert_comment(meta, exp_comment)
+
+    def _assert_comment(self, item, exp_comment):
         if exp_comment:
- assert_equals(self._setting_with(setting_name).comment, exp_comment)
+            assert_equals(item.comment, exp_comment)

     def _setting_with(self, name):
         return getattr(self._datafile.setting_table, name)
@@ -246,23 +270,6 @@
         assert_equals(len(self._datafile.testcase_table.tests), 1)
         assert_equals(len(self._nth_uk(0).steps), 1)

-    def _assert_meta(self, index, exp_name, exp_value):
-        meta = self._setting_with('metadata')[index]
-        assert_equals(meta.name, exp_name)
-        assert_equals(meta.value, exp_value)
-
-    def _assert_fixture(self, fixture_name, exp_name, exp_args):
-        fixture = self._setting_with(fixture_name)
-        self._assert_name_and_args(fixture, exp_name, exp_args)
-
-    def _assert_import(self, index, exp_name, exp_args):
-        imp = self._datafile.setting_table.imports[index]
-        self._assert_name_and_args(imp, exp_name, exp_args)
-
-    def _assert_name_and_args(self, item, exp_name, exp_args):
-        assert_equals(item.name, exp_name)
-        assert_equals(item.args, exp_args)
-
     def _start_table(self, name):
         return self._populator.start_table(name)

@@ -294,10 +301,20 @@

     def test_end_of_line_setting_comment(self):
self._create_table('settings', [['Force Tags', 'Foo', 'Bar', '#comment'],
+                                        ['Library', 'Foo', '#Lib comment'],
                                         ['#comment between rows'],
-                                        ['Default Tags', 'Quux']])
+                                        ['Default Tags', 'Quux'],
+                                        ['Variables', 'varz.py'],
+                                        ['# between values'],
+                                        ['...', 'arg'],
+                                        ['Meta: metaname', 'metavalue'],
+                                        ['#last line is commented'],
+                                        ])
         self._assert_setting('force_tags', ['Foo', 'Bar'], 'comment')
+        self._assert_import(0, 'Foo', [], 'Lib comment')
self._assert_setting('default_tags', ['Quux'], 'comment between rows')
+        self._assert_import(1, 'varz.py', ['arg'], ' between values')
+ self._assert_meta(0, 'metaname', 'metavalue', 'last line is commented')


 if __name__ == '__main__':

Reply via email to