tree.py
Go to the documentation of this file.
00001 #   Aseba - an event-based framework for distributed robot control
00002 #   Copyright (C) 2007--2011:
00003 #           Stephane Magnenat <stephane at magnenat dot net>
00004 #           (http://stephane.magnenat.net)
00005 #           and other contributors, see authors.txt for details
00006 #
00007 #   This program is free software: you can redistribute it and/or modify
00008 #   it under the terms of the GNU Lesser General Public License as published
00009 #   by the Free Software Foundation, version 3 of the License.
00010 #
00011 #   This program is distributed in the hope that it will be useful,
00012 #   but WITHOUT ANY WARRANTY; without even the implied warranty of
00013 #   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014 #   GNU Lesser General Public License for more details.
00015 #
00016 #   You should have received a copy of the GNU Lesser General Public License
00017 #   along with this program. If not, see <http://www.gnu.org/licenses/>.
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             # it is a leaf, insert it
00060             child = WikiNode(title, link)
00061             self.children.append(child)
00062             return child
00063         else:
00064             # search a corresponding child
00065             for x in self.children:
00066                 if x.link == breadcrumbs[0]:
00067                     # match
00068                     return x.insert(title, link, breadcrumbs[1:])
00069             # failure
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 


aseba
Author(s): Stéphane Magnenat
autogenerated on Sun Oct 5 2014 23:46:39