Beispiel #1
0
    def use_args(self, args, interact=True):
        """Handles command-line arguments."""
        logger.quiet, logger.verbose = args.quiet, args.verbose
        if DEVELOP:
            logger.tracing = args.trace
        if args.recursion_limit is not None:
            self.set_recursion_limit(args.recursion_limit)
        if args.jobs is not None:
            self.set_jobs(args.jobs)
        if args.display:
            self.show = True
        if args.style is not None:
            self.prompt.set_style(args.style)
        if args.documentation:
            launch_documentation()
        if args.tutorial:
            launch_tutorial()

        self.setup(
            target=args.target,
            strict=args.strict,
            minify=args.minify,
            line_numbers=args.line_numbers,
            keep_lines=args.keep_lines,
        )

        if args.mypy is not None:
            self.set_mypy_args(args.mypy)

        if args.source is not None:
            if args.interact and args.run:
                logger.warn("extraneous --run argument passed; --interact implies --run")
            if args.package and self.mypy:
                logger.warn("extraneous --package argument passed; --mypy implies --package")
            if args.standalone and args.package:
                raise CoconutException("cannot compile as both --package and --standalone")
            if args.standalone and self.mypy:
                raise CoconutException("cannot compile as both --package (implied by --mypy) and --standalone")
            if (args.run or args.interact) and os.path.isdir(args.source):
                if args.run:
                    raise CoconutException("source path must point to file not directory when --run is enabled")
                if args.interact:
                    raise CoconutException("source path must point to file not directory when --run (implied by --interact) is enabled")
            if args.watch and os.path.isfile(args.source):
                raise CoconutException("source path must point to directory not file when --watch is enabled")

            if args.dest is None:
                if args.nowrite:
                    dest = None  # no dest
                else:
                    dest = True  # auto-generate dest
            elif args.nowrite:
                raise CoconutException("destination path cannot be given when --nowrite is enabled")
            elif os.path.isfile(args.dest):
                raise CoconutException("destination path must point to directory not file")
            else:
                dest = args.dest
            if args.package or self.mypy:
                package = True
            elif args.standalone:
                package = False
            else:
                package = None  # auto-decide package

            with self.running_jobs():
                filepaths = self.compile_path(args.source, dest, package, args.run or args.interact, args.force)
            self.run_mypy(filepaths)

        elif (args.run
              or args.nowrite
              or args.force
              or args.package
              or args.standalone
              or args.watch):
            raise CoconutException("a source file/folder must be specified when options that depend on the source are enabled")

        if args.code is not None:
            self.execute(self.comp.parse_block(args.code))
        stdin = not sys.stdin.isatty()  # check if input was piped in
        if stdin:
            self.execute(self.comp.parse_block(sys.stdin.read()))
        if args.jupyter is not None:
            self.start_jupyter(args.jupyter)
        if args.interact or (interact and not (
                stdin
                or args.source
                or args.code
                or args.tutorial
                or args.documentation
                or args.watch
                or args.jupyter is not None
        )):
            self.start_prompt()
        if args.watch:
            self.watch(args.source, dest, package, args.run, args.force)
Beispiel #2
0
    def use_args(self, args, interact=True, original_args=None):
        """Handle command-line arguments."""
        logger.quiet, logger.verbose = args.quiet, args.verbose
        if DEVELOP:
            logger.tracing = args.trace

        logger.log("Using " + PYPARSING + ".")
        if original_args is not None:
            logger.log("Directly passed args:", original_args)
        logger.log("Parsed args:", args)

        if args.recursion_limit is not None:
            set_recursion_limit(args.recursion_limit)
        if args.jobs is not None:
            self.set_jobs(args.jobs)
        if args.display:
            self.show = True
        if args.style is not None:
            self.prompt.set_style(args.style)
        if args.history_file is not None:
            self.prompt.set_history_file(args.history_file)
        if args.documentation:
            launch_documentation()
        if args.tutorial:
            launch_tutorial()

        self.setup(
            target=args.target,
            strict=args.strict,
            minify=args.minify,
            line_numbers=args.line_numbers,
            keep_lines=args.keep_lines,
            no_tco=args.no_tco,
        )

        if args.mypy is not None:
            self.set_mypy_args(args.mypy)

        if args.argv is not None:
            sys.argv = [args.source if args.source is not None else ""]
            sys.argv.extend(args.argv)

        if args.source is not None:
            if args.interact and args.run:
                logger.warn("extraneous --run argument passed; --interact implies --run")
            if args.package and self.mypy:
                logger.warn("extraneous --package argument passed; --mypy implies --package")

            if args.standalone and args.package:
                raise CoconutException("cannot compile as both --package and --standalone")
            if args.standalone and self.mypy:
                raise CoconutException("cannot compile as both --package (implied by --mypy) and --standalone")
            if args.no_write and self.mypy:
                raise CoconutException("cannot compile with --no-write when using --mypy")
            if (args.run or args.interact) and os.path.isdir(args.source):
                if args.run:
                    raise CoconutException("source path must point to file not directory when --run is enabled")
                if args.interact:
                    raise CoconutException("source path must point to file not directory when --run (implied by --interact) is enabled")
            if args.watch and os.path.isfile(args.source):
                raise CoconutException("source path must point to directory not file when --watch is enabled")

            if args.dest is None:
                if args.no_write:
                    dest = False  # no dest
                else:
                    dest = True  # auto-generate dest
            elif args.no_write:
                raise CoconutException("destination path cannot be given when --no-write is enabled")
            else:
                dest = args.dest

            if args.package or self.mypy:
                package = True
            elif args.standalone:
                package = False
            else:
                package = None  # auto-decide package

            with self.running_jobs(exit_on_error=not args.watch):
                filepaths = self.compile_path(args.source, dest, package, args.run or args.interact, args.force)
            self.run_mypy(filepaths)

        elif (
            args.run
            or args.no_write
            or args.force
            or args.package
            or args.standalone
            or args.watch
        ):
            raise CoconutException("a source file/folder must be specified when options that depend on the source are enabled")

        if args.code is not None:
            self.execute(self.comp.parse_block(args.code))
        got_stdin = False
        if args.jupyter is not None:
            self.start_jupyter(args.jupyter)
        elif stdin_readable():
            logger.log("Reading piped input from stdin...")
            self.execute(self.comp.parse_block(sys.stdin.read()))
            got_stdin = True
        if args.interact or (interact and not (
                got_stdin
                or args.source
                or args.code
                or args.tutorial
                or args.documentation
                or args.watch
                or args.jupyter is not None
        )):
            self.start_prompt()
        if args.watch:
            self.watch(args.source, dest, package, args.run, args.force)
