B
    1zf                 @   s   d Z ddlZddlZddlZddlmZ ddlmZ ddlm	Z	m
Z
mZ ddlZddlmZ ddlmZ ddlmZ dd	lmZ dd
lmZ ddlmZ ddlmZ dZG dd de
ZG dd deZdS )z4Implementation of bandit security testing in Flake8.    N)	lru_cache)Path)Dict
NamedTupleSet)ConfigFileFinder)utils)BanditConfig)BanditMetaAst)Metrics)BanditNodeVisitor)BanditTestSetz3.0.0c               @   sB   e Zd ZU eed< eed< eed< eeddd dddZd	S )
Flake8BanditConfigprofiletarget_pathsexcluded_paths    )maxsize)returnc       
   
   C   s  i }t  }t  }tdd d  }t }y|| dd |dD }|drp|ddd	d|d< |d	r|d	dd	d|d
< |dr|d	d}x,|D ]$}|
drd| }|t| qW |dr,|d	d}x0|D ](}|
drd| }|t| q W W nP tjttfk
r~ }	 z(i }t|	dkrntjd|	  W d d }	~	X Y nX | |||S )NZbanditc             S   s   i | ]\}}||qS  r   ).0kvr   r   P/home/ankuromar296_gmail_com/.local/lib/python3.7/site-packages/flake8_bandit.py
<dictcomp>)   s    z7Flake8BanditConfig.from_config_file.<locals>.<dictcomp>skipsSB,excludetestsincludetargets/.zNo section: 'bandit'zUnable to parse config file: )setr   Zlocal_config_filesconfigparserConfigParserreaditemsgetreplacesplit
startswithaddr   ErrorKeyError	TypeErrorstrsysstderrwrite)
clsr   r   r   Zini_fileconfigZbandit_configpathspather   r   r   from_config_file   s:    






$z#Flake8BanditConfig.from_config_fileN)	__name__
__module____qualname__r   __annotations__r   classmethodr   r;   r   r   r   r   r      s   
r   c               @   s8   e Zd ZdZdZeZdd Zdd Zdd Z	d	d
 Z
dS )BanditTesterzFlake8 class for checking code for bandit test errors.

    This class is necessary and used by flake8 to check the python
    file or files that are being tested.

    zflake8-banditc             C   s   || _ || _|| _d S )N)filenametreelines)selfrC   rB   rD   r   r   r   __init__Y   s    zBanditTester.__init__c             C   s   t  }t| j}t|j}|| |jr8|j|sR|j	rVt
|j	|dkrVg S y,t| jd t tt |jddi t d}W n: tk
r   t| jt tt |jddg t d}Y nX || j dd |jjD S )Nr   )r   F)fnamefdatametaasttestsetdebugnosec_linesmetrics)rG   rI   rJ   rK   rL   rM   c             S   s&   g | ]}|j d d|j|jdqS )r   r   )test_id
issue_textline_number)rN   r+   textlineno)r   itemr   r   r   
<listcomp>   s   z.BanditTester._check_source.<locals>.<listcomp>)r   r;   r   rB   r%   parentsr.   r   intersectionr   lenr   r
   r   r	   r   r   r1   generic_visitrC   testerresults)rE   r7   filepathZ	filepathsZbnvr   r   r   _check_source^   s8    


zBanditTester._check_sourcec             c   sT   | j r| js|   x:|  D ].}d|d |d f }|d d|t| fV  qW dS )z:run will check file source through the bandit code linter.z%s %srN   rO   rP   r   N)rC   rD   _load_sourcer\   type)rE   warnmessager   r   r   run   s
    zBanditTester.runc             C   sN   | j dkr"d| _ t d| _nt| j | _| jsJt	d
| j| _dS )zLoads the file in a way that auto-detects source encoding and deals
        with broken terminal encodings for stdin.

        Stolen from flake8_import_order because it's good.
        )stdin-Nrb   T N)rB   stdin_utilsZstdin_get_value
splitlinesrD   pycodestyle	readlinesrC   astparsejoin)rE   r   r   r   r]      s    
zBanditTester._load_sourceN)r<   r=   r>   __doc__name__version__versionrF   r\   ra   r]   r   r   r   r   rA   N   s   1	rA   ) rl   ri   r&   r3   	functoolsr   pathlibr   typingr   r   r   rg   Zflake8.options.configr   Zflake8r   re   Zbandit.core.configr	   Zbandit.core.meta_astr
   Zbandit.core.metricsr   Zbandit.core.node_visitorr   Zbandit.core.test_setr   rn   r   objectrA   r   r   r   r   <module>   s"   7