Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #99801

Re: Generate config file from template using Python search and replace.

From Peter Otten <__peter__@web.de>
Newsgroups comp.lang.python
Subject Re: Generate config file from template using Python search and replace.
Date 2015-12-01 15:41 +0100
Organization None
Message-ID <mailman.75.1448980899.14615.python-list@python.org> (permalink)
References (4 earlier) <0dd0db34-7f8f-4e8b-ad6a-c82ad19c2e5e@googlegroups.com> <mailman.33.1448874871.14615.python-list@python.org> <8c787187-910c-40df-9235-9e4c2dafb19c@googlegroups.com> <mailman.64.1448966994.14615.python-list@python.org> <076b883f-f649-48e0-a064-f05d4d6c5909@googlegroups.com>

Show all headers | View raw


Mr Zaug wrote:

> Actually, I don't understand what you mean by "all other braces." What
> braces are you talking about? The placeholders in the template file (the
> file being read in) have braces around them but they are not escaped.

The example you provide was

      $include "_dispatcher_publish_filters.any"
      /1000 { /type "allow"  /glob "* /CONTENT_PATH/*.html*" }
      /1001 { /type "allow"  /glob "POST /DAMPATH/www/*.html *" }

If you want to use string formatting to replace CONTENT_PATH and DAMPATH in 
the above excerpt you have to change the template to

      $include "_dispatcher_publish_filters.any"
      /1000 {{ /type "allow"  /glob "* /{CONTENT_PATH}/*.html*" }}
      /1001 {{ /type "allow"  /glob "POST /{DAMPATH}/www/*.html *" }}


> Also, do I really need curly braces to tell Python what my placeholders
> are?

Let's take a slightly modified example to answer that one:

      $include "_dispatcher_publish_filters.any"
      /1000 { /type "allow"  /glob "* /CONTENT_PATH/*.html*" }
      /1001 { /type "allow"  /glob "POST /DAMPATH/www/*.html *" }
      /1002 { /type "allow"  /glob "* /ALTERNATIVE_CONTENT_PATH/*.html*" }


If you try to fill in the CONTENT_PATH with 

    data = data.replace("CONTENT_PATH", "foo")

the result will be

      $include "_dispatcher_publish_filters.any"
      /1000 { /type "allow"  /glob "* /foo/*.html*" }
      /1001 { /type "allow"  /glob "POST /DAMPATH/www/*.html *" }
      /1002 { /type "allow"  /glob "* /ALTERNATIVE_foo/*.html*" }

Look at the butchered line starting with /1002, a problem that MRAB already 
pointed out before. The braces are not necessary if the string you are 
replacing doesn't occur elsewhere in the template, but they make the 
replacement process both flexible and unambiguous.

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


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