Skip to content

Commit

Permalink
Refactor KQC command line parser
Browse files Browse the repository at this point in the history
  • Loading branch information
caspar-iqm committed Apr 16, 2024
1 parent 9db4255 commit 726a16c
Showing 1 changed file with 21 additions and 8 deletions.
29 changes: 21 additions & 8 deletions klayout_package/python/kqcircuits/run.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
logging.basicConfig(level=logging.WARN, stream=sys.stdout)


def run():
def argument_parser():
parser = argparse.ArgumentParser(description="KQC console scripts")

subparser = parser.add_subparsers(dest="command")
Expand Down Expand Up @@ -101,16 +101,17 @@ def run():
singularity_parser.add_argument(
"--singularity-remote-path", type=str, help="Installation path for the singularity image on remote"
)
return parser, subparser

args, args_for_script = parser.parse_known_args()

def run_kqc(args, args_for_script):
if args.command == "sim":
if args.export_script == "ls":
simdir = Path(SCRIPTS_PATH / "simulations")
for f in listdir(simdir):
if isfile(join(simdir, f)):
print(f)
return
return True

if args.remote:
remote_host = str(args.export_script)
Expand All @@ -122,11 +123,11 @@ def run():
args.export_path_basename,
args_for_script,
)
return
return True
if args.remote_run_only:
remote_host = str(args.export_script)
remote_run_only(remote_host, args.kqc_remote_tmp_path, args.detach, args.poll_interval, args_for_script)
return
return True

script_file = Path(args.export_script)
if args.export_path_basename is not None:
Expand All @@ -138,26 +139,29 @@ def run():
script_file = Path(SCRIPTS_PATH / "simulations" / script_file)
if not script_file.is_file():
logging.error(f"Export script not found at {args.export_script} or {script_file}")
return
return True
export_and_run(script_file, export_path, args.quiet, args.export_only, args_for_script)
return True
elif args.command == "mask":
if args.mask_script == "ls":
maskdir = Path(SCRIPTS_PATH / "masks")
for f in listdir(maskdir):
if isfile(join(maskdir, f)):
print(f)
return
return True
script_file = Path(args.mask_script)
if not script_file.is_file():
script_file = Path(SCRIPTS_PATH / "masks" / args.mask_script)
if not script_file.is_file():
logging.error(f"Mask script not found at {args.mask_script} or {script_file}")
return
return True
if args.debug:
subprocess.call([sys.executable, script_file, "-d"])
return True
else: # Windows needs this for multiprocessing
with open(script_file) as mask_file:
exec(mask_file.read()) # pylint: disable=exec-used
return True
elif args.command == "singularity":
if args.build:
chdir(Path(ROOT_PATH / "singularity"))
Expand All @@ -178,4 +182,13 @@ def run():
elif args.push is not None:
export_singularity(args.push, args.singularity_remote_path)
else:
return False
return True


def run():
"""Main entry point for the KQC console command"""
parser, _ = argument_parser()
args, args_for_script = parser.parse_known_args()
if not run_kqc(args, args_for_script):
parser.print_usage()

0 comments on commit 726a16c

Please sign in to comment.