Skip to content

Commit 059fe86

Browse files
Russell HayRussell Hay
authored andcommitted
tableau#86 Repository location stripping (tableau#88)
* Add the ability to clear repository location on the datasource for retargetting * fixing a pep8 issue that was missed * Adding None check for repository-location in case it doesn't exist
1 parent 200c3f8 commit 059fe86

File tree

5 files changed

+133
-96
lines changed

5 files changed

+133
-96
lines changed

tableaudocumentapi/datasource.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -214,6 +214,11 @@ def version(self):
214214
def connections(self):
215215
return self._connections
216216

217+
def clear_repository_location(self):
218+
tag = self._datasourceXML.find('./repository-location')
219+
if tag is not None:
220+
self._datasourceXML.remove(tag)
221+
217222
###########
218223
# fields
219224
###########

tableaudocumentapi/xfile.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,10 @@ def save_into_archive(xml_tree, filename, new_filename=None):
105105

106106
def _save_file(container_file, xml_tree, new_filename=None):
107107

108-
if container_file is None:
109-
container_file = new_filename
108+
if new_filename is None:
109+
new_filename = container_file
110110

111111
if zipfile.is_zipfile(container_file):
112112
save_into_archive(xml_tree, container_file, new_filename)
113113
else:
114-
xml_tree.write(container_file, encoding="utf-8", xml_declaration=True)
114+
xml_tree.write(new_filename, encoding="utf-8", xml_declaration=True)

test/assets/datasource_test.tds

