-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #218 from ninoseki/foo
feat: improve RTF support
- Loading branch information
Showing
6 changed files
with
293 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import contextlib | ||
|
||
import rtfparse.entities | ||
|
||
from .rtfparser_patch import Control_Word | ||
|
||
|
||
@contextlib.contextmanager | ||
def with_monkey_patched_rtfparser(): | ||
original = rtfparse.entities.Control_Word | ||
|
||
try: | ||
rtfparse.entities.Control_Word = Control_Word | ||
yield | ||
finally: | ||
rtfparse.entities.Control_Word = original |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
import io | ||
|
||
# Own modules | ||
from rtfparse import re_patterns, utils | ||
from rtfparse.entities import CONTROL_WORD, INTEGER_MAGNITUDE, Entity, logger | ||
|
||
|
||
class Control_Word(Entity): # noqa: N801 | ||
def __init__(self, encoding: str, file: io.BufferedReader) -> None: | ||
super().__init__() | ||
self.encoding = encoding | ||
logger.debug(f"Reading Control Word at file position {file.tell()}") | ||
self.control_name = "missing" | ||
self.parameter = "" | ||
self.bindata = b"" | ||
self.start_position = file.tell() | ||
logger.debug(f"Starting at file position {self.start_position}") | ||
probe = file.read(CONTROL_WORD) | ||
if match := re_patterns.control_word.match(probe): | ||
self.control_name = match.group("control_name").decode(self.encoding) | ||
logger.debug(f"Preliminary {self.control_name = }") | ||
parameter = match.group("parameter") | ||
if parameter is not None: | ||
self.parameter = int(parameter.decode(self.encoding)) | ||
logger.debug(f"{self.parameter = }") | ||
self.control_name = self.control_name.removesuffix(str(self.parameter)) | ||
logger.debug(f"Final {self.control_name = }") | ||
target_position = self.start_position + match.span()[1] | ||
if match.group("other"): | ||
logger.debug( | ||
f"Delimiter is {match.group('other').decode(self.encoding)}, len: {len(match.group('delimiter'))}" | ||
) | ||
target_position -= len(match.group("delimiter")) | ||
file.seek(target_position) | ||
# handle \binN: | ||
if self.control_name == "bin": | ||
self.bindata = file.read( | ||
utils.twos_complement(self.parameter, INTEGER_MAGNITUDE) | ||
) | ||
else: | ||
logger.warning("Missing Control Word") | ||
# file.seek(target_position) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.