o ]LbZi@sdZddlmZmZddlZddlZddlZddlmZddl m Z m Z ddl m Z ddlmZmZmZmZmZmZmZdd lmZmZmZejZd d ZGd d d eZGdddeZddZGdddeZ ddZ!GdddeZ"GdddeZ#GdddeZ$GdddeZ%GdddeZ&Gd d!d!eZ'Gd"d#d#eZ(Gd$d%d%eZ)e j*d&d'Gd(d)d)eZ+d*d+Z,dGd,d-Z-d.d/Z.dGd0d1Z/d2d3Z0d4d5Z1dHd6d7Z2dHd8d9Z3eZ4Gd:d;d;ej5Z6 < < Z7d?d@Z8ej9dAdBZ:ej9dCdDZ;dEdFZ>> def show(fn, verbose=False, **opts): ... import sys ... from . import ui as uimod ... ui = uimod.ui() ... ui.verbose = verbose ... ui.pushbuffer() ... try: ... return fn(ui, ui.formatter(pycompat.sysbytes(fn.__name__), ... pycompat.byteskwargs(opts))) ... finally: ... print(pycompat.sysstr(ui.popbuffer()), end='') Basic example: >>> def files(ui, fm): ... files = [(b'foo', 123, (0, 0)), (b'bar', 456, (1, 0))] ... for f in files: ... fm.startitem() ... fm.write(b'path', b'%s', f[0]) ... fm.condwrite(ui.verbose, b'date', b' %s', ... fm.formatdate(f[2], b'%Y-%m-%d %H:%M:%S')) ... fm.data(size=f[1]) ... fm.plain(b'\n') ... fm.end() >>> show(files) foo bar >>> show(files, verbose=True) foo 1970-01-01 00:00:00 bar 1970-01-01 00:00:01 >>> show(files, template=b'json') [ { "date": [0, 0], "path": "foo", "size": 123 }, { "date": [1, 0], "path": "bar", "size": 456 } ] >>> show(files, template=b'path: {path}\ndate: {date|rfc3339date}\n') path: foo date: 1970-01-01T00:00:00+00:00 path: bar date: 1970-01-01T00:00:01+00:00 Nested example: >>> def subrepos(ui, fm): ... fm.startitem() ... fm.write(b'reponame', b'[%s]\n', b'baz') ... files(ui, fm.nested(b'files', tmpl=b'{reponame}')) ... fm.end() >>> show(subrepos) [baz] foo bar >>> show(subrepos, template=b'{reponame}: {join(files % "{path}", ", ")}\n') baz: foo, bar )absolute_importprint_functionN)_)hexshort)attr)errorpycompattemplatefilters templatekw templater templateutilutil)cborutildateutil stringutilcCst|tdtttjttfS)zCheck if the given object can be directly passed in to formatter's write() and data() functions Returns False if the object is unsupported or must be pre-processed by formatdate(), formatdict(), or formatlist(). N) isinstancetypeboolintr longfloatbytes)objr5/usr/lib/python3/dist-packages/mercurial/formatter.py isprintablesrc@DeZdZdZdZeddZeddZeddZed d Z d S) _nullconverterz=convert non-primitive data types to be processed by formatterFcC|S)z$wrap nested data by appropriate typerdatatmplseprrr wrapnestedsz_nullconverter.wrapnestedcCs|\}}t||fSz(convert date tuple to appropriate format)r)datefmttstzrrr formatdates z_nullconverter.formatdatecCt|Sz:convert dict or key-value pairs to appropriate dict format)dictr"keyvaluer(r$rrr formatdictsz_nullconverter.formatdictcCr,z+convert iterable to appropriate list format)listr"namer(r$rrr formatlistsz_nullconverter.formatlistN __name__ __module__ __qualname____doc__ storecontext staticmethodr%r+r2r7rrrrrs   rc@seZdZdZddZddZddZdd Zd d Zd*d dZ d+ddZ d,ddZ ddZ ddZ ddZddZdd Zd!d"Zd#d$Zd-d&d'Zd(d)ZdS). baseformatterFcCs(||_||_||_||_d|_t|_dSN)_ui_topic_opts _converter_itemrhexfunc)selfuitopicopts converterrrr__init__s  zbaseformatter.__init__cCr r@rrGrrr __enter__zbaseformatter.__enter__cCs|dur |dSdSr@)end)rGexctypeexcvalue tracebackrrr__exit__s zbaseformatter.__exit__cCdS)z0show a formatted item once all data is collectedNrrMrrr _showitemzbaseformatter._showitemcCs|jdur |i|_dS)z begin an item in the format listNrErVrMrrr startitems  zbaseformatter.startitem%a %b %d %H:%M:%S %Y %1%2cCs|j||Sr&)rDr+)rGr'r(rrrr+zbaseformatter.formatdatekeyvalueN cCs|j|||||Sr-)rDr2)rGr"r0r1r(r$rrrr2szbaseformatter.formatdictcCs|j||||Sr3)rDr7)rGr"r6r(r$rrrr7szbaseformatter.formatlistcKs|t|}tdd|DsJ|jjrctxfctxrepoNr.0krrr sz(baseformatter.context..r`r_raN) r byteskwargsallrDr= changectxreporEupdate)rGctxsrrrcontexts zbaseformatter.contextcCstS)z#set of field names to be referenced)setrMrrrdatahintszbaseformatter.datahintcKst|}|j|dS)z8insert data into item that's not shown in default outputN)r rfrErjrGr"rrrr"s zbaseformatter.datacOs:|}t|t|ksJ||f|jt||dS)z3do default text output while assigning data to itemNsplitlenrErjzip)rGfieldsdeftext fielddatarJ fieldkeysrrrwriteszbaseformatter.writecOs2|}t|t|ksJ|jt||dS)z4do conditional write (primarily for plain formatter)Nrp)rGcondrtrurvrJrwrrr condwriteszbaseformatter.condwritecKrU)z$show raw text for non-templated modeNrrGtextrJrrrplainrWzbaseformatter.plaincCrU)zcheck for plain formatter usageFrrMrrrisplainzbaseformatter.isplaincCs*g}|j||||j|<t|j|j|S)z9sub formatter to store nested data in the specified field)rDr%rE_nestedformatterrA)rGfieldr#r$r"rrrnested szbaseformatter.nestedcCs|jdur |dSdS)zend output for the formatterNrXrMrrrrPs  zbaseformatter.end)rZ)r\r]Nr^)Nr^Nr)r9r:r; strict_formatrLrNrTrVrYr+r2r7rlrnr"rxrzr}r~rrPrrrrr?s&       r?cCst|||tdS)zformatter that prints nothing)rK)r?r)rHrIrJrrr nullformatterrc@s eZdZdZddZddZdS)rz6build sub items and store them in the parent formattercCstj||di|d||_dS)Nr)rIrJrK)r?rL_data)rGrHrKr"rrrrLs  z_nestedformatter.__init__cC|j|jdSr@rappendrErMrrrrV#z_nestedformatter._showitemN)r9r:r;r<rLrVrrrrrs rcCst|tr tt|S|S)z'iterate key-value pairs in stable order)rr.sortedr iteritems)r"rrr _iteritems's rc@r) _plainconverterz(convert non-primitive data types to textFcCs td)Ns%plainformatter should never be nested)r ProgrammingErrorr!rrrr%3 z_plainconverter.wrapnestedcCs t||S)z(stringify date tuple in the given format)rdatestrr'r(rrrr+7s z_plainconverter.formatdatecs6tjdur dtj|fddt|DS)z*stringify key-value pairs separated by sepNs%s=%sc3s(|]\}}||fVqdSr@rrcrdvr(prefmtrrreCs z-_plainconverter.formatdict..)r identitybytestrjoinrr/rrrr2<sz_plainconverter.formatdictcs2tjdur dtj|fdd|DS)z#stringify iterable separated by sepNs%sc3s|] }|VqdSr@r)rcerrrreNsz-_plainconverter.formatlist..)r rrrr5rrrr7Gs z_plainconverter.formatlistNr8rrrrr.s    rc@sZeZdZdZddZddZddZdd Zd d Zd d Z ddZ dddZ ddZ dS)plainformatterzthe default text output schemecsLt||||t|jrt|_nt|_|ur|j|_dSfdd|_dS)Ncs |Sr@)rx)srJoutrr]s z)plainformatter.__init__..) r?rLr debugflagrrFrrx_writerGrHrrIrJrrrrLTs zplainformatter.__init__cCdSr@rrMrrrrY_rOzplainformatter.startitemcKrr@rrorrrr"brOzplainformatter.datacOs|j||fi|dSr@r)rGrtrurvrJrrrrxeszplainformatter.writecOs"|r|j||fi|dSdS)zdo conditional writeNr)rGryrtrurvrJrrrrzhszplainformatter.condwritecKs|j|fi|dSr@rr{rrrr}mszplainformatter.plaincCrU)NTrrMrrrr~prOzplainformatter.isplainNrcCr r@r)rGrr#r$rrrrsrzplainformatter.nestedcCrr@rrMrrrrPwrOzplainformatter.endr) r9r:r;r<rLrYr"rxrzr}r~rrPrrrrrQs   rc@$eZdZddZddZddZdS)debugformattercCs.t||||t||_|jd|jdS)Ns%s = [ )r?rLr_outrxrBrrrrrL|szdebugformatter.__init__cCs"|jdtj|jddddS)Ns %s, r)indentlevel)rrxrpprintrErMrrrrVszdebugformatter._showitemcCt||jddS)Ns] r?rPrrxrMrrrrP zdebugformatter.endNr9r:r;rLrVrPrrrrr{s rc@r)pickleformattercCs"t||||t||_g|_dSr@)r?rLrrrrrrrrLs zpickleformatter.__init__cCrr@rrMrrrrVrzpickleformatter._showitemcCs"t||jt|jdSr@)r?rPrrxpickledumpsrrMrrrrPs zpickleformatter.endNrrrrrrs rc@(eZdZdZddZddZddZdS) cborformatterz2serialize items as an indefinite-length CBOR arraycCs*t||||t||_|jtjdSr@)r?rLrrrxrBEGIN_INDEFINITE_ARRAYrrrrrLszcborformatter.__init__cCs|jdt|jdSr)rrxrr streamencoderErMrrrrVszcborformatter._showitemcCst||jtjdSr@)r?rPrrxrBREAKrMrrrrPs zcborformatter.endN)r9r:r;r<rLrVrPrrrrrs  rc@r) jsonformatterTcCs.t||||t||_|jdd|_dS)N[T)r?rLrrrx_firstrrrrrLs  zjsonformatter.__init__cCs|jrd|_n|jd|jdd}t|jD] \}}|r%d}n|jdtj|dd}|jd||fq|jddS) NF,s { Ts, )paranoids "%s": %ss })rrrxrrEitemsr json)rGfirstrdrurrrrVs   zjsonformatter._showitemcCr)N ] rrMrrrrPrzjsonformatter.endN)r9r:r;rrLrVrPrrrrrs  rc@r) _templateconverterz=convert non-primitive data types to be processed by templaterTcCstj|||dS)z$wrap nested data by templatable type)r#r$)r mappinglistr!rrrr%sz_templateconverter.wrapnestedcCs t|S)zreturn date tuple)rr'rrrrr+s z_templateconverter.formatdatecs6ttfdd}tj|dS)zAbuild object that can be evaluated as either plain string or dictc3stVdSr@)rr2rr"r(r0r$r1rrfsz(_templateconverter.formatdict..f)r0r1r(gen)rsortdictrr hybriddict)r"r0r1r(r$rrrrr2s  z_templateconverter.formatdictcs,tfdd}tj|dS)zAbuild object that can be evaluated as either plain string or listc3stVdSr@)rr7rr"r(r6r$rrrsz(_templateconverter.formatlist..f)r6r(r)r4r hybridlist)r"r6r(r$rrrrr7sz_templateconverter.formatlistNr8rrrrrs    rc@sDeZdZdddZddZddZejdd Zd d Z d d Z dS)templateformatterNcCs|t||||t||_|j|_t||tjt |tj d|_ |r'|j j |t||j gd|_t|_|didS)Ndefaults resourcescache) docheader docfooter separatorr)r?rLrrref_tref loadtemplaterr keywordstemplateresources defaulttempl_trrjtemplatepartsmap_parts itertoolscount_counter _renderitem)rGrHrrIrJspecoverridetemplatesrrrrLs"  ztemplateformatter.__init__cCsB|j}t|j|d<}|dkr|di||j|dS)Nsindexrr)rEcopynextrrr)rGitemindexrrrrVs  ztemplateformatter._showitemcCsZ||jvrdS|j|}|D]\}}|durt|||<q|j|j||dSr@)rrr wrappedvaluerrxrrender)rGpartrrrdrrrrr s  ztemplateformatter._renderitemcCs|j|jSr@)r symbolsusedrrMrrr _symbolsusedr[ztemplateformatter._symbolsusedcCs |jdS)z5set of field names to be referenced from the templater)rrMrrrrnrztemplateformatter.datahintcCst||didS)Nr)r?rPrrMrrrrPrztemplateformatter.endr@) r9r:r;rLrVrr propertycacherrnrPrrrrrs   rT)frozenc@s<eZdZeZeZeZejddZejddZ dS) templatespecN)default) r9r:r;ribrr#mapfilerefargsfprrrrr!s  rcCs tdddSr@rrrrrempty_templatespec*s rcCst|dd|Sr@r)rrrrrreference_templatespec.srcCs$tjr t|tr Jdtd|dS)Nstmpl must not be a strr)r ispy3rstrr)r#rrrliteral_templatespec2s rcCst|d||dS)N)rr)rIrrrrrmapfile_templatespec8src Cst|stSd|vr t|S|dvrt|S|d\}}}|dvr7|r7|dr7t|t||dddStj |d sUt d |pJt |\}}|rUt |||S| d |r_t|S|d kru| td tttdd|vs}d|vrtj|rtj|drt |tj|St|d}|}Wdt|S1swYt|St|S)aFind the template matching the given -T/--template spec 'tmpl' 'tmpl' can be any of the following: - a literal template (e.g. '{rev}') - a reference to built-in template (i.e. formatter) - a map-file name or path (e.g. 'changelog') - a reference to [templates] in config file - a path to raw template file A map file defines a stand-alone template environment. If a map file selected, all templates defined in the file will be loaded, and the template matching the given topic will be rendered. Aliases won't be loaded from user config, but from the map file. If no map file selected, all templates in [templates] section will be available as well as aliases in [templatealias]. {>cborjsondebugpickle(>rr)N)rrs map-cmdline. templatesslistsavailable styles: %s sspecify a template/\smap-srb)rrr partitionendswithr parseexprospathrqtry_open_templaterconfigrxr stylelistr Abortisfilebasename startswithrealpathr posixfileread) rHrIr#funcfsepftailmapnamerrrrrlookuptemplate<sB     rcs`|j|ji}|jr|fdd|D|S|jr.|D]}d|j|f}|vr-|||<q|S)zCreate a mapping of {part: ref}c3s |] }|vr||fVqdSr@r)rcptrrresz#templatepartsmap..s%s:%s)rrrj)rr partnamespartsmaprrrrrr~s rcCsD|jr|jrJ|jrtjj|j|j|||dSt||j|||dS)zPCreate a templater from either a literal template or loading from a map filer)r#rr frommapfiler maketemplater)rHrrrrrrrrs rcCsJ|d}tj||||d}|jdd|dD|r#||jd<|S)z0Create a templater from a string template 'tmpl's templatealias)rrraliasescss"|] \}}|t|fVqdSr@)r unquotestringrrrrres z maketemplater..rr) configitemsr rrj)rHr#rrrr"rrrrr!s   r!c@steZdZdZdddZddZddZd d Zd d Zd dZ ddZ ddZ ddZ ddZ ddZe edZdS)rz@Resource mapper designed for the default templatekw and functionNcCsi||d|_dS)N)cacheraui)_resmap)rGrHrirrrrLs ztemplateresources.__init__csfddDS)Ncs h|] }|dur|qSr@)_getsomerbmappingrGrr sz2templateresources.availablekeys..) knownkeysrGr*rr)r availablekeyss ztemplateresources.availablekeyscCshdS)N>r&r_r`rar%revcacherrMrrrr,sztemplateresources.knownkeyscCs@||vrdS|||}|tur|j|||}||<|Sr@)r,r( _placeholder _loadermaprGr*r0rrrrlookups  ztemplateresources.lookupcCs|i}||r i|d<||r ||r t||d}||d<d|vr.||dr.t|d<d|vr<||drr2rrr _getliterals ztemplateresources._getliteralcCsd|vpd|vS)z=Test if context revision is set or unset in the given mappingr4r_rr-rrrr6rztemplateresources._hasnodespeccCN||d}||d}|dus|durdSz||WStjy&YdSw)Nrar4)r(r?r RepoLookupError)rGr*rinoderrr_loadctx   ztemplateresources._loadctxcCr@)Nr_r5)r(r?r LookupError)rGr*ctxr rrr _loadfctxrDztemplateresources._loadfctx)r_r`r@)r9r:r;r<rLr.r,r3r<r(r8r?r6rCrGr1rrrrrs     rrc CsT|j|i} |r|| d|j<|r|| d|j<|r || d|j<t|||||| dS)z]Build template formatter that handles customizable built-in templates such as -Tjson(...)s %s:docheaders %s:docfooters %s:separator)r)rr) rHrrIrJrr# docheader docfooter separator templatesrrr_internaltemplateformatters  rLc Csxt|||dd}|jdkr%|jdur%t|||||d|jtjtjdS|jdkr1t||||S|jdkrK|jdurKt|||||d|jdd d d S|jdkrWt ||||S|jd krl|jduseJd t ||||S|jdkr|jduszJd t ||||S|js|j s|j r|jdusJd t|||||S|ddrt ||||S|ddrt ||||St||||S)Nstemplaterrs{dict(%s)|cbor})r#rHrIrs{dict(%s)|json}s[ rs, )r#rHrIrJrzfunction-style not supportedrr&s formatdebugs formatjson)rr=rrrLrrrrrrrr#rr configboolr)rHrrIrJrrrr formatter sT      rNc csxt|d+}t|||| }|VWdn1swYWddSWddS1s5wYdS)zrCreate a formatter that writes outputs to the specified file Must be invoked using the 'with' statement. swbN)rrrN)rHfilenamerIrJrfmrrr openformatterOs"rQccs |VdSr@r)rPrrr _neverendingZs rRcCs |r t|j||j|jSt|S)zCreate a formatter backed by file if filename specified, else return the given formatter Must be invoked using the 'with' statement. This will never call fm.end() of the given formatter. )rQrArBrCrR)rPrOrrr maybereopen_srSr@)NNN)rrr)=r< __future__rr contextlibrr i18nrrBrr thirdpartyrr r r r r rrutilsrrrrrobjectrr?rrrrrrrrrrrrrrrrrrrrr!r0resourcemapperrrLrNcontextmanagerrQrRrSrrrrs^d  $  a #* & 2  B _ /