Beispiel #3
0
    def use_args(self, args, interact=True, original_args=None):
        """Handle command-line arguments."""
        logger.quiet, logger.verbose = args.quiet, args.verbose
        if DEVELOP:
            logger.tracing = args.trace

        logger.log("Using " + PYPARSING + ".")
        if original_args is not None:
            logger.log("Directly passed args:", original_args)
        logger.log("Parsed args:", args)

        if args.recursion_limit is not None:
            set_recursion_limit(args.recursion_limit)
        if args.jobs is not None:
            self.set_jobs(args.jobs)
        if args.display:
            self.show = True
        if args.style is not None:
            self.prompt.set_style(args.style)
        if args.documentation:
            launch_documentation()
        if args.tutorial:
            launch_tutorial()

        self.setup(
            target=args.target,
            strict=args.strict,
            minify=args.minify,
            line_numbers=args.line_numbers,
            keep_lines=args.keep_lines,
            no_tco=args.no_tco or args.mypy is not None,
        )

        if args.mypy is not None:
            if args.no_tco:
                logger.warn(
                    "extraneous --no-tco argument passed; --mypy implies --no-tco"
                )
            self.set_mypy_args(args.mypy)

        if args.argv is not None:
            sys.argv = [args.source if args.source is not None else ""]
            sys.argv.extend(args.argv)

        if args.source is not None:
            if args.interact and args.run:
                logger.warn(
                    "extraneous --run argument passed; --interact implies --run"
                )
            if args.package and self.mypy:
                logger.warn(
                    "extraneous --package argument passed; --mypy implies --package"
                )

            if args.standalone and args.package:
                raise CoconutException(
                    "cannot compile as both --package and --standalone")
            if args.standalone and self.mypy:
                raise CoconutException(
                    "cannot compile as both --package (implied by --mypy) and --standalone"
                )
            if args.no_write and self.mypy:
                raise CoconutException(
                    "cannot compile with --no-write when using --mypy")
            if (args.run or args.interact) and os.path.isdir(args.source):
                if args.run:
                    raise CoconutException(
                        "source path must point to file not directory when --run is enabled"
                    )
                if args.interact:
                    raise CoconutException(
                        "source path must point to file not directory when --run (implied by --interact) is enabled"
                    )
            if args.watch and os.path.isfile(args.source):
                raise CoconutException(
                    "source path must point to directory not file when --watch is enabled"
                )

            if args.dest is None:
                if args.no_write:
                    dest = None  # no dest
                else:
                    dest = True  # auto-generate dest
            elif args.no_write:
                raise CoconutException(
                    "destination path cannot be given when --no-write is enabled"
                )
            elif os.path.isfile(args.dest):
                raise CoconutException(
                    "destination path must point to directory not file")
            else:
                dest = args.dest

            if args.package or self.mypy:
                package = True
            elif args.standalone:
                package = False
            else:
                package = None  # auto-decide package

            with self.running_jobs(exit_on_error=not args.watch):
                filepaths = self.compile_path(args.source, dest, package,
                                              args.run or args.interact,
                                              args.force)
            self.run_mypy(filepaths)

        elif (args.run or args.no_write or args.force or args.package
              or args.standalone or args.watch):
            raise CoconutException(
                "a source file/folder must be specified when options that depend on the source are enabled"
            )

        if args.code is not None:
            self.execute(self.comp.parse_block(args.code))
        got_stdin = False
        if stdin_readable():
            logger.log("Reading piped input from stdin...")
            self.execute(self.comp.parse_block(sys.stdin.read()))
            got_stdin = True
        if args.jupyter is not None:
            self.start_jupyter(args.jupyter)
        if args.interact or (interact
                             and not (got_stdin or args.source or args.code
                                      or args.tutorial or args.documentation or
                                      args.watch or args.jupyter is not None)):
            self.start_prompt()
        if args.watch:
            self.watch(args.source, dest, package, args.run, args.force)