Lines changed: 94 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -1,93 +1,94 @@
1-
<?xml version='1.0' encoding='utf-8' ?>
2-
<datasource formatted-name='postgres.1of3kl00aoax5d1a1ejma1397430' inline='true' source-platform='mac' version='9.3' xmlns:user='http://www.tableausoftware.com/xml/user'>
3-
<connection authentication='username-password' class='postgres' dbname='TestV1' odbc-native-protocol='yes' port='5432' server='postgres91.test.tsi.lan' username='test'>
4-
<relation name='xy' table='[public].[xy]' type='table' />
5-
<metadata-records>
6-
<metadata-record class='column'>
7-
<remote-name>a</remote-name>
8-
<remote-type>130</remote-type>
9-
<local-name>[a]</local-name>
10-
<parent-name>[xy]</parent-name>
11-
<remote-alias>a</remote-alias>
12-
<ordinal>1</ordinal>
13-
<local-type>string</local-type>
14-
<aggregation>Count</aggregation>
15-
<width>255</width>
16-
<contains-null>true</contains-null>
17-
<attributes>
18-
<attribute datatype='string' name='DebugRemoteType'>&quot;SQL_WVARCHAR&quot;</attribute>
19-
<attribute datatype='string' name='DebugWireType'>&quot;SQL_C_WCHAR&quot;</attribute>
20-
<attribute datatype='string' name='TypeIsVarchar'>&quot;true&quot;</attribute>
21-
</attributes>
22-
</metadata-record>
23-
<metadata-record class='column'>
24-
<remote-name>Today's Date</remote-name>
25-
<remote-type>130</remote-type>
26-
<local-name>[Today&apos;s Date]</local-name>
27-
<parent-name>[xy]</parent-name>
28-
<remote-alias>a</remote-alias>
29-
<ordinal>1</ordinal>
30-
<local-type>string</local-type>
31-
<aggregation>Count</aggregation>
32-
<width>255</width>
33-
<contains-null>true</contains-null>
34-
<attributes>
35-
<attribute datatype='string' name='DebugRemoteType'>&quot;SQL_WVARCHAR&quot;</attribute>
36-
<attribute datatype='string' name='DebugWireType'>&quot;SQL_C_WCHAR&quot;</attribute>
37-
<attribute datatype='string' name='TypeIsVarchar'>&quot;true&quot;</attribute>
38-
</attributes>
39-
</metadata-record>
40-
<metadata-record class='column'>
41-
<remote-name>x</remote-name>
42-
<remote-type>3</remote-type>
43-
<local-name>[x]</local-name>
44-
<parent-name>[xy]</parent-name>
45-
<remote-alias>x</remote-alias>
46-
<ordinal>2</ordinal>
47-
<local-type>integer</local-type>
48-
<aggregation>Sum</aggregation>
49-
<precision>10</precision>
50-
<contains-null>true</contains-null>
51-
<attributes>
52-
<attribute datatype='string' name='DebugRemoteType'>&quot;SQL_INTEGER&quot;</attribute>
53-
<attribute datatype='string' name='DebugWireType'>&quot;SQL_C_SLONG&quot;</attribute>
54-
</attributes>
55-
</metadata-record>
56-
<metadata-record class='column'>
57-
<remote-name>y</remote-name>
58-
<remote-type>3</remote-type>
59-
<local-name>[y]</local-name>
60-
<parent-name>[xy]</parent-name>
61-
<remote-alias>y</remote-alias>
62-
<ordinal>3</ordinal>
63-
<local-type>integer</local-type>
64-
<aggregation>Sum</aggregation>
65-
<precision>10</precision>
66-
<contains-null>true</contains-null>
67-
<attributes>
68-
<attribute datatype='string' name='DebugRemoteType'>&quot;SQL_INTEGER&quot;</attribute>
69-
<attribute datatype='string' name='DebugWireType'>&quot;SQL_C_SLONG&quot;</attribute>
70-
</attributes>
71-
</metadata-record>
72-
</metadata-records>
73-
</connection>
74-
<aliases enabled='yes' />
75-
<column datatype='integer' name='[Number of Records]' role='measure' type='quantitative' user:auto-column='numrec'>
76-
<calculation class='tableau' formula='1' />
77-
</column>
78-
<column caption='A' datatype='string' name='[a]' role='dimension' type='nominal'>
79-
<desc>
80-
<formatted-text>
81-
<run bold='true' fontsize='96'>A thing</run>
82-
<run fontcolor='#686868'>&#10;Something will go here too, in a muted gray</run>
83-
</formatted-text>
84-
</desc>
85-
</column>
86-
<column caption='Today&apos;s Date' datatype='string' name='[Today&apos;s Date]' role='dimension' type='nominal' />
87-
<column caption='X' datatype='integer' name='[x]' role='measure' type='ordinal' />
88-
<column caption='Y' datatype='integer' name='[y]' role='measure' type='quantitative' />
89-
<layout dim-ordering='alphabetic' dim-percentage='0.5' measure-ordering='alphabetic' measure-percentage='0.5' show-structure='true' />
90-
<semantic-values>
91-
<semantic-value key='[Country].[Name]' value='&quot;United States&quot;' />
92-
</semantic-values>
93-
</datasource>
1+
<?xml version='1.0' encoding='utf-8' ?>
2+
<datasource formatted-name='postgres.1of3kl00aoax5d1a1ejma1397430' inline='true' source-platform='mac' version='9.3' xmlns:user='http://www.tableausoftware.com/xml/user'>
3+
<repository-location />
4+
<connection authentication='username-password' class='postgres' dbname='TestV1' odbc-native-protocol='yes' port='5432' server='postgres91.test.tsi.lan' username='test'>
5+
<relation name='xy' table='[public].[xy]' type='table' />
6+
<metadata-records>
7+
<metadata-record class='column'>
8+
<remote-name>a</remote-name>
9+
<remote-type>130</remote-type>
10+
<local-name>[a]</local-name>
11+
<parent-name>[xy]</parent-name>
12+
<remote-alias>a</remote-alias>
13+
<ordinal>1</ordinal>
14+
<local-type>string</local-type>
15+
<aggregation>Count</aggregation>
16+
<width>255</width>
17+
<contains-null>true</contains-null>
18+
<attributes>
19+
<attribute datatype='string' name='DebugRemoteType'>&quot;SQL_WVARCHAR&quot;</attribute>
20+
<attribute datatype='string' name='DebugWireType'>&quot;SQL_C_WCHAR&quot;</attribute>
21+
<attribute datatype='string' name='TypeIsVarchar'>&quot;true&quot;</attribute>
22+
</attributes>
23+
</metadata-record>
24+
<metadata-record class='column'>
25+
<remote-name>Today's Date</remote-name>
26+
<remote-type>130</remote-type>
27+
<local-name>[Today&apos;s Date]</local-name>
28+
<parent-name>[xy]</parent-name>
29+
<remote-alias>a</remote-alias>
30+
<ordinal>1</ordinal>
31+
<local-type>string</local-type>
32+
<aggregation>Count</aggregation>
33+
<width>255</width>
34+
<contains-null>true</contains-null>
35+
<attributes>
36+
<attribute datatype='string' name='DebugRemoteType'>&quot;SQL_WVARCHAR&quot;</attribute>
37+
<attribute datatype='string' name='DebugWireType'>&quot;SQL_C_WCHAR&quot;</attribute>
38+
<attribute datatype='string' name='TypeIsVarchar'>&quot;true&quot;</attribute>
39+
</attributes>
40+
</metadata-record>
41+
<metadata-record class='column'>
42+
<remote-name>x</remote-name>
43+
<remote-type>3</remote-type>
44+
<local-name>[x]</local-name>
45+
<parent-name>[xy]</parent-name>
46+
<remote-alias>x</remote-alias>
47+
<ordinal>2</ordinal>
48+
<local-type>integer</local-type>
49+
<aggregation>Sum</aggregation>
50+
<precision>10</precision>
51+
<contains-null>true</contains-null>
52+
<attributes>
53+
<attribute datatype='string' name='DebugRemoteType'>&quot;SQL_INTEGER&quot;</attribute>
54+
<attribute datatype='string' name='DebugWireType'>&quot;SQL_C_SLONG&quot;</attribute>
55+
</attributes>
56+
</metadata-record>
57+
<metadata-record class='column'>
58+
<remote-name>y</remote-name>
59+
<remote-type>3</remote-type>
60+
<local-name>[y]</local-name>
61+
<parent-name>[xy]</parent-name>
62+
<remote-alias>y</remote-alias>
63+
<ordinal>3</ordinal>
64+
<local-type>integer</local-type>
65+
<aggregation>Sum</aggregation>
66+
<precision>10</precision>
67+
<contains-null>true</contains-null>
68+
<attributes>
69+
<attribute datatype='string' name='DebugRemoteType'>&quot;SQL_INTEGER&quot;</attribute>
70+
<attribute datatype='string' name='DebugWireType'>&quot;SQL_C_SLONG&quot;</attribute>
71+
</attributes>
72+
</metadata-record>
73+
</metadata-records>
74+
</connection>
75+
<aliases enabled='yes' />
76+
<column datatype='integer' name='[Number of Records]' role='measure' type='quantitative' user:auto-column='numrec'>
77+
<calculation class='tableau' formula='1' />
78+
</column>
79+
<column caption='A' datatype='string' name='[a]' role='dimension' type='nominal'>
80+
<desc>
81+
<formatted-text>
82+
<run bold='true' fontsize='96'>A thing</run>
83+
<run fontcolor='#686868'>&#10;Something will go here too, in a muted gray</run>
84+
</formatted-text>
85+
</desc>
86+
</column>
87+
<column caption='Today&apos;s Date' datatype='string' name='[Today&apos;s Date]' role='dimension' type='nominal' />
88+
<column caption='X' datatype='integer' name='[x]' role='measure' type='ordinal' />
89+
<column caption='Y' datatype='integer' name='[y]' role='measure' type='quantitative' />
90+
<layout dim-ordering='alphabetic' dim-percentage='0.5' measure-ordering='alphabetic' measure-percentage='0.5' show-structure='true' />
91+
<semantic-values>
92+
<semantic-value key='[Country].[Name]' value='&quot;United States&quot;' />
93+
</semantic-values>
94+
</datasource>

