Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Get inside! Sample the range of functionality easily built with JMSL Library for Time Series Data Analysis, Heat Maps, Portfolio Optimization, Monte Carlo Simulation, Stock Price Charting and more. Download Now!
  #1  
Old September 8th, 2003, 12:43 PM
telex4's Avatar
telex4 telex4 is offline
Wacky hack
Dev Shed Novice (500 - 999 posts)
 
Join Date: Apr 2001
Location: London, England
Posts: 512 telex4 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 25 m 29 sec
Reputation Power: 8
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)

Reply With Quote
  #2  
Old September 8th, 2003, 04:53 PM
netytan's Avatar
netytan netytan is offline
Hello World :)
Dev Shed Frequenter (2500 - 2999 posts)
 
Join Date: Mar 2003
Location: Hull, UK
Posts: 2,529 netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level)netytan User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 2 Days 17 h 19 m 5 sec
Reputation Power: 63
Send a message via ICQ to netytan Send a message via AIM to netytan Send a message via MSN to netytan Send a message via Yahoo to netytan
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.

Reply With Quote
  #3  
Old September 8th, 2003, 06:14 PM
telex4's Avatar
telex4 telex4 is offline
Wacky hack
Dev Shed Novice (500 - 999 posts)
 
Join Date: Apr 2001
Location: London, England
Posts: 512 telex4 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 h 25 m 29 sec
Reputation Power: 8
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
File Type: tar config.tar (20.0 KB, 153 views)

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Handling config files


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway