Select to view content in your preferred language

add an Schedule entry_point

449
2
08-02-2022 04:37 AM
HildermesJoséMedeirosFilho
Occasional Contributor
#!/usr/bin/python
"""-----------------------------------------------------------------------------
Script Name: proschedule.py
Description: schedules automation entry point
Created By: Hildermes Jose Medeiros Filho
Date: 2022-08-01
-----------------------------------------------------------------------------"""
import os
import argparse
from glob import glob
from pathlib import Path
import runpy
from xml.dom.minidom import parseString

SCHEDULES_LIST = []
def main():
"""
Main function it ask for scripts arguments
:return:
"""
args = parseArguments()
run_schedules(number=args.number)


def run_schedules(number: int = -1):
"""
Run one schedule in the SCHEDULES_LIST
:param number: schedule index
:return:
"""
global SCHEDULES_LIST
if len(SCHEDULES_LIST) >= 1:
if number not in range(0, len(SCHEDULES_LIST), 1):
schedules_index_list = [x for x in range(0, len(SCHEDULES_LIST), 1)]
print('--n Invalid schedule number or no schedule number informed')
if schedules_index_list[0] == schedules_index_list[-1]:
print(f'--n Only one schedule, number: {schedules_index_list[0]}')
else:
print(f'--n Schedule number must be a integer between {schedules_index_list[0]} and {schedules_index_list[-1]}')
else:
schedule_path = SCHEDULES_LIST[number]
schedule_name = Path(schedule_path).name
script = fr'{schedule_path}\task.py'
print(f'Running {schedule_name}')
res = runpy.run_path(script)
xml_str = res.get('xml', None)
if xml_str:
dom = parseString(xml_str)
print('\n'.join([el.firstChild.wholeText for el in dom.getElementsByTagName('msg')]))
else:
print(f'{schedule_name} runpy error')
else:
print('No schedule found')



def parseArguments():
"""
Parse arguments from command line
:return: parser.parse_args()
"""
global SCHEDULES_LIST
schedules_folder = os.path.expandvars(r'%localappdata%\Esri\ArcGISPro\Geoprocessing\ScheduledTools')
SCHEDULES_LIST = glob(schedules_folder+os.sep+'*'+os.sep, recursive=True)
SCHEDULES_LIST = [Path(path) for path in SCHEDULES_LIST if Path(path).name != 'Logs']
schedules_name_list = [Path(path).name for path in SCHEDULES_LIST if Path(path).name != 'Logs']
schedules_index_list = [x for x in range(0, len(schedules_name_list), 1)]

parser = argparse.ArgumentParser(description='Script to run schedules',
usage='python proschedule.py -n <number>',
formatter_class=argparse.RawTextHelpFormatter)

if schedules_index_list:
schedules_n_label = '\n'.join([f'{x} - {schedules_name_list[x]} ' for x in schedules_index_list])
else:
schedules_n_label = 'No schedule found'

n_help = f"""Schedules numbers:\n{schedules_n_label}\n\n"""


v_help = """script version"""
parser.add_argument('-n', '--number', type=int, default=-1, help=n_help)
parser.add_argument("--version", action="version", version='%(prog)s - Version 1.0', help=v_help)

args = parser.parse_args()
return args


if __name__ == '__main__':
main()

I do a entry points for arcgis-pro-schedules like above.

In my case i call it python command prompt on windows but I suppose its doable in other architectures with minimal changes.

HildermesJosMedeirosFilho_0-1659440345014.png

 

Tags (1)
0 Kudos
2 Replies
HildermesJoséMedeirosFilho
Occasional Contributor

Dear, all.
I would like to warn, entry point is a script programmed to be called in a specif python env.

Like so, as Esri you could implemment one in arcpy package or arcgis package.

is similar to the proswap command

In short, my sugesstion is to make a entry_point command to pro environment, so we can run our arcgispro schedules.

If I can do it in my private packages, why can't you?

Those poit above are the one that motivated me to post the suggestion at python ideas.

I sincerely belive it should be there, thank you.

0 Kudos
HildermesJoséMedeirosFilho
Occasional Contributor

HildermesJosMedeirosFilho_0-1661209095930.png

This is a basic example. I did a "curses-windows" to manager schedules.
I just need to open the python-command-prompt and type protop...
We could have some more meaningful entry points, right? I mean, some one did proswap, why not protop? proskd....as developer I think is not pratical to open arcgis to stop schedules, run scheds, etc.

0 Kudos