3 =^&@sdZddlZdZZyeWn&ek rBeZGdddZYnXyddlm Z Wne k rle Z YnXej djZGdd d eZd d Zd d ZddZddZdddZd ddZe ddddfddZee fddZdS)!a rsonlite -- an extremely lightweight version of rson. Copyright (c) 2012, Patrick Maupin License :: MIT http://pypi.python.org/pypi/rsonlite http://code.google.com/p/rson/ rsonlite makes it easy to build a file parser for declarative hierarchical data structures using indentation. (Spaces only, tabs not considered indentation.) The only special characters are '#', '=', and indentation: - Indentation denotes a key/value relationship. The value is indented from the key. - = Denotes the start of a free-format string. These strings can contain '=' and '#' characters, and even be multi-line, but every line in the string must be indented past the initial equal sign. Note that, for multi-line strings, indentation is preserved but normalized such that at least one line starts in the left column. This allows for restructuredText or Python code to exist inside multi-line strings. - # Denotes the start of a line comment, when not inside a free-format string. The only Python objects resulting from parsing a file with rsonlite are: - strings: free-format strings (described above) can contain any character, but the whitespace before/after the string may be stripped. Regular strings must fit on a single line and cannot contain '=' or '#' characters. Regular strings may be used as keys in key/value pairs, but free-format strings may not. - tuple: A key/value pair is a two-element tuple. The key is always a string. The value is always a list. - list: The top level is a list, and the value element of every key/value pair tuple is also a list. Lists can contain strings and key/value pair tuples. Nz0.1.0c@s eZdZdS)unicodeN)__name__ __module__ __qualname__rrf/cygdrive/c/Jenkins/workspace/AmbiqSuite-Full/ambiqsuite-sdk/workspace/tools/bsp_generator/rsonlite.pyrDsr) OrderedDictz( *|=[^ ]*|#[^ ]*|[^ #=]+)c@s eZdZdZddZddZdS) RsonTokenz> A string that may be annotated with location information cCstj||}||_||_|S)N)str__new__linecol)clssr r selfrrrr Ts zRsonToken.__new__cCstt|||j|jS)N)r r r r )rotherrrr__add__YszRsonToken.__add__N)rrr__doc__r rrrrrr Qsr ccst|tr|jdd}nt|ts.|jdd}d|jddjdd}d}xFt|D]:}|jdrt|d7}t|}qTt |||V|t|7}qTWdS) z Convert string into (probable) tokens (some tokens may be recombined later, e.g. if they contain # or = but were already inside a string) zutf-8replace z  rN) isinstancerencode basestringdecodersplitter startswithlenr )sourcer tokr rrrgettoks]s     r!ccsZ|dj}xJ|D]B}x||jkr0dV|d7}qW|j|d|jV|d7}qWdS)zB Returns one string for each line, properly dedented. rr N)r r rstrip)lineinfodedentZlinenumr rrr multilinevs    r'cCs|j}|ddj}g}xJ|rd|dj|krd|j}|j|krR|d|7<q|j||j}qW|rtdd|D}|r|jdt||j|djt ||}t||j|jS) z& Returns a free-formatted string. rNcss|] }|jVqdS)N)r ).0r rrr sz getfreeformat..rrr*) r stripr popappendmininsertr joinr')toklistZfirsttokZfirstcolZcurlineZ firstpartr%r r&rrr getfreeformats   r2c Csdtt|}|jdg}g}d}|}x|rF|j}|jdrDq*|j}||kr~|j||f|}|g}}|d|f|d <x||kr|j\}}qW||krtd} d| _|j | _ | |jdr|jt |||q*|jt |j |j |j|r*|d j |j kr*|j}|jdr6|d t |||gf|d <q*|jds*tq*W|\}|dkr\gS|dS) a load a string into an rsonlite datastructure. If the source is not a string instance, then loads will attempt to convert it into a string instance, by encoding to UTF-8 on Python 2, or decoding from UTF-8 on Python 3. Nr#z3unindent does not match any outer indentation levelz =r*r*r*r*r*r*)listr!reverser,rr r-IndentationErrorfilenamer linenor2r r$AssertionError) rr1resultZstackZcurcolZcurlistr r ZoldlisterrrrrloadssB       r= r"cs@ddfddg}|j||ddj|S)z/ Dump a string loaded with loads back out. cSs&d|kr"d|jdg|jd}|S)Nrr")r0split)dataindent2rrr getstringszdumps..getstringc st|tstt|x|D]}t|tr|\}}t|dkrnt|dtrnd|||d|fqd||f||qt|tstd|ksd|ksd|krԈ|d||dqd||fqWdS) Nrrz %s%s = %sz%s%srr4r3z )rr5r:reprtuplerr)r@rAkeyvalue)r-rBindentrecurserrrHs  " zdumps..recurser"r)r-r0)r@rGinitial_indentr;r)r-rBrGrHrdumpss rJcsFfddg}|jd|dddj|S)z Pretty-print a string loaded by loads into something that makes it easy to see the actual structure of the data. The return value of this should be parseable by eval() cst|tstx|D]}t|ttfs*tt|trt|ddksXt|ddt rd|t|df|d|d|qd|t|fqWdS)Nrrz%s(%s, [z%s])z%s%s,)rr5r:rDrrrC)r@rA)r-rGrHrrrHs  $zpretty..recurse[]r"r)r-r0)r@rGr;r)r-rGrHrprettys  rMTF)truefalseZnullcCsb||kr||S|jdr^|jdr^|dd}xdD]}||kr6|Sq6Wdd|jdDS|S) z This gives an example of handling the JSON special identifiers true, false and null, and also of handling simple arrays. rKrLrz"'[]{} cSsg|] }|jqSr)r+)r(xrrr szstringparse..,r*)rendswithr?)rZspecialtZchrrr stringparses  rUcs*fddt|tr |nt|S)z Return the simplest structure that uses dicts instead of tuples, and doesn't lose any source information. Use ordered dicts if they are available. cst|dkr&t|dtr&|dSdd|D}|s<|Stt|t|krffdd|DSg}d}x^|D]V}t|ts|j|d}qt|\}}|dks||kr}|j||||<qtW|S)NrrcSsg|]}t|tr|dqS)r)rrD)r(rPrrrrQsz0simpleparse..recurse..c3s|]\}}||fVqdS)Nr)r(rPy)rHrrr)sz/simpleparse..recurse..)rrrsetrDr-)Zmylistkeysr;ZcurdictitemrErF)rHstddictrUrrrHs(    zsimpleparse..recurse)rr5r=)rrUrZr)rHrZrUr simpleparse sr[)r>r")r>)rreversion __version__r NameErrorr r collectionsrrZ ImportErrordictcompilefindallrr r!r'r2r=rJrMrUr[rrrr8s*    +