[XML-SIG] Useless fun thing for XML - comments or helpers?
Stefane Fermigier
fermigie@math.jussieu.fr
Thu, 3 Sep 1998 18:27:24 +0200
On Thu, Sep 03, 1998 at 11:28:02AM +0100, Fredrik Lundh wrote:
>
> Here's a first stab. This is tested with MSIE 5.0 on a Swedish NT installation
> (so you definitely need to change the directory to run it -- a production version
> should of course use the registry to find out where the directory is located).
Similar program using pydom:
import os, string
from xml.dom.writer import XmlWriter
from xml.dom.core import *
#
ROOT_DIR = 'favorites' # Fix this on your machine, I don't have NT.
dom_factory = DOMFactory()
class Name(Element):
def __init__(self, name):
Element.__init__(self, 'Name')
self.appendChild(dom_factory.createTextNode(name))
class Url(Element):
def __init__(self, url):
Element.__init__(self, 'Url')
self.appendChild(dom_factory.createTextNode(url))
class Folder(Element):
def __init__(self, folder_name):
Element.__init__(self, 'Folder')
self.appendChild(Name(folder_name))
class Bookmark(Element):
def __init__(self, name, url):
Element.__init__(self, 'Bookmark')
self.appendChild(Name(name))
self.appendChild(Url(url))
# One could also use the factory everywhere instead of defining these
# classes.
# This class is almost untouched from Frederik's version.
class MSIE:
def __init__(self):
self.root = Folder('')
self.path = ROOT_DIR # Fix this if you're on Windows.
self.__walk(self.root)
def __walk(self, this, subpath=[]):
# traverse favourites folder
path = os.path.join(self.path, string.join(subpath, os.sep))
for file_name in os.listdir(path):
fullname = os.path.join(path, file_name)
if os.path.isdir(fullname):
node = Folder(file_name)
this.appendChild(node)
self.__walk(node, subpath + [file])
else:
url = self.__geturl(fullname)
if url:
this.appendChild(Bookmark(os.path.splitext(file_name)[0], url))
def __geturl(self, file):
try:
fp = open(file)
if fp.readline() != "[InternetShortcut]\n":
return None
while 1:
s = fp.readline()
if not s:
break
if s[:4] == "URL=":
return s[4:-1]
except IOError:
pass
return None
bookmarks = MSIE()
writer = XmlWriter()
writer.newline_after_start = ['Folder', 'Bookmark']
writer.newline_after_end = ['Name', 'Url', 'Folder', 'Bookmark']
writer.write(bookmarks.root)
Cheers,
S.
--
Stéfane Fermigier, MdC à l'Université Paris 7. Tel: 01.44.27.61.01 (Bureau).
<www.math.jussieu.fr/~fermigie/>, <www.aful.org>, <www.linux-center.org>.
"Python is so much easier to write and experiment with that I write it
in Python first, then translate to Java if necessary - despite being
the author of a Java book!" Gordon McMillan