33
44import io
55import logging
6- import re
76
87# Own modules
98from rtfparse import re_patterns , utils
1918GROUP_START = BACKSLASH + IGNORABLE
2019MAX_CW_LETTERS = 32 # As specified in RTF Spec
2120INTEGER_MAGNITUDE = 32 # As specified in RTF Spec
22- PLAIN_TEXT = CONTROL_WORD = (
23- BACKSLASH + MAX_CW_LETTERS + MINUS + len (str ((1 << INTEGER_MAGNITUDE ) // 2 )) + DELIMITER
24- )
21+ PLAIN_TEXT = CONTROL_WORD = BACKSLASH + MAX_CW_LETTERS + MINUS + len (str ((1 << INTEGER_MAGNITUDE ) // 2 )) + DELIMITER
2522
2623
2724class Entity :
@@ -37,23 +34,23 @@ def probe(cls, pattern: re_patterns.Bytes_Regex, file: io.BufferedReader) -> Byt
3734 logger .debug (f"{ probed = } " )
3835 file .seek (original_position )
3936 logger .debug (f"Probe returned to position { file .tell ()} " )
40- if match := re_patterns .group_start .match (probed ):
37+ if re_patterns .group_start .match (probed ):
4138 result = Bytestring_Type .GROUP_START
42- elif match := re_patterns .group_end .match (probed ):
39+ elif re_patterns .group_end .match (probed ):
4340 result = Bytestring_Type .GROUP_END
44- elif match := re_patterns .control_word .match (probed ):
41+ elif re_patterns .control_word .match (probed ):
4542 result = Bytestring_Type .CONTROL_WORD
46- elif match := re_patterns .control_symbol .match (probed ):
43+ elif re_patterns .control_symbol .match (probed ):
4744 result = Bytestring_Type .CONTROL_SYMBOL
48- elif match := re_patterns .plain_text .match (probed ):
45+ elif re_patterns .plain_text .match (probed ):
4946 result = Bytestring_Type .PLAIN_TEXT
5047 else :
51- logger .debug (f "This does not match anything, it's probably a newline, moving on" )
48+ logger .debug ("This does not match anything, it's probably a newline, moving on" )
5249 original_position += 1
5350 file .seek (original_position )
5451 logger .debug (f"Probe moved to position { file .tell ()} " )
5552 if not probed :
56- logger .debug (f "Reached unexpected end of file." )
53+ logger .debug ("Reached unexpected end of file." )
5754 result = Bytestring_Type .GROUP_END
5855 break
5956 continue
@@ -85,16 +82,14 @@ def __init__(self, encoding: str, file: io.BufferedReader) -> None:
8582 logger .debug (f"Final { self .control_name = } " )
8683 target_position = self .start_position + match .span ()[1 ]
8784 if match .group ("other" ):
88- logger .debug (
89- f"Delimiter is { match .group ('other' ).decode (self .encoding )} , len: { len (match .group ('delimiter' ))} "
90- )
85+ logger .debug (f"Delimiter is { match .group ('other' ).decode (self .encoding )} , len: { len (match .group ('delimiter' ))} " )
9186 target_position -= len (match .group ("delimiter" ))
9287 file .seek (target_position )
9388 # handle \binN:
9489 if self .control_name == "bin" :
9590 self .bindata = file .read (utils .twos_complement (self .parameter , INTEGER_MAGNITUDE ))
9691 else :
97- logger .warning (f "Missing Control Word" )
92+ logger .warning ("Missing Control Word" )
9893 file .seek (self .start_position )
9994
10095 def __repr__ (self ) -> str :
@@ -112,9 +107,7 @@ def __init__(self, encoding: str, file: io.BufferedReader) -> None:
112107 if self .text == "'" :
113108 self .char = file .read (SYMBOL ).decode (self .encoding )
114109 self .text = bytes ((int (self .char , base = 16 ),)).decode (self .encoding )
115- logger .debug (
116- f"Encountered escaped ANSI character, read two more bytes: { self .char } , character: { self .text } "
117- )
110+ logger .debug (f"Encountered escaped ANSI character, read two more bytes: { self .char } , character: { self .text } " )
118111 if self .text in "\\ {}" :
119112 file .seek (file .tell () - SYMBOL )
120113
@@ -127,16 +120,14 @@ def __init__(self, encoding: str, file: io.BufferedReader) -> None:
127120 super ().__init__ ()
128121 self .encoding = encoding
129122 self .text = ""
130- logger .debug (f "Constructing Plain_Text" )
123+ logger .debug ("Constructing Plain_Text" )
131124 while True :
132125 self .start_position = file .tell ()
133126 read = file .read (PLAIN_TEXT )
134- logger .debug (
135- f"Read file from { self .start_position } to position { file .tell ()} , read: { read } "
136- )
127+ logger .debug (f"Read file from { self .start_position } to position { file .tell ()} , read: { read } " )
137128 # see if we have read all the plain text there is:
138129 if match := re_patterns .plain_text .match (read ):
139- logger .debug (f "This matches the plain text pattern" )
130+ logger .debug ("This matches the plain text pattern" )
140131 _text = match .group ("text" ).decode (self .encoding )
141132 logger .debug (f"{ _text = } " )
142133 self .text = "" .join ((self .text , _text ))
@@ -158,7 +149,7 @@ def __repr__(self) -> str:
158149class Group (Entity ):
159150 def __init__ (self , encoding : str , file : io .BufferedReader ) -> None :
160151 super ().__init__ ()
161- logger .debug (f "Group.__init__" )
152+ logger .debug ("Group.__init__" )
162153 self .encoding = encoding
163154 self .known = False
164155 self .name = "unknown"
@@ -177,9 +168,7 @@ def __init__(self, encoding: str, file: io.BufferedReader) -> None:
177168 file .seek (self .start_position + GROUP_START - IGNORABLE )
178169 logger .debug (f"Returned to position { file .tell ()} " )
179170 else :
180- logger .warning (
181- utils .warn (f"Expected a group but found no group start. Creating unknown group" )
182- )
171+ logger .warning (utils .warn ("Expected a group but found no group start. Creating unknown group" ))
183172 file .seek (self .start_position )
184173 while True :
185174 probed = self .probe (re_patterns .probe , file )
0 commit comments