10000 Added Save/Save As support to Datasource. This required plumbing a fi… · PBrad/document-api-python@abe6ec1 · GitHub
[go: up one dir, main page]

Skip to content

Commit abe6ec1

Browse files
committed
Added Save/Save As support to Datasource. This required plumbing a filename through from the from_file classmethod and building an ElementTree to serialize into a file. This pretty much mirrors the Workbook model. I also removed the file validation check on save since we validate on instantiation already, at this point you know you have a 'twb/tds' open. Added a matching test
1 parent d6c27d8 commit abe6ec1

File tree

3 files changed

+43
-9
lines changed

3 files changed

+43
-9
lines changed

Document API/tableaudocumentapi/datasource.py

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,14 @@ class Datasource(object):
1818
# Public API.
1919
#
2020
###########################################################################
21-
def __init__(self, dsxml):
21+
def __init__(self, dsxml, filename=None):
2222
"""
2323
Constructor. Default is to create datasource from xml.
2424
2525
"""
26+
self._filename = filename
2627
self._datasourceXML = dsxml
28+
self._datasourceTree = ET.ElementTree(self._datasourceXML)
2729
self._name = self._datasourceXML.get('name') or self._datasourceXML.get('formatted-name') # TDS files don't have a name attribute
2830
self._version = self._datasourceXML.get('version')
2931
self._connection = Connection(self._datasourceXML.find('connection'))
@@ -32,7 +34,36 @@ def __init__(self, dsxml):
3234
def from_file(cls, filename):
3335
"Initialize datasource from file (.tds)"
3436
dsxml = ET.parse(filename).getroot()
35-
return cls(dsxml)
37+
return cls(dsxml, filename)
38+
39+
def save(self):
40+
"""
41+
Call finalization code and save file.
42+
43+
Args:
44+
None.
45+
46+
Returns:
47+
Nothing.
48+
49+
"""
50+
51+
# save the file
52+
self._datasourceTree.write(self._filename)
53+
54+
def save_as(self, new_filename):
55+
"""
56+
Save our file with the name provided.
57+
58+
Args:
59+
new_filename: New name for the workbook file. String.
60+
61+
Returns:
62+
Nothing.
63+
64+
"""
65+
self._datasourceTree.write(new_filename)
66+
3667

3768
###########
3869
# name

Document API/tableaudocumentapi/workbook.py

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -90,13 +90,8 @@ def save_as(self, new_filename):
9090
9191
"""
9292

93-
# We have a valid type of input file
94-
if self._is_valid_file(new_filename):
95-
# save the file
96-
self._workbookTree.write(new_filename)
97-
else:
98-
print('Invalid file type. Must be .twb or .tds.')
99-
raise Exception()
93+
self._workbookTree.write(new_filename)
94+
10095

10196
###########################################################################
10297
#

Document API/test.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,14 @@ def test_can_extract_connection(self):
7878
ds = Datasource.from_file(self.tds_file.name)
7979
self.assertIsInstance(ds.connection, Connection)
8080

81+
def test_can_save_tds(self):
82+
original_tds = Datasource.from_file(self.tds_file.name)
83+
original_tds.connection.dbname = 'newdb.test.tsi.lan'
84+
original_tds.save()
85+
86+
new_tds = Datasource.from_file(self.tds_file.name)
87+
self.assertEqual(new_tds.connection.dbname, 'newdb.test.tsi.lan')
88+
8189

8290
class WorkbookModelTests(unittest.TestCase):
8391

0 commit comments

Comments
 (0)
0