From c0f201cfff1cf06cba5374dd9d25b4e24b0e8299 Mon Sep 17 00:00:00 2001 From: Russell Hay Date: Tue, 18 Oct 2016 08:57:15 -0700 Subject: [PATCH 1/3] Add caption support for datasources --- tableaudocumentapi/datasource.py | 10 +++++++++ test/assets/datasource_test.tds | 3 ++- test/test_datasource.py | 35 +++++++++++++++++++++++++++++++- 3 files changed, 46 insertions(+), 2 deletions(-) diff --git a/tableaudocumentapi/datasource.py b/tableaudocumentapi/datasource.py index 63157e9..d5cfb64 100644 --- a/tableaudocumentapi/datasource.py +++ b/tableaudocumentapi/datasource.py @@ -137,6 +137,7 @@ def __init__(self, dsxml, filename=None): 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._caption = self._datasourceXML.get('caption', '') self._connection_parser = ConnectionParser( self._datasourceXML, version=self._version) self._connections = self._connection_parser.get_connections() @@ -207,6 +208,15 @@ def name(self): def version(self): return self._version + @property + def caption(self): + return self._caption + + @caption.setter + def caption(self, value): + self._datasourceXML.attrib['caption'] = value + self._caption = value + ########### # connections ########### diff --git a/test/assets/datasource_test.tds b/test/assets/datasource_test.tds index 5f280eb..407127d 100644 --- a/test/assets/datasource_test.tds +++ b/test/assets/datasource_test.tds @@ -1,5 +1,6 @@ - + diff --git a/test/test_datasource.py b/test/test_datasource.py index baf5bc3..9b8828b 100644 --- a/test/test_datasource.py +++ b/test/test_datasource.py @@ -1,5 +1,9 @@ -import unittest +import os import os.path +import shutil +import tempfile +import unittest + from tableaudocumentapi import Datasource, Workbook @@ -22,6 +26,19 @@ class DataSourceFieldsTDS(unittest.TestCase): def setUp(self): self.ds = Datasource.from_file(TEST_TDS_FILE) + self.to_delete = set() + + def cleanUp(self): + for path in self.to_delete: + if os.path.isdir(path): + shutil.rmtree(path, ignore_errors=True) + elif os.path.isfile(path): + os.unlink(path) + + def get_temp_file(self, filename): + tempdir = tempfile.mkdtemp('tda-datasource') + self.to_delete.add(tempdir) + return os.path.join(tempdir, filename) def test_datasource_returns_correct_fields(self): self.assertIsNotNone(self.ds.fields) @@ -63,6 +80,22 @@ def test_datasource_field_description(self): self.assertIsNotNone(actual) self.assertTrue(u'muted gray' in actual) + def test_datasource_caption(self): + actual = self.ds.caption + self.assertIsNotNone(actual) + self.assertEqual(actual, 'foo') + + def test_datasource_can_set_caption(self): + filename = self.get_temp_file('test_datasource_can_set_caption') + self.ds.caption = 'bar' + self.ds.save_as(filename) + + actual = Datasource.from_file(filename) + self.assertIsNotNone(actual) + self.assertIsNotNone(actual.caption) + self.assertEqual(actual.caption, 'bar') + + def test_datasource_clear_repository_location(self): filename = os.path.join(TEST_ASSET_DIR, 'clear-repository-test.tds') From c039ae75816dfe03ce8bd6f0ba775e0b61fac8dd Mon Sep 17 00:00:00 2001 From: Russell Hay Date: Tue, 18 Oct 2016 09:03:35 -0700 Subject: [PATCH 2/3] fix a pep8 issue E303 --- test/test_datasource.py | 1 - 1 file changed, 1 deletion(-) diff --git a/test/test_datasource.py b/test/test_datasource.py index 9b8828b..cafdeb8 100644 --- a/test/test_datasource.py +++ b/test/test_datasource.py @@ -95,7 +95,6 @@ def test_datasource_can_set_caption(self): self.assertIsNotNone(actual.caption) self.assertEqual(actual.caption, 'bar') - def test_datasource_clear_repository_location(self): filename = os.path.join(TEST_ASSET_DIR, 'clear-repository-test.tds') From 3315788853c2477fbaab7e6f6baa9d7c2ab1bbb2 Mon Sep 17 00:00:00 2001 From: Russell Hay Date: Tue, 18 Oct 2016 14:17:24 -0700 Subject: [PATCH 3/3] Add ability to remove the caption using del --- tableaudocumentapi/datasource.py | 7 ++++++- test/test_datasource.py | 9 +++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/tableaudocumentapi/datasource.py b/tableaudocumentapi/datasource.py index d5cfb64..a34cba5 100644 --- a/tableaudocumentapi/datasource.py +++ b/tableaudocumentapi/datasource.py @@ -214,9 +214,14 @@ def caption(self): @caption.setter def caption(self, value): - self._datasourceXML.attrib['caption'] = value + self._datasourceXML.set('caption', value) self._caption = value + @caption.deleter + def caption(self): + del self._datasourceXML.attrib['caption'] + self._caption = '' + ########### # connections ########### diff --git a/test/test_datasource.py b/test/test_datasource.py index cafdeb8..838bc55 100644 --- a/test/test_datasource.py +++ b/test/test_datasource.py @@ -95,6 +95,15 @@ def test_datasource_can_set_caption(self): self.assertIsNotNone(actual.caption) self.assertEqual(actual.caption, 'bar') + def test_datasource_can_remove_caption(self): + filename = self.get_temp_file('test_datasource_can_remove_caption') + del self.ds.caption + self.ds.save_as(filename) + + actual = Datasource.from_file(filename) + self.assertIsNotNone(actual) + self.assertEqual(actual.caption, '') + def test_datasource_clear_repository_location(self): filename = os.path.join(TEST_ASSET_DIR, 'clear-repository-test.tds') pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy