Skip to content

Commit

Permalink
Added option of specifying a customer target directory
Browse files Browse the repository at this point in the history
  • Loading branch information
kcartier-wri committed Aug 23, 2024
1 parent 350b304 commit e1976fc
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
WorldPop
)
from tests.resources.bbox_constants import BBOX_BR_LAURO_DE_FREITAS_1
from tools.general_tools import create_temp_folder
from tools.general_tools import create_temp_folder, create_folder

# RUN_DUMPS is the master control for whether the writes and tests are executed
# Setting RUN_DUMPS to True turns on code execution.
Expand All @@ -34,20 +34,28 @@
# Both the tests and QGIS file are implemented for the same bounding box in Brazil.
COUNTRY_CODE_FOR_BBOX = 'BRA'
BBOX = BBOX_BR_LAURO_DE_FREITAS_1
# Specify None to write to a temporary default folder otherwise specify a valid custom target path.
CUSTOM_DUMP_DIRECTORY = None

@pytest.mark.skipif(RUN_DUMPS == False, reason='Skipping since RUN_DUMPS set to False')
def test_geotiff_writing():
qgis_project_file = 'layers_for_br_lauro_de_freitas.qgs'

source_folder = os.path.dirname(__file__)
output_temp_folder = create_temp_folder('test_result_tif_files', True)

if CUSTOM_DUMP_DIRECTORY is None:
output_temp_folder = create_temp_folder('test_result_tif_files', True)
else:
output_temp_folder = create_folder(CUSTOM_DUMP_DIRECTORY, True)

source_qgis_file = os.path.join(source_folder, qgis_project_file)
target_qgis_file = os.path.join(output_temp_folder, qgis_project_file)
shutil.copyfile(source_qgis_file, target_qgis_file)

process_layers(output_temp_folder)

print("\n\033[93mQGIS project file and layer files were written to the %s folder.\033[0m" % output_temp_folder)


def process_layers(output_temp_folder):
write_albedo(output_temp_folder)
Expand Down
33 changes: 27 additions & 6 deletions tools/general_tools.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,37 @@
import os
import tempfile
import shutil

def create_temp_folder(sub_directory_name: str, delete_existing_files: bool):
scratch_dir_name = tempfile.TemporaryDirectory().name
dirpath = os.path.dirname(scratch_dir_name)
temp_dir = os.path.join(dirpath, sub_directory_name)

if os.path.isdir(temp_dir) is False:
os.makedirs(temp_dir)
elif delete_existing_files is True:
shutil.rmtree(temp_dir)
os.makedirs(temp_dir)
_create_target_folder(temp_dir, delete_existing_files)

return temp_dir

def create_folder(folder_path, delete_existing_files: bool):
if _is_valid_path(folder_path) is False:
raise ValueError(f"The custom path '%s' is not valid. Stopping." % folder_path)
_create_target_folder(folder_path, delete_existing_files)
return folder_path

def _is_valid_path(path: str):
return os.path.exists(path)

def _create_target_folder(folder_path, delete_existing_files: bool):
if os.path.isdir(folder_path) is False:
os.makedirs(folder_path)
elif delete_existing_files is True:
_remove_all_files_in_directory(folder_path)

def _remove_all_files_in_directory(directory):
# Iterate over all the files in the directory
for filename in os.listdir(directory):
file_path = os.path.join(directory, filename)
try:
# Check if it is a file and remove it
if os.path.isfile(file_path):
os.remove(file_path)
except Exception as e:
print(f"Error: {e}")

0 comments on commit e1976fc

Please sign in to comment.