[IPython-user] pyreadline question

Michael Foord fuzzyman at voidspace.org.uk
Sun Apr 2 11:55:26 CDT 2006


Jörgen Stenarson wrote:
> Michael Foord skrev:
>> Michael Foord wrote:
>>> Hello all,
>>>
>>> I'm updating Movable Python and am including pyreadline.
>>>
>>> First of all the instructions on installing from subversion seem to 
>>> include an incorrect step.
>>>
>>>  From http://projects.scipy.org/ipython/ipython/wiki/PyReadline/Intro
>>>
>>>     "copy pyreadlineconfig.ini from pyreadline-egg/pyreadline/config"
>>>
>>> After running ``setup.py install`` I have a 
>>> ``pyreadline/configuration`` directory which contains the ini file, 
>>> but no ``pyreadline-egg`` directory and no ``config`` directory. I 
>>> assume this is the right file anyway.
>>>
>>> The instructions say to copy this file to "your homedir". Movable 
>>> Python is intended to be run from a USB stick on arbitrary 
>>> computers, so I would like to be able to specify an alternative 
>>> location for this file. How do I do this ?
>>>
>>> I don't mind patching the distribution, but I would prefer a better 
>>> solution.
>
> Michael,
> I have attached patches to ipython and pyreadline that should make it 
> possible to set your own path to the configfile.
>
> I had to make some backwards incompatible changes to how pyreadline is 
> started. Now the recommended way is to "import readline" to initialize 
> pyreadline, if you want to set the config_path do:
>
This is great - many thanks.

I *had* decided just to use the old readline for the next version of 
Movable Python (probably sometime next week). Looks like that has changed.

When I get to hacking this together I'll let you know how I get on.

All the best,

Fuzzyman
http://www.voidspace.org.uk/python/index.shtml

