Skip to content
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

Handles Pyramid path regex and subpaths #70

Merged
merged 3 commits into from
Apr 11, 2017
Merged

Conversation

gabisurita
Copy link
Collaborator

Fixes: #68
Related to: Kinto/kinto#1180

Support path regex and subpath matching in Pyramid routes.

Considerations:

  • Subpaths are considered regular path attributes.
  • Regex is used as the attribute pattern on the swagger documentation

Notes:

  • Traverse and subpath are very primitive and only allow the definitions to be valid. Maybe we can try a better approach. Ideas?

r? @glasserc

Support path regex and subpath matching in Pyramid routes.
Subpaths are considered regular path attributes.
@gabisurita gabisurita requested a review from glasserc April 8, 2017 22:07
@coveralls
Copy link

coveralls commented Apr 8, 2017

Coverage Status

Coverage remained the same at 100.0% when pulling 44c916e on 68-path-handling into 91e0c48 on master.

Copy link
Contributor

@glasserc glasserc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for taking the time to read through all the Pyramid documentation. My head started to spin by the time I got to "There are only four real differences between a purely traversal-based application and a hybrid application".

I think the approach taken here is a good one. Maybe I didn't understand the question but I don't think we can really offer much for *traverse and *subpath without a lot of complexity in doing traversals ourselves. I'd prefer to leave this alone until we get a feature request with a concrete use case.

I'm not an expert on Pyramid by any means so feel free to correct me if my understanding of *subpath and *traverse in my review comments is wrong :)

# handle traverse and subpath
# docs.pylonsproject.org/projects/pyramid/en/latest/narr/hybrid.html
for subpath_marker in ('*subpath', '*traverse'):
if name.find(subpath_marker) > 0:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should this be >= 0? Or should we just use subpath_marker in name?

# docs.pylonsproject.org/projects/pyramid/en/latest/narr/hybrid.html
for subpath_marker in ('*subpath', '*traverse'):
if name.find(subpath_marker) > 0:
name = subpath_marker[1:]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is it possible to have a pattern for *subpath or *traversal? Should we just return here to make it obvious that the pattern part of this function isn't relevant, or maybe move this stanza to the top of the function?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure about this one, but I think we can just ignore the pattern in such cases.

@@ -145,8 +144,10 @@ def from_path(self, path):
:type path: string
:rtype: list
"""
path_components = path.split('/')
param_names = [comp[1:-1] for comp in path_components
if comp.startswith('{') and comp.endswith('}')]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The documentation at http://docs.pylonsproject.org/projects/pyramid/en/latest/narr/hybrid.html makes it seem like *traverse and *subpath don't occur in {/} but just show up at the end of the path.

params = self.handler.from_path('/my/{param:\\d+}/path/{id:[a-z]{8,42}}')
names = [param['name'] for param in params]
expected = ['param', 'id']
self.assertEqual(sorted(names), sorted(expected))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice; maybe we should add a test to verify that the patterns get captured too?

@@ -148,6 +148,12 @@ def from_path(self, path):
param_names = [comp[1:-1] for comp in path_components
if comp.startswith('{') and comp.endswith('}')]

# handle traverse and subpath
# docs.pylonsproject.org/projects/pyramid/en/latest/narr/hybrid.html
for subpath_marker in ('*subpath', '*traverse'):
Copy link
Collaborator Author

@gabisurita gabisurita Apr 10, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We also need to update the swagger path in here.

@coveralls
Copy link

coveralls commented Apr 10, 2017

Coverage Status

Coverage remained the same at 100.0% when pulling 2fe2b3b on 68-path-handling into 91e0c48 on master.

@gabisurita
Copy link
Collaborator Author

I think the approach taken here is a good one. Maybe I didn't understand the question but I don't think we can really offer much for *traverse and *subpath without a lot of complexity in doing traversals ourselves. I'd prefer to leave this alone until we get a feature request with a concrete use case.

I agree, by this approach I just want to make sure that the result is valid. and that we do include a way to provide the subpath on the documentation.

# handle traverse and subpath as regular parameters
# docs.pylonsproject.org/projects/pyramid/en/latest/narr/hybrid.html
for subpath_marker in ('*subpath', '*traverse'):
path = path.replace(subpath_marker, '{subpath}')
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we assume that subpath and traverse are always outside { .* } we can do something like this.

@coveralls
Copy link

coveralls commented Apr 10, 2017

Coverage Status

Coverage remained the same at 100.0% when pulling 67ecbf0 on 68-path-handling into 91e0c48 on master.

Copy link
Contributor

@glasserc glasserc left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very slick!

@gabisurita gabisurita merged commit db7fcc9 into master Apr 11, 2017
@gabisurita gabisurita deleted the 68-path-handling branch April 11, 2017 01:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants