Source code for ase2sprkkr_cli.commands.example

#!/usr/bin/env python
"""
This command just show the path to the examples
"""





help = "Show path to the examples, or example itself, or copy a given example."
description = "See also the 'shell -e' subcommand for running examples interactivelly"
aliases = ["examples"]


[docs] def parser(parser): parser.add_argument("example", type=int, nargs="?", help="The number of the example to print. If ommited") parser.add_argument("-c", "--copy", help="Copy the example to a given dir", type=str) parser.add_argument("-C", "--cd", help="Print `cd <path_to_the_example(s dir)> and exit", action='store_true', default=False) parser.add_argument("-p", "--path", help="Print a path to the example(s)", action="store_true") parser.add_argument("-s", "--script", help="Print a path to the example main script", action="store_true") parser.add_argument("-g", "--regex", help="Find an example according to the given regex", type=str)
[docs] def run(args, global_args): from ase2sprkkr.gui import examples def cd(dir): import os dir = str(dir) if os.name == "nt": import subprocess return print(f"cd /d {subprocess.list2cmdline([dir])}") import shlex return print(f"cd {shlex.quote(dir)}") if not args.example: if args.script: raise ValueError("The --script argument requires to specify the example number.") if args.cd: return cd(examples.examples_dir()) if not args.path: print("Examples dir: ", end="") print(examples.examples_dir()) if args.path: return print("") exs = examples.list_of_examples(regex=args.regex) print(f"{'NAME':<30} {'SCRIPT':<20} DESCRIPTION") for e in exs: print(f"{e.name:<30} {e.main_script.name:<20} {e.short_docstring or ''}") return example = examples.Example.by_number(args.example) show = True if args.cd: return cd(example.dir) if args.copy: example.copy(args.copy) show = False if args.path: print(example.dir) show = False if args.script: print(example.main_script) show = False if show: print(example.source())