def preprocess(func, c, expose_request=False): """ A decorator that facilitates preprocessing per method. Setting C{expose_request} to C{True} will set the underlying request object (if there is one), usually HTTP and set it to the first argument of the preprocessing callable. If there is no request object, the default is C{None}. @raise TypeError: C{func} and preprocessor must be callable. """ if not python.callable(func): raise TypeError('func must be callable') if not python.callable(c): raise TypeError('Preprocessor must be callable') attr = func if isinstance(func, types.UnboundMethodType): attr = func.im_func if expose_request is True: c = globals()['expose_request'](c) setattr(attr, '_pyamf_preprocessor', c) return func
def getTypeFunc(self, data): """ Returns a callable that will encode C{data} to C{self.stream}. If C{data} is unencodable, then C{None} is returned. """ if data is None: return self.writeNull t = type(data) print "data" print data # try types that we know will work if t is str or issubclass(t, str): return self.writeBytes if t is unicode or issubclass(t, unicode): return self.writeString elif t is bool: return self.writeBoolean elif t is float: return self.writeNumber elif t in python.int_types: return self.writeNumber elif t in (list, tuple): return self.writeList elif isinstance(data, (list, tuple)): return self.writeSequence elif t is types.GeneratorType: return self.writeGenerator elif t is pyamf.UndefinedType: return self.writeUndefined elif t in (datetime.date, datetime.datetime, datetime.time): return self.writeDate # FIX: while data was not is_xml, this code caused pyamf to fail during the second video play #elif xml.is_xml(data): # return self.writeXML # check for any overridden types for type_, func in pyamf.TYPE_MAP.iteritems(): try: if isinstance(data, type_): return _CustomTypeFunc(self, func) except TypeError: if python.callable(type_) and type_(data): return _CustomTypeFunc(self, func) # now try some types that won't encode if t in python.class_types: # can't encode classes return None elif isinstance(data, python.func_types): # can't encode code objects return None elif isinstance(t, types.ModuleType): # cannot encode module objects return None # well, we tried .. return self.writeObject
def assertDecodes(self, bytes, cb, encoding=pyamf.AMF3, raw=False): if not isinstance(bytes, basestring): bytes = _join(bytes) ret = list(pyamf.decode(bytes, encoding=encoding)) if not raw and len(ret) == 1: ret = ret[0] if python.callable(cb): cb(ret) else: self.assertEqual(ret, cb)
def assertDecodes(self, bytes, cb, encoding=pyamf.AMF3, raw=False): if not isinstance(bytes, six.string_types): bytes = _join(bytes) ret = list(pyamf.decode(bytes, encoding=encoding)) if not raw and len(ret) == 1: ret = ret[0] if python.callable(cb): cb(ret) else: self.assertEqual(ret, cb)
def addService(self, service, name=None, description=None, authenticator=None, expose_request=None, preprocessor=None): """ Adds a service to the gateway. @param service: The service to add to the gateway. @type service: C{callable}, class instance, or a module @param name: The name of the service. @type name: C{str} @raise pyamf.remoting.RemotingError: Service already exists. @raise TypeError: C{service} cannot be a scalar value. @raise TypeError: C{service} must be C{callable} or a module. """ if isinstance(service, (int, float, six.string_types)): raise TypeError("Service cannot be a scalar value") allowed_types = ( types.ModuleType, types.FunctionType, dict, types.MethodType, types.InstanceType, object, ) if not python.callable(service) and \ not isinstance(service, allowed_types): raise TypeError("Service must be a callable, module, or an object") if name is None: # TODO: include the module in the name if isinstance(service, type): name = service.__name__ elif isinstance(service, types.FunctionType): name = service.__name__ elif isinstance(service, types.ModuleType): name = service.__name__ else: name = str(service) if name in self.services: raise remoting.RemotingError("Service %s already exists" % name) self.services[name] = ServiceWrapper(service, description, authenticator, expose_request, preprocessor)
def _get_service_func(self, method, params): """ @raise InvalidServiceMethodError: Calls to private methods are not allowed. @raise UnknownServiceMethodError: Unknown method. @raise InvalidServiceMethodError: Service method must be callable. """ service = None if isinstance(self.service, (type, types.ClassType)): service = self.service() else: service = self.service if method is not None: method = str(method) if method.startswith('_'): raise InvalidServiceMethodError( "Calls to private methods are not allowed") try: func = getattr(service, method) except AttributeError: raise UnknownServiceMethodError( "Unknown method %s" % str(method)) if not python.callable(func): raise InvalidServiceMethodError( "Service method %s must be callable" % str(method)) return func if not python.callable(service): raise UnknownServiceMethodError( "Unknown method %s" % str(self.service)) return service
def _get_service_func(self, method, params): """ @raise InvalidServiceMethodError: Calls to private methods are not allowed. @raise UnknownServiceMethodError: Unknown method. @raise InvalidServiceMethodError: Service method must be callable. """ service = None if isinstance(self.service, type): service = self.service() else: service = self.service if method is not None: method = str(method) if method.startswith('_'): raise InvalidServiceMethodError( "Calls to private methods are not allowed") try: func = getattr(service, method) except AttributeError: raise UnknownServiceMethodError( "Unknown method %s" % str(method)) if not python.callable(func): raise InvalidServiceMethodError( "Service method %s must be callable" % str(method)) return func if not python.callable(service): raise UnknownServiceMethodError( "Unknown method %s" % str(self.service)) return service
def addService(self, service, name=None, description=None, authenticator=None, expose_request=None, preprocessor=None): """ Adds a service to the gateway. @param service: The service to add to the gateway. @type service: C{callable}, class instance, or a module @param name: The name of the service. @type name: C{str} @raise pyamf.remoting.RemotingError: Service already exists. @raise TypeError: C{service} cannot be a scalar value. @raise TypeError: C{service} must be C{callable} or a module. """ if isinstance(service, (int, long, float, basestring)): raise TypeError("Service cannot be a scalar value") allowed_types = ( types.ModuleType, types.FunctionType, types.DictType, types.MethodType, types.InstanceType, types.ObjectType, ) if not python.callable(service) and \ not isinstance(service, allowed_types): raise TypeError("Service must be a callable, module, or an object") if name is None: # TODO: include the module in the name if isinstance(service, (type, types.ClassType)): name = service.__name__ elif isinstance(service, types.FunctionType): name = service.func_name elif isinstance(service, types.ModuleType): name = service.__name__ else: name = str(service) if name in self.services: raise remoting.RemotingError("Service %s already exists" % name) self.services[name] = ServiceWrapper( service, description, authenticator, expose_request, preprocessor )
def expose_request(func): """ A decorator that adds an expose_request flag to the underlying callable. @raise TypeError: C{func} must be callable. """ if not python.callable(func): raise TypeError("func must be callable") if isinstance(func, types.UnboundMethodType): setattr(func.im_func, '_pyamf_expose_request', True) else: setattr(func, '_pyamf_expose_request', True) return func
def expose_request(func): """ A decorator that adds an expose_request flag to the underlying callable. @raise TypeError: C{func} must be callable. """ if not python.callable(func): raise TypeError("func must be callable") if isinstance(func, types.UnboundMethodType): setattr(func.__func__, '_pyamf_expose_request', True) else: setattr(func, '_pyamf_expose_request', True) return func
def getMethods(self): """ Gets a C{dict} of valid method callables for the underlying service object. """ callables = {} for name in dir(self.service): method = getattr(self.service, name) if name.startswith('_') or not python.callable(method): continue callables[name] = method return callables
def add_post_decode_processor(func): """ Adds a function to be called when a payload has been successfully decoded. This is useful for adapter as the last chance to modify the Python graph before it enters user land. The function takes two arguments, the decoded payload and the context's `extra` dict. It MUST return the payload that will be finally returned. @see: L{pyamf.codec.Decoder.finalise} @since: 0.7.0 """ if not python.callable(func): raise TypeError("%r must be callable" % (func,)) POST_DECODE_PROCESSORS.append(func)
def add_post_decode_processor(func): """ Adds a function to be called when a payload has been successfully decoded. This is useful for adapter as the last chance to modify the Python graph before it enters user land. The function takes two arguments, the decoded payload and the context's `extra` dict. It MUST return the payload that will be finally returned. @see: L{pyamf.codec.Decoder.finalise} @since: 0.7.0 """ if not python.callable(func): raise TypeError('%r must be callable' % (func,)) POST_DECODE_PROCESSORS.append(func)