def main(): argparser = argparse.ArgumentParser( prog='mathics', usage='%(prog)s [options] [FILE]', add_help=False, description="Mathics is a general-purpose computer algebra system.", epilog="""Please feel encouraged to contribute to Mathics! Create your own fork, make the desired changes, commit, and make a pull request.""") argparser.add_argument( 'FILE', nargs='?', type=argparse.FileType('r'), help='execute commands from FILE') argparser.add_argument( '--help', '-h', help='show this help message and exit', action='help') argparser.add_argument( '--persist', help='go to interactive shell after evaluating FILE or -e', action='store_true') argparser.add_argument( '--quiet', '-q', help='don\'t print message at startup', action='store_true') argparser.add_argument( '-script', help='run a mathics file in script mode', action='store_true') argparser.add_argument( '--execute', '-e', action='append', metavar='EXPR', help='evaluate EXPR before processing any input files (may be given ' 'multiple times)') argparser.add_argument( '--colors', nargs='?', help='interactive shell colors') argparser.add_argument( '--no-completion', help="disable tab completion", action='store_true') argparser.add_argument( '--no-readline', help="disable line editing (implies --no-completion)", action='store_true') argparser.add_argument( '--version', '-v', action='version', version='%(prog)s ' + __version__) args = argparser.parse_args() quit_command = 'CTRL-BREAK' if sys.platform == 'win32' else 'CONTROL-D' definitions = Definitions(add_builtin=True) definitions.set_ownvalue('$Line', Integer(0)) # Reset the line number shell = TerminalShell( definitions, args.colors, want_readline=not(args.no_readline), want_completion=not(args.no_completion)) if not (args.quiet or args.script): print() print(version_string + '\n') print(license_string + '\n') print("Quit by pressing {0}\n".format(quit_command)) if args.execute: for expr in args.execute: print(shell.get_in_prompt() + expr) evaluation = Evaluation(shell.definitions, out_callback=shell.out_callback) exprs = evaluation.parse(expr) results = evaluation.evaluate(exprs, timeout=settings.TIMEOUT) shell.print_results(results) if not args.persist: return if args.FILE is not None: lines = args.FILE.readlines() if args.script and lines[0].startswith('#!'): lines[0] = '' results = [] query_gen = parse_lines(lines, shell.definitions) evaluation = Evaluation(shell.definitions, out_callback=shell.out_callback) try: for query in query_gen: results.extend(evaluation.evaluate([query], timeout=settings.TIMEOUT)) except TranslateError as exc: evaluation.recursion_depth = 0 evaluation.stopped = False evaluation.message('Syntax', exc.msg, *exc.args) except (KeyboardInterrupt): print('\nKeyboardInterrupt') except (SystemExit, EOFError): print("\n\nGood bye!\n") if not args.persist: return total_input = "" while True: try: evaluation = Evaluation(shell.definitions, out_callback=shell.out_callback) line = shell.read_line(shell.get_in_prompt(continued=total_input != '')) total_input += line try: query = parse(total_input, shell.definitions) except TranslateError as exc: if line == '' or not isinstance(exc, IncompleteSyntaxError): evaluation.message('Syntax', exc.msg, *exc.args) total_input = "" continue total_input = "" if query is None: continue results = evaluation.evaluate([query], timeout=settings.TIMEOUT) shell.print_results(results) except (KeyboardInterrupt): print('\nKeyboardInterrupt') except (SystemExit, EOFError): print("\n\nGood bye!\n") break
def main(): argparser = argparse.ArgumentParser( prog='mathics', usage='%(prog)s [options] [FILE]', add_help=False, description="Mathics is a general-purpose computer algebra system.", epilog="""Please feel encouraged to contribute to Mathics! Create your own fork, make the desired changes, commit, and make a pull request.""") argparser.add_argument('FILE', nargs='?', type=argparse.FileType('r'), help='execute commands from FILE') argparser.add_argument('--help', '-h', help='show this help message and exit', action='help') argparser.add_argument( '--persist', help='go to interactive shell after evaluating FILE or -e', action='store_true') argparser.add_argument('--quiet', '-q', help='don\'t print message at startup', action='store_true') argparser.add_argument('-script', help='run a mathics file in script mode', action='store_true') argparser.add_argument( '--execute', '-e', action='append', metavar='EXPR', help='evaluate EXPR before processing any input files (may be given ' 'multiple times)') argparser.add_argument('--colors', nargs='?', help='interactive shell colors') argparser.add_argument('--no-completion', help="disable tab completion", action='store_true') argparser.add_argument( '--no-readline', help="disable line editing (implies --no-completion)", action='store_true') argparser.add_argument('--version', '-v', action='version', version='%(prog)s ' + __version__) args = argparser.parse_args() quit_command = 'CTRL-BREAK' if sys.platform == 'win32' else 'CONTROL-D' definitions = Definitions(add_builtin=True) definitions.set_line_no(0) # Reset the line number shell = TerminalShell(definitions, args.colors, want_readline=not (args.no_readline), want_completion=not (args.no_completion)) if not (args.quiet or args.script): print() print(version_string + '\n') print(license_string + '\n') print("Quit by pressing {0}\n".format(quit_command)) if args.execute: for expr in args.execute: print(shell.get_in_prompt() + expr) evaluation = Evaluation(shell.definitions, out_callback=shell.out_callback) exprs = evaluation.parse(expr) results = evaluation.evaluate(exprs, timeout=settings.TIMEOUT) shell.print_results(results) if not args.persist: return if args.FILE is not None: lines = args.FILE.readlines() if args.script and lines[0].startswith('#!'): lines[0] = '' results = [] query_gen = parse_lines(lines, shell.definitions) evaluation = Evaluation(shell.definitions, out_callback=shell.out_callback) try: for query in query_gen: results.extend( evaluation.evaluate([query], timeout=settings.TIMEOUT)) except TranslateError as exc: evaluation.recursion_depth = 0 evaluation.stopped = False evaluation.message('Syntax', exc.msg, *exc.args) except (KeyboardInterrupt): print('\nKeyboardInterrupt') except (SystemExit, EOFError): print("\n\nGood bye!\n") if not args.persist: return total_input = "" while True: try: evaluation = Evaluation(shell.definitions, out_callback=shell.out_callback) line = shell.read_line( shell.get_in_prompt(continued=total_input != '')) total_input += line try: query = parse(total_input, shell.definitions) except TranslateError as exc: if line == '' or not isinstance(exc, IncompleteSyntaxError): evaluation.message('Syntax', exc.msg, *exc.args) total_input = "" continue total_input = "" if query is None: continue results = evaluation.evaluate([query], timeout=settings.TIMEOUT) shell.print_results(results) except (KeyboardInterrupt): print('\nKeyboardInterrupt') except (SystemExit, EOFError): print("\n\nGood bye!\n") break