From c55bf9d5167521467155e16838cad90328e8a3b3 Mon Sep 17 00:00:00 2001 From: Chris Timperley Date: Mon, 28 Jan 2019 15:40:51 -0500 Subject: [PATCH] Added __contains__ and __delitem__ to BugManager (#315) * updated version * added __contains__ to client-side BugManager * added __delitem__ to client-side BugManager * added __contains__ to server-side BugManager * added __delitem__ to server-side BugManager * added DELETE /bugs/:uid route to server * bug fix: wrong HTTP verb --- CHANGELOG.md | 8 ++++++++ bugzoo/client/bug.py | 27 ++++++++++++++++++++++++++- bugzoo/mgr/bug.py | 14 +++++++++++++- bugzoo/server/__init__.py | 12 ++++++++++++ bugzoo/version.py | 2 +- 5 files changed, 60 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index ee5ccf9f3..9380b0dc0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,11 @@ +## 2.1.24 (XXXX-XX-XX) + +### Features + +* Added `__contains__` to client- and server-side `BugManager`. +* Added `__delitem__` to client- and server-side `BugManager`. + + ## 2.1.23 (2019-02-28) ### Features diff --git a/bugzoo/client/bug.py b/bugzoo/client/bug.py index a961b0866..12e8af078 100644 --- a/bugzoo/client/bug.py +++ b/bugzoo/client/bug.py @@ -20,6 +20,17 @@ class BugManager(object): def __init__(self, api: APIClient) -> None: self.__api = api + def __contains__(self, name: str) -> bool: + """ + Determines whether there is a bug registered under a given name. + """ + r = self.__api.get('bugs/{}'.format(name)) + if r.status_code == 200: + return True + elif r.status_code == 404: + return False + self.__api.handle_erroneous_response(r) + def __getitem__(self, name: str) -> Bug: """ Retrieves the bug registered under a given name. @@ -27,7 +38,7 @@ def __getitem__(self, name: str) -> Bug: Raises: KeyError: if no bug is found with the given name. """ - logger.info("Fetching information for bug: %s", name) + logger.debug("Fetching information for bug: %s", name) r = self.__api.get('bugs/{}'.format(name)) if r.status_code == 200: @@ -39,6 +50,20 @@ def __getitem__(self, name: str) -> Bug: logger.info("Unexpected API response when retrieving bug: %s", name) self.__api.handle_erroneous_response(r) + def __delitem__(self, name: str) -> None: + """ + Deregisters a bug under a given name. + + Raises: + KeyError: if no bug is found with the given name. + """ + r = self.__api.delete('bugs/{}'.format(name)) + if r.status_code == 204: + return + if r.status_code == 404: + raise KeyError("no bug found with given name: {}".format(name)) + self.__api.handle_erroneous_response(r) + def __iter__(self) -> Iterator[str]: """ Returns an iterator over the names of the bugs registered with diff --git a/bugzoo/mgr/bug.py b/bugzoo/mgr/bug.py index 376798faa..7063a5497 100644 --- a/bugzoo/mgr/bug.py +++ b/bugzoo/mgr/bug.py @@ -21,6 +21,12 @@ def __init__(self, self.__installation = installation self.__bugs = {} + def __contains__(self, name: str) -> bool: + """ + Determines whether a bug is registered under a given name. + """ + return name in self.__bugs + def __getitem__(self, name: str) -> Bug: """ Attempts to fetch the description of a named bug. @@ -50,11 +56,17 @@ def register(self, bug: Bug) -> None: add = register + def __delitem__(self, name: str) -> None: + """ + Attempts to deregister a bug with a given name. + """ + del self.__bugs[name] + def deregister(self, bug: Bug) -> None: """ Attempts to deregister a given bug from this manager. """ - del self.__bugs[bug.name] + del self[bug.name] remove = deregister diff --git a/bugzoo/server/__init__.py b/bugzoo/server/__init__.py index 3c5a968e0..653a719d2 100644 --- a/bugzoo/server/__init__.py +++ b/bugzoo/server/__init__.py @@ -580,6 +580,18 @@ def docker_images(name: str): return UnexpectedServerError.from_exception(ex), 500 +@app.route('/bugs/', methods=['DELETE']) +@throws_errors +def deregister_bug(uid: str): + try: + del daemon.bugs[uid] + return '', 204 + except KeyError: + return BugNotFound(uid), 404 + except Exception as ex: + return UnexpectedServerError.from_exception(ex), 500 + + def run(*, port: int = 6060, host: str = '0.0.0.0', diff --git a/bugzoo/version.py b/bugzoo/version.py index 648bf4878..c2ca560bc 100644 --- a/bugzoo/version.py +++ b/bugzoo/version.py @@ -1 +1 @@ -__version__ = '2.1.23' +__version__ = '2.1.24'