8000 New approach for checking we are loading the right file by t8y8 · Pull Request #62 · tableau/document-api-python · GitHub
[go: up one dir, main page]

Skip to content

New approach for checking we are loading the right file #62

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 27, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion tableaudocumentapi/datasource.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ def __init__(self, dsxml, filename=None):
def from_file(cls, filename):
"""Initialize datasource from file (.tds)"""

dsxml = xml_open(filename).getroot()
dsxml = xml_open(filename, cls.__name__.lower()).getroot()
return cls(dsxml, filename)

def save(self):
Expand Down
2 changes: 1 addition & 1 deletion tableaudocumentapi/workbook.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def __init__(self, filename):

self._filename = filename

self._workbookTree = xml_open(self._filename)
self._workbookTree = xml_open(self._filename, self.__class__.__name__.lower())

self._workbookRoot = self._workbookTree.getroot()
# prepare our datasource objects
Expand Down
32 changes: 22 additions & 10 deletions tableaudocumentapi/xfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,28 @@ class TableauVersionNotSupportedException(Exception):
pass


def xml_open(filename):
# Determine if this is a twb or twbx and get the xml root
class TableauInvalidFileException(Exception):
pass


def xml_open(filename, expected_root=None):

if zipfile.is_zipfile(filename):
tree = get_xml_from_archive(filename)
else:
tree = ET.parse(filename)
file_version = Version(tree.getroot().attrib.get('version', '0.0'))

tree_root = tree.getroot()

file_version = Version(tree_root.attrib.get('version', '0.0'))

if file_version < MIN_SUPPORTED_VERSION:
raise TableauVersionNotSupportedException(file_version)

if expected_root and (expected_root != tree_root.tag):
raise TableauInvalidFileException(
"'{}'' is not a valid '{}' file".format(filename, expected_root))

return tree


Expand All @@ -40,14 +53,13 @@ def temporary_directory(*args, **kwargs):

def find_file_in_zip(zip_file):
for filename in zip_file.namelist():
try:
with zip_file.open(filename) as xml_candidate:
ET.parse(xml_candidate).getroot().tag in (
'workbook', 'datasource')
with zip_file.open(filename) as xml_candidate:
try:
ET.parse(xml_candidate)
return filename
except ET.ParseError:
# That's not an XML file by gosh
pass
except ET.ParseError:
# That's not an XML file by gosh
pass


def get_xml_from_archive(filename):
Expand Down
1 change: 1 addition & 0 deletions test/assets/TABLEAU_82_TWB.twb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<?xml version='1.0' encoding='utf-8' ?><workbook source-build='9.3.1 (9300.16.0510.0100)' source-platform='mac' version='8.2' xmlns:user='http://www.tableausoftware.com/xml/user'><datasources><datasource caption='xy (TestV1)' inline='true' name='sqlserver.17u3bqc16tjtxn14e2hxh19tyvpo' version='9.3'><connection authentication='sspi' class='sqlserver' dbname='TestV1' odbc-native-protocol='yes' one-time-sql='' server='mssql2012.test.tsi.lan' username=''></connection></datasource></datasources></workbook>
21 changes: 21 additions & 0 deletions test/bvt.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@
import xml.etree.ElementTree as ET

from tableaudocumentapi import Workbook, Datasource, Connection, ConnectionParser
from tableaudocumentapi.xfile import TableauInvalidFileException, TableauVersionNotSupportedException

TEST_DIR = os.path.dirname(__file__)

TABLEAU_82_TWB = os.path.join(TEST_DIR, 'assets', 'TABLEAU_82_TWB.twb')

TABLEAU_93_TWB = os.path.join(TEST_DIR, 'assets', 'TABLEAU_93_TWB.twb')

TABLEAU_93_TDS = os.path.join(TEST_DIR, 'assets', 'TABLEAU_93_TDS.tds')
Expand Down Expand Up @@ -282,10 +285,28 @@ def test_can_open_twbx_and_save_as_changes(self):


class EmptyWorkbookWillLoad(unittest.TestCase):

def test_no_exceptions_thrown(self):
wb = Workbook(EMPTY_WORKBOOK)
self.assertIsNotNone(wb)


class LoadOnlyValidFileTypes(unittest.TestCase):

def test_exception_when_workbook_given_tdsx(self):
with self.assertRaises(TableauInvalidFileException):
wb = Workbook(TABLEAU_10_TDSX)

def test_exception_when_datasource_given_twbx(self):
with self.assertRaises(TableauInvalidFileException):
ds = Datasource.from_file(TABLEAU_10_TWBX)


class SupportedWorkbookVersions(unittest.TestCase):

def test_82_workbook_throws_exception(self):
with self.assertRaises(TableauVersionNotSupportedException):
wb = Workbook(TABLEAU_82_TWB)

if __name__ == '__main__':
unittest.main()
0