LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-16-2011, 02:23 PM   #1
EricTRA
LQ Guru
 
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 20 with Awesome WM
Posts: 6,805
Blog Entries: 1

Rep: Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297
Python ConfigParser not saving comments


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
 
Old 06-16-2011, 06:51 PM   #2
bgeddy
Senior Member
 
Registered: Sep 2006
Location: Liverpool - England
Distribution: slackware64 13.37 and -current, Dragonfly BSD
Posts: 1,810

Rep: Reputation: 232Reputation: 232Reputation: 232
I think you should have a look at the python module cfgparse as it seems to do what you want. I'm no expert with this module but have been playing around with it and it seems to be a lot better than the standard ConfigParser.

In saying that, as you are saying you are just starting out with python, perhaps it would be better to work your way through a tutorial such as the standard one before playing with external libraries and such. Forgive me if I seem perhaps condescending as your supplied code indicates you have a good grasp of python basics and classes, etc.
 
1 members found this post helpful.
Old 06-17-2011, 12:15 AM   #3
EricTRA
LQ Guru
 
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 20 with Awesome WM
Posts: 6,805

Original Poster
Blog Entries: 1

Rep: Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297
Hello,

Thank you for your reply and your pointers. I'll look into cfgparse and see if it suits my needs better than ConfigParser.

I've read some books about Python but since I'm limited in free time, I got the little bit of knowledge obtained from the books and started with hands on practice. I'm one of those old-timers that gets a grasp on something new a lot faster and easier when using it in a trial / error way, of course with the help of Google and LQ.

Kind regards,

Eric
 
Old 06-17-2011, 03:15 AM   #4
EricTRA
LQ Guru
 
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 20 with Awesome WM
Posts: 6,805

Original Poster
Blog Entries: 1

Rep: Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297Reputation: 1297
Hi,

The cfgparse module does what I need, so this one is solved. Thanks bgeddy!

Kind regards,

Eric
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Saving file data using Python in an embedded system in an safe and fast way Fabio Paolini Programming 7 04-24-2011 11:07 AM
Python 2.6 dependencies error installing python-sqlite2-2.5.5-1.1.i586.rpm jmp007 Linux - Newbie 1 02-11-2011 11:05 AM
LXer: Python Python Python (aka Python 3) LXer Syndicated Linux News 0 08-05-2009 08:30 PM
LXer: Review: Programming in Python 3: A Complete Introduction to the Python Language LXer Syndicated Linux News 0 01-26-2009 04:50 AM
saving and not saving sessions on desktop ergo_sum Linux - Newbie 2 09-26-2003 07:27 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:15 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration