Back to index

moin  1.9.0~rc2
Public Member Functions | Public Attributes Class Reference

List of all members.

Public Member Functions

def __init__
def __getattr__
def __call__

Public Attributes


Detailed Description

Implements a callable that constructs URLs with the given base. The
function can be called with any number of positional and keyword
arguments which than are used to assemble the URL.  Works with URLs
and posix paths.

Positional arguments are appended as individual segments to
the path of the URL:

>>> href = Href('/foo')
>>> href('bar', 23)
>>> href('foo', bar=23)

If any of the arguments (positional or keyword) evaluates to `None` it
will be skipped.  If no keyword arguments are given the last argument
can be a :class:`dict` or :class:`MultiDict` (or any other dict subclass),
otherwise the keyword arguments are used for the query parameters, cutting
off the first trailing underscore of the parameter name:

>>> href(is_=42)
>>> href({'foo': 'bar'})

Combining of both methods is not allowed:

>>> href({'foo': 'bar'}, bar=42)
Traceback (most recent call last):
TypeError: keyword arguments and query-dicts can't be combined

Accessing attributes on the href object creates a new href object with
the attribute name as prefix:

>>> bar_href =
>>> bar_href("blub")

If `sort` is set to `True` the items are sorted by `key` or the default
sorting algorithm:

>>> href = Href("/", sort=True)
>>> href(a=1, b=2, c=3)

.. versionadded:: 0.5
    `sort` and `key` were added.

Definition at line 563 of file

Constructor & Destructor Documentation

def (   self,
  base = './',
  charset = 'utf-8',
  sort = False,
  key = None 

Definition at line 614 of file

00615     def __init__(self, base='./', charset='utf-8', sort=False, key=None):
00616         if not base:
00617             base = './'
00618         self.base = base
00619         self.charset = charset
00620         self.sort = sort
00621         self.key = key

Member Function Documentation

def (   self,

Definition at line 631 of file

00632     def __call__(self, *path, **query):
00633         if path and isinstance(path[-1], dict):
00634             if query:
00635                 raise TypeError('keyword arguments and query-dicts '
00636                                 'can\'t be combined')
00637             query, path = path[-1], path[:-1]
00638         elif query:
00639             query = dict([(k.endswith('_') and k[:-1] or k, v)
00640                           for k, v in query.items()])
00641         path = '/'.join([url_quote(x, self.charset) for x in path
00642                          if x is not None]).lstrip('/')
00643         rv = self.base
00644         if path:
00645             if not rv.endswith('/'):
00646                 rv += '/'
00647             rv = urlparse.urljoin(rv, path)
00648         if query:
00649             rv += '?' + url_encode(query, self.charset, sort=self.sort,
00650                                    key=self.key)
00651         return str(rv)

Here is the call graph for this function:

Definition at line 622 of file

00623     def __getattr__(self, name):
00624         if name[:2] == '__':
00625             raise AttributeError(name)
00626         base = self.base
00627         if base[-1:] != '/':
00628             base += '/'
00629         return Href(urlparse.urljoin(base, name), self.charset, self.sort,
00630                     self.key)

Member Data Documentation

Definition at line 617 of file

Definition at line 618 of file

Definition at line 620 of file

Definition at line 619 of file

The documentation for this class was generated from the following file: