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):
52
52
53
53
54
54
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 :
56
64
with zip_file .open (filename ) as xml_candidate :
57
65
try :
58
66
ET .parse (xml_candidate )
@@ -81,10 +89,10 @@ def build_archive_file(archive_contents, zip_file):
81
89
82
90
83
91
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,
85
93
# 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
88
96
89
97
if new_filename is None :
90
98
new_filename = filename
Original file line number Diff line number Diff line change 12
12
'BadZip.zip'
13
13
)
14
14
15
+ TWBX_WITH_CACHE_FILES = os .path .join (
16
+ TEST_ASSET_DIR ,
17
+ 'Cache.twbx'
18
+ )
19
+
15
20
16
21
class XFileEdgeTests (unittest .TestCase ):
17
22
def test_find_file_in_zip_no_xml_file (self ):
18
23
badzip = zipfile .ZipFile (BAD_ZIP_FILE )
19
24
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