U
    tdG                     @   s   d Z ddlmZmZ ddlZddlZddlmZ ddlm	Z	m
Z
 ddlmZ G dd dZG d	d
 d
ZG dd dedZG dd deZG dd dZG dd deeZG dd deeZdd Zedkree  dS )ag  Command-line user interface of igraph

The command-line interface launches a Python shell with the igraph
module automatically imported into the main namespace. This is mostly a
convenience module and it is used only by the C{igraph} command line
script which executes a suitable Python shell and automatically imports
C{igraph}'s classes and functions in the top-level namespace.

Supported Python shells are:

  - IDLE shell (class L{IDLEShell})
  - IPython shell (class L{IPythonShell})
  - Classic Python shell (class L{ClassicPythonShell})

The shells are tried in the above mentioned preference order one by
one, unless the C{global.shells} configuration key is set which
overrides the default order. IDLE shell is only tried in Windows
unless explicitly stated by C{global.shells}, since Linux and
Mac OS X users are likely to invoke igraph from the command line.
    )ABCMetaabstractmethodN__version__)set_progress_handlerset_status_handler)Configurationc                   @   s   e Zd ZdZdZdZdZdZdZdZ	dZ
dZdZdZdZdZdZdZdZdZd Z Z Z Z Z Z ZZd Z Z ZZd Z  Z! Z"Z#d$ Z%d$ Z&d$ Z'e(j)fddZ*e+dd	 Z,e+d
d Z-dd Z.dd Z/dS )TerminalControllera  
    A class that can be used to portably generate formatted output to
    a terminal.

    C{TerminalController} defines a set of instance variables whose
    values are initialized to the control sequence necessary to
    perform a given action.  These can be simply included in normal
    output to the terminal:

        >>> term = TerminalController()
        >>> print('This is '+term.GREEN+'green'+term.NORMAL)
        This is green

    Alternatively, the L{render()} method can used, which replaces
    C{${action}} with the string required to perform C{action}:

        >>> term = TerminalController()
        >>> print(term.render('This is ${GREEN}green${NORMAL}'))
        This is green

    If the terminal doesn't support a given action, then the value of
    the corresponding instance variable will be set to ''.  As a
    result, the above code will still work on terminals that do not
    support color, except that their output will not be colored.
    Also, this means that you can test whether the terminal supports a
    given action by simply testing the truth value of the
    corresponding instance variable:

        >>> term = TerminalController()
        >>> if term.CLEAR_SCREEN:
        ...     print 'This terminal supports clearning the screen.'
        ...

    Finally, if the width and height of the terminal are known, then
    they will be stored in the C{COLS} and C{LINES} attributes.

    @author: Edward Loper
     z
    BOL=cr UP=cuu1 DOWN=cud1 LEFT=cub1 RIGHT=cuf1
    CLEAR_SCREEN=clear CLEAR_EOL=el CLEAR_BOL=el1 CLEAR_EOS=ed BOLD=bold
    BLINK=blink DIM=dim REVERSE=rev UNDERLINE=smul NORMAL=sgr0
    HIDE_CURSOR=cinvis SHOW_CURSOR=cnormz.BLACK BLUE GREEN CYAN RED MAGENTA YELLOW WHITEz.BLACK RED GREEN YELLOW BLUE MAGENTA CYAN WHITEc                 C   s  zddl }W n tk
r"   Y dS X | s0dS z|  W n tk
rR   Y dS X |d| _|d| _| jD ](}|	d\}}t
| || |pd qr| d}|rttt| j| jD ] \}}t
| || ||pd q| d}	|	r.ttt| j| jD ]$\}}t
| || |	|p&d q| d	}
|
r~ttt| j| jD ](\}}t
| d
| | |
|pvd qT| d}|rttt| j| jD ](\}}t
| d
| | ||pd qdS )aR  
        Create a C{TerminalController} and initialize its attributes
        with appropriate values for the current terminal.
        C{term_stream} is the stream that will be used for terminal
        output; if this stream is not a tty, then the terminal is
        assumed to be a dumb terminal (i.e., have no capabilities).
        r   Ncolslines=r
   ZsetfZsetafZsetbZBG_Zsetab)cursesImportErrorisattyZ	setupterm	ExceptionZtigetnumCOLSLINES_STRING_CAPABILITIESsplitsetattr	_tigetstrziprangelen_COLORS_tparm_ANSICOLORS)selfZterm_streamr   Z
