#!/usr/bin/env python

# filesystem_plugin_test.py

# Copyright (C) 2005-2006 Visual Media FX Ltd.
# Released under the LGPL.
# For more information, see http://www.openlibraries.org.

# unit test to enumerate all available plugins

import platform
import sys
if platform.system( ) == "Linux":
	import dl
	sys.setdlopenflags( dl.RTLD_NOW | dl.RTLD_GLOBAL )

import openpluginlib
import openimagelib
import openassetlib


#for arg in sys.argv:
#	print arg
#	openpluginlib.init(arg)

	
def print_asset_content( asset ):
	print "\n\tGUID: ", asset.guid()
	if asset.size() == 0:
		print "Contains no metadata"
	else:
		print "\t", asset.size(), " associated pieces of metadata"
		for metadata in asset:
			print "\t\t", metadata.first, ": ", metadata.second

def print_set_content( set ):
	if set.size() == 0:
		print "No Hits!"
	else:
		print "Got ", set.size(), " hits:"
		for a in set:
			print_asset_content(a)

def print_is_convertible( asset ):
	if openassetlib.is_convertible(asset) == True:
		print "asset is convertible"
	else:
		print "asset isn't convertible"
 	
# import the uuid component		
# [Got from http://home.arcor.de/jurner/python/uuid/dist/uuid-0.3.2.tar.gz]
import uuid

# test it works
print uuid.uuid_time()

# define guid generator class acceptable to oal
class uuid_generator(openassetlib.guid_generator):
	def generate_guid(self):
		return uuid.uuid_time()
		
# instantiate it
uuid_gen = uuid_generator()

# initialiaze oal
openassetlib.init(uuid_gen)

# test the guid generator
print openassetlib.generate_guid()	

asset = openassetlib.create_asset()
print asset
print_is_convertible(asset)
print "Does the asset contain any metadata: ", asset.empty()
	
asset.guid(openassetlib.generate_guid())
print "Adding metadata..."
asset.set("a_flag", True)
asset.set("a_string", "Hello :-)")
asset.set("a_number", 1234567890)
print asset.guid()
print "a_flag (", asset.count("a_flag"), "): ", asset.get("a_flag")
print "a_string (", asset.count("a_string"), "): ", asset.get("a_string")
print "a_number (", asset.count("a_number"), "): ", asset.get("a_number")
print "Now does the asset contain any metadata: ", asset.empty()

asset2 = openassetlib.create_asset(asset)
print asset2
print_is_convertible(asset2)
	
asset2.guid(openassetlib.generate_guid())
asset2.set("a_flag", False)
asset2.set("a_string", "pudding")
asset2.set("a_number", 54321)
print asset2.guid()
print "a_flag (", asset2.count("a_flag"), "): ", asset2.get("a_flag")
print "a_string (", asset2.count("a_string"), "): ", asset2.get("a_string")
print "a_number (", asset2.count("a_number"), "): ", asset2.get("a_number")

# test6 the find functionality on the asset interface
print asset2.find("a_string").second

############################################################################
#------------TEST Set interface
#
print "creating new set"
set = openassetlib.create_set()
print set
print "Is it empty?: ", set.empty()
print "pushing 2 onto back..."
print "set size = ", set.size()
set.push_back(asset)
print "set size = ", set.size()
print "Is it empty?: ", set.empty()
set.push_back(asset2)
print "set size = ", set.size()
print_set_content(set)

print "popping back..."
set.pop_back()
print "set size = ", set.size()
print_set_content(set)

print "Adding popped one back and removing first asset..."
set.push_back(asset2)
print "set size = ", set.size()
set.remove(asset)
print_set_content(set)

print "inserting removed on back to offset 1..."
set.insert(asset, 1)
print "set size = ", set.size()
print_set_content(set)

print "Creating another set from the first..."
another_set = openassetlib.create_set(set)
print "set size = ", another_set.size()
print_set_content(another_set)

print "Testing iterator: print by printing out all guids"
for a in another_set:
	print "GUID: ", a.guid()
	
print "Seeing whats at offset 0..."
print another_set.at(0).guid()

print "Seeing whats at offset 1..."
print another_set.at(1).guid()


print "clearing content of set..."
another_set.clear()
print "set size = ", another_set.size()
print_set_content(another_set)

print "Creating yet another set..."
yet_another_set = openassetlib.create_set(asset2)
print_set_content(yet_another_set)

####################################################################################
#----------- TEST Searches
#

x = openpluginlib.discovery( openpluginlib.all_query_traits( "openassetlib", "", "filesystem", 0 ) )
if x.size() == 0:
	print "No plugins discovered!"
