Skip to content

Commit 95d50d7

Browse files
committed
adding initial version of Document API
Includes setup.py along with example of replicating a workbook
1 parent 7295c40 commit 95d50d7

8 files changed

Lines changed: 1340 additions & 0 deletions

File tree

Document API/Examples/Replicate Workbook/Sample - Superstore.twb

Lines changed: 920 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
DBFriendlyName,Server,Database,User
2+
DB-001,SQL.SERVER.120,spdb-001,louisebrown
3+
DB-002,SQL.SERVER.127,spdb-002,brendadavis
4+
DB-003,SQL.SERVER.193,spdb-003,helensimmons
5+
DB-004,SQL.SERVER.126,spdb-004,lorirussell
6+
DB-005,SQL.SERVER.173,spdb-005,beverlysmith
7+
DB-006,SQL.SERVER.176,spdb-006,phyllisclark
8+
DB-007,SQL.SERVER.181,spdb-007,debramurphy
9+
DB-008,SQL.SERVER.191,spdb-008,jeffreywood
10+
DB-009,SQL.SERVER.174,spdb-009,richardramirez
11+
DB-010,SQL.SERVER.115,spdb-010,christinamiller
12+
DB-011,SQL.SERVER.176,spdb-011,larrywashington
13+
DB-012,SQL.SERVER.106,spdb-012,martinadams
14+
DB-013,SQL.SERVER.133,spdb-013,joycewilliams
15+
DB-014,SQL.SERVER.177,spdb-014,andrewbailey
16+
DB-015,SQL.SERVER.157,spdb-015,randybryant
17+
DB-016,SQL.SERVER.180,spdb-016,frankevans
18+
DB-017,SQL.SERVER.147,spdb-017,josephallen
19+
DB-018,SQL.SERVER.175,spdb-018,thomasbaker
20+
DB-019,SQL.SERVER.115,spdb-019,ericfoster
21+
DB-020,SQL.SERVER.155,spdb-020,janeking
22+
DB-021,SQL.SERVER.141,spdb-021,edwardmorris
23+
DB-022,SQL.SERVER.189,spdb-022,shawnjackson
24+
DB-023,SQL.SERVER.139,spdb-023,stephanieperez
25+
DB-024,SQL.SERVER.164,spdb-024,melissabarnes
26+
DB-025,SQL.SERVER.106,spdb-025,alangarcia
27+
DB-026,SQL.SERVER.199,spdb-026,juliahenderson
28+
DB-027,SQL.SERVER.104,spdb-027,jackmorgan
29+
DB-028,SQL.SERVER.103,spdb-028,catherineward
30+
DB-029,SQL.SERVER.159,spdb-029,chriswatson
31+
DB-030,SQL.SERVER.147,spdb-030,janetflores
32+
DB-031,SQL.SERVER.149,spdb-031,donaldlee
33+
DB-032,SQL.SERVER.167,spdb-032,jessicascott
34+
DB-033,SQL.SERVER.152,spdb-033,craigroberts
35+
DB-034,SQL.SERVER.104,spdb-034,laurahoward
36+
DB-035,SQL.SERVER.145,spdb-035,stevenreed
37+
DB-036,SQL.SERVER.143,spdb-036,bobbylong
38+
DB-037,SQL.SERVER.182,spdb-037,christopherprice
39+
DB-038,SQL.SERVER.124,spdb-038,clarenceanderson
40+
DB-039,SQL.SERVER.149,spdb-039,heatherpowell
41+
DB-040,SQL.SERVER.176,spdb-040,sarahmartin
42+
DB-041,SQL.SERVER.106,spdb-041,ashleyrivera
43+
DB-042,SQL.SERVER.121,spdb-042,billywright
44+
DB-043,SQL.SERVER.171,spdb-043,mariapeterson
45+
DB-044,SQL.SERVER.173,spdb-044,jimmybennett
46+
DB-045,SQL.SERVER.126,spdb-045,kevingonzalez
47+
DB-046,SQL.SERVER.110,spdb-046,anthonylopez
48+
DB-047,SQL.SERVER.134,spdb-047,tinacox
49+
DB-048,SQL.SERVER.136,spdb-048,annetorres
50+
DB-049,SQL.SERVER.176,spdb-049,carolyntaylor
51+
DB-050,SQL.SERVER.180,spdb-050,judywilson
52+
DB-051,SQL.SERVER.168,spdb-051,marthathomas
53+
DB-052,SQL.SERVER.121,spdb-052,sarakelly
54+
DB-053,SQL.SERVER.160,spdb-053,marysanchez
55+
DB-054,SQL.SERVER.191,spdb-054,kathleenalexander
56+
DB-055,SQL.SERVER.192,spdb-055,haroldhernandez
57+
DB-056,SQL.SERVER.185,spdb-056,kathrynjames
58+
DB-057,SQL.SERVER.180,spdb-057,scottmartinez
59+
DB-058,SQL.SERVER.154,spdb-058,gloriapatterson
60+
DB-059,SQL.SERVER.199,spdb-059,adamrodriguez
61+
DB-060,SQL.SERVER.194,spdb-060,francescooper
62+
DB-061,SQL.SERVER.135,spdb-061,samueldiaz
63+
DB-062,SQL.SERVER.127,spdb-062,denisemitchell
64+
DB-063,SQL.SERVER.178,spdb-063,royrogers
65+
DB-064,SQL.SERVER.197,spdb-064,theresalewis
66+
DB-065,SQL.SERVER.174,spdb-065,williamrichardson
67+
DB-066,SQL.SERVER.154,spdb-066,justinedwards
68+
DB-067,SQL.SERVER.108,spdb-067,katherinenelson
69+
DB-068,SQL.SERVER.186,spdb-068,wandajohnson
70+
DB-069,SQL.SERVER.177,spdb-069,lindahall
71+
DB-070,SQL.SERVER.138,spdb-070,tammywhite
72+
DB-071,SQL.SERVER.112,spdb-071,raymondgray
73+
DB-072,SQL.SERVER.117,spdb-072,nicholasharris
74+
DB-073,SQL.SERVER.196,spdb-073,shirleybutler
75+
DB-074,SQL.SERVER.113,spdb-074,benjaminjones
76+
DB-075,SQL.SERVER.174,spdb-075,janiceparker
77+
DB-076,SQL.SERVER.191,spdb-076,lisaphillips
78+
DB-077,SQL.SERVER.146,spdb-077,philipwalker
79+
DB-078,SQL.SERVER.116,spdb-078,margaretcollins
80+
DB-079,SQL.SERVER.148,spdb-079,dianestewart
81+
DB-080,SQL.SERVER.112,spdb-080,nancygonzales
82+
DB-081,SQL.SERVER.191,spdb-081,nicolegriffin
83+
DB-082,SQL.SERVER.136,spdb-082,mildredsanders
84+
DB-083,SQL.SERVER.162,spdb-083,cynthiacampbell
85+
DB-084,SQL.SERVER.147,spdb-084,pamelabell
86+
DB-085,SQL.SERVER.157,spdb-085,jacquelineross
87+
DB-086,SQL.SERVER.195,spdb-086,kimberlyhughes
88+
DB-087,SQL.SERVER.114,spdb-087,sandrayoung
89+
DB-088,SQL.SERVER.157,spdb-088,jennifergreen
90+
DB-089,SQL.SERVER.119,spdb-089,jasonperry
91+
DB-090,SQL.SERVER.135,spdb-090,rubycook
92+
DB-091,SQL.SERVER.185,spdb-091,bonniebrooks
93+
DB-092,SQL.SERVER.198,spdb-092,henrythompson
94+
DB-093,SQL.SERVER.164,spdb-093,louiscarter
95+
DB-094,SQL.SERVER.177,spdb-094,joanhill
96+
DB-095,SQL.SERVER.156,spdb-095,markrobinson
97+
DB-096,SQL.SERVER.120,spdb-096,andreaturner
98+
DB-097,SQL.SERVER.148,spdb-097,paulajenkins
99+
DB-098,SQL.SERVER.132,spdb-098,douglascoleman
100+
DB-099,SQL.SERVER.126,spdb-099,emilymoore
101+
DB-100,SQL.SERVER.143,spdb-100,charles
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import csv # so we can work with our database list (in a CSV file)
2+
import copy # to make copies
3+
4+
############################################################
5+
# Step 1) Use Workbook object from the Document API
6+
############################################################
7+
from tableaudocumentapi import Workbook
8+
9+
############################################################
10+
# Step 2) Open the .twb we want to replicate
11+
############################################################
12+
sourceWB = Workbook('Sample - Superstore.twb')
13+
14+
############################################################
15+
# Step 3) Use a database list (in CSV), loop thru and
16+
# create new .twb's with their settings
17+
############################################################
18+
with open('databases.csv') as csvfile:
19+
next(csvfile) # Skip the first line which is our CSV header row
20+
databases = csv.reader(csvfile, delimiter=',', quotechar='"')
21+
for row in databases:
22+
newWB = copy.copy(sourceWB)
23+
24+
# Set our unique values for this database
25+
newWB.datasources[0].connection.server = row[1] # Server
26+
newWB.datasources[0].connection.dbname = row[2] # Database
27+
newWB.datasources[0].connection.username = row[3] # User
28+
newWB.save_as(row[0] + ' - Superstore' + '.twb') # Save our newly created .twb with the new file name

