Coverage for sources/classcore/__/nomina.py: 100%

32 statements  

« prev     ^ index     » next       coverage.py v7.16.2, created at 2026-10-15 18:04 +0000

1# vim: set filetype=python fileencoding=utf-8: 

2# -*- coding: utf-8 -*- 

3 

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#============================================================================# 

19 

20 

21''' Common names and type aliases. ''' 

22 

23 

24from . import imports as __ 

25 

26 

27ComparisonResult: __.typx.TypeAlias = bool | __.types.NotImplementedType 

28NominativeArguments: __.typx.TypeAlias = __.cabc.Mapping[ str, __.typx.Any ] 

29PositionalArguments: __.typx.TypeAlias = __.cabc.Sequence[ __.typx.Any ] 

30 

31 

32package_name = __name__.split( '.', maxsplit = 1 )[ 0 ] 

33 

34 

35_levels = frozenset( ( 'class', 'classes', 'instance', 'instances' ) ) 

36_cores = frozenset( ( 

37 'behaviors', 

38 'construction_arguments', 

39 'dynadoc_configuration', 

40 'in_progress', 

41 'mutables_names', 'mutables_predicates', 'mutables_regexes', 

42 'visibles_names', 'visibles_predicates', 'visibles_regexes', 

43 'assigner_core', 'deleter_core', 'surveyor_core' ) ) 

44_hex_digits = frozenset( '0123456789abcdef' ) 

45_digest_length = 64 

46_mangled_cores = frozenset( ( 'class_behaviors', 'class_in_progress' ) ) 

47 

48 

49class AttrnameCalculator: 

50 ''' Calculates framework attribute names from level and core. 

51 

52 Reference implementation of the attribute namer convention: 

53 call instances to produce names, and consult ``is_internal_name`` 

54 to detect names this framework generated. Downstream packages 

55 may subclass (or otherwise provide callables exposing the same 

56 optional detector) to contribute their own internal names. 

57 ''' 

58 

59 def is_internal_name( self, name: str ) -> bool: 

60 prefix = f"_{package_name}_" 

61 if not name.startswith( prefix ): 

62 return False 

63 body = name[ len( prefix ): ] 

64 # Static stems: _{package}_{level}_{core}_, trailing underscore. 

65 # The class-level behaviors and in_progress stems are stored 

66 # mangled only, so they are excluded from static acceptance. 

67 if body.endswith( '_' ): 

68 level, _, core = body[ :-1 ].partition( '_' ) 

69 if level in _levels and core in _cores: 

70 return not ( 

71 'class' == level 

72 and f"{level}_{core}" in _mangled_cores ) 

73 return False 

74 # Mangled forms: one of the mangled cores + 64 lowercase hex, 

75 # no trailing underscore. The mangling accessors append digests 

76 # to these stems only. 

77 for mangled_core in _mangled_cores: 

78 mangled_prefix = f"{mangled_core}_" 

79 if body.startswith( mangled_prefix ): 

80 suffix = body[ len( mangled_prefix ): ] 

81 return ( 

82 _digest_length == len( suffix ) 

83 and all( c in _hex_digits for c in suffix ) ) 

84 return False 

85 

86 def __call__( self, level: str, core: str ) -> str: 

87 return f"_{package_name}_{level}_{core}_" 

88 

89 

90calculate_attrname = AttrnameCalculator( ) 

91 

92 

93def calculate_contribution_name( ) -> str: 

94 ''' Returns metaclass attribute name holding internal-name detectors. ''' 

95 return f"_{package_name}_internal_names_"