From 86f5aae3b239280df0db5304b6e0092b1ad2124b Mon Sep 17 00:00:00 2001 From: Oleg Broytman Date: Tue, 19 Apr 2016 13:27:21 +0300 Subject: [PATCH] init.py: refactor paging --- lib/python/init.py | 58 ++++++++++++++++++++++++++++++---------------- 1 file changed, 38 insertions(+), 20 deletions(-) diff --git a/lib/python/init.py b/lib/python/init.py index c0afb68..15bcb32 100644 --- a/lib/python/init.py +++ b/lib/python/init.py @@ -121,29 +121,56 @@ def init(): # set displayhook from pprint import pprint + + class BasePPrintPager: + def pipe(self, value): + pprint(value, stream=self.stdin) + + def close(self): + self.stdin.close() + try: from subprocess import Popen, PIPE except ImportError: - use_subprocess = False + class PPrintPager(BasePPrintPager): + def __init__(self): + self.pipe = Popen(pager, shell=True, stdin=PIPE) + self.stdin = self.pipe.stdin + + def close(self): + BasePPrintPager.close(self) + self.pipe.wait() else: - use_subprocess = True + class PPrintPager(BasePPrintPager): + def __init__(self): + self.stdin = os.popen(pager, 'w') pager = os.environ.get("PAGER") or 'more' def displayhook(value): if value is not None: __builtin__._ = value - if use_subprocess: - _pipe = Popen(pager, shell=True, stdin=PIPE) - pipe = _pipe.stdin - else: - pipe = os.popen(pager, 'w') - pprint(value, stream=pipe) - pipe.close() - if use_subprocess: - _pipe.wait() + pprint_pager = PPrintPager() + pprint_pager.pipe(value) + pprint_pager.close() sys.displayhook = displayhook + from traceback import print_exception + + def excepthook(etype, evalue, etraceback): + print_exception(etype, evalue, etraceback) + + sys.excepthook = excepthook + + # From Thomas Heller: + # https://mail.python.org/pipermail/python-list/2001-April/099020.html + + # import pdb + # + # def info(*args): + # pdb.pm() + # sys.excepthook = info + # utilities # From: Paul Magwene: @@ -181,15 +208,6 @@ def init(): __builtin__.ls = DirLister() __builtin__.cd = DirChanger() - # From Thomas Heller: - # https://mail.python.org/pipermail/python-list/2001-April/099020.html - - # import pdb - # - # def info(*args): - # pdb.pm() - # sys.excepthook = info - # print working directory class Pwd: -- 2.39.2