home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2011 February / maximum-cd-2011-02.iso / DiscContents / digsby_setup85.exe / lib / util / apitools.pyo (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2010-11-24  |  8.3 KB  |  222 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.6)
  3.  
  4. import inspect
  5. import util.net as net
  6. import util.callbacks as callbacks
  7. import util.mimehelpers as mimehelpers
  8.  
  9. class API_Sender_FormPost(object):
  10.     
  11.     def _info_for_argument(self, arg):
  12.         value = arg.value
  13.         extra = { }
  14.         if arg.spec.type == 'data':
  15.             if isinstance(value, unicode):
  16.                 value = value.encode('utf8')
  17.             
  18.         
  19.         if arg.spec.type == 'image-noenc':
  20.             extra.update(disposition = dict(filename = 'image'))
  21.         
  22.         return dict(name = arg.spec.name, type = arg.spec.type, value = arg.value, **extra)
  23.  
  24.     
  25.     def serialize_method_call(self, api, call):
  26.         parts = map(self._info_for_argument, call.bound_args)
  27.         mime_form = mimehelpers.form_builder(parts)
  28.         return (call.get_endpoint(api.API_BASE), { }, mime_form)
  29.  
  30.  
  31.  
  32. class API_MethodArgSpec(object):
  33.     
  34.     def __init__(self, name, type, optional = False):
  35.         self.name = name
  36.         self.type = type
  37.         self.optional = optional
  38.  
  39.     
  40.     def __repr__(self):
  41.         return '%s(name = %r, type = %r, optional = %r)' % (type(self).__name__, self.name, self.type, self.optional)
  42.  
  43.     
  44.     def __call__(self, val):
  45.         arg = API_MethodArg(val, self)
  46.         arg.typecheck()
  47.         return arg
  48.  
  49.  
  50.  
  51. class API_MethodArg(object):
  52.     _typecheckers = {
  53.         'data': (lambda x: if not isinstance(x, basestring):
  54. passhasattr(x, 'read')),
  55.         'image-noenc': bytes,
  56.         'image': bytes }
  57.     
  58.     def __init__(self, value, argspec):
  59.         self.value = value
  60.         self.spec = argspec
  61.  
  62.     
  63.     def typecheck(self):
  64.         failed = False
  65.         if self.spec.type in self._typecheckers:
  66.             if not self._typecheckers[self.spec.type](self.value):
  67.                 failed = True
  68.             
  69.         elif type(self.spec.type) is type:
  70.             if not isinstance(self.value, self.spec.type):
  71.                 failed = True
  72.             
  73.         else:
  74.             failed = True
  75.         if failed:
  76.             raise TypeError('%r does not match argument specification: %r', self.value, self.spec)
  77.         failed
  78.  
  79.     
  80.     def __repr__(self):
  81.         return '<%r(%r)>' % (self.spec, self.value)
  82.  
  83.  
  84.  
  85. class API_Method(object):
  86.     
  87.     def __init__(self, *argspec, **properties):
  88.         self.__dict__.update(properties)
  89.         self.argspecs = argspec
  90.         names = set()
  91.         for spec in argspec:
  92.             if spec.name in names:
  93.                 raise TypeError('Duplicate argument %r in function definition', spec.name)
  94.             spec.name in names
  95.             names.add(spec.name)
  96.         
  97.  
  98.     
  99.     def get_endpoint(self, base = None, name = None):
  100.         if base and name:
  101.             return net.httpjoin(base, name)
  102.         endpoint = getattr(self, 'endpoint', None)
  103.         if endpoint is None:
  104.             base = getattr(self, 'api_base', base)
  105.             name = getattr(self, 'name', name)
  106.             if None in (base, name):
  107.                 raise ValueError('No known endpoint')
  108.             None in (base, name)
  109.             endpoint = net.httpjoin(base, name)
  110.         
  111.         return endpoint
  112.  
  113.     
  114.     def __call__(self, *args, **kwargs):
  115.         return API_MethodCall(self, *args, **kwargs)
  116.  
  117.     
  118.     def __repr__(self):
  119.         name = getattr(self, 'name', 'unnamed-function')
  120.         args_strs = _[1]
  121.         args_strs.extend((lambda .0: for arg in .0:
  122. if arg.optional:
  123. '%s = None : %s' % (arg.name, arg.type)continue)(self.argspecs))
  124.         args_str = ', '.join(args_strs)
  125.         return '<%s(%s)>' % (name, args_str)
  126.  
  127.  
  128.  
  129. class API_MethodCall(object):
  130.     
  131.     def __init__(self, spec, *_args, **_kwargs):
  132.         self.spec = spec
  133.         self.argvals = _args
  134.         self.kwargs = _kwargs
  135.         args = dict((lambda .0: for spec in .0:
  136. (spec.name, (spec, None)))(self.spec.argspecs))
  137.         i = 0
  138.         for argval, argspec in enumerate(zip(self.argvals, self.spec.argspecs)):
  139.             arg = argspec(argval)
  140.             if args[argspec.name][1] is not None:
  141.                 raise TypeError('%r got multiple values for argument %r', self.spec, argspec.name)
  142.             args[argspec.name][1] is not None
  143.             args[argspec.name] = (argspec, arg)
  144.         
  145.         for argspec in self.spec.argspecs[i:]:
  146.             if argspec.name in self.kwargs:
  147.                 arg = argspec(self.kwargs[argspec.name])
  148.                 if args[argspec.name][1] is not None:
  149.                     raise TypeError('%r got multiple values for argument %r', self.spec, argspec.name)
  150.                 args[argspec.name][1] is not None
  151.                 args[argspec.name] = (argspec, arg)
  152.                 continue
  153.         
  154.         for argspec, argval in args.values():
  155.             if argval is None and not (argspec.optional):
  156.                 raise TypeError('Argument %r is required', argspec.name)
  157.             not (argspec.optional)
  158.         
  159.         self.bound_args = (dict,)((lambda .0: for argspec in .0:
  160. (argspec.name, args[argspec.name][1]))(self.spec.argspecs))
  161.  
  162.     
  163.     def get_endpoint(self, base = None, name = None):
  164.         return self.spec.get_endpoint(base = base, name = name)
  165.  
  166.     
  167.     def __repr__(self):
  168.         return '%r(%s)' % (self.spec, ', '.join((lambda .0: for name, arg in .0:
  169. if arg is not None:
  170. '%s = %s' % (name, repr(arg.value)[:200])continue)(self.bound_args.items())))
  171.  
  172.  
  173.  
  174. def apimethod_from_pyfunc(f, *_types, **k):
  175.     pyspec = inspect.getargspec(f)
  176.     args = []
  177.     names = list(pyspec.args)
  178.     types = list(_types)
  179.     if 'self' == names[0]:
  180.         names.pop(0)
  181.     
  182.     if pyspec.defaults is not None:
  183.         defaults = list(pyspec.defaults)
  184.         while defaults:
  185.             default = defaults.pop(-1)
  186.             argname = names.pop(-1)
  187.             type = types.pop(-1)
  188.             args.insert(0, API_MethodArgSpec(argname, type, optional = True))
  189.     else:
  190.         defaults = []
  191.     while names and types:
  192.         argname = names.pop(-1)
  193.         type = types.pop(-1)
  194.         args.insert(0, API_MethodArgSpec(argname, type, optional = False))
  195.     if names and defaults or types:
  196.         raise TypeError('Had extraneous stuff when defining an API method: names = %r, defaults = %r, types = %r', names, defaults, types)
  197.     types
  198.     methodname = k.pop('name', f.__name__)
  199.     methodspec = API_Method(name = methodname, *args, **k)
  200.     return methodspec
  201.  
  202.  
  203. def apicall(*_types, **kw):
  204.     
  205.     def funcwrapper(f):
  206.         return apicaller(apimethod_from_pyfunc(f, *_types, **kw))
  207.  
  208.     return funcwrapper
  209.  
  210.  
  211. def apicaller(methodspec):
  212.     
  213.     def wrapper(api, *method_args, **method_kwargs):
  214.         callback = method_kwargs.get('callback', None)
  215.         method_call = methodspec(*method_args, **method_kwargs)
  216.         response_handler = api.get_response_handler(method_call, callback)
  217.         api.send_method(method_call, success = response_handler, error = callback.error)
  218.  
  219.     wrapper = (callbacks.callsback,)(wrapper)
  220.     return wrapper
  221.  
  222.