a
    ÷£BeH  ã                   @   sj   d Z ddlZddlmZ ddlZddlZddlZddlZddlZddl	m
Z
 dZedfdd„Zdd	„ ZdS )
z
Utilities for end-users.
é    N)Ú
namedtuple)ÚInterpreterFc                    s¨   t rtjddtjd� G ‡ ‡fdd„dƒ}zddl}ddl}W n tyX   tdƒ Y nL0 | |ƒ j	¡ | 
d	¡ | 
d
¡ | 
d¡ | 
d¡ | 
d¡ | d¡ dS )a…  
    This function sets up :mod:`readline` to use Jedi in a Python interactive
    shell.

    If you want to use a custom ``PYTHONSTARTUP`` file (typically
    ``$HOME/.pythonrc.py``), you can add this piece of code::

        try:
            from jedi.utils import setup_readline
        except ImportError:
            # Fallback to the stdlib readline completer if it is installed.
            # Taken from http://docs.python.org/2/library/rlcompleter.html
            print("Jedi is not installed, falling back to readline")
            try:
                import readline
                import rlcompleter
                readline.parse_and_bind("tab: complete")
            except ImportError:
                print("Readline is not installed either. No tab completion is enabled.")
        else:
            setup_readline()

    This will fallback to the readline completer if Jedi is not installed.
    The readline completer will only complete names in the global namespace,
    so for example::

        ran<TAB>

    will complete to ``range``.

    With Jedi the following code::

        range(10).cou<TAB>

    will complete to ``range(10).count``, this does not work with the default
    cPython :mod:`readline` completer.

    You will also need to add ``export PYTHONSTARTUP=$HOME/.pythonrc.py`` to
    your shell profile (usually ``.bash_profile`` or ``.profile`` if you use
    bash).
    z/tmp/jedi.logÚa)ÚfilenameÚfilemodeÚlevelc                       s   e Zd Z‡ ‡fdd„ZdS )zsetup_readline.<locals>.JediRLc              	      sÒ   |dkr¬t j dt ¡ ¡ z‚zPt dtˆ ƒ ¡ tˆ ˆj	gƒ}|j
ˆd�}t d|¡ ‡ fdd„|D ƒ| _W n    t dt ¡  ¡ ‚ Y n0 W t j d¡ nt j d¡ 0 z| j| W S  tyÌ   Y dS 0 dS )	a  
            This complete stuff is pretty weird, a generator would make
            a lot more sense, but probably due to backwards compatibility
            this is still the way how it works.

            The only important part is stuff in the ``state == 0`` flow,
            everything else has been copied from the ``rlcompleter`` std.
            library module.
            r   zStart REPL completion: )ÚfuzzyzREPL completions: %sc                    s(   g | ] }ˆ d t ˆ ƒ|j … |j ‘qS )N)ÚlenZ_like_name_lengthZname_with_symbols)Ú.0Úc©Útext© úM/home/yuhw/wc/wire-cell-python/wcpy/lib/python3.9/site-packages/jedi/utils.pyÚ
<listcomp>Y   s   ÿz;setup_readline.<locals>.JediRL.complete.<locals>.<listcomp>zREPL Completion error:
N)ÚsysÚpathÚinsertÚosÚgetcwdÚloggingÚdebugÚreprr   Ú__dict__ÚcompleteÚmatchesÚerrorÚ	tracebackÚ
format_excÚpopÚ
IndexError)Úselfr   ÚstateÚinterpreterZcompletions©r   Únamespace_moduler   r   r   E   s$    

þ
z'setup_readline.<locals>.JediRL.completeN)Ú__name__Ú
__module__Ú__qualname__r   r   r$   r   r   ÚJediRLD   s   r)   r   Nz$Jedi: Module readline not available.ztab: completezset completion-ignore-case onzset show-all-if-unmodifiedzset show-all-if-ambiguous onz&set completion-prefix-display-length 2Ú )ÚREADLINE_DEBUGr   ÚbasicConfigÚDEBUGÚrlcompleterÚreadlineÚImportErrorÚprintZset_completerr   Úparse_and_bindZset_completer_delims)r%   r   r)   r.   r/   r   r$   r   Úsetup_readline   s&    *ý#




r3   c                  C   s8   t ddƒ} ddlm} t d|¡}| dd„ t|ƒD ƒŽ S )z_
    Returns a namedtuple of Jedi's version, similar to Python's
    ``sys.version_info``.
    ÚVersionzmajor, minor, micror   )Ú__version__z
[a-z]+|\d+c                 S   s$   g | ]\}}|d kr|nt |ƒ‘qS )é   )Úint)r
   ÚiÚxr   r   r   r   †   ó    z version_info.<locals>.<listcomp>)r   Újedir5   ÚreÚfindallÚ	enumerate)r4   r5   Ztuplr   r   r   Úversion_info~   s    
r?   )Ú__doc__Ú__main__Úcollectionsr   r   r   r<   r   r   r;   r   r+   r3   r?   r   r   r   r   Ú<module>   s   k