The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.
|
 |
|
Dev Shed Forums
> Programming Languages
> Python Programming
|
Handling config files
Discuss Handling config files in the Python Programming forum on Dev Shed. Handling config files Python Programming forum discussing coding techniques, tips and tricks, and Zope related information. Python was designed from the ground up to be a completely object-oriented programming language.
|
|
 |
|
|
|
|

Dev Shed Forums Sponsor:
|
|
|

September 8th, 2003, 12:43 PM
|
 |
Wacky hack
|
|
Join Date: Apr 2001
Location: London, England
Posts: 513
Time spent in forums: 1 h 38 m 37 sec
Reputation Power: 13
|
|
|
Handling config files
I was trying to find some code snippet to post in the "post some code" thread, but was unable to find anything snappy and well written in Python in my hard drive, so here's a slightly longer bit if code I wrote and now find really useful.
It handles config files... you might guess what app it came from
Code:
def loadConfig(self):
"""Load user configuration file"""
self.config = {}
self.parser = ConfigParser.ConfigParser()
if not os.path.isfile(self.configfile):
self.parser.write(open(self.configfile, 'w'))
self.parser.readfp(open(self.configfile, 'r'))
variables = { \
'mplayer': ['paths', self.findProgram("mplayer")],
'outputdir': ['paths', os.path.expanduser("~")], \
'dvd_device': ['paths', os.path.join("/", "dev", "dvd")], \
'deinterlacing': ['vencode', 0] \
}
for key in variables.keys():
self.cautiousLoad(variables[key][0], key, variables[key][1])
def cautiousLoad(self, section, var, default):
"""Load a configurable variable within an exception clause,
in case variable is not in configuration file"""
try:
self.config[var] = int(self.parser.get(section, var))
except:
self.config[var] = default
try:
self.parser.set(section, var, default)
except:
self.parser.add_section(section)
self.parser.set(section, var, default)
self.parser.write(open(self.configfile, 'w'))
def findProgram(self, program):
"""Looks for program in path, and returns full path if found"""
for path in config.paths:
if os.path.isfile(os.path.join(path, program)):
return os.path.join(path, program)
|

September 8th, 2003, 04:53 PM
|
 |
Hello World :)
|
|
Join Date: Mar 2003
Location: Hull, UK
|
|
I wrote a small config file parser for something I was working on a lil while ago. Here's a sample config file..
Code:
[some key]
followed
by
some
string
[another key]
Any string you like as long as it doesn't contain too '\n' char's together.
[yet another]
It's pretty basic and has room for improvments i.e error catching, but it does the job :) and it's very easy to use.
and the parser
Code:
#!/urs/bin/env python
def load(file):
configure = {}
data = open(file, 'r').read().split('\n\n')
for part in data:
part = part.split('\n', 1)
configure[part[0][1:-1]] = part[1].strip()
return configure
if __name__ == '__main__':
config = load('config.txt')
print str(config) + '\n\n'
for each in config.keys():
print config[each] + '\n'
Anyway there you have it! QuickRip right telex  , the 'mpayer' and 'dvd_devide' where a bit of a give away.. oh just out of interest, could you attach the whole file/classes you use for handling your config?
Have fun,
Mark.
|

September 8th, 2003, 06:14 PM
|
 |
Wacky hack
|
|
Join Date: Apr 2001
Location: London, England
Posts: 513
Time spent in forums: 1 h 38 m 37 sec
Reputation Power: 13
|
|
Good guess
I've attached the config file (a sample, with lots of comments that the methods will ignore, and unfortunately lose when writing the config file... a feature missing in Python's ConfigParser lib) and base.py, which contains the config methods in the QuickRip class.
The rest of the code is pretty messy, as it's CVS code that's suddenly been left mid-work and needs picking up at some point 
|
Developer Shed Advertisers and Affiliates
| Thread Tools |
Search this Thread |
|
|
|
| Display Modes |
Rate This Thread |
Linear Mode
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
|
|