Coverage for sources/ictruck/recipes/logging.py: 100%
25 statements
« prev ^ index » next coverage.py v7.7.1, created at 2025-03-21 22:29 +0000
« prev ^ index » next coverage.py v7.7.1, created at 2025-03-21 22:29 +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 ''' Installs configured truck into builtins.
51 Application developers should call this early before importing
52 library packages which may also use the builtin truck.
53 '''
54 truck = produce_truck( )
55 __.install_builtin_safely(
56 alias, truck, __.exceptions.AttributeNondisplacement )
57 if __.is_absent( additional_aliases ): additional_aliases = { }
58 for falias, flavor in additional_aliases.items( ):
59 __.install_builtin_safely(
60 falias, truck( flavor ), __.exceptions.AttributeNondisplacement )
61 return truck
64@_validate_arguments
65def produce_printer( mname: str, flavor: __.Flavor ) -> __.Printer:
66 ''' Produces printer which maps flavors to logging levels. '''
67 logger = _logging.getLogger( mname )
68 level = (
69 getattr( _logging, flavor.upper( ) ) if isinstance( flavor, str )
70 else _logging.DEBUG )
71 return lambda x: logger.log( level, x )
74@_validate_arguments
75def produce_truck( ) -> __.Truck:
76 ''' Produces icecream truck which integrates with standard logging. '''
77 active_flavors = { None: frozenset( {
78 'debug', 'info', 'warning', 'error', 'critical' } ) }
79 flavors: __.ImmutableDictionary[ __.Flavor, __.FlavorConfiguration ] = (
80 __.ImmutableDictionary(
81 { name: __.FlavorConfiguration( )
82 for name in active_flavors[ None ] } ) )
83 generalcfg = __.VehicleConfiguration( flavors = flavors )
84 nomargs = dict(
85 active_flavors = active_flavors,
86 generalcfg = generalcfg,
87 printer_factory = produce_printer )
88 return __.Truck( **nomargs ) # pyright: ignore