seanpedrickcase commited on
Commit
1418017
·
1 Parent(s): 7b345c3

Made changes to hopefully resolve issue with downloading cost centre details from S3 to container

Browse files
Files changed (3) hide show
  1. app.py +2 -2
  2. tools/aws_functions.py +14 -3
  3. tools/config.py +7 -2
app.py CHANGED
@@ -648,7 +648,7 @@ with app:
648
 
649
  # If relevant environment variable is set, load in the default allow list file from S3 or locally. Even when setting S3 path, need to local path to give a download location
650
  if GET_DEFAULT_ALLOW_LIST == "True" and (ALLOW_LIST_PATH or S3_ALLOW_LIST_PATH):
651
- if not os.path.exists(ALLOW_LIST_PATH) and S3_ALLOW_LIST_PATH:
652
  app.load(download_file_from_s3, inputs=[s3_default_bucket, s3_default_allow_list_file, default_allow_list_output_folder_location]).\
653
  success(load_in_default_allow_list, inputs = [default_allow_list_output_folder_location], outputs=[in_allow_list])
654
  print("Successfully loaded allow list from S3")
@@ -659,7 +659,7 @@ with app:
659
 
660
  # If relevant environment variable is set, load in the default cost code file from S3 or locally
661
  if GET_COST_CODES == "True" and (COST_CODES_PATH or S3_COST_CODES_PATH):
662
- if not os.path.exists(COST_CODES_PATH) and S3_COST_CODES_PATH:
663
  app.load(download_file_from_s3, inputs=[s3_default_bucket, s3_default_cost_codes_file, default_cost_codes_output_folder_location]).\
664
  success(load_in_default_cost_codes, inputs = [default_cost_codes_output_folder_location, default_cost_code_textbox], outputs=[cost_code_dataframe, cost_code_dataframe_base, cost_code_choice_drop])
665
  print("Successfully loaded cost codes from S3")
 
648
 
649
  # If relevant environment variable is set, load in the default allow list file from S3 or locally. Even when setting S3 path, need to local path to give a download location
650
  if GET_DEFAULT_ALLOW_LIST == "True" and (ALLOW_LIST_PATH or S3_ALLOW_LIST_PATH):
651
+ if not os.path.exists(ALLOW_LIST_PATH) and S3_ALLOW_LIST_PATH and RUN_AWS_FUNCTIONS == 1:
652
  app.load(download_file_from_s3, inputs=[s3_default_bucket, s3_default_allow_list_file, default_allow_list_output_folder_location]).\
653
  success(load_in_default_allow_list, inputs = [default_allow_list_output_folder_location], outputs=[in_allow_list])
654
  print("Successfully loaded allow list from S3")
 
659
 
660
  # If relevant environment variable is set, load in the default cost code file from S3 or locally
661
  if GET_COST_CODES == "True" and (COST_CODES_PATH or S3_COST_CODES_PATH):
662
+ if not os.path.exists(COST_CODES_PATH) and S3_COST_CODES_PATH and RUN_AWS_FUNCTIONS == 1:
663
  app.load(download_file_from_s3, inputs=[s3_default_bucket, s3_default_cost_codes_file, default_cost_codes_output_folder_location]).\
664
  success(load_in_default_cost_codes, inputs = [default_cost_codes_output_folder_location, default_cost_code_textbox], outputs=[cost_code_dataframe, cost_code_dataframe_base, cost_code_choice_drop])
665
  print("Successfully loaded cost codes from S3")
tools/aws_functions.py CHANGED
@@ -40,9 +40,20 @@ if RUN_AWS_FUNCTIONS == "1":
40
  def download_file_from_s3(bucket_name:str, key:str, local_file_path_and_name:str, RUN_AWS_FUNCTIONS:str = RUN_AWS_FUNCTIONS):
41
 
42
  if RUN_AWS_FUNCTIONS == "1":
43
- s3 = boto3.client('s3', region_name=AWS_REGION)
44
- s3.download_file(bucket_name, key, local_file_path_and_name)
45
- print(f"File downloaded from s3://{bucket_name}/{key} to {local_file_path_and_name}")
 
 
 
 
 
 
 
 
 
 
 
46
 
47
 
