#!/usr/bin/env python
"""
common tools for parsers
"""

import sys
import simplejson

def netstring(x):
    """
        >>> netstring('banana')
        '6:banana,'
    """
    return str(len(x)) + ':' + x + ','

def jsonify(d):
    return simplejson.dumps(d, indent=2, sort_keys=True)

def export(generator):
    for item in generator:
        sys.stdout.write(netstring(jsonify(item)))

if __name__ == "__main__":
    import doctest
    doctest.testmod()

@@ -381,16 +381,230 @@
 tem)))%0A%0A
+def unexport(fh):%0A    n = '0'%0A    while n:%0A        n = int(n + ''.join(c for c in iter(lambda: fh.read(1), ':')))%0A        yield simplejson.loads(fh.read(n))%0A        assert fh.read(1) == ','%0A        n = fh.read(1)%0A%0A
 if __nam

add unexport function for njs format