[python] Move REPL-specific code into repl() method
@@ -6,13 +6,6 @@ from pathlib import Path
import sys
from typing import Dict, List, Union
-try:
- # importing readline on compatible platforms
- # changes how `input` works for the REPL
- import readline # noqa: F401
-except ImportError:
- pass
-
from .exceptions import (
PolarRuntimeError,
InlineQueryFailedError,
@@ -26,35 +19,6 @@ from .host import Host
from .query import Query
from .predicate import Predicate
-
-# https://github.com/django/django/blob/3e753d3de33469493b1f0947a2e0152c4000ed40/django/core/management/color.py
-def supports_color():
- supported_platform = sys.platform != "win32" or "ANSICON" in os.environ
- is_a_tty = hasattr(sys.stdout, "isatty") and sys.stdout.isatty()
- return supported_platform and is_a_tty
-
-
-RESET = ""
-FG_BLUE = ""
-FG_RED = ""
-
-
-if supports_color():
- # \001 and \002 signal these should be ignored by readline. Explanation of
- # the issue: https://stackoverflow.com/a/9468954/390293. Issue has been
- # observed in the Python REPL on Linux by @samscott89 and @plotnick, but
- # not on macOS or Windows (with readline installed) or in the Ruby or
- # Node.js REPLs, both of which also use readline.
- RESET = "\001\x1b[0m\002"
- FG_BLUE = "\001\x1b[34m\002"
- FG_RED = "\001\x1b[31m\002"
-
-
-def print_error(error):
- print(FG_RED + type(error).__name__ + RESET)
- print(error)
-
-
CLASSES: Dict[str, type] = {}
@@ -198,6 +162,37 @@ class Polar:
def repl(self, files=[]):
"""Start an interactive REPL session."""
+ try:
+ # importing readline on compatible platforms
+ # changes how `input` works for the REPL
+ import readline # noqa: F401
+ except ImportError:
+ pass
+
+ # https://github.com/django/django/blob/3e753d3de33469493b1f0947a2e0152c4000ed40/django/core/management/color.py
+ def supports_color():
+ supported_platform = sys.platform != "win32" or "ANSICON" in os.environ
+ is_a_tty = hasattr(sys.stdout, "isatty") and sys.stdout.isatty()
+ return supported_platform and is_a_tty
+
+ RESET = ""
+ FG_BLUE = ""
+ FG_RED = ""
+
+ if supports_color():
+ # \001 and \002 signal these should be ignored by readline. Explanation of
+ # the issue: https://stackoverflow.com/a/9468954/390293. Issue has been
+ # observed in the Python REPL on Linux by @samscott89 and @plotnick, but
+ # not on macOS or Windows (with readline installed) or in the Ruby or
+ # Node.js REPLs, both of which also use readline.
+ RESET = "\001\x1b[0m\002"
+ FG_BLUE = "\001\x1b[34m\002"
+ FG_RED = "\001\x1b[31m\002"
+
+ def print_error(error):
+ print(FG_RED + type(error).__name__ + RESET)
+ print(error)
+
self.load_files(files)
while True:
