:mod:`magpie.utils` =================== .. py:module:: magpie.utils Module Contents --------------- Classes ~~~~~~~ .. autoapisummary:: magpie.utils.ExtendedEnum magpie.utils.SingletonMeta Functions ~~~~~~~~~ .. autoapisummary:: magpie.utils.get_logger magpie.utils.set_logger_config magpie.utils.print_log magpie.utils.raise_log magpie.utils.bool2str magpie.utils.islambda magpie.utils.isclass magpie.utils.make_dirs magpie.utils.get_settings_from_config_ini magpie.utils.get_json magpie.utils.get_header magpie.utils.convert_response magpie.utils.get_admin_cookies magpie.utils.get_settings magpie.utils.patch_magpie_url magpie.utils.get_magpie_url magpie.utils.get_phoenix_url magpie.utils.get_twitcher_protected_service_url magpie.utils.is_magpie_ui_path magpie.utils.fully_qualified_name magpie.utils.log_request_format magpie.utils.log_request magpie.utils.log_exception_tween magpie.utils.is_json_body .. data:: CONTENT_TYPE_ANY :annotation: = */* .. data:: CONTENT_TYPE_JSON :annotation: = application/json .. data:: CONTENT_TYPE_FORM :annotation: = application/x-www-form-urlencoded .. data:: CONTENT_TYPE_HTML :annotation: = text/html .. data:: CONTENT_TYPE_PLAIN :annotation: = text/plain .. data:: CONTENT_TYPE_APP_XML :annotation: = application/xml .. data:: CONTENT_TYPE_TXT_XML :annotation: = text/xml .. data:: FORMAT_TYPE_MAPPING .. data:: SUPPORTED_ACCEPT_TYPES .. data:: SUPPORTED_FORMAT_TYPES .. data:: KNOWN_CONTENT_TYPES .. function:: get_logger(name: magpie.typedefs.Str, level: Optional[int] = None, force_stdout: bool = None, message_format: Optional[Str] = None, datetime_format: Optional[Str] = None) -> logging.Logger Immediately sets the logger level to avoid duplicate log outputs from the `root logger` and `this logger` when `level` is ``logging.NOTSET``. .. data:: LOGGER .. function:: set_logger_config(logger: logging.Logger, force_stdout: bool = False, message_format: Optional[Str] = None, datetime_format: Optional[Str] = None) -> logging.Logger Applies the provided logging configuration settings to the logger. .. function:: print_log(msg: magpie.typedefs.Str, logger: Optional[logging.Logger] = None, level: int = logging.INFO, **kwargs: Any) -> None Logs the requested message to the logger and optionally enforce printing to the console according to configuration value defined by ``MAGPIE_LOG_PRINT``. .. function:: raise_log(msg: magpie.typedefs.Str, exception: Type[Exception] = Exception, logger: Optional[logging.Logger] = None, level: int = logging.ERROR) -> NoReturn Logs the provided message to the logger and raises the corresponding exception afterwards. :raises exception: whichever exception provided is raised systematically after logging. .. function:: bool2str(value: Any) -> magpie.typedefs.Str Converts :paramref:`value` to explicit ``"true"`` or ``"false"`` :class:`str` with permissive variants comparison that can represent common falsy or truthy values. .. function:: islambda(func: Any) -> bool Evaluate if argument is a callable :class:`lambda` expression. .. function:: isclass(obj: Any) -> bool Evaluate an object for :class:`class` type (ie: class definition, not an instance nor any other type). .. function:: make_dirs(path) .. function:: get_settings_from_config_ini(config_ini_path, ini_main_section_name='app:magpie_app') .. function:: get_json(response) Retrieves the 'JSON' body of a response using the property/callable according to the response's implementation. .. function:: get_header(header_name: magpie.typedefs.Str, header_container: magpie.typedefs.AnyHeadersType, default: Optional[Str] = None, split: Optional[Union[Str, List[Str]]] = None) -> Optional[Str] Retrieves ``header_name`` by fuzzy match (independently of upper/lower-case and underscore/dash) from various framework implementations of ``Headers``. If ``split`` is specified, the matched ``header_name`` is first split with it and the first item is returned. This allows to parse complex headers (e.g.: ``text/plain; charset=UTF-8`` to ``text/plain`` with ``split=';'``). :param header_name: header to find. :param header_container: where to look for `header_name`. :param default: value to returned if `header_container` is invalid or `header_name` could not be found. :param split: character(s) to use to split the *found* `header_name`. .. function:: convert_response(response: magpie.typedefs.AnyResponseType) -> pyramid.response.Response Converts a :class:`requests.Response` object to an equivalent :class:`pyramid.response.Response` object. Content of the :paramref:`response` is expected to be JSON. :param response: response to be converted :returns: converted response .. function:: get_admin_cookies(container: magpie.typedefs.AnySettingsContainer, verify: bool = True, raise_message: Optional[Str] = None) -> magpie.typedefs.CookiesType .. function:: get_settings(container: magpie.typedefs.AnySettingsContainer) -> magpie.typedefs.SettingsType .. function:: patch_magpie_url(container: magpie.typedefs.AnySettingsContainer) -> magpie.typedefs.SettingsType Updates potentially missing configuration settings for normal application execution. .. function:: get_magpie_url(container: Optional[AnySettingsContainer] = None) -> magpie.typedefs.Str .. function:: get_phoenix_url(container: Optional[AnySettingsContainer] = None) -> magpie.typedefs.Str .. function:: get_twitcher_protected_service_url(magpie_service_name, hostname=None) .. function:: is_magpie_ui_path(request: pyramid.request.Request) -> bool Determines if the request path corresponds to any Magpie UI location. .. function:: fully_qualified_name(obj: Union[(Any, Type[Any])]) -> str Obtains the ``'.'`` full path definition of the object to allow finding and importing it. .. function:: log_request_format(request: pyramid.request.Request) -> magpie.typedefs.Str .. function:: log_request(event: pyramid.events.NewRequest) -> None Subscriber event that logs basic details about the incoming requests. .. function:: log_exception_tween(handler, registry) Tween factory that logs any exception before re-raising it. Application errors are marked as ``ERROR`` while non critical HTTP errors are marked as ``WARNING``. .. function:: is_json_body(body: Any) -> bool .. class:: ExtendedEnum Bases: :class:`enum.Enum` Utility :class:`enum.Enum` methods. Create an extended enum with these utilities as follows:: class CustomEnum(ExtendedEnum): ItemA = "A" ItemB = "B" .. method:: names(cls) -> List[Str] :classmethod: Returns the member names assigned to corresponding enum elements. .. method:: values(cls) -> List[AnyKey] :classmethod: Returns the literal values assigned to corresponding enum elements. .. method:: get(cls: magpie.typedefs.AnyKey, key_or_value: Optional[Any], default=None) -> Optional[_TC] :classmethod: Finds an enum entry by defined name or its value. Returns the entry directly if it is already a valid enum. .. class:: SingletonMeta Bases: :class:`type` A metaclass that creates a Singleton base class when called. Create a class such that:: @six.add_metaclass(SingletonMeta) class A(object): pass @six.add_metaclass(SingletonMeta) class B(object): pass a1 = A() a2 = A() b1 = B() b2 = B() a1 is a2 # True b1 is b2 # True a1 is b1 # False .. attribute:: _instances .. method:: __call__(cls, *args, **kwargs) Call self as a function.