add some better error checking to the page_tree stuff
authorTero Marttila <terom@fixme.fi>
Sat, 07 Feb 2009 02:51:36 +0200
changeset 17 b538e1f7011c
parent 16 4a40718c7b4b
child 18 0e701fbd6760
add some better error checking to the page_tree stuff
lib/page_tree.py
lib/tree_parse.py
--- a/lib/page_tree.py	Sat Feb 07 02:46:58 2009 +0200
+++ b/lib/page_tree.py	Sat Feb 07 02:51:36 2009 +0200
@@ -7,6 +7,12 @@
 # path to file containing the page metadata tree
 PAGE_TREE_FILE = "pages/list"
 
+class PageTreeError (Exception) :
+    """
+        Error parsing/loading the page tree
+    """
+
+    pass
 
 class PageInfo (object) :
     """
@@ -118,6 +124,9 @@
         # parse tree
         tree = tree_parse.parse(path, ':')
 
+        if not tree :
+            raise PageTreeError("No root node found")
+
         def _create_node (parent, item) :
             """
                 Creates and returns a PageInfo from the given parent node and (line_number, line, children) tuple item
@@ -125,9 +134,15 @@
 
             # unpack
             line_number, line, children = item
+            
+            # parse line
+            url = title = None
+            
+            try :
+                url, title = line.split(':')
 
-            # parse line
-            url, title = line.split(':')
+            except :
+                raise PageTreeError("Invalid line: %s:%d: %r" % (path, line_number, line))
 
             # remove whitespace
             url = url.strip()
--- a/lib/tree_parse.py	Sat Feb 07 02:46:58 2009 +0200
+++ b/lib/tree_parse.py	Sat Feb 07 02:51:36 2009 +0200
@@ -5,6 +5,13 @@
     A file consists of a number of lines, and each line consists of indenting whitespace and data. Each line has a parent
 """
 
+class TreeParseError (Exception) :
+    """
+        Error parsing a tree file
+    """
+
+    pass
+
 def _read_lines (path, stop_tokens='') :
     """
         Reads lines from the given path, ignoring empty lines, and yielding (line_number, indent, line) tuples, where 
@@ -109,7 +116,7 @@
                         break
 
                     elif stack_indent < indent :
-                        assert False, "Bad un-indent"
+                        raise TreeParseError("Bad unindent on %s:%d, %d < %d" % (path, line_number, stack_indent, indent))
         
         # add to parent?
         if parent :