|
| 1 | +# |
| 2 | +#Also see sasview\src\sas\qtgui\Perspectives\Fitting\media\cli.rst |
| 3 | +# |
| 4 | +""" |
| 5 | +**SasView command line interface** |
| 6 | +
|
| 7 | +This functionality is under development. Interactive sessions do not yet |
| 8 | +work in the Windows console. |
| 9 | +
|
| 10 | +**Usage:** |
| 11 | +
|
| 12 | +sasview [flags] |
| 13 | + *Run SasView. If no flag is given, or -q or -V are given, this will start |
| 14 | + the GUI.* |
| 15 | +
|
| 16 | +sasview [flags] script [args...] |
| 17 | + *Run a python script using the installed SasView libraries [passing |
| 18 | + optional arguments]* |
| 19 | +
|
| 20 | +sasview [flags] -m module [args...] |
| 21 | + *Run a SasView/Sasmodels/Bumps module as main [passing optional arguments]* |
| 22 | +
|
| 23 | +sasview [flags] -c "python statements" [args...] |
| 24 | + *Execute python statements using the installed SasView libraries* |
| 25 | +
|
| 26 | +sasview -V |
| 27 | + *Print sasview version and exit.* |
| 28 | +
|
| 29 | +**Flags:** |
| 30 | +
|
| 31 | + -i, --interactive. *Enter an interactive session after command/module/script.* |
| 32 | +
|
| 33 | + -o, --console. *Open a console to show command output. (Windows only)* |
| 34 | +
|
| 35 | + -q, --quiet. *Suppress startup messages on interactive console.* |
| 36 | +
|
| 37 | +Note: On Windows any console output is ignored by default. You can either |
| 38 | +open a console to show the output with the *-o* flag or redirect output to |
| 39 | +a file using something like *sasview ... > output.txt*. |
| 40 | +""" |
| 41 | +import sys |
| 42 | + |
| 43 | +# TODO: Support dropping datafiles onto .exe? |
| 44 | +# TODO: Maybe use the bumps cli with project file as model? |
| 45 | + |
| 46 | +import argparse |
| 47 | + |
| 48 | +def parse_cli(argv): |
| 49 | + """ |
| 50 | + Parse the command argv returning an argparse.Namespace. |
| 51 | +
|
| 52 | + * version: bool - print version |
| 53 | + * command: str - string to exec |
| 54 | + * module: str - module to run as main |
| 55 | + * interactive: bool - run interactive |
| 56 | + * args: list[str] - additional arguments, or script + args |
| 57 | + """ |
| 58 | + parser = argparse.ArgumentParser() |
| 59 | + parser.add_argument("-V", "--version", action='store_true', |
| 60 | + help="Print sasview version and exit") |
| 61 | + parser.add_argument("-m", "--module", type=str, |
| 62 | + help="Run module with remaining arguments sent to main") |
| 63 | + parser.add_argument("-c", "--command", type=str, |
| 64 | + help="Execute command") |
| 65 | + parser.add_argument("-i", "--interactive", action='store_true', |
| 66 | + help="Start interactive interpreter after running command") |
| 67 | + parser.add_argument("-o", "--console", action='store_true', |
| 68 | + help="Open console to display output (windows only)") |
| 69 | + parser.add_argument("-q", "--quiet", action='store_true', |
| 70 | + help="Don't print banner when entering interactive mode") |
| 71 | + parser.add_argument("args", nargs="*", |
| 72 | + help="script followed by args") |
| 73 | + |
| 74 | + # Special case: abort argument processing after -m or -c. |
| 75 | + have_trigger = False |
| 76 | + collect_rest = False |
| 77 | + keep = [] |
| 78 | + rest = [] |
| 79 | + for arg in argv[1:]: |
| 80 | + # Append argument to the parser argv or collect them as extra args. |
| 81 | + if collect_rest: |
| 82 | + rest.append(arg) |
| 83 | + else: |
| 84 | + keep.append(arg) |
| 85 | + # For an action that needs an argument (e.g., -m module) we need |
| 86 | + # to keep the next argument for the parser, but the remaining arguments |
| 87 | + # get collected as extra args. Trigger and collect will happen in one |
| 88 | + # step if the trigger requires no args or if the arg was provided |
| 89 | + # with the trigger (e.g., -mmodule) |
| 90 | + if have_trigger: |
| 91 | + collect_rest = True |
| 92 | + if arg.startswith('-m') or arg.startswith('--module'): |
| 93 | + have_trigger = True |
| 94 | + collect_rest = arg not in ('-m', '--module') |
| 95 | + elif arg.startswith('-c') or arg.startswith('--command'): |
| 96 | + have_trigger = True |
| 97 | + collect_rest = arg not in ('-c', '--command') |
| 98 | + |
| 99 | + opts = parser.parse_args(keep) |
| 100 | + if collect_rest: |
| 101 | + opts.args = rest |
| 102 | + return opts |
| 103 | + |
| 104 | +def main(logging="production"): |
| 105 | + from sas.system import log |
| 106 | + from sas.system import lib |
| 107 | + from sas.system import console |
| 108 | + |
| 109 | + # I/O redirection for the windows console. Need to do this early so that |
| 110 | + # output will be displayed on the console. Presently not working for |
| 111 | + # for production (it always opens the console even if it is not needed) |
| 112 | + # so require "sasview con ..." to open the console. Not an infamous |
| 113 | + # "temporary fix" I hope... |
| 114 | + if "-i" in sys.argv[1:] or "-o" in sys.argv[1:]: |
| 115 | + console.setup_console() |
| 116 | + |
| 117 | + # Eventually argument processing might affect logger or config, so do it first |
| 118 | + cli = parse_cli(sys.argv) |
| 119 | + |
| 120 | + # Setup logger and sasmodels |
| 121 | + if logging == "production": |
| 122 | + log.production() |
| 123 | + elif logging == "development": |
| 124 | + log.development() |
| 125 | + else: |
| 126 | + raise ValueError(f"Unknown logging mode \"{logging}\"") |
| 127 | + lib.setup_sasmodels() |
| 128 | + lib.setup_qt_env() # Note: does not import any gui libraries |
| 129 | + |
| 130 | + if cli.version: # -V |
| 131 | + import sas |
| 132 | + print(f"SasView {sas.__version__}") |
| 133 | + # Exit immediately after -V. |
| 134 | + return 0 |
| 135 | + |
| 136 | + context = {'exit': sys.exit} |
| 137 | + if cli.module: # -m module [arg...] |
| 138 | + import runpy |
| 139 | + # TODO: argv[0] should be the path to the module file not the dotted name |
| 140 | + sys.argv = [cli.module, *cli.args] |
| 141 | + context = runpy.run_module(cli.module, run_name="__main__") |
| 142 | + elif cli.command: # -c "command" |
| 143 | + sys.argv = ["-c", *cli.args] |
| 144 | + exec(cli.command, context) |
| 145 | + elif cli.args: # script [arg...] |
| 146 | + import runpy |
| 147 | + sys.argv = cli.args |
| 148 | + context = runpy.run_path(cli.args[0], run_name="__main__") |
| 149 | + elif not cli.interactive: # no arguments so start the GUI |
| 150 | + from sas.qtgui.MainWindow.MainWindow import run_sasview |
| 151 | + # sys.argv is unchanged |
| 152 | + # Maybe hand cli.quiet to run_sasview? |
| 153 | + run_sasview() |
| 154 | + return 0 # don't drop into the interactive interpreter |
| 155 | + |
| 156 | + # TODO: Start interactive with ipython rather than normal python |
| 157 | + # For ipython use: |
| 158 | + # from IPython import start_ipython |
| 159 | + # sys.argv = ["ipython", *args] |
| 160 | + # sys.exit(start_ipython()) |
| 161 | + if cli.interactive: |
| 162 | + import code |
| 163 | + # The python banner is something like |
| 164 | + # f"Python {sys.version} on {platform.system().lower()}" |
| 165 | + # where sys.version contains "VERSION (HGTAG, DATE)\n[COMPILER]" |
| 166 | + # We are replacing it with something that includes the sasview version. |
| 167 | + if cli.quiet: |
| 168 | + exitmsg = banner = "" |
| 169 | + else: |
| 170 | + import platform |
| 171 | + import sas |
| 172 | + # Form dotted python version number out of sys.version_info |
| 173 | + major, minor, micro = sys.version_info[:3] |
| 174 | + sasview_ver = f"SasView {sas.__version__}" |
| 175 | + python_ver = f"Python {major}.{minor}.{micro}" |
| 176 | + os_ver = platform.system() |
| 177 | + banner = f"{sasview_ver} for {python_ver} on {os_ver}" |
| 178 | + exitmsg = "" |
| 179 | + code.interact(banner=banner, exitmsg=exitmsg, local=context) |
| 180 | + |
| 181 | + return 0 |
| 182 | + |
| 183 | +if __name__ == "__main__": |
| 184 | + sys.exit(main()) |
0 commit comments