aboutsummaryrefslogtreecommitdiffstats
path: root/libpathod
diff options
context:
space:
mode:
Diffstat (limited to 'libpathod')
-rw-r--r--libpathod/app.py38
-rw-r--r--libpathod/utils.py25
2 files changed, 58 insertions, 5 deletions
diff --git a/libpathod/app.py b/libpathod/app.py
index 399d1340..51bd014b 100644
--- a/libpathod/app.py
+++ b/libpathod/app.py
@@ -37,10 +37,8 @@ class Log(_Page):
class Pathod(object):
- anchor = "/p/"
- def __init__(self, application, request, **settings):
+ def __init__(self, spec, application, request, **settings):
self.application, self.request, self.settings = application, request, settings
- spec = urllib.unquote(self.request.uri)[len(self.anchor):]
try:
self.response = rparse.parse(self.settings, spec)
except rparse.ParseException, v:
@@ -53,9 +51,17 @@ class Pathod(object):
self.response.render(self.request)
+class RequestPathod(Pathod):
+ anchor = "/p/"
+ def __init__(self, application, request, **settings):
+ spec = urllib.unquote(request.uri)[len(self.anchor):]
+ Pathod.__init__(self, spec, application, request, **settings)
+
+
class PathodApp(tornado.web.Application):
def __init__(self, **settings):
self.templates = tornado.template.Loader(utils.data.path("templates"))
+ self.appsettings = settings
tornado.web.Application.__init__(
self,
[
@@ -63,13 +69,37 @@ class PathodApp(tornado.web.Application):
(r"/log", Log),
(r"/help", Help),
(r"/preview", Preview),
- (r"/p/.*", Pathod, settings),
+ (r"/p/.*", RequestPathod, settings),
],
static_path = utils.data.path("static"),
template_path = utils.data.path("templates"),
debug=True
)
+ def add_anchor(self, pattern, spec):
+ """
+ Anchors are added to the beginning of the handlers.
+ """
+ # We assume we have only one host...
+ l = self.handlers[0][1]
+ class FixedPathod(Pathod):
+ def __init__(self, application, request, **settings):
+ Pathod.__init__(self, spec, application, request, **settings)
+ FixedPathod.spec = spec
+ l.insert(0, tornado.web.URLSpec(pattern, FixedPathod, self.appsettings))
+
+ def get_anchors(self, pattern, spec):
+ """
+ Anchors are added to the beginning of the handlers.
+ """
+ pass
+
+ def remove_anchor(self, pattern, spec):
+ """
+ Anchors are added to the beginning of the handlers.
+ """
+ pass
+
# begin nocover
def run(application, port, ssl_options):
diff --git a/libpathod/utils.py b/libpathod/utils.py
index 104ee148..daeccdea 100644
--- a/libpathod/utils.py
+++ b/libpathod/utils.py
@@ -1,4 +1,27 @@
-import copy, os
+import copy, os, re
+import rparse
+
+class AnchorError(Exception): pass
+
+def parse_anchor_spec(s, settings):
+ """
+ For now, this is very simple, and you can't have an '=' in your regular
+ expression.
+ """
+ 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
+
class Data:
def __init__(self, name):