U
    nufH                     @   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k
rZ   td Y nLX || 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rt jdt  zvzPtdt   t	 j
g}|jd}td|  fdd|D | _W n    tdt    Y nX W 5 t jd X z| j| W S  tk
r   Y dS X 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 ./tmp/pip-unpacked-wheel-ol4ehs9f/jedi/utils.py
<listcomp>Y   s   z;setup_readline.<locals>.JediRL.complete.<locals>.<listcomp>zREPL Completion error:
N)syspathinsertosgetcwdpoploggingdebugreprr   __dict__completematcheserror	traceback
format_exc
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      s     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