#! /usr/bin/python
# -*- coding: iso-8859-1 -*-

#####################################################
#    Copyright (C) 2006 by Cesar Kallas   	
#    cesarkallas@soulivre.org   
#    http://cesarkallas.soulivre.org
#                                                                          
#    This program is free software; you can redistribute it and#or modify  
#    it under the terms of the GNU General Public License as published by 
#    the Free Software Foundation; either version 2 of the License, or     
#    (at your option) any later version.                                   
#                                                                          
#    This program is distributed in the hope that it will be useful,       
#    but WITHOUT ANY WARRANTY; without even the implied warranty of        
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         
#    GNU General Public License for more details.                          
#                                                                          
#    You should have received a copy of the GNU General Public License     
#    along with this program; if not, write to the                         
#    Free Software Foundation, Inc.,                                       
#    59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             
#####################################################

from ConfigParser import *

class Opcoes(object):
	"""
		Classe que gerencia o arquivo de opcoes
		Data: 31/08/2006
	"""

	def __init__(self, arquivoOpcoes="opcoes.cfg"):
		self.arquivoOpcoes = arquivoOpcoes
		self.config = ConfigParser()
	
	def setHost(self, h):
		self.host = h
		
	def getHost(self):
		return self.host
		
	def ler(self, secao):
		"""
			Abre o arquivo somente para leitura
			Entrada: opcoes.cfg
			Retorno OK: Dicionario com as opcoes lidas no arquivo
			Retorno falho: -1
		"""
		try:
			self.config.read(self.arquivoOpcoes)
			dic = {}
			for option in self.config.options(secao):
				dic[option] = self.config.get(secao, option, 1)
			return dic
		except:
			raise
			return -1
			
		
	def gerarLista(self, secao):
		"""
			Abre o arquivo somente para leitura
			Entrada: opcoes.cfg
			Retorno OK: Dicionario com as opcoes lidas no arquivo
			Retorno falho: -1
		"""
		try:
			listaOpcoes = []
			self.config.read(self.arquivoOpcoes)
			for secao in self.config.sections():
				dicOpcao = {}
				dicItems = {}
				for item in self.config.options(secao):
					dicItems[item] = self.config.get(secao, item, 1)
				dicOpcao[secao] = dicItems
				listaOpcoes.append(dicOpcao)
			return listaOpcoes
			self.config.close()
		except:
			raise
			return -1
					
	def gravar(self, novoDicOpcoes):
		"""
			Abre o arquivo somente para escrita
			Entrada: Dicionario novoDicOpcoes
			Retorno OK: 1
			Retorno falho: -1
		"""
		try:
			arquivo = open(self.arquivoOpcoes, 'w')
			arquivo.write(str(novoDicOpcoes) + '\n')
			arquivo.flush()
			arquivo.close()
			self.dicOpcoes = novoDicOpcoes
			return 1
		except:
			raise
			return -1
