2 new revisions:

Revision: 288c079e899e
Author:   Janne Härkönen <[email protected]>
Date:     Fri Nov 18 04:06:37 2011
Log:      etreewrapper: cleanup of import logic
http://code.google.com/p/robotframework/source/detail?r=288c079e899e

Revision: 808cc1a5f083
Author:   Janne Härkönen <[email protected]>
Date:     Fri Nov 18 04:07:46 2011
Log: resultbuilder: made XmlSource a context manager and moved to utils...
http://code.google.com/p/robotframework/source/detail?r=808cc1a5f083

==============================================================================
Revision: 288c079e899e
Author:   Janne Härkönen <[email protected]>
Date:     Fri Nov 18 04:06:37 2011
Log:      etreewrapper: cleanup of import logic
http://code.google.com/p/robotframework/source/detail?r=288c079e899e

Modified:
 /src/robot/utils/etreewrapper.py

=======================================
--- /src/robot/utils/etreewrapper.py    Wed Oct  5 04:37:08 2011
+++ /src/robot/utils/etreewrapper.py    Fri Nov 18 04:06:37 2011
@@ -14,26 +14,24 @@

 import sys
 from StringIO import StringIO
+
 try:
-    import xml.etree.cElementTree as ET
+    from xml.etree import cElementTree as ET
 except ImportError:
     try:
         import cElementTree as ET
     except ImportError:
         try:
-            import xml.etree.ElementTree as ET
- # Raises ImportError due to missing expat on IronPython by default:
-            # http://ironpython.codeplex.com/workitem/21407
+            from xml.etree import ElementTree as ET
+            # Raises ImportError due to missing expat on IronPython < 2.7.1
+            # by default http://ironpython.codeplex.com/workitem/21407
             ET.parse(StringIO('<test/>'))
         except ImportError:
             try:
-                import elementtree.ElementTree as ET
-            # Can cause AttributeError on IronPython:
-            # http://ironpython.codeplex.com/workitem/31545
-            except (ImportError, AttributeError):
+                from elementtree import ElementTree as ET
+            except ImportError:
raise ImportError('No valid ElementTree XML parser module found')

-
 def get_root(path=None, string=None, node=None):
# This should NOT be changed to 'if not node:'. See chapter Truth Testing
     # from http://effbot.org/zone/element.htm#the-element-type

==============================================================================
Revision: 808cc1a5f083
Author:   Janne Härkönen <[email protected]>
Date:     Fri Nov 18 04:07:46 2011
Log:      resultbuilder: made XmlSource a context manager and moved to utils

Also changed error messages in cases of invalid and non-existing
XML source a bit.
http://code.google.com/p/robotframework/source/detail?r=808cc1a5f083

Added:
 /src/robot/utils/xmlsource.py
Modified:
 /atest/robot/cli/rebot/invalid_usage.txt
 /src/robot/result/builders.py
 /src/robot/utils/__init__.py

=======================================
--- /dev/null
+++ /src/robot/utils/xmlsource.py       Fri Nov 18 04:07:46 2011
@@ -0,0 +1,41 @@
+#  Copyright 2008-2011 Nokia Siemens Networks Oyj
+#
+#  Licensed under the Apache License, Version 2.0 (the "License");
+#  you may not use this file except in compliance with the License.
+#  You may obtain a copy of the License at
+#
+#      http://www.apache.org/licenses/LICENSE-2.0
+#
+#  Unless required by applicable law or agreed to in writing, software
+#  distributed under the License is distributed on an "AS IS" BASIS,
+#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+#  See the License for the specific language governing permissions and
+#  limitations under the License.
+
+import os
+from StringIO import StringIO
+
+from robot.errors import DataError
+
+
+class XmlSource(object):
+
+    def __init__(self, source):
+        self._source = source
+        if isinstance(source, basestring) and not os.path.isfile(source):
+            if source.startswith('<'):
+                self._source = StringIO(source)
+            else:
+ raise DataError("Output file '%s' does not exist." % source)
+
+    def __enter__(self):
+        return self._source
+
+    def __exit__(self, exc_type, exc_value, exc_trace):
+        if exc_type is None or exc_type is DataError:
+            return False
+        raise DataError(exc_value)
+
+    def __str__(self):
+        return str(self._source)
+
=======================================
--- /atest/robot/cli/rebot/invalid_usage.txt    Tue Nov  8 04:57:08 2011
+++ /atest/robot/cli/rebot/invalid_usage.txt    Fri Nov 18 04:07:46 2011
@@ -17,11 +17,11 @@

 Non-XML Input
     Create File  ${MYOUTDIR}/invalid.txt  Hello, world
