Path: csiph.com!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail From: Peter Otten <__peter__@web.de> Newsgroups: comp.lang.python Subject: Re: Generate config file from template using Python search and replace. Date: Tue, 01 Dec 2015 11:49:37 +0100 Organization: None Lines: 67 Message-ID: References: <73046000-f634-40f2-9c83-f03a5db134e6@googlegroups.com> <4f923003-4f85-4a69-bfda-165194211bb4@googlegroups.com> <0dd0db34-7f8f-4e8b-ad6a-c82ad19c2e5e@googlegroups.com> <8c787187-910c-40df-9235-9e4c2dafb19c@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset="ISO-8859-1" Content-Transfer-Encoding: 7Bit X-Trace: news.uni-berlin.de Wa2YmY18urDbJvLwIKZUyAWyeyF/DDXJZZqMsv+AmIqQ== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'subject:Python': 0.05; 'cache': 0.05; 'sys': 0.05; 'that?': 0.05; 'remaining': 0.07; 'subject:file': 0.07; '"r")': 0.09; 'braces': 0.09; 'literal': 0.09; 'received:80.91': 0.09; 'received:80.91.229': 0.09; 'received:gmane.org': 0.09; 'received:list': 0.09; 'script,': 0.09; 'snippet': 0.09; 'subject:using': 0.09; 'python': 0.10; 'template': 0.11; 'question.': 0.13; 'properly': 0.15; 'did.': 0.16; 'options"': 0.16; 'options"),': 0.16; 'received:80.91.229.3': 0.16; 'received:dip0.t-ipconnect.de': 0.16; 'received:io': 0.16; 'received:plane.gmane.org': 0.16; 'received:psf.io': 0.16; 'received:t-ipconnect.de': 0.16; 'subject:Generate': 0.16; 'subject:search': 0.16; 'yup,': 0.16; '}}.': 0.16; 'wrote:': 0.16; '"you': 0.18; 'script.': 0.18; '2015': 0.20; 'permission': 0.20; 'skip:" 30': 0.20; 'posted': 0.21; 'referring': 0.22; 'sorry,': 0.22; 'file.': 0.22; 'import': 0.24; 'skip:b 30': 0.24; 'words': 0.24; 'script': 0.25; 'header :User-Agent:1': 0.26; 'header:X-Complaints-To:1': 0.26; 'sense': 0.26; 'skip:# 10': 0.27; 'see,': 0.27; 'skip:e 30': 0.27; 'went': 0.28; 'escaped': 0.29; "i'm": 0.30; 'print': 0.30; 'becomes': 0.30; 'task': 0.30; 'supposed': 0.31; 'skip:s 30': 0.31; "can't": 0.32; 'skip:d 40': 0.32; 'run': 0.33; 'problem': 0.33; 'foo': 0.33; 'open': 0.33; 'file': 0.34; 'skip:d 20': 0.34; 'skip:c 30': 0.35; 'replace': 0.35; 'instead': 0.36; 'monday,': 0.36; 'to:addr :python-list': 0.36; 'subject:: ': 0.37; 'received:org': 0.37; 'data': 0.39; 'sure': 0.39; 'format': 0.39; 'subject:from': 0.39; 'to:addr:python.org': 0.40; 'received:de': 0.40; 'your': 0.60; '30,': 0.63; 'incorporate': 0.66; 'here': 0.66; 'serial': 0.70; 'otten': 0.84 X-Injected-Via-Gmane: http://gmane.org/ X-Gmane-NNTP-Posting-Host: p57bd9ec4.dip0.t-ipconnect.de User-Agent: KNode/4.13.3 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:99787 Mr Zaug wrote: > On Monday, November 30, 2015 at 4:14:48 AM UTC-5, Peter Otten wrote: >> Mr Zaug wrote: >> >> > On Sunday, November 29, 2015 at 5:50:51 PM UTC-5, Peter Otten wrote: >> >> Mr Zaug wrote: >> >> >> >> > When I run this script on OS X El Capitan, I see, >> >> > >> >> > # permission sensitive cache >> >> > $include "_dispatcher_shared_auth-checker: >> >> > >> >> > Was I supposed to incorporate it into the script I posted? >> >> >> >> Are you referring to my post? I'm sorry, I can't make sense of your >> >> question. >> > >> > Yes. The snippet you posted went way over my head. When I ran it, it >> > printed >> > >> > # permission sensitive cache >> > $include "_dispatcher_shared_auth-checker: >> >> It's hard to tell from that problem description what might have gone >> wrong. However: >> >> In your template file you have to enclose all words you want to replace >> in braces ("you have foo options" becomes "you have {foo} options"), to >> replace all literal { with {{ and all literal } with }}. >> >> Did you do that? > > Yup, I sure did. So here is my current script. The only remaining task now > is figuring out how to write the contents to a new file. > > #!/usr/bin/env python > import os > import sys > > script, template_file = sys.argv > print "Opening the template file..." > > print "What is the serial number of the site?", > nnn = raw_input() [...] > with open (template_file, "r") as a_string: > data=a_string.read().replace('{SERVER_NAME}', > server_name).replace('{BRAND}', brand).replace('{CONTENT_PATH}', > content_path).replace('{DAMPATH}', dampath).replace('{ENV}', > env).replace('{CACHE_DOCROOT}', cache_docroot) If all other braces are properly escaped you can use format instead of replace: data = a_string.read().format( SERVER_NAME=server_name, BRAND=brand, CONTENT_PATH=content_path DAMPATH=dampath, ENV=env, CACHE_DOCROOT=cache_doc_root)