o
    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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args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   TOPICr   ?z/name of the topic or component to show help for)metavardefaultnargshelp)usageadd_argument_groupadd_argument)clsr   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   _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HelpSectiondisplay_helpdisplay)r   klasserr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.
        	SoSReportr   
SoSCleanerSoSCollectorRemoteTransportCluster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   
static_mapr!   mod_help_secr   r   r   r3   \   s2   

zSoSHelper.get_obj_for_topicc                 C   s2   ddl m} | jjdd }||v r|| S d S )Nr   )
TRANSPORTSr$   )sos.collector.sosnoderS   r   r   r*   )r   rS   
_transportr   r   r   rL   |   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   )r?   clustersr$   rT   r/   )	sos.collectorr?   sos.collector.clusters_load_modulesr&   rW   r   r   r*   )r   r?   sosrW   clusterr   r   r   rM      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 )NrF   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>)rG   rH   r   r   load_local_policyinspect
getmembersisclass__subclasses__policymatch_plugin)r   rP   memspluginspluginr!   r   r   r   rJ      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$   rT   r   rf   r   )r   r   r*   sos.policies.distrosr   rE   distrosget_modulesr   __name__lowerr+   )r   _topicr[   _helperrP   rf   _pr   r   r   rK      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)rC   rD   zreport.plugins.$pluginr'   r)   rE   z		{:<36}{}F)newlineN)r6   add_textr   add_sectionformatr8   )r   	self_helpsubsectr   rep_ex
avail_helpsectionssectr   r   r   r0      sJ   


	zSoSHelper.display_self_help)rn   
__module____qualname____doc__descconfigure_loggingload_policy
load_probearg_defaultsr   classmethodr#   r.   r<   r3   rL   rM   rJ   rK   r0   __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 )r6   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   r{   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   rs   lnr   r   r   rt      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_sectionr{   r   r   r   r   ru     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)r{   r4   r6   r   r   r   r   r     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)
r5   r   r   r   TERMSIZEr   r   
splitlinesr{   r8   )r   r   sectionr   r   r   r8   #  s   
zHelpSection.displayN)r   r   r   )T)r   r   )
rn   r}   r~   r   r   r   rt   ru   r   r8   r   r   r   r   r6      s    




r6   )rb   rG   r1   oscollectionsr   sos.componentr   sos.policiesr   sos.report.pluginsr   sos.utilitiesr   r   textwrapr   minget_terminal_sizecolumnsr   r4   r
   r6   r   r   r   r   <module>   s$   
 =