Skip to content
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

Create New Connections per #68 #69

Merged
merged 5 commits into from
Aug 15, 2016
Merged
Show file tree
Hide file tree
Changes from 3 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
14 changes: 13 additions & 1 deletion tableaudocumentapi/connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
# Connection - A class for writing connections to Tableau files
#
###############################################################################
import xml.etree.ElementTree as ET
from tableaudocumentapi.dbclass import is_valid_dbclass


Expand Down Expand Up @@ -33,6 +34,17 @@ def __init__(self, connxml):
def __repr__(self):
return "'<Connection server='{}' dbname='{}' @ {}>'".format(self._server, self._dbname, hex(id(self)))

@classmethod
def from_attributes(cls, server, dbname, username, dbclass, authentication=''):
root = ET.Element('connection', authentication=authentication)
xml = cls(root)
xml.server = server
xml.dbname = dbname
xml.username = username
xml.dbclass = dbclass

return xml

###########
# dbname
###########
Expand Down Expand Up @@ -120,4 +132,4 @@ def dbclass(self, value):
raise AttributeError("'{}' is not a valid database type".format(value))

self._class = value
self._connectionXML.set('dbclass', value)
self._connectionXML.set('class', value)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

Totally a bug in my dbclass code.

25 changes: 25 additions & 0 deletions tableaudocumentapi/datasource.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
###############################################################################
import collections
import itertools
import random
import string
import xml.etree.ElementTree as ET
import xml.sax.saxutils as sax

Expand Down Expand Up @@ -38,6 +40,7 @@ def _is_used_by_worksheet(names, field):


class FieldDictionary(MultiLookupDict):

def used_by_sheet(self, name):
# If we pass in a string, no need to get complicated, just check to see if name is in
# the field's list of worksheets
Expand All @@ -63,7 +66,15 @@ def _column_object_from_metadata_xml(metadata_xml):
return _ColumnObjectReturnTuple(field_object.id, field_object)


def make_unique_name(dbclass):
rand_part = ''.join(random.choice(
Copy link
Contributor

Choose a reason for hiding this comment

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

Our unique names are a hash based on the current time, so we should probably do that rather than random data. I can find the relevant code if you need it.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Please!

string.ascii_lowercase + string.digits) for _ in range(28))
name = dbclass + '.' + rand_part
return name


class ConnectionParser(object):

def __init__(self, datasource_xml, version):
self._dsxml = datasource_xml
self._dsversion = version
Expand Down Expand Up @@ -116,6 +127,20 @@ def from_file(cls, filename):
dsxml = xml_open(filename, cls.__name__.lower()).getroot()
return cls(dsxml, filename)

@classmethod
def from_scratch(cls, caption, connections):
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this ever called by the end user? I'm not sold on this name.

Copy link
Contributor Author

@t8y8 t8y8 Aug 9, 2016

Choose a reason for hiding this comment

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

Yeah, it'd be what you call, I just changed it to from_connections when I fixed a bug in my next commit, though that name isn't sexy either

root = ET.Element('datasource', caption=caption, version='10.0')
Copy link
Contributor

Choose a reason for hiding this comment

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

I feel like we are going to do this pattern a bunch. Would it make more sense to build up a logical model that we can then automatically serialize into the xml in some way rather than hard coding specific xml manipulations?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, I was thinking about some sort of DSBuilder or something, the direct XML feels gross.

It would also allow us to specify different versions (at least 9.3 vs 10)

outer_connection = ET.SubElement(root, 'connection')
outer_connection.set('class', 'federated')
named_conns = ET.SubElement(outer_connection, 'named-connections')
for conn in connections:
nc = ET.SubElement(named_conns,
'named-connection',
name=make_unique_name(conn.dbclass),
caption=conn.server)
nc.append(conn._connectionXML)
return cls(root)

def save(self):
"""
Call finalization code and save file.
Expand Down
19 changes: 19 additions & 0 deletions test/bvt.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,25 @@ def test_bad_dbclass_rasies_attribute_error(self):
with self.assertRaises(AttributeError):
conn.dbclass = 'NotReal'

def test_can_create_connection_from_scratch(self):
conn = Connection.from_attributes(
server='a', dbname='b', username='c', dbclass='mysql', authentication='d')
self.assertEqual(conn.server, 'a')
self.assertEqual(conn.dbname, 'b')
self.assertEqual(conn.username, 'c')
self.assertEqual(conn.dbclass, 'mysql')
self.assertEqual(conn.authentication, 'd')

def test_can_create_datasource_from_scratch(self):
conn1 = Connection.from_attributes(
server='a', dbname='b', username='c', dbclass='mysql', authentication='d')
conn2 = Connection.from_attributes(
server='1', dbname='2', username='3', dbclass='mysql', authentication='7')
ds = Datasource.from_scratch('test', connections=[conn1, conn2])

self.assertEqual(ds.connections[0].server, 'a')
self.assertEqual(ds.connections[1].server, '1')


class DatasourceModelTests(unittest.TestCase):

Expand Down