else:
	for y in x:
		print y.libname(), ':', y.name(), y.merit()
 		plugin = y.create_plugin("")
 		print plugin
 		if plugin is None:
 			print "failed to create"
 		else:
 			print "created successfully!  :-)"
 			break
	print "Searching for .png files in c:\\media\\images (non-recursive)"
	
	set1 = openassetlib.search(plugin, ".*\.png", "c:\\Media\\Images", False)
	print set1
	print_set_content(set1)
	
	print "Searching for .png files in c:\\media\\images (recursive)"
	set2 = openassetlib.search(plugin, ".*\.png", "c:\\Media\\Images", True)
	print set2
	print_set_content(set2)
	
	print "finding the asset with the guid at the 3rd offset..."
	asset_at_offset = set2.at(3)
	asset_at_offset_guid = asset_at_offset.guid()
	found_asset = set2.find(asset_at_offset_guid)
	print_asset_content(found_asset)
	
	print "Searching last set for all *logo*.png's files in a folder *png*"
	set_search_result_set = openassetlib.search(set2, ".*png.*", ".*logo.*\.png");
	print set_search_result_set
	print_set_content(set_search_result_set)
	
	print "Searching the same set for all *logo*.png's files in a folder *png* and whose filesize < 20000"
	set_search_result_set = openassetlib.search(set2, ".*png.*", ".*logo.*\.png", "<", 20000);
	print set_search_result_set
	print_set_content(set_search_result_set)
	
	print "Searching for girl.bmp in c:\\media\\images (recursive)"
	set3 = openassetlib.search(plugin, "girl.bmp", "c:\\Media\\Images", True)
	print set3
	print_set_content(set3)

	print "Converting asset to an oil image object"
	if set3.empty() == True:
		print "No asset to convert to image object - set is empty"
	else:
		image = openassetlib.convert_to_image(set3.at(0))
		print image
	
	print "Searching for girl.bmp in c:\\media\\images (non-recursive)"
	set4 = openassetlib.search(plugin, "girl.bmp", "c:\\Media\\Images", False)
	print set4
	print_set_content(set4)
	
	print "Searching for all assets > 3000000 bytes long in c:\\media (recursive)"
	set5 = openassetlib.search(plugin, ".*", "c:\\Media", True, ">", 3000000)
	print set5
	print_set_content(set5)
		
#	print "Searching the set for files in c:\\media folder"
#	set_search_result_set = openassetlib.search(set5, "location", "==", "c:/Media")
#	set_search_result_set = openassetlib.search(set5, "location", "==", "c:\\Media")
#	print set_search_result_set
#	print_set_content(set_search_result_set)
	
	set5.at(0).set("a_flag", False)
	set5.at(1).set("a_flag", True)
	set5.at(2).set("a_number", int(1234567890))
	set5.at(3).set("a_number", int(54321))
#	set_search_result_set = openassetlib.search(set5, "a_flag", "==", False)
#	print set_search_result_set
#	print_set_content(set_search_result_set)
#	set_search_result_set = openassetlib.search(set5, "a_flag", "==", True)
#	print set_search_result_set
#	print_set_content(set_search_result_set)

	print "HERE GOES!!...."
	set_search_result_set = openassetlib.search(set5, "a_number", "<", int(60000))
	print set_search_result_set
	print_set_content(set_search_result_set)
		
	print "Searching the set for files with a filesize of 11501568"
	set_search_result_set = openassetlib.search(set5, "filesize", "==", "11501568")
	print set_search_result_set
	print_set_content(set_search_result_set)

	print "Searching for all assets modified after 2005/10/1 00:00:00 in c:\\media (non-recursive)"
	datetime = openassetlib.tm
	datetime.tm_year = 2006-1900
	datetime.tm_mon = 7
	datetime.tm_mday = 4
	datetime.tm_hour = 12
	datetime.tm_min = 0
	datetime.tm_sec = 0
	print datetime.tm_year + 1900, datetime.tm_mon, datetime.tm_mday, " ", datetime.tm_hour, ":", datetime.tm_min, ":", datetime.tm_sec
#	set6 = openassetlib.search(plugin, ".*", "c:\\Media", False, "<", datetime)
#	print set6
#	print_set_content(set6)
	
	##########################################################################
	#---------- TEST Van Diagram calcs
	#
	print "Intersection of last 2 sets:"
	set7 = openassetlib.calculate_intersection(set1, set2)
	print set7
	print_set_content(set7)

	print "Difference between same 2 sets:"
	set8 = openassetlib.calculate_difference(set1, set2)
	print set8
	print_set_content(set8)

	print "Union of same 2 sets:"
	set9 = openassetlib.calculate_union(set1, set2)
	print set9
	print_set_content(set9)

	
	openassetlib.uninit()
	openpluginlib.uninit()
	