Skip to content

Commit 8caa915

Browse files
committed
Make Interaction an abstract base class
1 parent a808521 commit 8caa915

File tree

4 files changed

+45
-25
lines changed

4 files changed

+45
-25
lines changed

bpython/cli.py

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -319,14 +319,14 @@ def make_colors(config: Config) -> Dict[str, int]:
319319

320320

321321
class CLIInteraction(repl.Interaction):
322-
def __init__(self, config: Config, statusbar: Optional["Statusbar"] = None):
323-
super().__init__(config, statusbar)
322+
def __init__(self, config: Config, statusbar: "Statusbar"):
323+
super().__init__(config)
324+
self.statusbar = statusbar
324325

325326
def confirm(self, q: str) -> bool:
326327
"""Ask for yes or no and return boolean"""
327328
try:
328-
if self.statusbar:
329-
reply = self.statusbar.prompt(q)
329+
reply = self.statusbar.prompt(q)
330330
except ValueError:
331331
return False
332332

@@ -335,14 +335,10 @@ def confirm(self, q: str) -> bool:
335335
def notify(
336336
self, s: str, n: int = 10, wait_for_keypress: bool = False
337337
) -> None:
338-
if self.statusbar:
339-
self.statusbar.message(s, n)
338+
self.statusbar.message(s, n)
340339

341340
def file_prompt(self, s: str) -> Optional[str]:
342-
if self.statusbar:
343-
return self.statusbar.prompt(s)
344-
else:
345-
return None
341+
return self.statusbar.prompt(s)
346342

347343

348344
class CLIRepl(repl.Repl):
@@ -1675,7 +1671,7 @@ def check(self) -> None:
16751671

16761672
self.settext(self._s)
16771673

1678-
def message(self, s: str, n: int = 3) -> None:
1674+
def message(self, s: str, n: float = 3.0) -> None:
16791675
"""Display a message for a short n seconds on the statusbar and return
16801676
it to its original state."""
16811677
self.timer = int(time.time() + n)

bpython/curtsiesfrontend/interaction.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ def __init__(
3939
self.prompt = ""
4040
self._message = ""
4141
self.message_start_time = time.time()
42-
self.message_time = 3
42+
self.message_time = 3.0
4343
self.permanent_stack = []
4444
if permanent_text:
4545
self.permanent_stack.append(permanent_text)
@@ -149,7 +149,7 @@ def should_show_message(self):
149149
return bool(self.current_line)
150150

151151
# interaction interface - should be called from other greenlets
152-
def notify(self, msg, n=3, wait_for_keypress=False):
152+
def notify(self, msg, n=3.0, wait_for_keypress=False):
153153
self.request_context = greenlet.getcurrent()
154154
self.message_time = n
155155
self.message(msg, schedule_refresh=wait_for_keypress)

bpython/repl.py

Lines changed: 30 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2222
# THE SOFTWARE.
2323

24+
import abc
2425
import code
2526
import inspect
2627
import os
@@ -346,21 +347,39 @@ def clear(self) -> None:
346347
self.index = -1
347348

348349

349-
class Interaction:
350-
def __init__(self, config: Config, statusbar: Optional["Statusbar"] = None):
350+
class Interaction(metaclass=abc.ABCMeta):
351+
def __init__(self, config: Config):
351352
self.config = config
352353

353-
if statusbar:
354-
self.statusbar = statusbar
354+
@abc.abstractmethod
355+
def confirm(self, s: str) -> bool:
356+
pass
355357

356-
def confirm(self, s):
357-
raise NotImplementedError
358+
@abc.abstractmethod
359+
def notify(
360+
self, s: str, n: float = 10.0, wait_for_keypress: bool = False
361+
) -> None:
362+
pass
363+
364+
@abc.abstractmethod
365+
def file_prompt(self, s: str) -> Optional[str]:
366+
pass
367+
368+
369+
class NoInteraction(Interaction):
370+
def __init__(self, config: Config):
371+
super().__init__(config)
358372

359-
def notify(self, s, n=10, wait_for_keypress=False):
360-
raise NotImplementedError
373+
def confirm(self, s: str) -> bool:
374+
return False
375+
376+
def notify(
377+
self, s: str, n: float = 10.0, wait_for_keypress: bool = False
378+
) -> None:
379+
pass
361380

362-
def file_prompt(self, s):
363-
raise NotImplementedError
381+
def file_prompt(self, s: str) -> Optional[str]:
382+
return None
364383

365384

366385
class SourceNotFound(Exception):
@@ -458,7 +477,7 @@ def __init__(self, interp: Interpreter, config: Config):
458477
] = None
459478
self._C: Dict[str, int] = {}
460479
self.prev_block_finished: int = 0
461-
self.interact = Interaction(self.config)
480+
self.interact: Interaction = NoInteraction(self.config)
462481
# previous pastebin content to prevent duplicate pastes, filled on call
463482
# to repl.pastebin
464483
self.prev_pastebin_content = ""

bpython/urwid.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import locale
3939
import signal
4040
import urwid
41+
from typing import Optional
4142

4243
from . import args as bpargs, repl, translations
4344
from .formatter import theme_map
@@ -526,7 +527,8 @@ def render(self, size, focus=False):
526527

527528
class URWIDInteraction(repl.Interaction):
528529
def __init__(self, config, statusbar, frame):
529-
super().__init__(config, statusbar)
530+
super().__init__(config)
531+
self.statusbar = statusbar
530532
self.frame = frame
531533
urwid.connect_signal(statusbar, "prompt_result", self._prompt_result)
532534
self.callback = None
@@ -563,6 +565,9 @@ def _prompt_result(self, text):
563565
self.callback = None
564566
callback(text)
565567

568+
def file_prompt(self, s: str) -> Optional[str]:
569+
raise NotImplementedError
570+
566571

567572
class URWIDRepl(repl.Repl):
568573

0 commit comments

Comments
 (0)