def __init__(self, callback, path=NoPath, methods=Method.GET, resource=None, tags=None, summary=None, middleware=None): # type: (Callable, Path, Methods, Type[Resource], Tags, str, List[Any]) -> None """ :param callback: Function we are routing :param path: A sub path that can be used as a action. :param methods: HTTP method(s) this function responses to. :param resource: Specify the resource that this function encodes/decodes, default is the one specified on the ResourceAPI instance. :param tags: Tags to be applied to operation :param summary: Summary of the what method does (for documentation) :param middleware: List of additional middleware """ self.base_callback = self.callback = callback self.url_path = UrlPath.from_object(path) self.methods = force_tuple(methods) self._resource = resource # Sorting/hashing self.sort_key = Operation._operation_count Operation._operation_count += 1 # If this operation is bound to a ResourceAPI self.binding = None self.middleware = MiddlewareList(middleware or []) self.middleware.append( self) # Add self as middleware to obtain pre-dispatch support # Security object self.security = None # Documentation self.deprecated = False self.summary = summary self.consumes = set() self.produces = set() self.responses = set() self.parameters = set() self._tags = set(force_tuple(tags)) # Copy values from callback (if defined) for attr in ('deprecated', 'consumes', 'produces', 'responses', 'parameters', 'security'): value = getattr(callback, attr, None) if value is not None: setattr(self, attr, value) # Add a default response self.responses.add(DefaultResponse('Unhandled error', Error))
def __init__(self, title, enabled=True, enable_ui=False, host=None, schemes=None): # type: (str, bool, bool, str, Union[str, Tuple[str]]) -> None # Register operations if enabled: self._operations.append(Operation(SwaggerSpec.get_swagger)) if enable_ui: self._operations.append( Operation(SwaggerSpec.get_ui, UrlPath.parse('ui'))) self._operations.append( Operation(SwaggerSpec.get_static, UrlPath.parse('ui/{file_name:String}'))) super(SwaggerSpec, self).__init__() self.title = title self.enabled = enabled self.enable_ui = enabled and enable_ui self.host = host self.schemes = set(force_tuple(schemes or ())) self._ui_cache = None
def contribute_to_class(self, cls, _): cls._meta = self self.name = cls.__name__ self.class_name = "%s.%s" % (cls.__module__, cls.__name__) if self.meta: meta_attrs = self.meta.__dict__.copy() for name in self.meta.__dict__: if name.startswith("_"): del meta_attrs[name] for attr_name in self.META_OPTION_NAMES: if attr_name in meta_attrs: # Allow meta to be defined as namespace if attr_name == "namespace": setattr(self, "name_space", meta_attrs.pop(attr_name)) # Allow key_field_names to be defined as key_field_name elif attr_name == "key_field_name": setattr(self, "key_field_names", meta_attrs.pop(attr_name)) else: setattr(self, attr_name, meta_attrs.pop(attr_name)) elif hasattr(self.meta, attr_name): setattr(self, attr_name, getattr(self.meta, attr_name)) # Any leftover attributes must be invalid. if meta_attrs != {}: raise TypeError("'class Meta' got invalid attribute(s): %s" % ",".join(meta_attrs.keys())) del self.meta # Ensure key fields is a tuple self.key_field_names = force_tuple(self.key_field_names) if not self.verbose_name: self.verbose_name = self.name.replace("_", " ").strip("_ ") if not self.verbose_name_plural: self.verbose_name_plural = self.verbose_name + "s"
def __init__(self, field_names, separator='', **kwargs): """ :param field_names: Name(s) of fields to make up key :type field_names: str | tuple[str] | list[str] :param separator: Separator to use between values. :type separator: str :param kwargs: Additional kwargs for VirtualField """ kwargs.setdefault('data_type_name', 'String') super(MultiPartField, self).__init__(**kwargs) self.field_names = force_tuple(field_names) self.separator = separator self._fields = None
def contribute_to_class(self, cls, _): cls._meta = self self.name = cls.__name__ self.class_name = "%s.%s" % (cls.__module__, cls.__name__) if self.meta: meta_attrs = { name: value for name, value in self.meta.__dict__.copy().items() if not name.startswith('_') } for attr_name in self.META_OPTION_NAMES: if attr_name in meta_attrs: value = meta_attrs.pop(attr_name) # Allow meta to be defined as namespace if attr_name == 'namespace': attr_name = 'name_space' # Allow key_field_names to be defined as key_field_name elif attr_name == 'key_field_name': attr_name = 'key_field_names' value = [value] setattr(self, attr_name, value) elif hasattr(self.meta, attr_name): setattr(self, attr_name, getattr(self.meta, attr_name)) # Any leftover attributes must be invalid. if meta_attrs != {}: raise TypeError("'class Meta' got invalid attribute(s): %s" % ','.join(meta_attrs.keys())) del self.meta # Ensure key fields is a tuple self.key_field_names = force_tuple(self.key_field_names) if not self.verbose_name: self.verbose_name = self.name.replace('_', ' ').strip('_ ') if not self.verbose_name_plural: self.verbose_name_plural = self.verbose_name + 's'
def test_values(self, value, expected): assert utils.force_tuple(value) == expected
def __getitem__(self, item): # type: (Union[int, slice]) -> UrlPath return UrlPath(*force_tuple(self._nodes[item]))