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