lukearno/selector — Issue #10: Add URL Generation
Author: lukearno
Created: 2011-11-24 01:56:00+00:00
State: open

```python
s = selector.Selector()
s.add("/foos/{fooid}", name='afoo')
assert s.url('afoo', fooid=11) == '/foo/'
```

Maybe parser can optionally return something like this instead of a regex string:

```python
class EndPoint(object):
def __init__(self, construct=True):
self._construct = construct
def match(self, path_info):
# for simple parser, compile and use regex here
def url(self, *a, **kw):
if self._construct is True:
# build url with string.Template
else:
# raise something
```

I think type tuples for the parser could have an optional third: `('digit', '\d+', '%d')`. Defaulting to `'%s'`, so `('word', '\w+')` is the same as `('word', '\w+', '%s')`.

The parser can implement this by building up a template for `string.Template` as it goes, unless it hits a conditional portion (`[]` square brackets), in which case it produces an `EndPoint(contruct=False)`

Partials seem like an obvious bonus here:

```python
s.add('/books/{book}/chapters/{chapter}', GET=show_chapter)
chapter_url = s.url_parial('chapter', book=2)
assert chapter_url(chapter=15) == "/books/2/chapters/15"
```

Also, how do we support this in the mapping file format?

```
story: /stories/{story}
GET mymodule:show_story
```

might be the same as

```python
s.add('/stories/{story}', name='story', GET=show_story)
```

... **and** if **that** is going to work, then I guess this wil

```python
s.add('story: /stories/{story}', GET=show_story)
```

I don't think there is any existing reason to put whitespace in a path expression line in a mapping file, so I think `len(pathexpr.strip().split())` should serve as a sufficient test for this case.