From d6698bbc45eb2b8287ed32d05b9c99c4aeaf542f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=2EFernando=20S=C3=A1nchez?= Date: Sun, 30 Dec 2012 19:55:14 +0100 Subject: [PATCH] Added niceties to the default python shell --- pythonrc.py | 136 ++++++++++++++++++++++++++++++++++++++++++++++++++++ zshrc | 3 ++ 2 files changed, 139 insertions(+) create mode 100644 pythonrc.py diff --git a/pythonrc.py b/pythonrc.py new file mode 100644 index 0000000..24b1a68 --- /dev/null +++ b/pythonrc.py @@ -0,0 +1,136 @@ +""" +This file is executed when the Python interactive shell is started if +$PYTHONSTARTUP is in your environment and points to this file. It's just +regular Python commands, so do what you will. Your ~/.inputrc file can greatly +complement this file. + +""" +import os + +try: + import readline + import rlcompleter + import atexit +except ImportError: + print("You need readline, rlcompleter, and atexit") + +readline.parse_and_bind("tab: complete") +#readline.parse_and_bind ("bind ^I rl_complete") + +class Completer(object): + def __init__(self): + # Enable a History + self.HISTFILE=os.path.expanduser("%s/.pyhistory" % os.environ["HOME"]) + + # Read the existing history if there is one + if os.path.exists(self.HISTFILE): + readline.read_history_file(self.HISTFILE) + + # Set maximum number of items that will be written to the history file + readline.set_history_length(300) + atexit.register(self.savehist) + + def savehist(self): + import readline + readline.write_history_file(self.HISTFILE) + + +c = Completer() + +WELCOME='' +# Color Support +class TermColors(dict): + """Gives easy access to ANSI color codes. Attempts to fall back to no color + for certain TERM values. (Mostly stolen from IPython.)""" + + COLOR_TEMPLATES = ( + ("Black" , "0;30"), + ("Red" , "0;31"), + ("Green" , "0;32"), + ("Brown" , "0;33"), + ("Blue" , "0;34"), + ("Purple" , "0;35"), + ("Cyan" , "0;36"), + ("LightGray" , "0;37"), + ("DarkGray" , "1;30"), ("LightRed" , "1;31"), + ("LightGreen" , "1;32"), + ("Yellow" , "1;33"), + ("LightBlue" , "1;34"), + ("LightPurple" , "1;35"), + ("LightCyan" , "1;36"), + ("White" , "1;37"), + ("Normal" , "0"), + ) + + NoColor = '' + _base = '\001\033[%sm\002' + + def __init__(self): + if os.environ.get('TERM') in ('xterm-color', 'xterm-256color', 'linux', + 'screen', 'screen-256color', 'screen-bce'): + self.update(dict([(k, self._base % v) for k,v in self.COLOR_TEMPLATES])) + else: + self.update(dict([(k, self.NoColor) for k,v in self.COLOR_TEMPLATES])) +_c = TermColors() + + + +import sys +# Enable Color Prompts +sys.ps1 = '%s>>> %s' % (_c['Green'], _c['Normal']) +sys.ps2 = '%s... %s' % (_c['Red'], _c['Normal']) + +# Enable Pretty Printing for stdout +def my_displayhook(value): + if value is not None: + try: + import __builtin__ + __builtin__._ = value + except ImportError: + __builtins__._ = value + + import pprint + pprint.pprint(value) + del pprint + +sys.displayhook = my_displayhook + +EDITOR = os.environ.get('EDITOR', 'vim') +EDIT_CMD = '\e' + +from tempfile import mkstemp +from code import InteractiveConsole + +class EditableBufferInteractiveConsole(InteractiveConsole): + def __init__(self, *args, **kwargs): + self.last_buffer = [] # This holds the last executed statement + InteractiveConsole.__init__(self, *args, **kwargs) + + def runsource(self, source, *args): + self.last_buffer = [ source.encode('latin-1') ] + return InteractiveConsole.runsource(self, source, *args) + + def raw_input(self, *args): + line = InteractiveConsole.raw_input(self, *args) + if line == EDIT_CMD: + fd, tmpfl = mkstemp('.py') + os.write(fd, b'\n'.join(self.last_buffer)) + os.close(fd) + os.system('%s %s' % (EDITOR, tmpfl)) + line = open(tmpfl).read() + os.unlink(tmpfl) + tmpfl = '' + lines = line.split( '\n' ) + for i in range(len(lines) - 1): self.push( lines[i] ) + line = lines[-1] + return line + +# clean up namespace +del sys + +c = EditableBufferInteractiveConsole(locals=locals()) +c.interact(banner=WELCOME) + +# Exit the Python shell on exiting the InteractiveConsole +import sys +sys.exit() diff --git a/zshrc b/zshrc index 06d9c2a..b0be5f1 100644 --- a/zshrc +++ b/zshrc @@ -39,6 +39,9 @@ source $ZSH/oh-my-zsh.sh # Customize to your needs... export PATH=/usr/lib/lightdm/lightdm:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/home/balkian/code/personal-scripts/ +PYTHONSTARTUP=~/.pythonrc.py +export PYTHONSTARTUP + # TMUX # if [[ -z "$KONSOLE_DBUS_SERVICE" & `which tmux` ]]; then # if no session is started, start a new session