Skip to content

Commit 3c833e4

Browse files
committed
Updated unzip function to not overwrite existing binaries unless a flag is passed to the constructor. Also updated the docs to fix a typo with quickstart.
1 parent af22191 commit 3c833e4

File tree

2 files changed

+28
-12
lines changed

2 files changed

+28
-12
lines changed

docs/quickstart.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ with open('/path/to/original.docx', 'rb') as f:
2727
with open('/path/to/modified.docx', 'rb') as f:
2828
modified_bytes = f.read()
2929

30+
# This is a tuple, bytes @ element 0
3031
output = wrapper.run_redlines('AuthorTag', original_bytes, modified_bytes)
3132
```
3233

@@ -39,5 +40,5 @@ Process or save the output as needed. For example, to save the redline output to
3940

4041
```python
4142
with open('/path/to/redline_output.docx', 'wb') as f:
42-
f.write(output)
43+
f.write(output[0])
4344
```

src/python_redlines/engines.py

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,9 @@
1414

1515

1616
class XmlPowerToolsEngine(object):
17-
def __init__(self):
17+
def __init__(self, reload_binary: bool = False):
1818
self.extracted_binaries_path = self._unzip_binary()
19+
self._reload_binary = reload_binary
1920

2021
def _unzip_binary(self):
2122
"""
@@ -27,39 +28,53 @@ def _unzip_binary(self):
2728
binaries_path = os.path.join(base_path, 'dist')
2829
logger.debug(f"Python redlining binaries path: {binaries_path}")
2930

31+
os_name = platform.system().lower()
32+
arch = 'x64' # Assuming x64 architecture
33+
34+
if os_name == 'linux':
35+
zip_name = f"linux-{arch}-{__version__}.tar.gz"
36+
binary_name = 'linux-x64/redlines'
37+
38+
elif os_name == "windows":
39+
zip_name = f"win-{arch}-{__version__}.zip"
40+
binary_name = 'win-x64/redlines.exe'
41+
42+
elif os_name == "darwin":
43+
zip_name = f"osx-{arch}-{__version__}.tar.gz"
44+
binary_name = 'osx-x64/redlines'
45+
else:
46+
raise EnvironmentError("Unsupported OS")
47+
3048
target_path = os.path.join(base_path, 'bin')
3149
logger.debug(f"Target path: {target_path}")
3250

51+
# If target folder doesn't exist... created
3352
if not os.path.exists(target_path):
3453
os.makedirs(target_path)
54+
else:
55+
# If we don't want to reload the binary and it already exists... just return path
56+
if not self._reload_binary:
57+
return os.path.join(target_path, binary_name)
3558

36-
os_name = platform.system().lower()
37-
arch = 'x64' # Assuming x64 architecture
38-
59+
# Otherwise, go ahead and unzip... and this may vary depending on the architecture & env
3960
if os_name == 'linux':
40-
zip_name = f"linux-{arch}-{__version__}.tar.gz"
41-
binary_name = 'linux-x64/redlines'
4261
zip_path = os.path.join(binaries_path, zip_name)
4362
if os.path.exists(zip_path):
4463
with tarfile.open(zip_path, 'r:gz') as tar_ref:
4564
tar_ref.extractall(target_path)
4665

4766
elif os_name == 'windows':
48-
zip_name = f"win-{arch}-{__version__}.zip"
49-
binary_name = 'win-x64/redlines.exe'
5067
zip_path = os.path.join(binaries_path, zip_name)
5168
if os.path.exists(zip_path):
5269
with zipfile.ZipFile(zip_path, 'r') as zip_ref:
5370
zip_ref.extractall(target_path)
5471

5572
elif os_name == 'darwin':
56-
zip_name = f"osx-{arch}-{__version__}.tar.gz"
57-
binary_name = 'osx-x64/redlines'
5873
zip_path = os.path.join(binaries_path, zip_name)
5974
if os.path.exists(zip_path):
6075
with tarfile.open(zip_path, 'r:gz') as tar_ref:
6176
tar_ref.extractall(target_path)
62-
77+
# This is redundant given above test... but I'm leaving it here.
6378
else:
6479
raise EnvironmentError("Unsupported OS")
6580

0 commit comments

Comments
 (0)