summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authornoah <noah@656d521f-e311-0410-88e0-e7920216d269>2002-08-31 06:07:37 +0000
committernoah <noah@656d521f-e311-0410-88e0-e7920216d269>2002-08-31 06:07:37 +0000
commit48dba9cb118bcc099b57241033b9d70a84299298 (patch)
tree21f053bc6507df235cc3984bcb6479d2afe50332
parentadddd34434586d50f02d8672edd51f489df00ac1 (diff)
downloadpexpect-48dba9cb118bcc099b57241033b9d70a84299298.tar.gz
Added expect_exact() method to pexpect and added a test for it()
git-svn-id: http://pexpect.svn.sourceforge.net/svnroot/pexpect/trunk@39 656d521f-e311-0410-88e0-e7920216d269
-rw-r--r--pexpect/pexpect.py47
-rwxr-xr-xpexpect/tests/test_expect.py38
2 files changed, 85 insertions, 0 deletions
diff --git a/pexpect/pexpect.py b/pexpect/pexpect.py
index 04baed6..85a7a14 100644
--- a/pexpect/pexpect.py
+++ b/pexpect/pexpect.py
@@ -186,6 +186,53 @@ class spawn:
return self.expect_list(compiled_pattern_list, local_timeout)
+ def expect_exact (self, str_list, local_timeout = None):
+ '''This is similar to expect() except that it takes
+ list of regular strings instead of compiled regular expressions.
+ The idea is that this should be much faster. It could also be
+ useful when you don't want to have to worry about escaping
+ regular expression characters that you want to match.
+ You may also pass just a string without a list and the single
+ string will be converted to a list.
+ '''
+ matched_pattern = None
+ before_pattern = None
+ index = None
+
+ if type(str_list)is StringType:
+ str_list = [str_list]
+
+ try:
+ done = 0
+ incoming = ''
+ while not done: # Keep reading until done.
+ c = self.read(1, local_timeout)
+ incoming = incoming + c
+
+ # Sequence through the list of patterns and look for a match.
+ index = 0
+ for str_target in str_list:
+ match_index = incoming.find (str_target)
+ if match_index >= 0:
+ matched_pattern = incoming[match_index:]
+ before_pattern = incoming[:match_index]
+ done = 1
+ break
+ else:
+ index = index + 1
+ except Exception, e:
+ ### Here I should test if the client wants to pass exceptions, or
+ ### to return some state flag. Exception versus return value.
+ matched_pattern = None
+ before_pattern = incoming
+ index = -1
+ raise
+
+ self.before = before_pattern
+ self.matched = matched_pattern
+ return index
+
+
def expect_list(self, re_list, local_timeout = None):
'''This is called by expect(). This takes a list of compiled
regular expressions. This returns the matched index into the
diff --git a/pexpect/tests/test_expect.py b/pexpect/tests/test_expect.py
new file mode 100755
index 0000000..af07085
--- /dev/null
+++ b/pexpect/tests/test_expect.py
@@ -0,0 +1,38 @@
+#!/usr/bin/env python
+import pexpect
+import unittest
+import commands
+
+class ExpectTestCase(unittest.TestCase):
+ #def runTest (self):
+ def test_expectExact (self):
+ the_old_way = commands.getoutput('/bin/ls -l')
+
+ p = pexpect.spawn('/bin/ls -l')
+ try:
+ the_new_way = ''
+ while 1:
+ p.expect_exact ('\n')
+ the_new_way = the_new_way + p.before# + '\n'
+ except:
+ the_new_way = the_new_way[:-1]
+ the_new_way = the_new_way.replace('\r','\n')
+
+ print type(the_old_way)
+ print the_old_way
+ print the_new_way
+
+ fout = open('kill1','wb')
+ fout.write(the_old_way)
+ fout.close
+ fout = open('kill2', 'wb')
+ fout.write(the_new_way)
+ fout.close
+ assert the_old_way == the_new_way
+
+
+if __name__ == '__main__':
+ unittest.main()
+
+suite = unittest.makeSuite(ExpectTestCase,'test')
+