import ConfigParser, argparse, os
import netifaces as ni

class MalcomSetup(dict):
	"""Configuraiton loader"""
	def __init__(self):
		super(MalcomSetup, self).__init__()
		
	def save_config():
		raise NotImplemented
		  
	def load_config(self, args):
		self.parse_command_line(args)
		self.sanitize_paths()
		self.get_network_interfaces()

	def sanitize_paths(self):
		if not self['SNIFFER_DIR'].startswith('/'):
			self['SNIFFER_DIR'] = os.path.abspath(os.path.join(os.path.dirname(os.path.realpath(__file__)), '..', 'sniffer'))
		if not self['YARA_PATH'].startswith('/'):
			self['YARA_PATH'] = os.path.abspath(os.path.join(os.path.dirname(os.path.realpath(__file__)), '..', 'yara'))
		if not self['FEEDS_DIR'].startswith('/'):
			self['FEEDS_DIR'] = os.path.abspath(os.path.join(os.path.dirname(os.path.realpath(__file__)), '..', 'feeds'))

	def parse_command_line(self, args):

		if args.config:
			self.parse_config_file(args.config)
		else:
			self['LISTEN_INTERFACE'] = args.interface
			self['LISTEN_PORT'] = args.port
			self['MAX_WORKERS'] = args.max_workers
			self['TLS_PROXY_PORT'] = args.tls_proxy_port
			self['FEEDS'] = args.feeds
			self['SNIFFER'] = args.sniffer
			self['SNIFFER_NETWORK'] = False
			self['SNIFFER_DIR'] = os.path.abspath(os.path.join(os.path.dirname(os.path.realpath(__file__)), '..', 'sniffer'))
			self['YARA_PATH'] = os.path.abspath(os.path.join(os.path.dirname(os.path.realpath(__file__)), '..', 'yara'))
			self['FEEDS_DIR'] = os.path.abspath(os.path.join(os.path.dirname(os.path.realpath(__file__)), '..', 'feeds'))
			self['ANALYTICS'] = args.analytics
			self['WEB'] = True
			self['AUTH'] = False

		
	def parse_config_file(self, filename):
		
		config = ConfigParser.ConfigParser(allow_no_value=True)
		config.read(filename)

		sections = config.sections()

		if config.has_section('web'):
			self['WEB'] = config.getboolean('web', 'activated')
			self['LISTEN_INTERFACE'] = config.get('web', 'listen_interface')
			self['LISTEN_PORT'] = config.getint('web', 'listen_port')
			self['AUTH'] = config.getboolean('web', 'auth')

		if config.has_section('analytics'):
			self['ANALYTICS'] = config.getboolean('analytics', 'activated')
			self['MAX_WORKERS'] = config.getint('analytics', 'max_workers')

		if config.has_section('feeds'):
			self['FEEDS'] = config.getboolean('feeds', 'activated')
			self['FEEDS_DIR'] = config.get('feeds', 'feeds_dir')
			self['FEEDS_SCHEDULER'] = config.getboolean('feeds', 'scheduler')

		if config.has_section('sniffer'):
			self['SNIFFER'] = config.getboolean('sniffer', 'activated')
			self['SNIFFER_DIR'] = config.get('sniffer', 'sniffer_dir')
			self['TLS_PROXY_PORT'] = config.getint('sniffer', 'tls_proxy_port')
			self['YARA_PATH'] = config.get('sniffer', 'yara_path')
			self['SNIFFER_NETWORK'] = config.getboolean('sniffer', 'network')

		if config.has_section('feeds'):
			self['ACTIVATED_FEEDS'] = []
			for feed in config.options('feeds'):
				self['ACTIVATED_FEEDS'].append(feed)
		

	def get_network_interfaces(self):
		self['IFACES'] = {}
		for i in [i for i in ni.interfaces() if i.find('eth') != -1]:
			self['IFACES'][i] = ni.ifaddresses(i).get(2,[{'addr':'Not defined'}])[0]['addr']

	def to_dict(self):
		return self.__dict__

	def __getattr__(self, name):
		return self.get(name, None)

	def __setattr__(self, name, value):
		self[name] = value

@@ -2813,24 +2813,805 @@
 'network')%0A%0A
+%09%09self%5B'DATABASE'%5D = %7B'HOSTS': 'localhost', 'PORT': 27017, 'NAME': 'malcom'%7D%0A%09%09if config.has_section('database'):%0A%09%09%09db_params = dict(config.items('database'))%0A%09%09%09if 'hosts' in db_params:%0A%09%09%09%09self%5B'DATABASE'%5D%5B'HOSTS'%5D = split(db_params%5B'hosts'%5D, ',')%0A%09%09%09if 'port' in db_params:%0A%09%09%09%09self%5B'DATABASE'%5D%5B'PORT'%5D = db_params%5B'port'%5D%0A%09%09%09if 'name' in db_params:%0A%09%09%09%09self%5B'DATABASE'%5D%5B'NAME'%5D = db_params%5B'name'%5D%0A%09%09%09if 'username' in db_params:%0A%09%09%09%09self%5B'DATABASE'%5D%5B'USERNAME'%5D = db_params%5B'username'%5D%0A%09%09%09if 'password' in db_params:%0A%09%09%09%09self%5B'DATABASE'%5D%5B'PASSWORD'%5D = db_params%5B'password'%5D%0A%09%09%09if 'authentication_database' in db_params:%0A%09%09%09%09self%5B'DATABASE'%5D%5B'SOURCE'%5D = db_params%5B'authentication_database'%5D%0A%09%09%09if 'replset' in db_params:%0A%09%09%09%09self%5B'DATABASE'%5D%5B'REPLSET'%5D = db_params%5B'replset'%5D%0A%0A
 %09%09if config.

Add parsing for a "database" section from the configuration file, refs #40