def MageGroupFromString(line): """Returns a new MageGroup, created from a string representation""" result = MageGroup([],RecessiveOn=False) trans = {'off':('Off',True),\ 'recessiveon':('RecessiveOn',True),\ 'color':'Color',\ 'radius':'Radius',\ 'nobutton':('NoButton',True),\ 'dominant':('Dominant',True),\ 'lens':('Lens',True), 'master':'Master',\ 'instance':'Instance',\ 'clone':'Clone'} #extract all delimited fields: label & KeyWordOptions (master, etc) delimited_fields = [] while 1: part = extract_delimited(line,'{','}') if part is not None: delimited_fields.append(part) line = line.replace('{'+part+'}','') else: break #the first one is always the label label = delimited_fields[0] #the later ones (starting with 1) are keyword options field_idx = 1 #gather all left-over pieces pieces = line.split() if 'sub' in pieces[0]: #@(sub)group result.Subgroup = True result.Label = label #process all optional pieces for piece in pieces[1:]: try: #here we're finding the key. The value will be '', because it #is stored in delimited_fields (accesible by field_idx) key,value = piece.split('=') setattr(result,trans[key],delimited_fields[field_idx]) field_idx += 1 except ValueError: setattr(result,trans[piece][0],trans[piece][1]) return result