Skip to content

Commit

Permalink
Testing out the full pass through to make changes to data source prop…
Browse files Browse the repository at this point in the history
…erties then save a TWBX
  • Loading branch information
Bryant Howell committed Dec 5, 2019
1 parent ee39299 commit 99c1e58
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 19 deletions.
22 changes: 9 additions & 13 deletions tableau_documents/tableau_datasource.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def __init__(self, datasource_xml: Optional[ET.Element] = None, logger_obj: Opti

self.logger = logger_obj
self._connections = []
self.ds_name = None
self._ds_name = None
self.ds_version_type = None
self._ds_version = None
self._published = False
Expand Down Expand Up @@ -120,9 +120,9 @@ def __init__(self, datasource_xml: Optional[ET.Element] = None, logger_obj: Opti
else:
self.xml = datasource_xml
if self.xml.get("caption"):
self.ds_name = self.xml.attrib["caption"]
self._ds_name = self.xml.attrib["caption"]
elif self.xml.get("name"):
self.ds_name = self.xml.attrib['name']
self._ds_name = self.xml.attrib['name']
xml_version = self.xml.attrib['version']
# Determine whether it is a 9 style or 10 style federated datasource
if xml_version in ['9.0', '9.1', '9.2', '9.3']:
Expand Down Expand Up @@ -190,9 +190,6 @@ def __init__(self, datasource_xml: Optional[ET.Element] = None, logger_obj: Opti
c = e.find('connection')
self.existing_tde_filename = c.get('dbname')

# To make work as tableau_document from TableauFile
# self._datasources.append(self)

self._columns = None
# Possible, though unlikely, that there would be no columns
if self.xml.find('column') is not None:
Expand All @@ -202,13 +199,13 @@ def __init__(self, datasource_xml: Optional[ET.Element] = None, logger_obj: Opti
self._extract_filename = None
self.ds_generator = None

#@property
#def datasources(self) -> List[TableauDatasource]:
# return self._datasources
@property
def ds_name(self) -> str:
return self._ds_name

#@property
#def document_type(self) -> str:
# return self._document_type
@ds_name.setter
def ds_name(self, new_name: str):
self.xml.set("caption", new_name)

@property
def tde_filename(self) -> str:
Expand Down Expand Up @@ -271,7 +268,6 @@ def update_tables_with_new_database_or_schema(self, original_db_or_schema: str,
relation.set('table', relation.get('table').replace("[{}]".format(original_db_or_schema),
"[{}]".format(new_db_or_schema)))


@staticmethod
def create_new_datasource_xml(version: str) -> ET.Element:
# nsmap = {u"user": u'http://www.tableausoftware.com/xml/user'}
Expand Down
13 changes: 8 additions & 5 deletions tableau_documents/tableau_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -646,7 +646,7 @@ def save_new_file(self, new_filename_no_extension: str):
new_filename = new_filename_no_extension
self.log('Saving to a file with new filename {}'.format(new_filename))

initial_save_filename = "{}.{}".format(new_filename, self._final_file_type)
initial_save_filename = "{}.{}".format(new_filename, 'tdsx')
# Make sure you don't overwrite the existing original file
files = list(filter(os.path.isfile, os.listdir(os.curdir))) # files only
save_filename = initial_save_filename
Expand Down Expand Up @@ -758,7 +758,7 @@ def save_new_file(self, new_filename_no_extension: str):
new_filename = new_filename_no_extension
self.log('Saving to a file with new filename {}'.format(new_filename))

initial_save_filename = "{}.{}".format(new_filename, self._final_file_type)
initial_save_filename = "{}.{}".format(new_filename, 'twbx')
# Make sure you don't overwrite the existing original file
files = list(filter(os.path.isfile, os.listdir(os.curdir))) # files only
save_filename = initial_save_filename
Expand All @@ -774,18 +774,21 @@ def save_new_file(self, new_filename_no_extension: str):
# with that filename so it can insert in any changed datasources
self.log('Creating from original file {}'.format(self.orig_filename))
file_obj = open(self.orig_filename, 'rb')
temp_wb_filename = 'temp.twb'
o_zf = zipfile.ZipFile(file_obj)
o_zf.extract(self.tableau_document.twb_filename)
shutil.copy(self.tableau_document.twb_filename, 'temp.twb')
shutil.copy(self.tableau_document.twb_filename, temp_wb_filename)
os.remove(self.tableau_document.twb_filename)
self.tableau_document.twb_filename = 'temp.twb'
self.tableau_document.twb_filename = temp_wb_filename
file_obj.close()

# Call to the TableauXmlFile object to write the file to disk
self.tableau_xml_file.save_new_file(filename_no_extension=self.packaged_filename)
new_zf.write(self.packaged_filename)
self.log('Removing file {}'.format(self.packaged_filename))

# Clean up the new file and the temp file
os.remove(self.packaged_filename)
os.remove(temp_wb_filename)

temp_directories_to_remove = {}

Expand Down
3 changes: 2 additions & 1 deletion tableau_documents/tableau_workbook.py
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ def get_xml_string(self) -> str:
final_datasources = self.datasources
for ds in final_datasources:
self.log('Writing datasource XML into the workbook')
ds_string = ds.get_datasource_xml()
ds_string = ds.get_xml_string()
print(ds_string)
if isinstance(ds_string, bytes):
final_string = ds_string.decode('utf-8')
else:
Expand Down

0 comments on commit 99c1e58

Please sign in to comment.