import ConfigParser
import hashlib
import os
from normalize import normalize_path, remove_arguments

class Service:
	name = None
	imagepath = None
	imagepath_md5 = None
	imagepath_normal = None
	imagepath_normal_md5 = None

	def __init__(self, init_name):
		self.name = init_name
		
	def output(self):
		print "[%s]\n%s:%s\n%s:%s\n" % (self.name, self.imagepath, self.imagepath_md5, self.imagepath_normal, self.imagepath_normal_md5)
	
def import_service_list(filename):
	services = []
	config = ConfigParser.RawConfigParser()
	config.read(filename)
	
	for name in config.sections():
		s = Service(name)
		s.imagepath = config.get(name, "ImagePath")
		services.append(s)
		
	return services
	
def md5(filename):
	if(filename != None and os.path.exists(filename)):
		file = open(filename, 'rb').read()
		return hashlib.md5(file).hexdigest()
	return None
	
# this uses the path as returned by ConfigParser
def calculate_md5s_broken(services):
	for s in services:
		s.imagepath_md5 = md5(s.imagepath)

# this uses the path as processed by normalized_path
def calculate_md5s_works(services):
	for s in services:
		s.imagepath_normal_md5 = md5(s.imagepath_normal)
		
def print_md5s(services):
	for s in services:
		s.output()
		
def print_services(services):
	for s in services:
		print s.imagepath
		print repr(s.imagepath)
		print

def normalize_service_paths(services):
	for s in services:
		s.imagepath_normal = normalize_path(s.imagepath)

def remove_service_arguments(services):
	for s in services:
		s.imagepath_normal = remove_arguments(s.imagepath_normal)
		
if __name__ == "__main__":
	services = import_service_list("services.ini")
	
	normalize_service_paths(services)
	remove_service_arguments(services)
	
	calculate_md5s_broken(services)
	calculate_md5s_works(services)
	
	print_services(services)
	
	print "-----------------------------"
	
	print_md5s(services)