8000 Added Save/Save As support to Datasource by t8y8 · Pull Request #6 · tableau/document-api-python · GitHub
[go: up one dir, main page]

Skip to content

Added Save/Save As support to Datasource #6

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 1 commit into from
May 12, 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
35 changes: 33 additions & 2 deletions Document API/tableaudocumentapi/datasource.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,14 @@ class Datasource(object):
# Public API.
#
###########################################################################
def __init__(self, dsxml):
def __init__(self, dsxml, filename=None):
"""
Constructor. Default is to create datasource from xml.

"""
self._filename = filename
self._datasourceXML = dsxml
self._datasourceTree = ET.ElementTree(self._datasourceXML)
self._name = self._datasourceXML.get('name') or self._datasourceXML.get('formatted-name') # TDS files don't have a name attribute
self._version = self._datasourceXML.get('version')
self._connection = Connection(self._datasourceXML.find('connection'))
Expand All @@ -32,7 +34,36 @@ def __init__(self, dsxml):
def from_file(cls, filename):
"Initialize datasource from file (.tds)"
dsxml = ET.parse(filename).getroot()
return cls(dsxml)
return cls(dsxml, filename)

def save(self):
"""
Call finalization code and save file.

Args:
None.

Returns:
Nothing.

"""

# save the file
self._datasourceTree.write(self._filename)

def save_as(self, new_filename):
"""
Save our file with the name provided.

Args:
new_filename: New name for the workbook file. String.

Returns:
Nothing.

"""
self._datasourceTree.write(new_filename)


###########
# name
Expand Down
9 changes: 2 additions & 7 deletions Document API/tableaudocumentapi/workbook.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,8 @@ def save_as(self, new_filename):

"""

# We have a valid type of input file
if self._is_valid_file(new_filename):
# save the file
self._workbookTree.write(new_filename)
else:
print('Invalid file type. Must be .twb or .tds.')
raise Exception()
self._workbookTree.write(new_filename)


###########################################################################
#
Expand Down
8 changes: 8 additions & 0 deletions Document API/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,14 @@ def test_can_extract_connection(self):
ds = Datasource.from_file(self.tds_file.name)
self.assertIsInstance(ds.connection, Connection)

def test_can_save_tds(self):
original_tds = Datasource.from_file(self.tds_file.name)
original_tds.connection.dbname = 'newdb.test.tsi.lan'
original_tds.save()

new_tds = Datasource.from_file(self.tds_file.name)
self.assertEqual(new_tds.connection.dbname, 'newdb.test.tsi.lan')


class WorkbookModelTests(unittest.TestCase):

Expand Down
0