#!/usr/bin/env python # hendry at iki.fi # GPL import md5, os from baseconvert import baseconvert class uristore: """ For storing URLs on the filesystem """ def __init__(self, savedir=".r"): os.umask(000) self.savedir = os.path.abspath(savedir) def __getitem__(self, key): d = os.path.normpath(self.savedir+'/'+'/'.join(list(key))) #print d if os.path.exists(d): return open(d+"/URI").read().strip() return None def store(self, uri): BASE36 = "abcdefghijklmnopqrstuvwxyz0123456789" BASE67 = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789$+-:_" #print uri # uri given, key returned m = md5.new(uri).hexdigest() surink = baseconvert(m,BASE36,BASE67) j = '' for i in surink: j = j+i if self[j]: if self[j] == uri: # already stored return j else: # nothing there, so we store d = os.path.normpath(self.savedir+'/'+'/'.join(list(j))) #print "creating ", d os.mkdir(d) f = open(d+'/URI', 'w') f.write(uri) f.close() return j if __name__ == "__main__": f = Uristore() import sys a = f.store(sys.argv[1]) print a print f[a]