dojango icon indicating copy to clipboard operation
dojango copied to clipboard

24 - extract_nodelist_options only allows key = val to span a single line

Open klipstein opened this issue 13 years ago • 0 comments

Since the current extract function only allowed arguments to go on a single
line I wrote this to allow multiline arguments.

def extract_multiline_nodelist_options(nodelist, context=None):
    """
    Returns a dict containing the key=value options listed within the nodelist.
    The value can be a python dict, list, tuple, or number/string literal.
    """
    ret={}
    if context:
        rendered = nodelist.render(context)
    else:
        rendered = nodelist.render({})
    if rendered.find("=") > 0:
        opts = rendered.split("\n")
        for i in range(len(opts)):
           while i+1 < len(opts) and opts[i+1].find("=") == -1:
              opts[i] += opts.pop(i+1)
        for key, val in [opt.strip().split("=") for opt in opts if opt != ""]:
           ret[key.strip()]=eval(val.strip())
    return ret 

Original link: http://code.google.com/p/dojango/issues/detail?id=24

klipstein avatar Nov 06 '11 12:11 klipstein