capabilityZattribcap_nameZset_fgicolorZset_fg_ansiZset_bgZset_bg_ansi r"   I/home/sam/Atlas/atlas_env/lib/python3.8/site-packages/igraph/app/shell.py__init__o   s>    	



"
zTerminalController.__init__c                 C   s.   ddl }|| pd}|d}tdd|S )zcRewrites string capabilities to remove "delays" which are not
        required for modern terminalsr   N    latin-1z\$<\d+>[/*]?r
   )r   Ztigetstrdecoderesub)r   r   capr"   r"   r#   r      s    
zTerminalController._tigetstrc                 C   s(   dd l }|| d|pd}|dS )Nr   r&   r%   )r   Ztparmencoder'   )r   paramr   r*   r"   r"   r#   r      s    zTerminalController._tparmc                 C   s   t d| j|S )z
        Replace each $-substitutions in the given template string with
        the corresponding terminal control string (if it's defined) or
        '' (if it's not).
        zr\$\$|\${\w+})r(   r)   _render_sub)r   templater"   r"   r#   render   s    zTerminalController.renderc                 C   s*   |  }|dkr|S t| |dd S dS )zHelper function for L{render}z$$   N)groupgetattr)r   matchsr"   r"   r#   r-      s    zTerminalController._render_subN)0__name__
__module____qualname____doc__BOLUPZDOWNLEFTRIGHTZCLEAR_SCREEN	CLEAR_EOLZ	CLEAR_BOLZ	CLEAR_EOSZBOLDZBLINKZDIMZREVERSEZNORMALHIDE_CURSORSHOW_CURSORZBLACKZBLUEZGREENZCYANZREDZMAGENTAZYELLOWZWHITEZBG_BLACKZBG_BLUEZBG_GREENZBG_CYANZBG_REDZ
BG_MAGENTAZ	BG_YELLOWZBG_WHITEr   r   r   r   sysstdoutr$   staticmethodr   r   r/   r-   r"   r"   r"   r#   r	       s<   ( 4

r	   c                   @   s:   e Zd ZdZdZdZdd ZdddZd	d
 Zdd Z	dS )ProgressBara:  
    A 2-line progress bar.

    The progress bar looks roughly like this in the console::

                                Header
        20% [===========----------------------------------]

    The progress bar is colored, if the terminal supports color
    output; and adjusts to the width of the terminal.
    z5%3d%% ${GREEN}[${BOLD}%s%s${NORMAL}${GREEN}]${NORMAL}z${BOLD}${CYAN}%s${NORMAL}
c                 C   st   || _ | j jr| j jr| j js&td| j jp0d| _|| j| _	| j | j
d| j | _d| _d| _d| _d S )NzKTerminal isn't capable enough -- you should use a simpler progress display.K   r
   Tr   )termr>   r;   r:   
ValueErrorr   widthr/   BARprogress_barHEADERcenterheaderclearedlast_percentlast_message)r   rF   r"   r"   r#   r$      s    zProgressBar.__init__Nc              	   C   s   | j rtjd| j  d| _ |dkr.| j}n|| _|dkrD| j}n|| _t| jd |d  }tj| j	j
| j	j | j	j | j	j | j	| j|| j  | j|d| d| jd |  f  d  dS )a  Updates the progress bar.

        @param percent: the percentage to be shown. If C{None}, the previous
          value will be used.
        @param message: the message to be shown above the progress bar. If
          C{None}, the previous message will be used.
        
FN
   g      Y@r   -)rN   rA   rB   writerM   rP   rO   intrH   rF   r:   r;   r>   r/   rK   rL   rJ   )r   percentmessagenr"   r"   r#   update   s2     zProgressBar.updatec                 C   s   | j | dS )zyUpdates the message of the progress bar.

        @param message: the message to be shown above the progress bar
        )rW   )rY   strip)r   rW   r"   r"   r#   update_message  s    zProgressBar.update_messagec                 C   sT   | j sPtj| jj| jj | jj | jj | jj | jj  d| _ d| _d| _	dS )z9Clears the progress bar (i.e. removes it from the screen)Tr   r
   N)
rN   rA   rB   rT   rF   r:   r>   r;   rO   rP   r   r"   r"   r#   clear  s"    zProgressBar.clear)NN)
r6   r7   r8   r9   rI   rK   r$   rY   r[   r]   r"   r"   r"   r#   rD      s   
!rD   c                   @   sD   e Zd ZdZdd Zedd Zdd Zdd	 Zd
d Z	dd Z
dS )Shellz7Superclass of the embeddable shells supported by igraphc                 C   s   d S Nr"   r\   r"   r"   r#   r$   "  s    zShell.__init__c                 C   s   t d S r_   )NotImplementedErrorr\   r"   r"   r#   __call__%  s    zShell.__call__c                 C   s
   t | dS )zChecks whether the shell supports progress bars.

        This is done by checking for the existence of an attribute
        called C{_progress_handler}._progress_handlerhasattrr\   r"   r"   r#   supports_progress_bar)  s    zShell.supports_progress_barc                 C   s
   t | dS )zChecks whether the shell supports status messages.

        This is done by checking for the existence of an attribute
        called C{_status_handler}._status_handlerrc   r\   r"   r"   r#   supports_status_messages0  s    zShell.supports_status_messagesc                 C   s   |   r| jS dS )z:Returns the progress handler (if exists) or None (if not).N)re   rb   r\   r"   r"   r#   get_progress_handler7  s    zShell.get_progress_handlerc                 C   s   |   r| jS dS )z8Returns the status handler (if exists) or None (if not).N)rg   rf   r\   r"   r"   r#   get_status_handler=  s    zShell.get_status_handlerN)r6   r7   r8   r9   r$   r   ra   re   rg   rh   ri   r"   r"   r"   r#   r^     s   
r^   )	metaclassc                       s(   e Zd ZdZ fddZdd Z  ZS )	IDLEShellzmIDLE embedded shell interface.

    This class allows igraph to be embedded in IDLE (the Tk Python IDE).
    c                    s   t    ddl}d|j_z
tj W n tk
r>   dt_Y nX |jjdd}|j	| |
  |j|}| szt|j| _|| _dS )zConstructor.

        Imports IDLE's embedded shell. The implementation of this method is
        ripped from idlelib.PyShell.main() after removing the unnecessary
        parts.r   NTz>>> Idle)Z	className)superr$   Zidlelib.PyShellZPyShellZuse_subprocessrA   ps1AttributeErrorZTkZfixwordbreaksZwithdrawZPyShellFileListZ
open_shellr`   Zpyshell_shell_root)r   idlelibrootflist	__class__r"   r#   r$   M  s    

zIDLEShell.__init__c                 C   s&   | j jd | j  | j  dS )zStarts the shellfrom igraph import *N)rp   ZinterpZ
execsourcerq   mainloopdestroyr\   r"   r"   r#   ra   g  s    
zIDLEShell.__call__)r6   r7   r8   r9   r$   ra   __classcell__r"   r"   ru   r#   rk   D  s   rk   c                   @   s8   e Zd ZdZdd Zdd Zedd Zedd	 Zd
S )ConsoleProgressBarMixinz;Mixin class for console shells that support a progress bar.c                 C   sN   zt t | j_W n6 tk
r.   |   Y n tk
rH   |   Y nX d S r_   )rD   r	   rv   rJ   rG   _disable_handlers	TypeErrorr\   r"   r"   r#   r$   q  s    z ConsoleProgressBarMixin.__init__c                 C   sD   z
| j `W n tk
r   Y nX z
| j `W n tk
r>   Y nX dS )zXDisables the status and progress handlers if the terminal is not
        capable enough.N)rv   rb   ro   rf   r\   r"   r"   r#   r|   |  s    

z)ConsoleProgressBarMixin._disable_handlersc                 C   s&   |dkr| j   n| j || dS )zProgress bar handler, called when C{igraph} reports the progress
        of an operation

        @param message: message provided by C{igraph}
        @param percentage: percentage provided by C{igraph}
        d   N)rJ   r]   rY   )clsrW   
percentager"   r"   r#   rb     s    z)ConsoleProgressBarMixin._progress_handlerc                 C   s   | j | dS )zStatus message handler, called when C{igraph} sends a status
        message to be displayed.

        @param message: message provided by C{igraph}
        N)rJ   r[   )r   rW   r"   r"   r#   rf     s    z'ConsoleProgressBarMixin._status_handlerN)	r6   r7   r8   r9   r$   r|   classmethodrb   rf   r"   r"   r"   r#   r{   n  s   
r{   c                   @   s    e Zd ZdZdd Zdd ZdS )IPythonShellznIPython embedded shell interface.

    This class allows igraph to be embedded in IPython's interactive shell.c                 C   s   t |  t|  ddl}ddlm} || _zLzddlm} W n  t	k
r`   ddl
m} Y nX | | _|jd W nB t	k
r   ddl}|j  | _| jjd |jd Y nX dS )zTConstructor.

        Imports IPython's embedded shell with separator lines removed.r   Nr   )TerminalIPythonAppz--noseprw   z-nosep)r^   r$   r{   rA   IPythonr   ipython_versionZIPython.terminal.ipappr   r   ZIPython.frontend.terminal.ipappinstancerp   argvappendZIPython.ShellstartZIP	runsource)r   rA   r   r   r   r"   r"   r#   r$     s"    


zIPythonShell.__init__c                 C   sP   t dt dd | jjjdkrB| j  | jjd | j  n
| j	  dS )Starts the embedded shell.igraph %s running inside r
   )endr   rw   N)
printr   rp   rv   r6   Z
initializeshellexr   rx   r\   r"   r"   r#   ra     s    
zIPythonShell.__call__Nr6   r7   r8   r9   r$   ra   r"   r"   r"   r#   r     s    r   c                   @   s    e Zd ZdZdd Zdd ZdS )ClassicPythonShellz_Classic Python shell interface.

    This class allows igraph to be embedded in Python's shell.c                 C   s   t |  t|  d| _dS )z4Constructor.

        Imports Python's classic shellN)r^   r$   r{   rp   r\   r"   r"   r#   r$     s    

zClassicPythonShell.__init__c                 C   sL   | j dkr>ddlm} | | _ tdt dtjd | j d | j   dS )r   Nr   )InteractiveConsoler   r
   )r   filerw   )	rp   coder   r   r   rA   stderrr   Zinteract)r   r   r"   r"   r#   ra     s    
zClassicPythonShell.__call__Nr   r"   r"   r"   r#   r     s   r   c            	   	   C   s  t  } | jr$td| j tjd ntdtjd d| krdd | d dD }g }tdd t 	 D }|D ]6}|
|d	}|d	krtd
| tjd qr|| qrn(ttg}dd	l}| dkr|dt d	}|D ]>}z| }W  qW q tk
r   dt|kr Y qX qt|trf| d r^| rHt|  | r^t|  |  ntdtjd tdtjd d	S )zLThe main entry point for igraph when invoked from the command
    line shellzUsing configuration from %s)r   z%No configuration file, using defaultsZshellsc                 S   s   g | ]}|  qS r"   )rZ   ).0partr"   r"   r#   
<listcomp>  s     zmain.<locals>.<listcomp>,c                 S   s,   g | ]$\}}t |trt|tr||fqS r"   )
isinstancetype
issubclassr^   )r   kvr"   r"   r#   r     s   
 
Nz!Warning: unknown shell class `%s'r   WindowsZClassicverbosez#No suitable Python shell was found.z.Check configuration variable `general.shells'.)r   r   filenamer   rA   r   r   dictglobalsitemsgetr   r   r   platformsysteminsertrk   r   strr   r^   re   r   rh   rg   r   ri   )	configpartsZshell_classesZavailable_classesr   r   r   r   Zshell_classr"   r"   r#   main  sP    


r   __main__)r9   abcr   r   r(   rA   Zigraphr   Zigraph._igraphr   r   Zigraph.configurationr   r	   rD   r^   rk   r{   r   r   r   r6   exitr"   r"   r"   r#   <module>   s"    )W%*108