Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #99683
| Path | csiph.com!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 | Sun, 29 Nov 2015 00:07:53 +0100 |
| Organization | None |
| Lines | 116 |
| Message-ID | <mailman.3.1448752087.14615.python-list@python.org> (permalink) |
| References | <73046000-f634-40f2-9c83-f03a5db134e6@googlegroups.com> |
| Mime-Version | 1.0 |
| Content-Type | text/plain; charset="ISO-8859-1" |
| Content-Transfer-Encoding | 7Bit |
| X-Trace | news.uni-berlin.de ByN4dqdtHLVtASLVkrusaQ9Kk9j1xMDFsNr1AjTDmT8A== |
| Return-Path | <python-python-list@m.gmane.org> |
| 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; 'args': 0.04; 'subject:Python': 0.05; 'none:': 0.05; 'sys': 0.05; '"__main__":': 0.07; '__name__': 0.07; 'main()': 0.07; 'skip:/ 10': 0.07; 'subject:file': 0.07; '"w")': 0.09; "'w')": 0.09; 'lines:': 0.09; 'lookup': 0.09; 'os.path': 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; 'def': 0.13; 'file,': 0.15; 'skip:f 30': 0.15; 'variables': 0.15; '(),': 0.16; 'argparse': 0.16; 'key):': 0.16; 'line)': 0.16; 'main():': 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; 'self[key]': 0.16; 'subject:Generate': 0.16; 'subject:search': 0.16; 'templating': 0.16; 'wrote:': 0.16; 'string': 0.17; 'config': 0.18; 'exists': 0.18; 'skip:{ 20': 0.18; 'try:': 0.18; 'input': 0.18; '>>>': 0.20; 'skip:" 30': 0.20; 'parser': 0.22; 'variables.': 0.22; 'file.': 0.22; 'import': 0.24; 'script': 0.25; 'header:User-Agent:1': 0.26; 'header:X-Complaints-To:1': 0.26; 'values': 0.28; 'looks': 0.29; 'cat': 0.29; 'way?': 0.29; 'themselves': 0.29; 'asked': 0.29; 'there.': 0.30; 'print': 0.30; 'skip:s 30': 0.31; 'skip:_ 10': 0.32; 'class': 0.33; 'foo': 0.33; 'skip:/ 20': 0.33; 'file': 0.34; 'except': 0.34; 'replace': 0.35; 'skip:p 30': 0.35; 'but': 0.36; 'skip:i 20': 0.36; 'should': 0.36; 'there': 0.36; 'lines': 0.36; 'basic': 0.36; 'to:addr:python-list': 0.36; 'subject:: ': 0.37; 'two': 0.37; 'received:org': 0.37; 'several': 0.38; 'skip:o 20': 0.38; 'subject:from': 0.39; 'to:addr:python.org': 0.40; 'where': 0.40; 'received:de': 0.40; 'your': 0.60; 'here': 0.66; 'today.': 0.67; 'choose': 0.68; 'serial': 0.70; 'suits': 0.84 |
| X-Injected-Via-Gmane | http://gmane.org/ |
| X-Gmane-NNTP-Posting-Host | p57bd892c.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 <python-list.python.org> |
| List-Unsubscribe | <https://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe> |
| List-Archive | <http://mail.python.org/pipermail/python-list/> |
| List-Post | <mailto:python-list@python.org> |
| List-Help | <mailto:python-list-request@python.org?subject=help> |
| List-Subscribe | <https://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe> |
| Xref | csiph.com comp.lang.python:99683 |
Show key headers only | View raw
Mr Zaug wrote:
> I need to generate a config file based on an existing "template" file. I
> need to replace a set of strings with other strings globally in the
> generated file.
>
> Here is a snippet of the template file, where CONTENT_PATH and DAMPATH are
> two "placeholders" or variables. There are several other such
> placeholders.
>
> $include "_dispatcher_publish_filters.any"
> /1000 { /type "allow" /glob "* /CONTENT_PATH/*.html*" }
> /1001 { /type "allow" /glob "POST /DAMPATH/www/*.html *" }
>
> The script's user will be asked to type in unique values when prompted for
> DAMPATH or CONTENT_PATH.
>
> Since I know the variables themselves are not going to change (because the
> contents of the template file don't spontaneously change) should I be
> using regex to search for them or is there a better way? I was planning on
> using re.sub but I don't know whether that's the best way. Here's what my
> script looks like today.
>
> from sys import argv
> import re
> from os.path import exists
>
> script, template_file = argv
> print "Opening the template file..."
>
> in_file = open(template_file)
> lines = in_file.readlines()
>
> print "What is the serial number of the site?",
> _NNN = raw_input()
>
> print "What is the brand, or product name?",
> _BRAND = raw_input()
>
> print "What is the content path?",
> _CONTENT_PATH = raw_input()
>
> out_file = open(_nnn + _brand + "_farm.any", 'w')
>
> for line in lines:
> re.sub('NNN', _NNN, line)
> re.sub('BRAND, _BRAND', line)
> re.sub('CONTENT_PATH', _CONTENT_PATH, line)
>
> out_file.close()
There are many templating systems out there. Pick the one that suits you
best. A very basic one is str.format():
>>> "{foo} {bar}".format(foo=1, bar=2)
'1 2'
Here's what your script might become if you choose that one:
$ cat interactive_template.txt
$include "_dispatcher_publish_filters.any"
/1000 {{ /type "allow" /glob "* /{CONTENT_PATH}/*.html*" }}
/1001 {{ /type "allow" /glob "POST /{DAMPATH}/www/*.html *" }}
$ cat interactive_template.py
try:
format_map = str.format_map
except AttributeError: # python 2 compatibility
import string
def format_map(text, lookup):
return string.Formatter().vformat(text, (), lookup)
input = raw_input
class Lookup(dict):
def __missing__(self, key):
self[key] = value = input("{}: ".format(key))
return value
def main():
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("template_file")
parser.add_argument("generated_file", nargs="?")
args = parser.parse_args()
with open(args.template_file) as instream:
template_text = instream.read()
lookup = Lookup()
generated_text = format_map(template_text, lookup)
generated_file = args.generated_file
if generated_file is None:
generated_file = format_map("{nnn}{brand}_farm.any", lookup)
print("Writing {}".format(generated_file))
with open(generated_file, "w") as outstream:
outstream.write(generated_text)
if __name__ == "__main__":
main()
$ python interactive_template.py interactive_template.txt
CONTENT_PATH: foo
DAMPATH: bar
nnn: baz
brand: ham
Writing bazham_farm.any
$ cat bazham_farm.any
$include "_dispatcher_publish_filters.any"
/1000 { /type "allow" /glob "* /foo/*.html*" }
/1001 { /type "allow" /glob "POST /bar/www/*.html *" }
Back to comp.lang.python | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Generate config file from template using Python search and replace. Mr Zaug <matthew.herzog@gmail.com> - 2015-11-28 13:45 -0800
Re: Generate config file from template using Python search and replace. Mr Zaug <matthew.herzog@gmail.com> - 2015-11-28 14:04 -0800
Re: Generate config file from template using Python search and replace. Peter Otten <__peter__@web.de> - 2015-11-29 00:07 +0100
Re: Generate config file from template using Python search and replace. Mr Zaug <matthew.herzog@gmail.com> - 2015-11-29 12:23 -0800
Re: Generate config file from template using Python search and replace. Peter Otten <__peter__@web.de> - 2015-11-29 23:50 +0100
Re: Generate config file from template using Python search and replace. Mr Zaug <matthew.herzog@gmail.com> - 2015-11-29 17:28 -0800
Re: Generate config file from template using Python search and replace. Peter Otten <__peter__@web.de> - 2015-11-30 10:14 +0100
Re: Generate config file from template using Python search and replace. Mr Zaug <matthew.herzog@gmail.com> - 2015-11-30 20:54 -0800
Re: Generate config file from template using Python search and replace. Peter Otten <__peter__@web.de> - 2015-12-01 11:49 +0100
Re: Generate config file from template using Python search and replace. Mr Zaug <matthew.herzog@gmail.com> - 2015-12-01 05:18 -0800
Re: Generate config file from template using Python search and replace. Mr Zaug <matthew.herzog@gmail.com> - 2015-12-01 06:18 -0800
Re: Generate config file from template using Python search and replace. Peter Otten <__peter__@web.de> - 2015-12-01 15:41 +0100
Re: Generate config file from template using Python search and replace. Mr Zaug <matthew.herzog@gmail.com> - 2015-12-01 07:21 -0800
Re: Generate config file from template using Python search and replace. Peter Otten <__peter__@web.de> - 2015-12-01 16:43 +0100
Re: Generate config file from template using Python search and replace. Mr Zaug <matthew.herzog@gmail.com> - 2015-12-01 10:43 -0800
Re: Generate config file from template using Python search and replace. Mr Zaug <matthew.herzog@gmail.com> - 2015-11-29 18:31 -0800
Re: Generate config file from template using Python search and replace. Rob Hills <rhills@medimorphosis.com.au> - 2015-11-30 10:40 +0800
Re: Generate config file from template using Python search and replace. MRAB <python@mrabarnett.plus.com> - 2015-11-30 03:23 +0000
csiph-web