<s> from __future__ import absolute_import , division <EOL> from twisted . python . compat import intToBytes <EOL> def parse ( s ) : <EOL> s = s . strip ( ) <EOL> expr = [ ] <EOL> while s : <EOL> if s [ <NUM_LIT:0> : <NUM_LIT:1> ] == b'<STR_LIT:(>' : <EOL> newSexp = [ ] <EOL> if expr : <EOL> expr [ - <NUM_LIT:1> ] . append ( newSexp ) <EOL> expr . append ( newSexp ) <EOL> s = s [ <NUM_LIT:1> : ] <EOL> continue <EOL> if s [ <NUM_LIT:0> : <NUM_LIT:1> ] == b'<STR_LIT:)>' : <EOL> aList = expr . pop ( ) <EOL> s = s [ <NUM_LIT:1> : ] <EOL> if not expr : <EOL> assert not s <EOL> return aList <EOL> continue <EOL> i = <NUM_LIT:0> <EOL> while s [ i : i + <NUM_LIT:1> ] . isdigit ( ) : i += <NUM_LIT:1> <EOL> assert i <EOL> length = int ( s [ : i ] ) <EOL> data = s [ i + <NUM_LIT:1> : i + <NUM_LIT:1> + length ] <EOL> expr [ - <NUM_LIT:1> ] . append ( data ) <EOL> s = s [ i + <NUM_LIT:1> + length : ] <EOL> assert <NUM_LIT:0> , "<STR_LIT>" <EOL> def pack ( sexp ) : <EOL> s = b"<STR_LIT>" <EOL> for o in sexp : <EOL> if type ( o ) in ( type ( ( ) ) , type ( [ ] ) ) : <EOL> s += b'<STR_LIT:(>' <EOL> s += pack ( o ) <EOL> s += b'<STR_LIT:)>' <EOL> else : <EOL> s += intToBytes ( len ( o ) ) + b"<STR_LIT::>" + o <EOL> return s </s>
