Hello all,
Mind you, these are my very first baby steps in Python so please don't assume I even know what I'm talking about
I have a lot of scripts in Bash and am 'converting' one of them to Python just to get to know how Python works. Up to now I'm loving it, the feel, the logic, the power, ...
I'm having problems with the following:
1. I have a template file which will be filled in by a technician, meaning it's filled with comments (#) and variable = value combinations, where the technician needs to fill in the values needed under the section referring to the configuration he's performing.
2. After installing from DVD/ISO, the software RPMs get installed and there is a default configuration file containing variable = value combinations where the values need to be changed to the values indicated in the template file mentioned in 1.
In order to do this I played around (and Googled a lot) with ConfigParser. I can load the template, get whatever value I need, load the configuration file, set the new value to the correct variable and write the file.
But...
I checked that the template and configuration files loaded (open()) contain everything, including comments. However when I write the configuration file all comments get lost.
Here's what I have so far:
Class to create a fakesection header since ConfigParser needs sections and the configuration file doesn't have those.
Code:
class FakeSectionHead(object):
def __init__(self, fd):
self.fd = fd
self.fakesection = '[fakesection]\n'
def readline(self):
if self.fakesection:
try:
return self.fakesection
finally:
self.fakesection = None
else:
return self.fd.readline()
Snippet from the template file:
Code:
# COMPONENT SPECIFIC CONFIGURATION
#
[ENDPOINT]
# Required packages for Endpoint
REQPKGS = WowzaMediaServer,wowza,cdnnode
# Provide URL to the tracker in FQDN with port
# servername.domain.com:port
TRACKER =
# Provide a unique nodename for the endpoint
NODENAME = #
# Provide the site-id to which this endpoint belongs
SITEID =
# Provide IP:port combination to the log server
LOGSRV =
# Provide DNS Suffix (e.g. .cdn.fakedomain.com)
DNSSUFF =
# Set this to the IP to bind the endpoint process to
BINDIP =
# Set this to the bindinterface the BINDIP is configured on
BINDNIC =
# Provide the license for your WowzaMediaServer
WOWZALIC =
# Provide the maintenance IP to limit SSH to
MAINTIP =
Snippet from default configuration file:
Code:
##############################
# General
##############################
# Node name. just a unique name to identify easily e.g. MADRID-27
node-name = ENDPOINT4
# Site ID. site name where the node is physically installed
site-id = Barcelona
# End point capabilities
# comma separated list of supported delivery types:
# - http. Plain HTTP
# - rtmp. RTMP (Real Time Messaging Protocol). Flash.
# - smooth. Smooth Streaming (Silverlight)
# - iphone. HTTP Live Streaming (Apple)
# - rtsp. Real Time Streaming Protocol (Quicktime, blackberry, nokia).
# - rtmpe. RTMPE (Real Time Messaging Protocol Encrypted). Flash.
# - httpdyn. Http Dynamic Streaming. (Flash)
# - httpredir. HTTP Redirect
#
capability = vod,live,http,rtmp,smooth,iphone,rtsp,rtmpe,httpdyn,httpredir
# Dns suffix used by iphone live streaming to patch m3u8 files (D)
dns-suffix = .intcdn.fakedomain.com
##############################
# Tracker
##############################
# Url for the tracker
tracker-url = http://172.23.145.23:8000
# Url for the tracker
#tracker2-url = http://10.95.98.126:8000
#for p2p between site test
tracker-debug = 1
# Update period to send node stats to the tracker (in seconds) (D)
tracker-update-period = 10
# Site monitor update period to poll origin servers (in seconds) (D)
site-monitor-update-period = 300
# File stats update period (in seconds) (D)
file-stats-update-period = 300
# Bucket update period (in seconds) (D)
bucket-update-period = 30
Python code snippets:
Loading the template and get a value.
Code:
pf1 = ConfigParser.ConfigParser()
fd1 = open("/TEST/configtemplate.cfg")
pf1.readfp(fd1)
NODENAME = pf1.get('ENDPOINT', 'NODENAME')
Loading the configuration file and setting a value:
Code:
pf2 = ConfigParser.ConfigParser()
fd2 = open("/TEST/nodeconfig.cfg")
pf2.readfp(FakeSectionHead(fd2))
pf2.set('fakesection', 'node-name', NODENAME)
pf2.write(r"/TEST/nodeconfig.cfg", "w")
fd2.close()
When I check the contents of nodeconfig.cfg after that the correct value has indeed been set to the correct variable but all of the comments have been deleted.
I tried with ConfigParser.ConfigParser, ConfigParser.RawConfigParser and ConfigParser.SafeConfigParser but always get the same result. The comments in the nodeconfig.cfg file need to be maintained as they are for clarity since these servers are maintained/controlled/accessed by different teams.
Can anybody point me to what I'm doing wrong? Or am I using the wrong tool for the job?
All help or helpful links are highly appreciated. Thanks in advance.
Kind regards,
Eric