From 973f606f3f82119680f5844f6d7e5e93736d8b5f Mon Sep 17 00:00:00 2001 From: Griatch Date: Sun, 24 Apr 2011 16:30:43 +0000 Subject: [PATCH] Added the functionality to let the @set command accept simple lists and dicts (relevant for @batch building) --- src/commands/default/building.py | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/commands/default/building.py b/src/commands/default/building.py index e55fd1e98..2dcf73257 100644 --- a/src/commands/default/building.py +++ b/src/commands/default/building.py @@ -1112,7 +1112,10 @@ class CmdSetAttribute(ObjManipCommand): Sets attributes on objects. The second form clears a previously set attribute while the last form inspects the current value of the attribute - (if any). + (if any). You can also set lists [...] and dicts {...} + on attributes with @set (but not nested combinations). Also + note that such lists/dicts will always hold strings (never numbers). + Use @py if you need to set arbitrary lists and dicts. """ key = "@set" @@ -1156,6 +1159,18 @@ class CmdSetAttribute(ObjManipCommand): string += "\n%s has no attribute '%s'." % (obj.name, attr) else: # setting attribute(s) + + # analyze if we are trying to set a list or a dict. + if value.startswith('[') and value.endswith(']'): + value = value.lstrip('[').rstrip(']').split(',') + value = [utils.to_str(val) for val in value] + elif value.startswith('{') and value.endswith('}') and ':' in value: + dictpairs = value.lstrip('{').rstrip('}').split(',') + try: + value = dict([[utils.to_str(p.strip()) for p in pair.split(':')] for pair in dictpairs]) + except Exception: + pass + for attr in attrs: obj.set_attribute(attr, value) string += "\nCreated attribute %s/%s = %s" % (obj.name, attr, value)