8000 My first crack at a ConnectionParser. Tests pass but aren't pretty ye… · PBrad/document-api-python@e0ceed5 · GitHub
[go: up one dir, main page]

Skip to content
8000

Commit e0ceed5

Browse files
committed
My first crack at a ConnectionParser. Tests pass but aren't pretty yet. I'm not sure if I like this approach or not.
1 parent 1d07ff3 commit e0ceed5

File tree

2 files changed

+30
-4
lines changed

2 files changed

+30
-4
lines changed

tableaudocumentapi/datasource.py

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,26 @@
66
import xml.etree.ElementTree as ET
77
from tableaudocumentapi import Connection
88

9+
class ConnectionParser(object):
10+
11+
def __init__(self, datasource_xml, version):
12+
self._dsxml = datasource_xml
13+
self._dsversion = version
14+
15+
def get_connections(self):
16+
if float(self._dsversion) < 10:
17+
connections = self._extract_legacy_connection()
18+
else:
19+
connections = self._extract_federated_connections()
20+
return connections
21+
22+
def _extract_federated_connections(self):
23+
return list(map(Connection,self._dsxml.findall('.//named-connections/named-connection/*')))
24+
25+
def _extract_legacy_connection(self):
26+
return Connection(self._dsxml.find('connection'))
27+
28+
929
class Datasource(object):
1030
"""
1131
A class for writing datasources to Tableau files.
@@ -27,10 +47,8 @@ def __init__(self, dsxml, filename=None):
2747
self._datasourceTree = ET.ElementTree(self._datasourceXML)
2848
self._name = self._datasourceXML.get('name') or self._datasourceXML.get('formatted-name') # TDS files don't have a name attribute
2949
self._version = self._datasourceXML.get('version')
30-
if self._version == '10.0':
31-
self._connection = list(map(Connection,self._datasourceXML.findall('.//named-connections/named-connection/*')))
32-
else:
33-
self._connection = Connection(self._datasourceXML.find('connection'))
50+
self._connection_parser = ConnectionParser(self._datasourceXML, version=self._version)
51+
self._connection = self._connection_parser.get_connections()
3452

3553
@classmethod
3654
def from_file(cls, filename):

test.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,13 @@ def test_is_valid_file_with_invalid_inputs(self):
3939
self.assertFalse(Workbook._is_valid_file('file1.tds2'))
4040
self.assertFalse(Workbook._is_valid_file('file2.twb3'))
4141

42+
class ConnectionParserTests(unittest.TestCase):
43+
44+
def test_can_extract_legacy_connection(self):
45+
pass
46+
47+
def test_can_extract_federated_connections(self):
48+
pass
4249

4350
class ConnectionModelTests(unittest.TestCase):
4451

@@ -128,6 +135,7 @@ def test_can_update_datasource_connection_and_saveV10(self):
128135
self.assertEqual(new_wb.datasources[0].connection[0].dbname, 'newdb.test.tsi.lan')
129136

130137
temp.close()
138+
os.unlink(temp.name)
131139

132140
def test_can_extract_datasourceV10(self):
133141
temp = io.FileIO('v10test.twb', 'w')

0 commit comments

Comments
 (0)
0