Skip to content

Commit bae3fd0

Browse files
committed
Override get_session_formatted_for_file in curtsies Repl
1 parent 8bd20fc commit bae3fd0

File tree

2 files changed

+32
-36
lines changed

2 files changed

+32
-36
lines changed

bpython/curtsiesfrontend/repl.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
import tempfile
1111
import time
1212
import unicodedata
13+
from enum import Enum
1314

1415
import blessings
1516
import greenlet
@@ -50,7 +51,6 @@
5051
from ..repl import (
5152
Repl,
5253
SourceNotFound,
53-
LineTypeTranslator as LineType,
5454
)
5555
from ..translations import _
5656

@@ -70,6 +70,14 @@
7070
MAX_EVENTS_POSSIBLY_NOT_PASTE = 20
7171

7272

73+
class LineType(Enum):
74+
"""Used when adding a tuple to all_logical_lines, to get input / output values
75+
having to actually type/know the strings"""
76+
77+
INPUT = "input"
78+
OUTPUT = "output"
79+
80+
7381
class FakeStdin:
7482
"""The stdin object user code will reference
7583
@@ -365,7 +373,7 @@ def __init__(
365373
# Entries are tuples, where
366374
# - the first element the line (string, not fmtsr)
367375
# - the second element is one of 2 global constants: "input" or "output"
368-
# (use LineType.INPUT or LineType.OUTPUT to avoid typing these strings)
376+
# (use LineTypeTranslator.INPUT or LineTypeTranslator.OUTPUT to avoid typing these strings)
369377
self.all_logical_lines = []
370378

371379
# formatted version of lines in the buffer kept around so we can
@@ -2032,6 +2040,17 @@ def key_help_text(self):
20322040
"{} : {}".format(func.rjust(max_func), key) for func, key in pairs
20332041
)
20342042

2043+
def get_session_formatted_for_file(self) -> str:
2044+
def process():
2045+
for line, lineType in self.all_logical_lines:
2046+
if lineType == LineType.INPUT:
2047+
yield line
2048+
elif line.rstrip():
2049+
yield "# OUT: %s" % line
2050+
yield "### %s" % self.current_line
2051+
2052+
return "\n".join(process())
2053+
20352054

20362055
def is_nop(char):
20372056
return unicodedata.category(str(char)) == "Cc"

bpython/repl.py

Lines changed: 11 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
import textwrap
3434
import time
3535
import traceback
36-
from enum import Enum
3736
from itertools import takewhile
3837
from pathlib import Path
3938
from pygments.lexers import Python3Lexer
@@ -355,14 +354,6 @@ class SourceNotFound(Exception):
355354
"""Exception raised when the requested source could not be found."""
356355

357356

358-
class LineTypeTranslator(Enum):
359-
"""Used when adding a tuple to all_logical_lines, to get input / output values
360-
having to actually type/know the strings"""
361-
362-
INPUT = "input"
363-
OUTPUT = "output"
364-
365-
366357
class Repl:
367358
"""Implements the necessary guff for a Python-repl-alike interface
368359
@@ -778,37 +769,23 @@ def line_is_empty(line):
778769
indentation = 0
779770
return indentation
780771

781-
def get_session_formatted_for_file(self):
772+
def get_session_formatted_for_file(self) -> str:
782773
"""Format the stdout buffer to something suitable for writing to disk,
783774
i.e. without >>> and ... at input lines and with "# OUT: " prepended to
784775
output lines and "### " prepended to current line"""
785776

786-
if hasattr(self, "all_logical_lines"):
787-
# Curtsies
788-
789-
def process():
790-
for line, lineType in self.all_logical_lines:
791-
if lineType == LineTypeTranslator.INPUT:
792-
yield line
793-
elif line.rstrip():
794-
yield "# OUT: %s" % line
795-
yield "### %s" % self.current_line
796-
797-
return "\n".join(process())
798-
799-
else: # cli and Urwid
800-
session_output = self.getstdout()
777+
session_output = self.getstdout()
801778

802-
def process():
803-
for line in session_output.split("\n"):
804-
if line.startswith(self.ps1):
805-
yield line[len(self.ps1) :]
806-
elif line.startswith(self.ps2):
807-
yield line[len(self.ps2) :]
808-
elif line.rstrip():
809-
yield f"# OUT: {line}"
779+
def process():
780+
for line in session_output.split("\n"):
781+
if line.startswith(self.ps1):
782+
yield line[len(self.ps1) :]
783+
elif line.startswith(self.ps2):
784+
yield line[len(self.ps2) :]
785+
elif line.rstrip():
786+
yield f"# OUT: {line}"
810787

811-
return "\n".join(process())
788+
return "\n".join(process())
812789

813790
def write2file(self):
814791
"""Prompt for a filename and write the current contents of the stdout

0 commit comments

Comments
 (0)