Patrick Williams | 12fc939 | 2021-08-06 09:16:53 -0500 | [diff] [blame] | 1 | def set_doappend(d, var, val, sep=' '): |
Brad Bishop | 9dc5671 | 2016-07-07 15:56:02 -0400 | [diff] [blame] | 2 | values = (d.getVar(var, True) or '').split(sep) |
| 3 | if filter(bool, values): |
| 4 | d.appendVar(var, '%s%s' %(sep, val)) |
| 5 | else: |
| 6 | d.setVar(var, val) |
| 7 | |
| 8 | |
| 9 | def listvar_to_list(d, list_var, sep=' '): |
Saqib Khan | dc2090d | 2017-03-07 07:05:56 -0600 | [diff] [blame] | 10 | return list(filter(bool, (d.getVar(list_var, True) or '').split(sep))) |
Brad Bishop | 167aaae | 2016-08-24 14:08:35 -0400 | [diff] [blame] | 11 | |
| 12 | |
| 13 | def compose_list(d, fmtvar, *listvars, **kw): |
| 14 | import itertools |
| 15 | fmt = d.getVar(fmtvar, True) |
| 16 | lists = [listvar_to_list(d, x) for x in listvars] |
| 17 | lst = [fmt.format(*x) for x in itertools.product(*lists)] |
| 18 | return (kw.get('sep') or ' ').join(lst) |
| 19 | |
| 20 | |
| 21 | def compose_list_zip(d, fmtvar, *listvars, **kw): |
| 22 | fmt = d.getVar(fmtvar, True) |
| 23 | lists = [listvar_to_list(d, x) for x in listvars] |
| 24 | lst = [fmt.format(*x) for x in zip(*lists)] |
| 25 | return (kw.get('sep') or ' ').join(lst) |