
    -_g
                     ,    d dl Z d dlmZ  G d d      Zy)    N)Lockc                   T    e Zd ZdZg Zg ZdZdZdZd Z	d Z
d Zd Zd Zd	 Zd
 Zd Zy)SoSMapzStandardized way to store items with their obfuscated counterparts.

    Each type of sanitization that SoSCleaner supports should have a
    corresponding SoSMap() object, to allow for easy retrieval of obfuscated
    items.
    TFc                 \    i | _         t               | _        g | _        t	               | _        y )N)datasetset_regexes_madecompiled_regexesr   lock)selfs    ?/usr/lib/python3/dist-packages/sos/cleaner/mappings/__init__.py__init__zSoSMap.__init__   s$     U "F	    c                    |rD|| j                   v s6|| j                  j                         v s| j                  rt	        |      dk  ry| j
                  D ])  }t        j                  ||t        j                        s) y y)zSome items need to be completely ignored, for example link-local or
        loopback addresses should not be obfuscated
           TF)		skip_keysr   valuesignore_short_itemslenignore_matchesrematchI)r   itemskips      r   ignore_itemzSoSMap.ignore_item%   sm     tt~~-9L9L9N1N++D	Q'' 	DxxdBDD)	 r   c                     | j                  |      r|S | j                  5  | j                  |      | j                  |<   | j                  r| j                  |       | j                  |   cddd       S # 1 sw Y   yxY w)zAdd a particular item to the map, generating an obfuscated pair
        for it.

        Positional arguments:

            :param item:        The plaintext object to obfuscate
        N)r   r   sanitize_itemr   compile_regexesadd_regex_itemr   r   s     r   addz
SoSMap.add1   so     D!KYY 	&!%!3!3D!9DLL####D)<<%		& 	& 	&s   A
A44A=c                    | j                  |      ry|| j                  vrf| j                  j                  |       | j                  j	                  || j                  |      f       | j                  j                  d d       yy)zAdd an item to the regexes dict and then re-sort the list that the
        parsers will use during parse_line()

        :param item:    The unobfuscated item to generate a regex for
        :type item:     ``str``
        Nc                     t        | d         S )Nr   )r   )xs    r   <lambda>z'SoSMap.add_regex_item.<locals>.<lambda>V   s    S1Y r   T)keyreverse)r   r	   r"   r
   appendget_regex_resultsortr!   s     r   r    zSoSMap.add_regex_itemA   s~     D!t)))
 ""4( !!(($0E0Ed0K)LM!!&&+>&M *r   c                     | j                   rdt        j                  |       d}nt        j                  |      }t        j                  |t        j                        S )a9  Generate the object/value that is used by the parser when iterating
        over pre-generated regexes during parse_line(). For most parsers this
        will simply be a ``re.Pattern()`` object, but for more complex parsers
        this can be overridden to provide a different object, e.g. a tuple,
        for that parer's specific iteration needs.

        :param item:    The unobfuscated string to generate the regex for
        :type item:     ``str``

        :returns:       A compiled regex pattern for the item
        :rtype:         ``re.Pattern``
        z(?<![a-z0-9])z
(?=\b|_|-))match_full_words_onlyr   escapecompiler   r!   s     r   r*   zSoSMap.get_regex_resultX   sF     %%#BIIdO#4J?D99T?Dzz$%%r   c                     |S )zPerform the obfuscation relevant to the item being added to the map.

        This should be overridden by each type of map that subclasses SoSMap

        Positional arguments:

            :param item:        The plaintext object to obfuscate
         r!   s     r   r   zSoSMap.sanitize_itemk   s	     r   c                     | j                  |      r|S || j                  vr| j                  |      S | j                  |   S )zRetrieve an item's obfuscated counterpart from the map. If the item
        does not yet exist in the map, add it by generating one on the fly
        )r   r   r"   r!   s     r   getz
SoSMap.getv   s@     D!Kt||#88D>!||D!!r   c                 :    | j                   j                  |       y)a%  Update the map using information from a previous run to ensure that
        we have consistent obfuscation between reports

        Positional arguments:

            :param config:    A dict of mappings with the form of
                              {clean_entry: 'obfuscated_entry'}
        N)r   update)r   configs     r   conf_updatezSoSMap.conf_update   s     	F#r   N)__name__
__module____qualname____doc__r   r   r   r   r-   r   r   r"   r    r*   r   r3   r7   r1   r   r   r   r      sM     NIO!
& N.&&	"	$r   r   )r   	threadingr   r   r1   r   r   <module>r=      s    
 y$ y$r   