@@ -402,148 +402,3 @@ def commonprefix(names, base=''):
402402 if c != s2 [i ]:
403403 return s1 [:i ]
404404 return s1
405-
406-
407- def has_leopard_libedit (config ):
408- # Detect if we are using Leopard's libedit.
409- # Adapted from IPython's rlineimpl.py.
410- if config .using_pyrepl or sys .platform != 'darwin' :
411- return False
412-
413- # Official Python docs state that 'libedit' is in the docstring for
414- # libedit readline.
415- return config .readline .__doc__ and 'libedit' in config .readline .__doc__
416-
417-
418- def setup ():
419- """
420- Install fancycompleter as the default completer for readline.
421- """
422- completer = Completer ()
423- readline = completer .config .readline
424- if has_leopard_libedit (completer .config ):
425- readline .parse_and_bind ("bind ^I rl_complete" )
426- else :
427- readline .parse_and_bind ('tab: complete' )
428- readline .set_completer (completer .complete )
429- return completer
430-
431-
432- def interact_pyrepl ():
433- import sys
434- from pyrepl import readline
435- from pyrepl .simple_interact import run_multiline_interactive_console
436- sys .modules ['readline' ] = readline
437- run_multiline_interactive_console ()
438-
439-
440- def setup_history (completer , persist_history ):
441- import atexit
442- readline = completer .config .readline
443- #
444- if isinstance (persist_history , (str , unicode )):
445- filename = persist_history
446- else :
447- filename = '~/.history.py'
448- filename = os .path .expanduser (filename )
449- if os .path .isfile (filename ):
450- readline .read_history_file (filename )
451-
452- def save_history ():
453- readline .write_history_file (filename )
454- atexit .register (save_history )
455-
456-
457- def interact (persist_history = None ):
458- """
459- Main entry point for fancycompleter: run an interactive Python session
460- after installing fancycompleter.
461-
462- This function is supposed to be called at the end of PYTHONSTARTUP:
463-
464- - if we are using pyrepl: install fancycompleter, run pyrepl multiline
465- prompt, and sys.exit(). The standard python prompt will never be
466- reached
467-
468- - if we are not using pyrepl: install fancycompleter and return. The
469- execution will continue as normal, and the standard python prompt will
470- be displayed.
471-
472- This is necessary because there is no way to tell the standard python
473- prompt to use the readline provided by pyrepl instead of the builtin one.
474-
475- By default, pyrepl is preferred and automatically used if found.
476- """
477- import sys
478- completer = setup ()
479- if persist_history :
480- setup_history (completer , persist_history )
481- if completer .config .using_pyrepl and '__pypy__' not in sys .builtin_module_names :
482- # if we are on PyPy, we don't need to run a "fake" interpeter, as the
483- # standard one is fake enough :-)
484- interact_pyrepl ()
485- sys .exit ()
486-
487-
488- class Installer (object ):
489- """
490- Helper to install fancycompleter in PYTHONSTARTUP
491- """
492-
493- def __init__ (self , basepath , force ):
494- fname = os .path .join (basepath , 'python_startup.py' )
495- self .filename = os .path .expanduser (fname )
496- self .force = force
497-
498- def check (self ):
499- PYTHONSTARTUP = os .environ .get ('PYTHONSTARTUP' )
500- if PYTHONSTARTUP :
501- return 'PYTHONSTARTUP already defined: %s' % PYTHONSTARTUP
502- if os .path .exists (self .filename ):
503- return '%s already exists' % self .filename
504-
505- def install (self ):
506- import textwrap
507- error = self .check ()
508- if error and not self .force :
509- print (error )
510- print ('Use --force to overwrite.' )
511- return False
512- with open (self .filename , 'w' ) as f :
513- f .write (textwrap .dedent ("""
514- import fancycompleter
515- fancycompleter.interact(persist_history=True)
516- """ ))
517- self .set_env_var ()
518- return True
519-
520- def set_env_var (self ):
521- if sys .platform == 'win32' :
522- os .system ('SETX PYTHONSTARTUP "%s"' % self .filename )
523- print ('%PYTHONSTARTUP% set to' , self .filename )
524- else :
525- print ('startup file written to' , self .filename )
526- print ('Append this line to your ~/.bashrc:' )
527- print (' export PYTHONSTARTUP=%s' % self .filename )
528-
529-
530- if __name__ == '__main__' :
531- def usage ():
532- print ('Usage: python -m fancycompleter install [-f|--force]' )
533- sys .exit (1 )
534-
535- cmd = None
536- force = False
537- for item in sys .argv [1 :]:
538- if item in ('install' ,):
539- cmd = item
540- elif item in ('-f' , '--force' ):
541- force = True
542- else :
543- usage ()
544- #
545- if cmd == 'install' :
546- installer = Installer ('~' , force )
547- installer .install ()
548- else :
549- usage ()
0 commit comments