Esempio n. 1
0
    def watch(self, source, write=True, package=None, run=False, force=False):
        """Watches a source and recompiles on change."""
        from coconut.command.watch import Observer, RecompilationWatcher

        source = fixpath(source)

        print()
        logger.show_tabulated("Watching", showpath(source), "(press Ctrl-C to end)...")

        def recompile(path):
            if os.path.isfile(path) and os.path.splitext(path)[1] in code_exts:
                with self.handling_exceptions():
                    self.run_mypy(self.compile_path(path, write, package, run, force))

        observer = Observer()
        observer.schedule(RecompilationWatcher(recompile), source, recursive=True)

        with self.running_jobs():
            observer.start()
            try:
                while True:
                    time.sleep(watch_interval)
            except KeyboardInterrupt:
                logger.show("Got KeyboardInterrupt; stopping watcher.")
            finally:
                observer.stop()
                observer.join()
Esempio n. 2
0
    def watch(self, source, write=True, package=None, run=False, force=False):
        """Watches a source and recompiles on change."""
        from coconut.command.watch import Observer, RecompilationWatcher

        source = fixpath(source)

        print()
        logger.show_tabulated("Watching", showpath(source),
                              "(press Ctrl-C to end)...")

        def recompile(path):
            if os.path.isfile(path) and os.path.splitext(path)[1] in code_exts:
                with self.handling_exceptions():
                    self.run_mypy(
                        self.compile_path(path, write, package, run, force))

        observer = Observer()
        observer.schedule(RecompilationWatcher(recompile),
                          source,
                          recursive=True)

        with self.running_jobs():
            observer.start()
            try:
                while True:
                    time.sleep(watch_interval)
            except KeyboardInterrupt:
                logger.show("Got KeyboardInterrupt; stopping watcher.")
            finally:
                observer.stop()
                observer.join()
Esempio n. 3
0
 def exit_on_error(self):
     """Exit if exit_code is abnormal."""
     if self.exit_code:
         if self.errmsg is not None:
             logger.show("Exiting due to " + self.errmsg + ".")
             self.errmsg = None
         if self.using_jobs:
             kill_children()
         sys.exit(self.exit_code)
Esempio n. 4
0
 def exit_on_error(self):
     """Exit if exit_code is abnormal."""
     if self.exit_code:
         if self.errmsg is not None:
             logger.show("Exiting due to " + self.errmsg + ".")
             self.errmsg = None
         if self.using_jobs:
             kill_children()
         sys.exit(self.exit_code)
Esempio n. 5
0
 def handles_prompt_toolkit_errors_func(self, *args, **kwargs):
     if self.style is not None:
         try:
             return func(self, *args, **kwargs)
         except (KeyboardInterrupt, EOFError):
             raise
         except (Exception, AssertionError):
             logger.print_exc()
             logger.show("Syntax highlighting failed; switching to --style none.")
             self.style = None
     return func(self, *args, **kwargs)
Esempio n. 6
0
 def start_prompt(self):
     """Start the interpreter."""
     logger.show("Coconut Interpreter:")
     logger.show("(type 'exit()' or press Ctrl-D to end)")
     self.start_running()
     while self.running:
         try:
             code = self.get_input()
             if code:
                 compiled = self.handle_input(code)
                 if compiled:
                     self.execute(compiled, use_eval=None)
         except KeyboardInterrupt:
             printerr("\nKeyboardInterrupt")
Esempio n. 7
0
 def start_prompt(self):
     """Start the interpreter."""
     logger.show("Coconut Interpreter:")
     logger.show("(type 'exit()' or press Ctrl-D to end)")
     self.start_running()
     while self.running:
         try:
             code = self.get_input()
             if code:
                 compiled = self.handle_input(code)
                 if compiled:
                     self.execute(compiled, use_eval=None)
         except KeyboardInterrupt:
             printerr("\nKeyboardInterrupt")
Esempio n. 8
0
    def watch(self, source, write=True, package=None, run=False, force=False):
        """Watch a source and recompiles on change."""
        from coconut.command.watch import Observer, RecompilationWatcher

        source = fixpath(source)

        logger.show()
        logger.show_tabulated("Watching", showpath(source),
                              "(press Ctrl-C to end)...")

        def recompile(path):
            path = fixpath(path)
            if os.path.isfile(path) and os.path.splitext(path)[1] in code_exts:
                with self.handling_exceptions():
                    if write is True or write is None:
                        writedir = write
                    else:
                        # correct the compilation path based on the relative position of path to source
                        dirpath = os.path.dirname(path)
                        writedir = os.path.join(
                            write, os.path.relpath(dirpath, source))
                    filepaths = self.compile_path(path,
                                                  writedir,
                                                  package,
                                                  run,
                                                  force,
                                                  show_unchanged=False)
                    self.run_mypy(filepaths)

        watcher = RecompilationWatcher(recompile)
        observer = Observer()
        observer.schedule(watcher, source, recursive=True)

        with self.running_jobs():
            observer.start()
            try:
                while True:
                    time.sleep(watch_interval)
                    watcher.keep_watching()
            except KeyboardInterrupt:
                logger.show_sig("Got KeyboardInterrupt; stopping watcher.")
            finally:
                observer.stop()
                observer.join()
Esempio n. 9
0
File: util.py Progetto: pyzh/coconut
 def input(self, more=False):
     """Prompts for code input."""
     sys.stdout.flush()
     if more:
         msg = more_prompt
     else:
         msg = main_prompt
     if self.style is not None:
         if prompt_toolkit is None:
             raise CoconutInternalException(
                 "cannot highlight style without prompt_toolkit",
                 self.style)
         try:
             return prompt_toolkit.prompt(msg, **self.prompt_kwargs())
         except EOFError:
             raise  # issubclass(EOFError, Exception), so we have to do this
         except Exception:
             logger.print_exc()
             logger.show(
                 "Syntax highlighting failed; switching to --style none.")
             self.style = None
     return input(msg)
Esempio n. 10
0
    def watch(self, source, write=True, package=None, run=False, force=False):
        """Watch a source and recompiles on change."""
        from coconut.command.watch import Observer, RecompilationWatcher

        source = fixpath(source)

        logger.show()
        logger.show_tabulated("Watching", showpath(source), "(press Ctrl-C to end)...")

        def recompile(path):
            path = fixpath(path)
            if os.path.isfile(path) and os.path.splitext(path)[1] in code_exts:
                with self.handling_exceptions():
                    if write is True or write is None:
                        writedir = write
                    else:
                        # correct the compilation path based on the relative position of path to source
                        dirpath = os.path.dirname(path)
                        writedir = os.path.join(write, os.path.relpath(dirpath, source))
                    filepaths = self.compile_path(path, writedir, package, run, force, show_unchanged=False)
                    self.run_mypy(filepaths)

        watcher = RecompilationWatcher(recompile)
        observer = Observer()
        observer.schedule(watcher, source, recursive=True)

        with self.running_jobs():
            observer.start()
            try:
                while True:
                    time.sleep(watch_interval)
                    watcher.keep_watching()
            except KeyboardInterrupt:
                logger.show_sig("Got KeyboardInterrupt; stopping watcher.")
            finally:
                observer.stop()
                observer.join()