#!/usr/bin/env python

###############################################################################
#
# Copyright (C) 2010 Karl Ostmo
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
###############################################################################
#
# Author: Karl Ostmo
# Date: Feb. 17, 2010
# Version: 1.0
# Description: Produces a histogram plot of orders from Google Checkout data
#
###############################################################################

import os
GTHUMB_COLLECTIONS_DIRECTORY = "~/.gnome2/gthumb/collections"

# NOTE: This script is designed for gThumb pre-2.11.  Version 2.11 uses an XML storage format.
# =============================================================================

def addQuotes(some_string):

	return "\"" + some_string + "\""

	'''
	try:
		return "\"" + unicode(some_string, "utf-8") + "\""
	except Exception, e:
		print "Bad string:", some_string
		raise e
	'''

# =============================================================================
def make_library(gthumb_library_name):
	import os, shutil
	gthumb_library_path = os.path.join(os.path.expanduser(GTHUMB_COLLECTIONS_DIRECTORY), gthumb_library_name)
	if os.path.exists(gthumb_library_path):
		shutil.rmtree(gthumb_library_path)
	os.mkdir(gthumb_library_path)

	return gthumb_library_path

# =============================================================================

def write_catalog(image_paths, collection_name, library_path=None):

	import os
	collection_path = collection_name + ".gqv"
	if library_path:
		collection_path = os.path.join(library_path, collection_path)

	f = open(collection_path, "w")
	
	import urllib


	f.write( "# sort: manual\n" )	# Required to prevent auto-sorting of list (ommiting this or specifying "none" doesn't work)
	for path in image_paths:
		f.write( addQuotes( urllib.quote( os.path.abspath(path) ) ) + "\n" )
#	f.write( "#end" )
	f.close()
