terom@29: terom@29: """ terom@29: URL mapping for the irclogs.qmsk.net site terom@29: """ terom@29: terom@36: import re terom@37: import os.path terom@36: terom@29: # our own handlers terom@29: import handlers terom@29: terom@36: # mapper terom@36: from lib import map terom@29: terom@36: class URLError (Exception) : terom@29: """ terom@36: Error with an URL definition terom@29: """ terom@29: terom@36: pass terom@29: terom@37: class LabelValue (object) : terom@37: """ terom@37: Represents the value of a ValueLabel... terom@37: """ terom@37: terom@37: def __init__ (self, label, value) : terom@37: """ terom@37: Just store terom@37: """ terom@37: terom@37: self.label = label terom@37: self.value = value terom@37: terom@37: def __str__ (self) : terom@37: return "%s=%s" % (self.label.key, self.value) terom@37: terom@37: def __repr__ (self) : terom@37: return "<%s>" % self terom@37: terom@36: class Label (object) : terom@36: """ terom@36: Base class for URL labels (i.e. the segments of the URL between /s) terom@36: """ terom@36: terom@36: @staticmethod terom@36: def parse (mask, defaults) : terom@36: """ terom@36: Parse the given label-segment, and return a *Label instance terom@36: """ terom@36: terom@36: # empty? terom@36: if not mask : terom@36: return EmptyLabel() terom@36: terom@36: # simple value? terom@36: match = SimpleValueLabel.EXPR.match(mask) terom@36: terom@36: if match : terom@36: # key terom@36: key = match.group('key') terom@36: terom@36: # default? terom@36: default = defaults.get(key) terom@36: terom@36: # build terom@36: return SimpleValueLabel(key, default) terom@36: terom@36: # static? terom@36: match = StaticLabel.EXPR.match(mask) terom@36: terom@36: if match : terom@36: return StaticLabel(match.group('name')) terom@36: terom@36: # invalid terom@36: raise URLError("Invalid label: %r" % (mask, )) terom@37: terom@38: def match (self, value=None) : terom@37: """ terom@37: Match this label against the given value, returning either True to match without a value, a LabelValue terom@38: object, or boolean false to not match. terom@38: terom@38: If value is None, this means that only a default value should be accepted. XXX: currently returned default terom@38: is not used. terom@37: """ terom@36: terom@37: abstract terom@36: terom@36: class EmptyLabel (Label) : terom@36: """ terom@36: An empty label, i.e. just a slash in the URL terom@36: """ terom@36: terom@36: def __eq__ (self, other) : terom@36: """ terom@36: Just compares type terom@36: """ terom@36: terom@36: return isinstance(other, EmptyLabel) terom@36: terom@38: def match (self, value=None) : terom@37: """ terom@37: Match empty string -> True terom@37: """ terom@38: terom@38: # no default terom@38: if value is None : terom@38: return False terom@38: terom@38: # only empty segments terom@37: if value == '' : terom@37: return True terom@37: terom@36: def __str__ (self) : terom@36: return '' terom@36: terom@36: class StaticLabel (Label) : terom@36: """ terom@36: A simple literal Label, used for fixed terms in the URL terom@36: """ terom@36: terom@36: EXPR = re.compile(r'^(?P[a-zA-Z_.-]+)$') terom@36: terom@36: def __init__ (self, name) : terom@36: """ terom@36: The given name is the literal name of this label terom@36: """ terom@36: terom@36: self.name = name terom@36: terom@36: def __eq__ (self, other) : terom@36: """ terom@36: Compares names terom@36: """ terom@36: terom@36: return isinstance(other, StaticLabel) and self.name == other.name terom@36: terom@38: def match (self, value=None) : terom@37: """ terom@37: Match exactly -> True terom@37: """ terom@37: terom@38: # no defaults terom@38: if value is None : terom@38: return False terom@38: terom@38: # match name terom@37: if value == self.name : terom@37: return True terom@37: terom@36: def __str__ (self) : terom@36: return self.name terom@36: terom@36: class ValueLabel (Label) : terom@36: """ terom@36: A label with a key and a value terom@37: terom@37: XXX: do we even need this? terom@36: """ terom@36: terom@36: def __init__ (self, key, default) : terom@36: """ terom@36: Set the key and default value. Default value may be None if there is no default value defined terom@36: """ terom@36: terom@36: self.key = key terom@36: self.default = default terom@36: terom@36: def __eq__ (self, other) : terom@36: """ terom@36: Compares keys terom@36: """ terom@36: terom@36: return isinstance(other, ValueLabel) and self.key == other.key terom@36: terom@36: class SimpleValueLabel (ValueLabel) : terom@36: """ terom@36: A label that has a name and a simple string value terom@36: """ terom@36: terom@36: EXPR = re.compile(r'^\{(?P[a-zA-Z_][a-zA-Z0-9_]*)\}$') terom@36: terom@36: def __init__ (self, key, default) : terom@36: """ terom@36: The given key is the name of this label's value terom@36: """ terom@36: terom@36: super(SimpleValueLabel, self).__init__(key, default) terom@36: terom@38: def match (self, value=None) : terom@37: """ terom@37: Match -> LabelValue terom@37: """ terom@38: terom@38: # default? terom@38: if value is None and self.default : terom@38: return LabelValue(self, self.default) terom@38: terom@38: # only non-empty values! terom@38: elif value : terom@38: return LabelValue(self, value) terom@37: terom@36: def __str__ (self) : terom@36: if self.default : terom@36: return '{%s=%s}' % (self.key, self.default) terom@36: terom@36: else : terom@36: return '{%s}' % (self.key, ) terom@36: terom@36: class URL (object) : terom@36: """ terom@36: Represents a specific URL terom@36: """ terom@36: terom@36: def __init__ (self, url_mask, handler, **defaults) : terom@36: """ terom@36: Create an URL with the given url mask, handler, and default values terom@36: """ terom@36: terom@36: # store terom@36: self.url_mask = url_mask terom@36: self.handler = handler terom@36: self.defaults = defaults terom@36: terom@36: # build our labels terom@36: self.label_path = [Label.parse(mask, defaults) for mask in url_mask.split('/')] terom@36: terom@36: def get_label_path (self) : terom@36: """ terom@36: Returns a list containing the labels in this url terom@36: """ terom@36: terom@36: # copy self.label_path terom@36: return list(self.label_path) terom@36: terom@37: def execute (self, request, label_values) : terom@36: """ terom@37: Invoke the handler, using the given label values terom@36: """ terom@37: terom@37: # start with the defaults terom@37: kwargs = self.defaults() terom@36: terom@37: # then add all the values terom@37: for label_value in label_values : terom@37: kwargs[label_value.label.key] = label_value.value terom@37: terom@37: # execute the handler terom@37: return self.handler(request, **kwargs) terom@37: terom@36: def __str__ (self) : terom@36: return '/'.join(str(label) for label in self.label_path) terom@36: terom@36: def __repr__ (self) : terom@36: return "URL(%r, %r)" % (str(self), self.handler) terom@36: terom@36: class URLNode (object) : terom@36: """ terom@36: Represents a node in the URLTree terom@36: """ terom@36: terom@36: def __init__ (self, parent, label) : terom@36: """ terom@36: Initialize with the given parent and label, empty children dict terom@36: """ terom@36: terom@36: # the parent URLNode terom@36: self.parent = parent terom@36: terom@36: # this node's Label terom@36: self.label = label terom@36: terom@36: # list of child URLNodes terom@36: self.children = [] terom@36: terom@36: # this node's URL, set by add_url for an empty label_path terom@36: self.url = None terom@36: terom@36: def _build_child (self, label) : terom@36: """ terom@36: Build, insert and return a new child Node terom@36: """ terom@36: terom@36: # build new child terom@36: child = URLNode(self, label) terom@36: terom@36: # add to children terom@36: self.children.append(child) terom@36: terom@36: # return terom@36: return child terom@36: terom@36: def add_url (self, url, label_path) : terom@36: """ terom@36: Add a URL object to this node under the given path. Uses recursion to process the path. terom@36: terom@36: The label_path argument is a (partial) label path as returned by URL.get_label_path. terom@36: terom@36: If label_path is empty (len zero, or begins with EmptyLabel), then the given url is assigned to this node, if no terom@36: url was assigned before. terom@36: """ terom@36: terom@36: # matches this node? terom@36: if not label_path or isinstance(label_path[0], EmptyLabel) : terom@36: if self.url : terom@36: raise URLError(url, "node already defined") terom@36: terom@36: else : terom@36: # set terom@36: self.url = url terom@36: terom@36: else : terom@36: # pop child label from label_path terom@36: child_label = label_path.pop(0) terom@36: terom@36: # look for the child to recurse into terom@36: child = None terom@36: terom@36: # look for an existing child with that label terom@36: for child in self.children : terom@36: if child.label == child_label : terom@36: # found, use this terom@36: break terom@36: terom@36: else : terom@36: # build a new child terom@36: child = self._build_child(child_label) terom@36: terom@36: # recurse to handle the rest of the label_path terom@36: child.add_url(url, label_path) terom@36: terom@37: def match (self, label_path) : terom@36: """ terom@37: Locate the URL object corresponding to the given label_path value under this node. terom@36: terom@37: Returns a (url, label_values) tuple terom@36: """ terom@36: terom@36: # empty label_path? terom@37: if not label_path or label_path[0] == '' : terom@37: # the search ends at this node terom@36: if self.url : terom@36: # this URL is the best match terom@37: return (self.url, []) terom@36: terom@38: # look for default-only values, DFS terom@38: for child in self.children : terom@38: # does the child's label accept a default match? terom@38: if child.label.match() : terom@38: return child.match(label_path) terom@38: terom@36: else : terom@36: # incomplete URL terom@37: raise URLError("no URL handler defined for this Node") terom@36: terom@36: else : terom@37: # pop the next label from the label path terom@36: label = label_path.pop(0) terom@36: terom@37: # return one match... terom@37: match = value = None terom@36: terom@36: # recurse through our children terom@36: for child in self.children : terom@37: # match value terom@37: value = child.label.match(label) terom@36: terom@37: # skip those that don't match at all terom@37: if not value : terom@37: continue; terom@37: terom@37: # already found a match? :/ terom@37: if match : terom@37: raise URLError("Ambiguous URL") terom@37: terom@37: # ok, but continue looking to make sure there's no ambiguous URLs terom@37: match = child terom@37: terom@37: # found something? terom@37: if not match : terom@37: raise URLError("No child found for label") terom@37: terom@37: # ok, recurse into the match terom@37: url, label_value = match.match(label_path) terom@37: terom@37: # add our value? terom@37: if isinstance(value, LabelValue) : terom@37: label_value.append(value) terom@37: terom@37: # return the match terom@37: return url, label_value terom@36: terom@36: def dump (self, indent=0) : terom@36: """ terom@36: Returns a multi-line string representation of this Node terom@36: """ terom@36: terom@36: return '\n'.join([ terom@36: "%-45s%s" % ( terom@36: ' '*indent + str(self.label) + ('/' if self.children else ''), terom@36: (' -> %r' % self.url) if self.url else '' terom@36: ) terom@36: ] + [ terom@36: child.dump(indent + 4) for child in self.children terom@36: ]) terom@36: terom@36: def __str__ (self) : terom@36: return "%s/[%s]" % (self.label, ','.join(str(child) for child in self.children)) terom@36: terom@36: class URLTree (map.Mapper) : terom@36: """ terom@36: Map requests to handlers, using a defined tree of URLs terom@36: """ terom@36: terom@36: def __init__ (self, url_list) : terom@36: """ terom@36: Initialize the tree using the given list of URLs terom@36: """ terom@36: terom@36: # root node terom@36: self.root = URLNode(None, EmptyLabel()) terom@36: terom@36: # just add each URL terom@36: for url in url_list : terom@36: self.add_url(url) terom@36: terom@36: def add_url (self, url) : terom@36: """ terom@36: Adds the given URL to the tree. The URL must begin with a root slash. terom@36: """ terom@36: # get url's label path terom@36: path = url.get_label_path() terom@36: terom@36: # should begin with root terom@36: root_label = path.pop(0) terom@36: assert root_label == self.root.label, "URL must begin with root" terom@36: terom@36: # add to root terom@36: self.root.add_url(url, path) terom@36: terom@36: def match (self, url) : terom@36: """ terom@37: Find the URL object best corresponding to the given url, matching any ValueLabels. terom@37: terom@37: Returns an (URL, [LabelValue]) tuple. terom@36: """ terom@36: terom@36: # normalize the URL terom@36: url = os.path.normpath(url) terom@36: terom@36: # split it into labels terom@37: path = url.split('/') terom@37: terom@37: # ensure that it starts with a / terom@37: root_label = path.pop(0) terom@37: assert self.root.label.match(root_label), "URL must begin with root" terom@36: terom@36: # just match starting at root terom@37: return self.root.match(path) terom@36: terom@36: def handle_request (self, request) : terom@36: """ terom@36: Looks up the request's URL, and invokes its handler terom@36: """ terom@36: terom@36: # get the request's URL path terom@37: url, label_values = self.match(request.get_page_name()) terom@36: terom@36: # let the URL handle it terom@37: url.execute(request, label_values) terom@36: terom@36: # urls terom@36: index = URL( '/', handlers.index ) terom@36: channel_view = URL( '/channel/{channel}', handlers.channel_view ) terom@36: channel_last = URL( '/channel/{channel}/last/{count}/{format}', handlers.channel_last, count=100, format="html" ) terom@36: channel_search = URL( '/channel/{channel}/search', handlers.channel_search ) terom@36: terom@36: # mapper terom@36: mapper = URLTree([index, channel_view, channel_last, channel_search]) terom@36: