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

Toni Fuente tonifuente at yahoo.co.uk
Wed Oct 19 12:05:15 EDT 2016


* Peter Otten <__peter__ at web.de> [2016-10-19 16:52:50 +0200]:

> Date: Wed, 19 Oct 2016 16:52:50 +0200
> From: Peter Otten <__peter__ at web.de>
> To: tutor at python.org
> Subject: Re: [Tutor] Subdividing config file by matching line from a file
> Organization: None
> User-Agent: KNode/4.13.3
> Message-ID: <nu81c2$6g8$1 at blaine.gmane.org>
> 
> Toni Fuente via Tutor wrote:
> 
> > 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,
> > 
> 
> You have to search all chunks for a specific site, i. e. you need the outer 
> loop to iterate over the sites and the inner loops (repeatedly) over the 
> chunks. It is easier to see the structure when you factor the site-search 
> into its own function:
> 
> def find_chunk(site, chunks):
>     for chunk in chunks:
>         if any(site in line for line in chunk):
>             return chunk
>     return None # no appropriate chunk found 
>                 # (you might instead raise an Exception)
> 
> 
> for line in open("sites"):
>     site = line.strip()
>     chunk = find_chunk(site, chunks)
>     if chunk is not None:
>         print "Site:", site
>         print "Config:"
>         print "".join(chunk)
>     else:
>         print >> sys.stderr, "no configuration for site", site
>     print "---"
> 
> (If the number of chunks were huge you'd build some kind of lookup table 
> mapping site to chunk, but in your case this is hardly worthwile)
> 
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> https://mail.python.org/mailman/listinfo/tutor


Thank you Peter, that help me a lot, I can see now better what I am
doing.

-- 
Toni

Sólo la verdad os hará libres.
		-- San Juan. (Siglo I) Apóstol cristiano. 


More information about the Tutor mailing list