File tree Expand file tree Collapse file tree 3 files changed +21
-4
lines changed
Expand file tree Collapse file tree 3 files changed +21
-4
lines changed Original file line number Diff line number Diff line change @@ -52,7 +52,15 @@ def temporary_directory(*args, **kwargs):
5252
5353
5454def 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
8391def 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
Original file line number Diff line number Diff line change 1212 'BadZip.zip'
1313)
1414
15+ TWBX_WITH_CACHE_FILES = os .path .join (
16+ TEST_ASSET_DIR ,
17+ 'Cache.twbx'
18+ )
19+
1520
1621class 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' )
You can’t perform that action at this time.
0 commit comments