o
    *b*                     @   s   d dl 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	 d dl
mZ d dlmZmZ d dlmZ z
ee jdZW n eyK   dZY nw G d	d
 d
eZG dd dZdS )    N)OrderedDict)SoSComponent)import_policy)Plugin)boldImporterHelper)fillx   c                       s   e Zd ZdZdZdZdZdZddiZ fddZ	e
dd	 Zd
d Zdd Zdd Zdd Zdd Zdd Zdd Zdd Z  ZS )	SoSHelperzProvide better, more in-depth help for specific parts of sos than is
    provided in either standard --help output or in manpages.
    zDetailed help infomationFtopic c                    s"   t t| ||| | jj| _d S )N)superr
   __init__optsr   )selfparserargsZcmdline	__class__ 3/usr/lib/python3/dist-packages/sos/help/__init__.pyr   +   s   zSoSHelper.__init__c                 C   s*   d|_ |dd}|jdddddd	 d S )
Nzsos help TOPIC [options]zHelp Information Optionsz<These options control what detailed information is displayedr   ZTOPICr   ?z/name of the topic or component to show help for)metavardefaultnargshelp)usageadd_argument_groupadd_argument)clsr   Zhelp_grpr   r   r   add_parser_options/   s   
zSoSHelper.add_parser_optionsc                 C   sF   | j jdd }dddd}||v r!| j j||| | j _d S d S )N.r   cleaner	collector)cleanmaskcollect)r   r   splitreplace)r   Z_com_replacer   r   r   sanitize_topic_component:   s   z"SoSHelper.sanitize_topic_componentc              
   C   s   | j js|   td |   z|  }W n! ty8 } ztd| j j|f  td W Y d }~nd }~ww |rdzt	 }|
| |  W d S  tyc } ztd|  W Y d }~d S d }~ww td| j j  d S )Nr   z Could not load help for '%s': %s   zError loading help: %szNo help section found for '%s')r   r   display_self_helpsysexitr*   get_obj_for_topic	ExceptionprintHelpSectionZdisplay_helpdisplay)r   klasserrZhtr   r   r   executeD   s*   

zSoSHelper.executec                 C   s   dddddddd}d	}| j j|v r&td
| j j }t||| j j }|S | j| j| j| jd}|D ]}| j j	|rD||  } |S q3|S )zBased on the help topic we're after, try to smartly decide which
        object we need to manipulate in order to get help information.
        Z	SoSReportr   Z
SoSCleanerSoSCollectorZRemoteTransportZClusterZPolicy)reportreport.pluginsr"   r#   zcollector.transportszcollector.clusterspoliciesNsos.)zreport.plugins.z	policies.zcollector.transports.zcollector.clusters.)
r   r   	importlibimport_modulegetattr_get_plugin_variant_get_policy_by_name_get_collect_transport_get_collect_cluster
startswith)r   Z
static_mapr   modZ_helpZ_secr   r   r   r/   \   s2   

zSoSHelper.get_obj_for_topicc                 C   s2   ddl m} | jjdd }||v r|| S d S )Nr   )
TRANSPORTSr!   )Zsos.collector.sosnoderE   r   r   r'   )r   rE   Z
_transportr   r   r   rA   |   s
   z SoSHelper._get_collect_transportc                 C   sX   ddl m} dd l}||jjd}|D ]}|d | jjdd kr)|d   S qd S )Nr   )r7   clustersr!   rF   r+   )	Zsos.collectorr7   Zsos.collector.clustersZ_load_modulesr#   rG   r   r   r'   )r   r7   sosrG   Zclusterr   r   r   rB      s   
zSoSHelper._get_collect_clusterc                 C   sd   t d| jj }|   t|tj}dd |D }|D ]}| r/| j	
| }|  S qd S )Nr;   c                 S   s"   g | ]}t |d  tr|d  qS )r+   )
issubclassr   ).0mr   r   r   
<listcomp>   s   " z1SoSHelper._get_plugin_variant.<locals>.<listcomp>)r<   r=   r   r   Zload_local_policyinspectZ
getmembersZisclass__subclasses__policyZmatch_plugin)r   rD   ZmemsZpluginsZpluginr   r   r   r   r?      s   zSoSHelper._get_plugin_variantc                 C   sl   | j jdd }dd l}t|jj}| D ]}t|D ]}|j	
 dd}||kr2|    S qqd S )Nr!   rF   r   rO   r   )r   r   r'   Zsos.policies.distrosr   r:   ZdistrosZget_modulesr   __name__lowerr(   )r   Z_topicrH   Z_helperrD   rO   Z_pr   r   r   r@      s   zSoSHelper._get_policy_by_namec                 C   s   t dd}|dtd  |d}td}|d|  td}|d	|  |d
}|d ddddddd}|D ]}|jdt||| dd q>|  dS )zhDisplays the help information for this component directly, that is
        help for `sos help`.
        zDetailed help for sos helpzThe 'help' sub-command is used to provide more detailed information on different sub-commands available to sos as well as different components at play within those sub-commands.ah  SoS - officially pronounced "ess-oh-ess" - is a diagnostic and supportability utility used by several Linux distributions as an easy-to-use tool for standardized data collection. The most known component of which is %s (formerly sosreport) which is used to collect troubleshooting information into an archive for review by sysadmins or technical support teams.z
