Refactor add parameter

This commit is contained in:
John Piasetzki 2016-10-19 23:51:40 -04:00
parent 50e4dd167e
commit 04a6cc9416

View File

@ -56,24 +56,16 @@ class Router:
def add_parameter(match): def add_parameter(match):
# We could receive NAME or NAME:PATTERN # We could receive NAME or NAME:PATTERN
parts = match.group(1).split(':') parameter_name = match.group(1)
if len(parts) == 2: parameter_pattern = 'string'
parameter_name, parameter_pattern = parts if ':' in parameter_name:
else: parameter_name, parameter_pattern = parameter_name.split(':', 1)
parameter_name = parts[0]
parameter_pattern = 'string'
default = (None, parameter_pattern)
# Pull from pre-configured types # Pull from pre-configured types
parameter_regex = REGEX_TYPES.get(parameter_pattern) parameter_type, parameter_pattern = REGEX_TYPES.get(parameter_pattern, default)
if parameter_regex: parameters.append(Parameter(name=parameter_name, cast=parameter_type))
parameter_type, parameter_pattern = parameter_regex return '({})'.format(parameter_pattern)
else:
parameter_type = None
parameter = Parameter(name=parameter_name, cast=parameter_type)
parameters.append(parameter)
return "({})".format(parameter_pattern)
pattern_string = re.sub("<(.+?)>", add_parameter, uri) pattern_string = re.sub("<(.+?)>", add_parameter, uri)
pattern = re.compile("^{}$".format(pattern_string)) pattern = re.compile("^{}$".format(pattern_string))