Skip to content

Commit

Permalink
Split name-to-backend conversion into its own function and associated…
Browse files Browse the repository at this point in the history
… exception
  • Loading branch information
LinuxMercedes committed Dec 28, 2018
1 parent a794f74 commit a8fb4a3
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 13 deletions.
14 changes: 14 additions & 0 deletions assigner/backends/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,17 @@
from assigner.backends.mock import MockBackend

pyflakes = [BackendBase, RepoError, GitlabBackend, MockBackend]

backend_names = {
"gitlab": GitlabBackend,
"mock": MockBackend,
}

class NoSuchBackend(Exception):
pass

def from_name(name: str):
try:
return backend_names[name]
except KeyError:
raise NoSuchBackend("Cannot find backend with name {}".format(name))
15 changes: 4 additions & 11 deletions assigner/backends/decorators.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import logging

from assigner.backends import GitlabBackend, MockBackend
from assigner.backends import from_name
from assigner.config import requires_config


Expand All @@ -11,15 +11,8 @@ def requires_config_and_backend(func):
"""Provides a backend depending on configuration."""
@requires_config
def wrapper(config, cmdargs, *args, **kwargs):
try:
config.backend
except KeyError:
logger.info(
"The 'backend' field in config is not set; it will default to Gitlab."
)
return func(config, GitlabBackend, cmdargs, *args, **kwargs)

if config.backend["name"] == "gitlab":
return func(config, GitlabBackend, cmdargs, *args, **kwargs)
return func(config, MockBackend, cmdargs, *args, **kwargs)
backend = from_name(config.backend["name"])

return func(config, backend, cmdargs, *args, **kwargs)
return wrapper
4 changes: 2 additions & 2 deletions assigner/tests/commands/get_integration_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def setUp(self):
"assigner.commands.get.get_filtered_roster", autospec=True
)
self.mock_backend = self._create_patch(
"assigner.backends.decorators.GitlabBackend", autospec=True
"assigner.backends.GitlabBackend", autospec=True
)
self.mock_os = self._create_patch(
"assigner.commands.get.os", autospec=True
Expand All @@ -27,7 +27,7 @@ def setUp(self):
"path": "somepath/",
"hw_name": "HW2",
"section": "SP2017",
"student": "",
"student": MagicMock(),
"branch": ["a", "b"],
"force": MagicMock()
})
Expand Down

0 comments on commit a8fb4a3

Please sign in to comment.