Skip to content

Commit

Permalink
Added __contains__ and __delitem__ to BugManager (#315)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
ChrisTimperley authored Jan 28, 2019
1 parent 5957100 commit c55bf9d
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 3 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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
Expand Down
27 changes: 26 additions & 1 deletion bugzoo/client/bug.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,25 @@ 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.
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:
Expand All @@ -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
Expand Down
14 changes: 13 additions & 1 deletion bugzoo/mgr/bug.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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

Expand Down
12 changes: 12 additions & 0 deletions bugzoo/server/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,18 @@ def docker_images(name: str):
return UnexpectedServerError.from_exception(ex), 500


@app.route('/bugs/<uid>', 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',
Expand Down
2 changes: 1 addition & 1 deletion bugzoo/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '2.1.23'
__version__ = '2.1.24'

0 comments on commit c55bf9d

Please sign in to comment.