
import os, site, tempfile, yaml
from PyQt5.QtCore import Qt
from PyQt5.QtGui import QPalette, QColor

def configure():
	_files = []
	for fname in [ 'icons.yaml', 'actions.yaml', 'menu.yaml', 'config.yaml' ]:
		_files += [os.path.join(p, 'fexqt', fname) for p in site.getsitepackages()]
		if 'APPDATA' in os.environ:
			_files += os.path.join(os.environ['APPDATA'], 'fex', fname)
		elif 'XDG_CONFIG_HOME' in os.environ:
			_files += [os.path.join(os.environ['XDG_CONFIG_HOME'], 'fex', fname)]
		elif 'HOME' in os.environ:
			_files += [os.path.join(os.environ['HOME'], '.config', 'fex', fname)]
		_files += [os.path.join(os.getcwd(), fname)]

	_yaml = """
constants:

   Window: &Window "%(Window)s"
   Background: &Background "%(Background)s"
   WindowText: &WindowText "%(WindowText)s"
   Foreground: &Foreground "%(Foreground)s"
   Base: &Base "%(Base)s"
   AlternateBase: &AlternateBase "%(AlternateBase)s"
   ToolTipBase: &ToolTipBase "%(ToolTipBase)s"
   ToolTipText: &ToolTipText "%(ToolTipText)s"
   Text: &Text "%(Text)s"
   Button: &Button "%(Button)s"
   ButtonText: &ButtonText "%(ButtonText)s"
   BrightText: &BrightText "%(BrightText)s"
   Highlight: &Highlight "%(Highlight)s"

   IconOnly: &IconOnly %(IconOnly)d
   TextOnly: &TextOnly %(TextOnly)d
   TextBesideIcon: &TextBesideIcon %(TextBesideIcon)d
   TextUnderIcon: &TextUnderIcon %(TextUnderIcon)d
   FollowStyle: &FollowStyle %(FollowStyle)d

   TempFile: &TempFile "%(TempFile)s"
""" % {

		'Window': QColor(QPalette.Window).name(),
		'Background': QColor(QPalette.Background).name(),
		'WindowText': QColor(QPalette.WindowText).name(),
		'Foreground': QColor(QPalette.Foreground).name(),
		'Base': QColor(QPalette.Base).name(),
		'AlternateBase': QColor(QPalette.AlternateBase).name(),
		'ToolTipBase': QColor(QPalette.ToolTipBase).name(),
		'ToolTipText': QColor(QPalette.ToolTipText).name(),
		'Text': QColor(QPalette.Text).name(),
		'Button': QColor(QPalette.Button).name(),
		'ButtonText': QColor(QPalette.ButtonText).name(),
		'BrightText': QColor(QPalette.BrightText).name(),
		'Highlight':  QColor(QPalette.Highlight).name(),

		'IconOnly': Qt.ToolButtonIconOnly,
		'TextOnly': Qt.ToolButtonTextOnly,
		'TextBesideIcon': Qt.ToolButtonTextBesideIcon,
		'TextUnderIcon': Qt.ToolButtonTextUnderIcon,
		'FollowStyle': Qt.ToolButtonFollowStyle,

		'TempFile': os.path.join(tempfile.gettempdir(), 'fex.log'),
	}

	for fname in _files:
		if (os.path.isfile(fname)):
			with open(fname, 'r') as fin:
				print(' INFO: reading config from %s' % fname)
				_yaml += fin.read()
	return yaml.load(_yaml)