Document API/setup.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
try:
2+
from setuptools import setup
3+
except ImportError:
4+
from distutils.core import setup
5+
6+
setup(
7+
name='tableaudocumentapi',
8+
version='0.0.1',
9+
summary='A Python module for working with Tableau files.',
10+
author='Tableau Software',
11+
author_email='github@tableau.com',
12+
url='https://github.com/tableau/tableausdk-python',
13+
py_modules=['tableaudocumentapi'],
14+
license='MIT',
15+
description='A Python module for working with Tableau files.'
16+
)
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
__version__ = '0.0.1'
2+
__VERSION__ = __version__
3+
from .connection import Connection
4+
from .datasource import Datasource
5+
from .workbook import Workbook
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
###############################################################################
2+
#
3+
# Connection - A class for writing connections to Tableau files
4+
#
5+
###############################################################################
6+
class Connection(object):
7+
"""
8+
A class for writing connections to Tableau files.
9+
10+
"""
11+
12+
###########################################################################
13+
#
14+
# Public API.
15+
#
16+
###########################################################################
17+
18+
def __init__(self, connxml):
19+
"""
20+
Constructor.
21+
22+
"""
23+
self._connectionXML = connxml
24+
self._dbname = connxml.get('dbname')
25+
self._server = connxml.get('server')
26+
self._username = connxml.get('username')
27+
28+
###########
29+
# dbname
30+
###########
31+
@property
32+
def dbname(self):
33+
return self._dbname
34+
35+
@dbname.setter
36+
def dbname(self, value):
37+
"""
38+
Set the connection's database name property.
39+
40+
Args:
41+
value: New name of the database. String.
42+
43+
Returns:
44+
Nothing.
45+
46+
"""
47+
self._dbname = value
48+
self._connectionXML.set('dbname', value)
49+
50+
###########
51+
# server
52+
###########
53+
@property
54+
def server(self):
55+
return self._server
56+
57+
@server.setter
58+
def server(self, value):
59+
"""
60+
Set the connection's server property.
61+
62+
Args:
63+
value: New server. String.
64+
65+
Returns:
66+
Nothing.
67+
68+
"""
69+
self._server = value
70+
self._connectionXML.set('server', value)
71+
72+
###########
73+
# username
74+
###########
75+
@property
76+
def username(self):
77+
return self._username
78+
79+
@username.setter
80+
def username(self, value):
81+
"""
82+
Set the connection's username property.
83+
84+
Args:
85+
value: New username value. String.
86+
87+
Returns:
88+
Nothing.
89+
90+
"""
91+
self._username = value
92+
self._connectionXML.set('username', value)
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
###############################################################################
2+
#
3+
# Datasource - A class for writing datasources to Tableau files
4+
#
5+
###############################################################################
6+
import xml.etree.ElementTree as ET
7+
from tableaudocumentapi import Connection
8+
9+
class Datasource(object):
10+
"""
11+
A class for writing datasources to Tableau files.
12+
13+
"""
14+
15+
###########################################################################
16+
#
17+
# Public API.
18+
#
19+
###########################################################################
20+
def __init__(self, dsxml):
21+
"""
22+
Constructor. Default is to create datasource from xml.
23+
24+
"""
25+
self._datasourceXML = dsxml
26+
self._name = self._datasourceXML.get('name')
27+
self._version = self._datasourceXML.get('version')
28+
self._connection = Connection(self._datasourceXML.find('connection'))
29+
30+
@classmethod
31+
def from_file(cls, filename):
32+
"Initialize datasource from file (.tds)"
33+
dsxml = ET.parse(filename).getroot()
34+
return cls(dsxml)
35+
36+
###########
37+
# name
38+
###########
39+
@property
40+
def name(self):
41+
return self._name
42+
43+
###########
44+
# version
45+
###########
46+
@property
47+
def version(self):
48+
return self._version
49+
50+
###########
51+
# connection
52+
###########
53+
@property
54+
def connection(self):
55+
return self._connection

0 commit comments

Comments
 (0)