Skip to content

Commit

Permalink
Fix #117 by only attempting files with the right extension inside the…
Browse files Browse the repository at this point in the history
… archive (#118)
  • Loading branch information
t8y8 authored Dec 12, 2016
1 parent a426a42 commit fb36a3a
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 4 deletions.
16 changes: 12 additions & 4 deletions tableaudocumentapi/xfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,15 @@ def temporary_directory(*args, **kwargs):


def find_file_in_zip(zip_file):
for filename in zip_file.namelist():
'''Returns the twb/tds file from a Tableau packaged file format. Packaged
files can contain cache entries which are also valid XML, so only look for
files with a .tds or .twb extension.
'''

candidate_files = filter(lambda x: x.split('.')[-1] in ('twb', 'tds'),
zip_file.namelist())

for filename in candidate_files:
with zip_file.open(filename) as xml_candidate:
try:
ET.parse(xml_candidate)
Expand Down Expand Up @@ -81,10 +89,10 @@ def build_archive_file(archive_contents, zip_file):


def save_into_archive(xml_tree, filename, new_filename=None):
# Saving a archive means extracting the contents into a temp folder,
# Saving an 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
# packaging it back up into a zip with a very specific format
# e.g. no empty files for directories, which Windows and Mac do by default

if new_filename is None:
new_filename = filename
Expand Down
Binary file added test/assets/Cache.twbx
Binary file not shown.
9 changes: 9 additions & 0 deletions test/test_xfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,17 @@
'BadZip.zip'
)

TWBX_WITH_CACHE_FILES = os.path.join(
TEST_ASSET_DIR,
'Cache.twbx'
)


class XFileEdgeTests(unittest.TestCase):
def test_find_file_in_zip_no_xml_file(self):
badzip = zipfile.ZipFile(BAD_ZIP_FILE)
self.assertIsNone(find_file_in_zip(badzip))

def test_only_find_twbs(self):
twb_from_twbx_with_cache = zipfile.ZipFile(TWBX_WITH_CACHE_FILES)
self.assertEqual(find_file_in_zip(twb_from_twbx_with_cache), 'Superstore.twb')

0 comments on commit fb36a3a

Please sign in to comment.