1

我们使用了一些我们没有源代码的编译 python 代码。代码提示用户输入,我们正在尝试自动化该部分。

基本上会询问用户名、密码,然后根据特定情况询问一些不同的问题。我不知道编译后的函数是否使用 raw_input、input 或其他东西。

我已经能够使用 StringIO 用用户名和密码替换 stdin,我可以用我自己的类替换 stdout 并找出出现的提示,但是在有选择地将数据放入基于 stdin 时我很难过关于我从标准输出中读到的内容。

import sys
import re
from StringIO import StringIO

def test():
    overwrite = raw_input("The file exists, overwrite? ")
    notify = raw_input("This file is marked for notifies.  Notify?")
    sys.stdout.write("Overwrite: %s, Notify: %s" % (overwrite,notify))

class Catcher(object):
    def __init__(self):
        pass

    def write(self, msg):
        if re.search("The file exists, overwrite", msg):
            # put data into stdin 
        if re.search("The file is marked for notification", msg):
            # put data into stdin

sys.stdout = Catcher()
test()

我不能只预加载一个 StringIO 对象,因为问题可能会因情况而异,但我需要自动输入标准输入,因为他们试图将其放入自动构建系统中,因此他们将提供默认值通过命令行回答出现的任何问题。

如果我在调用编译函数之前将 stdin 设置为一个空的 StringIO 对象,那么它只会出现 EOF 错误 - 不知道如何让它等待输入。

像这样的东西:

import sys
import re
from StringIO import StringIO

def test():
    overwrite = raw_input("The file exists, overwrite? ")
    notify = raw_input("This file is marked for notifies.  Notify?")
    sys.__stdout__.write("Overwrite: %s, Notify: %s" % (overwrite,notify))

class Catcher(object):
    def __init__(self, stdin):
        self.stdin = stdin

    def write(self, msg):
        if re.search("The file exists, overwrite", msg):
            self.stdin.write('yes\n')
        if re.search("The file is marked for notification", msg):
            self.stdin.write('no\n')

sys.stdin = StringIO()
sys.stdout = Catcher(sys.stdin)
test()

产生:

Traceback (most recent call last):
  File "./teststdin.py", line 25, in <module>
    test()
  File "./teststdin.py", line 8, in test
    overwrite = raw_input("The file exists, overwrite? ")
EOFError: EOF when reading a line

有任何想法吗?

4

2 回答 2

3

如果您想从StringIO刚写过的内容中读取内容,则必须先将其倒回开始写入的位置。
此外,您的第二次搜索测试错误的字符串。

这应该有效:

import sys
import re
from StringIO import StringIO

def test():
    overwrite = raw_input("The file exists, overwrite? ")
    notify = raw_input("This file is marked for notifies.  Notify?")
    sys.__stdout__.write("Overwrite: %s, Notify: %s" % (overwrite,notify))

class Catcher(object):
    def __init__(self, stdin):
        self.stdin = stdin

    def write(self, msg):
        if re.search("The file exists, overwrite?", msg):
            self.stdin.truncate(0)
            self.stdin.write('yes\n')
            self.stdin.seek(0)
        if re.search("This file is marked for notifies.  Notify?", msg):
            self.stdin.truncate(0)
            self.stdin.write('no\n')
            self.stdin.seek(0)

sys.stdin = StringIO()
sys.stdout = Catcher(sys.stdin)
test()
于 2012-11-20T19:54:54.540 回答
1

这是一个完全避免的解决方案StringIO

import sys
import re

class Catcher(object):
    def __init__(self, handler):
        self.handler = handler
        self.inputs = []

    def __enter__(self):
        self.__stdin  = sys.stdin
        self.__stdout = sys.stdout
        sys.stdin = self
        sys.stdout = self

    def __exit__(self, type, value, traceback):
        sys.stdin  = self.__stdin
        sys.stdout = self.__stdout

    def write(self, value):
        result = self.handler(value)
        if result:
            self.inputs = [result] + self.inputs

    def readline(self):
        return self.inputs.pop()

用作:

def test():
    overwrite = raw_input("The file exists, overwrite? ")
    notify = raw_input("This file is marked for notifies.  Notify?")
    sys.__stdout__.write("Overwrite: %s, Notify: %s" % (overwrite,notify))


@Catcher
def exist_notify(msg):
    if re.search("The file exists, overwrite", msg):
        return 'yes'
    if re.search("This file is marked for notifies", msg):
        return 'no'

with exist_notify:
    test()
于 2012-11-20T19:40:46.033 回答