a !c!0@sddlmZddlmZddlmZddlZddlZddlZddlZddl Z ddl Z Gddde Z ddZ d d Zd d Zd dZddZddZddZd'ddZddZd(ddZd)ddZd d!Zd"d#Zd$d%Zed&\ZZeZdS)*)print_function)unicode_literals)unicodeNc@s$eZdZddZddZddZdS) UnicodeStreamcCs||_||_dSN)streamencoding)selfrrr ,/usr/lib/python3.9/site-packages/dnf/i18n.py__init__$szUnicodeStream.__init__cCst|ts.tjjr ||jdn ||jd}z|j |WnZt y||jjd}t |jdrx|jj |n||jjd}|j |Yn0dS)Nreplacebackslashreplacebufferignore) isinstancestrdnfpycompPY3decoderencoderwriteUnicodeEncodeErrorhasattrr)r sZs_bytesr r r r(s    zUnicodeStream.writecCs t|j|Sr)getattrr)r namer r r __getattr__7szUnicodeStream.__getattr__N)__name__ __module__ __qualname__r rrr r r r r#srcCs0|dur dS|}|ds(|dr,dSdS)aReturn true if encoding can express any Unicode character. Even if an encoding can express all accented letters in the given language, we can't generally settle for it in DNF since sometimes we output special characters like the registered trademark symbol (U+00AE) and surprisingly many national non-unicode encodings, including e.g. ASCII and ISO-8859-2, don't contain it. NFzutf-Zutf_T)lower startswith)rr"r r r _full_ucd_support:s r$cCstd}|drdS|S)z= Take the best shot at the current system's string encoding. FZANSIzutf-8)localegetpreferredencodingr#)rr r r _guess_encodingKs r'c CsztjtjdWn|tjyztjtjddtjd<Wn.tjyptjtjddtjd<Yn0td tjdt j dYn0dS)NzC.UTF-8LC_ALLCz&Failed to set locale, defaulting to {})file) rr setlocaler%r)Errorosenvironprintformatsysstderrr r r r setup_localePsr4cCs^tj}|sttjtjz |j}Wnty>d}Yn0t|sZt |t t_dSdS)z Check that stdout is of suitable encoding and handle the situation if not. Returns True if stdout was of suitable encoding already and no changes were needed. NFT) r2stdoutisattysignalSIGPIPESIG_DFLrAttributeErrorr$rr')r5rr r r setup_stdout^s   r;cCst|ddtjS)z It uses print instead of passing the prompt to raw_input. raw_input doesn't encode the passed string and the output goes into stderr r()end)r0rrZ raw_input)Zucstringr r r ucd_inputrs r=cCstjjr:tj|r$t|tddSt|tr2|St|St|tjjrL|St|drxztj|WSt yvYn0tjjt|tddSdS)zD Like the builtin unicode() but tries to use a reasonable encoding. r)errorsZ __unicode__N) rrrZ is_py3bytesrr'rrr UnicodeError)objr r r ucd}s    rAcCst|dvrdSdS)N)WF) unicodedataZeast_asian_width)Zucharr r r _exact_width_charsrGcCsV|durt||fSd}d}|D],}t|}|||kr<qN||7}||7}q ||fS)a' Return the textual width of a Unicode string, chopping it to a specified value. This is what you want to use instead of %.*s, as it does the "right" thing with regard to different Unicode character width Eg. "%.*s" % (10, msg) <= becomes => "%s" % (chop_str(msg, 10)) Nrr() exact_widthrG)msgchopwidthZ chopped_msgcharZ char_widthr r r chop_strs   rMcCstdd|DS)zQ Calculates width of char at terminal screen (Asian char counts for two) css|]}t|VqdSr)rG).0cr r r zexact_width..)sum)rIr r r rHsrHTr(cCsjt||\}}||kr0|s|rfd|||g}n6d||}|rTd||||g}nd||||g}|S)a Expand a msg to a specified "width" or chop to same. Expansion can be left or right. This is what you want to use instead of %*.*s, as it does the "right" thing with regard to different Unicode character width. prefix and suffix should be used for "invisible" bytes, like highlighting. Examples: ``"%-*.*s" % (10, 20, msg)`` becomes ``"%s" % (fill_exact_width(msg, 10, 20))``. ``"%20.10s" % (msg)`` becomes ``"%s" % (fill_exact_width(msg, 20, 10, left=False))``. ``"%s%.10s%s" % (pre, msg, suf)`` becomes ``"%s" % (fill_exact_width(msg, 0, 10, prefix=pre, suffix=suf))``. r( )rMjoin)rIfillrJleftprefixsuffixrKZextrar r r fill_exact_widths rYFcsfdd|d}|ddd}g}|}d}d}d} |D]`} | d} || } } | \}} d} |rv| rvd } |r|t| krd } |r| s|d kr|| krd } | r||d|}d}|t| krd } |r| d} | } t|| |krd}||| |}q@d }| d}|} | }|s8|d kr8|}|D]V}|t| |krt| t|kr|| d|d|} | |7} | d7} q<| dd}q@|r||dd|S) zq Works like we want textwrap.wrap() to work, uses Unicode strings and doesn't screw up lists/blocks/etc. csd}d}|D]}|dkrq&|d7}q |dvr6|dfSt||ddd}|dvr||t|d}|dpz|d}|r||d|fS|dfS)NrXrSrE)-*.oâ)r\r]r^r_u•u‣u∘)rMlen)linecountZbyteZlist_chrZnxt_indent_at_begr r res z%textwrap_fill.._indent_at_beg  z FrrSTr()rstripr splitraappendlstriprHrT)textrKinitial_indentsubsequent_indentlinesretindentZ wrap_lastZcsabZ cspc_indentrbZlsabZ lspc_indentZforce_nlwordsZspcsZwordr rdr textwrap_fillsh           rtcCsHt|}t|}||kr|S||kr4||kr0|S|S||kr@|S|SdS)a Automatically selects the short (abbreviated) or long (full) message depending on whether we have enough screen space to display the full message or not. If a caller by mistake passes a long string as msg_short and a short string as a msg_long this function recognizes the mistake and swaps the arguments. This function is especially useful in the i18n context when you cannot predict how long are the translated messages. Limitations: 1. If msg_short is longer than width you will still get an overflow. This function does not abbreviate the string. 2. You are not obliged to provide an actually abbreviated string, it is perfectly correct to pass the same string twice if you don't want any abbreviation. However, if you provide two different strings but having the same width this function is unable to recognize which one is correct and you should assume that it is unpredictable which one is returned. Example: ``select_short_long (10, _("Repo"), _("Repository"))`` will return "Repository" in English but the results in other languages may be different. N)rH)rKZ msg_shortZmsg_longZ width_shortZ width_longr r r select_short_long'srucCs2tdd}tjjj|dd}t|tj|S)z< Easy gettext translations setup based on given domain name cs fddS)Ncs t|Sr)rA)wZfncr r TrQz2translation..ucd_wrapper..r rwr rwr ucd_wrapperSsz translation..ucd_wrapperT)fallback)r4rrgettext translationmapZ gettext_setup)rrytr r r r|Nsr|cCs(t|td|}d|vr |S|SdS)Nrh)_chr)contextmessageresultr r r pgettextYsrr)N)NTr(r()rZr(r()Z __future__rrZ dnf.pycomprrr%r.r7r2rFobjectrr$r'r4r;r=rArGrMrHrYrtrur|rrZP_ZC_r r r r s2      " O'