Skip to content

Commit

Permalink
Cleaned up script after documenting github corner. The template is ba…
Browse files Browse the repository at this point in the history
…ck to normal now
  • Loading branch information
cjsostad committed Oct 29, 2024
1 parent 0cbaceb commit 9ce73b7
Showing 1 changed file with 0 additions and 187 deletions.
187 changes: 0 additions & 187 deletions map_automation_scripts_snippets/arcgis_pro_3_toolbox_template.pyt
Original file line number Diff line number Diff line change
Expand Up @@ -40,194 +40,7 @@ class NameOfYourTool(object):
def getParameterInfo(self):
"""This function assigns parameter information for tool"""

ef setup_logging():
''' Set up logging for the script '''
# Create the log folder filename
log_folder = f'autoast_logs_{datetime.datetime.now().strftime("%Y%m%d")}'

# Create the log folder in the current directory if it doesn't exits
if not os.path.exists(log_folder):
os.mkdir(log_folder)

# Check if the log folder was created successfully
assert os.path.exists(log_folder), "Error creating log folder, check permissions and path"

# Create the log file path with the date and time appended
log_file = os.path.join(log_folder, f'ast_log_{datetime.datetime.now().strftime("%Y%m%d_%H%M%S")}.log')



# Set up logging config to DEBUG level
logging.basicConfig(filename=log_file,
level=logging.DEBUG,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')

# Create the logger object and set to the current file name
logger = logging.getLogger(__name__)

print("Logging set up")
logger.info("Logging set up")

print("Starting Script")
logger.info("Starting Script")

return logger
###############################################################################################################################################################################



def import_ast(logger):
# Get the toolbox path from environment variables
ast_toolbox = os.getenv('TOOLBOX') # File path

if ast_toolbox is None:
print("Unable to find the toolbox. Check the path in .env file")
logger.error("Unable to find the toolbox. Check the path in .env file")
exit()

# Import the toolbox
try:
arcpy.ImportToolbox(ast_toolbox)
print(f"AST Toolbox imported successfully.")
logger.info(f"AST Toolbox imported successfully.")
except Exception as e:
print(f"Error importing toolbox: {e}")
logger.error(f"Error importing toolbox: {e}")
exit()

# Assign the shapefile template for FW Setup to a variable
template = os.getenv('TEMPLATE') # File path in .env
if template is None:
print("Unable to find the template. Check the path in .env file")
logger.error("Unable to find the template. Check the path in .env file")

return template


###############################################################################################################################################################################
#
# Set up the database connection
#
###############################################################################################################################################################################
def setup_bcgw(logger):
# Get the secret file containing the database credentials
SECRET_FILE = os.getenv('SECRET_FILE')

# If secret file found, load the secret file and display a print message, if not found display an error message
if SECRET_FILE:
load_dotenv(SECRET_FILE)
print(f"Secret file {SECRET_FILE} found")
logger.info(f"Secret file {SECRET_FILE} found")
else:
print("Secret file not found")
logger.error("Secret file not found")

# Assign secret file data to variables
DB_USER = os.getenv('BCGW_USER')
DB_PASS = os.getenv('BCGW_PASS')

# If DB_USER and DB_PASS found display a print message, if not found display an error message
if DB_USER and DB_PASS:
print(f"Database user {DB_USER} and password found")
logger.info(f"Database user {DB_USER} and password found")
else:
print("Database user and password not found")
logger.error("Database user and password not found")

# Define current path of the executing script
current_path = os.path.dirname(os.path.realpath(__file__))

# Create the connection folder
connection_folder = 'connection'
connection_folder = os.path.join(current_path, connection_folder)

# Check for the existance of the connection folder and if it doesn't exist, print an error and create a new connection folder
if not os.path.exists(connection_folder):
print("Connection folder not found, creating new connection folder")
logger.info("Connection folder not found, creating new connection folder")
os.mkdir(connection_folder)

# Check for an existing bcgw connection, if there is one, remove it
if os.path.exists(os.path.join(connection_folder, 'bcgw.sde')):
os.remove(os.path.join(connection_folder, 'bcgw.sde'))

# Create a bcgw connection
bcgw_con = arcpy.management.CreateDatabaseConnection(connection_folder,
'bcgw.sde',
'ORACLE',
'bcgw.bcgov/idwprod1.bcgov',
'DATABASE_AUTH',
DB_USER,
DB_PASS,
'DO_NOT_SAVE_USERNAME')

print("new db connection created")
logger.info("new db connection created")


arcpy.env.workspace = bcgw_con.getOutput(0)

print("workspace set to bcgw connection")
logger.info("workspace set to bcgw connection")

secrets = [DB_USER, DB_PASS]

return secrets
###############################################################################################################################################################################

class AST_FACTORY:
''' AST_FACTORY creates and manages status tool runs '''
XLSX_SHEET_NAME = 'ast_config'
AST_PARAMETERS = {
0: 'region',
1: 'feature_layer',
2: 'crown_file_number',
3: 'disposition_number',
4: 'parcel_number',
5: 'output_directory',
6: 'output_directory_same_as_input',
7: 'dont_overwrite_outputs',
8: 'skip_conflicts_and_constraints',
9: 'suppress_map_creation',
10: 'add_maps_to_current',
11: 'run_as_fcbc',

}

ADDITIONAL_PARAMETERS = {
12: 'ast_condition',
13: 'file_number'
}

AST_CONDITION_COLUMN = 'ast_condition'
DONT_OVERWRITE_OUTPUTS = 'dont_overwrite_outputs'
AST_SCRIPT = ''
job_index = None # Initialize job_index as a global variable

def __init__(self, queuefile, db_user, db_pass, logger=None, current_path=None) -> None:
self.user = db_user
self.user_cred = db_pass
self.queuefile = queuefile
self.jobs = []
self.logger = logger or logging.getLogger(__name__)
self.current_path = current_path

def load_jobs(self):
'''
load jobs will check for the existence of the queuefile, if it exists it will load the jobs from the queuefile. Checking if they
are Complete and if not, it will add them to the jobs as Queued
'''

#global job_index
self.logger.info("##########################################################################################################################")
self.logger.info("#")
self.logger.info("Loading Jobs...")
self.logger.info("#")
self.logger.info("##########################################################################################################################")

# Initialize the jobs list to store jobs
self.jobs = []

#This parameter is the file number of the application
file_num = arcpy.Parameter(
displayName = "Lands File Number",
Expand Down

0 comments on commit 9ce73b7

Please sign in to comment.