#1
  1. Wacky hack
    Devshed Novice (500 - 999 posts)

    Join Date
    Apr 2001
    Location
    London, England
    Posts
    513
    Rep Power
    14

    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)
  2. #2
  3. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    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.
  4. #3
  5. Wacky hack
    Devshed Novice (500 - 999 posts)

    Join Date
    Apr 2001
    Location
    London, England
    Posts
    513
    Rep Power
    14
    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
    Attached Files

IMN logo majestic logo threadwatch logo seochat tools logo