8000 Move save logic into helper function. Remove unused tests and methods… · onware/document-api-python@8385b5c · GitHub
[go: up one dir, main page]

Skip to content

Commit 8385b5c

Browse files
committed
Move save logic into helper function. Remove unused tests and methods from Workbook class
1 parent bd6d3c9 commit 8385b5c

File tree

4 files changed

+23
-51
lines changed

4 files changed

+23
-51
lines changed

tableaudocumentapi/archivefile.py renamed to tableaudocumentapi/containerfile.py

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def build_archive_file(archive_contents, zip):
4545

4646
def save_into_archive(xml_tree, filename, new_filename=None):
4747
# Saving a archive means extracting the contents into a temp folder,
48-
# saving the changes over the twb in that folder, and then
48+
# saving the changes over the twb/tds in that folder, and then
4949
# packaging it back up into a specifically formatted zip with the correct
5050
# relative file paths
5151

@@ -56,12 +56,22 @@ def save_into_archive(xml_tree, filename, new_filename=None):
5656
with temporary_directory() as temp_path:
5757
file_type = os.path.splitext(filename)[-1].lower()
5858
with zipfile.ZipFile(filename) as zf:
59-
twb_file = find_file_in_zip(zf, file_type)
59+
xml_file = find_file_in_zip(zf, file_type)
6060
zf.extractall(temp_path)
61-
# Write the new version of the twb to the temp directory
61+
# Write the new version of the file to the temp directory
6262
xml_tree.write(os.path.join(
63-
temp_path, twb_file), encoding="utf-8", xml_declaration=True)
63+
temp_path, xml_file), encoding="utf-8", xml_declaration=True)
6464

6565
# Write the new archive with the contents of the temp folder
6666
with zipfile.ZipFile(new_filename, "w", compression=zipfile.ZIP_DEFLATED) as new_archive:
6767
build_archive_file(temp_path, new_archive)
68+
69+
70+
def _save_file(container_file, xml_tree, new_filename=None):
71+
if zipfile.is_zipfile(container_file):
72+
save_into_archive(xml_tree, container_file, new_filename)
73+
else:
74+
xml_tree.write(container_file, encoding="utf-8", xml_declaration=True)
75+
76+
77+

tableaudocumentapi/datasource.py

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import zipfile
88

99
import xml.etree.ElementTree as ET
10-
from tableaudocumentapi import Connection, archivefile
10+
from tableaudocumentapi import Connection, containerfile
1111

1212

1313
class ConnectionParser(object):
@@ -61,7 +61,7 @@ def from_file(cls, filename):
6161
"Initialize datasource from file (.tds)"
6262

6363
if zipfile.is_zipfile(filename):
64-
dsxml = archivefile.get_xml_from_archive(filename).getroot()
64+
dsxml = containerfile.get_xml_from_archive(filename).getroot()
6565
else:
6666
dsxml = ET.parse(filename).getroot()
6767
return cls(dsxml, filename)
@@ -80,11 +80,7 @@ def save(self):
8080

8181
# save the file
8282

83-
if zipfile.is_zipfile(self._filename):
84-
archivefile.save_into_archive(self._datasourceTree, self._filename)
85-
else:
86-
self._datasourceTree.write(
87-
self._filename, encoding="utf-8", xml_declaration=True)
83+
containerfile._save_file(self._filename, self._datasourceTree)
8884

8985
def save_as(self, new_filename):
9086
"""
@@ -97,12 +93,7 @@ def save_as(self, new_filename):
9793
Nothing.
9894
9995
"""
100-
if zipfile.is_zipfile(self._filename):
101-
archivefile.save_into_archive(
102-
self._datasourceTree, self._filename, new_filename)
103-
else:
104-
self._datasourceTree.write(
105-
new_filename, encoding="utf-8", xml_declaration=True)
96+
containerfile._save_file(self._filename, self._datasourceTree, new_filename)
10697

10798
###########
10899
# name

tableaudocumentapi/workbook.py

Lines changed: 5 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
import xml.etree.ElementTree as ET
1010

11-
from tableaudocumentapi import Datasource, archivefile
11+
from tableaudocumentapi import Datasource, containerfile
1212

1313
###########################################################################
1414
#
@@ -37,7 +37,7 @@ def __init__(self, filename):
3737

3838
# Determine if this is a twb or twbx and get the xml root
3939
if zipfile.is_zipfile(self._filename):
40-
self._workbookTree = archivefile.get_xml_from_archive(
40+
self._workbookTree = containerfile.get_xml_from_archive(
4141
self._filename)
4242
else:
4343
self._workbookTree = ET.parse(self._filename)
@@ -74,13 +74,7 @@ def save(self):
7474
"""
7575

7676
# save the file
77-
78-
if zipfile.is_zipfile(self._filename):
79-
archivefile.save_into_archive(
80-
self._workbookTree, filename=self._filename)
81-
else:
82-
self._workbookTree.write(
83-
self._filename, encoding="utf-8", xml_declaration=True)
77+
containerfile._save_file(self._filename, self._workbookTree)
8478

8579
def save_as(self, new_filename):
8680
"""
@@ -93,13 +87,8 @@ def save_as(self, new_filename):
9387
Nothing.
9488
9589
"""
96-
97-
if zipfile.is_zipfile(self._filename):
98-
archivefile.save_into_archive(
99-
self._workbookTree, self._filename, new_filename)
100-
else:
101-
self._workbookTree.write(
102-
new_filename, encoding="utf-8", xml_declaration=True)
90+
containerfile._save_file(
91+
self._filename, self._workbookTree, new_filename)
10392

10493
###########################################################################
10594
#
@@ -115,8 +104,3 @@ def _prepare_datasources(self, xmlRoot):
115104
datasources.append(ds)
116105

117106
return datasources
118-
119-
@staticmethod
120-
def _is_valid_file(filename):
121-
fileExtension = os.path.splitext(filename)[-1].lower()
122-
return fileExtension in ('.twb', '.tds')

test/bvt.py

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -23,19 +23,6 @@
2323
TABLEAU_10_TDSX = os.path.join(TEST_DIR, 'assets', 'TABLEAU_10_TDSX.tdsx')
2424

2525

26-
class HelperMethodTests(unittest.TestCase):
27-
28-
def test_is_valid_file_with_valid_inputs(self):
29-
self.assertTrue(Workbook._is_valid_file('file1.tds'))
30-
self.assertTrue(Workbook._is_valid_file('file2.twb'))
31-
self.assertTrue(Workbook._is_valid_file('tds.twb'))
32-
33-
def test_is_valid_file_with_invalid_inputs(self):
34-
self.assertFalse(Workbook._is_valid_file(''))
35-
self.assertFalse(Workbook._is_valid_file('file1.tds2'))
36-
self.assertFalse(Workbook._is_valid_file('file2.twb3'))
37-
38-
3926
class ConnectionParserTests(unittest.TestCase):
4027

4128
def test_can_extract_legacy_connection(self):

0 commit comments

Comments
 (0)
0