def _add_ns_sio(self):
        """ Adds metadata about the service's namespace and SimpleIO definition.
        """
        # Namespace can be declared as a service-level attribute of a module-level one. Former takes precedence.
        service_ns = getattr(self.service_class, 'namespace', APISPEC.NAMESPACE_NULL)
        mod = getmodule(self.service_class)
        mod_ns = getattr(mod, 'namespace', APISPEC.NAMESPACE_NULL)

        self.namespace.name = service_ns if service_ns else mod_ns

        # Set namespace's documentation but only if it was declared top-level and is equal to our own
        if self.namespace.name and self.namespace.name == mod_ns:
            self.namespace.docs = getattr(mod, 'namespace_docs', '')

        # SimpleIO
        sio = getattr(self.service_class, 'SimpleIO', None)

        if sio:
            for api_spec_info in SIO_TYPE_MAP:

                _api_spec_info = Bunch()
                _api_spec_info.name = api_spec_info.name
                _api_spec_info.param_list = Bunch()
                _api_spec_info.request_elem = getattr(sio, 'request_elem', None)
                _api_spec_info.response_elem = getattr(sio, 'response_elem', None)

                for param_list_name in _sio_attrs:
                    _param_list = []
                    param_list = getattr(sio, param_list_name, [])
                    param_list = param_list if isinstance(param_list, (tuple, list)) else [param_list]

                    for param in param_list:
                        param_name = param if isinstance(param, basestring) else param.name
                        _param_info = Bunch()
                        _param_info.name = param_name
                        _param_info.is_required = 'required' in param_list_name

                        if isinstance(param, AsIs):
                            type_info = api_spec_info.DEFAULT

                        elif is_bool(param, param_name, self.simple_io_config.bool.prefix):
                            type_info = api_spec_info.BOOLEAN

                        elif is_int(param_name, self.simple_io_config.int.exact, self.simple_io_config.int.suffix):
                            type_info = api_spec_info.INTEGER

                        else:
                            try:
                                type_info = api_spec_info.map[param.__class__]
                            except KeyError:
                                type_info = api_spec_info.DEFAULT

                        _param_info.type, _param_info.subtype = type_info
                        _param_list.append(_param_info)

                    _api_spec_info.param_list[param_list_name] = _param_list

                self.simple_io[_api_spec_info.name] = SimpleIO(_api_spec_info).to_bunch()
Beispiel #2
0
    def _add_ns_sio(self):
        """ Adds metadata about the service's namespace and SimpleIO definition.
        """
        # Namespace can be declared as a service-level attribute of a module-level one. Former takes precedence.
        service_ns = getattr(self.service_class, 'namespace',
                             APISPEC.NAMESPACE_NULL)
        mod = getmodule(self.service_class)
        mod_ns = getattr(mod, 'namespace', APISPEC.NAMESPACE_NULL)

        self.namespace.name = service_ns if service_ns else mod_ns

        # Set namespace's documentation but only if it was declared top-level and is equal to our own
        if self.namespace.name and self.namespace.name == mod_ns:
            self.namespace.docs = getattr(mod, 'namespace_docs', '')

        # SimpleIO
        sio = getattr(self.service_class, 'SimpleIO', None)

        if sio:

            # This can be reused across all the output data formats
            sio_desc = self.get_sio_desc(sio)

            for api_spec_info in _SIO_TYPE_MAP:

                _api_spec_info = Bunch()
                _api_spec_info.name = api_spec_info.name
                _api_spec_info.param_list = Bunch()
                _api_spec_info.request_elem = getattr(sio, 'request_elem',
                                                      None)
                _api_spec_info.response_elem = getattr(sio, 'response_elem',
                                                       None)

                for param_list_name in _sio_attrs:
                    _param_list = []
                    param_list = getattr(sio, param_list_name, [])
                    param_list = param_list if isinstance(
                        param_list, (tuple, list)) else [param_list]

                    for param in param_list:

                        # Actual parameter name
                        param_name = param if isinstance(
                            param, basestring) else param.name

                        # To look up description based on parameter's name
                        desc_dict = sio_desc.input if param_list_name.startswith(
                            'input') else sio_desc.output

                        # Parameter details object
                        _param_info = Bunch()
                        _param_info.name = param_name
                        _param_info.is_required = 'required' in param_list_name
                        _param_info.description = desc_dict.get(
                            param_name
                        ) or ''  # Always use a string, even if an empty one

                        if isinstance(param, AsIs):
                            type_info = api_spec_info.DEFAULT

                        elif is_bool(param, param_name,
                                     self.simple_io_config.bool.prefix):
                            type_info = api_spec_info.BOOLEAN

                        elif is_int(param_name,
                                    self.simple_io_config.int.exact,
                                    self.simple_io_config.int.suffix):
                            type_info = api_spec_info.INTEGER

                        else:
                            try:
                                type_info = api_spec_info.map[param.__class__]
                            except KeyError:
                                type_info = api_spec_info.DEFAULT

                        _param_info.type, _param_info.subtype = type_info
                        _param_list.append(_param_info)

                    _api_spec_info.param_list[param_list_name] = _param_list

                self.simple_io[_api_spec_info.name] = SimpleIO(
                    _api_spec_info, sio_desc, self.needs_sio_desc).to_bunch()