From 4fc64ac04ffbec8e3a51ea3f7a129f17530ee3ef Mon Sep 17 00:00:00 2001 From: Aldo Cortesi Date: Sun, 24 Jun 2012 16:38:32 +1200 Subject: Enable anchors on command line. --- libpathod/pathod.py | 14 +++++++++++--- libpathod/utils.py | 21 ++++----------------- 2 files changed, 15 insertions(+), 20 deletions(-) (limited to 'libpathod') diff --git a/libpathod/pathod.py b/libpathod/pathod.py index 1491072c..ba537768 100644 --- a/libpathod/pathod.py +++ b/libpathod/pathod.py @@ -2,6 +2,8 @@ import urllib, threading, re from netlib import tcp, http, odict, wsgi import version, app, rparse +class PathodError(Exception): pass + class PathodHandler(tcp.BaseHandler): def handle(self): @@ -73,7 +75,7 @@ class Pathod(tcp.TCPServer): addr: (address, port) tuple. If port is 0, a free port will be automatically chosen. ssloptions: a dictionary containing certfile and keyfile specifications. - prefix: string specifying the prefix at which to anchor response generation. + prefix: string specifying the prefix at which to anchor response generation. staticdir: path to a directory of static resources, or None. anchors: A list of (regex, spec) tuples, or None. """ @@ -88,8 +90,14 @@ class Pathod(tcp.TCPServer): self.anchors = [] if anchors: for i in anchors: - arex = re.compile(i[0]) - aresp = rparse.parse(self.request_settings, i[1]) + try: + arex = re.compile(i[0]) + except re.error: + raise PathodError("Invalid regex in anchor: %s"%i[0]) + try: + aresp = rparse.parse(self.request_settings, i[1]) + except rparse.ParseException, v: + raise PathodError("Invalid page spec in anchor: '%s', %s"%(i[1], str(v))) self.anchors.append((arex, aresp)) @property diff --git a/libpathod/utils.py b/libpathod/utils.py index f421b8a6..39732849 100644 --- a/libpathod/utils.py +++ b/libpathod/utils.py @@ -1,27 +1,14 @@ import os, re import rparse -class AnchorError(Exception): pass - -def parse_anchor_spec(s, settings): +def parse_anchor_spec(s): """ - For now, this is very simple, and you can't have an '=' in your regular - expression. + Return a tuple, or None on error. """ if not "=" in s: - raise AnchorError("Invalid anchor definition: %s"%s) - rex, spec = s.split("=", 1) - try: - re.compile(rex) - except re.error: - raise AnchorError("Invalid regex in anchor: %s"%s) - try: - rparse.parse(settings, spec) - except rparse.ParseException, v: - raise AnchorError("Invalid page spec in anchor: '%s', %s"%(s, str(v))) - - return rex, spec + return None + return tuple(s.split("=", 1)) class Data: -- cgit v1.2.3