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