-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added the attributes extension (#148)
Co-authored-by: Gabriel Niebler <[email protected]>
- Loading branch information
1 parent
feaec68
commit f94f372
Showing
4 changed files
with
134 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
""" | ||
Attributes extension | ||
-------------------- | ||
.. versionadded:: 0.8.0 | ||
""" | ||
import typing | ||
from urllib.parse import urljoin | ||
|
||
from lxml.objectify import ObjectifiedElement | ||
|
||
from ..utils.base import ExtensionBase | ||
|
||
|
||
class Attribute(ExtensionBase): | ||
""" | ||
Access attribute namespaces and definitions | ||
""" | ||
base_path = "/attribute" | ||
|
||
def list_namespaces(self) -> typing.List[str]: | ||
""" | ||
Get a list of all namespaces | ||
:return: List of namespace names | ||
""" | ||
response = self.osc.request( | ||
url=urljoin(self.osc.url, f"{self.base_path}/"), | ||
method="GET" | ||
) | ||
content = self.osc.get_objectified_xml(response) | ||
return [entry.get("name") for entry in content.findall("entry")] | ||
|
||
def get_namespace_meta(self, namespace: str) -> ObjectifiedElement: | ||
""" | ||
Get the meta of the namespace | ||
:param namespace: namespace name | ||
:return: Objectified XML element | ||
""" | ||
response = self.osc.request( | ||
url=urljoin(self.osc.url, f"{self.base_path}/{namespace}/_meta"), | ||
method="GET" | ||
) | ||
return self.osc.get_objectified_xml(response) | ||
|
||
def list_attributes(self, namespace: str) -> typing.List[str]: | ||
""" | ||
List the attributes available in namespace | ||
:param namespace: Namespace name | ||
:return: List of attribute names | ||
""" | ||
response = self.osc.request( | ||
url=urljoin(self.osc.url, f"{self.base_path}/{namespace}"), | ||
method="GET" | ||
) | ||
content = self.osc.get_objectified_xml(response) | ||
return [entry.get("name") for entry in content.findall("entry")] | ||
|
||
def get_attribute_meta(self, namespace: str, name: str) -> ObjectifiedElement: | ||
""" | ||
Get meta data for attribute | ||
:param namespace: Namespace name | ||
:param name: Attribute name | ||
:return: Objectified XML element | ||
""" | ||
response = self.osc.request( | ||
url=urljoin(self.osc.url, f"{self.base_path}/{namespace}/{name}/_meta"), | ||
method="GET" | ||
) | ||
return self.osc.get_objectified_xml(response) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import responses | ||
|
||
from .base import OscTest | ||
|
||
|
||
class TestAttribute(OscTest): | ||
def setUp(self): | ||
super().setUp() | ||
|
||
self.mock_request( | ||
method=responses.GET, | ||
url=self.osc.url + '/attribute/', | ||
body="<directory><entry name='Foo'/><entry name='Bar'/></directory>" | ||
) | ||
|
||
self.mock_request( | ||
method=responses.GET, | ||
url=self.osc.url + '/attribute/Foo/_meta', | ||
body="<namespace name='Foo'><modifiable_by user='A'/></namespace>" | ||
) | ||
|
||
self.mock_request( | ||
method=responses.GET, | ||
url=self.osc.url + '/attribute/Foo', | ||
body="<directory><entry name='Hello'/><entry name='World'/></directory>" | ||
) | ||
|
||
self.mock_request( | ||
method=responses.GET, | ||
url=self.osc.url + '/attribute/Foo/Hello/_meta', | ||
body="<definition name='Hello' namespace='Foo'><description>Lorem ipsum</description>" | ||
"<count>1</count><modifiable_by role='B'/></definition>" | ||
) | ||
|
||
@responses.activate | ||
def test_list_namespace(self): | ||
self.assertEqual(["Foo", "Bar"], self.osc.attributes.list_namespaces()) | ||
|
||
@responses.activate | ||
def test_get_namespace_meta(self): | ||
meta = self.osc.attributes.get_namespace_meta("Foo") | ||
self.assertEqual(meta.get("name"), "Foo") | ||
|
||
@responses.activate | ||
def test_list_attributes(self): | ||
self.assertEqual(["Hello", "World"], self.osc.attributes.list_attributes("Foo")) | ||
|
||
@responses.activate | ||
def test_get_attribute_meta(self): | ||
meta = self.osc.attributes.get_attribute_meta("Foo", "Hello") | ||
self.assertEqual(meta.get("name"), "Hello") | ||
self.assertEqual(meta.get("namespace"), "Foo") |