o
    G%@ik+                     @   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 d dlmZ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)fill)SoSComponent)import_policy)Plugin)boldImporterHelperx   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  ||| | jj| _d S )N)super__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y: } ztd| j j d|  td W Y d }~nd }~ww |rgzt	 }|
| |  W d S  tyf } ztd|  W Y d }~d S d }~ww td| j j d d S )Nr   zCould not load help for 'z':    zError loading help: zNo help section found for '')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	}d
}| j j|v r'td| j j }t||| j j }|S | j| j| j| jd}|	 D ]\}}| j j
|rG| } |S q6|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	SoSUpload)reportreport.pluginsr%   r&   zcollector.transportszcollector.clusterspoliciesuploadN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items
startswith)r   
static_mapr!   mod_help_secvaluer   r   r   r4   \   s4   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.sosnoderX   r   r   r*   )r   rX   
_transportr   r   r   rO   }   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$   rY   r/   )	sos.collectorr@   sos.collector.clusters_load_modulesr&   r\   r   r   r*   )r   r@   sosr\   clusterr   r   r   rP      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 )NrI   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>)rJ   rK   r   r   load_local_policyinspect
getmembersisclass__subclasses__policymatch_plugin)r   rT   memspluginspluginr!   r   r   r   rM      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$   rY   r   rk   r   )r   r   r*   sos.policies.distrosr   rG   distrosget_modulesr   __name__lowerr+   )r   _topicr`   _helperrT   rk   _pr   r   r   rN      s   zSoSHelper._get_policy_by_namec           	      C   s   t dd}|dtd d |d}td}|d| d	 td
}|d| d |d}|d dddddddd}| D ]\}}|jdt|d| dd qG|  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.zSoS - 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 z
sos reportz (formerly sosreport) which is used to collect troubleshooting information into an archive for review by sysadmins or technical support teams.zHow to search using sos helpz$component.$topic.$subtopicz8To get more information on a given topic, use the form 'z'.zsos help report.plugins.kernelzFor example 'zM' 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#Detailed help on the upload commandz+How sos operates on different distributions)rE   rF   zreport.plugins.$pluginr'   r)   rH   rG   	z<36F)newlineN)r7   add_textr   add_sectionrQ   r9   )	r   	self_helpsubsectr   rep_ex
avail_helpsectionssectrW   r   r   r   r1      sB   
	

 zSoSHelper.display_self_help)rs   
__module____qualname____doc__descconfigure_loggingload_policy
load_probearg_defaultsr   classmethodr#   r.   r=   r4   rO   rP   rM   rN   r1   __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 )r7   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   ry   lnr   r   r   rz      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   r{     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   r5   r7   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)
r6   r   r   r   TERMSIZEr   r   
splitlinesr   r9   )r   r   sectionr   r   r   r9   &  s   
zHelpSection.displayN)r   r   r   )T)r   r   )
rs   r   r   r   r   r   rz   r{   r   r9   r   r   r   r   r7      s    




r7   )rg   rJ   r2   oscollectionsr   textwrapr   sos.componentr   sos.policiesr   sos.report.pluginsr   sos.utilitiesr   r   minget_terminal_sizecolumnsr   r5   r
   r7   r   r   r   r   <module>   s$   
 @