48
  def download_folder_from_s3(bucket_name:str, s3_folder:str, local_folder:str, RUN_AWS_FUNCTIONS:str = RUN_AWS_FUNCTIONS):
 
40
  def download_file_from_s3(bucket_name:str, key:str, local_file_path_and_name:str, RUN_AWS_FUNCTIONS:str = RUN_AWS_FUNCTIONS):
41
 
42
  if RUN_AWS_FUNCTIONS == "1":
43
+
44
+ try:
45
+ print("bucket_name:", bucket_name)
46
+ print("key:", key)
47
+ print("local_file_path_and_name:", local_file_path_and_name)
48
+
49
+ # Ensure the local directory exists
50
+ os.makedirs(os.path.dirname(local_file_path_and_name), exist_ok=True)
51
+
52
+ s3 = boto3.client('s3', region_name=AWS_REGION)
53
+ s3.download_file(bucket_name, key, local_file_path_and_name)
54
+ print(f"File downloaded from s3://{bucket_name}/{key} to {local_file_path_and_name}")
55
+ except Exception as e:
56
+ print("Could not download file:", key, "from s3 due to", e)
57
 
58
 
59
  def download_folder_from_s3(bucket_name:str, s3_folder:str, local_folder:str, RUN_AWS_FUNCTIONS:str = RUN_AWS_FUNCTIONS):
tools/config.py CHANGED
@@ -59,6 +59,8 @@ def add_folder_to_path(folder_path: str):
59
  else:
60
  print(f"Folder not found at {folder_path} - not added to PATH")
61
 
 
 
62
  # If you have an aws_config env file in the config folder, you can load in app variables this way, e.g. 'config/app_config.env'
63
  APP_CONFIG_PATH = get_or_create_env_var('APP_CONFIG_PATH', 'config/app_config.env') # e.g. config/app_config.env
64
 
@@ -204,7 +206,10 @@ REDACTION_LANGUAGE = get_or_create_env_var("REDACTION_LANGUAGE", "en") # Current
204
  ###
205
 
206
  TLDEXTRACT_CACHE = get_or_create_env_var('TLDEXTRACT_CACHE', 'tld/.tld_set_snapshot')
207
- extract = TLDExtract(cache_dir=TLDEXTRACT_CACHE)
 
 
 
208
 
209
  # Get some environment variables and Launch the Gradio app
210
  COGNITO_AUTH = get_or_create_env_var('COGNITO_AUTH', '0')
@@ -232,7 +237,7 @@ else: OUTPUT_ALLOW_LIST_PATH = 'config/default_allow_list.csv'
232
 
233
  SHOW_COSTS = get_or_create_env_var('SHOW_COSTS', 'False')
234
 
235
- GET_COST_CODES = get_or_create_env_var('GET_COST_CODES', 'False')
236
 
237
  DEFAULT_COST_CODE = get_or_create_env_var('DEFAULT_COST_CODE', '')
238
 
 
59
  else:
60
  print(f"Folder not found at {folder_path} - not added to PATH")
61
 
62
+ ensure_folder_exists("config/")
63
+
64
  # If you have an aws_config env file in the config folder, you can load in app variables this way, e.g. 'config/app_config.env'
65
  APP_CONFIG_PATH = get_or_create_env_var('APP_CONFIG_PATH', 'config/app_config.env') # e.g. config/app_config.env
66
 
 
206
  ###
207
 
208
  TLDEXTRACT_CACHE = get_or_create_env_var('TLDEXTRACT_CACHE', 'tld/.tld_set_snapshot')
209
+ try:
210
+ extract = TLDExtract(cache_dir=TLDEXTRACT_CACHE)
211
+ except:
212
+ extract = TLDExtract(cache_dir=None)
213
 
214
  # Get some environment variables and Launch the Gradio app
215
  COGNITO_AUTH = get_or_create_env_var('COGNITO_AUTH', '0')
 
237
 
238
  SHOW_COSTS = get_or_create_env_var('SHOW_COSTS', 'False')
239
 
240
+ GET_COST_CODES = get_or_create_env_var('GET_COST_CODES', 'True')
241
 
242
  DEFAULT_COST_CODE = get_or_create_env_var('DEFAULT_COST_CODE', '')
243