############################################################################
#    Copyright (C) 2005 by Manuel Amador (Rudd-O)                          #
#    rudd-o@amautacorp.com                                                 #
#                                                                          #
#    This program is free software; you can redistribute it and#or modify  #
#    it under the terms of the GNU General Public License as published by  #
#    the Free Software Foundation; either version 2 of the License, or     #
#    (at your option) any later version.                                   #
#                                                                          #
#    This program is distributed in the hope that it will be useful,       #
#    but WITHOUT ANY WARRANTY; without even the implied warranty of        #
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         #
#    GNU General Public License for more details.                          #
#                                                                          #
#    You should have received a copy of the GNU General Public License     #
#    along with this program; if not, write to the                         #
#    Free Software Foundation, Inc.,                                       #
#    59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             #
############################################################################

# FIXME implement an interface that allows the indexer to compare in-store file objects' interface versions with its corresponding plugin"s interface version, to provide for automatic schema migration.  Bump the interface number on your plugin, and pof all files are automatically reindexed when they are modified (or perhaps upon startup/reload)
# FIXME implement an observer pattern so that the index database detects when the plugins are reloaded, suspend database access to client classes, run __prepare_indexes and refresh objects whose interfaces have changed (ay que pensar bien el orden de mierda de estas operaciones para que no se vaya todo a la verga"), 

import sys
import os
import glob
import imp
import commands
from MetadataService import common

from MetadataService.InfospaceAgents.Filesystem.platform import is_executable

if not is_executable("extract2"): raise ImportError, "libextractor is not available"

def get_metadata(filename):
		""" Returns a dictionary with the metadata keys and values for the file.  All strings are platform-encoded. """
		command = "extract %s"%common.shell_quote(filename)
		output = commands.getoutput(command)
		output = output.splitlines()
		returned = {}
		for line in output:
			try: key,value = line.split("-",2)
			except: continue
			returned[key.strip()] = value.strip()
		return returned

# fixme subpar implementation
def get_mimetype(filename):
		metadata = get_metadata(filename)
		if metadata.has_key("mimetype"): return metadata["mimetype"]