Skip to content

#86 Repository location stripping #88

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 4 commits into from
Oct 4, 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
5 changes: 5 additions & 0 deletions tableaudocumentapi/datasource.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,11 @@ def version(self):
def connections(self):
return self._connections

def clear_repository_location(self):
tag = self._datasourceXML.find('./repository-location')
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Intentional that it's ./ here but .// in the test?

if tag is not None:
self._datasourceXML.remove(tag)

###########
# fields
###########
Expand Down
6 changes: 3 additions & 3 deletions tableaudocumentapi/xfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, thanks!

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)
187 changes: 94 additions & 93 deletions test/assets/datasource_test.tds
Original file line number Diff line number Diff line change
@@ -1,93 +1,94 @@
<?xml version='1.0' encoding='utf-8' ?>
<datasource formatted-name='postgres.1of3kl00aoax5d1a1ejma1397430' inline='true' source-platform='mac' version='9.3' xmlns:user='http://www.tableausoftware.com/xml/user'>
<connection authentication='username-password' class='postgres' dbname='TestV1' odbc-native-protocol='yes' port='5432' server='postgres91.test.tsi.lan' username='test'>
<relation name='xy' table='[public].[xy]' type='table' />
<metadata-records>
<metadata-record class='column'>
<remote-name>a</remote-name>
<remote-type>130</remote-type>
<local-name>[a]</local-name>
<parent-name>[xy]</parent-name>
<remote-alias>a</remote-alias>
<ordinal>1</ordinal>
<local-type>string</local-type>
<aggregation>Count</aggregation>
<width>255</width>
<contains-null>true</contains-null>
<attributes>
<attribute datatype='string' name='DebugRemoteType'>&quot;SQL_WVARCHAR&quot;</attribute>
<attribute datatype='string' name='DebugWireType'>&quot;SQL_C_WCHAR&quot;</attribute>
<attribute datatype='string' name='TypeIsVarchar'>&quot;true&quot;</attribute>
</attributes>
</metadata-record>
<metadata-record class='column'>
<remote-name>Today's Date</remote-name>
<remote-type>130</remote-type>
<local-name>[Today&apos;s Date]</local-name>
<parent-name>[xy]</parent-name>
<remote-alias>a</remote-alias>
<ordinal>1</ordinal>
<local-type>string</local-type>
<aggregation>Count</aggregation>
<width>255</width>
<contains-null>true</contains-null>
<attributes>
<attribute datatype='string' name='DebugRemoteType'>&quot;SQL_WVARCHAR&quot;</attribute>
<attribute datatype='string' name='DebugWireType'>&quot;SQL_C_WCHAR&quot;</attribute>
<attribute datatype='string' name='TypeIsVarchar'>&quot;true&quot;</attribute>
</attributes>
</metadata-record>
<metadata-record class='column'>
<remote-name>x</remote-name>
<remote-type>3</remote-type>
<local-name>[x]</local-name>
<parent-name>[xy]</parent-name>
<remote-alias>x</remote-alias>
<ordinal>2</ordinal>
<local-type>integer</local-type>
<aggregation>Sum</aggregation>
<precision>10</precision>
<contains-null>true</contains-null>
<attributes>
<attribute datatype='string' name='DebugRemoteType'>&quot;SQL_INTEGER&quot;</attribute>
<attribute datatype='string' name='DebugWireType'>&quot;SQL_C_SLONG&quot;</attribute>
</attributes>
</metadata-record>
<metadata-record class='column'>
<remote-name>y</remote-name>
<remote-type>3</remote-type>
<local-name>[y]</local-name>
<parent-name>[xy]</parent-name>
<remote-alias>y</remote-alias>
<ordinal>3</ordinal>
<local-type>integer</local-type>
<aggregation>Sum</aggregation>
<precision>10</precision>
<contains-null>true</contains-null>
<attributes>
<attribute datatype='string' name='DebugRemoteType'>&quot;SQL_INTEGER&quot;</attribute>
<attribute datatype='string' name='DebugWireType'>&quot;SQL_C_SLONG&quot;</attribute>
</attributes>
</metadata-record>
</metadata-records>
</connection>
<aliases enabled='yes' />
<column datatype='integer' name='[Number of Records]' role='measure' type='quantitative' user:auto-column='numrec'>
<calculation class='tableau' formula='1' />
</column>
<column caption='A' datatype='string' name='[a]' role='dimension' type='nominal'>
<desc>
<formatted-text>
<run bold='true' fontsize='96'>A thing</run>
<run fontcolor='#686868'>&#10;Something will go here too, in a muted gray</run>
</formatted-text>
</desc>
</column>
<column caption='Today&apos;s Date' datatype='string' name='[Today&apos;s Date]' role='dimension' type='nominal' />
<column caption='X' datatype='integer' name='[x]' role='measure' type='ordinal' />
<column caption='Y' datatype='integer' name='[y]' role='measure' type='quantitative' />
<layout dim-ordering='alphabetic' dim-percentage='0.5' measure-ordering='alphabetic' measure-percentage='0.5' show-structure='true' />
<semantic-values>
<semantic-value key='[Country].[Name]' value='&quot;United States&quot;' />
</semantic-values>
</datasource>
<?xml version='1.0' encoding='utf-8' ?>
<datasource formatted-name='postgres.1of3kl00aoax5d1a1ejma1397430' inline='true' source-platform='mac' version='9.3' xmlns:user='http://www.tableausoftware.com/xml/user'>
<repository-location />
<connection authentication='username-password' class='postgres' dbname='TestV1' odbc-native-protocol='yes' port='5432' server='postgres91.test.tsi.lan' username='test'>
<relation name='xy' table='[public].[xy]' type='table' />
<metadata-records>
<metadata-record class='column'>
<remote-name>a</remote-name>
<remote-type>130</remote-type>
<local-name>[a]</local-name>
<parent-name>[xy]</parent-name>
<remote-alias>a</remote-alias>
<ordinal>1</ordinal>
<local-type>string</local-type>
<aggregation>Count</aggregation>
<width>255</width>
<contains-null>true</contains-null>
<attributes>
<attribute datatype='string' name='DebugRemoteType'>&quot;SQL_WVARCHAR&quot;</attribute>
<attribute datatype='string' name='DebugWireType'>&quot;SQL_C_WCHAR&quot;</attribute>
<attribute datatype='string' name='TypeIsVarchar'>&quot;true&quot;</attribute>
</attributes>
</metadata-record>
<metadata-record class='column'>
<remote-name>Today's Date</remote-name>
<remote-type>130</remote-type>
<local-name>[Today&apos;s Date]</local-name>
<parent-name>[xy]</parent-name>
<remote-alias>a</remote-alias>
<ordinal>1</ordinal>
<local-type>string</local-type>
<aggregation>Count</aggregation>
<width>255</width>
<contains-null>true</contains-null>
<attributes>
<attribute datatype='string' name='DebugRemoteType'>&quot;SQL_WVARCHAR&quot;</attribute>
<attribute datatype='string' name='DebugWireType'>&quot;SQL_C_WCHAR&quot;</attribute>
<attribute datatype='string' name='TypeIsVarchar'>&quot;true&quot;</attribute>
</attributes>
</metadata-record>
<metadata-record class='column'>
<remote-name>x</remote-name>
<remote-type>3</remote-type>
<local-name>[x]</local-name>
<parent-name>[xy]</parent-name>
<remote-alias>x</remote-alias>
<ordinal>2</ordinal>
<local-type>integer</local-type>
<aggregation>Sum</aggregation>
<precision>10</precision>
<contains-null>true</contains-null>
<attributes>
<attribute datatype='string' name='DebugRemoteType'>&quot;SQL_INTEGER&quot;</attribute>
<attribute datatype='string' name='DebugWireType'>&quot;SQL_C_SLONG&quot;</attribute>
</attributes>
</metadata-record>
<metadata-record class='column'>
<remote-name>y</remote-name>
<remote-type>3</remote-type>
<local-name>[y]</local-name>
<parent-name>[xy]</parent-name>
<remote-alias>y</remote-alias>
<ordinal>3</ordinal>
<local-type>integer</local-type>
<aggregation>Sum</aggregation>
<precision>10</precision>
<contains-null>true</contains-null>
<attributes>
<attribute datatype='string' name='DebugRemoteType'>&quot;SQL_INTEGER&quot;</attribute>
<attribute datatype='string' name='DebugWireType'>&quot;SQL_C_SLONG&quot;</attribute>
</attributes>
</metadata-record>
</metadata-records>
</connection>
<aliases enabled='yes' />
<column datatype='integer' name='[Number of Records]' role='measure' type='quantitative' user:auto-column='numrec'>
<calculation class='tableau' formula='1' />
</column>
<column caption='A' datatype='string' name='[a]' role='dimension' type='nominal'>
<desc>
<formatted-text>
<run bold='true' fontsize='96'>A thing</run>
<run fontcolor='#686868'>&#10;Something will go here too, in a muted gray</run>
</formatted-text>
</desc>
</column>
<column caption='Today&apos;s Date' datatype='string' name='[Today&apos;s Date]' role='dimension' type='nominal' />
<column caption='X' datatype='integer' name='[x]' role='measure' type='ordinal' />
<column caption='Y' datatype='integer' name='[y]' role='measure' type='quantitative' />
<layout dim-ordering='alphabetic' dim-percentage='0.5' measure-ordering='alphabetic' measure-percentage='0.5' show-structure='true' />
<semantic-values>
<semantic-value key='[Country].[Name]' value='&quot;United States&quot;' />
</semantic-values>
</datasource>
18 changes: 18 additions & 0 deletions test/bvt.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):

Expand Down
13 changes: 13 additions & 0 deletions test/test_datasource.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):

Expand Down