1- import argparse
21import time
3- from ipaddress import ip_address
4- from pyhdx .web import serve
5- from pyhdx .config import cfg
6- from pyhdx .local_cluster import verify_cluster , default_cluster
2+ from typing import Union , Optional
3+ from pathlib import Path
74
5+ import typer
6+ from ipaddress import ip_address
7+ import yaml
88
9- # todo add check to see if the web module requirements are installed
109
10+ app = typer .Typer ()
1111
12- def main ():
13- parser = argparse .ArgumentParser (prog = "pyhdx" , description = "PyHDX Launcher" )
12+ @app .command ()
13+ def serve (scheduler_address : Optional [str ] = typer .Option (None , help = "Address for dask scheduler to use" )):
14+ """Launch the PyHDX web application"""
1415
15- parser .add_argument ("serve" , help = "Runs PyHDX Dashboard" )
16- parser .add_argument (
17- "--scheduler_address" , help = "Run with local cluster <ip>:<port>"
18- )
19- args = parser .parse_args ()
16+ from pyhdx .config import cfg
17+ from pyhdx .local_cluster import verify_cluster , default_cluster
2018
21- if args . scheduler_address :
22- ip , port = args . scheduler_address .split (":" )
19+ if scheduler_address is not None :
20+ ip , port = scheduler_address .split (":" )
2321 if not ip_address (ip ):
2422 print ("Invalid IP Address" )
2523 return
2624 elif not 0 <= int (port ) < 2 ** 16 :
2725 print ("Invalid port, must be 0-65535" )
2826 return
29- cfg .set ("cluster" , "scheduler_address" , args . scheduler_address )
27+ cfg .set ("cluster" , "scheduler_address" , scheduler_address )
3028
3129 scheduler_address = cfg .get ("cluster" , "scheduler_address" )
3230 if not verify_cluster (scheduler_address ):
@@ -37,8 +35,9 @@ def main():
3735 scheduler_address = f"{ ip } :{ port } "
3836 print (f"Started new Dask LocalCluster at { scheduler_address } " )
3937
40- if args .serve :
41- serve .run_apps ()
38+ # Start the PyHDX web application
39+ from pyhdx .web import serve as serve_pyhdx
40+ serve_pyhdx .run_apps ()
4241
4342 loop = True
4443 while loop :
@@ -49,11 +48,22 @@ def main():
4948 loop = False
5049
5150
52- if __name__ == "__main__" :
53- import sys
51+ @app .command ()
52+ def process (
53+ jobfile : Path = typer .Argument (..., help = "Path to .yaml jobfile" ),
54+ cwd : Optional [Path ] = typer .Option (None , help = "Optional path to working directory" )
55+ ):
56+ """
57+ Process a HDX dataset according to a jobfile
58+ """
59+
60+ from pyhdx .batch_processing import JobParser
5461
55- sys .argv .append ("serve" )
56- sys .argv .append ("--scheduler_address" )
57- sys .argv .append ("127.0.0.1:53270" )
62+ job_spec = yaml .safe_load (jobfile .read_text ())
63+ parser = JobParser (job_spec , cwd = cwd )
5864
59- main ()
65+ parser .execute ()
66+
67+
68+ if __name__ == "__main__" :
69+ app ()
0 commit comments