U
    id*                     @   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k
r   dZY nX 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)usageZadd_argument_groupadd_argument)clsr   Zhelp_grpr   r   r   add_parser_options/   s    zSoSHelper.add_parser_optionsc                 C   sB   | j jdd }dddd}||kr>| j j||| | j _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k
rn } z"td| j j|f  td W 5 d }~X Y nX |rzt	 }|
| |  W q tk
r } ztd|  W 5 d }~X Y qX n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|krJtd
| j j }t||| j j }n<| j| j| j| jd}|D ] }| j j	|rd||  } qqd|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.   \   s,    

zSoSHelper.get_obj_for_topicc                 C   s2   ddl m} | jjdd }||kr.|| S d S )Nr   )
TRANSPORTSr    )Zsos.collector.sosnoderD   r   r   r&   )r   rD   Z
_transportr   r   r   r@   |   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   )r6   clustersr    rE   r*   )	Zsos.collectorr6   Zsos.collector.clustersZ_load_modulesr"   rF   r   r   r&   )r   r6   sosrF   Zclusterr   r   r   rA      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   rC   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 ]8}t|D ]*}|j	
 dd}||kr:|    S q:q.d S )Nr    rE   r   rN   r   )r   r   r&   Zsos.policies.distrosr   r9   ZdistrosZget_modulesr   __name__lowerr'   )r   Z_topicrG   Z_helperrC   rN   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)r7   r8   zreport.plugins.$pluginr#   r%   r9   z		{:<36}{}F)newlineN)r1   add_textr   add_sectionformatr2   )r   Z	self_helpZsubsectr   Zrep_exZ
avail_helpsectionsZsectr   r   r   r+      sJ    


	zSoSHelper.display_self_help)rO   
__module____qualname____doc__ZdescZconfigure_loggingZload_policyZ
load_probeZarg_defaultsr   classmethodr   r)   r5   r.   r@   rA   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 )r1   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   rU   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\   rQ   lnr   r   r   rR      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_sectionrU   r^   r   r   r   rS     s    zHelpSection.add_sectionc                 C   s(   || j k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)rU   r/   r1   r^   r   r   r   rb     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| 	  qDdS )zWPrint the HelpSection contents, including any subsections, to
        console.
        )widthZinitial_indentr   N)
r0   r   r   r[   TERMSIZEr]   r\   
splitlinesrU   r2   )r   ra   Zsectionr   r   r   r2   #  s      
zHelpSection.displayN)r   r   r   )T)r   r   )r   r   )
rO   rV   rW   rX   r   r_   rR   rS   rb   r2   r   r   r   r   r1      s   




r1   )rL   r;   r,   oscollectionsr   Zsos.componentr   Zsos.policiesr   Zsos.report.pluginsr   Zsos.utilitiesr   r   textwrapr   minget_terminal_sizecolumnsrd   r/   r
   r1   r   r   r   r   <module>   s    
 =