OwlCyberSecurity - MANAGER
Edit File: btm_utils.cpython-38.opt-1.pyc
U ����e5d�&����������������������@���s|���d�Z�ddlmZ�ddlmZmZ�ddlmZmZ�eZ eZ ejZeZ dZdZdZG�dd ��d e�Zddd�Zd d��Zdd��Zd S�)z0Utility functions used by the btm_matcher module����)�pytree)�grammar�token)�pattern_symbols�python_symbols���������c�������������������@���s:���e�Zd�ZdZd dd�Zdd��Zdd��Zd d ��Zdd��ZdS�)�MinNodez�This class serves as an intermediate representation of the pattern tree during the conversion to sets of leaf-to-root subpatternsNc�����������������C���s.���||�_�||�_g�|�_d|�_d�|�_g�|�_g�|�_d�S�)NF)�type�name�children�leaf�parent�alternatives�group)�selfr���r�����r����)/usr/lib64/python3.8/lib2to3/btm_utils.py�__init__���s����zMinNode.__init__c�����������������C���s���t�|�j�d�t�|�j��S�)N� )�strr���r���)r���r���r���r����__repr__���s����zMinNode.__repr__c�����������������C���s����|�}g�}|r�|j�tkr^|j�|��t|j�t|j�krRt|j�g}g�|_|j}qn|j}d}q�|j�tkr�|j �|��t|j �t|j�kr�t |j �}g�|_ |j}qn|j}d}q�|j�tjkr�|j r�|�|j ��n|�|j���|j}q|S�)z�Internal method. Returns a characteristic path of the pattern tree. This method must be run for all leaves until the linear subpatterns are merged into a singleN)r����TYPE_ALTERNATIVESr����append�lenr ����tupler���� TYPE_GROUPr����get_characteristic_subpattern�token_labels�NAMEr���)r����node�subpr���r���r����leaf_to_root!���s8���� zMinNode.leaf_to_rootc�����������������C���s&���|�����D�]}|���}|r|��S�qdS�)a���Drives the leaf_to_root method. The reason that leaf_to_root must be run multiple times is because we need to reject 'group' matches; for example the alternative form (a | b c) creates a group [b c] that needs to be matched. Since matching multiple linear patterns overcomes the automaton's capabilities, leaf_to_root merges each group into a single choice based on 'characteristic'ity, i.e. (a|b c) -> (a|b) if b more characteristic than c Returns: The most 'characteristic'(as defined by get_characteristic_subpattern) path for the compiled pattern tree. N)�leavesr#���)r����lr"���r���r���r����get_linear_subpatternK���s����zMinNode.get_linear_subpatternc�����������������c���s*���|�j�D�]}|���E�dH��q|�j�s&|�V��dS�)z-Generator that returns the leaves of the treeN)r ���r$���)r����childr���r���r���r$���`���s���� zMinNode.leaves)NN) �__name__� __module__�__qualname__�__doc__r���r���r#���r&���r$���r���r���r���r���r ������s��� *r ���Nc����������� ������C���s���d}|�j�tjkr|�jd�}�|�j�tjkr�t|�j�dkrFt|�jd�|�}nFttd�}|�jD�]4}|�j� |�d�rlqVt||�}|dk rV|j� |��qV�n|�j�tjkr�t|�j�dkr�ttd�}|�jD�]}t||�}|r�|j� |��q�|js�d}nt|�jd�|�}�n�|�j�tj k�r�t|�jd�tj��r>|�jd�jdk�r>t|�jd�|�S�t|�jd�tj��rd|�jd�jdk�s�t|�j�dk�r�t|�jd�d��r�|�jd�jdk�r�dS�d }d}d}d }d} d } |�jD�]d}|j�tjk�r�d }|}n*|j�tjk�r�d }|} n|j�tjk�r�|}t|d��r�|jdk�r�d } �q�| �rT|�jd�}t|d��r^|jdk�r^|�jd�}n |�jd�}|j�tjk�r�|jd k�r�ttd�}n4tt|j��r�ttt|j�d�}nttt|j�d�}n\|j�tjk�r�|j�d�}|tk�r�tt|�d�}nttj|d�}n|j�tjk�rt||�}|�rL| jd�jdk�r4d}n| jd�jdk�rHnt�|�r�|dk �r�|jdd��D�]&}t||�}|dk �rj|j� |���qj|�r�||_|S�)z� Internal function. Reduces a compiled pattern tree to an intermediate representation suitable for feeding the automaton. This also trims off any optional pattern elements(like [a], a*). N���������)r���r����(�[�valueTF�=�����any�')r���r����*�+r���)r����symsZMatcherr ���ZAlternativesr����reduce_treer ���r����indexr���ZAlternativer���ZUnit� isinstancer���ZLeafr0����hasattrZDetailsZRepeaterr���r ����TYPE_ANY�getattr�pysyms�STRING�strip�tokens�NotImplementedErrorr���) r!���r���Znew_noder'���Zreducedr���Zdetails_nodeZalternatives_nodeZhas_repeaterZ repeater_nodeZhas_variable_nameZ name_leafr���r���r���r���r8���g���s����� ����� r8���c��������������������s����t�|�t�s|�S�t|��dkr"|�d�S�g�}g�}dddddg�g�}d��|�D�]d}tt|d d ����rDtt|��fdd ���r||�|��qDtt|�fdd ���r�|�|��qD|�|��qD|r�|}�n|r�|}�n|r�|}�t|�td �S�)z�Picks the most characteristic from a list of linear patterns Current order used is: names > common_names > common_chars r���r,����in�for�if�not�Nonez[]().,:c�����������������S���s���t�|��tkS��N)r���r�����xr���r���r����<lambda>���������z/get_characteristic_subpattern.<locals>.<lambda>c��������������������s���t�|�t�o|���kS�rH����r:���r���rI���)�common_charsr���r���rK������rL���c��������������������s���t�|�t�o|���kS�rH���rM���rI���)�common_namesr���r���rK�����rL���)�key)r:����listr���r3����rec_testr����max)ZsubpatternsZsubpatterns_with_namesZsubpatterns_with_common_namesZsubpatterns_with_common_chars� subpatternr���)rN���rO���r���r�������s6���� � �r���c�����������������c���s8���|�D�].}t�|ttf�r(t||�E�dH��q||�V��qdS�)zPTests test_func on all items of sequence and items of included sub-iterablesN)r:���rQ���r���rR���)ZsequenceZ test_funcrJ���r���r���r���rR�����s����rR���)N)r+�����r���Zpgen2r���r���Zpygramr���r���r7���r>���ZopmaprA���r���r<���r���r����objectr ���r8���r���rR���r���r���r���r����<module>���s���W �%