#xPc@sdZddkZddkZddkZddkZddkZddkZddkZddkZddk Z ddk Z ddk Z yddk Z Wne j onXddkZddkZddkZddkZddklZlZlZlZlZlZlZlZlZlZddklZddkl Z l!Z!l"Z"l#Z#l$Z$ddk%l&Z&ddk'l(Z(dZ)d Z*d ei+fd YZ,d ei-fd YZ.dei/fdYZ0dei/fdYZ1dei/fdYZ2e.e1e2fZ3de4fdYZ5dei6fdYZ7dei8fdYZ9dei:fdYZ;e;e,e1e.e0fZ<dZ=dS(s+ Functionality for Command Line Interface. iN( t PublicErrort CommandErrort HelpErrort InternalErrortNoSuchNamespaceErrortValidationErrortNotFoundtNotConfiguredErrort PromptFailedtConversionError(tCLI_TAB(tPasswordtBytestFiletStrtStrEnum(t_(t API_VERSIONcCs't|tpt|iddS(sl Takes a Python identifier and transforms it into form suitable for the Command Line Interface. Rt-(t isinstancetstrtAssertionErrortreplace(tname((s./usr/lib/python2.6/site-packages/ipalib/cli.pytto_cli:scCst|iddS(sh Takes a string from the Command Line Interface and transforms it into a Python identifier. RR(RR(tcli_name((s./usr/lib/python2.6/site-packages/ipalib/cli.pytfrom_cliCsttextuicBseZdZdZd$dZdZdZdZd$dZ dZ dZ d$d Z d$d Z d d Zd d Zdd edZd hdgedZd$d$d$edd dZd$d$d$edd dZeedddZdZdZdZdZdZd$dZdZedZdZd$d$e d Z!d$d!Z"ed"Z#ed#Z$RS(%s; Backend plugin to nicely format output to stdout. c CswtiiocyHtitititiddddd}ti d|dSWqst j odSXndS(s Return the width (in characters) of output tty. If stdout is not a tty, this method will return ``None``. tHHHHiiN( tsyststdouttisattytfcntltioctlttermiost TIOCGWINSZtstructtpacktunpacktIOErrortNone(tselftwinsize((s./usr/lib/python2.6/site-packages/ipalib/cli.pyt get_tty_widthPscst|ttfjotdtt|fnt|djodSdjotd|DStfd|DS(s Return the max width (in characters) of a specified column. For example: >>> ui = textui() >>> rows = [ ... ('a', 'package'), ... ('an', 'egg'), ... ] >>> ui.max_col_width(rows, col=0) # len('an') 2 >>> ui.max_col_width(rows, col=1) # len('package') 7 >>> ui.max_col_width(['a', 'cherry', 'py']) # len('cherry') 6 srows: need %r or %r; got %ricssx|]}t|VqWdS(N(tlen(t.0trow((s./usr/lib/python2.6/site-packages/ipalib/cli.pys ys c3s#x|]}t|VqWdS(N(R,(R-R.(tcol(s./usr/lib/python2.6/site-packages/ipalib/cli.pys zs N(ttypetlistttuplet TypeErrorR,R(tmax(R)trowsR/((R/s./usr/lib/python2.6/site-packages/ipalib/cli.pyt max_col_width`s cCsE|titifjptt|dddjodS|iS(NtencodingsUTF-8(RtstdinRRtgetattrR(R7(R)tstream((s./usr/lib/python2.6/site-packages/ipalib/cli.pyt__get_encoding|s cskt|tjo iti}|i|St|ttfjotfd|DS|S(s) Decode text from stdin. c3s"x|]}i|VqWdS(N(tdecode(R-tv(R)(s./usr/lib/python2.6/site-packages/ipalib/cli.pys s (R0Rt_textui__get_encodingRR8R<R1R2(R)tvalueR7((R)s./usr/lib/python2.6/site-packages/ipalib/cli.pyR<s cCs9t|tjpt|iti}|i|S(s3 Encode text for output to stdout. (R0tunicodeRR>RRtencode(R)t unicode_textR7((s./usr/lib/python2.6/site-packages/ipalib/cli.pyRAscCs+|djp |djo ||S||S(Ni(R((R)tntsingulartplural((s./usr/lib/python2.6/site-packages/ipalib/cli.pyt choose_numbers cCs)t|tjoti|S|SdS(s Convert a binary value to base64. We know a value is binary if it is a python str type, otherwise it is a plain string. N(R0Rtbase64t b64encode(R)R?((s./usr/lib/python2.6/site-packages/ipalib/cli.pyt encode_binaryscCst|GHdS(s? Print exactly like ``print`` statement would. N(R@(R)tstring((s./usr/lib/python2.6/site-packages/ipalib/cli.pyt print_plainscCsb|djo|i}n|dj o)|t|jo||d d}nt|GHdS(s Force printing on a single line, using ellipsis if needed. For example: >>> ui = textui() >>> ui.print_line('This line can fit!', width=18) This line can fit! >>> ui.print_line('This line wont quite fit!', width=18) This line wont ... The above example aside, you normally should not specify the ``width``. When you don't, it is automatically determined by calling `textui.get_tty_width()`. is...N(R(R+R,R@(R)ttexttwidth((s./usr/lib/python2.6/site-packages/ipalib/cli.pyt print_lines   cCsI|djo|i}nx%ti|i|D] }|GHq6WdS(s( Print a paragraph, automatically word-wrapping to tty width. For example: >>> text = ''' ... Python is a dynamic object-oriented programming language that can ... be used for many kinds of software development. ... ''' >>> ui = textui() >>> ui.print_paragraph(text, width=45) Python is a dynamic object-oriented programming language that can be used for many kinds of software development. The above example aside, you normally should not specify the ``width``. When you don't, it is automatically determined by calling `textui.get_tty_width()`. The word-wrapping is done using the Python ``textwrap`` module. See: http://docs.python.org/library/textwrap.html N(R(R+ttextwraptwraptstrip(R)RLRMtline((s./usr/lib/python2.6/site-packages/ipalib/cli.pytprint_paragraphs  icCst||GHdS(s{ Print at specified indentation level. For example: >>> ui = textui() >>> ui.print_indented('One indentation level.') One indentation level. >>> ui.print_indented('Two indentation levels.', indent=2) Two indentation levels. >>> ui.print_indented('No indentation.', indent=0) No indentation. N(R (R)RLtindent((s./usr/lib/python2.6/site-packages/ipalib/cli.pytprint_indentedscCs>x7|D]/\}}|id||i|f|qWdS(s Print (key = value) pairs, one pair per line. For example: >>> items = [ ... ('in_server', True), ... ('mode', u'production'), ... ] >>> ui = textui() >>> ui.print_keyval(items) in_server = True mode = u'production' >>> ui.print_keyval(items, indent=0) in_server = True mode = u'production' Also see `textui.print_indented`. s%s = %rN(RURI(R)R5RTtkeyR?((s./usr/lib/python2.6/site-packages/ipalib/cli.pyt print_keyvals s%s: %sc s,t|tptt|ttfp'i||i|f|n|o8x|D])}i||i|f|qbWntfd|}t|djoet |dttfjoHx@|D]8}di |}i||i|f|qWdSt|djodi |}ndSi } | ou|ondt |dt|df} | t| 8} t i|| d t}t|djo d g}qn |g}i|||df|x)|d D]} id| | fqWdS( s Print an ldap attribute. For example: >>> attr = 'dn' >>> ui = textui() >>> ui.print_attribute(attr, u'dc=example,dc=com') dn: dc=example,dc=com >>> attr = 'objectClass' >>> ui.print_attribute(attr, [u'top', u'someClass'], one_value_per_line=False) objectClass: top, someClass >>> ui.print_attribute(attr, [u'top', u'someClass']) objectClass: top objectClass: someClass cs i|S((RI(R=(R)(s./usr/lib/python2.6/site-packages/ipalib/cli.pytsis: Ns, s%s%st itbreak_long_wordsui(Rt basestringRR1R2RURItmapR,R0tjoinR+R RORPtFalseRK( R)tattrR?tformatRTtone_value_per_lineR=tlRLtline_lents_indentRR((R)s./usr/lib/python2.6/site-packages/ipalib/cli.pytprint_attributes>'+0'   tdncsttptttptt|ttfptfd}x0|D](jo|=qpqpWxtD]|qWdS(s+ Print an ldap entry dict. csYjo(iddn!idddS(NRTRa(Re(ta(RTR_tattr_mapR)Ratentry(s./usr/lib/python2.6/site-packages/ipalib/cli.pyt print_attrEs  "N(RtdictRR1R2tsorted(R)RiRTRht attr_orderRaRj((RTR_RhR)RaRis./usr/lib/python2.6/site-packages/ipalib/cli.pyt print_entry1<s    c Csmt|ttfptt}xC|D];} |p dGHnt}|i| ||||||q*WdS(Nt(RR1R2RtTrueR^t print_entry( R)tentriestordertlabelstflagst print_allR`RTtfirstRi((s./usr/lib/python2.6/site-packages/ipalib/cli.pyt print_entriesVs c CsDt|ttfot|}nt|tpt|djot}t}nt}|dj ox||D]p} | |joqwn|i| | } |i| g} || } d| jo | ddgdgjoqwnt| toat i | djoqwn|i || df||i | |||||d|dnt| ttfoTt d| Do=|i| d|||i| ||||||dn|i| | ||||| =qwWn|oJxGt|D]5} |i| | } |i| || |||qWndS( s tsuppress_emptyuRoiRTicss"x|]}t|tVqWdS(N(RRk(R-tval((s./usr/lib/python2.6/site-packages/ipalib/cli.pys }s N(RR1R2RkRR(RpR^tgettfrontendt entry_countRURqtallReRxRl( R)RiRsRtRuRvR`RTRaRVtlabeltflagR?((s./usr/lib/python2.6/site-packages/ipalib/cli.pyRq_sL      & iRcCst|tptt|djpt|t|}|o|i||n|i|||o|i||ndS(s Print a string with a dashed line above and/or below. For example: >>> ui = textui() >>> ui.print_dashed('Dashed above and below.') ----------------------- Dashed above and below. ----------------------- >>> ui.print_dashed('Only dashed below.', above=False) Only dashed below. ------------------ >>> ui.print_dashed('Only dashed above.', below=False) ------------------ Only dashed above. iN(RR[RR,RU(R)RJtabovetbelowRTtdashtdashes((s./usr/lib/python2.6/site-packages/ipalib/cli.pyt print_dashedscCs|i|dddddS(s Print a primary header at indentation level 0. For example: >>> ui = textui() >>> ui.print_h1('A primary header') ================ A primary header ================ RTiRt=N(R(R)RL((s./usr/lib/python2.6/site-packages/ipalib/cli.pytprint_h1s cCs|i|dddddS(s Print a secondary header at indentation level 1. For example: >>> ui = textui() >>> ui.print_h2('A secondary header') ------------------ A secondary header ------------------ RTiRRN(R(R)RL((s./usr/lib/python2.6/site-packages/ipalib/cli.pytprint_h2s cCs|idt|dS(sb Print a command name. The typical use for this is to mark the start of output from a command. For example, a hypothetical ``show_status`` command would output something like this: >>> ui = textui() >>> ui.print_name('show_status') ------------ show-status: ------------ s%s:N(RR(R)R((s./usr/lib/python2.6/site-packages/ipalib/cli.pyt print_namescCs|i||dS(N(R(R)tmsgtoutput((s./usr/lib/python2.6/site-packages/ipalib/cli.pyt print_headerscCs|i|dS(s Print a summary at the end of a comand's output. For example: >>> ui = textui() >>> ui.print_summary('Added user "jdoe"') ----------------- Added user "jdoe" ----------------- N(R(R)R((s./usr/lib/python2.6/site-packages/ipalib/cli.pyt print_summarys cCsft|tj o3t|tttfjptt|}n|i|i|||dS(s Print a summary count. The typical use for this is to print the number of items returned by a command, especially when this return count can vary. This preferably should be used as a summary and should be the final text a command outputs. For example: >>> ui = textui() >>> ui.print_count(1, '%d goose', '%d geese') ------- 1 goose ------- >>> ui.print_count(['Don', 'Sue'], 'Found %d user', 'Found %d users') ------------- Found 2 users ------------- If ``count`` is not an integer, it must be a list or tuple, and then ``len(count)`` is used as the count. N( R0tintR1R2RkRR,RRF(R)tcountRDRE((s./usr/lib/python2.6/site-packages/ipalib/cli.pyt print_counts #cCsdt|GHdS(Ns ** %s **(R@(R)RL((s./usr/lib/python2.6/site-packages/ipalib/cli.pyt print_errorscCsPy |i||i|SWn)ttfj oHtd|nXdS(sPrompt user for input Handles encoding the prompt and decoding the input. On end of stream or ctrl+c, raise PromptFailed. RN(R<RAtKeyboardInterrupttEOFErrorR(R)tpromptRt prompt_func((s./usr/lib/python2.6/site-packages/ipalib/cli.pyt prompt_helpers  cCs|id||fdS(Ns >>> %s: %s(RK(R)t attributeterror((s./usr/lib/python2.6/site-packages/ipalib/cli.pytprint_prompt_attribute_errorscCsZ|od|}n d|}|djod|}nd||f}|i||S(s( Prompt user for input. u[%s]u%su%s: u %s [%s]: N(R(R(R)Rtdefaultt get_valuestoptionalR((s./usr/lib/python2.6/site-packages/ipalib/cli.pyRs  cCsd }|d j o|o d}q.d}n|od||f}n d|}xgto_|i||i}|d jotS|d jotS|d j o|d jo|SqVWd S( s Prompt user for yes/no input. This method returns True/False according to user response. Parameter "default" should be True, False or None If Default parameter is not None, user can enter an empty input instead of Yes/No answer. Value passed to Default is returned in that case. If Default parameter is None, user is asked for Yes/No answer until a correct answer is provided. Answer is then returned. tYestNou%s Yes/No (default %s): u %s Yes/No: uyesuyununouN(uyesuy(ununo(R(RpRtlowerR^(R)RRtdefault_promptRtdata((s./usr/lib/python2.6/site-packages/ipalib/cli.pyt prompt_yesno&s"      cCstiiodt|}ttdtd|}xtok|i||dti}|p|S|i||dti}||jo|S|i tdqBWn|i tii i SdS(sx Prompt user for a password or read it in via stdin depending on whether there is a tty or not. u%s: s!Enter %(label)s again to verify: RRsPasswords do not match!N( RR8RR@RRkRpRtgetpassRR<treadlineRQ(R)RtconfirmRt repeat_prompttpw1tpw2((s./usr/lib/python2.6/site-packages/ipalib/cli.pytprompt_passwordJs c Cs|ii ptii odSt|}|djotdtdnd}x`|D]X}h}x$|D]} |i| d|| R<RARFRIRKRNRSRURWRpReRnRxRqRRRRRRRRt raw_inputRRR^RRRR(((s./usr/lib/python2.6/site-packages/ipalib/cli.pyRKs@          8   /         $ thelpcBskeZdZedfZeZdZdZdZ dZ dZ dZ dZ d ZRS( s. Display help for a command or topic. scommand?sipalib.pluginscCs#|tjodS|iddS(sp Return last part of ``module`` name, or ``None`` if module is this file. For example: Nt.i(Rtsplit(R)tmodule((s./usr/lib/python2.6/site-packages/ipalib/cli.pyt_get_command_modules cCseti|pt|nti|}t|dd}|djo|i|df}n|S(Nttopic(Rtmodulest __import__R9R(R(R)t module_nameRR((s./usr/lib/python2.6/site-packages/ipalib/cli.pyt_get_module_topics  cCs8t|i|dt|f}||i|ds (Rt _builtinstCommandtNO_CLIRRR(tappendt_PLUGIN_BASE_MODULER@RRRRRQRR4R,RtrsplitRtkeyst_mtltsuperRt _on_finalize( R)tcRRtmtdocRRt_[1]((s./usr/lib/python2.6/site-packages/ipalib/cli.pyRsD     )&,$!.9DcCst|}d|i|f}|djp |djo|idS||ijo|i|n2||ijoq|i|}|iotd|nt t dt t |i i GH|i ii|in|tijo|i|n|djostd|iD}xf|iD]H}|i|}|ioq;ndt|ii||ifGHq;Wntd|dS(Ns%s.%sttopicsRs Purpose: %stcommandscssx|]}t|VqWdS(N(R,(R-R((s./usr/lib/python2.6/site-packages/ipalib/cli.pys s s%s %s(RRR(t print_topicsRtprint_commandsRRRR@RRRQtBackendtclit build_parsert print_helpRRR4RRtljusttsummary(R)RVRRtcmdRtcname((s./usr/lib/python2.6/site-packages/ipalib/cli.pytruns0    *    ,cCst|ii}ttdGHdGHttdGHxI|iD]>}ttdGHdt|ii|i |i fGHqFWdGHttdGHx@|D]8}|i|}dt|i|i |dfGHqWdGHttdGHdS( Ns'Usage: ipa [global-options] COMMAND ...RosBuilt-in commands:sHelp subtopics:s %s %ss Help topics:is.Try `ipa --help` for a list of global options.( RlRRR@RRRRRRR(R)RRttR((s./usr/lib/python2.6/site-packages/ipalib/cli.pyR s  + )c Cs3||ijot|i|dtjohx|i|dD]N}|i|d|d}|i|d}dt|i||fGHq@Wn||ijo&|i|d}|i|d}ng}x|iD]}}t|i|dtj oqn||i|djoqn|i|d|d}|i|d|d}PqWd|i|f}ttti |i pdi }||i jo&t |djotd|n|GH|oOdGHttdGHx6|D]*}dt|ii||ifGHqWnd GHdS( Niiis %s %ss%s.%sRoRsTopic commands:s (RR0RkRRRR@RRRRRQRR,RRR( R)RtsubtopicRRRRRR((s./usr/lib/python2.6/site-packages/ipalib/cli.pyRs>.& )#,(RRRRt takes_argsR2t has_outputRRRRRRRR(((s./usr/lib/python2.6/site-packages/ipalib/cli.pyRs   6  t show_mappingscBs;eZdZeddedfZeZdZRS(sA Show mapping of LDAP attributes to command-line option. t command_nameRs Command namecCst|}||ijotd|n|i|i}d d g}t|dd}xc|D]X}|iod|ijoqin|i|i|ift |t|i}qiWx2|D]*}t |di |d|d GHqWdS( NRt ParametersLDAP attributes =========s==============itwebuis : i(RsLDAP attribute(s =========s==============( RRRtoptionsR,texcludeRRt param_specR4RR(R)RtparamstoutRtparamtitem((s./usr/lib/python2.6/site-packages/ipalib/cli.pyRKs    ( RRRRRRR2RR(((s./usr/lib/python2.6/site-packages/ipalib/cli.pyR@s   tconsolecBs eZdZeZdZRS(s)Start the IPA interactive Python console.cCs#tiddtd|idS(Ns'(Custom IPA interactive Python console)tlocaltapi(tcodetinteractRkR(R)((s./usr/lib/python2.6/site-packages/ipalib/cli.pyRas(RRRR2RR(((s./usr/lib/python2.6/site-packages/ipalib/cli.pyR\s tshow_apicBs2eZdZdZdZdZddZRS(s%Show attributes on dynamic API objects namespaces*c CsK|djot|i}n;x1|D])}||ijotd|q'q'W|}|i|}td|D}|iiidt }xl|D]d}|ddjo| o dGHn|o t }ndd|d|di ||d fGHqWt |djo d }nd t |}|iii |dS( NRcss#x|]}t|dVqWdS(iN(R,(R-Rb((s./usr/lib/python2.6/site-packages/ipalib/cli.pys vs RiRos%s%s %rRYiis1 attribute shown.s%d attributes show.(R(R2RRt_show_api__traverseR4RRRRpR^RR,R( R)t namespacestnamesRtlinestmlRwRRR((s./usr/lib/python2.6/site-packages/ipalib/cli.pyRms2     cCs?g}x2|D]*}|i|}|id|||q W|S(Ns%s(Rt_show_api__traverse_namespace(R)RRRt namespace((s./usr/lib/python2.6/site-packages/ipalib/cli.pyt __traverses  ic Cs|i|||fx|D]}||}|i|d||ft|dpqnx\|D]T}||}t|tio1t|djo|i||||dqeqeWqWdS(Nit__iter__ii(RthasattrRtplugablet NameSpaceR,R( R)RRRttabt member_nametmemberRCR_((s./usr/lib/python2.6/site-packages/ipalib/cli.pyt__traverse_namespaces  &(s namespaces*(RRRRRRR(((s./usr/lib/python2.6/site-packages/ipalib/cli.pyRhs   t CollectorcBs#eZdZdZdZRS(cCsti|dhdS(Nt_Collector__options(tobjectt __setattr__(R)((s./usr/lib/python2.6/site-packages/ipalib/cli.pyt__init__scCsu||ijoA|i|}t|tjo||f}qQ||f}n||i|1s c Csetddi|i|}h}x|iD]}td|idt|i}d|ijoq4n|i o|i i od|dRRpRWRKR.R0(((s./usr/lib/python2.6/site-packages/ipalib/cli.pyRs    2  5cCs`d}y|idd\}}xtD]}|i|q(W|i|id|ijo tnti |i i i |Wnt j odGH|iidn[tj o}|}nAtj o4}|iid|iit|t}nX|dj o>t|tpt|ii|iti |indS(NtcontextRt config_loadedRosoperation aborteds%s: %s(R(tbootstrap_with_global_optionst cli_pluginstregistert load_pluginstfinalizeRRRtexitRRRRtlogtinfoRt StandardErrort exceptionRORRRRRRtstrerrortrval(RRRR*tklassR((s./usr/lib/python2.6/site-packages/ipalib/cli.pyRs.     " (>RtreRORRRRtsocketR R"R$RGtdefault_encoding_utf8t ImportErrorR|tbackendRtutilterrorsRRRRRRRRRR t constantsR t parametersR R R RRRLRtipapython.versionRRRRRR3RRRRRtcli_application_commandsR R tIndentedHelpFormatterRR Rt ExecutionerRRtR(((s./usr/lib/python2.6/site-packages/ipalib/cli.pyts\               F( M 6 $