python-redlines 3.0.0__tar.gz → 3.1.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {python_redlines-3.0.0 → python_redlines-3.1.0}/PKG-INFO +10 -5
- {python_redlines-3.0.0 → python_redlines-3.1.0}/README.md +5 -0
- {python_redlines-3.0.0 → python_redlines-3.1.0}/src/python_redlines/__about__.py +1 -1
- {python_redlines-3.0.0 → python_redlines-3.1.0}/src/python_redlines/__init__.py +2 -0
- {python_redlines-3.0.0 → python_redlines-3.1.0}/src/python_redlines/engines.py +165 -1
- {python_redlines-3.0.0 → python_redlines-3.1.0}/.gitignore +0 -0
- {python_redlines-3.0.0 → python_redlines-3.1.0}/hatch_build.py +0 -0
- {python_redlines-3.0.0 → python_redlines-3.1.0}/pyproject.toml +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.5
|
|
2
2
|
Name: python-redlines
|
|
3
|
-
Version: 3.
|
|
3
|
+
Version: 3.1.0
|
|
4
4
|
Summary: Open-source DOCX comparison tool, powered by the Docxodus engine, to generate native Word tracked changes (redlines) in Python without MS Word dependencies.
|
|
5
5
|
Project-URL: Homepage, https://github.com/JSv4/Python-Redlines
|
|
6
6
|
Project-URL: Documentation, https://jsv4.github.io/Python-Redlines/
|
|
@@ -21,12 +21,12 @@ Classifier: Programming Language :: Python :: Implementation :: CPython
|
|
|
21
21
|
Requires-Python: >=3.11
|
|
22
22
|
Requires-Dist: platformdirs>=3.0
|
|
23
23
|
Provides-Extra: all
|
|
24
|
-
Requires-Dist: python-redlines-docxodus==3.
|
|
25
|
-
Requires-Dist: python-redlines-ooxmlpowertools==3.
|
|
24
|
+
Requires-Dist: python-redlines-docxodus==3.1.0; extra == 'all'
|
|
25
|
+
Requires-Dist: python-redlines-ooxmlpowertools==3.1.0; extra == 'all'
|
|
26
26
|
Provides-Extra: docxodus
|
|
27
|
-
Requires-Dist: python-redlines-docxodus==3.
|
|
27
|
+
Requires-Dist: python-redlines-docxodus==3.1.0; extra == 'docxodus'
|
|
28
28
|
Provides-Extra: ooxmlpowertools
|
|
29
|
-
Requires-Dist: python-redlines-ooxmlpowertools==3.
|
|
29
|
+
Requires-Dist: python-redlines-ooxmlpowertools==3.1.0; extra == 'ooxmlpowertools'
|
|
30
30
|
Description-Content-Type: text/markdown
|
|
31
31
|
|
|
32
32
|
# python-redlines
|
|
@@ -68,6 +68,11 @@ version as the wrapper. For older releases, pin both packages explicitly (for
|
|
|
68
68
|
example, `python-redlines[docxodus]==0.3.0` and
|
|
69
69
|
`python-redlines-docxodus==0.3.0`). Existing published metadata is unchanged.
|
|
70
70
|
|
|
71
|
+
3.1.0 keeps the 3.0.0 engine. It adds `PendingRevisionsWarning`, emitted when an
|
|
72
|
+
input document carries pending tracked changes that the comparison will accept, and
|
|
73
|
+
an engine now starts from a binary extracted ahead of time onto a read-only
|
|
74
|
+
filesystem. See [Upgrading to 3.1.0](https://github.com/JSv4/Python-Redlines#upgrading-to-310).
|
|
75
|
+
|
|
71
76
|
3.0.0 bundles Docxodus v17.0.0 and adds one setting, `minimum_anchor_word_count`.
|
|
72
77
|
Existing call signatures and settings are unchanged from 2.0.0, but comparison markup
|
|
73
78
|
and revision counts can differ, and some very deeply nested documents are now refused.
|
|
@@ -37,6 +37,11 @@ version as the wrapper. For older releases, pin both packages explicitly (for
|
|
|
37
37
|
example, `python-redlines[docxodus]==0.3.0` and
|
|
38
38
|
`python-redlines-docxodus==0.3.0`). Existing published metadata is unchanged.
|
|
39
39
|
|
|
40
|
+
3.1.0 keeps the 3.0.0 engine. It adds `PendingRevisionsWarning`, emitted when an
|
|
41
|
+
input document carries pending tracked changes that the comparison will accept, and
|
|
42
|
+
an engine now starts from a binary extracted ahead of time onto a read-only
|
|
43
|
+
filesystem. See [Upgrading to 3.1.0](https://github.com/JSv4/Python-Redlines#upgrading-to-310).
|
|
44
|
+
|
|
40
45
|
3.0.0 bundles Docxodus v17.0.0 and adds one setting, `minimum_anchor_word_count`.
|
|
41
46
|
Existing call signatures and settings are unchanged from 2.0.0, but comparison markup
|
|
42
47
|
and revision counts can differ, and some very deeply nested documents are now refused.
|
|
@@ -8,6 +8,7 @@ from .engines import (
|
|
|
8
8
|
DocxodusEngine,
|
|
9
9
|
EngineExecutionError,
|
|
10
10
|
EngineNotInstalledError,
|
|
11
|
+
PendingRevisionsWarning,
|
|
11
12
|
XmlPowerToolsEngine,
|
|
12
13
|
)
|
|
13
14
|
|
|
@@ -17,5 +18,6 @@ __all__ = [
|
|
|
17
18
|
"DocxodusEngine",
|
|
18
19
|
"EngineExecutionError",
|
|
19
20
|
"EngineNotInstalledError",
|
|
21
|
+
"PendingRevisionsWarning",
|
|
20
22
|
"__version__",
|
|
21
23
|
]
|
|
@@ -1,15 +1,19 @@
|
|
|
1
|
+
import filecmp
|
|
1
2
|
import importlib.metadata
|
|
2
3
|
import importlib.resources
|
|
3
4
|
import logging
|
|
4
5
|
import os
|
|
5
6
|
import platform
|
|
7
|
+
import posixpath
|
|
6
8
|
import subprocess
|
|
7
9
|
import tarfile
|
|
8
10
|
import tempfile
|
|
9
11
|
import warnings
|
|
10
12
|
import zipfile
|
|
13
|
+
import zlib
|
|
11
14
|
from pathlib import Path
|
|
12
15
|
from typing import Optional, Tuple, Union
|
|
16
|
+
from xml.parsers import expat
|
|
13
17
|
|
|
14
18
|
import platformdirs
|
|
15
19
|
|
|
@@ -22,6 +26,129 @@ class EngineNotInstalledError(ImportError):
|
|
|
22
26
|
"""Raised when an engine is used but its binary package is not installed."""
|
|
23
27
|
|
|
24
28
|
|
|
29
|
+
class PendingRevisionsWarning(UserWarning):
|
|
30
|
+
"""An input document carries pending tracked changes that the comparison will accept.
|
|
31
|
+
|
|
32
|
+
Both engines compare the accepted view of each document, so the redline does not carry
|
|
33
|
+
those changes over as tracked changes by their original authors. Silence it with ``warnings.filterwarnings("ignore", category=PendingRevisionsWarning)``.
|
|
34
|
+
"""
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
_W_NAMESPACES = (
|
|
38
|
+
'http://schemas.openxmlformats.org/wordprocessingml/2006/main',
|
|
39
|
+
'http://purl.oclc.org/ooxml/wordprocessingml/main', # Strict
|
|
40
|
+
)
|
|
41
|
+
|
|
42
|
+
# Every element that records a pending tracked change: insertions, deletions, moves,
|
|
43
|
+
# formatting changes, and table-structure changes. Tags are written as expat reports them
|
|
44
|
+
# with namespace_separator='}': 'namespace}local'.
|
|
45
|
+
_REVISION_TAGS = frozenset(
|
|
46
|
+
f'{namespace}}}{name}'
|
|
47
|
+
for namespace in _W_NAMESPACES
|
|
48
|
+
for name in (
|
|
49
|
+
'ins', 'del', 'moveFrom', 'moveTo',
|
|
50
|
+
'rPrChange', 'pPrChange', 'sectPrChange', 'numberingChange',
|
|
51
|
+
'tblPrChange', 'tblPrExChange', 'tblGridChange', 'trPrChange', 'tcPrChange',
|
|
52
|
+
'cellIns', 'cellDel', 'cellMerge',
|
|
53
|
+
'customXmlInsRangeStart', 'customXmlDelRangeStart',
|
|
54
|
+
'customXmlMoveFromRangeStart', 'customXmlMoveToRangeStart',
|
|
55
|
+
)
|
|
56
|
+
)
|
|
57
|
+
|
|
58
|
+
_RELATIONSHIP_TAG = 'http://schemas.openxmlformats.org/package/2006/relationships}Relationship'
|
|
59
|
+
|
|
60
|
+
# Relationship types, by their last segment (transitional and Strict share it), of the
|
|
61
|
+
# parts related to the main document whose revisions Docxodus accepts before comparing.
|
|
62
|
+
# It leaves the comments and glossary parts' own revisions in place.
|
|
63
|
+
_ACCEPTED_STORY_TYPES = ('header', 'footer', 'footnotes', 'endnotes', 'styles')
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
# What reading or parsing one part of a damaged package can raise: a missing part
|
|
67
|
+
# (KeyError), a DTD (ValueError, below), a bad CRC or truncated data, an unsupported or
|
|
68
|
+
# encrypted member, malformed XML, or an encoding Python does not know (LookupError).
|
|
69
|
+
# Each is confined to its part, so one bad part does not hide pending changes in the others.
|
|
70
|
+
_UNREADABLE_PART = (KeyError, ValueError, LookupError, EOFError, RuntimeError, NotImplementedError,
|
|
71
|
+
zlib.error, zipfile.BadZipFile, expat.ExpatError)
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
class _StopParsing(Exception):
|
|
75
|
+
"""Raised from a parser callback once a part has answered the question."""
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def _parse_part(package: zipfile.ZipFile, name: str, on_start) -> None:
|
|
79
|
+
"""Stream one XML part through expat, calling ``on_start(tag, attributes)`` at each
|
|
80
|
+
element start, with *tag* written ``namespace}local``.
|
|
81
|
+
|
|
82
|
+
Raises ValueError for a part with a DTD, in whatever encoding: a .docx may not
|
|
83
|
+
contain one, and expanding its entities is how an entity bomb works.
|
|
84
|
+
"""
|
|
85
|
+
def refuse_dtd(*_):
|
|
86
|
+
raise ValueError(f'{name} contains a DTD')
|
|
87
|
+
|
|
88
|
+
parser = expat.ParserCreate(namespace_separator='}')
|
|
89
|
+
parser.StartDoctypeDeclHandler = refuse_dtd
|
|
90
|
+
parser.StartElementHandler = on_start
|
|
91
|
+
with package.open(name) as stream:
|
|
92
|
+
parser.ParseFile(stream)
|
|
93
|
+
|
|
94
|
+
|
|
95
|
+
def _related_parts(package: zipfile.ZipFile, source: str, types) -> list:
|
|
96
|
+
"""Names of the parts *source* ('' for the package itself) relates with one of *types*."""
|
|
97
|
+
folder, name = posixpath.split(source)
|
|
98
|
+
parts = []
|
|
99
|
+
|
|
100
|
+
def collect(tag, attributes):
|
|
101
|
+
if (tag == _RELATIONSHIP_TAG
|
|
102
|
+
and attributes.get('Type', '').rsplit('/', 1)[-1] in types
|
|
103
|
+
and attributes.get('TargetMode') != 'External'):
|
|
104
|
+
target = attributes.get('Target', '')
|
|
105
|
+
parts.append(target.lstrip('/') if target.startswith('/')
|
|
106
|
+
else posixpath.normpath(posixpath.join(folder, target)))
|
|
107
|
+
|
|
108
|
+
try:
|
|
109
|
+
_parse_part(package, posixpath.join(folder, '_rels', f'{name}.rels'), collect)
|
|
110
|
+
except _UNREADABLE_PART:
|
|
111
|
+
# No relationships part, or a damaged one: keep whatever was read before it failed.
|
|
112
|
+
logger.debug("Could not read the relationships of %r", source, exc_info=True)
|
|
113
|
+
return parts
|
|
114
|
+
|
|
115
|
+
|
|
116
|
+
def _has_pending_revision(package: zipfile.ZipFile, name: str) -> bool:
|
|
117
|
+
def stop_at_revision(tag, _attributes):
|
|
118
|
+
if tag in _REVISION_TAGS:
|
|
119
|
+
raise _StopParsing
|
|
120
|
+
|
|
121
|
+
try:
|
|
122
|
+
_parse_part(package, name, stop_at_revision)
|
|
123
|
+
except _StopParsing:
|
|
124
|
+
return True
|
|
125
|
+
except _UNREADABLE_PART:
|
|
126
|
+
# A part the package names but cannot supply or parse: skip it, and let the
|
|
127
|
+
# engine report it if it matters.
|
|
128
|
+
logger.debug("Could not check part %s for pending tracked changes", name, exc_info=True)
|
|
129
|
+
return False
|
|
130
|
+
|
|
131
|
+
|
|
132
|
+
def _pending_revision_parts(path) -> list:
|
|
133
|
+
"""The parts of the .docx at *path* that hold pending tracked changes the engines
|
|
134
|
+
accept before comparing: the main document and its headers, footers, footnotes,
|
|
135
|
+
endnotes and styles.
|
|
136
|
+
|
|
137
|
+
Returns an empty list for anything that is not a readable .docx; the engine then
|
|
138
|
+
reports the problem itself.
|
|
139
|
+
"""
|
|
140
|
+
try:
|
|
141
|
+
with zipfile.ZipFile(path) as package:
|
|
142
|
+
main_parts = _related_parts(package, '', ('officeDocument',))
|
|
143
|
+
stories = [story for main in main_parts
|
|
144
|
+
for story in _related_parts(package, main, _ACCEPTED_STORY_TYPES)]
|
|
145
|
+
return [name for name in dict.fromkeys(main_parts + stories)
|
|
146
|
+
if _has_pending_revision(package, name)]
|
|
147
|
+
except (OSError, ValueError, zipfile.BadZipFile):
|
|
148
|
+
logger.debug("Could not open %s to check it for pending tracked changes", path, exc_info=True)
|
|
149
|
+
return []
|
|
150
|
+
|
|
151
|
+
|
|
25
152
|
class EngineExecutionError(subprocess.CalledProcessError):
|
|
26
153
|
"""An engine exited unsuccessfully; its diagnostics appear in the message.
|
|
27
154
|
|
|
@@ -70,6 +197,10 @@ class BaseEngine(object):
|
|
|
70
197
|
BINARY_BASE_NAME: str = NotImplemented
|
|
71
198
|
EXTRA_NAME: str = NotImplemented
|
|
72
199
|
|
|
200
|
+
# Whether the engine hands byte-identical inputs back untouched, pending tracked
|
|
201
|
+
# changes included, instead of comparing them.
|
|
202
|
+
_RETURNS_IDENTICAL_INPUTS_UNCHANGED = False
|
|
203
|
+
|
|
73
204
|
def __init__(self, target_path: Optional[str] = None):
|
|
74
205
|
self.target_path = target_path
|
|
75
206
|
self.extracted_binaries_path = self._resolve_binary()
|
|
@@ -105,7 +236,9 @@ class BaseEngine(object):
|
|
|
105
236
|
if not binary_path.exists():
|
|
106
237
|
self._extract_archive(archive, extract_root)
|
|
107
238
|
|
|
108
|
-
|
|
239
|
+
# chmod fails on a read-only filesystem and for a user that does not own the file,
|
|
240
|
+
# so only attempt it when the binary is not already executable.
|
|
241
|
+
if not is_windows and not os.access(binary_path, os.X_OK):
|
|
109
242
|
os.chmod(binary_path, 0o755)
|
|
110
243
|
|
|
111
244
|
return str(binary_path)
|
|
@@ -160,6 +293,10 @@ class BaseEngine(object):
|
|
|
160
293
|
A nonzero engine exit raises EngineExecutionError (a CalledProcessError
|
|
161
294
|
subclass) with captured stderr and stdout included in its message.
|
|
162
295
|
|
|
296
|
+
If either document carries pending tracked changes, a PendingRevisionsWarning
|
|
297
|
+
names it: the engine accepts those changes before comparing, so the redline
|
|
298
|
+
does not keep them as tracked changes by their original authors.
|
|
299
|
+
|
|
163
300
|
Additional keyword arguments are passed to _build_command() for engine-specific
|
|
164
301
|
options. DocxodusEngine supports: case_insensitive, detect_moves,
|
|
165
302
|
move_similarity_threshold, move_minimum_word_count, minimum_anchor_word_count,
|
|
@@ -180,6 +317,7 @@ class BaseEngine(object):
|
|
|
180
317
|
modified_path = self._as_path(modified, scratch_files)
|
|
181
318
|
|
|
182
319
|
command = self._build_command(author_tag, original_path, modified_path, target_path, **kwargs)
|
|
320
|
+
self._warn_about_pending_revisions(original_path, modified_path)
|
|
183
321
|
|
|
184
322
|
# Capture stdout and stderr
|
|
185
323
|
try:
|
|
@@ -199,6 +337,27 @@ class BaseEngine(object):
|
|
|
199
337
|
finally:
|
|
200
338
|
self._cleanup_temp_files(scratch_files)
|
|
201
339
|
|
|
340
|
+
def _warn_about_pending_revisions(self, original_path, modified_path):
|
|
341
|
+
"""Tell the caller that the comparison will accept the inputs' pending tracked
|
|
342
|
+
changes (issue #41). Called from run_redline, so stacklevel 3 is the caller."""
|
|
343
|
+
found = [(side, parts)
|
|
344
|
+
for side, path in (('original', original_path), ('modified', modified_path))
|
|
345
|
+
if (parts := _pending_revision_parts(path))]
|
|
346
|
+
if not found:
|
|
347
|
+
return
|
|
348
|
+
if self._RETURNS_IDENTICAL_INPUTS_UNCHANGED and filecmp.cmp(original_path, modified_path, shallow=False):
|
|
349
|
+
return
|
|
350
|
+
|
|
351
|
+
where = ' and the '.join(f"{side} document ({', '.join(parts)})" for side, parts in found)
|
|
352
|
+
warnings.warn(
|
|
353
|
+
f"Pending tracked changes found in the {where}. {type(self).__name__} accepts every "
|
|
354
|
+
"pending change in both documents before comparing them, so the redline does not "
|
|
355
|
+
"carry them over as tracked changes by their original authors. "
|
|
356
|
+
"Accept or reject them before comparing if that history matters.",
|
|
357
|
+
PendingRevisionsWarning,
|
|
358
|
+
stacklevel=3,
|
|
359
|
+
)
|
|
360
|
+
|
|
202
361
|
def _as_path(self, document, scratch_files):
|
|
203
362
|
"""Return a filesystem path for *document*, writing bytes out if needed.
|
|
204
363
|
|
|
@@ -251,6 +410,11 @@ class DocxodusEngine(BaseEngine):
|
|
|
251
410
|
BINARY_BASE_NAME = 'redline'
|
|
252
411
|
EXTRA_NAME = 'docxodus'
|
|
253
412
|
|
|
413
|
+
# Docxodus skips the comparison for byte-identical inputs and returns the document
|
|
414
|
+
# as it came, pending changes and all (JSv4/Docxodus#1894), so there is nothing to
|
|
415
|
+
# warn about. tests/test_pending_revisions.py pins this against the bundled binary.
|
|
416
|
+
_RETURNS_IDENTICAL_INPUTS_UNCHANGED = True
|
|
417
|
+
|
|
254
418
|
# Settings that died with WmlComparer in Docxodus v11.0.0. They are rejected
|
|
255
419
|
# rather than dropped, because dropping them would not be a breaking change
|
|
256
420
|
# but a wrong-answer one: the v12 CLI rejects --engine as an unknown flag,
|
|
File without changes
|
|
File without changes
|
|
File without changes
|