sos reportzHow to search using sos helpz$component.$topic.$subtopicz<To get more information on a given topic, use the form '%s'.zsos help report.plugins.kernelz\For example '%s' will provide more information on the kernel plugin for the report function.zAvailable Help SectionszThe following help sections are available. Additional help topics and subtopics may be displayed within their respective help section.
z#Detailed help on the report commandz'Information on the plugin design of sosz!Information on a specific $pluginz"Detailed help on the clean commandz$Detailed help on the collect commandz+How sos operates on different distributions)r8   r9   zreport.plugins.$pluginr$   r&   r:   z		{:<36}{}F)newlineN)r2   add_textr   add_sectionformatr3   )r   Z	self_helpZsubsectr   Zrep_exZ
avail_helpsectionsZsectr   r   r   r,      sJ   


	zSoSHelper.display_self_help)rP   
__module____qualname____doc__ZdescZconfigure_loggingZload_policyZ
load_probeZarg_defaultsr   classmethodr    r*   r6   r/   rA   rB   r?   r@   r,   __classcell__r   r   r   r   r
      s&    


 	
r
   c                   @   sH   e Zd ZdZdddZdd Zddd	Zdd
dZdddZdd Z	dS )r2   zThis class is used to build the output displayed by `sos help` in a
    standard fashion that provides easy formatting controls.
    r   c                 C   s   || _ || _|| _t | _dS )a  
        :param title:   The title of the output section, will be prominently
                        displayed
        :type title:    ``str``

        :param content: The text content to be displayed with this section
        :type content:  ``str``

        :param indent:  If the section should be nested, set this to a multiple
                        of 4.
        :type indent:   ``int``
        N)titlecontentindentr   rV   r   r\   r]   r^   r   r   r   r      s   zHelpSection.__init__c                 C   s
   || _ dS )zSet or override the title for this help section

        :param title:   The name to set for this help section
        :type title:    ``str``
        N)r\   )r   r\   r   r   r   	set_title   s   
zHelpSection.set_titleTc                 C   s,   | j r|rdnd}|| }|  j |7  _ dS )zAdd body text to this section. If content for this section already
        exists, append the new ``content`` after a newline.

        :param content:     The text to add to the section
        :type content:      ``str``
        z


N)r]   )r   r]   rR   lnr   r   r   rS      s   zHelpSection.add_textc                 C   s   |  ||| | j| S )ac  Add a section of text to the help section that will be displayed
        when the HelpSection object is printed.

        Sections will be printed *in the order added*.

        This will return a subsection object with which block(s) of text may be
        added to the subsection associated with ``title``.

        :param title:   The title of the subsection being added
        :type title:    ``str``

        :param content: The text the new section should contain
        :type content:  ``str``

        :returns:   The newly created subsection for ``title``
        :rtype:     ``HelpSection``
        )_add_sectionrV   r_   r   r   r   rT     s   
zHelpSection.add_sectionc                 C   s(   || j v r	tdt|||| j |< dS )zInternal method used to add a new subsection to this help output

        :param title:   The title of the subsection being added
        :type title:    ``str`
        z(A section with that title already existsN)rV   r0   r2   r_   r   r   r   rc     s   
zHelpSection._add_sectionc                 C   sd   t tt| jt| jd | j D ]}t t|t| jd q| jD ]}t d | j| 	  q"dS )zWPrint the HelpSection contents, including any subsections, to
        console.
        )widthinitial_indentr   N)
r1   r   r   r\   TERMSIZEr^   r]   
splitlinesrV   r3   )r   rb   sectionr   r   r   r3   #  s   
zHelpSection.displayN)r   r   r   )T)r   r   )
rP   rW   rX   rY   r   r`   rS   rT   rc   r3   r   r   r   r   r2      s    




r2   )rM   r<   r-   oscollectionsr   Zsos.componentr   Zsos.policiesr   Zsos.report.pluginsr   Zsos.utilitiesr   r   textwrapr   minget_terminal_sizecolumnsrf   r0   r
   r2   r   r   r   r   <module>   s$   
 =