Extracting values from a Cherokee configuration file
I need to extract the maximum vserver
id and the maximum source
id from a Cherokee configuration file, so I can auto-generate configuration for test instances. The following is the solution I came up with:
import os
import re
def increpl(matchobj):
"""
Replace the include statement with the content of the file.
"""
inc = matchobj.group(1)
return parse(inc)
def parse(filename):
"""
Returns the configuration with all includes resolved.
"""
cfile = open(filename, 'r')
content = cfile.read()
cfile.close()
return re.sub('include\s*=\s*(.*)\s*', increpl, content)
def get_ids(filename):
"""
Returns the maximum vserver id, and the maximum source id
present in this configuration.
"""
config = parse(filename)
vs_ids = [int(x) for x in (re.findall('vserver\!(\d+)\!', config))]
src_ids = [int(x) for x in (re.findall('source\!(\d+)\!', config))]
return max(list(set(vs_ids))), max(list(set(src_ids)))
if __name__ == "__main__":
vserver_id, source_id = get_ids("cherokee.conf")
print vserver_id, source_id
Are there any obvious improvements you would make to this?