From def5c821ba145aac450947f8eba7e57e484fe9de Mon Sep 17 00:00:00 2001 From: Ian Cordasco Date: Tue, 23 Aug 2016 16:20:14 -0500 Subject: Add missing variables function At some point the uritemplate module added a variables function. This adds that interface to maintain compatibility. Closes #26 --- docs/index.rst | 2 ++ tests/test_uritemplate.py | 6 +++++- uritemplate/__init__.py | 6 ++++-- uritemplate/api.py | 19 +++++++++++++++++++ 4 files changed, 30 insertions(+), 3 deletions(-) diff --git a/docs/index.rst b/docs/index.rst index b846d44..35776f0 100644 --- a/docs/index.rst +++ b/docs/index.rst @@ -53,6 +53,8 @@ API .. autofunction:: uritemplate.partial +.. autofunction:: uritemplate.variables + .. autoclass:: uritemplate.URITemplate :members: diff --git a/tests/test_uritemplate.py b/tests/test_uritemplate.py index 26aeb72..b1abc96 100644 --- a/tests/test_uritemplate.py +++ b/tests/test_uritemplate.py @@ -1,5 +1,5 @@ from unittest import TestCase, main -from uritemplate import URITemplate, expand, partial +from uritemplate import URITemplate, expand, partial, variables from uritemplate import variable @@ -580,6 +580,10 @@ class TestAPI(TestCase): URITemplate('https://api.github.com/users/sigmavirus24{/other}') ) + def test_variables(self): + self.assertEqual(variables(self.uri), + URITemplate(self.uri).variable_names) + if __name__ == '__main__': main() diff --git a/uritemplate/__init__.py b/uritemplate/__init__.py index 03abf08..8e76ea5 100644 --- a/uritemplate/__init__.py +++ b/uritemplate/__init__.py @@ -19,6 +19,8 @@ __copyright__ = 'Copyright 2013 Ian Cordasco' __version__ = '2.0.0rc1' __version_info__ = tuple(int(i) for i in __version__.split('.') if i.isdigit()) -from uritemplate.api import URITemplate, expand, partial # noqa: E402 +from uritemplate.api import ( + URITemplate, expand, partial, variables # noqa: E402 +) -__all__ = ('URITemplate', 'expand', 'partial') +__all__ = ('URITemplate', 'expand', 'partial', 'variables') diff --git a/uritemplate/api.py b/uritemplate/api.py index cbcec09..37c7c45 100644 --- a/uritemplate/api.py +++ b/uritemplate/api.py @@ -50,3 +50,22 @@ def partial(uri, var_dict=None, **kwargs): """ return URITemplate(uri).partial(var_dict, **kwargs) + + +def variables(uri): + """Parse the variables of the template. + + This returns all of the variable names in the URI Template. + + :returns: Set of variable names + :rtype: set + + Example:: + + variables('https://api.github.com{/end}) + # => {'end'} + variables('https://api.github.com/repos{/username}{/repository}') + # => {'username', 'repository'} + + """ + return set(URITemplate(uri).variable_names) -- cgit v1.2.3