#!/usr/bin/env python
"""
This script maps eric3 icon names to kde icon names.  Run it from the command
line without arguments to learn its usage.

Email me with questions or comments:  troy@gci.net
"""
import os
import sys

icon_mapping = """
addBookmark.png,actions/bookmark_add.png
altn.png
anychar.png,actions/charset.png
back.png,actions/back.png
begline.png,actions/editclear.png
bookmark.png,actions/bookmark.png
bookmarkNext.png,actions/up.png
bookmarkPrevious.png,actions/down.png
bookmarkToggle.png,actions/bookmark.png
break.png,actions/cancel.png
breakDisabled.png,actions/fileclose.png
breakpointNext.png
breakpointPrevious.png
breakpointToggle.png
browser.png,apps/konqueror.png
cBreak.png
cBreakpointToggle.png
characters.png
class.png,devices/blockdevice.png
close.png,actions/fileclose.png
comment.png
compareFiles.png
configure.png,actions/configure.png
configureShortcuts.png,actions/configure_shortcuts.png
continue.png
coverageProject.png
coverageScript.png
cyclopsProject.png
cyclopsScript.png
debugProject.png
debugScript.png
designer.png,apps/designer.png
diffFiles.png
dirClosed.png,filesystems/folder.png
dirOpen.png,filesystems/folder_open.png
editBookmarks.png
editComment.png
editCopy.png,actions/editcopy.png
editCut.png,actions/editcut.png
editDelete.png,actions/editdelete.png
editIndent.png,actions/text_right.png
editPaste.png,actions/editpaste.png
editRedo.png,actions/redo.png
editUncomment.png
editUndo.png,actions/undo.png
editUnindent.png,actions/text_left.png
empty.png
endline.png
eric.png
exceptions.png
exit.png,actions/exit.png
exportShortcuts.png,actions/fileexport.png
fileDesigner.png
fileIDL.png
fileLinguist.png
fileMisc.png,mimetypes/misc.png
fileModified.png
filePixmap.png,mimetypes/image.png
fileProject.png,mimetypes/resource.png
filePython.png,mimetypes/source_py.png
filePython2.png
fileSave.png,actions/filesave.png
fileSaveAll.png,actions/save_all.png
fileSaveAs.png,actions/filesaveas.png
fileSaveProject.png
fileShortcuts.png
find.png,actions/find.png
findNext.png,actions/next.png
forward.png,actions/forward.png
globalVariables.png
goto.png,actions/goto.png
gotoBrace.png
group.png
home.png,filesystems/folder_home.png
importShortcuts.png,actions/fileimport.png
linguist.png
localVariables.png
method.png
namedgroup.png
namedreference.png
neglookahead.png
neglookbehind.png
new.png,actions/filenew.png
newWindow.png,actions/window_new.png
nongroup.png
nonwordboundary.png
open.png,actions/fileopen.png
poslookahead.png
poslookbehind.png
print.png,actions/fileprint.png
profileProject.png
profileScript.png
projectClose.png
projectFind.png
projectForms.png
projectInterfaces.png
projectNew.png,actions/folder_new.png
projectOpen.png
projectOthers.png
projectProps.png
projectSave.png
projectSaveAs.png
projectSources.png
projectTranslations.png
reload.png,actions/reload.png
remsplitHorizontal.png
remsplitVertical.png
repeat.png
restart.png,actions/reload.png
runProject.png
runScript.png,actions/run.png
shell.png,actions/openterm.png
splitHorizontal.png,actions/view_top_bottom.png
splitVertical.png,actions/view_left_right.png
step.png
stepOut.png
stepOver.png
stepQuit.png
syntaxError.png
syntaxErrorGoto.png
tBreak.png
tCBreak.png
toolsConfigure.png,actions/configure_toolbars.png
unhighlight.png
unittest.png
unittestProject.png
unittestRestart.png
unittestScript.png
whatsThis.png,actions/contexthelp.png
wordboundary.png
zoomIn.png,actions/viewmag+.png
zoomOut.png,actions/viewmag-.png
zoomTo.png,actions/viewmag.png
"""


def mk_icon_mapping():
    """ returns a list [ericicon, kdeicon] pairs from the icon mapping string 

    """
    strlines = icon_mapping.strip().split()
    strpairs = [line.split(',') for line in strlines]
    return strpairs


def mk_icon_symlinks(items, kde_dir):
    """ creates symlinks from the kde d

    """
    for item in items:
        try:
            eric_name, source_part = item
        except (ValueError, ):
            pass
        else:
            kde_source = os.path.join(kde_dir, source_part)
            if os.path.exists(kde_source):
                try:
                    os.symlink(kde_source, eric_name)
                except (OSError, ), ex:
                    print 'Unable to link file ', ex, eric_name


def get_kde_icon_dir(theme_dir, icon_size):
    """ runs kde-config to find the kde icon path

    (PyKDE can do this but not all Eric3 users have PyKDE (but they should))
    """
    dirs = os.popen('kde-config --path icon').readline().strip()
    for icon_dir in dirs.split(':'):
        theme_path = os.path.join(icon_dir, theme_dir, icon_size)
        if os.path.exists(theme_path):
            return theme_path


if __name__ == '__main__':
    try:
        icon_theme_name = sys.argv[1]
    except (IndexError, ):
        scr_name = sys.argv[0]
        print 'Usage:\n\t%s icon_theme [icon_size]\n' % (scr_name, )
        print 'Example:\n\t%s sparkling 16x16\n' % (scr_name, )
        sys.exit(1)

    try:
        icon_size = sys.argv[2]
    except (IndexError, ):
        icon_size = '16x16'

    print 'Using KDE icon directory name %s, icon size %s.' % \
            (icon_theme_name, icon_size)
    icon_theme_dir = get_kde_icon_dir(icon_theme_name, icon_size)
    mk_icon_symlinks(items=mk_icon_mapping(), kde_dir=icon_theme_dir)
    print 'Done.  Configure Eric3 to use this directory for its icons.'
