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 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 __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 image_handler(data, **kwargs): if hasattr(data, 'read'): return data elif hasattr(data, 'save'): output = stream() if introspect.takes_all_arguments(data.save, 'format') or introspect.takes_kwargs(data.save): data.save(output, format=image_format.upper()) else: data.save(output) output.seek(0) return output elif hasattr(data, 'render'): return data.render() elif os.path.isfile(data): return open(data, 'rb')
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 __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