test/bvt.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,6 +187,24 @@ def test_can_save_tds(self):
187187
new_tds = Datasource.from_file(self.tds_file.name)
188188
self.assertEqual(new_tds.connections[0].dbname, 'newdb')
189189

190+
def test_can_save_as_tds(self):
191+
new_filename = os.path.join(
192+
os.path.dirname(self.tds_file.name),
193+
"new_{}".format(os.path.basename(self.tds_file.name))
194+
)
195+
196+
try:
197+
original_tds = Datasource.from_file(self.tds_file.name)
198+
original_tds.connections[0].dbname = 'newdb'
199+
200+
original_tds.save_as(new_filename)
201+
202+
new_tds = Datasource.from_file(new_filename)
203+
self.assertEqual(new_tds.connections[0].dbname, 'newdb')
204+
finally:
205+
if os.path.exists(new_filename):
206+
os.unlink(new_filename)
207+
190208

191209
class DatasourceModelV10TDSXTests(unittest.TestCase):
192210

test/test_datasource.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,19 @@ def test_datasource_field_description(self):
6363
self.assertIsNotNone(actual)
6464
self.assertTrue(u'muted gray' in actual)
6565

66+
def test_datasource_clear_repository_location(self):
67+
filename = os.path.join(TEST_ASSET_DIR, 'clear-repository-test.tds')
68+
69+
self.assertIsNotNone(self.ds._datasourceXML.find('.//repository-location'))
70+
self.ds.clear_repository_location()
71+
try:
72+
self.ds.save_as(filename)
73+
with open(filename, 'r') as newfile:
74+
self.assertFalse('repository-location' in newfile.read())
75+
finally:
76+
if os.path.exists(filename):
77+
os.unlink(filename)
78+
6679

6780
class DataSourceFieldsTWB(unittest.TestCase):
6881

0 commit comments

Comments
 (0)
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