[Tutor] Subdividing config file by matching line from a file

Toni Fuente tonifuente at yahoo.co.uk
Wed Oct 19 10:06:41 EDT 2016


Hello everybody,

I am trying to divide a configuration file (configs) in the form of:

<VirtualHost xxx.xxx.xxx.xxx>
ServerAdmin webmaster at foo.com
ServerName www.foo.com
DocumentRoot /home/public/web/htdocs/foo.com
   ScriptAlias /cgi-bin/ /home/public/web/htdocs/foo.com/cgi-bin/
</VirtualHost>

[...]

<VirtualHost xxx.xxx.xxx.xxx>
ServerAdmin webmaster at bar.com
ServerName www.bar.com
DocumentRoot /home/public/web/htdocs/bar.com/
</VirtualHost>

[...]


into different individual config files for the different domains.

In a different file (sites), I've got a list of domains. 

foo.com
example.com
blabla.com
[...]

I've approached this problem by creating first a list of all the config
"chunks" with this piece of code:


#!/usr/bin/python

token = '\n'
chunks = []
current_chunk = []


for line in open('configs'):
   if line.startswith(token) and current_chunk:
      # if line starts with token and the current chunk is not empty
      chunks.append(current_chunk[:]) #  add not empty chunk to chunks
      current_chunk = [] #  make current chunk blank
   # just append a line to the current chunk on each iteration
   current_chunk.append(line)

chunks.append(current_chunk)  #  append the last chunk outside the loop


Dividing the chunks of confg by blank line: token='\n' into a list of lists.

And now I am stuck trying to iterate through the chunks list, and iterate
through the sites file trying to find the "site" string and if found, print
the entire "chunk", which will be send to a personal site configuration file.
Like so:

for item in chunks:
    #print item
    for line in open('sites'):
        #print line
        for i in item:
            if line not in i:
                continue
            else:
                print item
                break

I don't get the results that I am expecting. Is is the right way to approach
this problem? Any direction will be very helpful.

Thank you in advance,

-- 
Toni


More information about the Tutor mailing list