Skip to content

Commit

Permalink
New template object uses expand function
Browse files Browse the repository at this point in the history
  • Loading branch information
codeinthehole committed Jun 17, 2013
1 parent 0f892e4 commit 4876938
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 39 deletions.
37 changes: 3 additions & 34 deletions purl/__init__.py
Expand Up @@ -15,6 +15,8 @@
import re
import functools

from . import template


# Python 2/3 compatibility
import sys
Expand Down Expand Up @@ -420,37 +422,4 @@ def __init__(self, url_str):
self._base = url_str

def expand(self, variables=None):
if variables is None:
variables = {}
regexp = re.compile("{([^\}]+)}")
url = regexp.sub(functools.partial(self._replace, variables),
self._base)
return URL(url)

def _replace(self, variables, match):
expression = match.group(1)

# Escaping functions (don't need to be in method body)
escape_all = functools.partial(quote, safe="/")
escape_reserved = functools.partial(quote, safe="/!")

# Splitting functions
split_basic = lambda x: x.split(',')
split_operator = lambda x: x[1:].split(',')

# operator -> (prefix, separator, split, escape)
operators = {
'+': ('', ',', split_operator, escape_reserved),
'#': ('#', ',', split_operator, escape_reserved),
'.': ('.', '.', split_operator, escape_all),
'/': ('/', '/', split_operator, escape_all),
';': (';', ';', split_operator, escape_all),
}
default = ('', ',', split_basic, escape_all)
prefix, separator, split, escape = operators.get(expression[0], default)

replacements = []
for key in split(expression):
if key in variables:
replacements.append(escape(variables[key]))
return prefix + separator.join(replacements)
return URL(template.expand(self._base, variables))
10 changes: 5 additions & 5 deletions purl/template.py
Expand Up @@ -47,7 +47,6 @@ def _format_pair_no_equals(explode, separator, escape, key, value):
return key
return _format_pair(explode, separator, escape, key, value)


def _format_pair_with_equals(explode, separator, escape, key, value):
"""
Format a key, value pair including the equals sign
Expand Down Expand Up @@ -145,11 +144,12 @@ def _split_operator(string):

# Escaping functions
# ------------------
# These are responsible for splitting a string into a sequence of (key,
# modifier) tuples

_escape_all = functools.partial(quote, safe="")
_escape_reserved = functools.partial(quote, safe="/!,.;")
def _escape_all(value):
return quote(str(value), safe="")

def _escape_reserved(value):
return quote(str(value), safe="/!,.;")

# Operator map
# ------------
Expand Down
12 changes: 12 additions & 0 deletions tests/template_tests.py
@@ -0,0 +1,12 @@
from unittest import TestCase

import purl


class TemplateTests(TestCase):

def test_basic_expansion(self):
template = purl.Template('http://example.com{+path,x}/here')
url = template.expand({'path': '/foo/bar', 'x': 1024})
self.assertEquals('http://example.com/foo/bar,1024/here',
url.as_string())

0 comments on commit 4876938

Please sign in to comment.