o @&atD@sdZddlZddlZddlmZeZGdddeZGdddeZ Gdd d e Z Gd d d e Z Gd d d eZ GdddeZ GdddeZGdddeZdS)aModule for parsing shorthand syntax. This module parses any CLI options that use a "shorthand" syntax:: --foo A=b,C=d |------| | Shorthand syntax This module provides two main classes to do this. First, there's a ``ShorthandParser`` class. This class works on a purely syntactic level. It looks only at the string value provided to it in order to figure out how the string should be parsed. However, because there was a pre-existing shorthand parser, we need to remain backwards compatible with the previous parser. One of the things the previous parser did was use the associated JSON model to control how the expression was parsed. In order to accommodate this a post processing class is provided that takes the parsed values from the ``ShorthandParser`` as well as the corresponding JSON model for the CLI argument and makes any adjustments necessary to maintain backwards compatibility. This is done in the ``BackCompatVisitor`` class. N)is_document_typec@seZdZddZddZdS) _NamedRegexcCs||_t|tj|_dSN)namerecompileUNICODEregex)selfr regex_strr 2/usr/lib/python3/dist-packages/awscli/shorthand.py__init__3sz_NamedRegex.__init__cCs |j|Sr)r matchr valuer r r r7s z_NamedRegex.matchN)__name__ __module__ __qualname__rrr r r r r2s rc@seZdZddZdS)ShorthandParseErrorcCs|jd|j}}}d|jd|jvr&|jd|jd}|j|d}d|j|jdvrL|j|j|jdd}|jd|}|j|d}d|d|d|fS)N z%s %s%s ^)rindexrindex)r consumed remaining num_spaces last_newline next_newliner r r _error_location=sz#ShorthandParseError._error_locationN)rrrr"r r r r r;s rc$eZdZfddZddZZS)ShorthandParseSyntaxErrorcs4||_||_||_||_|}tt||dSr)rexpectedactualr_construct_msgsuperr$r)r rr%r&rmsg __class__r r rUs z"ShorthandParseSyntaxError.__init__cCsd|j|j|f}|S)Nz,Expected: '%s', received: '%s' for input: %s)r%r&r"r r)r r r r']sz(ShorthandParseSyntaxError._construct_msgrrrrr' __classcell__r r r*r r$Ts r$cr#)DuplicateKeyInObjectErrorcs.||_||_||_|}tt||dSr)keyrrr'r(r/r)r r0rrr)r*r r rfs z"DuplicateKeyInObjectError.__init__cCsd|j|f}|S)NzxSecond instance of key "%s" encountered for input: %s This is often because there is a preceding "," instead of a space.)r0r"r,r r r r'ms z(DuplicateKeyInObjectError._construct_msgr-r r r*r r/es r/c@s eZdZdS)DocumentTypesNotSupportedErrorN)rrrr r r r r1vsr1c@seZdZdZeddZeddZdZdZdZ d Z ed d j e eed Z ed d j e ee d Z ddZddZddZddZddZddZddZddZddZd d!Zd"d#Zd$d%Zd&d'Zd>d)d*Zd+d,Zd-d.Zd?d0d1Zd2d3Zd4d5Z d6d7Z!d8d9Z"d:d;Z#d-Z\\-z|-￿u\s\!\#-&\(-\+\--\\\^-\|~-￿u\s\!\#-&\(-\+\--\<\>-￿z(\\,)firstzC({escaped_comma}|[{start_word}])({escaped_comma}|[{follow_chars}])*) escaped_comma start_word follow_charssecondcCs g|_dSr)_tokensr r r r rs zShorthandParser.__init__cCs||_d|_|S)aSParse shorthand syntax. For example:: parser = ShorthandParser() parser.parse('a=b') # {'a': 'b'} parser.parse('a=b,c') # {'a': ['b', 'c']} :type value: str :param value: Any value that needs to be parsed. :return: Parsed value, which will be a dictionary. r) _input_value_index _parameterrr r r parseszShorthandParser.parsecCsi}|\}}|||<|j}|jt|jkr@|jddd|\}}||vr1t||j|d|||<|j}|jt|jks|SN,Tconsume_whitespacer)_keyvalr;lenr:_expectr/)r paramsr0val last_indexr r r r<s    zShorthandParser._parametercCs&|}|jddd|}||fS)N=Tr@)_keyrD_values)r r0valuesr r r rBszShorthandParser._keyvalcCsRtjtjd}|j}|s!||vrn |jd7_|r|j||jS)Nz-_.#/:r)string ascii_lettersdigitsr;_at_eof_currentr:)r valid_charsstartr r r rIs zShorthandParser._keycCs<|rdS|dkr|S|dkr|S|S)Nr[{)rOrP_explicit_list _hash_literal _csv_valuer9r r r rJs  zShorthandParser._valuescCs|}||s|j|jdkr|S|jddd|g} z!|}||r5||Wn"|jddd||WntyU|rN| dYnwq!t |dkr_|S|Sr>) _first_value_consume_whitespacerOr:r;rD _second_valueappendr$ _backtrack_torC)r first_valuecsv_listcurrentr r r rWs2     zShorthandParser._csv_valuecCs>|j|j|jd}|dur||}|ddSdS)N\,r?r) _FIRST_VALUErr:r;_consume_matched_regexreplacerstrip)r resultrr r r _value s  zShorthandParser._valuecCsp|jdddg}|dkr1|}||||dkr+|d||dks|d|S)NrSTr@]r?)rDrP_explicit_valuesr[rY)r rKrFr r r rUs     zShorthandParser._explicit_listcC0|dkr |S|dkr|S|S)NrSrT)rPrUrVrXr9r r r rh"s  z ShorthandParser._explicit_valuescCs|jdddi}|dkr;|}|jddd|}||dkr1|d||||<|dks|d|S)NrTTr@}rHr?)rDrPrIrhrY)r keyvalsr0vr r r rV+s    zShorthandParser._hash_literalcCri)N'")rP_single_quoted_value_double_quoted_valuerfr9r r r rX:s  zShorthandParser._first_valuecC|j|jddS)Nrm escaped_char)_consume_quoted_SINGLE_QUOTEDr9r r r roBsz$ShorthandParser._single_quoted_valueNcCs:||dd}|dur|d||}|dd}|S)Nrz\%sz\\\)_must_consume_regexrc)r r rsrr r r rtHs  zShorthandParser._consume_quotedcCrq)Nrnrr)rt_DOUBLE_QUOTEDr9r r r rpOz$ShorthandParser._double_quoted_valuecCsD|dkr |S|dkr|S||j}|ddS)Nrmrnr`r?)rProrprx _SECOND_VALUErcrd)r rr r r rZRs   zShorthandParser._second_valueFcCsv|r||jt|jkrt|j|d|j|j|j}||kr*t|j|||j|jd7_|r9|dSdS)NEOFr)rYr;rCr:r$)r charrAr&r r r rD[s  zShorthandParser._expectcCs@||j|jd}|dur||St|jd|jd|j)Nz<%s>z)rr:r;rbr$r)r r rer r r rxis  z#ShorthandParser._must_consume_regexcCs<|\}}|j|j||j|}|j||7_|Sr)spanr:r;)r rerRendrlr r r rbps z&ShorthandParser._consume_matched_regexcCs |jt|jkr|j|jStSr)r;rCr:_EOFr9r r r rPvs zShorthandParser._currentcCs|jt|jkSr)r;rCr:r9r r r rO}rzzShorthandParser._at_eofcCsR|jdkr#|j|j|kr'|jd8_|jdkr%|j|j|ks dSdSdSdS)Nrr)r;r:)r r}r r r r\*zShorthandParser._backtrack_tocCsR|tkr#|tjvr'|jd7_|tkr%|tjvs dSdSdSdS)Nr)rPrrL whitespacer;r9r r r rYrz#ShorthandParser._consume_whitespacer)F)%rrr__doc__rrury _START_WORD_FIRST_FOLLOW_CHARS_SECOND_FOLLOW_CHARS_ESCAPED_COMMAformatrar{rr=r<rBrIrJrWrfrUrhrVrXrortrprZrDrxrbrPrOr\rYr r r r r2zsb     0    r2c@s<eZdZddZddZddZddZd d Zd d Zd S) ModelVisitorcCs|i|d|dS)Nr)_visit)r rEmodelr r r visitszModelVisitor.visitcCs&t|d|j|j}|||||dS)Nz _visit_%s)getattr type_name _visit_scalar)r parentshaperrmethodr r r rs zModelVisitor._visitc Cs<t|tsdS|jD]\}}||||||q dSr) isinstancedictmembersitemsrgetr rrrr member_name member_shaper r r _visit_structures  zModelVisitor._visit_structurecCs6t|tsdSt|D] \}}|||j||q dSr)rlist enumeratermember)r rrrrielementr r r _visit_lists zModelVisitor._visit_listcCs:t|tsdS|j}|D] \}}|||||qdSr)rrrrr)r rrrr value_shapekrlr r r _visit_maps zModelVisitor._visit_mapcCsdSrr r rrrrr r r rszModelVisitor._visit_scalarN) rrrrrrrrrr r r r rs rcs4eZdZddZfddZddZddZZS) BackCompatVisitorc Csf|||t|ts dS|jD]\}}z ||||||Wqty0td|wdS)NzwShorthand syntax does not support document types. Use JSON input for top-level argument to specify nested parameter: %s) _raise_if_document_type_foundrrrrrrr1rrr r r rs    z"BackCompatVisitor._visit_structurecs:t|ts|dur|g||<dSdStt|||||Sr)rrr(rrrr*r r rs  zBackCompatVisitor._visit_listcCs|durdS|j}|dvrt|||<dS|dvr!t|||<dS|dkr=|dkr1d||<dS|dkr?d||<dSdSdS)N)integerlong)doublefloatbooleantrueTfalseF)rintrlower)r rrrrrr r r rs    zBackCompatVisitor._visit_scalarcCs|dur t|r tdSdSr)rr1)r rrr r r rsz/BackCompatVisitor._raise_if_document_type_found)rrrrrrrr.r r r*r rs   r)rrrL awscli.utilsrobjectrr Exceptionrr$r/r1r2rrr r r r s   !