Skip to content

Instantly share code, notes, and snippets.

@Ozerich
Created April 10, 2011 10:31
Show Gist options
  • Select an option

  • Save Ozerich/912225 to your computer and use it in GitHub Desktop.

Select an option

Save Ozerich/912225 to your computer and use it in GitHub Desktop.
import os
import re
import glob
class parser(object):
config_dir = 'D:/Parser/'
config_file = 'httpd.conf'
config_path = os.path.join(config_dir, config_file);
def is_installed(self):
return os.path.exists(config_path)
def get_hosts(self):
r = {}
f = open(self.config_path)
text = f.read()
pat = re.compile('<VirtualHost (.+?:\d+)>',re.S)
for m in pat.finditer(text):
beg = m.start();
while(text[beg] != '\n' and beg > 0):
beg = beg - 1
enabled = not (text[beg + 1] == '#')
item = {
"name" : m.group(1),
"enabled" : not enabled}
if(enabled):
pat_in = re.compile('(.+?)[^#]\s*</VirtualHost>', re.S);
else:
pat_in = re.compile('(.+?)#\s*</VirtualHost>', re.S);
data = pat_in.search(text, beg)
if(not data):
continue;
data = data.group()
data = data.replace('\n#','\n')
item['config'] = re.sub('#.+?\n','',data);
r[item['name']] = item;
f.close()
return r;
def save_host(self, host):
f = open(self.config_path, 'a+')
f.write(host)
f.close()
def delete_host(self, id):
id = id.replace('*','\*')
id = id.replace('.','\.')
f = open(self.config_path, 'r')
text = f.read()
f.close()
text = repl_pat.sub('', text)
f = open(self.config_path, 'w')
f.write(text)
f.close()
def disable_host(self, id):
id = id.replace('*','\*')
id = id.replace('.','\.')
f = open(self.config_path, 'r')
text = f.read()
f.close()
pat = re.compile('(?:#\s*)*<VirtualHost ' + id + '>(.+?)</VirtualHost>', re.S)
res = pat.search(text);
host_text = res.group(0);
host_text = host_text.replace('\n', '\n#');
host_text = '#' + host_text;
text = text.replace(res.group(0), host_text);
f = open(self.config_path, 'w')
f.write(text)
f.close()
def enable_host(self, id):
id = id.replace('*','\*')
id = id.replace('.','\.')
f = open(self.config_path, 'r')
text = f.read()
f.close()
pat = re.compile('#\s*<VirtualHost ' + id + '>(.+?)#\s*</VirtualHost>', re.S)
res = pat.search(text);
host_text = res.group(0);
host_text = host_text.replace('\n#', '\n');
host_text = host_text[1:]
text = text.replace(res.group(0), host_text);
f = open(self.config_path, 'w')
f.write(text)
f.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment