X-Git-Url: https://git.phdru.name/?a=blobdiff_plain;f=lib%2Fpython%2Finit.py;h=1690a42ab9d2996a9fe35e3a681ea62942a53e91;hb=b979a2004c12b0a5b4e61661322717e6c8556b8f;hp=642eb6c6ab545f3f52443768c2fcebb61c056b08;hpb=b0cebb6cdb495409e43550c82c68258c3269d179;p=dotfiles.git diff --git a/lib/python/init.py b/lib/python/init.py index 642eb6c..1690a42 100644 --- a/lib/python/init.py +++ b/lib/python/init.py @@ -27,6 +27,7 @@ def init(): # https://mail.python.org/pipermail/python-list/2001-March/062888.html try: + import rlcompleter # noqa: need for completion import readline initfile = os.environ.get('INPUTRC') \ or os.path.expanduser('~/.inputrc') @@ -113,21 +114,87 @@ def init(): else: try: locale.setlocale(locale.LC_ALL, '') - except (ImportError, locale.Error): pass # no locale support or unsupported locale - # set displayhook + # set displayhook and excepthook from pprint import pprint + pager = os.environ.get("PAGER") or 'more' + + # if your pager is 'less', options '-F' and '-R' must be passed to it, + # and option '-X' is very much recommended + if pager == 'less': + less = os.environ.get("LESS") or '' + for opt in 'X', 'R', 'F': + if opt not in less: + less = opt + less + os.environ["LESS"] = less + + class BasePager: + def write(self, value): + self.stdin.write(value) + + def pprint(self, value): + pprint(value, stream=self.stdin) + + def close(self): + self.stdin.close() + + try: + from subprocess import Popen, PIPE + except ImportError: + class Pager(BasePager): + def __init__(self): + self.pipe = Popen(pager, shell=True, stdin=PIPE) + self.stdin = self.pipe.stdin + + def close(self): + BasePager.close(self) + self.pipe.wait() + else: + class Pager(BasePager): + def __init__(self): + self.stdin = os.popen(pager, 'w') def displayhook(value): if value is not None: __builtin__._ = value - pprint(value) + pager = Pager() + pager.pprint(value) + pager.close() sys.displayhook = displayhook + from traceback import format_exception + + def excepthook(etype, evalue, etraceback): + lines = format_exception(etype, evalue, etraceback) + pager = Pager() + for line in lines: + pager.write( + '\033[31m' + line.rstrip('\n') + '\033[0m\n') # red, reset + pager.close() + + sys.excepthook = excepthook + + #try: + # import cgitb + #except ImportError: + # pass + #else: + # # cgitb.enable() overrides sys.excepthook + # cgitb.enable(format='text') + + # 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: @@ -165,15 +232,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: @@ -233,7 +291,6 @@ def init(): return "Usage: pager('filename')" def __call__(self, filename): - pager = os.environ.get("PAGER") or 'more' os.system("%s '%s'" % (pager, filename.replace("'", '"\'"'))) __builtin__.pager = _Pager()