#!/usr/bin/env python
"""
Opening the ASE2SPRKKR result files and/or scripts in the shell or Jupyter notebook
"""
description = "Open SPR-KKR result files or scripts in an interactive shell or Jupyter notebook."
help = "Open SPR-KKR result files in a Python shell or Jupyter notebook"
[docs]
def parser(parser):
parser.add_argument(
"-o", "--output", help="SPR-KKR result file name (see the supported files above).", action="append", default=[]
)
parser.add_argument("-j", "--jupyter", help="Open Jupyter notebook.", action="store_true")
parser.add_argument("-D", "--pdb", help="Run the command inside pdb", action="store_true")
parser.add_argument(
"-e",
"--example",
help="Open and run the example given by number. The example files will be copied to the directory given by -d or to a temp directory.",
action="store",
type=int,
)
parser.add_argument(
"-d", "--directory", help='Directory, where to copy the example. Use "." for the current directory.'
)
parser.add_argument("-s", "--save", help="Save the script/Jupyter notebook.", action="store", type=str)
parser.add_argument(
"-r", "--run", help="Execute the notebook before saving and opening Jupyter notebook.", action="store", type=str
)
[docs]
def run(args, global_args):
from pathlib import Path
from ase2sprkkr.gui.shell import Python, JupyterLab, JupyterDependencyError, Pdb
import os
from ase2sprkkr.common.file_utils import Directory
try:
if args.jupyter:
shell = JupyterLab(run=args.run)
elif args.pdb:
shell = Pdb()
else:
shell = Python()
except JupyterDependencyError as exc:
raise SystemExit(str(exc)) from None
change_dir = True
if args.directory:
dire = Directory(args.directory)
elif args.example:
dire = Directory(False)
else:
change_dir = False
dire = Directory(".")
if change_dir:
cwd = Path(os.getcwd())
for i in args.output:
if change_dir:
i = cwd.resolve(i)
shell.add_output_file(i)
with dire as dr:
if args.example is not None:
try:
shell.run_example(args.example, dr)
except FileNotFoundError:
if global_args['debug']:
raise
print(f"No such example {args.example}")
exit(-1)
if args.save:
shell.save(args.save)
shell.open()