import re, sys

class ConfigReader:
	"""Class for easily reading text based config files"""
	def __init__(self, path):
		self.path  = path
		self.lines = []
		self.data  = ""
		self.comments = []
		self.config = []
		self.pairs  = {}
	
	def read(self):
		try:
			f = open(self.path, 'r')
			self.data = f.read()
			f.close
		except IOError as (errno, strerror):
			print "I/O error({0}): {1}".format(errno, strerror)
		self.lines = self.data.splitlines()

		for line in self.lines:
			if line.startswith('#'):
				self.comments.append(line)
			elif re.match("[a-zA-Z0-9]", line):
				self.config.append(line)
				m = re.match("^([^=]+)=(.*)$", line)
				key = m.group(1).strip()
				val = m.group(2).strip()
				if len(key) > 0:
					self.pairs[key] = val

	def get_path(self):
		return self.path

	def get_lines(self):
		return self.lines

	def get_comments(self):
		return self.comments

	def get_config(self):
		return self.config

	def get_data(self):
		return self.data

	def get_pairs(self):
		return self.pairs
