-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add google.api.core.path_template #3851
Add google.api.core.path_template #3851
Conversation
@lukesneeringer @dhermes this gave me more trouble than I anticipated, but the tests give me confidence that this works as expected. |
Still simpler than a lex-yacc implementation. ;-) Sanity check: Correct? |
Much simpler
…On Mon, Aug 21, 2017, 7:48 PM Luke Sneeringer ***@***.***> wrote:
Still simpler than a lex-yacc implementation. ;-)
Sanity check: Correct?
—
You are receiving this because you authored the thread.
Reply to this email directly, view it on GitHub
<#3851 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AAPUc6HWLCb78HkRVkBspwrTMa-n4IIbks5sakF9gaJpZM4O-Cw2>
.
|
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.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
|
||
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.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
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.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
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.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
|
||
@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.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
['/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.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
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.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
['/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.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
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.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
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.
Sorry, something went wrong.
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.
Sorry, something went wrong.
This comment was marked as spam.
This comment was marked as spam.
Sorry, something went wrong.
Towards #3842