Skip to content

Commit

Permalink
Add getEndpoint method to DataConnector
Browse files Browse the repository at this point in the history
  • Loading branch information
djmitche committed May 27, 2013
1 parent 2da7502 commit d0151d7
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 8 deletions.
8 changes: 4 additions & 4 deletions master/buildbot/data/connector.py
Expand Up @@ -93,7 +93,7 @@ def _setup(self):
module = reflect.namedModule(moduleName)
self._scanModule(module)

def _lookup(self, path):
def getEndpoint(self, path):
try:
return self.matcher[path]
except KeyError:
Expand All @@ -104,16 +104,16 @@ def get(self, path, filters=None, fields=None, order=None,
limit=None, offset=None):
resultSpec = resultspec.ResultSpec(filters=filters, fields=fields,
order=order, limit=limit, offset=offset)
endpoint, kwargs = self._lookup(path)
endpoint, kwargs = self.getEndpoint(path)
rv = yield endpoint.get(resultSpec, kwargs)
if resultSpec:
rv = resultSpec.apply(rv)
defer.returnValue(rv)

def startConsuming(self, callback, options, path):
endpoint, kwargs = self._lookup(path)
endpoint, kwargs = self.getEndpoint(path)
return endpoint.startConsuming(callback, options, kwargs)

def control(self, action, args, path):
endpoint, kwargs = self._lookup(path)
endpoint, kwargs = self.getEndpoint(path)
return endpoint.control(action, args, kwargs)
5 changes: 5 additions & 0 deletions master/buildbot/test/fake/fakedata.py
Expand Up @@ -273,6 +273,11 @@ def __init__(self, master, testcase):
self.realConnector = connector.DataConnector(master)
self.rtypes = self.realConnector.rtypes

def getEndpoint(self, path):
if not isinstance(path, tuple):
raise TypeError('path must be a tuple')
return self.realConnector.getEndpoint(path)

def get(self, path, filters=None, fields=None,
order=None, limit=None, offset=None):
if not isinstance(path, tuple):
Expand Down
17 changes: 13 additions & 4 deletions master/buildbot/test/unit/test_data_connector.py
Expand Up @@ -19,7 +19,7 @@
from twisted.python import reflect
from buildbot.test.fake import fakemaster
from buildbot.test.util import interfaces
from buildbot.data import connector, base, types, resultspec
from buildbot.data import connector, base, types, resultspec, exceptions

class Tests(interfaces.InterfaceTests):

Expand All @@ -32,6 +32,11 @@ def get(self, path, filters=None, fields=None,
order=None, limit=None, offset=None):
pass

def test_signature_getEndpoint(self):
@self.assertArgSpecMatches(self.data.getEndpoint)
def getEndpoint(self, path):
pass

def test_signature_startConsuming(self):
@self.assertArgSpecMatches(self.data.startConsuming)
def startConsuming(self, callback, options, path):
Expand Down Expand Up @@ -154,10 +159,14 @@ def test_scanModule(self):
# and that it added an attribute
self.assertIsInstance(self.data.rtypes.test, TestResourceType)

def test_lookup(self):
def test_getEndpoint(self):
ep = self.patchFooPattern()
self.assertEqual(self.data._lookup(('foo', '1', 'bar')),
(ep, dict(fooid=1)))
got = self.data.getEndpoint(('foo', '10', 'bar'))
self.assertEqual(got, (ep, {'fooid': 10}))

def test_getEndpoint_missing(self):
self.assertRaises(exceptions.InvalidPathError, lambda :
self.data.getEndpoint(('xyz',)))

def test_get(self):
ep = self.patchFooPattern()
Expand Down
10 changes: 10 additions & 0 deletions master/docs/developer/data.rst
Expand Up @@ -89,6 +89,16 @@ Within the buildmaster process, the root of the data API is available at `self.m

The return value is composed of simple Python objects - lists, dicts, strings, numbers, and None, along with :py:class:`~buildbot.data.base.Link` instances giving paths to other resources.

.. py:method:: getEndpoint(path)
:param tuple path: A tuple of path elements representing the API path.
Numbers can be passed as strings or integers.
:raises: :py:exc:`~buildbot.data.exceptions.InvalidPathError`
:returns: tuple of endpoint and a dictionary of keyword arguments from the path

Get the endpoint responsible for the given path, along with any arguments extracted from the path.
This can be used by callers that need access to information from the endpoint beyond that returned from ``get``.

.. py:method:: startConsuming(callback, options, kwargs)
:param callback: a function to call for each message
Expand Down

0 comments on commit d0151d7

Please sign in to comment.