Skip to content

Commit

Permalink
Merge pull request #72 from hoverinc/revert-tableau-file-archive-change
Browse files Browse the repository at this point in the history
Reverted TableauFile archive change, and updated some printing messages
  • Loading branch information
JustinGrilli authored Mar 1, 2024
2 parents 1aa0d96 + aef6a59 commit bf9c835
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 20 deletions.
19 changes: 14 additions & 5 deletions tableau_utilities/scripts/datasource.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import os
import shutil

import tableau_utilities.tableau_file.tableau_file_objects as tfo

from time import time
from tableau_utilities.general.config_column_persona import personas, get_persona_by_attribs, \
get_persona_by_metadata_local_type
from tableau_utilities.general.cli_styling import Color
Expand Down Expand Up @@ -58,15 +59,18 @@ def datasource(args, server=None):

# Downloads the datasource from Tableau Server if the datasource is not local
if location == 'online':
start = time()
print(f'{color.fg_cyan}...Downloading {datasource_name}...{color.reset}')
d = server.get.datasource(datasource_id, datasource_name, project_name)
datasource_path = server.download.datasource(d.id, include_extract=include_extract)
print(f'{color.fg_green}{symbol.success} Downloaded Datasource:', f'{color.fg_yellow}{datasource_path}{color.reset}', '\n')
print(f'{color.fg_green}{symbol.success} (Done in {round(time() - start)} sec) '
f'Downloaded Datasource: {color.fg_yellow}{datasource_path}{color.reset}\n')

datasource_file_name = os.path.basename(datasource_path)
ds = Datasource(datasource_path)

if save_tds:
start = time()
print(f'{color.fg_cyan}...Extracting {datasource_file_name}...{color.reset}')
save_folder = f'{datasource_file_name} - BEFORE'
os.makedirs(save_folder, exist_ok=True)
Expand All @@ -76,7 +80,8 @@ def datasource(args, server=None):
else:
xml_path = ds.unzip(extract_to=save_folder, unzip_all=True)
if debugging_logs:
print(f'{color.fg_green}{symbol.success} BEFORE - TDS SAVED TO: {color.fg_yellow}{xml_path}{color.reset}')
print(f'{color.fg_green}{symbol.success} (Done in {round(time() - start)} sec) '
f'BEFORE - TDS SAVED TO: {color.fg_yellow}{xml_path}{color.reset}')

# List each of the objects specified to list
if list_objects:
Expand Down Expand Up @@ -164,10 +169,13 @@ def datasource(args, server=None):

# Save the datasource if an edit may have happened
if column_name or folder_name or delete or enforce_connection:
start = time()
ds.save()
print(f'{color.fg_green}{symbol.success} Saved changes to: {color.fg_yellow}{datasource_path}{color.reset}')
print(f'{color.fg_green}{symbol.success} (Done in {round(time() - start)} sec) '
f'Saved changes to: {color.fg_yellow}{datasource_path}{color.reset}')

if save_tds:
start = time()
print(f'{color.fg_cyan}...Extracting {datasource_file_name}...{color.reset}')
save_folder = f'{datasource_file_name} - AFTER'
os.makedirs(save_folder, exist_ok=True)
Expand All @@ -177,4 +185,5 @@ def datasource(args, server=None):
else:
xml_path = ds.unzip(extract_to=save_folder, unzip_all=True)
if debugging_logs:
print(f'{color.fg_green}{symbol.success} AFTER - TDS SAVED TO: {color.fg_yellow}{xml_path}{color.reset}')
print(f'{color.fg_green}{symbol.success} (Done in {round(time() - start)} sec) '
f'AFTER - TDS SAVED TO: {color.fg_yellow}{xml_path}{color.reset}')
31 changes: 16 additions & 15 deletions tableau_utilities/tableau_file/tableau_file.py
Original file line number Diff line number Diff line change
Expand Up @@ -97,23 +97,24 @@ def save(self):
temp_path = os.path.join(temp_folder, self.file_basename)
dupe_temp_path = os.path.join(temp_folder, f'__tmp_{self.file_basename}')
shutil.move(self.file_path, temp_path)
# Re-archive the zipped files, minus the XML file
with ZipFile(temp_path) as old:
with ZipFile(dupe_temp_path, 'w') as new:
new.comment = old.comment
for f in old.filelist:
ext = f.filename.split('.')[-1]
if ext in ['tds', 'twb']:
xml_path = os.path.join(temp_folder, f.filename)
continue
new.writestr(f, old.read(f.filename))
# Unzip the zipped files
extracted_files = list()
with ZipFile(temp_path) as z:
for f in z.filelist:
ext = f.filename.split('.')[-1]
path = z.extract(member=f, path=temp_folder)
extracted_files.append(path)
if ext in ['tds', 'twb']:
xml_path = path
# Update XML file
self._tree.write(xml_path, encoding="utf-8", xml_declaration=True)
# Add the XML file to the new archive
with ZipFile(dupe_temp_path, 'a', compression=ZIP_DEFLATED) as new:
new.write(xml_path, arcname=xml_path.split(temp_folder)[-1])
# Move file back to the original folder and remove temp file remnants
shutil.move(dupe_temp_path, self.file_path)
# Repack the unzipped file
with ZipFile(temp_path, 'w') as z:
for file in extracted_files:
arcname = file.split(temp_folder)[-1]
z.write(file, arcname=arcname)
# Move file back to the original folder and remove any unpacked contents
shutil.move(temp_path, self.file_path)
shutil.rmtree(temp_folder)
else:
# Update the Tableau file's contents
Expand Down

0 comments on commit bf9c835

Please sign in to comment.