#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|iddS(sl Takes a Python identifier and transforms it into form suitable for the Command Line Interface. Rt-(treplace(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(tstrR(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 cCs%t|dddjodS|iS(NtencodingsUTF-8(tgetattrR&R5(R'tstream((s./usr/lib/python2.6/site-packages/ipalib/cli.pyt__get_encoding|scskt|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 (R.Rt_textui__get_encodingRtstdinR9R/R0(R'tvalueR5((R's./usr/lib/python2.6/site-packages/ipalib/cli.pyR9s cCs|iti}|i|S(s3 Encode text for output to stdout. (R;RRtencode(R't unicode_textR5((s./usr/lib/python2.6/site-packages/ipalib/cli.pyR>scCs+|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(R.Rtbase64t b64encode(R'R=((s./usr/lib/python2.6/site-packages/ipalib/cli.pyt encode_binaryscCst|GHdS(s? Print exactly like ``print`` statement would. N(tunicode(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*RG(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'RJRKtline((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'RJtindent((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(RSRF(R'R3RRtkeyR=((s./usr/lib/python2.6/site-packages/ipalib/cli.pyt print_keyvals s%s: %sc st|ttfp'i||i|f|n|o8x|D])}i||i|f|qKWntfd|}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((RF(R:(R'(s./usr/lib/python2.6/site-packages/ipalib/cli.pytsis: Ns, s%s%st itbreak_long_wordsui(t isinstanceR/R0RSRFtmapR*R.tjoinR)R RMRNtFalseRI( R'tattrR=tformatRRtone_value_per_lineR:tlRJtline_lents_indentRP((R's./usr/lib/python2.6/site-packages/ipalib/cli.pytprint_attributes<'+0'   tdncsvfd}x0|D](jo|=q%q%WxtD]|q^WdS(s+ Print an ldap entry dict. csYjo(iddn!idddS(NRRR_(Rc(ta(RRR]tattr_mapR'R_tentry(s./usr/lib/python2.6/site-packages/ipalib/cli.pyt print_attrEs  "N(tsorted(R'RgRRRft attr_orderR_Rh((RRR]RfR'R_Rgs./usr/lib/python2.6/site-packages/ipalib/cli.pyt print_entry1<s     c CsPt}xC|D];} |p dGHnt}|i| ||||||q WdS(Nt(tTrueR\t print_entry( R'tentriestordertlabelstflagst print_allR^RRtfirstRg((s./usr/lib/python2.6/site-packages/ipalib/cli.pyt print_entriesVs c Cs-t|ttfot|}n|djot}t}nt}|dj ox||D]p} | |joq`n|i| | } |i| g} || } d| jo | ddgdgjoq`nt| toati | djoq`n|i || df||i | |||||d|dnt| ttfoTt d| Do=|i | d|||i| ||||||dn|i | | ||||| =q`Wn|oJxGt|D]5} |i| | } |i | || |||qWndS( s tsuppress_emptyuRliRRicss"x|]}t|tVqWdS(N(RYtdict(R+tval((s./usr/lib/python2.6/site-packages/ipalib/cli.pys }s N(RYR/R0RwR&RmR\tgettfrontendt entry_countRSRntallRcRuRi( R'RgRpRqRrRsR^RRR_RTtlabeltflagR=((s./usr/lib/python2.6/site-packages/ipalib/cli.pyRn_sJ      & iRcCsZ|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. N(R*RS(R'RHtabovetbelowRRtdashtdashes((s./usr/lib/python2.6/site-packages/ipalib/cli.pyt print_dasheds cCs|i|dddddS(s Print a primary header at indentation level 0. For example: >>> ui = textui() >>> ui.print_h1('A primary header') ================ A primary header ================ RRiRt=N(R(R'RJ((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 ------------------ RRiRRN(R(R'RJ((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 cCsCt|tj ot|}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(R.tintR*RRC(R'tcountRARB((s./usr/lib/python2.6/site-packages/ipalib/cli.pyt print_countscCsdt|GHdS(Ns ** %s **(RG(R'RJ((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(R9R>tKeyboardInterrupttEOFErrorR(R'tpromptR}t prompt_func((s./usr/lib/python2.6/site-packages/ipalib/cli.pyt prompt_helpers  cCs|id||fdS(Ns >>> %s: %s(RI(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'R}tdefaultt 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&RmRtlowerR\(R'R}Rtdefault_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: R}RsPasswords do not match!N( RR<RRGRRwRmRtgetpassRR9treadlineRO(R'R}tconfirmRt 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|| RCRFRIRLRQRSRURmRcRkRuRnRRRRRRRRt 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__R6R&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_MODULERGRRRRRORR2R*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_commandsRRRRGRRROtBackendtclit build_parsert print_helpRRR2RRtljusttsummary(R'RTRRtcmdRtcname((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 ...RlsBuilt-in commands:sHelp subtopics:s %s %ss Help topics:is.Try `ipa --help` for a list of global options.( RiRRRGRRRRRRR(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.%sRlRsTopic commands:s (RR.RwRRRRGRRRRRORR*RRR( R'RtsubtopicRRRRRR((s./usr/lib/python2.6/site-packages/ipalib/cli.pyRs>.& )#,(RRRRt takes_argsR0t 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_nameR}s 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_specR2RR(R'RtparamstoutRtparamtitem((s./usr/lib/python2.6/site-packages/ipalib/cli.pyRKs    ( RRRRRRR0RR(((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(tcodetinteractRwR(R'((s./usr/lib/python2.6/site-packages/ipalib/cli.pyRas(RRRR0RR(((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+R`((s./usr/lib/python2.6/site-packages/ipalib/cli.pys vs RiRls%s%s %rRWiis1 attribute shown.s%d attributes show.(R&R0RRt_show_api__traverseR2RRRRmR\RR*R( R't namespacestnamesRtlinestmlRtRPR((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(RthasattrRYtplugablet NameSpaceR*R( R'RRRttabt member_nametmemberR@R]((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|RtreRMRRRRtsocketRR R"RDtdefault_encoding_utf8t ImportErrorRztbackendRtutilterrorsRRRRRRRRRR t constantsR t parametersR R R RRRJRtipapython.versionRRRRRR1RRRRRtcli_application_commandsR R tIndentedHelpFormatterRRRt ExecutionerRRrR(((s./usr/lib/python2.6/site-packages/ipalib/cli.pyts\               F( M 6 $