Coverage for sources/ictruck/recipes/logging.py: 100%
24 statements
« prev ^ index » next coverage.py v7.8.2, created at 2025-06-06 03:49 +0000
« prev ^ index » next coverage.py v7.8.2, created at 2025-06-06 03:49 +0000
1# vim: set filetype=python fileencoding=utf-8:
2# -*- coding: utf-8 -*-
4#============================================================================#
5# #
6# Licensed under the Apache License, Version 2.0 (the "License"); #
7# you may not use this file except in compliance with the License. #
8# You may obtain a copy of the License at #
9# #
10# http://www.apache.org/licenses/LICENSE-2.0 #
11# #
12# Unless required by applicable law or agreed to in writing, software #
13# distributed under the License is distributed on an "AS IS" BASIS, #
14# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. #
15# See the License for the specific language governing permissions and #
16# limitations under the License. #
17# #
18#============================================================================#
21''' Recipes for integration with standard logging. '''
24from __future__ import annotations
26import logging as _logging
28from . import __
31_validate_arguments = (
32 __.validate_arguments(
33 globalvars = globals( ),
34 errorclass = __.exceptions.ArgumentClassInvalidity ) )
37InstallAdditionalAliasesArgument: __.typx.TypeAlias = __.typx.Annotated[
38 __.Absential[ __.cabc.Mapping[ str, str ] ],
39 __.typx.Doc(
40 ''' Mapping of builtins attribute names to flavors. ''' ),
41]
44@_validate_arguments
45def install(
46 alias: __.InstallAliasArgument = __.builtins_alias_default,
47 additional_aliases: InstallAdditionalAliasesArgument = __.absent,
48) -> __.Truck:
49 ''' Produces truck and installs it into builtins with alias.
51 Replaces an existing truck, preserving global module configurations.
53 Library developers should call :py:func:`__.register_module` instead.
54 '''
55 truck = produce_truck( ).install( alias = alias )
56 if __.is_absent( additional_aliases ): additional_aliases = { }
57 for falias, flavor in additional_aliases.items( ):
58 __.install_builtin_safely(
59 falias, truck( flavor ), __.exceptions.AttributeNondisplacement )
60 return truck
63@_validate_arguments
64def produce_printer( mname: str, flavor: __.Flavor ) -> __.Printer:
65 ''' Produces printer which maps flavors to logging levels. '''
66 logger = _logging.getLogger( mname )
67 level = (
68 getattr( _logging, flavor.upper( ) ) if isinstance( flavor, str )
69 else _logging.DEBUG )
70 return lambda x: logger.log( level, x )
73@_validate_arguments
74def produce_truck( ) -> __.Truck:
75 ''' Produces icecream truck which integrates with standard logging. '''
76 active_flavors = { None: frozenset( {
77 'debug', 'info', 'warning', 'error', 'critical' } ) }
78 flavors: __.ImmutableDictionary[ __.Flavor, __.FlavorConfiguration ] = (
79 __.ImmutableDictionary(
80 { name: __.FlavorConfiguration( )
81 for name in active_flavors[ None ] } ) )
82 generalcfg = __.VehicleConfiguration( flavors = flavors )
83 nomargs = dict(
84 active_flavors = active_flavors,
85 generalcfg = generalcfg,
86 printer_factory = produce_printer )
87 return __.Truck( **nomargs ) # pyright: ignore