Skip to content

Commit 35b5500

Browse files
authored
Merge pull request #100 from jaybythebay/fix-target-directory
CLI: Set target directory to absolute to override general working dir…
2 parents 0f2c94f + 7658057 commit 35b5500

File tree

3 files changed

+50
-11
lines changed

3 files changed

+50
-11
lines changed

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
long_description=readme,
1313
long_description_content_type='text/markdown',
1414
name="tableau_utilities",
15-
version="2.2.14",
15+
version="2.2.15",
1616
requires_python=">=3.8",
1717
packages=[
1818
'tableau_utilities',

tableau_utilities/scripts/cli.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,10 +454,39 @@ def set_datasource_connection_args(args):
454454
def main():
455455
args = parser.parse_args()
456456

457+
# Set absolute paths before the working directory change
458+
457459
# Set absolute path of the settings_path, if it exists and is not already absolute
458460
if not os.path.isabs(args.settings_path) and os.path.exists(args.settings_path):
459461
args.settings_path = os.path.abspath(args.settings_path)
460462

463+
if args.definitions_csv and not os.path.isabs(args.definitions_csv):
464+
args.definitions_csv = os.path.abspath(args.definitions_csv)
465+
466+
# Set absolute path of the target_directory, if it exists and is not already absolute
467+
if args.command == 'merge_config' and args.target_directory and not os.path.isabs(args.target_directory):
468+
args.target_directory = os.path.abspath(args.target_directory)
469+
470+
# Set absolute path of the existing config, if it exists and is not already absolute
471+
if args.command == 'merge_config' and args.existing_config and not os.path.isabs(args.existing_config):
472+
args.existing_config = os.path.abspath(args.existing_config)
473+
474+
# Set absolute path of the additional config, if it exists and is not already absolute
475+
if args.command == 'merge_config' and args.additional_config and not os.path.isabs(args.additional_config):
476+
args.additional_config = os.path.abspath(args.additional_config)
477+
478+
# Set absolute path of the additional config, if it exists and is not already absolute
479+
if args.command == 'merge_config' and args.merged_config and not os.path.isabs(args.merged_config):
480+
args.merged_config = os.path.abspath(args.merged_config)
481+
482+
# Set absolute path of the column config to apply, if it exists and is not already absolute
483+
if args.command == 'apply_configs' and args.column_config and not os.path.isabs(args.column_config):
484+
args.column_config = os.path.abspath(args.column_config)
485+
486+
# Set absolute path of the column config to apply, if it exists and is not already absolute
487+
if args.command == 'apply_configs' and args.calculated_column_config and not os.path.isabs(args.calculated_column_config):
488+
args.calculated_column_config = os.path.abspath(args.calculated_column_config)
489+
461490
# Set args from connection group, from settings YAML / Environment Variables, when not provided in the command
462491
set_datasource_connection_args(args)
463492

tableau_utilities/scripts/merge_config.py

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import json
2+
import os
23
from tableau_utilities.general.cli_styling import Color, Symbol
34
from tableau_utilities.scripts.gen_config import load_csv_with_definitions, generate_config
45

@@ -15,6 +16,7 @@ def read_file(file_path):
1516
Returns:
1617
dict: The JSON content as a dictionary.
1718
"""
19+
1820
try:
1921
with open(file_path, "r") as infile:
2022
config = json.load(infile)
@@ -38,6 +40,10 @@ def write_file(file_name, config, debugging_logs=False):
3840
config: The dictionary to write to the file
3941
4042
"""
43+
44+
# Ensure the file_name is an absolute path
45+
file_name = os.path.abspath(file_name)
46+
4147
with open(file_name, "w") as outfile:
4248
json.dump(config, outfile)
4349

@@ -180,19 +186,21 @@ def merge_configs(args, server=None):
180186
existing_config_path = args.existing_config
181187
additional_config_path = args.additional_config
182188
definitions_csv_path = args.definitions_csv
183-
# definitions_csv_local_name_path = args.definitions_csv_local_name
184189
merge_with = args.merge_with
185-
file_name = f'{args.merged_config}.json'
186190
target_directory = args.target_directory
187191
debugging_logs = args.debugging_logs
188192
existing_config = args.existing_config
193+
merged_config_path = args.merged_config
189194

190195

191196
# Merge 2 configs
192197
if merge_with == 'config':
193-
read_merge_write(existing_config_path, additional_config_path, file_name, debugging_logs)
198+
read_merge_write(existing_config_path=existing_config_path,
199+
additional_config_path=additional_config_path,
200+
output_config_path=merged_config_path,
201+
debugging_logs=debugging_logs)
194202

195-
# Merge a config with a definitions csv. This
203+
# Merge a config with a definitions csv.
196204
elif merge_with == 'csv':
197205
# Log paths
198206
if debugging_logs:
@@ -202,16 +210,16 @@ def merge_configs(args, server=None):
202210
f'{COLOR.fg_grey}{definitions_csv_path}{COLOR.reset}')
203211

204212
# Read files
205-
existing_config = read_file(existing_config)
213+
existing_config_content = read_file(existing_config)
206214
definitions_mapping = load_csv_with_definitions(file=definitions_csv_path, debugging_logs=debugging_logs)
207215

208216
# Merge
209-
new_config = add_definitions_mapping_any_local_name(existing_config, definitions_mapping)
217+
new_config = add_definitions_mapping_any_local_name(existing_config_content, definitions_mapping)
210218

211219
# Sort and write the merged config
212220
new_config = sort_config(new_config, debugging_logs)
213221

214-
write_file(file_name=file_name, config=new_config, debugging_logs=debugging_logs)
222+
write_file(file_name=existing_config, config=new_config, debugging_logs=debugging_logs)
215223

216224
print(f'{COLOR.fg_yellow}DEFINITIONS CSV {SYMBOL.arrow_r} '
217225
f'{COLOR.fg_grey}{definitions_csv_path}{COLOR.reset}')
@@ -220,7 +228,7 @@ def merge_configs(args, server=None):
220228
print(f'{COLOR.fg_yellow}ADDITIONAL CONFIG {SYMBOL.arrow_r} '
221229
f'{COLOR.fg_grey}{additional_config_path}{COLOR.reset}')
222230
print(f'{COLOR.fg_green}{SYMBOL.success} MERGED CONFIG {SYMBOL.arrow_r} '
223-
f'{COLOR.fg_grey}{file_name}{COLOR.reset}')
231+
f'{COLOR.fg_grey}{existing_config}{COLOR.reset}')
224232

225233
elif merge_with == 'generate_merge_all':
226234
# Generate the configs and return the paths of where they are
@@ -229,8 +237,10 @@ def merge_configs(args, server=None):
229237
f'{COLOR.fg_grey}{new_column_config_path} {SYMBOL.sep} {new_calculated_column_config_path}{COLOR.reset}')
230238
print(f'{COLOR.fg_yellow}TARGET DIRECTORY {SYMBOL.arrow_r} {COLOR.fg_grey}{target_directory}{COLOR.reset}')
231239

232-
existing_column_config_path = f'{target_directory}column_config.json'
233-
existing_calc_config_path = f'{target_directory}tableau_calc_config.json'
240+
# Set the full path for the target directory for files to merge with and output to
241+
# This overrides the working directory setting where temporary files are stored
242+
existing_column_config_path = os.path.join(target_directory, 'column_config.json')
243+
existing_calc_config_path = os.path.join(target_directory, 'tableau_calc_config.json')
234244

235245
read_merge_write(existing_config_path=existing_column_config_path,
236246
additional_config_path=new_column_config_path,

0 commit comments

Comments
 (0)