ImageCloud funtioning again

I spent a little bit of time hacking at ImageCloud after math class. The code is working again, but it’s ugly on Windows. The Strings that have the filenames do \\ on windows. I think this is because it needs to escape the slash, but it is still ugly. I knew I should have ignored Windows… 😉

 #!/usr/bin/env python
 # -*- coding: iso-8859-1 -*-

 # Program : ImageCloud - A Python Photo TaggerDB
 # Author  : Kristofer White
 # E-Mail  : kristoferwhite@gmail.com


 ## Import Modules And Such ##
 import os, sys, glob, pickle

 class iCloud(object):

 	def prompt(self, tagDB):
 		print "Do you want to [B]rowse for, [A]dd images, or [Q]uit?"
 		ans = raw_input("[B/A/Q] ?")
 		if ans.lower() == "b":
 			Browse.dbQuery(Browse())
 		elif ans.lower() == "a":
 			Add()
 		elif ans.lower() == "q":
 			self.dbPickle(tagDB)
 			sys.exit(0)
 		else:
 			print "Unrecognized response! Try again"
 			self.prompt(tagDB)
 	def dbPickle(self, tagDB):
 		# it is possible to do:
 		# tagDB = pickle.dump(tagDB, os.path.expanduser("~/.imagecloudDB"))
 		fout = open(os.path.expanduser("~/.imagecloudDB"), "w")
 		pickle.dump(tagDB, fout)
 		fout.close()
 	def dbUnPickle(self):
 		# it is possible to do:
 		# tagDB = pickle.load(os.path.expanduser("~/.imagecloudDB"))
 		fin = open(os.path.expanduser("~/.imagecloudDB"), "r")
 		tagDB = pickle.load(fin)
 		fin.close()
 		return tagDB



 class Browse(object):

 	def dbQuery(self):
 		""" Search Gallery for Tag """
 		tagDB = iCloud.dbUnPickle(iCloud())
 		tag = raw_input("That is the tag that you wish to search for? > ")
 		if tag in tagDB:
 			print tagDB[tag].split("||")
 			iCloud.prompt(iCloud(), tagDB)
 		else:
 			print "Sorry, tag not found!"
 			self.dbQuery()
 	def tagCloudGen(self):
 		""" Generate the Tag Cloud """



 class Add(object):

 	def __init__(self):
 		""" Ask for image dir """
 		path = raw_input("Path ?")
 		self.folderAdd(os.path.abspath(os.path.expanduser(path)))
 	def folderAdd(self, path):
 		""" Add folder passed via path """
 		filelist = glob.glob("%s/*.[Jj][Pp][Gg]" % path)
 		filelist.extend(glob.glob("%s/*.[Pp][Nn][Gg]" % path))
 		filelist.extend(glob.glob("%s/*.[Gg][Ii][Ff]" % path))
 		self.tagger(filelist)
 	def tagger(self, filelist):
 		""" Tag files from filelist and pickle """
 		tagDB = iCloud.dbUnPickle(iCloud())
 		print "Seperate tags by spaces!"
 		for filename in filelist:
 			taglist = raw_input("Tags for %s ?n" % filename).split()
 			for tag in taglist:
 				if tag in tagDB:
 					tagDB[tag] = "%s||%s" % (tagDB[tag], os.path.abspath(filename))
 				else:
 					tagDB[tag] = os.path.abspath(filename)
 		iCloud.dbPickle(iCloud(), tagDB)
 		print tagDB
 		iCloud.prompt(iCloud(), tagDB)


 if __name__ == "__main__":

 	try:
 		fin = open(os.path.expanduser("~/.imagecloudDB"), "r")
 		tagDB = pickle.load(fin)
 		fin.close()
 	except IOError:
 		tagDB = {}
 		iCloud.dbPickle(iCloud(), tagDB)
 	c= iCloud()
 	c.prompt(tagDB)

technorati tags:, , ,

Advertisements
Explore posts in the same categories: ImageCloud, python

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s


%d bloggers like this: