Convert string formats

This commit is contained in:
John Piasetzki 2016-10-19 23:56:23 -04:00
parent 04a6cc9416
commit e25e1c0e4b

View File

@ -2,14 +2,14 @@ import re
from collections import namedtuple from collections import namedtuple
from .exceptions import NotFound, InvalidUsage from .exceptions import NotFound, InvalidUsage
Route = namedtuple("Route", ['handler', 'methods', 'pattern', 'parameters']) Route = namedtuple('Route', ['handler', 'methods', 'pattern', 'parameters'])
Parameter = namedtuple("Parameter", ['name', 'cast']) Parameter = namedtuple('Parameter', ['name', 'cast'])
REGEX_TYPES = { REGEX_TYPES = {
"string": (None, "[^/]+"), 'string': (None, r'[^/]+'),
"int": (int, "\d+"), 'int': (int, r'\d+'),
"number": (float, "[0-9\\.]+"), 'number': (float, r'[0-9\\.]+'),
"alpha": (None, "[A-Za-z]+"), 'alpha': (None, r'[A-Za-z]+'),
} }
@ -67,8 +67,8 @@ class Router:
parameters.append(Parameter(name=parameter_name, cast=parameter_type)) parameters.append(Parameter(name=parameter_name, cast=parameter_type))
return '({})'.format(parameter_pattern) return '({})'.format(parameter_pattern)
pattern_string = re.sub("<(.+?)>", add_parameter, uri) pattern_string = re.sub(r'<(.+?)>', add_parameter, uri)
pattern = re.compile("^{}$".format(pattern_string)) pattern = re.compile(r'^{}$'.format(pattern_string))
route = Route( route = Route(
handler=handler, methods=methods_dict, pattern=pattern, handler=handler, methods=methods_dict, pattern=pattern,
@ -101,8 +101,8 @@ class Router:
if route: if route:
if route.methods and request.method not in route.methods: if route.methods and request.method not in route.methods:
raise InvalidUsage( raise InvalidUsage(
"Method {} not allowed for URL {}".format( 'Method {} not allowed for URL {}'.format(
request.method, request.url), status_code=405) request.method, request.url), status_code=405)
return route.handler, args, kwargs return route.handler, args, kwargs
else: else:
raise NotFound("Requested URL {} not found".format(request.url)) raise NotFound('Requested URL {} not found'.format(request.url))