Revision: eaa8f8c75bb6
Branch:   default
Author:   Robot Framework Developers (robotframew...@gmail.com)
Date:     Mon Sep 30 11:34:39 2013 UTC
Log:      RestReader: Simplify handling code dependent on docutils.

Update issue 1495
It's easier to have docutils dependent code in a separate module. No ifs
needed there anymore. Also directives are registered only once, because
this module is never imported more than once (and would be cached if it was).
http://code.google.com/p/robotframework/source/detail?r=eaa8f8c75bb6

Added:
 /src/robot/parsing/restsupport.py
Modified:
 /src/robot/parsing/restreader.py

=======================================
--- /dev/null
+++ /src/robot/parsing/restsupport.py   Mon Sep 30 11:34:39 2013 UTC
@@ -0,0 +1,61 @@
+#  Copyright 2008-2013 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.
+
+from robot.errors import DataError
+
+try:
+    from docutils.core import publish_doctree, publish_from_doctree
+    from docutils.parsers.rst.directives import register_directive
+    from docutils.parsers.rst.directives.body import CodeBlock
+except ImportError:
+    raise DataError("Using reStructuredText test data requires having "
+                    "'docutils' module installed.")
+
+
+class IgnoreCode(CodeBlock):
+
+    def run(self):
+        return []
+
+
+class CaptureRobotData(CodeBlock):
+
+    def run(self):
+        if 'robotframework' in self.arguments:
+            store = RobotDataStorage(self.state_machine.document)
+            store.add_data(self.content)
+        return []
+
+
+# 'sourcode' directive is our old custom directive used in User Guide and
+# Quick Start Guide. Should be replaced with the standard 'code' directive.
+register_directive('sourcecode', IgnoreCode)
+register_directive('code', CaptureRobotData)
+
+
+class RobotDataStorage(object):
+
+    def __init__(self, doctree):
+        if not hasattr(doctree, '_robot_data'):
+            doctree._robot_data = []
+        self._robot_data = doctree._robot_data
+
+    def add_data(self, rows):
+        self._robot_data.extend(rows)
+
+    def get_data(self):
+        return '\n'.join(self._robot_data)
+
+    def has_data(self):
+        return bool(self._robot_data)
=======================================
--- /src/robot/parsing/restreader.py    Mon Sep 30 11:11:09 2013 UTC
+++ /src/robot/parsing/restreader.py    Mon Sep 30 11:34:39 2013 UTC
@@ -14,22 +14,13 @@

 from cStringIO import StringIO

-from robot.errors import DataError
-
 from .htmlreader import HtmlReader
 from .txtreader import TxtReader


 def RestReader():
-    try:
-        from docutils.core import publish_doctree, publish_from_doctree
-    except ImportError:
-        raise DataError("Using reStructuredText test data requires having "
-                        "'docutils' module installed.")
-
-    if not register_custom_directives.registered:
-        register_custom_directives()
-        register_custom_directives.registered = True
+    from .restsupport import (publish_doctree, publish_from_doctree,
+                              RobotDataStorage)

     class RestReader(object):

@@ -55,46 +46,3 @@
             return HtmlReader().read(htmlfile, rawdata)

     return RestReader()
-
-
-def register_custom_directives():
-    from docutils.parsers.rst.directives import register_directive
-    from docutils.parsers.rst.directives.body import CodeBlock
-
-    class IgnoreCode(CodeBlock):
-
-        def run(self):
-            return []
-
-    class CaptureRobotData(CodeBlock):
-
-        def run(self):
-            if 'robotframework' in self.arguments:
-                store = RobotDataStorage(self.state_machine.document)
-                store.add_data(self.content)
-            return []
-
- # 'sourcode' directive is our old custom directive used in User Guide and - # Quick Start Guide. Should be replaced with the standard 'code' directive.
-    register_directive('sourcecode', IgnoreCode)
-    register_directive('code', CaptureRobotData)
-
-
-register_custom_directives.registered = False
-
-
-class RobotDataStorage(object):
-
-    def __init__(self, document):
-        if not hasattr(document, '_robot_data'):
-            document._robot_data = []
-        self._robot_data = document._robot_data
-
-    def add_data(self, rows):
-        self._robot_data.extend(rows)
-
-    def get_data(self):
-        return '\n'.join(self._robot_data)
-
-    def has_data(self):
-        return bool(self._robot_data)

--

--- You received this message because you are subscribed to the Google Groups "robotframework-commit" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to robotframework-commit+unsubscr...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to