- Rebot should fail ${MYOUTDIR}${/}invalid.txt (\\[Fatal Error\\] .*: Content is not allowed in prolog.\\n)?Opening XML file '.*invalid.txt' failed: .*
-
-Non-Compatible XML
+ Rebot should fail ${MYOUTDIR}${/}invalid.txt (\\[Fatal Error\\] .*: Content is not allowed in prolog.\\n)?Reading XML source '.*invalid.txt' failed: .*
+
+Incompatible XML
     Create File  ${MYOUTDIR}/invalid.xml  <not><our>type</our></not>
- Rebot should fail ${MYOUTDIR}${/}invalid.xml File '.*invalid.xml' is not Robot Framework output file.* + Rebot should fail ${MYOUTDIR}${/}invalid.xml Reading XML source '.*invalid.xml' failed: Incompatible XML element 'not'

 Invalid Output Directory
[Documentation] Test error handling when some of the output dirs (where to write output, report, etc.) is invalid (i.e. can't be created). To make sure creating dirs fails their names are non-standard (that's not enough in Linux) parent is made read only.
@@ -50,6 +50,7 @@
 Rebot Should Fail  [Arguments]  ${options}  ${exp msg}
     Set Runners
     ${rc}  ${output} =  Run And Return RC and Output  ${REBOT} ${options}
+    Log  ${output}
     Should Be Equal As Integers  ${rc}  252
Should Match Regexp ${output} ^\\[ .*ERROR.* \\] ${exp msg}${USAGETIP}$

=======================================
--- /src/robot/result/builders.py       Fri Nov 18 00:40:53 2011
+++ /src/robot/result/builders.py       Fri Nov 18 04:07:46 2011
@@ -12,12 +12,10 @@
 #  See the License for the specific language governing permissions and
 #  limitations under the License.

-import os
-from StringIO import StringIO
+from __future__ import with_statement

 from robot.errors import DataError
-from robot.utils.etreewrapper import ET
-from robot import utils
+from robot.utils import ET, XmlSource

 from executionresult import ExecutionResult, CombinedExecutionResult
 from suiteteardownfailed import SuiteTeardownFailureHandler
@@ -31,30 +29,9 @@
     source = XmlSource(sources[0])
     try:
         return ExecutionResultBuilder(source).build(ExecutionResult())
-    # TODO: handle source in errors messages when it's a file object
     except DataError, err:
- raise DataError("File '%s' is not Robot Framework output file: %s" % (source, err))
-    except:
-        raise DataError("Opening XML file '%s' failed: %s"
-                        % (source, utils.get_error_message()))
-
-
-class XmlSource(object):
-
-    def __init__(self, source):
-        self._source = source
-
-    @property
-    def source(self):
-        if not isinstance(self._source, basestring):
-            return self._source
-        if os.path.isfile(self._source):
-            return self._source
-        try:
-            ET.XML(self._source)
-            return StringIO(self._source)
-        except ET.ParseError:
- raise DataError("Output file '%s' does not exist." % self._source)
+        raise DataError("Reading XML source '%s' failed: %s"
+                        % (source, unicode(err)))


 class ExecutionResultBuilder(object):
@@ -64,8 +41,9 @@

     def build(self, result):
         elements = ElementStack(RootElement())
- for action, elem in ET.iterparse(self._source.source, events=('start', 'end')):
-            result = getattr(elements, action)(elem, result)
+        with self._source as source:
+ for action, elem in ET.iterparse(source, events=('start', 'end')):
+               result = getattr(elements, action)(elem, result)
SuiteTeardownFailureHandler(result.generator).visit_suite(result.suite)
         return result

@@ -104,7 +82,7 @@
         for child_type in self._children():
             if child_type.tag == tag:
                 return child_type()
-        raise DataError("Unexpected element '%s'" % tag)
+        raise DataError("Incompatible XML element '%s'" % tag)

     def _children(self):
         return []
=======================================
--- /src/robot/utils/__init__.py        Sat Nov  5 16:32:49 2011
+++ /src/robot/utils/__init__.py        Fri Nov 18 04:07:46 2011
@@ -20,6 +20,7 @@
 from error import (get_error_message, get_error_details, ErrorDetails,
                    RERAISED_EXCEPTIONS)
 from escaping import escape, unescape
+from etreewrapper import ET
 from htmlutils import html_format, html_escape, html_attr_escape
 from htmlwriter import HtmlWriter
 from importing import simple_import, import_
@@ -35,6 +36,7 @@
 from text import (cut_long_message, format_assign_message,
                   pad_console_length, get_console_length)
 from unic import unic, safe_repr
+from xmlsource import XmlSource
 from xmlwriter import XmlWriter

 import sys

Reply via email to