#!/usr/bin/env python
"""
SETUP-GUI script for Package PLIB
Copyright (C) 2008 by Peter A. Donis

This script determines what GUI toolkits are present on the system,
and writes a _setup.py module to the plib.gui directory that defines
appropriate constants. This module is then loaded by the main gui module
to determine which toolkits are available for use. The script should be
run after the sub-packages for PLIB are installed, since it uses some of
them.

Note that this script should only need to be run on initial installation
of PLIB or when toolkit packages are installed or uninstalled.
"""

import sys
import os
import compiler

from plib.stdlib import sitepath, plibpath

# Check which GUI toolkits are available

qt_file = os.path.join(sitepath, "qt.so")
qt_present = os.path.isfile(qt_file)
if qt_present:
    try:
        import qt
    except ImportError:
        qt_present = False

kde_file = os.path.join(sitepath, "kdecore.so")
kde_present = os.path.isfile(kde_file)
if kde_present:
    try:
        import kdecore
    except ImportError:
        kde_present = False

gtk_file = os.path.join(sitepath, "pygtk.pth")
gtk_present = os.path.isfile(gtk_file)
if gtk_present:
    try:
        import pygtk
        pygtk.require('2.0')
        import gtk
    except ImportError:
        gtk_present = False
    except AssertionError:
        gtk_present = False

wx_file = os.path.join(sitepath, "wx.pth")
wx_present = os.path.isfile(wx_file)
if wx_present:
    try:
        import wx
    except ImportError:
        wx_present = False

bool_disp = {False: "not present", True: "present"}

print "Site package path:", sitepath
print "Qt:", bool_disp[qt_present]
print "KDE:", bool_disp[kde_present]
print "Gtk:", bool_disp[gtk_present]
print "WxWidgets:", bool_disp[wx_present]

print "Writing _setup.py module to GUI directory..."

module_vars = [ ("QT_PRESENT", qt_present),
    ("KDE_PRESENT", kde_present),
    ("GTK_PRESENT", gtk_present),
    ("WX_PRESENT", wx_present) ]

lines = [ "#!/usr/bin/env python\n",
    "# _SETUP.PY -- PLIB.GUI Toolkit Setup Module\n",
    "# *** This module is automatically generated; do not edit. ***\n",
    "\n" ]

lines.extend(("%s = %s\n" % vars for vars in module_vars))

outpath = os.path.join(plibpath, "gui", "_setup.py")
outfile = open(outpath, 'w')
outfile.writelines(lines)
outfile.close()

print "Byte-compiling _setup.py..."
compiler.compileFile(outpath)

print "PLIB GUI setup done!"