Beispiel #4
0
    def use_args(self, args, interact=True, original_args=None):
        """Handle command-line arguments."""
        # fix args
        if not DEVELOP:
            args.trace = args.profile = False

        # set up logger
        logger.quiet, logger.verbose, logger.tracing = args.quiet, args.verbose, args.trace
        if args.trace or args.profile:
            unset_fast_pyparsing_reprs()
        if args.profile:
            collect_timing_info()

        logger.log(cli_version)
        if original_args is not None:
            logger.log("Directly passed args:", original_args)
        logger.log("Parsed args:", args)

        # validate general command args
        if args.mypy is not None and args.line_numbers:
            logger.warn("extraneous --line-numbers argument passed; --mypy implies --line-numbers")
        if args.site_install and args.site_uninstall:
            raise CoconutException("cannot --site-install and --site-uninstall simultaneously")

        # process general command args
        if args.recursion_limit is not None:
            set_recursion_limit(args.recursion_limit)
        if args.jobs is not None:
            self.set_jobs(args.jobs)
        if args.display:
            self.show = True
        if args.style is not None:
            self.prompt.set_style(args.style)
        if args.history_file is not None:
            self.prompt.set_history_file(args.history_file)
        if args.vi_mode:
            self.prompt.vi_mode = True
        if args.docs:
            launch_documentation()
        if args.tutorial:
            launch_tutorial()
        if args.site_uninstall:
            self.site_uninstall()
        if args.site_install:
            self.site_install()
        if args.argv is not None:
            self.argv_args = list(args.argv)

        # additional validation after processing
        if args.profile and self.jobs != 0:
            raise CoconutException("--profile incompatible with --jobs {jobs}".format(jobs=args.jobs))

        # process general compiler args
        self.setup(
            target=args.target,
            strict=args.strict,
            minify=args.minify,
            line_numbers=args.line_numbers or args.mypy is not None,
            keep_lines=args.keep_lines,
            no_tco=args.no_tco,
            no_wrap=args.no_wrap,
        )

        # process mypy args (must come after compiler setup)
        if args.mypy is not None:
            self.set_mypy_args(args.mypy)

        if args.source is not None:
            # warnings if source is given
            if args.interact and args.run:
                logger.warn("extraneous --run argument passed; --interact implies --run")
            if args.package and self.mypy:
                logger.warn("extraneous --package argument passed; --mypy implies --package")

            # errors if source is given
            if args.standalone and args.package:
                raise CoconutException("cannot compile as both --package and --standalone")
            if args.standalone and self.mypy:
                raise CoconutException("cannot compile as both --package (implied by --mypy) and --standalone")
            if args.no_write and self.mypy:
                raise CoconutException("cannot compile with --no-write when using --mypy")

            # process all source, dest pairs
            src_dest_package_triples = [
                self.process_source_dest(src, dst, args)
                for src, dst in (
                    [(args.source, args.dest)]
                    + (getattr(args, "and") or [])
                )
            ]

            # do compilation
            with self.running_jobs(exit_on_error=not args.watch):
                filepaths = []
                for source, dest, package in src_dest_package_triples:
                    filepaths += self.compile_path(source, dest, package, run=args.run or args.interact, force=args.force)
            self.run_mypy(filepaths)

        # validate args if no source is given
        elif (
            args.run
            or args.no_write
            or args.force
            or args.package
            or args.standalone
            or args.watch
        ):
            raise CoconutException("a source file/folder must be specified when options that depend on the source are enabled")
        elif getattr(args, "and"):
            raise CoconutException("--and should only be used for extra source/dest pairs, not the first source/dest pair")

        # handle extra cli tasks
        if args.code is not None:
            self.execute(self.comp.parse_block(args.code))
        got_stdin = False
        if args.jupyter is not None:
            self.start_jupyter(args.jupyter)
        elif stdin_readable():
            logger.log("Reading piped input from stdin...")
            self.execute(self.comp.parse_block(sys.stdin.read()))
            got_stdin = True
        if args.interact or (
            interact and not (
                got_stdin
                or args.source
                or args.code
                or args.tutorial
                or args.docs
                or args.watch
                or args.site_uninstall
                or args.site_install
                or args.jupyter is not None
                or args.mypy == [mypy_install_arg]
            )
        ):
            self.start_prompt()
        if args.watch:
            # src_dest_package_triples is always available here
            self.watch(src_dest_package_triples, args.run, args.force)
        if args.profile:
            print_timing_info()