From f73bf0e01400402069c32b432fd9acdd665a084f Mon Sep 17 00:00:00 2001 From: Oleg Broytman Date: Wed, 4 Feb 2015 21:36:00 +0300 Subject: [PATCH] python/init.py: add new builtins: cat, sh, pager, editor --- lib/python/init.py | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/lib/python/init.py b/lib/python/init.py index d835c1c..23e950b 100644 --- a/lib/python/init.py +++ b/lib/python/init.py @@ -183,5 +183,51 @@ def init(): __builtin__.exit = __builtin__.quit = x + class _Cat: + def __repr__(self): + return "Usage: cat('filename')" + + def __call__(self, filename): + fp = open(filename, 'rU') + text = fp.read() + fp.close() + print text + + __builtin__.cat = _Cat() + + + class _Sh: + def __repr__(self): + os.system(os.environ["SHELL"]) + return '' + + def __call__(self, cmdline): + os.system(cmdline) + + __builtin__.sh = _Sh() + + + class _Pager: + def __repr__(self): + return "Usage: pager('filename')" + + def __call__(self, filename): + pager = os.environ["PAGER"] or 'more' + os.system("%s '%s'" % (pager, filename.replace("'", '"\'"'))) + + __builtin__.pager = _Pager() + + + class _Editor: + def __repr__(self): + return "Usage: edit('filename')" + + def __call__(self, filename): + editor = os.environ["VISUAL"] or os.environ["EDITOR"] or 'vi' + os.system("%s '%s'" % (editor, filename.replace("'", '"\'"'))) + + __builtin__.edit = __builtin__.editor = _Editor() + + init() del init -- 2.39.2