-
Notifications
You must be signed in to change notification settings - Fork 182
Initial TDSX support. #44
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
9a8f0d1
Initial TDSX support. Lots of duplicated code with TWBX's
t8y8 bd6d3c9
Refactor all the archive manipulation logic into one module
t8y8 8385b5c
Move save logic into helper function. Remove unused tests and methods…
t8y8 cbcda8f
Now find the file by checking if it's a workbook or datasource, and o…
t8y8 813e079
ZipFile.open should be a context manager
t8y8 be4c9c8
Missed a spot with the context manager
t8y8 a885f15
The truth is out there...
t8y8 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,76 @@ | ||
import contextlib | ||
import os | ||
import shutil | ||
import tempfile | ||
import zipfile | ||
|
||
import xml.etree.ElementTree as ET | ||
|
||
|
||
@contextlib.contextmanager | ||
def temporary_directory(*args, **kwargs): | ||
d = tempfile.mkdtemp(*args, **kwargs) | ||
try: | ||
yield d | ||
finally: | ||
shutil.rmtree(d) | ||
|
||
|
||
def find_file_in_zip(zip): | ||
for filename in zip.namelist(): | ||
try: | ||
with zip.open(filename) as xml_candidate: | ||
ET.parse(xml_candidate).getroot().tag in ( | ||
'workbook', 'datasource') | ||
return filename | ||
except ET.ParseError: | ||
# That's not an XML file by gosh | ||
pass | ||
|
||
|
||
def get_xml_from_archive(filename): | ||
with zipfile.ZipFile(filename) as zf: | ||
with zf.open(find_file_in_zip(zf)) as xml_file: | ||
xml_tree = ET.parse(xml_file) | ||
|
||
return xml_tree | ||
|
||
|
||
def build_archive_file(archive_contents, zip): | ||
for root_dir, _, files in os.walk(archive_contents): | ||
relative_dir = os.path.relpath(root_dir, archive_contents) | ||
for f in files: | ||
temp_file_full_path = os.path.join( | ||
archive_contents, relative_dir, f) | ||
zipname = os.path.join(relative_dir, f) | ||
zip.write(temp_file_full_path, arcname=zipname) | ||
|
||
|
||
def save_into_archive(xml_tree, filename, new_filename=None): | ||
# Saving a archive means extracting the contents into a temp folder, | ||
# saving the changes over the twb/tds in that folder, and then | ||
# packaging it back up into a specifically formatted zip with the correct | ||
# relative file paths | ||
|
||
if new_filename is None: | ||
new_filename = filename | ||
|
||
# Extract to temp directory | ||
with temporary_directory() as temp_path: | ||
with zipfile.ZipFile(filename) as zf: | ||
xml_file = find_file_in_zip(zf) | ||
zf.extractall(temp_path) | ||
# Write the new version of the file to the temp directory | ||
xml_tree.write(os.path.join( | ||
temp_path, xml_file), encoding="utf-8", xml_declaration=True) | ||
|
||
# Write the new archive with the contents of the temp folder | ||
with zipfile.ZipFile(new_filename, "w", compression=zipfile.ZIP_DEFLATED) as new_archive: | ||
build_archive_file(temp_path, new_archive) | ||
|
||
|
||
def _save_file(container_file, xml_tree, new_filename=None): | ||
if zipfile.is_zipfile(container_file): | ||
save_into_archive(xml_tree, container_file, new_filename) | ||
else: | ||
xml_tree.write(container_file, encoding="utf-8", xml_declaration=True) |
Binary file not shown.
This file contains hidden or 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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this pattern repeated? Seems like this could be a good candidate for a helper function.