Source code for toycoronagraph.__main__

"""
python -m toycoronagraph COMMAND

    new        write a parameter file holding only the modules you choose (+ its JSON schema)
    describe   explain a key, a section or a module
    migrate    rewrite an old-layout parameter file ([target], [telescope], ...) in the new layout
    schema     write the JSON schema (editor hover, completion and checks)
    reference  print the parameter reference (Markdown)
"""
import argparse
import os
import sys
from toycoronagraph import params_docs as docs


[docs] def main(argv=None): """ Run one command (see the module docstring); the text is generated by toycoronagraph.params_docs. Args: argv (list of str): the arguments after "python -m toycoronagraph"; None = sys.argv[1:]. Raises: SystemExit: bad arguments (argparse), or `new` would overwrite a file without --force. """ parser = argparse.ArgumentParser(prog="python -m toycoronagraph", description=__doc__, formatter_class=argparse.RawDescriptionHelpFormatter) sub = parser.add_subparsers(dest="command", required=True) from toycoronagraph.sources import source_kinds p = sub.add_parser("new", help="write a parameter file with only the chosen modules") p.add_argument("--sources", default="fits_image", help="comma-separated kinds; known: " + ", ".join(source_kinds())) p.add_argument("--coronagraphs", default="vortex:2", help='comma-separated, e.g. "vortex:2,vortex:6"') p.add_argument("--full", action="store_true", help="also list the optional keys (commented out)") p.add_argument("-o", "--output", default="toycoronagraph_para.toml") p.add_argument("--force", action="store_true", help="overwrite an existing file") p = sub.add_parser("describe", help="explain a key, section or module") p.add_argument("query", help='e.g. "star.distance", "grid", "wind", "wind.mdot", "vortex", "charge"') p = sub.add_parser("migrate", help="rewrite an old-layout file in the new layout") p.add_argument("file") p.add_argument("-o", "--output", help="default: <file>_new.toml") p = sub.add_parser("schema", help="write the JSON schema") p.add_argument("-o", "--output", default=docs.SCHEMA_FILE) sub.add_parser("reference", help="print the parameter reference (Markdown)") args = parser.parse_args(argv) if args.command == "new": if os.path.exists(args.output) and not args.force: sys.exit("%s exists; use --force to overwrite it, or -o another name" % args.output) docs.new_config([s.strip() for s in args.sources.split(",") if s.strip()], [c.strip() for c in args.coronagraphs.split(",") if c.strip()], path=args.output, full=args.full) print("wrote %s (and %s next to it)" % (args.output, docs.SCHEMA_FILE)) elif args.command == "describe": print(docs.describe(args.query)) elif args.command == "migrate": out = args.output or os.path.splitext(args.file)[0] + "_new.toml" docs.migrate(args.file, out) print("wrote %s; the original %s is unchanged" % (out, args.file)) elif args.command == "schema": docs.write_schema(args.output) print("wrote " + args.output) elif args.command == "reference": print(docs.reference_markdown())
if __name__ == "__main__": main()