diff --git a/tableaudocumentapi/datasource.py b/tableaudocumentapi/datasource.py index 69318ed..88a2b29 100644 --- a/tableaudocumentapi/datasource.py +++ b/tableaudocumentapi/datasource.py @@ -213,6 +213,11 @@ def version(self): def connections(self): return self._connections + def clear_repository_location(self): + tag = self._datasourceXML.find('./repository-location') + if tag is not None: + self._datasourceXML.remove(tag) + ########### # fields ########### diff --git a/tableaudocumentapi/xfile.py b/tableaudocumentapi/xfile.py index 66e5aac..3067781 100644 --- a/tableaudocumentapi/xfile.py +++ b/tableaudocumentapi/xfile.py @@ -105,10 +105,10 @@ def save_into_archive(xml_tree, filename, new_filename=None): def _save_file(container_file, xml_tree, new_filename=None): - if container_file is None: - container_file = new_filename + if new_filename is None: + new_filename = container_file if zipfile.is_zipfile(container_file): save_into_archive(xml_tree, container_file, new_filename) else: - xml_tree.write(container_file, encoding="utf-8", xml_declaration=True) + xml_tree.write(new_filename, encoding="utf-8", xml_declaration=True) diff --git a/test/assets/datasource_test.tds b/test/assets/datasource_test.tds index bfab77b..5f280eb 100644 --- a/test/assets/datasource_test.tds +++ b/test/assets/datasource_test.tds @@ -1,93 +1,94 @@ - - - - - - - a - 130 - [a] - [xy] - a - 1 - string - Count - 255 - true - - "SQL_WVARCHAR" - "SQL_C_WCHAR" - "true" - - - - Today's Date - 130 - [Today's Date] - [xy] - a - 1 - string - Count - 255 - true - - "SQL_WVARCHAR" - "SQL_C_WCHAR" - "true" - - - - x - 3 - [x] - [xy] - x - 2 - integer - Sum - 10 - true - - "SQL_INTEGER" - "SQL_C_SLONG" - - - - y - 3 - [y] - [xy] - y - 3 - integer - Sum - 10 - true - - "SQL_INTEGER" - "SQL_C_SLONG" - - - - - - - - - - - - A thing - Something will go here too, in a muted gray - - - - - - - - - - - + + + + + + + + a + 130 + [a] + [xy] + a + 1 + string + Count + 255 + true + + "SQL_WVARCHAR" + "SQL_C_WCHAR" + "true" + + + + Today's Date + 130 + [Today's Date] + [xy] + a + 1 + string + Count + 255 + true + + "SQL_WVARCHAR" + "SQL_C_WCHAR" + "true" + + + + x + 3 + [x] + [xy] + x + 2 + integer + Sum + 10 + true + + "SQL_INTEGER" + "SQL_C_SLONG" + + + + y + 3 + [y] + [xy] + y + 3 + integer + Sum + 10 + true + + "SQL_INTEGER" + "SQL_C_SLONG" + + + + + + + + + + + + A thing + Something will go here too, in a muted gray + + + + + + + + + + + diff --git a/test/bvt.py b/test/bvt.py index 60e42c3..21313dd 100644 --- a/test/bvt.py +++ b/test/bvt.py @@ -164,6 +164,24 @@ def test_can_save_tds(self): new_tds = Datasource.from_file(self.tds_file.name) self.assertEqual(new_tds.connections[0].dbname, 'newdb') + def test_can_save_as_tds(self): + new_filename = os.path.join( + os.path.dirname(self.tds_file.name), + "new_{}".format(os.path.basename(self.tds_file.name)) + ) + + try: + original_tds = Datasource.from_file(self.tds_file.name) + original_tds.connections[0].dbname = 'newdb' + + original_tds.save_as(new_filename) + + new_tds = Datasource.from_file(new_filename) + self.assertEqual(new_tds.connections[0].dbname, 'newdb') + finally: + if os.path.exists(new_filename): + os.unlink(new_filename) + class DatasourceModelV10TDSXTests(unittest.TestCase): diff --git a/test/test_datasource.py b/test/test_datasource.py index 66b3f79..baf5bc3 100644 --- a/test/test_datasource.py +++ b/test/test_datasource.py @@ -63,6 +63,19 @@ def test_datasource_field_description(self): self.assertIsNotNone(actual) self.assertTrue(u'muted gray' in actual) + def test_datasource_clear_repository_location(self): + filename = os.path.join(TEST_ASSET_DIR, 'clear-repository-test.tds') + + self.assertIsNotNone(self.ds._datasourceXML.find('.//repository-location')) + self.ds.clear_repository_location() + try: + self.ds.save_as(filename) + with open(filename, 'r') as newfile: + self.assertFalse('repository-location' in newfile.read()) + finally: + if os.path.exists(filename): + os.unlink(filename) + class DataSourceFieldsTWB(unittest.TestCase):