Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 import sys
00020
00021 class WikiNode:
00022 """Build a tree, mirroring the structure of a wikidot-based wiki"""
00023
00024 def __init__(self, title, link):
00025 """x.__init__(title, link) creates a new node x.
00026
00027 This node corresponds to a page with the title set to 'title'
00028 and whose link is 'link'."""
00029
00030 self.title = title
00031 self.link = link
00032 self.children = list()
00033
00034 def __repr__(self):
00035 """x.__repr__() <==> repr(x)"""
00036 return "{} ({}) - {} children".format(self.title, self.link, len(self.children))
00037
00038 def __getitem__(self, y):
00039 """x.__getitem__(y) <==> x[y]
00040
00041 Return the y-th child of the node"""
00042
00043 return self.children[y]
00044
00045 def insert(self, title, link, breadcrumbs):
00046 """Insert a new node into the tree.
00047
00048 Inputs:
00049 title: The page's title
00050 link: The page's URL (can be just the name, or full URL, or a partial path)
00051 breadcrumbs: list listing the URL of the parents, starting from the root
00052
00053 The URLs should be coherent between all inputs.
00054
00055 Output:
00056 Newly inserted node. 'None' if no corresponding parents.
00057 """
00058 if breadcrumbs == []:
00059
00060 child = WikiNode(title, link)
00061 self.children.append(child)
00062 return child
00063 else:
00064
00065 for x in self.children:
00066 if x.link == breadcrumbs[0]:
00067
00068 return x.insert(title, link, breadcrumbs[1:])
00069
00070 return None
00071
00072 def dump(self, level=0):
00073 """Recursively dump to stderr the whole tree"""
00074 print >> sys.stderr, level * ' ' + str(level) + " {} - {}".format(self.title, self.link)
00075 level += 1
00076 for x in self.children:
00077 x.dump(level)
00078