From c4c4b3bad1735bf48522a27cee46659f8db6eda4 Mon Sep 17 00:00:00 2001 From: T8y8 Date: Sat, 23 Jul 2016 00:23:44 -0700 Subject: [PATCH 1/2] We weren't truly checking for a workbook or datasource root tag, now we are! --- tableaudocumentapi/xfile.py | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/tableaudocumentapi/xfile.py b/tableaudocumentapi/xfile.py index 13e08c7..de4d123 100644 --- a/tableaudocumentapi/xfile.py +++ b/tableaudocumentapi/xfile.py @@ -17,12 +17,14 @@ def temporary_directory(*args, **kwargs): def find_file_in_zip(zip): + _VALID_ROOTS = ('workbook', 'datasource') + for filename in zip.namelist(): try: with zip.open(filename) as xml_candidate: - ET.parse(xml_candidate).getroot().tag in ( - 'workbook', 'datasource') - return filename + root_tag = ET.parse(xml_candidate).getroot().tag + if root_tag in _VALID_ROOTS: + return filename except ET.ParseError: # That's not an XML file by gosh pass From ea49e2f360e2179c1a4dab6b1c41c3aab349c584 Mon Sep 17 00:00:00 2001 From: Tyler Doyle Date: Sat, 23 Jul 2016 12:02:25 -0700 Subject: [PATCH 2/2] More robust approach for opening the right file (Not final approach, just want feedback) (Also trying the github in web editor, so this was added blind) --- tableaudocumentapi/xfile.py | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/tableaudocumentapi/xfile.py b/tableaudocumentapi/xfile.py index de4d123..dab7c68 100644 --- a/tableaudocumentapi/xfile.py +++ b/tableaudocumentapi/xfile.py @@ -17,14 +17,22 @@ def temporary_directory(*args, **kwargs): def find_file_in_zip(zip): - _VALID_ROOTS = ('workbook', 'datasource') + _VALID_ROOTS_AND_EXTS = { + 'workbook': ('.twb', '.twbx'), + 'datasource': ('.tds', '.tdsx') + } for filename in zip.namelist(): + _, file_extension = os.path.splitext(filename) + try: with zip.open(filename) as xml_candidate: root_tag = ET.parse(xml_candidate).getroot().tag - if root_tag in _VALID_ROOTS: + if root_tag in _VALID_ROOTS_AND_EXTS.keys() and \ + file_extension in _VALID_ROOTS_AND_EXTS.get(root_tag, None): return filename + else: + raise Exception # TableauInvalidFileException or something should go here except ET.ParseError: # That's not an XML file by gosh pass