diff --git a/setup.py b/setup.py index 1026b7e..0784961 100644 --- a/setup.py +++ b/setup.py @@ -2,7 +2,7 @@ setup( name='tableau_tools', - version='4.3.12', + version='4.3.13', packages=['tableau_tools', 'tableau_tools.tableau_rest_api', 'tableau_tools.tableau_documents', 'tableau_tools.examples'], url='https://github.com/bryantbhowell/tableau_tools', license='', diff --git a/tableau_documents/tableau_datasource.py b/tableau_documents/tableau_datasource.py index f1195a8..7f01cc3 100644 --- a/tableau_documents/tableau_datasource.py +++ b/tableau_documents/tableau_datasource.py @@ -85,8 +85,13 @@ def __init__(self, datasource_xml=None, logger_obj=None, ds_version=None): for published_datasource in published_datasources: self.log(u'Published Datasource connection tags found, building a TableauConnection object') self.connections.append(TableauConnection(published_datasource)) - self.relation_xml_obj = self.xml.find(u'.//relation', self.ns_map) - self.read_existing_relations() + + # Skip the relation if it is a Parameters datasource. Eventually, build out separate object + if self.xml.get(u'name') != u'Parameters': + self.relation_xml_obj = self.xml.find(u'.//relation', self.ns_map) + self.read_existing_relations() + else: + self.log(u'Found a Parameters datasource') self.repository_location = None diff --git a/tableau_documents/tableau_file.py b/tableau_documents/tableau_file.py index 0c41cc5..ee27416 100644 --- a/tableau_documents/tableau_file.py +++ b/tableau_documents/tableau_file.py @@ -131,6 +131,7 @@ def save_new_file(self, new_filename_no_extension): new_filename = new_filename_no_extension.split('.')[0] # simple algorithm to kill extension if new_filename is None: new_filename = new_filename_no_extension + self.log(u'Saving to a file with new filename {}'.format(new_filename)) # Change filetype if there are new extracts to add for ds in self.tableau_document.datasources: if ds.tde_filename is not None: @@ -146,11 +147,20 @@ def save_new_file(self, new_filename_no_extension): break if self._final_file_type in [u'twbx', u'tdsx']: - save_filename = u"{}.{}".format(new_filename, self._final_file_type) - new_zf = zipfile.ZipFile(save_filename, 'w') + initial_save_filename = u"{}.{}".format(new_filename, self._final_file_type) + # Make sure you don't overwrite the existing original file + files = filter(os.path.isfile, os.listdir(os.curdir)) # files only + save_filename = initial_save_filename + file_versions = 1 + while save_filename in files: + name_parts = initial_save_filename.split(u".") + save_filename = u"{} ({}).{}".format(name_parts[0],file_versions, name_parts[1]) + file_versions += 1 + new_zf = zipfile.ZipFile(save_filename, 'w', zipfile.ZIP_DEFLATED) # Save the object down self.log(u'Creating temporary XML file {}'.format(self.packaged_filename)) # Have to extract the original TWB to temporary file + self.log(u'Creating from original file {}'.format(self.orig_filename)) if self._original_file_type == u'twbx': file_obj = open(self.orig_filename, 'rb') o_zf = zipfile.ZipFile(file_obj) @@ -172,30 +182,28 @@ def save_new_file(self, new_filename_no_extension): if len(self.other_files) > 0: file_obj = open(self.orig_filename, 'rb') o_zf = zipfile.ZipFile(file_obj) + + # Find datasources with new extracts, and skip their files + extracts_to_skip = [] + for ds in self.tableau_document.datasources: + if ds.existing_tde_filename is not None and ds.tde_filename is not None: + extracts_to_skip.append(ds.existing_tde_filename) + for filename in self.other_files: - self.log(u'Extracting file {} temporarily'.format(filename)) - - if filename.find(u'.tde') != -1: - for ds in self.tableau_document.datasources: - if ds.existing_tde_filename == filename: - # If extract and nothing to replace, just write the existing file - if ds.tde_filename is None: - o_zf.extract(filename) - new_zf.write(filename) - os.remove(filename) - self.log(u'Removed file {}'.format(filename)) - lowest_level = filename.split('/') - temp_directories_to_remove[lowest_level[0]] = True - break - - else: - o_zf.extract(filename) - new_zf.write(filename) - os.remove(filename) - self.log(u'Removed file {}'.format(filename)) - lowest_level = filename.split('/') - temp_directories_to_remove[lowest_level[0]] = True - file_obj.close() + self.log(u'Looking into additional files: {}'.format(filename)) + + # Skip extracts listed for replacement + if filename in extracts_to_skip: + self.log(u'File {} is from an extract that has been replaced, skipping'.format(filename)) + continue + + o_zf.extract(filename) + new_zf.write(filename) + os.remove(filename) + self.log(u'Removed file {}'.format(filename)) + lowest_level = filename.split('/') + temp_directories_to_remove[lowest_level[0]] = True + file_obj.close() # If new extract, write that file for ds in self.tableau_document.datasources: if ds.tde_filename is not None: @@ -211,6 +219,15 @@ def save_new_file(self, new_filename_no_extension): return save_filename else: - save_filename = u"{}.{}".format(new_filename_no_extension, self.file_type) + initial_save_filename = u"{}.{}".format(new_filename_no_extension, self.file_type) + # Make sure you don't overwrite the existing original file + files = filter(os.path.isfile, os.listdir(os.curdir)) # files only + save_filename = initial_save_filename + file_versions = 1 + while save_filename in files: + name_parts = initial_save_filename.split(u".") + save_filename = u"{} ({}).{}".format(name_parts[0],file_versions, name_parts[1]) + file_versions += 1 + self.tableau_document.save_file(save_filename) return save_filename