home *** CD-ROM | disk | FTP | other *** search
- # Source Generated with Decompyle++
- # File: in.pyo (Python 2.6)
-
- import inspect
- import util.net as net
- import util.callbacks as callbacks
- import util.mimehelpers as mimehelpers
-
- class API_Sender_FormPost(object):
-
- def _info_for_argument(self, arg):
- value = arg.value
- extra = { }
- if arg.spec.type == 'data':
- if isinstance(value, unicode):
- value = value.encode('utf8')
-
-
- if arg.spec.type == 'image-noenc':
- extra.update(disposition = dict(filename = 'image'))
-
- return dict(name = arg.spec.name, type = arg.spec.type, value = arg.value, **extra)
-
-
- def serialize_method_call(self, api, call):
- parts = map(self._info_for_argument, call.bound_args)
- mime_form = mimehelpers.form_builder(parts)
- return (call.get_endpoint(api.API_BASE), { }, mime_form)
-
-
-
- class API_MethodArgSpec(object):
-
- def __init__(self, name, type, optional = False):
- self.name = name
- self.type = type
- self.optional = optional
-
-
- def __repr__(self):
- return '%s(name = %r, type = %r, optional = %r)' % (type(self).__name__, self.name, self.type, self.optional)
-
-
- def __call__(self, val):
- arg = API_MethodArg(val, self)
- arg.typecheck()
- return arg
-
-
-
- class API_MethodArg(object):
- _typecheckers = {
- 'data': (lambda x: if not isinstance(x, basestring):
- passhasattr(x, 'read')),
- 'image-noenc': bytes,
- 'image': bytes }
-
- def __init__(self, value, argspec):
- self.value = value
- self.spec = argspec
-
-
- def typecheck(self):
- failed = False
- if self.spec.type in self._typecheckers:
- if not self._typecheckers[self.spec.type](self.value):
- failed = True
-
- elif type(self.spec.type) is type:
- if not isinstance(self.value, self.spec.type):
- failed = True
-
- else:
- failed = True
- if failed:
- raise TypeError('%r does not match argument specification: %r', self.value, self.spec)
- failed
-
-
- def __repr__(self):
- return '<%r(%r)>' % (self.spec, self.value)
-
-
-
- class API_Method(object):
-
- def __init__(self, *argspec, **properties):
- self.__dict__.update(properties)
- self.argspecs = argspec
- names = set()
- for spec in argspec:
- if spec.name in names:
- raise TypeError('Duplicate argument %r in function definition', spec.name)
- spec.name in names
- names.add(spec.name)
-
-
-
- def get_endpoint(self, base = None, name = None):
- if base and name:
- return net.httpjoin(base, name)
- endpoint = getattr(self, 'endpoint', None)
- if endpoint is None:
- base = getattr(self, 'api_base', base)
- name = getattr(self, 'name', name)
- if None in (base, name):
- raise ValueError('No known endpoint')
- None in (base, name)
- endpoint = net.httpjoin(base, name)
-
- return endpoint
-
-
- def __call__(self, *args, **kwargs):
- return API_MethodCall(self, *args, **kwargs)
-
-
- def __repr__(self):
- name = getattr(self, 'name', 'unnamed-function')
- args_strs = _[1]
- args_strs.extend((lambda .0: for arg in .0:
- if arg.optional:
- '%s = None : %s' % (arg.name, arg.type)continue)(self.argspecs))
- args_str = ', '.join(args_strs)
- return '<%s(%s)>' % (name, args_str)
-
-
-
- class API_MethodCall(object):
-
- def __init__(self, spec, *_args, **_kwargs):
- self.spec = spec
- self.argvals = _args
- self.kwargs = _kwargs
- args = dict((lambda .0: for spec in .0:
- (spec.name, (spec, None)))(self.spec.argspecs))
- i = 0
- for argval, argspec in enumerate(zip(self.argvals, self.spec.argspecs)):
- arg = argspec(argval)
- if args[argspec.name][1] is not None:
- raise TypeError('%r got multiple values for argument %r', self.spec, argspec.name)
- args[argspec.name][1] is not None
- args[argspec.name] = (argspec, arg)
-
- for argspec in self.spec.argspecs[i:]:
- if argspec.name in self.kwargs:
- arg = argspec(self.kwargs[argspec.name])
- if args[argspec.name][1] is not None:
- raise TypeError('%r got multiple values for argument %r', self.spec, argspec.name)
- args[argspec.name][1] is not None
- args[argspec.name] = (argspec, arg)
- continue
-
- for argspec, argval in args.values():
- if argval is None and not (argspec.optional):
- raise TypeError('Argument %r is required', argspec.name)
- not (argspec.optional)
-
- self.bound_args = (dict,)((lambda .0: for argspec in .0:
- (argspec.name, args[argspec.name][1]))(self.spec.argspecs))
-
-
- def get_endpoint(self, base = None, name = None):
- return self.spec.get_endpoint(base = base, name = name)
-
-
- def __repr__(self):
- return '%r(%s)' % (self.spec, ', '.join((lambda .0: for name, arg in .0:
- if arg is not None:
- '%s = %s' % (name, repr(arg.value)[:200])continue)(self.bound_args.items())))
-
-
-
- def apimethod_from_pyfunc(f, *_types, **k):
- pyspec = inspect.getargspec(f)
- args = []
- names = list(pyspec.args)
- types = list(_types)
- if 'self' == names[0]:
- names.pop(0)
-
- if pyspec.defaults is not None:
- defaults = list(pyspec.defaults)
- while defaults:
- default = defaults.pop(-1)
- argname = names.pop(-1)
- type = types.pop(-1)
- args.insert(0, API_MethodArgSpec(argname, type, optional = True))
- else:
- defaults = []
- while names and types:
- argname = names.pop(-1)
- type = types.pop(-1)
- args.insert(0, API_MethodArgSpec(argname, type, optional = False))
- if names and defaults or types:
- raise TypeError('Had extraneous stuff when defining an API method: names = %r, defaults = %r, types = %r', names, defaults, types)
- types
- methodname = k.pop('name', f.__name__)
- methodspec = API_Method(name = methodname, *args, **k)
- return methodspec
-
-
- def apicall(*_types, **kw):
-
- def funcwrapper(f):
- return apicaller(apimethod_from_pyfunc(f, *_types, **kw))
-
- return funcwrapper
-
-
- def apicaller(methodspec):
-
- def wrapper(api, *method_args, **method_kwargs):
- callback = method_kwargs.get('callback', None)
- method_call = methodspec(*method_args, **method_kwargs)
- response_handler = api.get_response_handler(method_call, callback)
- api.send_method(method_call, success = response_handler, error = callback.error)
-
- wrapper = (callbacks.callsback,)(wrapper)
- return wrapper
-
-