Bug 1154681: Use static lookups in errors.py

r=jgraham

--HG--
extra : rebase_source : 722c91dc3060e92f34eb3a6070911ee62343d9ed
This commit is contained in:
Andreas Tolfsen 2015-04-21 12:26:39 +01:00
Родитель d82db4ae91
Коммит 7c51ddf2e5
2 изменённых файлов: 11 добавлений и 46 удалений

Просмотреть файл

@ -74,7 +74,7 @@ class TestLookup(marionette_test.MarionetteTestCase):
class TestAllExceptions(marionette_test.MarionetteTestCase):
def test_properties(self):
for exc in errors.excs:
for exc in errors.es_:
self.assertTrue(hasattr(exc, "code"),
"expected exception to have attribute `code'")
self.assertTrue(hasattr(exc, "status"),

Просмотреть файл

@ -6,6 +6,10 @@ import traceback
import types
class InstallGeckoError(Exception):
pass
class MarionetteException(Exception):
"""Raised when a generic non-recoverable exception has occured."""
@ -60,7 +64,6 @@ class InvalidArgumentException(MarionetteException):
class InvalidSessionIdException(MarionetteException):
status = "invalid session id"
class TimeoutException(MarionetteException):
code = (21,)
status = "timeout"
@ -182,53 +185,15 @@ class UnsupportedOperationException(MarionetteException):
status = "unsupported operation"
excs = [
ElementNotAccessibleException,
ElementNotSelectableException,
ElementNotVisibleException,
FrameSendFailureError,
FrameSendNotInitializedError,
InvalidArgumentException,
InvalidCookieDomainException,
InvalidElementCoordinates,
InvalidElementStateException,
InvalidSelectorException,
InvalidSessionIdException,
JavascriptException,
MarionetteException,
MoveTargetOutOfBoundsException,
NoAlertPresentException,
NoSuchElementException,
NoSuchFrameException,
NoSuchWindowException,
ScriptTimeoutException,
SessionNotCreatedException,
StaleElementException,
TimeoutException,
UnableToSetCookieException,
UnexpectedAlertOpen,
UnknownCommandException,
UnknownException,
UnsupportedOperationException,
]
es_ = [e for e in locals().values() if type(e) == type and issubclass(e, MarionetteException)]
by_string = {e.status: e for e in es_}
by_number = {c: e for e in es_ for c in e.code}
def lookup(identifier):
"""Finds error exception class by associated Selenium JSON wire
protocol number code, or W3C WebDriver protocol string."""
by_code = lambda exc: identifier in exc.code
by_status = lambda exc: exc.status == identifier
rv = None
lookup = by_string
if isinstance(identifier, int):
rv = filter(by_code, excs)
elif isinstance(identifier, types.StringTypes):
rv = filter(by_status, excs)
if not rv:
return MarionetteException
return rv[0]
__all__ = excs + ["lookup"]
lookup = by_number
return lookup.get(identifier, MarionetteException)