o
    *b	                     @   s4   d dl Z d dlmZ d dlmZ G dd deZdS )    N)SoSCleanerParser)SoSUsernameMapc                       sN   e Zd ZdZdZdZg Zg dZd fdd	Zdd	 Z	d
d Z
dd Z  ZS )SoSUsernameParsera  Parser for obfuscating usernames within an sosreport archive.

    Note that this parser does not rely on regex matching directly, like most
    other parsers do. Instead, usernames are discovered via scraping the
    collected output of lastlog. As such, we do not discover new usernames
    later on, and only usernames present in lastlog output will be obfuscated,
    and those passed via the --usernames option if one is provided.
    zUsername ParserZusername_map)
ZcoreZnobodyZ	nfsnobodyZshutdownstackZrebootrootZubuntuZusernameZwtmpNc                    s(   t  | _tt| | | j| d S N)r   mappingsuperr   __init__Zload_names_from_options)selfZconfigZ	opt_names	__class__ E/usr/lib/python3/dist-packages/sos/cleaner/parsers/username_parser.pyr
   +   s   zSoSUsernameParser.__init__c              	   C   sp   t  }| D ]#}z| d }W n	 ty   Y qw |r$| | jv r%q|| q|D ]}| j| q-dS )zSince we don't get the list of usernames from a straight regex for
        this parser, we need to override the initial parser prepping here.
        r   N)	set
splitlinessplit	Exceptionlower	skip_listaddr   get)r   ZcontentZuserslineuserZeachr   r   r   load_usernames_into_map0   s   z)SoSUsernameParser.load_usernames_into_mapc                 C   s&   | j jD ]}t|tj| j|< qd S r   )r   ZdatasetrecompileIregexes)r   r   r   r   r   generate_item_regexes@   s   z'SoSUsernameParser.generate_item_regexesc                 C   sX   d}t | j tddD ]\}}||r'|| j| |\}}||7 }q||fS )Nr   T)keyreverse)	sortedr   itemslensearchsubnr   r   r   )r   r   countr   Zreg_countr   r   r   
parse_lineD   s   
zSoSUsernameParser.parse_liner   )__name__
__module____qualname____doc__nameZmap_file_keyZregex_patternsr   r
   r   r   r)   __classcell__r   r   r   r   r      s    	r   )r   Zsos.cleaner.parsersr   Z!sos.cleaner.mappings.username_mapr   r   r   r   r   r   <module>   s   
