Revision: 152
Author:   janne.t.harkonen
Date:     Fri Aug 24 04:21:13 2012
Log:      move write implementation from Library to Client
http://code.google.com/p/robotframework-sshlibrary/source/detail?r=152

Modified:
 /trunk/atest/interactive_session.txt
 /trunk/src/SSHLibrary/__init__.py
 /trunk/src/SSHLibrary/core.py
 /trunk/src/SSHLibrary/javaclient.py
 /trunk/src/SSHLibrary/pythonclient.py

=======================================
--- /trunk/atest/interactive_session.txt        Wed Aug 22 21:55:51 2012
+++ /trunk/atest/interactive_session.txt        Fri Aug 24 04:21:13 2012
@@ -63,9 +63,7 @@
     [Teardown]    Switch Connection    1
     Set Prompt  ${None}
     Login as Valid User
-    Run Keyword And Expect Error
- ... Using 'Read Until Prompt', 'Write' or 'Write Bare' keyword requires setting prompt first. *
-    ...    Write    This should fail
+ Run Keyword And Expect Error Prompt is not set. Write This should fail

 *** Keywords ***
 Remove Counter And Read All Data
=======================================
--- /trunk/src/SSHLibrary/__init__.py   Fri Aug 24 04:20:49 2012
+++ /trunk/src/SSHLibrary/__init__.py   Fri Aug 24 04:21:13 2012
@@ -21,7 +21,7 @@
 from robot import utils

 from connectioncache import ConnectionCache
-from core import AuthenticationException, ClientConfig
+from core import AuthenticationException, ClientConfig, SSHClientException
 from config import (Configuration, StringEntry, NewlineEntry, TimeEntry,
         LogLevelEntry)
 from deprecated import DeprecatedSSHLibraryKeywords
@@ -352,7 +352,7 @@
         command. To get the output, one of the `Read XXX` keywords must be
         used.
         """
-        self.write_bare(text + self._config.newline)
+        self._write(text, add_newline=True)
         return self.read_until(self._config.newline, loglevel)

     def write_bare(self, text):
@@ -360,14 +360,14 @@

         Unlike `Write` does not consume the written text from the output.
         """
-        try:
-            text = str(text)
-        except UnicodeError:
-            raise ValueError('Only ASCII characters are allowed in SSH.'
-                             'Got: %s' % text)
-        self._ensure_prompt_is_set()
+        self._write(text)
+
+    def _write(self, text, add_newline=False):
         self._info("Writing %s" % repr(text))
-        self.ssh_client.write(text)
+        try:
+            self.ssh_client.write(text, add_newline)
+        except SSHClientException, e:
+            raise RuntimeError(e)

     def read(self, loglevel=None):
"""Reads and returns/logs everything currently available on the output.
@@ -443,16 +443,8 @@
         output of previous command has been read and the command does not
         produce prompt characters in its output.
         """
-        self._ensure_prompt_is_set()
         return self.read_until(self.ssh_client.config.prompt, loglevel)

-    def _ensure_prompt_is_set(self):
-        if not self.ssh_client.config.prompt:
-            raise RuntimeError("Using 'Read Until Prompt', 'Write' or "
-                "'Write Bare' keyword requires setting prompt first. "
-                "Prompt can be set either when taking library into use or "
-                "when using 'Open Connection' keyword.")
-
     def write_until_expected_output(self, text, expected, timeout,
                                     retry_interval, loglevel=None):
         """Writes given text repeatedly until `expected` appears in output.
=======================================
--- /trunk/src/SSHLibrary/core.py       Fri Aug 24 04:20:25 2012
+++ /trunk/src/SSHLibrary/core.py       Fri Aug 24 04:21:13 2012
@@ -35,6 +35,10 @@
     pass


+class SSHClientException(RuntimeError):
+    pass
+
+
 class Command(object):
     """Base class for remote commands."""

@@ -90,6 +94,26 @@
             return ret[0]
         return ret

+    def write(self, text, add_newline=False):
+        """Write `text` in shell session.
+
+        :param str text: the text to be written, must be ASCII.
+ :param bool add_newline: if True, a newline will be added to `text`.
+        """
+        self._ensure_prompt_is_set()
+        try:
+            text = str(text)
+        except UnicodeError:
+ raise SSHClientException('Invalid input, only ASCII characters '
+                                     'are allowed. Got: %s' % text)
+        if add_newline:
+            text += self.config.newline
+        self._write(text)
+
+    def _ensure_prompt_is_set(self):
+        if not self.config.prompt:
+            raise SSHClientException('Prompt is not set.')
+
     def put_file(self, source, dest, mode, newline_char):
         remotefile = self._create_remote_file(dest, mode)
         localfile = open(source, 'rb')
=======================================
--- /trunk/src/SSHLibrary/javaclient.py Fri Aug 24 04:20:36 2012
+++ /trunk/src/SSHLibrary/javaclient.py Fri Aug 24 04:21:13 2012
@@ -67,7 +67,7 @@
         self._writer = self.shell.getStdin()
         self._stdout = self.shell.getStdout()

-    def write(self, text):
+    def _write(self, text):
         self._writer.write(text)
         self._writer.flush()

=======================================
--- /trunk/src/SSHLibrary/pythonclient.py       Fri Aug 24 04:20:36 2012
+++ /trunk/src/SSHLibrary/pythonclient.py       Fri Aug 24 04:21:13 2012
@@ -70,7 +70,7 @@
         self.shell = self.client.invoke_shell(self.config.term_type,
                 self.config.width, self.config.height)

-    def write(self, text):
+    def _write(self, text):
         self.shell.sendall(text)

     def read(self):

Reply via email to