Skip to content

Conversation

theacodes
Copy link
Contributor

Towards #3842

@googlebot googlebot added the cla: yes This human has signed the Contributor License Agreement. label Aug 22, 2017
@theacodes
Copy link
Contributor Author

@lukesneeringer @dhermes this gave me more trouble than I anticipated, but the tests give me confidence that this works as expected.

@lukesneeringer
Copy link
Contributor

Still simpler than a lex-yacc implementation. ;-)

Sanity check: Correct?

@theacodes
Copy link
Contributor Author

theacodes commented Aug 22, 2017 via email

raise ValueError(
'Named variable \'{}\' not specified and needed by template '
'`{}` at position {}'.format(
name, match.string, match.start()))

This comment was marked as spam.

This comment was marked as spam.


Raises:
ValueError: If a positional or named variable is required by the
template but not specified.

This comment was marked as spam.

This comment was marked as spam.

if not template:
return _SINGLE_SEGEMENT_PATTERN.format(name)
elif template == '**':
return _MULTI_SEGEMENT_PATTERN.format(name)

This comment was marked as spam.

This comment was marked as spam.

Returns:
bool: True if the path matches.
"""
pattern = _generate_pattern_for_template(tmpl) + '$'

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.


@pytest.mark.parametrize('tmpl, args, kwargs, exc_match', [
# Missing positional arg.
['v1/*', [], {}, 'Positional'],

This comment was marked as spam.

This comment was marked as spam.

['/v1/*/{name}', ['a'], {'name': 'parent'}, '/v1/a/parent'],
['/v1/{name}/*', ['a'], {'name': 'parent'}, '/v1/parent/a'],
['/v1/{parent}/*/{child}/*', ['a', 'b'],
{'parent': 'parent', 'child': 'child'}, '/v1/parent/a/child/b'],

This comment was marked as spam.

This comment was marked as spam.

import six

# Regular expression for extracting variable parts from a path template.
# The variables can be expressed as:

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

This comment was marked as spam.

['/v1/{name=parent/*}/*', ['a'], {'name': 'parent/child'},
'/v1/parent/child/a'],
['/v1/*/{name=parent/**}', ['a'], {'name': 'parent/child/object'},
'/v1/a/parent/child/object'],

This comment was marked as spam.

This comment was marked as spam.

match (re.Match): A regular expression match.

Returns:
str: The expaned variable to replace the match.

This comment was marked as spam.

This comment was marked as spam.

import six

# Regular expression for extracting variable parts from a path template.
# The variables can be expressed as:

This comment was marked as spam.

is encountered.
"""
replacer = functools.partial(_expand_variable_match, list(args), kwargs)
return _VARIABLE_RE.sub(replacer, tmpl)

This comment was marked as spam.

This comment was marked as spam.

@theacodes theacodes merged commit 357bfeb into googleapis:master Aug 23, 2017
@theacodes theacodes deleted the api-core-path-template branch August 23, 2017 21:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: core cla: yes This human has signed the Contributor License Agreement.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants