Advertisement
Guest User

Lazy Fake Python

a guest
Jul 21st, 2010
274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.97 KB | None | 0 0
  1. '''
  2. LazyFakePython
  3. goathead 2010 - License GNU LGPL
  4. single call per line only
  5. attribute access ok
  6. argument types limited
  7. '''
  8.  
  9. class LazyPython(object):
  10.     def module( self, name ): return Proxy(name, space=self)
  11.     def __init__(self):
  12.         self.Commands = []
  13.         self.VarCount = 0
  14.     def flush(self):
  15.         self.VarCount = 0
  16.         a = ''
  17.         while self.Commands: a += self.Commands.pop(0) + ';'
  18.         return a
  19.  
  20.  
  21. class Proxy(object):
  22.  
  23.     def __init__(self,name, parent=None, space=None):
  24.         self._name = name
  25.         self._parent = parent
  26.         self._space = space
  27.     def __call__(self,*args,**kw):
  28.         Commands = self._space.Commands
  29.         #print( 'call ->', self._name )
  30.         a = ''
  31.         for arg in args:
  32.             if isinstance(arg, Proxy):
  33.                 if arg._parent: a += '%s,' %Commands.pop(-1)
  34.                 else: a += '%s,' %arg._name
  35.             elif type(arg) in (str,unicode): a += '"%s",' %arg
  36.             else: a += '%s,' %arg
  37.         for key in kw:
  38.             arg = kw[key]
  39.             if isinstance(arg, Proxy):
  40.                 if arg._parent: a += '%s=%s,' %(key,Commands.pop(-1))
  41.                 else: a += '%s=%s,' %(key,arg._name)
  42.             elif type(arg) in (str,unicode): a += '%s="%s",' %(key,arg)
  43.             else: a += '%s=%s,' %(key,arg)
  44.  
  45.         name = 'var%s' %self._space.VarCount
  46.         cmd = Commands[-1]
  47.         Commands[-1] = '%s = %s(%s)' %(name,cmd,a)
  48.         self._space.VarCount += 1
  49.         p = Proxy(name, space=self._space)
  50.         return p
  51.  
  52.     def __getattr__(self,name):
  53.         #print( 'getattr ->', name)
  54.         if not self._parent: self._space.Commands.append( self._name+'.'+name )
  55.         else: self._space.Commands[-1] += '.' + name
  56.         p = Proxy(name, parent=self, space=self._space)
  57.         return p
  58.  
  59. if __name__ == '__main__':
  60.     zpy = LazyPython()
  61.     bpy = zpy.module('bpy')
  62.  
  63.     bpy.ops
  64.     bpy.data.objects
  65.     v = bpy.nothing.nowhere.nohow(1,2,3,"hi", True, {'dict-test':1}, key="test" )
  66.     bpy.new.func(v.x)
  67.     #bpy.this.will.fail( v.x() )    # only one call per line allowed
  68.     for cmd in zpy.Commands:
  69.         print( cmd )
  70.    
  71.     pipe2blender = zpy.flush()
  72.     print( 'this string could be executed in blender by exec(s)' )
  73.     print( pipe2blender )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement