Coverage for sources/mimeogram/parts.py: 100%

44 statements  

« prev     ^ index     » next       coverage.py v7.7.1, created at 2025-03-29 22:32 +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''' Mimeogram parts. ''' 

22 

23 

24from __future__ import annotations 

25 

26from . import __ 

27from . import fsprotect as _fsprotect 

28 

29 

30class LineSeparators( __.enum.Enum ): 

31 ''' Line separators for various platforms. ''' 

32 

33 CR = '\r' # Classic MacOS 

34 CRLF = '\r\n' # DOS/Windows 

35 LF = '\n' # Unix/Linux 

36 

37 @classmethod 

38 def detect_bytes( # pylint: disable=inconsistent-return-statements 

39 selfclass, content: bytes, limit = 1024 

40 ) -> LineSeparators | None: 

41 ''' Detects newline characters in bytes array. ''' 

42 sample = content[ : limit ] 

43 found_cr = False 

44 for byte in sample: 

45 match byte: 

46 case 0xd: 

47 if found_cr: return selfclass.CR 

48 found_cr = True 

49 case 0xa: # linefeed 

50 if found_cr: return selfclass.CRLF 

51 return selfclass.LF 

52 case _: 

53 if found_cr: return selfclass.CR 

54 return 

55 

56 @classmethod 

57 def normalize_universal( selfclass, content: str ) -> str: 

58 ''' Normalizes all varieties of newline characters in text. ''' 

59 return content.replace( '\r\n', '\r' ).replace( '\r', '\n' ) 

60 

61 def nativize( self, content: str ) -> str: 

62 ''' Nativizes specific variety newline characters in text. ''' 

63 if LineSeparators.LF is self: return content 

64 return content.replace( '\n', self.value ) 

65 

66 def normalize( self, content: str ) -> str: 

67 ''' Normalizes specific variety newline characters in text. ''' 

68 if LineSeparators.LF is self: return content 

69 return content.replace( self.value, '\n' ) 

70 

71 

72class Resolutions( __.enum.Enum ): 

73 ''' Available resolutions for each part. ''' 

74 

75 Apply = 'apply' 

76 Ignore = 'ignore' 

77 

78 

79class Part( 

80 metaclass = __.ImmutableStandardDataclass, 

81 decorators = ( __.standard_dataclass, ), 

82): 

83 ''' Part of mimeogram. ''' 

84 location: str # TODO? 'Url' class 

85 mimetype: str 

86 charset: str 

87 linesep: LineSeparators 

88 content: str 

89 

90 # TODO? 'format' method 

91 # TODO? 'parse' method 

92 

93 

94class Target( 

95 metaclass = __.ImmutableStandardDataclass, 

96 decorators = ( __.standard_dataclass, ), 

97): 

98 ''' Target information for mimeogram part. ''' 

99 part: Part 

100 destination: __.Path 

101 protection: _fsprotect.Status