def __init__(self, function, args=None): self.api = hug.api.from_object(function) self.spec = getattr(function, 'original', function) self.arguments = introspect.arguments(function) self.name = introspect.name(function) self._function = function self.is_coroutine = introspect.is_coroutine(self.spec) if self.is_coroutine: self.spec = getattr(self.spec, '__wrapped__', self.spec) self.takes_args = introspect.takes_args(self.spec) self.takes_kwargs = introspect.takes_kwargs(self.spec) self.parameters = list( introspect.arguments(self.spec, self.takes_kwargs + self.takes_args)) if self.takes_kwargs: self.kwarg = self.parameters.pop(-1) if self.takes_args: self.arg = self.parameters.pop(-1) self.parameters = tuple(self.parameters) self.defaults = dict( zip(reversed(self.parameters), reversed(self.spec.__defaults__ or ()))) self.required = self.parameters[:-(len(self.spec.__defaults__ or ())) or None] self.is_method = introspect.is_method( self.spec) or introspect.is_method(function) if self.is_method: self.required = self.required[1:] self.parameters = self.parameters[1:] self.all_parameters = set(self.parameters) if self.spec is not function: self.all_parameters.update(self.arguments) if args is not None: transformers = args else: transformers = self.spec.__annotations__ self.transform = transformers.get('return', None) self.directives = {} self.input_transformations = {} for name, transformer in transformers.items(): if isinstance(transformer, str): continue elif hasattr(transformer, 'directive'): self.directives[name] = transformer continue if hasattr(transformer, 'from_string'): transformer = transformer.from_string elif hasattr(transformer, 'load'): transformer = MarshmallowInputSchema(transformer) elif hasattr(transformer, 'deserialize'): transformer = transformer.deserialize self.input_transformations[name] = transformer
def __init__(self, function): self.api = hug.api.from_object(function) self.spec = getattr(function, 'original', function) self.arguments = introspect.arguments(function) self.name = introspect.name(function) self._function = function self.is_coroutine = introspect.is_coroutine(self.spec) if self.is_coroutine: self.spec = getattr(self.spec, '__wrapped__', self.spec) self.takes_args = introspect.takes_args(self.spec) self.takes_kwargs = introspect.takes_kwargs(self.spec) self.parameters = list(introspect.arguments(self.spec, self.takes_kwargs + self.takes_args)) if self.takes_kwargs: self.kwarg = self.parameters.pop(-1) if self.takes_args: self.arg = self.parameters.pop(-1) self.parameters = tuple(self.parameters) self.defaults = dict(zip(reversed(self.parameters), reversed(self.spec.__defaults__ or ()))) self.required = self.parameters[:-(len(self.spec.__defaults__ or ())) or None] self.is_method = introspect.is_method(self.spec) or introspect.is_method(function) if self.is_method: self.required = self.required[1:] self.parameters = self.parameters[1:] self.all_parameters = set(self.parameters) if self.spec is not function: self.all_parameters.update(self.arguments) self.transform = self.spec.__annotations__.get('return', None) self.directives = {} self.input_transformations = {} for name, transformer in self.spec.__annotations__.items(): if isinstance(transformer, str): continue elif hasattr(transformer, 'directive'): self.directives[name] = transformer continue if hasattr(transformer, 'from_string'): transformer = transformer.from_string elif hasattr(transformer, 'load'): transformer = MarshmallowInputSchema(transformer) elif hasattr(transformer, 'deserialize'): transformer = transformer.deserialize self.input_transformations[name] = transformer
def auto_kwargs(function): """Modifies the provided function to support kwargs by only passing along kwargs for parameters it accepts""" supported = introspect.arguments(function) @wraps(function) def call_function(*args, **kwargs): return function(*args, **{key: value for key, value in kwargs.items() if key in supported}) return call_function
def __init__(self, function): self.spec = getattr(function, 'original', function) self.arguments = introspect.arguments(function) self._function = function self.is_coroutine = introspect.is_coroutine(self.spec) if self.is_coroutine: self.spec = getattr(self.spec, '__wrapped__', self.spec) self.takes_kargs = introspect.takes_kargs(self.spec) self.takes_kwargs = introspect.takes_kwargs(self.spec) self.parameters = introspect.arguments(self.spec, 1 if self.takes_kargs else 0) if self.takes_kargs: self.karg = self.parameters[-1] self.defaults = {} for index, default in enumerate(reversed(self.spec.__defaults__ or ())): self.defaults[self.parameters[-(index + 1)]] = default self.required = self.parameters[:-(len(self.spec.__defaults__ or ())) or None] if introspect.is_method(self.spec) or introspect.is_method(function): self.required = self.required[1:] self.parameters = self.parameters[1:] self.transform = self.spec.__annotations__.get('return', None) self.directives = {} self.input_transformations = {} for name, transformer in self.spec.__annotations__.items(): if isinstance(transformer, str): continue elif hasattr(transformer, 'directive'): self.directives[name] = transformer continue if hasattr(transformer, 'load'): transformer = MarshmallowSchema(transformer) elif hasattr(transformer, 'deserialize'): transformer = transformer.deserialize self.input_transformations[name] = transformer
def new_type_handler(function): class NewType(extend): __slots__ = () if chain and extend != Type: if error_text or exception_handlers: def __call__(self, value): try: value = super(NewType, self).__call__(value) return function(value) except Exception as exception: for take_exception, rewrite in exception_handlers.items( ): if isinstance(exception, take_exception): if isinstance(rewrite, str): raise ValueError(rewrite) else: raise rewrite(value) if error_text: raise ValueError(error_text) raise exception else: def __call__(self, value): value = super(NewType, self).__call__(value) return function(value) else: if error_text or exception_handlers: def __call__(self, value): try: return function(value) except Exception as exception: for take_exception, rewrite in exception_handlers.items( ): if isinstance(exception, take_exception): if isinstance(rewrite, str): raise ValueError(rewrite) else: raise rewrite(value) if error_text: raise ValueError(error_text) raise exception else: def __call__(self, value): return function(value) NewType.__doc__ = function.__doc__ if doc is None else doc if auto_instance and not (introspect.arguments(NewType.__init__, -1) or introspect.takes_kwargs(NewType.__init__) or introspect.takes_args(NewType.__init__)): return NewType() return NewType
def auto_kwargs(function): """Modifies the provided function to support kwargs by only passing along kwargs for parameters it accepts""" supported = introspect.arguments(function) @wraps(function) def call_function(*args, **kwargs): return function( *args, **{ key: value for key, value in kwargs.items() if key in supported }) return call_function
def __getattr__(self, name): function = self.api.http.versioned.get(self.api_version, {}).get(name, None) if not function: function = self.api.http.versioned.get(None, {}).get(name, None) if not function: raise AttributeError('API Function {0} not found'.format(name)) accepts = introspect.arguments(function.interface.function) if 'hug_api_version' in accepts: function = partial(function, hug_api_version=self.api_version) if 'hug_current_api' in accepts: function = partial(function, hug_current_api=self) return function
def __init__(self, function): self.spec = getattr(function, 'original', function) self.function = function self.takes_kargs = introspect.takes_kargs(self.spec) self.takes_kwargs = introspect.takes_kwargs(self.spec) self.parameters = introspect.arguments(self.spec, 1 if self.takes_kargs else 0) if self.takes_kargs: self.karg = self.parameters[-1] self.defaults = {} for index, default in enumerate(reversed(self.spec.__defaults__ or ())): self.defaults[self.parameters[-(index + 1)]] = default self.required = self.parameters[:-(len(self.spec.__defaults__ or ())) or None] if introspect.is_method(self.spec): self.required = self.required[1:] self.parameters = self.parameters[1:] self.transform = self.spec.__annotations__.get('return', None) self.directives = {} self.input_transformations = {} for name, transformer in self.spec.__annotations__.items(): if isinstance(transformer, str): continue elif hasattr(transformer, 'directive'): self.directives[name] = transformer continue if hasattr(transformer, 'load'): transformer = MarshmallowSchema(transformer) elif hasattr(transformer, 'deserialize'): transformer = transformer.deserialize self.input_transformations[name] = transformer
def new_type_handler(function): class NewType(extend): __slots__ = () _accept_context = accept_context if chain and extend != Type: if error_text or exception_handlers: if not accept_context: def __call__(self, value): try: value = super(NewType, self).__call__(value) return function(value) except Exception as exception: for take_exception, rewrite in exception_handlers.items(): if isinstance(exception, take_exception): if isinstance(rewrite, str): raise ValueError(rewrite) else: raise rewrite(value) if error_text: raise ValueError(error_text) raise exception else: if extend._accept_context: def __call__(self, value, context): try: value = super(NewType, self).__call__(value, context) return function(value, context) except Exception as exception: for take_exception, rewrite in exception_handlers.items(): if isinstance(exception, take_exception): if isinstance(rewrite, str): raise ValueError(rewrite) else: raise rewrite(value) if error_text: raise ValueError(error_text) raise exception else: def __call__(self, value, context): try: value = super(NewType, self).__call__(value) return function(value, context) except Exception as exception: for take_exception, rewrite in exception_handlers.items(): if isinstance(exception, take_exception): if isinstance(rewrite, str): raise ValueError(rewrite) else: raise rewrite(value) if error_text: raise ValueError(error_text) raise exception else: if not accept_context: def __call__(self, value): value = super(NewType, self).__call__(value) return function(value) else: if extend._accept_context: def __call__(self, value, context): value = super(NewType, self).__call__(value, context) return function(value, context) else: def __call__(self, value, context): value = super(NewType, self).__call__(value) return function(value, context) else: if not accept_context: if error_text or exception_handlers: def __call__(self, value): try: return function(value) except Exception as exception: for take_exception, rewrite in exception_handlers.items(): if isinstance(exception, take_exception): if isinstance(rewrite, str): raise ValueError(rewrite) else: raise rewrite(value) if error_text: raise ValueError(error_text) raise exception else: def __call__(self, value): return function(value) else: if error_text or exception_handlers: def __call__(self, value, context): try: return function(value, context) except Exception as exception: for take_exception, rewrite in exception_handlers.items(): if isinstance(exception, take_exception): if isinstance(rewrite, str): raise ValueError(rewrite) else: raise rewrite(value) if error_text: raise ValueError(error_text) raise exception else: def __call__(self, value, context): return function(value, context) NewType.__doc__ = function.__doc__ if doc is None else doc if auto_instance and not (introspect.arguments(NewType.__init__, -1) or introspect.takes_kwargs(NewType.__init__) or introspect.takes_args(NewType.__init__)): return NewType() return NewType