> import pyreadline.rlmain
> pyreadline.rlmain.config_path=r"c:\xxx\pyreadlineconfig.ini"
>
> before "import readline".
>
> Ville will have to decide on how he wants to expose this in ipython. A 
> workaround for now is to add those two lines before "import IPython" 
> in the ipython.py startup script.
>
> I'll hold off comitting the pyreadline_config patch until I hear from 
> Ville. So I can apply the patch to pyreadline after the patch to 
> ipython to avoid having a pyreadline that will not work with ipython.
>
> /Jörgen
> ------------------------------------------------------------------------
>
> Index: IPython/rlineimpl.py
> ===================================================================
> --- IPython/rlineimpl.py	(revision 1230)
> +++ IPython/rlineimpl.py	(working copy)
> @@ -15,10 +15,13 @@
>  
>  if sys.platform == 'win32':
>      try:
> -        from pyreadline import *
> +        import pyreadline.rlmain
> +        #add config for inputrcpath here:
> +        #pyreadline.rlmain.config_path="c:/python/test_config.ini"
> +        from readline import *
>          print "Using the new pyreadline (thanks for participating in the testing!)"
>          have_readline = True
> -        import pyreadline as _rl
> +        import readline as _rl
>      except ImportError:
>          print "IPython team recommends the new pyreadline for Windows use, it wasn't found."
>          print "It's superior especially with non-US keyboard layouts."
>   
> ------------------------------------------------------------------------
>
> Index: pyreadline/__init__.py
> ===================================================================
> --- pyreadline/__init__.py	(revision 1230)
> +++ pyreadline/__init__.py	(working copy)
> @@ -7,8 +7,9 @@
>  #  the file COPYING, distributed as part of this software.
>  #*****************************************************************************
>  
> +"""
> +from rlmain import *
>  
> -from rlmain import *
>  __all__ = [ 'parse_and_bind',
>              'get_line_buffer',
>              'insert_text',
> @@ -27,3 +28,4 @@
>              'get_completer_delims',
>              'add_history',
>              'GetOutputFile']
> +"""
> \ No newline at end of file
> Index: pyreadline/configuration/startup.py
> ===================================================================
> --- pyreadline/configuration/startup.py	(revision 1230)
> +++ pyreadline/configuration/startup.py	(working copy)
> @@ -1,14 +1,16 @@
>  # -*- coding: UTF-8 -*-
>  # Example snippet to use in a PYTHONSTARTUP file
>  try:
> -    import pyreadline,atexit
> +    import pyreadline.rlmain
> +    #pyreadline.rlmain.config_path=r"c:\xxx\pyreadlineconfig.ini"
> +    import readline,atexit
>  except ImportError:
>      print "Module readline not available."
>  else:
>      #import tab completion functionality
>      import rlcompleter
>      #activate tab completion
> -    pyreadline.parse_and_bind("tab: complete")
> -    pyreadline.rl.read_history_file()
> -    atexit.register(pyreadline.rl.write_history_file)
> -    del pyreadline,rlcompleter,atexit
> +    readline.parse_and_bind("tab: complete")
> +    readline.read_history_file()
> +    atexit.register(readline.write_history_file)
> +    del readline,rlcompleter,atexit
> Index: pyreadline/console.py
> ===================================================================
> --- pyreadline/console.py	(revision 1230)
> +++ pyreadline/console.py	(working copy)
> @@ -17,6 +17,7 @@
>  import traceback
>  import re
>  from logger import log
> +import os
>  
>  try:
>      # I developed this with ctypes 0.6
> @@ -144,6 +145,7 @@
>      'SetConsoleTitleA',
>      'SetConsoleWindowInfo',
>      'WriteConsoleA',
> +    'WriteFile',
>      'WriteConsoleOutputCharacterA',
>      ]
>  
> @@ -347,6 +349,13 @@
>          self.SetConsoleTextAttribute(self.hout, attr)
>          self.WriteConsoleA(self.hout, text, len(text), byref(n), None)
>          return len(text)
> +        
> +    if os.environ.get("EMACS","not")=="t":
> +        def write_color(self, text, attr=None):
> +            junk = c_int(0)
> +            self.WriteFile(self.hout, text, len(text), byref(junk), None)
> +            return len(text)
> +        write_plain = write_color
>  
>      # make this class look like a file object
>      def write(self, text):
> Index: pyreadline/init_rl.py
> ===================================================================
> --- pyreadline/init_rl.py	(revision 0)
> +++ pyreadline/init_rl.py	(revision 0)
> @@ -0,0 +1,33 @@
> +from rlmain import *
> +
> +rl = Readline()
> +
> +def GetOutputFile():
> +    '''Return the console object used by readline so that it can be used for printing in color.'''
> +    return rl.console
> +
> +# make these available so this looks like the python readline module
> +parse_and_bind = rl.parse_and_bind
> +get_line_buffer = rl.get_line_buffer
> +insert_text = rl.insert_text
> +read_init_file = rl.read_init_file
> +read_history_file = rl.read_history_file
> +write_history_file = rl.write_history_file
> +get_history_length = rl.get_history_length
> +set_history_length = rl.set_history_length
> +set_startup_hook = rl.set_startup_hook
> +set_pre_input_hook = rl.set_pre_input_hook
> +set_completer = rl.set_completer
> +get_completer = rl.get_completer
> +get_begidx = rl.get_begidx
> +get_endidx = rl.get_endidx
> +set_completer_delims = rl.set_completer_delims
> +get_completer_delims = rl.get_completer_delims
> +add_history = rl.add_history
> +
> +if __name__ == '__main__':
> +    res = [ rl.readline('In[%d] ' % i) for i in range(3) ]
> +    print res
> +else:
> +    console.install_readline(rl.readline)
> +
>
> Property changes on: pyreadline\init_rl.py
> ___________________________________________________________________
> Name: svn:keywords
>    + Id Author Revision Date
>
> Index: pyreadline/release.py
> ===================================================================
> --- pyreadline/release.py	(revision 1230)
> +++ pyreadline/release.py	(working copy)
> @@ -20,6 +20,7 @@
>  # because bdist_rpm does not accept dashes (an RPM) convention, and
>  # bdist_deb does not accept underscores (a Debian convention).
>  
> +branch = 'trunk'
>  
>  version = '1.3.svn'
>  
> Index: pyreadline/rlmain.py
> ===================================================================
> --- pyreadline/rlmain.py	(revision 1230)
> +++ pyreadline/rlmain.py	(working copy)
> @@ -23,6 +23,7 @@
>  import clipboard,logger,console
>  from   logger import log
>  from   keysyms import key_text_to_keyinfo
> +import release
>  
>  def quote_char(c):
>      if ord(c)>0:
> @@ -34,6 +35,8 @@
>  def inword(buffer,point):
>      return buffer[point:point+1] in [A-Za-z0-9]
>  
> +config_path=os.path.expanduser("~/pyreadlineconfig.ini")
> +
>  class Readline:
>      def __init__(self):
>          self.startup_hook = None
> @@ -1187,10 +1190,14 @@
>          mode.'''
>          pass
>  
> -    def read_inputrc(self,inputrcpath=os.path.expanduser("~/pyreadlineconfig.ini")):
> +    def read_inputrc(self,inputrcpath=None):
> +        if inputrcpath is None:
> +            inputrcpath=config_path  
>          def bind_key(key,name):
>              if hasattr(self,name):
>                  self._bind_key(key,getattr(self,name))
> +            else:
> +                print "Warning %s is not a valid function for bind_key"%name
>          def un_bind_key(key):
>              keyinfo = key_text_to_keyinfo(key)
>              if keyinfo in self.key_dispatch:
> @@ -1220,7 +1227,8 @@
>          def debug_output(on,filename="pyreadline_debug_log.txt"):  #Not implemented yet
>              logger.start_log(on,filename)
>              logger.log("STARTING LOG")
> -        loc={"bind_key":bind_key,
> +        loc={"branch":release.branch,
> +             "bind_key":bind_key,
>               "bind_exit_key":bind_exit_key,
>               "un_bind_key":un_bind_key,
>               "un_bind_exit_key":un_bind_exit_key,
> @@ -1263,34 +1271,3 @@
>      return prefix
>  
>  # create a Readline object to contain the state
> -rl = Readline()
> -
> -def GetOutputFile():
> -    '''Return the console object used by readline so that it can be used for printing in color.'''
> -    return rl.console
> -
> -# make these available so this looks like the python readline module
> -parse_and_bind = rl.parse_and_bind
> -get_line_buffer = rl.get_line_buffer
> -insert_text = rl.insert_text
> -read_init_file = rl.read_init_file
> -read_history_file = rl.read_history_file
> -write_history_file = rl.write_history_file
> -get_history_length = rl.get_history_length
> -set_history_length = rl.set_history_length
> -set_startup_hook = rl.set_startup_hook
> -set_pre_input_hook = rl.set_pre_input_hook
> -set_completer = rl.set_completer
> -get_completer = rl.get_completer
> -get_begidx = rl.get_begidx
> -get_endidx = rl.get_endidx
> -set_completer_delims = rl.set_completer_delims
> -get_completer_delims = rl.get_completer_delims
> -add_history = rl.add_history
> -
> -if __name__ == '__main__':
> -    res = [ rl.readline('In[%d] ' % i) for i in range(3) ]
> -    print res
> -else:
> -    console.install_readline(rl.readline)
> -
> Index: readline.py
> ===================================================================
> --- readline.py	(revision 1230)
> +++ readline.py	(working copy)
> @@ -2,4 +2,26 @@
>  #this file is needed in site-packages to emulate readline
>  #necessary for rlcompleter since it relies on the existance
>  #of a readline module
> -from pyreadline import *
> +
> +import pyreadline.rlmain
> +#pyreadline.rlmain.config_path=r"C:\xxx\pyreadlineconfig.ini"
> +from pyreadline.init_rl import *
> +
> +__all__ = [ 'parse_and_bind',
> +            'get_line_buffer',
> +            'insert_text',
> +            'read_init_file',
> +            'read_history_file',
> +            'write_history_file',
> +            'get_history_length',
> +            'set_history_length',
> +            'set_startup_hook',
> +            'set_pre_input_hook',
> +            'set_completer',
> +            'get_completer',
> +            'get_begidx',
> +            'get_endidx',
> +            'set_completer_delims',
> +            'get_completer_delims',
> +            'add_history',
> +            'GetOutputFile']
>   




More information about the IPython-user mailing list