Chinaunix首页 | 论坛 | 博客
  • 博客访问: 19867003
  • 博文数量: 679
  • 博客积分: 10495
  • 博客等级: 上将
  • 技术积分: 9308
  • 用 户 组: 普通用户
  • 注册时间: 2006-07-18 10:51
文章分类

全部博文(679)

文章存档

2012年(5)

2011年(38)

2010年(86)

2009年(145)

2008年(170)

2007年(165)

2006年(89)

分类: Python/Ruby

2010-02-10 10:20:12

import sys
import os
from StringIO import StringIO

from robotthread import Semaphore, current_thread

if os.name == 'java':
    from java.io import OutputStream, ByteArrayOutputStream, PrintStream
    from java.lang import System


def capture_output():
    _OUTPUT_CAPTURE.capture_output()

def release_output():
    return _OUTPUT_CAPTURE.release_output()


class _Output:
    
    def __init__(self):
        self._outs = {}
        self._sema = Semaphore()

    def write(self, msg):
        self._sema.acquire()
        thrd = current_thread()
        key = thrd.getName() == 'TIMED_RUN' and 'TIMED' or thrd
        if not self._outs.has_key(key):
            self._outs[key] = self._get_stream()
        self._outs[key].write(msg)
        self._sema.release()

    def get_value(self):
        self._sema.acquire()
        thrd = current_thread()
        key = self._outs.has_key('TIMED') and 'TIMED' or thrd
        if self._outs.has_key(key):
            self._outs[key].flush()
            msg = self._get_msg(key)
            del(self._outs[key])
            if key is not thrd and self._outs.has_key(thrd):
                del(self._outs[thrd])
        else:
            msg = ''
        self._sema.release()
        return msg

   
class _PythonOutput(_Output):

    def _get_stream(self):
        return StringIO()
   
    def _get_msg(self, key):
        return self._outs[key].getvalue()
   
    def flush(self):
        self._sema.acquire()
        thrd = current_thread()
        key = self._outs.has_key('TIMED') and 'TIMED' or thrd
        if self._outs.has_key(key):
            self._outs[key].flush()
        self._sema.release()
   

if os.name == 'java':

    class _JavaOutput(_Output, OutputStream):

        def __init__(self):
            OutputStream.__init__(self)
            _Output.__init__(self)
           
        def _get_stream(self):
            return ByteArrayOutputStream()
       
        def _get_msg(self, key):
            return self._outs[key].toString()


class _OutputCapture:

    def __init__(self):
        self._count = 0
        self._sema = Semaphore()

    def capture_output(self):
        self._sema.acquire()
        if self._count == 0:
            self._capture_output()
        self._count += 1
        self._sema.release()

    def _capture_output(self):
        sys.stdout = _PythonOutput()
        sys.stderr = _PythonOutput()
        if os.name == 'java':
            self._orig_java_out = System.out
            self._orig_java_err = System.err
            self._capt_java_out = _JavaOutput()
            self._capt_java_err = _JavaOutput()
            System.setOut(PrintStream(self._capt_java_out))
            System.setErr(PrintStream(self._capt_java_err))

    def release_output(self):
        self._sema.acquire()
        out, err = self._get_output()
        self._count -= 1
        if self._count == 0:
            self._release_output()
        self._sema.release()
        return out, err
   
    def _get_output(self):
        if os.name == 'java':
            out = self._capt_java_out.get_value()
            err = self._capt_java_err.get_value()
            if (out, err) !=  ('', ''):
                return out, err
        return sys.stdout.get_value(), sys.stderr.get_value()

    def _release_output(self):
        sys.stdout = sys.__stdout__
        sys.stderr = sys.__stderr__
        if os.name == 'java':
            System.setOut(self._orig_java_out)
            System.setErr(self._orig_java_err)


_OUTPUT_CAPTURE = _OutputCapture()
 

文件路径:robotframework-2.1.2\src\robot\utils\outputcapture.py
功能:输入输出加锁互斥控制
阅读(33416) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~