o
    O6uf)+  ã                   @   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ÚImporterHelperéx   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 )Ú	SoSHelperz‡Provide 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	}| j j|v r&t d
| j j ¡}t||| j j ƒ}|S | j| j| j| jdœ}|D ]}| j j 	|¡rD|| ƒ } |S q3|S )z�Based 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   r4   \   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.sosnoderT   r   r   r*   )r   rT   Ú
_transportr   r   r   rM   |   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$   rU   r/   )	Úsos.collectorr@   Úsos.collector.clustersÚ_load_modulesr&   rX   r   r   r*   )r   r@   ÚsosrX   Úclusterr   r   r   rN   ƒ   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 )NrG   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>)rH   rI   r   r   Úload_local_policyÚinspectÚ
getmembersÚisclassÚ__subclasses__ÚpolicyÚmatch_plugin)r   rQ   ÚmemsÚpluginsÚpluginr!   r   r   r   rK   �   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$   rU   r   rg   r   )r   r   r*   Úsos.policies.distrosr   rF   ÚdistrosÚget_modulesr   Ú__name__Úlowerr+   )r   Ú_topicr\   Ú_helperrQ   rg   Ú_pr   r   r   rL   ˜   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 ]}|jdt|ƒd›|| › �dd� qD| ¡  dS )zhDisplays the help information for this component directly, that is
        help for `sos help`.
        zDetailed help for sos helpz±The '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.zØ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 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 Sectionsz‡The 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)rD   rE   zreport.plugins.$pluginr'   r)   rF   ú	z<36F)ÚnewlineN)r7   Úadd_textr   Úadd_sectionr9   )r   Ú	self_helpÚsubsectr   Úrep_exÚ
avail_helpÚsectionsÚsectr   r   r   r1   ¥   sF   þýÿ
	ÿÿ
ÿú	þzSoSHelper.display_self_help)ro   Ú
__module__Ú__qualname__Ú__doc__ÚdescÚconfigure_loggingÚload_policyÚ
load_probeÚarg_defaultsr   Úclassmethodr#   r.   r=   r4   rM   rN   rK   rL   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   z…This 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 )z—Set 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 )zçAdd 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‰   ru   Úlnr   r   r   rv   ü   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   rw     s   
zHelpSection.add_sectionc                 C   s(   || j v r	tdƒ‚t|||ƒ| j |< dS )z©Internal 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   )
ro   r~   r   r€   r   rŒ   rv   rw   r�   r9   r   r   r   r   r7   Ý   s    




r7   )rc   rH   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$   
ÿ A