Skip to content

Commit 2c605e5

Browse files
committed
PEP8-ify some files so future editors will be happy. Also add a test file and add some test cases for a help method I plan to update in the next commit. I made _is_valid_file a static method since it's just a helper and it makes it easier to test
1 parent 31aca0e commit 2c605e5

4 files changed

Lines changed: 47 additions & 27 deletions

File tree

Document API/tableaudocumentapi/connection.py

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
# Connection - A class for writing connections to Tableau files
44
#
55
###############################################################################
6+
7+
68
class Connection(object):
79
"""
810
A class for writing connections to Tableau files.
@@ -36,13 +38,13 @@ def dbname(self):
3638
def dbname(self, value):
3739
"""
3840
Set the connection's database name property.
39-
41+
4042
Args:
4143
value: New name of the database. String.
42-
44+
4345
Returns:
4446
Nothing.
45-
47+
4648
"""
4749
self._dbname = value
4850
self._connectionXML.set('dbname', value)
@@ -58,17 +60,17 @@ def server(self):
5860
def server(self, value):
5961
"""
6062
Set the connection's server property.
61-
63+
6264
Args:
6365
value: New server. String.
64-
66+
6567
Returns:
6668
Nothing.
67-
69+
6870
"""
6971
self._server = value
7072
self._connectionXML.set('server', value)
71-
73+
7274
###########
7375
# username
7476
###########
@@ -80,13 +82,13 @@ def username(self):
8082
def username(self, value):
8183
"""
8284
Set the connection's username property.
83-
85+
8486
Args:
8587
value: New username value. String.
86-
88+
8789
Returns:
8890
Nothing.
89-
91+
9092
"""
9193
self._username = value
9294
self._connectionXML.set('username', value)

Document API/tableaudocumentapi/datasource.py

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

9+
910
class Datasource(object):
1011
"""
1112
A class for writing datasources to Tableau files.
@@ -26,20 +27,20 @@ def __init__(self, dsxml):
2627
self._name = self._datasourceXML.get('name')
2728
self._version = self._datasourceXML.get('version')
2829
self._connection = Connection(self._datasourceXML.find('connection'))
29-
30+
3031
@classmethod
3132
def from_file(cls, filename):
3233
"Initialize datasource from file (.tds)"
3334
dsxml = ET.parse(filename).getroot()
3435
return cls(dsxml)
35-
36+
3637
###########
3738
# name
3839
###########
3940
@property
4041
def name(self):
4142
return self._name
42-
43+
4344
###########
4445
# version
4546
###########

Document API/tableaudocumentapi/workbook.py

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import xml.etree.ElementTree as ET
88
from tableaudocumentapi import Datasource
99

10+
1011
class Workbook(object):
1112
"""
1213
A class for writing Tableau workbook files.
@@ -29,13 +30,14 @@ def __init__(self, filename):
2930
self._filename = filename
3031
self._workbookTree = ET.parse(filename)
3132
self._workbookRoot = self._workbookTree.getroot()
32-
33+
3334
# prepare our datasource objects
34-
self._datasources = self._prepare_datasources(self._workbookRoot) #self.workbookRoot.find('datasources')
35+
self._datasources = self._prepare_datasources(
36+
self._workbookRoot) # self.workbookRoot.find('datasources')
3537
else:
3638
print('Invalid file type. Must be .twb or .tds.')
3739
raise Exception()
38-
40+
3941
@classmethod
4042
def from_file(cls, filename):
4143
"Initialize datasource from file (.tds)"
@@ -46,14 +48,14 @@ def from_file(cls, filename):
4648
else:
4749
print('Invalid file type. Must be .twb or .tds.')
4850
raise Exception()
49-
51+
5052
###########
5153
# datasources
5254
###########
5355
@property
5456
def datasources(self):
5557
return self._datasources
56-
58+
5759
###########
5860
# filename
5961
###########
@@ -72,10 +74,10 @@ def save(self):
7274
Nothing.
7375
7476
"""
75-
77+
7678
# save the file
7779
self._workbookTree.write(self._filename)
78-
80+
7981
def save_as(self, value):
8082
"""
8183
Save our file with the name provided.
@@ -87,7 +89,7 @@ def save_as(self, value):
8789
Nothing.
8890
8991
"""
90-
92+
9193
# We have a valid type of input file
9294
if self._is_valid_file(value):
9395
# save the file
@@ -103,21 +105,20 @@ def save_as(self, value):
103105
###########################################################################
104106
def _prepare_datasources(self, xmlRoot):
105107
datasources = []
106-
108+
107109
# loop through our datasources and append
108110
for datasource in xmlRoot.find('datasources'):
109111
ds = Datasource(datasource)
110112
datasources.append(ds)
111-
113+
112114
return datasources
113-
114-
def _is_valid_file(self, filename):
115+
116+
@staticmethod
117+
def _is_valid_file(filename):
115118
valid = 0
116119
fileExtension = os.path.splitext(filename)[-1].lower()
117-
118120
if fileExtension == ".twb":
119121
valid = 1
120122
elif fileExtension == ".tds":
121123
valid = 1
122-
123124
return valid

Document API/test.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import unittest
2+
3+
from tableaudocumentapi import Workbook
4+
5+
class HelperMethodTests(unittest.TestCase):
6+
7+
def test_valid_file_with_valid_inputs(self):
8+
self.assertTrue(Workbook._is_valid_file('file1.tds'))
9+
self.assertTrue(Workbook._is_valid_file('file2.twb'))
10+
11+
def test_valid_file_with_invalid_inputs(self):
12+
self.assertFalse(Workbook._is_valid_file('file1.tds2'))
13+
self.assertFalse(Workbook._is_valid_file('file2.twb3'))
14+
15+
if __name__ == '__main__':
16+
unittest.main()

0 commit comments

Comments
 (0)