home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2011 January / maximum-cd-2011-01.iso / DiscContents / xbmc-9.11.exe / system / python / spyce / modules / spylambda.py < prev    next >
Encoding:
Python Source  |  2009-12-23  |  1.9 KB  |  56 lines

  1. ##################################################
  2. # SPYCE - Python-based HTML Scripting
  3. # Copyright (c) 2002 Rimon Barr.
  4. #
  5. # Refer to spyce.py
  6. # CVS: $Id: spylambda.py 20864 2009-06-02 06:16:47Z ceros7 $
  7. ##################################################
  8.  
  9. from spyceModule import spyceModule
  10. import string
  11.  
  12. __doc__ = """spylambda module produces functions from spyce strings."""
  13.  
  14. class spylambda(spyceModule):
  15.   def define(self, sig, code, memoize=0):
  16.     # compile spyce to cache errors early
  17.     spycecode = self._api.spyceString((code, sig))
  18.     if string.strip(sig): sigcomma = sig + ','
  19.     def processSpyce(args, kwargs, self=self, spycecode=spycecode):
  20.       s = None
  21.       try:
  22.         s = spycecode.newWrapper()
  23.         modules = self._api.getModules()
  24.         for name in modules.keys():
  25.           s.setModule(name, modules[name])
  26.         s.spyceInit(self._api.getRequest(), self._api.getResponse())
  27.         result = apply(s.spyceProcess, args, kwargs)
  28.       finally:
  29.         if s:
  30.           s.spyceDestroy()
  31.           spycecode.returnWrapper(s)
  32.       return result
  33.     if memoize:
  34.       def memoizer(f, id, stdout=self._api.getModule('stdout')):
  35.         def memoized(args, kwargs, f=f, id=id, stdout=stdout):
  36.           key = id, `args, kwargs`
  37.           try: r, s = stdout.memoizeCache[key]
  38.           except:
  39.             r, s = stdout.memoizeCache[key] = apply(stdout.capture, (f, args, kwargs))
  40.           print s
  41.           return r
  42.         return memoized
  43.       processSpyce = memoizer(processSpyce, code)
  44.     def makeArgProcessor(f):
  45.       dict = { 'f': f }
  46.       exec '''
  47. def processArg(*args, **kwargs):
  48.   return f(args, kwargs)
  49. ''' in dict
  50.       return dict['processArg']
  51.     return makeArgProcessor(processSpyce)
  52.   def __call__(self, sig, code, memoize=0):
  53.     return self.define(sig, code)
  54.   def __repr__(self):
  55.     return ''
  56.