def parse_confgen_modeline(modeline): tline = modeline.strip(' ').rstrip(' ') # Has to start with confgen if not tline.startswith('confgen'): raise Exception('Not a valid confgen modeline') # Split out the confgen part # Per the spec, a colon must come AFTER the confgen part # Spaces may be in between the colon (be liberal in what you accept) cf, sep, tline = tline.partition(':') values = { } # The rest is separated into key : value pairs while tline != '': # Separate out items # Per the spec, each key = value pair is separated by a comma. item, sep, tline = tline.partition(',') # Separate out key : value key, sep, value = item.partition('=') key = key.strip(' ').rstrip(' ') value = value.strip(' ').rstrip(' ') if sep == '': # Assume no value. This is legal, and makes the item just set to # true. values[key] = True else: # Assign a key, value pair to the dict values[key] = value return values