11import json
2+ import os
23from tableau_utilities .general .cli_styling import Color , Symbol
34from 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