load python script from memory stream

没有什么技术含量。T_T 真简单
from parser import *
class MemoryLoader:
    def __init__(self):
        self.allFuncs = {}
   
    def loadFromStream(self, inStream, fileName = ""):
        ast = suite(inStream)
        ldist = {}
        eval(ast.compile(fileName), ldist)
        if not ldist.has_key('__exports__'):
            raise "'%s' must has __exports__ value"%fileName
        for f in ldist['__exports__']:
            fo = ldist[f]
            self.allFuncs[f] = fo
    def call(self, name, *args, **kwds):
        self.allFuncs[name](*args, **kwds)
        
def test():
    loader = MemoryLoader()
    loader.loadFromStream("""__exports__=['foo']\ndef foo(str):\n\tprint str[::-1]""")
    loader.call('foo','hello')
if __name__ == '__main__':
    test()