Re: Your IDE's?
On Monday, March 25, 2019 at 5:38:41 PM UTC-4, John Doe wrote: > What is your favorite Python IDE? "Your IDE's?" is not a question, nor is any word in English made plural with an apostrophe s. -- https://mail.python.org/mailman/listinfo/python-list
Generate config file from template using Python search and replace.
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() -- https://mail.python.org/mailman/listinfo/python-list
Re: Generate config file from template using Python search and replace.
I should mention the template file is small, just 98 lines long and the working config file will be the same size. -- https://mail.python.org/mailman/listinfo/python-list
Re: Generate config file from template using Python search and replace.
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? -- https://mail.python.org/mailman/listinfo/python-list
I can't understand re.sub
I need to use re.sub to replace strings in a text file. I can't seem to understand how to use the re module to this end. result = re.sub(pattern, repl, string, count=0, flags=0); I think I understand that pattern is the regex I'm searching for and repl is the thing I want to substitute for whatever pattern finds but what is string? The items I'm searching for are few and they do not change. They are "CONTENT_PATH", "ENV" and "NNN". These appear on a few lines in a template file. They do not appear together on any line and they only appear once on each line. This should be simple, right? -- https://mail.python.org/mailman/listinfo/python-list
Re: I can't understand re.sub
Thanks. That does help quite a lot. -- https://mail.python.org/mailman/listinfo/python-list
Re: I can't understand re.sub
On Sunday, November 29, 2015 at 8:12:25 PM UTC-5, Rick Johnson wrote: > On Sunday, November 29, 2015 at 3:37:34 PM UTC-6, Mr Zaug wrote: > > > The items I'm searching for are few and they do not change. They are > > "CONTENT_PATH", "ENV" and "NNN". These appear on a few lines in a template > > file. They do not appear together on any line and they only appear once on > > each line. This should be simple, right? > > Yes. In fact so simple that string methods and a "for loop" will suffice. > Using regexps for this tasks would be like using a dump truck to haul a > teaspoon of salt. I rarely get a chance to do any scripting so yeah, I stink at it. Ideally I would have a script that will spit out a config file such as 087_pre-prod_snakeoil_farm.any and not need to manually rename said output file. -- https://mail.python.org/mailman/listinfo/python-list
Re: Generate config file from template using Python search and replace.
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: -- https://mail.python.org/mailman/listinfo/python-list
Re: Generate config file from template using Python search and replace.
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. I seem to be heading in this direction. #!/usr/bin/env python import re from os.path import exists script, template_file = argv print "Opening the template file..." with open (template_file, "r") as a_string: data=a_string.read().replace('BRAND', 'Fluxotine') print(data) So now the challenge is to use the read().replace magic for multiple values. -- https://mail.python.org/mailman/listinfo/python-list
Re: Generate config file from template using Python search and replace.
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() print "What is the brand, or product name?", brand = raw_input() print "What is the (fqdn) ServerName?", server_name = raw_input() print "What is the content path?", content_path = raw_input() print "What is the DAM path?", dampath = raw_input() print "Which environment is this for?", env = raw_input() print "What is the cache document root?", cache_docroot = 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) -- https://mail.python.org/mailman/listinfo/python-list
Re: Generate config file from template using Python search and replace.
Oh, that's much easier to read. Thanks! -- https://mail.python.org/mailman/listinfo/python-list
Re: Generate config file from template using Python search and replace.
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. Also, do I really need curly braces to tell Python what my placeholders are? -- https://mail.python.org/mailman/listinfo/python-list
Re: Generate config file from template using Python search and replace.
That makes sense. So I still can't see how to write the string object to a file whist naming the file with whatever values I provided for the NNN and BRAND variables. Printing the contents of the string object is working with all the expected substitutions. Do I need to convert the string object into a file before I write it? Or do I need to open a new file and somehow stuff the string object into it? -- https://mail.python.org/mailman/listinfo/python-list
Re: Generate config file from template using Python search and replace.
Ye, this does work. Many thanks! filename = "{NNN}_{BRAND}_farm.any".format(BRAND=brand, NNN=nnn) with open(filename, "w") as outstream: outstream.write(data) -- https://mail.python.org/mailman/listinfo/python-list