import os import re import stat import time import urllib import Image images = [] debug = 1 def select(query): lookupfs = [] if 'tag' in query: for i in query['tag']: lookupfs.append(picturerep[i]) if not lookupfs: # if no picture repositories specifically asked for, we'll just assume all. lookupfs = picturerep.values() if debug: print lookupfs for i in lookupfs: add_dir(i) if debug: print images for i in images[:]: year, month, day, hour, min, sec, wday, yday, isdst = time.localtime(os.stat(i)[stat.ST_MTIME]) if 'year' in query: if int(query["year"]) != year: if debug: print i, " year:", year, "not in query:", query["year"] images.remove(i) continue if 'month' in query: if int(query["month"]) != int(month): if debug: print i, " month:", month, "not in query:", query["month"] images.remove(i) continue if 'day' in query: if int(query["day"]) != day: if debug: print i, " day:", day, "not in query:", query["day"] images.remove(i) continue if 'time' in query: qhour, qmin, qsec = query["time"].split(':') if int(qhour) != hour or int(qmin) != min or int(qsec) != sec: if debug: print i, " hour min sec:", hour, min, sec, "not in query:", query["time"] images.remove(i) continue return images def add_dir(dir, traverse=0): filenames = os.listdir(dir) #filenames.sort() subdirs = [] for filename in filenames: pathname = os.path.join(dir, filename) if VALID_PICTURE(filename): images.append(pathname) if os.path.isdir(pathname) and traverse: subdirs.append(pathname) map(add_dir, subdirs) def VALID_PICTURE(name): if re.search("\.(png|jpg)$", name, re.I): return 1 if __name__ == '__main__': rep = {'bab': '/home/hendry/pictures/bab'} #print pix(rep, {'tag': ['bab'], 'month': 4, 'time': '17:52:01', 'day': 17, 'year': 2004}) print pix(rep, {'month': 4, 'time': '17:52:01', 'day': 17, 'year': 2004}) #print pix(rep, {'tag': ['kodak'], 'month': 6})