Author: laukpe
Date: Tue Feb 24 07:24:05 2009
New Revision: 1348

Modified:
   trunk/src/robot/libraries/OperatingSystem.py
   trunk/src/robot/utils/connectioncache.py
   trunk/utest/utils/test_connectioncache.py

Log:
Implement special NoConnection to ConnectionCache which can be used as current connection when no connections are open.

Modified: trunk/src/robot/libraries/OperatingSystem.py
==============================================================================
--- trunk/src/robot/libraries/OperatingSystem.py        (original)
+++ trunk/src/robot/libraries/OperatingSystem.py        Tue Feb 24 07:24:05 2009
@@ -290,7 +290,7 @@
             self._log(msg, 'WARN')
             if SYSLOG:
                 SYSLOG.warn(msg)
-        return PROCESSES.get_current().read()
+        return PROCESSES.current.read()

     def stop_process(self):
         """Stops the current process without reading from it.
@@ -301,7 +301,7 @@

         See `Start Process` and `Switch Process` for more information.
         """
-        PROCESSES.get_current().close()
+        PROCESSES.current.close()

     def stop_all_processes(self):
         """Stops all the processes and removes them from the process list.

Modified: trunk/src/robot/utils/connectioncache.py
==============================================================================
--- trunk/src/robot/utils/connectioncache.py    (original)
+++ trunk/src/robot/utils/connectioncache.py    Tue Feb 24 07:24:05 2009
@@ -30,17 +30,12 @@
     """

     def __init__(self, no_current_msg='No open connection'):
-        self.current = None
+        self.current = self._no_current = _NoConnection(no_current_msg)
         self.current_index = None
         self._connections = []
         self._aliases = NormalizedDict()
         self._no_current_msg = no_current_msg
-
-    def get_current(self):
-        if self.current is None:
-            raise DataError(self._no_current_msg)
-        return self.current
-
+
     def register(self, connection, alias=None):
"""Registers given connection with optional alias and returns its index.

@@ -86,7 +81,7 @@
         """Empties the connections cache.

         Indexes of new connections starts from 1 after this."""
-        self.current = None
+        self.current = self._no_current
         self.current_index = None
         self._connections = []
         self._aliases = NormalizedDict()
@@ -110,3 +105,17 @@
         if not 0 < index <= len(self._connections):
             raise ValueError
         return index
+
+
+class _NoConnection:
+
+    def __init__(self, msg):
+        self._msg = msg
+
+    def __getattr__(self, name):
+        if name.startswith('__') and name.endswith('__'):
+            raise AttributeError
+        raise DataError(self._msg)
+
+    def __nonzero__(self):
+        return False

Modified: trunk/utest/utils/test_connectioncache.py
==============================================================================
--- trunk/utest/utils/test_connectioncache.py   (original)
+++ trunk/utest/utils/test_connectioncache.py   Tue Feb 24 07:24:05 2009
@@ -25,6 +25,12 @@

     def test_initial(self):
         self._verify_initial_state()
+
+    def test_no_connection(self):
+        assert_raises_with_msg(DataError, 'No open connection', getattr,
+                               ConnectionCache().current, 'whatever')
+        assert_raises_with_msg(DataError, 'Custom msg', getattr,
+ ConnectionCache('Custom msg').current, 'xxx')

     def test_register_one(self):
         conn = ConnectionMock()
@@ -123,7 +129,7 @@
             assert_false(conn.closed_by_exit)

     def _verify_initial_state(self):
-        assert_none(self.cache.current)
+        assert_equals(self.cache.current, self.cache._no_current)
         assert_none(self.cache.current_index)
         assert_equals(self.cache._connections, [])
         assert_equals(self.cache._aliases, {})

Reply via email to