#!/usr/bin/python
import os, sys, string
import subprocess

def getPythonSrcCode (inputFileName):

	inputName = string.lower(inputFileName[:-len(os.path.splitext(inputFileName)[1])])
	inputName = inputName.replace(" ", "_")
	for char in "()!?,.;:#$%&^|{}[]`'\"<>~":
		inputName = inputName.replace(char, "")

	cmd = "gdk-pixbuf-csource --raw --name=" + inputName + " " + inputFileName

	p = subprocess.Popen(cmd, shell=True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, 
						 stderr=subprocess.STDOUT, close_fds=True) 
	(child_stdin, child_stdout, child_stderr) = (p.stdin, p.stdout, p.stderr)

	while True:
		line = child_stdout.readline()
		if (line[0] == "{"):
			break

	outputString = child_stdout.read()
	outputString = outputString.replace("/*", "#")
	outputString = outputString.replace("*/", "")
	outputString = outputString.replace("};", ")")

	return inputName + " = (\n  \"\"\n" + outputString

def main(argv = sys.argv):
	executablePaths = [ "/usr/bin", "/usr/local/bin" ]
	foundTool = False
	for path in executablePaths:
		if (os.path.exists(path + "/gdk-pixbuf-csource")):
			foundTool = True;
	if (foundTool == False):
		print "ERROR: gdk-pixbuf-csource is not installed!"
		sys.exit()
	if argv[1:]:
		fileName = argv[1:][0]
		if (os.path.isfile(fileName) == False):
			print "ERROR: file does not exits!"
		else:
			print getPythonSrcCode(fileName)
	else:
		print "Usage: gdk-pixbuf-pysource.py <icon.png>"

if __name__ == '__main__': main()


# 08.05.2010
# pasp@users.sourceforge.net

