3 D\ 6@sdZddlZddlZddlZddlZddlmZmZmZm Z ddl m Z ddl Z ddl mZmZmZejeZGdddeZGdd d eZd d ZGd d d eZGdddeZdee jfddZddZdejefddZddZ ddZ!ddZ"Gdddej#Z$dS)a Copyright 2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. N) WHITESPACEWHITESPACE_STR BACKSLASH STRINGCHUNK) NUMBER_RE)str_node dict_node list_nodec@seZdZdZdS)DuplicateErrorz< Error thrown when the template contains duplicates N)__name__ __module__ __qualname____doc__rrq/private/var/folders/pf/wv4htv3x0qs2c2mp0dnn0kchsvlck3/T/pip-install-emcbgzcf/cfn-lint/cfnlint/decode/cfn_json.pyr sr c@seZdZdZdS) NullErrorz7 Error thrown when the template contains Nulls N)r r r rrrrrr$srcCsVti||}xD|D]<\}}|dkr0tdj|||krFtdj||||<qW|S)z Check for duplicate keys on the current level, this is not desirable because a dict does not support this. It overwrites it with the last occurance, which can give unexpected results Nz"{}")rrformatr )Z ordered_pairsbeg_markend_markmappingkeyvaluerrrcheck_duplicates*s  rc@s"eZdZdZdddZddZdS) JSONDecodeErroraSubclass of ValueError with the following additional properties: msg: The unformatted error message doc: The JSON document being parsed pos: The start index of doc where parsing failed lineno: The line corresponding to pos colno: The column corresponding to pos  c Cs|jdd|d}||jdd|}d||||f}tj||||_||_||_||_||_t j ||d||dt |dt j |d|_ dS)N rz%s: line %d column %d (char %d))message)countrfind ValueError__init__msgdocposlinenocolnocfnlintZMatchlen ParseErrormatch)selfr#r$r%rr&r'errmsgrrrr"Cs  zJSONDecodeError.__init__cCs|j|j|j|jffS)N) __class__r#r$r%)r,rrr __reduce__QszJSONDecodeError.__reduce__N)r)r r r rr"r/rrrrr:s rc@s eZdZdZdZdZddZdS)MarkzMark of line and columnrcCs||_||_dS)N)linecolumn)r,r1r2rrrr"Zsz Mark.__init__N)r r r rr1r2r"rrrrr0Usr0TcCsg}|j}|d}x|||}|dkr4td|||j}|j\} } | rT|| | dkr`Pn,| dkr|rdj| } t| |||| qy ||} Wn tk rtd||YnX| dkry || } Wn*tk rdj| } t| ||YnX|d7}nt||}|d 7}d |ko,d knr|||d d krt||d}d|koldknrd|d d>|dB}|d7}tj dkrt |} nt |} || qWdj ||fS)aScan the string s for a JSON string. End is the index of the character in s after the quote that started the JSON string. Unescapes all valid JSON string escape sequences and raises ValueError on attempt to decode an invalid string. If strict is False then literal control characters are allowed in the string. Returns a tuple of the decoded string and the index of the character in s after the end quote.rNzUnterminated string starting at"\z"Invalid control character {0!r} atuzInvalid \escape: {0!r}iiz\uiii rr)r:r) appendrendgroupsr IndexErrorKeyError _decode_uXXXXsys version_infochrunichrjoin)sr<strict_b_mchunks_appendbeginchunkcontent terminatorr#esccharuniuni2rrr py_scanstringasT            2   rTc Cs`||d|d}t|dkrL|ddkrLy t|dStk rJYnXd}t|||dS)Nrr6xXzInvalid \uXXXX escape)r)intr!r)rFr%rPr#rrrr@s r@c1CsB|\}} | } g} | j} |dkr"i}|j} || | d}|dkrb||krh||| j} || | d}|dkrL|dk ry,t|| | d\}}|| ||}|| dfStk r}ztdj||| WYdd}~Xn6tk r}ztdj||| WYdd}~XnXi} |dk r@t|| | d\}}|| ||} | | dfS|dkrbtd|| | d7} x| d}t|| |\}} | ||}|| | ddkr||| j} || | ddkrtd || | d7} y:|| |kr| d7} || |kr||| dj} Wnt k r.YnXt|||t |\}}y||| \}} Wn4t k r}ztd |t |WYdd}~XnXt |||}| ||fy0|| }||kr||| dj} || }Wnt k rd }YnX| d7} |dkr Pn|d kr$td || d||| j} || | d}| d7} |dkrntd|| dqnW|dk r y t|| | \}}|| ||}Wnntk r}ztdj||||WYdd}~Xn8tk r}ztdj||||WYdd}~XnX|| fSt| } |dk r:t|| | \}}|| ||} | | fS)z? Custom Cfn JSON Object to store keys with start and end times Nrr3}zDuplicate found {}z Null Error {}z1Expecting property name enclosed in double quotes:zExpecting ':' delimiterzExpecting valuer,zExpecting ',' delimiter)r; setdefaultr<get_beg_end_markr rrrrTr>r) StopIterationstrrdict) s_and_endrG scan_once object_hookobject_pairs_hookmemo_w_wsrFr<Z orginal_endpairs pairs_appendmemo_getnextcharrrresulterrrLrrZkey_strrrr CfnJSONObjects     "$       "         $&  rnc sx|j |j|j tj|j |j|j|j|j |j |j  f ddfdd S)ze Make python based scanner For this use case we will not use the C based scanner csy ||}Wntk r(t|YnX|dkrB ||d S|dkrd ||df S|dkr~||dfS|dkr|||ddkrd|dfS|d kr|||dd krd |dfS|d kr|||d dkrd|d fS||}|dk rV|j\}}}|s$|rB||p0d|p:d}n|}||jfS|dkr|||ddkrd|dfS|dkr|||ddkrd|dfS|dkr|||ddkrd|dfSt|dS)z Scan once internal function r3r{[nrUnullNttrueTfr6falseFrNr:NaNIInfinity- z -Infinity)r>r^r=r<)stringidxrkmintegerfracexpres) _scan_once match_numberrercrd parse_arrayparse_constant parse_float parse_int parse_object parse_stringrbrGrrr#s@             z#py_make_scanner.._scan_oncec sz ||SjXdS)z Scan OnceN)clear)r~r)rrerrrbJs z"py_make_scanner..scan_once) rrrrr+rGrrrrcrdre)contextr) rrrercrdrrrrrrrbrGrpy_make_scanners$'rc Cs\|jdd|}||jdd|}t||}|jdd|}||jdd|}t||}||fS)zGet the Start and End Mark rr)rr r0) rFstartr<Z beg_linenoZ beg_colnorZ end_linenoZ end_colnorrrrr]Ts  r]c CsXd}tjjs.x:tj|dD] }||}qWnt|}|j}WdQRXtj|t dS)z" Load the given JSON file r)filesN)cls) rAstdinisatty fileinputinputopenreadjsonloadsCfnJSONDecoder)filenamerNr1fprrrload_s  rc@s eZdZdZddZddZdS)rz Converts a json string, where datetime and timedelta objects were converted into strings using the DateTimeAwareJSONEncoder, into a python object. cOsBtjj|f||t|_|j|_t|_i|_ t |_ t ||_ dS)N)r JSONDecoderr"rnr JSONArrayrrTrrerrdrrb)r,argskwargsrrrr"tszCfnJSONDecoder.__init__c Ks>tjj||f|\}}|\}}t|||\}} t||| |fS)z- Convert JSON array to be a list_node object )rdecoderrr]r ) r,rarbrvaluesr<rFrrrrrrr}szCfnJSONDecoder.JSONArrayN)r r r rr"rrrrrros r)%rrrAloggingrZ json.decoderrrrrZ json.scannerrr(Zcfnlint.decode.noderrr getLoggerr LOGGER Exceptionr rrr!robjectr0r+rTr@rnrr]rrrrrrrs,   ; gD