afmontanar / dojango

Automatically exported from code.google.com/p/dojango
0 stars 0 forks source link

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

Open GoogleCodeExporter opened 9 years ago

GoogleCodeExporter commented 9 years ago
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 issue reported on code.google.com by mtthw.me...@gmail.com on 4 Aug 2009 at 2:41

GoogleCodeExporter commented 9 years ago
Totally makes sense passingg multiline options. Have to test it before 
commiting it.

Original comment by tobias.k...@googlemail.com on 18 Aug 2009 at 10:22