You are not logged in Log in Join
You are here: Home » Members » shalabh » bookmarks.py » View File

Log in
Name

Password

 

bookmarks.py

File details
Size
2 K
File type
text/plain

File contents

import os.path
import re
import string
import time
import cPickle

lasttime=0
all_bookmarks = []

def visit(arg, dirname, names):
	global all_bookmarks
	baselevel=arg[0]
	hidelist=arg[1]
	expandlist=arg[2]
	
	#print "\n","-"*50,"\n"
	#print "Processing:",dirname,
	levels = string.split(dirname,"\\")
	thislevel=len(levels)
	level = thislevel-baselevel
	#print "(Level:",level,")"
	if level==0: return
	
	thisfolder=string.join(levels[-level:],".")
	for hidden in hidelist:
		if string.find(string.upper(thisfolder),string.upper(hidden))==0: return
	for folder in expandlist:
		
		pattern = re.compile(folder+"(\.|)")
		mo = pattern.search(thisfolder)
		if mo:
			thisfolder=re.sub(mo.re,"",thisfolder,1)
			if thisfolder=="": return
			level=level- string.count(folder,".")-1
			

	bookmarks=[]
	for name in names:
		mo = re.match(r"(.+?)\.url",name)
		if mo:
			title=mo.groups()[0]
			fullpath=os.path.join(dirname,name)
			infile=open(fullpath,"r")
			line=infile.readlines()[1]
			mo2 = re.match(r"BASEURL=(.+)$",line)
			if mo2:
				url = mo2.groups()[0]
				bookmarks.append((title,url))
				
	all_bookmarks.append((thisfolder, bookmarks, level))

	

def getbookmarks(self, gettime=0):
	filename = "bookmarks.pickle"
	global all_bookmarks
	if os.path.exists(filename):
		lasttime = os.stat(filename)[8]
		if (gettime==1): return time.ctime(lasttime)
		if not self.REQUEST.has_key('force'):
			#the next line prevents an entire recreation of bookmars unless current list is too old
			if (time.time() - lasttime)< (60*60*12): #too old = 12 hrs
				infofile = open(filename,"r")
				try:
					all_bookmarks = cPickle.load(infofile)
					infofile.close()
					if len(all_bookmarks)>0:
						return all_bookmarks
				except:
					pass
				
	all_bookmarks=[]
	
	basedir = "c:\\winnt\\profiles\\shalabh\\favorites\\links"
	baselevel=len(string.split(basedir,"\\"))
	hidelist=["misc.lab","misc.products","News"]
	expandlist=["InfoArchives","Misc"]
	
	os.path.walk(basedir,visit,(baselevel,hidelist,expandlist))

	infofile = open(filename,"w")
	cPickle.dump(all_bookmarks,infofile)
	infofile.close()
	
	if gettime==1:
		return time.ctime(time.time())
	else:
		return all_bookmarks
	
	#print all_bookmarks
	
	#print "<h2>Categories</h2>"
	
	#for folder in all_bookmarks:
		#print '<br><a href="#'+folder[0]+'">',folder[0],'</a>'
		
	#print "<h2>Bookmarks</h2>"
	#for folder in all_bookmarks:
		#print '<a name="'+folder[0]+'"> &nbsp; </a>'
		#print '<p><b>',folder[0],"</b><br>"
		#for bookmark in folder[1]:
		#	print '<a href="'+bookmark[1]+'">',bookmark[0],'</a><br>'


def test(self):
	a = [('a','aa','bb',),('b','bb','cc')]
	return a