remote_pipe.py :  » Development » PyObjC » trunk » pyobjc » pyobjc-core » Examples » NonFunctional » RemotePyInterpreter » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Development » PyObjC 
PyObjC » trunk » pyobjc » pyobjc core » Examples » NonFunctional » RemotePyInterpreter » remote_pipe.py
import itertools

def as_unicode(s, encoding='utf-8'):
    typ = type(s)
    if typ is unicode:
        pass
    elif issubclass(typ, unicode):
        s = unicode(s)
    elif issubclass(typ, str):
        s = unicode(s, encoding, 'replace')
    else:
        raise TypeError, 'expecting basestring, not %s' % (typ.__name__,)
    return s


def as_str(s, encoding='utf-8'):
    typ = type(s)
    if typ is str:
        pass
    elif issubclass(typ, str):
        s = str(s)
    elif issubclass(typ, unicode):
        s = s.encode(encoding)
    else:
        raise TypeError, 'expecting basestring, not %s' % (typ.__name__,)
    return s


class RemotePipe(object):
    def __init__(self, runcode, clientfile, netReprCenter, namespace, pool):
        self.runcode = runcode
        self.pool = pool
        self.clientfile = clientfile
        self.namespace = namespace
        self.result = self.namespace['__result__'] = {}
        self.netReprCenter = netReprCenter
        self.netrepr_list = netReprCenter.netrepr_list
        self.sequence = itertools.count()
        self.stdin = RemoteFileLike(self, 'stdin')
        self.stdout = RemoteFileLike(self, 'stdout')
        self.stderr = RemoteFileLike(self, 'stderr')

    def send(self, *args):
        self.clientfile.write(self.netrepr_list(args) + '\n')
        self.clientfile.flush()

    def respond(self, *args):
        self.send('respond', *args)
        
    def expect(self, *args):
        self.pool.push()
        try:
            return self._expect(*args)
        finally:
            self.pool.pop()

    def _expect(self, *args):
        ident = self.sequence.next()
        self.send('expect', ident, *args)
        while ident not in self.result:
            self.runcode(self.clientfile, self.namespace)
        return self.result.pop(ident)


class RemoteFileLike(object):
    softspace = 0
    closed = False
    encoding = 'utf-8'

    def __init__(self, pipe, ident):
        self.pipe = pipe
        self.ident = ident

    def __iter__(self):
        while True:
            rval = self.readline()
            if not rval:
                break
            yield rval

    def write(self, s):
        s = as_unicode(s, self.encoding)
        self.pipe.expect('RemoteFileLike.write', self.ident, s)

    def writelines(self, lines):
        for line in lines:
            self.write(line)
    
    def close(self):
        self.closed = True

    def flush(self):
        pass

    def isatty(self):
        return True

    def read(self, size=-1):
        return as_str(
            self.pipe.expect('RemoteFileLike.read', self.ident, size),
            self.encoding,
        )

    def readline(self, size=-1):
        return as_str(
            self.pipe.expect('RemoteFileLike.readline', self.ident, size),
            self.encoding,
        )

    def readlines(self):
        return list(self)
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.