8000 Fix #117 by only attempting files with the right extension inside the… · LluisRamon/document-api-python@fb36a3a · GitHub
[go: up one dir, main page]

Skip to content

Commit fb36a3a

Browse files
authored
Fix tableau#117 by only attempting files with the right extension inside the archive (tableau#118)
1 parent a426a42 commit fb36a3a

File tree

3 files changed

+21
-4
lines changed

3 files changed

+21
-4
lines changed

tableaudocumentapi/xfile.py

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,15 @@ def temporary_directory(*args, **kwargs):
5252

5353

5454
def find_file_in_zip(zip_file):
55-
for filename in zip_file.namelist():
55+
'''Returns the twb/tds file from a Tableau packaged file format. Packaged
56+
files can contain cache entries which are also valid XML, so only look for
57+
files with a .tds or .twb extension.
58+
'''
59+
60+
candidate_files = filter(lambda x: x.split('.')[-1] in ('twb', 'tds'),
61+
zip_file.namelist())
62+
63+
for filename in candidate_files:
5664
with zip_file.open(filename) as xml_candidate:
5765
try:
5866
ET.parse(xml_candidate)
@@ -81,10 +89,10 @@ def build_archive_file(archive_contents, zip_file):
8189

8290

8391
def save_into_archive(xml_tree, filename, new_filename=None):
84-
# Saving a archive means extracting the contents into a temp folder,
92+
# Saving an archive means extracting the contents into a temp folder,
8593
# saving the changes over the twb/tds in that folder, and then
86-
# packaging it back up into a specifically formatted zip with the correct
87-
# relative file paths
94+
# packaging it back up into a zip with a very specific format
95+
# e.g. no empty files for directories, which Windows and Mac do by default
8896

8997
if new_filename is None:
9098
new_filename = filename

test/assets/Cache.twbx

35.3 KB
Binary file not shown.

test/test_xfile.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,17 @@
1212
'BadZip.zip'
1313
)
1414

15+
TWBX_WITH_CACHE_FILES = os.path.join(
16+
TEST_ASSET_DIR,
17+
'Cache.twbx'
18+
)
19+
1520

1621
class XFileEdgeTests(unittest.TestCase):
1722
def test_find_file_in_zip_no_xml_file(self):
1823
badzip = zipfile.ZipFile(BAD_ZIP_FILE)
1924
self.assertIsNone(find_file_in_zip(badzip))
25+
26+
def test_only_find_twbs(self):
27+
twb_from_twbx_with_cache = zipfile.ZipFile(TWBX_WITH_CACHE_FILES)
28+
self.assertEqual(find_file_in_zip(twb_from_twbx_with_cache), 'Superstore.twb')

0 commit comments

Comments
 (0)
0