3 گa~Z@spdZddlZddlZddlmZddlmZddlmZddlmZddlmZddlm Z dd lm Z dd lm Z ddl Z ddlZ dd lmZdd lmZdd lmZddlmZddlmZddlmZejeZdZdZdddZGdddZeZ e dZ!e j"j#ej$GdddZ%e j"j#ej$GdddZ&e e%e&fdddZ'e e%e&fdd d!d"Z(dS)#zFThis modules define the actual display implementations used in CertbotN)Any)Iterable)List)Optional)TextIO)Tuple)TypeVar)Union)errors) interfaces) constants) completer)util)osokZcancelz- '-c@seZdZddddZdS)_DisplayServiceN)returncCs d|_dS)N)display)selfr/usr/lib/python3.6/obj.py__init__,sz_DisplayService.__init__)__name__ __module__ __qualname__rrrrrr+srTc seZdZdZeeddfdd Zd*eeeeeddd d Zd+ee e e eefe efe ee ee ee e e eeee ee fd d d Zd,ee ee eeee eefdddZd-eeee ee eeeedddZd.ee ee e ee eeee ee efdddZee ee eee edddZeedddZd/ee ee eeee eefddd Zee ee fe ee ed!d"d#Zee e e eefe efdd$d%d&Ze e ee fd'd(d)ZZS)0 FileDisplayzFile-based display.N)outfileforce_interactivercs tj||_||_d|_dS)NF)superrrr skipped_interaction)rrr ) __class__rrr<s zFileDisplay.__init__TF)messagepausewrapr decoratercCs~|rtj|}tjd||jj|r(dndd|r6dndjtjt |d|jj |rz|j |rptj dn tjdd S) aDisplays a notification and waits for user acceptance. :param str message: Message to display :param bool pause: Whether or not the program should pause for the user's confirmation :param bool wrap: Whether or not the application should wrap text :param bool force_interactive: True if it's safe to prompt the user because it won't cause any workflow regressions :param bool decorate: Whether to surround the message with a decorated frame zNotifying user: %sz{line}{frame}{line}z {msg}{line}z {frame}{line})lineframemsgzPress Enter to Continuez!Not pausing for user confirmationN) r wrap_linesloggerdebugrwriteformatrlinesep SIDE_FRAMEflush _can_interactinput_with_timeout)rr$r%r&r r'rrr notificationBs     zFileDisplay.notification) r$choicesok_label cancel_label help_labeldefaultcli_flagr unused_kwargsrc KsJ|j||||} | dk r t| fS|j|||jt|\} } | | dfS)aCDisplay a menu. .. todo:: This doesn't enable the help label/button (I wasn't sold on any interface I came up with for this). It would be a nice feature :param str message: title of menu :param choices: Menu lines, len must be > 0 :type choices: list of tuples (tag, item) or list of descriptions (tags will be enumerated) :param default: default value to return (if one exists) :param str cli_flag: option used to set this value with the CLI :param bool force_interactive: True if it's safe to prompt the user because it won't cause any workflow regressions :returns: tuple of (`code`, `index`) where `code` - str display exit code `index` - int index of the user's selection :rtype: tuple N)_return_defaultOK _print_menu_get_valid_int_anslen) rr$r7r8r9r:r;r<r r=return_defaultcode selectionrrrmenucs  zFileDisplay.menu)r$r;r<r r=rcKsT|j||||}|dk r t|fStjd|d}tj|}|dkrLtdfSt|fS)aAccept input from the user. :param str message: message to display to the user :param default: default value to return (if one exists) :param str cli_flag: option used to set this value with the CLI :param bool force_interactive: True if it's safe to prompt the user because it won't cause any workflow regressions :returns: tuple of (`code`, `input`) where `code` - str display exit code `input` - str of the user's input :rtype: tuple Nz%s (Enter 'c' to cancel): cCz-1)rIrJ)r?r@rr,r5CANCEL)rr$r;r<r r=rDansrrrinputs zFileDisplay.inputYesNo)r$ yes_labelno_labelr;r<r r=rc Ks|j||||}|dk r|Stj|}|jjdjtjttj|d|jj xttj djtj |tj |d} | j |dj s| j |djrdS| j |dj s| j |djrRdSqRWdS) aQuery the user with a yes/no question. Yes and No label must begin with different letters, and must contain at least one letter each. :param str message: question for the user :param str yes_label: Label of the "Yes" parameter :param str no_label: Label of the "No" parameter :param default: default value to return (if one exists) :param str cli_flag: option used to set this value with the CLI :param bool force_interactive: True if it's safe to prompt the user because it won't cause any workflow regressions :returns: True for "Yes", False for "No" :rtype: bool Nz{0}{frame}{msg}{0}{frame})r*r+z {yes}/{no}: )yesnorTF)r?rr,rr/r0rr1r2r3r5Zparens_around_char startswithlowerupper) rr$rPrQr;r<r r=rDrLrrryesnos"   zFileDisplay.yesno)r$tagsr;r<r r=rc Ks|j||||}|dk r t|fSx|j|||jddd\}} |tkr| jsrdjddtdt|dD} tj | } |j | |} | r|| fS|j j d t j|j jq"|gfSq"WdS) aCDisplay a checklist. :param str message: Message to display to user :param list tags: `str` tags to select, len(tags) > 0 :param default: default value to return (if one exists) :param str cli_flag: option used to set this value with the CLI :param bool force_interactive: True if it's safe to prompt the user because it won't cause any workflow regressions :returns: tuple of (`code`, `tags`) where `code` - str display exit code `tags` - list of selected tags :rtype: tuple NzrSelect the appropriate numbers separated by commas and/or spaces, or leave input blank to select all options shownT)r rHcss|]}t|VqdS)N)str).0xrrr sz(FileDisplay.checklist..r>z!** Error - Invalid selection **%s)r?r@rArMstripjoinrangerCrZseparate_list_input_scrub_checklist_inputrr/rr1r3) rr$rXr;r<r r=rDrErLindicesZ selected_tagsrrr checklists$  "    zFileDisplay.checklist)promptr;r<r rcCsN|j|rdS|dkrMsz6FileDisplay._scrub_checklist_input..r>csg|]}|dqS)r>r)rZrj)rXrrrkYs) ValueErrorlistsetrC)rrarXZ indices_intrjr)rXrr`@s   z"FileDisplay._scrub_checklist_input)r$r7rcCs|r t|dtr dd|D}|jjdjtj|d|jjttjxDt|dD]6\}}dj||d}|jjt j ||jjtjqVW|jjttj|jj d S) zPrint a menu on the screen. :param str message: title of menu :param choices: Menu lines :type choices: list of tuples (tag, item) or list of descriptions (tags will be enumerated) rcSs g|]}d|d|dfqS)z%s - %srr>r)rZrIrrrrkgsz+FileDisplay._print_menu..z{new}{msg}{new})newr+r>z {num}: {desc})ZnumdescN) isinstancetuplerr/r0rr1r2 enumeraterr,r3)rr$r7irpr+rrrrA[s zFileDisplay._print_menu)max_rc Csd}|dkrdj|d}nd}x|dkrtj|}|jdsF|jdrNtd fSy$t|}|dksh||krpd }tWq tk r|jjdjt j |jj Yq Xq Wt |fS) a5Get a numerical selection. :param int max: The maximum entry (len of choices), must be positive :returns: tuple of the form (`code`, `selection`) where `code` - str display exit code ('ok' or cancel') `selection` - int user's selection :rtype: tuple r>zMSelect the appropriate number [1-{max_}] then [enter] (press 'c' to cancel): )ruz@Press 1 [enter] to confirm the selection (press 'c' to cancel): rIrJz{0}** Invalid input **{0}rvrv) r0rr5rTrKrirlrr/rr1r3r@)rrurFZ input_msgrLrrrrBys&    zFileDisplay._get_valid_int_ans)TTFT)NNNNNF)NNF)rNrONNF)NNF)NNF)rrr__doc__rboolrrYr6r rrrrirrGrMrWrbrr?r4rhrr`rArB __classcell__rr)r#rr7s6!> *&*)(rc sheZdZdZeeeddfdd Zdeeeee j ddd Z dee e e edd d dZ deeeeeefeefeeeeeeeeeeeeeefd ddZd eeeeeeeeefdddZd!eeeeeee eeee dddZd"eeeeeeeeeeeeefdddZd#eeeeeeeeefdddZZS)$NoninteractiveDisplayzKA display utility implementation that never asks for interactive user inputN)r unused_argsr=rcstj||_dS)N)r!rr)rrr{r=)r#rrrs zNoninteractiveDisplay.__init__r()r$r<extrarcCs8d}||7}|r|d|7}|r.|dj|7}tj|S)zNReturn error to raise in case of an attempt to interact in noninteractive modez 0 :type choices: list of tuples (tag, item) or list of descriptions (tags will be enumerated) :param int default: the default choice :param dict kwargs: absorbs various irrelevant labelling arguments :returns: tuple of (`code`, `index`) where `code` - str display exit code `index` - int index of the user's selection :rtype: tuple :raises errors.MissingCommandlineFlag: if there was no default Nz Choices: )rreprr@) rr$r7r8r9r:r;r<r=rrrrGszNoninteractiveDisplay.menu)r$r;r<r=rcKs|dkr|j||t|fS)aKAccept input from the user. :param str message: message to display to the user :returns: tuple of (`code`, `input`) where `code` - str display exit code `input` - str of the user's input :rtype: tuple :raises errors.MissingCommandlineFlag: if there was no default N)rr@)rr$r;r<r=rrrrMs  zNoninteractiveDisplay.input)r$rPrQr;r<r=rcKs|dkr|j|||S)a+Decide Yes or No, without asking anybody :param str message: question for the user :param dict kwargs: absorbs yes_label, no_label :raises errors.MissingCommandlineFlag: if there was no default :returns: True for "Yes", False for "No" :rtype: bool N)r)rr$rPrQr;r<r=rrrrWs  zNoninteractiveDisplay.yesno)r$rXr;r<r=rcKs(|dkr |j||dj|dt|fS)ajDisplay a checklist. :param str message: Message to display to user :param list tags: `str` tags to select, len(tags) > 0 :param dict kwargs: absorbs default_status arg :returns: tuple of (`code`, `tags`) where `code` - str display exit code `tags` - list of selected tags :rtype: tuple Nz? ?)rr^r@)rr$rXr;r<r=rrrrbszNoninteractiveDisplay.checklistcKs|j|||S)aSimulate prompting the user for a directory. This function returns default if it is not ``None``, otherwise, an exception is raised explaining the problem. If cli_flag is not ``None``, the error message will include the flag that can be used to set this value with the CLI. :param str message: prompt to give the user :param default: default value to return (if one exists) :param str cli_flag: option used to set this value with the CLI :returns: tuple of the form (`code`, `string`) where `code` - int display exit code `string` - input entered by the user )rM)rr$r;r<r=rrrrhsz&NoninteractiveDisplay.directory_select)r()FTT)NNNNN)NN)NNNN)NN)NN)rrrrwrrrrYrr r~rrxr6r rrrirGrMrWrrbrhryrr)r#rrzs" T6rz)rcCstjstdtjS)zGet the display utility. :return: the display utility :rtype: Union[FileDisplay, NoninteractiveDisplay] :raise: ValueError if the display utility is not configured yet. zlThis function was called too early in Certbot's execution as the display utility hasn't been configured yet.)_SERVICErrlrrrr get_display-sr)rrcCstjj|tj|t_dS)zqSet the display service. :param Union[FileDisplay, NoninteractiveDisplay] display: the display service N)zopeZ componentZprovideUtilityr IDisplayrr)rrrr set_display;sr))rwZloggingreZtypingrrrrrrrr Zzope.componentrZzope.interfaceZcertbotr r Zcertbot._internalr Zcertbot._internal.displayr rZcertbot.compatrZ getLoggerrr-r@rKr2rrrZ interfaceZ implementerrrrzrrrrrrs@                k