Fluxmarks
Det här verkar fungera, men jag känner mig inte tillräckligt säker på Python för att säga ifall det är rätt. Känner ni att något särskilt bör förbättras eller bara vill ge något tips skriv en kommentar! =)
Fluxmarks genererar en menyfil för fluxbox med en submeny-entry utifrån den bookmarkfil som anges i fluxmarks.conf (som skall läggas under .fluxbox-katalogen i din hemkatalog).
fluxmarks.conf-exempel:
bookmarks = ~/.mozilla/firefox/zkqx52ug.default/bookmarks.html
menu = ~/.fluxbox/fluxmarks
browser = /usr/bin/firefox-bin
Meny-filen som genereras (menu i config-filen) måste sedan inkluderas i fluxbox huvudmenyfil , detta görs genom att man lägger till raden ”[include] (~/.fluxbox/fluxmarks)”.
programmet kan köras som demon ifall argumentet –daemon anges vid uppstart, i demon läge uppdateras menyfilen en gång i minuten (ifall något har skrivits till bookmark-filen). I normalt läge uppdateras menyfilen och programmet avslutas.
Källkod:
# #
# Fluxmarks v 0.1 #
# #
# Fluxmarks is a script for parsing netscape #
# bookmark files (1.0) (used in for example #
# Mozilla Firefox) and creating a fluxbox #
# menu file for the bookmarks, but more #
# importantly it's a project to teach myself #
# Python. (please don't slam me too much for #
# my mistakes. #
# #
# Author: Ã…ke Forslund #
# #
# Licence: Not sure, an open licence with #
# credit to me #
# #
# I hope this may be of use to someone #
# besides me #
# #
# #
# TODO: #
# * translation of html-coded chars #
# * Support for more browsers #
# (Opera, Safari, etc) #
# * configurable cutoff length of menu-titles #
# #
import sys, os
import re
import time
#Parse bookmark file and generate menu
def parseBookmarks(bookmarkPath, menuPath, program):
#open the bookmark file and the menu-file
bf = open(bookmarkPath, "r")
mf = open(menuPath, "rw+")
#make a submenu for the Fluxmarks
mf.write("[submenu] (Fluxmarks)\n")
#parse the bookmark file
line = bf.readline(4096)
print line #debug output
while(line != ""):
if(line.__contains__("<H3")): #New submenu
#Get name of subdir
matchObj = re.search("<H3.*", line)
try:
matchObj = re.search(">.*<", matchObj.group())
except: #We really would like to know if the regexp fails even though it shouldn't
print "[!!]\tRegexp failed!"
sys.exit()
mString = matchObj.group()
wrtString = "[submenu] (" + mString[1:len(mString) - 1] + ")\n"
mf.write(wrtString)
if(line.__contains__("A HREF")): #New address entry
#get bookmark info
#First the address
matchObj = re.search("=\".*?\"", line)
address = matchObj.group()[2:len(matchObj.group()) - 1]
#Then the title
matchObj = re.search("<A HREF.*",line)
parts = re.split("<.*?>", matchObj.group())
if(len(parts) > 1):
title = parts[1]
if(len(title) > cutOff): #If title length is too long
title = title[0:cutOff - 3] + "..." #Shorten it
wrtString = "[exec] (" + title + ") {" + program + " " + address + "}\n"
mf.write(wrtString)
else:
print "[!!]\tRegexp Failed!: " + matchObj.group()
if(line.__contains__("</DL>")):
#End of a directory, go up one level
mf.write("[end]\n")
line = bf.readline(16000)
print line
bf.close()
mf.close()
return
def main():
#defaults
bookmarkfile = ""
menufile = os.path.expanduser("~/.fluxbox/fluxmarks")
program = "firefox-bin"
#parse config file
configFile = open(os.path.expanduser("~/.fluxbox/fluxmarks.conf"),'r')
configLine = configFile.readline(1024)
while(configLine != ""):
configItems = re.split("\s*\=\s*", configLine)
for i in range(len(configItems)):
configItems[i] = re.sub("\A\s*|\s*\Z", "", configItems[i])
#Replaces whitespaces at the beginning and the end of the substrings
#check for valid config variables
if(configItems[0] == "bookmarks"):
bookmarkFile = configItems[1]
if(configItems[0] == "menu"):
menuFile = configItems[1]
if(configItems[0] == "browser"):
program = configItems[1]
if(configItems[0] == "cutoff"):
cutOff = configItems[1]
configLine = configFile.readline(1024)
configFile.close()
print "bookmarkFile = " + bookmarkFile
print "menuFile = " + menuFile
print "browser = " + program
#before we continue we make sure that the path is absolute
bookmarkFile = os.path.expanduser(bookmarkFile)
menuFile = os.path.expanduser(menuFile)
program = os.path.expanduser(program)
oldCTime = 0
while(1):
newCTime = os.stat(bookmarkFile)[9]
if(newCTime != oldCTime):
parseBookmarks(bookmarkFile, menuFile, program)
oldCTime = newCTime
if(daemonize == 0): #The process is not running in daemon mode
return #Exit
time.sleep(10)
return
#Execution starts here
#global variables
cutOff = 32
daemonize = 0
#parse commandline
if(len(sys.argv) > 1):
for i in range(1,len(sys.argv)):
if(sys.argv[1] == "--daemon"): #should we run in daemon mode
daemonize = 1
#Execution starts here
if(daemonize == 1):
try:
pid = os.fork()
if(pid != 0):
#Oh dear I am a parent I'll better return to the shell
sys.exit()
except OSError, e:
print >>sys.stderr, "fork failed: %d (%s)", e.errno, e.strerror
sys.exit()
#decouple from parent environment
#os.chdir("/")
#os.setsid()
os.umask(0)
#launch main function
main()


juli 24, 2008 kl 13:08 |
[...] Fluxmarks: För att lära mig lite mer om Pythons reguljära uttryck skrev jag det här programmet som tolkar bookmarks-filer (Netscape v 1.0) från till exempel firefox och genererar en meny-fil som enkelt kan integreras i fluxbox huvudmeny-fil. [...]