Revision: 3319
Author: pekka.klarck
Date: Tue May 18 07:14:11 2010
Log: Implemented parsing directories
http://code.google.com/p/robotframework/source/detail?r=3319

Modified:
 /trunk/src/robot/parsing/datareader.py
 /trunk/src/robot/parsing/newmodel.py

=======================================
--- /trunk/src/robot/parsing/datareader.py      Tue May 18 05:28:18 2010
+++ /trunk/src/robot/parsing/datareader.py      Tue May 18 07:14:11 2010
@@ -14,8 +14,9 @@

 import os

-from robot.errors import DataError
 from robot import utils
+from robot.output import LOGGER
+from robot.errors import DataError

 from populator import TestDataPopulator
 from htmlreader import HtmlReader
@@ -34,22 +35,87 @@
            'txt': TxtReader}


-def Reader(path):
-    extension = path.split('.')[-1].lower()
-    try:
-        return READERS[extension]()
-    except KeyError:
-        raise DataError("No reader found for extension '%s'" % extension)
-
-
-def read_data(path, datafile):
-    if not os.path.isfile(path):
-        raise DataError("Data source '%s' does not exist." % path)
-    try:
-        source = open(path, 'rb')
-    except:
-        raise DataError(utils.get_error_message())
-    try:
-        Reader(path).read(source, TestDataPopulator(datafile))
-    finally:
-        source.close()
+class FileReader(object):
+
+    def read(self, path, datafile):
+        LOGGER.info("Parsing test case file '%s'" % path)
+        source = self._open(path)
+        try:
+ self._get_reader(path).read(source, TestDataPopulator(datafile))
+        finally:
+            source.close()
+
+    def _open(self, path):
+        if not os.path.isfile(path):
+            raise DataError("Data source '%s' does not exist." % path)
+        try:
+            return open(path, 'rb')
+        except:
+            raise DataError(utils.get_error_message())
+
+    def _get_reader(self, path):
+        extension = path.lower().split('.')[-1]
+        try:
+            return READERS[extension]()
+        except KeyError:
+ raise DataError("No reader found for extension '%s'" % extension)
+
+
+class DirectoryReader(object):
+    ignored_prefixes = ('_', '.')
+    ignored_dirs = ('CVS',)
+
+    def read(self, path, datadir):
+        LOGGER.info("Parsing test data directory '%s'" % path)
+        init, children = self._get_children(path)
+        if init:
+            try:
+                FileReader().read(init, datadir)
+            except DataError, err:
+                LOGGER.error(unicode(err))
+        for child in children:
+            try:
+                datadir.add_child(child)
+            except DataError, err:
+                LOGGER.info("Parsing data source '%s' failed: %s"
+                            % (path, unicode(err)))
+
+    def _get_children(self, dirpath):
+        init = None
+        children = []
+        for name, path in self._list_dir(dirpath):
+            if self._is_init_file(name, path):
+                if not init:
+                    init = path
+                else:
+ LOGGER.error("Ignoring second test suite init file '%s'" % path)
+            elif self._is_ignored(name, path):
+                LOGGER.info("Ignoring file or directory '%s'" % name)
+            else:
+                children.append(path)
+        return init, children
+
+    def _list_dir(self, path):
+        # os.listdir returns Unicode entries when path is Unicode
+        names = os.listdir(utils.unic(path))
+        # http://bugs.jython.org/issue1593
+        if utils.is_jython:
+            from java.lang import String
+            names = [utils.unic(String(n)) for n in names]
+        for name in sorted(names, key=unicode.lower):
+            yield name, os.path.join(path, name)
+
+    def _is_init_file(self, name, path):
+        if not os.path.isfile(path):
+            return False
+        base, extension = name.lower().rsplit('.', 1)
+        return base == '__init__' and extension in READERS
+
+    def _is_ignored(self, name, path):
+        if name.startswith(self.ignored_prefixes):
+            return True
+        if os.path.isdir(path):
+            return name in self.ignored_dirs
+        extension = path.lower().split('.')[-1]
+        return extension not in READERS
+
=======================================
--- /trunk/src/robot/parsing/newmodel.py        Tue May 18 05:28:18 2010
+++ /trunk/src/robot/parsing/newmodel.py        Tue May 18 07:14:11 2010
@@ -12,12 +12,20 @@
 #  See the License for the specific language governing permissions and
 #  limitations under the License.

+import os
+
 from robot.errors import DataError
 from robot.variables import is_var

 from settings import (Documentation, Fixture, Timeout, Tags, Metadata,
                       Library, Resource, Variables, Arguments, Return)
-from datareader import read_data
+from datareader import FileReader, DirectoryReader
+
+
+def TestData(path):
+    if os.path.isdir(path):
+        return TestDataDirectory(path)
+    return TestCaseFile(path)


 class TestCaseFile(object):
@@ -29,7 +37,7 @@
         self.testcase_table = TestCaseTable()
         self.keyword_table = KeywordTable()
         if source:
-            read_data(source, self)
+            FileReader().read(source, self)

     def __iter__(self):
         for table in [self.setting_table, self.variable_table,
@@ -45,8 +53,17 @@
         self.variable_table = VariableTable()
self.testcase_table = TestCaseTableNotAllowed('test suite init file')
         self.keyword_table = KeywordTable()
+        self.children = []
         if source:
-            self._populate(source)
+            DirectoryReader().read(source, self)
+
+    def add_child(self, path):
+        self.children.append(TestData(path))
+
+    def __iter__(self):
+        for table in [self.setting_table, self.variable_table,
+                      self.keyword_table]:
+            yield table


 class DataTable(object):

Reply via email to