mirror of
https://github.com/ansible/awx.git
synced 2026-03-18 17:37:30 -02:30
Merge pull request #9862 from jakemcdermott/remove-suppress
Use built-in suppress from contextlib In python3, we use the built-in suppress from contextlib. Reviewed-by: Ryan Petrello <None>
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
from awxkit.utils import suppress
|
from contextlib import suppress
|
||||||
|
|
||||||
import awxkit.exceptions as exc
|
import awxkit.exceptions as exc
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,5 @@
|
|||||||
from awxkit.utils import suppress
|
from contextlib import suppress
|
||||||
|
|
||||||
import awxkit.exceptions as exc
|
import awxkit.exceptions as exc
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,6 @@
|
|||||||
from awxkit.utils import PseudoNamespace, random_title, suppress, update_payload, set_payload_foreign_key_args
|
from contextlib import suppress
|
||||||
|
|
||||||
|
from awxkit.utils import PseudoNamespace, random_title, update_payload, set_payload_foreign_key_args
|
||||||
from awxkit.api.resources import resources
|
from awxkit.api.resources import resources
|
||||||
from awxkit.api.mixins import HasCreate
|
from awxkit.api.mixins import HasCreate
|
||||||
import awxkit.exceptions as exc
|
import awxkit.exceptions as exc
|
||||||
|
|||||||
@@ -1,9 +1,10 @@
|
|||||||
|
from contextlib import suppress
|
||||||
import logging
|
import logging
|
||||||
import json
|
import json
|
||||||
import re
|
import re
|
||||||
|
|
||||||
from awxkit.api.pages import Credential, Organization, Project, UnifiedJob, UnifiedJobTemplate
|
from awxkit.api.pages import Credential, Organization, Project, UnifiedJob, UnifiedJobTemplate
|
||||||
from awxkit.utils import filter_by_class, random_title, update_payload, suppress, not_provided, PseudoNamespace, poll_until, random_utf8
|
from awxkit.utils import filter_by_class, random_title, update_payload, not_provided, PseudoNamespace, poll_until, random_utf8
|
||||||
from awxkit.api.mixins import DSAdapter, HasCreate, HasInstanceGroups, HasNotifications, HasVariables, HasCopy
|
from awxkit.api.mixins import DSAdapter, HasCreate, HasInstanceGroups, HasNotifications, HasVariables, HasCopy
|
||||||
from awxkit.api.resources import resources
|
from awxkit.api.resources import resources
|
||||||
import awxkit.exceptions as exc
|
import awxkit.exceptions as exc
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
|
from contextlib import suppress
|
||||||
import json
|
import json
|
||||||
|
|
||||||
from awxkit.utils import filter_by_class, not_provided, random_title, suppress, update_payload, set_payload_foreign_key_args, PseudoNamespace
|
from awxkit.utils import filter_by_class, not_provided, random_title, update_payload, set_payload_foreign_key_args, PseudoNamespace
|
||||||
from awxkit.api.pages import Credential, Inventory, Project, UnifiedJobTemplate
|
from awxkit.api.pages import Credential, Inventory, Project, UnifiedJobTemplate
|
||||||
from awxkit.api.mixins import HasCreate, HasInstanceGroups, HasNotifications, HasSurvey, HasCopy, DSAdapter
|
from awxkit.api.mixins import HasCreate, HasInstanceGroups, HasNotifications, HasSurvey, HasCopy, DSAdapter
|
||||||
from awxkit.api.resources import resources
|
from awxkit.api.resources import resources
|
||||||
|
|||||||
@@ -1,9 +1,11 @@
|
|||||||
|
from contextlib import suppress
|
||||||
|
|
||||||
from awxkit.api.mixins import HasCreate, HasCopy, DSAdapter
|
from awxkit.api.mixins import HasCreate, HasCopy, DSAdapter
|
||||||
from awxkit.api.pages import Organization
|
from awxkit.api.pages import Organization
|
||||||
from awxkit.api.resources import resources
|
from awxkit.api.resources import resources
|
||||||
from awxkit.config import config
|
from awxkit.config import config
|
||||||
import awxkit.exceptions as exc
|
import awxkit.exceptions as exc
|
||||||
from awxkit.utils import not_provided, random_title, suppress, PseudoNamespace
|
from awxkit.utils import not_provided, random_title, PseudoNamespace
|
||||||
from . import base
|
from . import base
|
||||||
from . import page
|
from . import page
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
|
from contextlib import suppress
|
||||||
|
|
||||||
from awxkit.api.mixins import HasCreate, HasInstanceGroups, HasNotifications, DSAdapter
|
from awxkit.api.mixins import HasCreate, HasInstanceGroups, HasNotifications, DSAdapter
|
||||||
from awxkit.utils import random_title, suppress, set_payload_foreign_key_args, PseudoNamespace
|
from awxkit.utils import random_title, set_payload_foreign_key_args, PseudoNamespace
|
||||||
from awxkit.api.resources import resources
|
from awxkit.api.resources import resources
|
||||||
import awxkit.exceptions as exc
|
import awxkit.exceptions as exc
|
||||||
from . import base
|
from . import base
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
from contextlib import suppress
|
||||||
import inspect
|
import inspect
|
||||||
import logging
|
import logging
|
||||||
import json
|
import json
|
||||||
@@ -6,7 +7,7 @@ import re
|
|||||||
from requests import Response
|
from requests import Response
|
||||||
import http.client as http
|
import http.client as http
|
||||||
|
|
||||||
from awxkit.utils import PseudoNamespace, is_relative_endpoint, are_same_endpoint, super_dir_set, suppress, is_list_or_tuple, to_str
|
from awxkit.utils import PseudoNamespace, is_relative_endpoint, are_same_endpoint, super_dir_set, is_list_or_tuple, to_str
|
||||||
from awxkit.api import utils
|
from awxkit.api import utils
|
||||||
from awxkit.api.client import Connection
|
from awxkit.api.client import Connection
|
||||||
from awxkit.api.registry import URLRegistry
|
from awxkit.api.registry import URLRegistry
|
||||||
|
|||||||
@@ -1,7 +1,8 @@
|
|||||||
|
from contextlib import suppress
|
||||||
|
|
||||||
from awxkit.api.pages import UnifiedJob
|
from awxkit.api.pages import UnifiedJob
|
||||||
from awxkit.api.resources import resources
|
from awxkit.api.resources import resources
|
||||||
import awxkit.exceptions as exc
|
import awxkit.exceptions as exc
|
||||||
from awxkit.utils import suppress
|
|
||||||
from . import page
|
from . import page
|
||||||
from . import base
|
from . import base
|
||||||
|
|
||||||
|
|||||||
@@ -1,5 +1,7 @@
|
|||||||
|
from contextlib import suppress
|
||||||
|
|
||||||
from awxkit.api.mixins import HasCreate, DSAdapter
|
from awxkit.api.mixins import HasCreate, DSAdapter
|
||||||
from awxkit.utils import suppress, random_title, PseudoNamespace
|
from awxkit.utils import random_title, PseudoNamespace
|
||||||
from awxkit.api.resources import resources
|
from awxkit.api.resources import resources
|
||||||
from awxkit.api.pages import Organization
|
from awxkit.api.pages import Organization
|
||||||
from awxkit.exceptions import NoContent
|
from awxkit.exceptions import NoContent
|
||||||
|
|||||||
@@ -1,9 +1,10 @@
|
|||||||
import awxkit.exceptions as exc
|
from contextlib import suppress
|
||||||
|
|
||||||
|
import awxkit.exceptions as exc
|
||||||
from awxkit.api.pages import base, WorkflowJobTemplate, UnifiedJobTemplate, JobTemplate
|
from awxkit.api.pages import base, WorkflowJobTemplate, UnifiedJobTemplate, JobTemplate
|
||||||
from awxkit.api.mixins import HasCreate, DSAdapter
|
from awxkit.api.mixins import HasCreate, DSAdapter
|
||||||
from awxkit.api.resources import resources
|
from awxkit.api.resources import resources
|
||||||
from awxkit.utils import update_payload, PseudoNamespace, suppress, random_title
|
from awxkit.utils import update_payload, PseudoNamespace, random_title
|
||||||
from . import page
|
from . import page
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@@ -1,8 +1,9 @@
|
|||||||
|
from contextlib import suppress
|
||||||
import json
|
import json
|
||||||
|
|
||||||
from awxkit.api.mixins import HasCreate, HasNotifications, HasSurvey, HasCopy, DSAdapter
|
from awxkit.api.mixins import HasCreate, HasNotifications, HasSurvey, HasCopy, DSAdapter
|
||||||
from awxkit.api.pages import Organization, UnifiedJobTemplate
|
from awxkit.api.pages import Organization, UnifiedJobTemplate
|
||||||
from awxkit.utils import filter_by_class, not_provided, update_payload, random_title, suppress, PseudoNamespace
|
from awxkit.utils import filter_by_class, not_provided, update_payload, random_title, PseudoNamespace
|
||||||
from awxkit.api.resources import resources
|
from awxkit.api.resources import resources
|
||||||
import awxkit.exceptions as exc
|
import awxkit.exceptions as exc
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import contextlib
|
from contextlib import contextmanager, suppress
|
||||||
|
|
||||||
from awxkit import api, utils, exceptions
|
from awxkit import api, utils, exceptions
|
||||||
from awxkit.config import config
|
from awxkit.config import config
|
||||||
@@ -56,7 +56,7 @@ def check_related(resource):
|
|||||||
if related in examined:
|
if related in examined:
|
||||||
continue
|
continue
|
||||||
print(related)
|
print(related)
|
||||||
with utils.suppress(exceptions.NotFound):
|
with suppress(exceptions.NotFound):
|
||||||
child_related = related.get()
|
child_related = related.get()
|
||||||
examined.append(related)
|
examined.append(related)
|
||||||
if 'results' in child_related and child_related.results:
|
if 'results' in child_related and child_related.results:
|
||||||
@@ -66,12 +66,12 @@ def check_related(resource):
|
|||||||
if not isinstance(_related, api.page.TentativePage) or _related in examined:
|
if not isinstance(_related, api.page.TentativePage) or _related in examined:
|
||||||
continue
|
continue
|
||||||
print(_related)
|
print(_related)
|
||||||
with utils.suppress(exceptions.NotFound):
|
with suppress(exceptions.NotFound):
|
||||||
_related.get()
|
_related.get()
|
||||||
examined.append(_related)
|
examined.append(_related)
|
||||||
|
|
||||||
|
|
||||||
@contextlib.contextmanager
|
@contextmanager
|
||||||
def as_user(v, username, password=None):
|
def as_user(v, username, password=None):
|
||||||
"""Context manager to allow running tests as an alternative login user."""
|
"""Context manager to allow running tests as an alternative login user."""
|
||||||
access_token = False
|
access_token = False
|
||||||
|
|||||||
@@ -362,28 +362,6 @@ def are_same_endpoint(first, second):
|
|||||||
return strip(first) == strip(second)
|
return strip(first) == strip(second)
|
||||||
|
|
||||||
|
|
||||||
@contextmanager
|
|
||||||
def suppress(*exceptions):
|
|
||||||
"""Context manager that suppresses the provided exceptions
|
|
||||||
|
|
||||||
:param exceptions: List of exceptions to suppress
|
|
||||||
|
|
||||||
Usage::
|
|
||||||
>>> with suppress(ZeroDivisionError):
|
|
||||||
>>> foo = 1/0
|
|
||||||
>>> # This code will not run
|
|
||||||
|
|
||||||
Note: This is an intermediate framework and test refactoring tool.
|
|
||||||
It's almost never a good idea to plan on using this. Also, note
|
|
||||||
that after the suppressed exception has been caught, no further
|
|
||||||
statements in the with block will be executed.
|
|
||||||
"""
|
|
||||||
try:
|
|
||||||
yield
|
|
||||||
except exceptions:
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
def utcnow():
|
def utcnow():
|
||||||
"""Provide a wrapped copy of the built-in utcnow that can be easily mocked."""
|
"""Provide a wrapped copy of the built-in utcnow that can be easily mocked."""
|
||||||
return datetime.utcnow()
|
return datetime.utcnow()
|
||||||
|
|||||||
@@ -216,32 +216,6 @@ class RecordingCallback(object):
|
|||||||
return self.value
|
return self.value
|
||||||
|
|
||||||
|
|
||||||
def test_suppress():
|
|
||||||
callback = RecordingCallback()
|
|
||||||
|
|
||||||
with utils.suppress(ZeroDivisionError, IndexError):
|
|
||||||
raise ZeroDivisionError
|
|
||||||
callback()
|
|
||||||
raise IndexError
|
|
||||||
raise KeyError
|
|
||||||
assert callback.call_count == 0
|
|
||||||
|
|
||||||
with utils.suppress(ZeroDivisionError, IndexError):
|
|
||||||
raise IndexError
|
|
||||||
callback()
|
|
||||||
raise ZeroDivisionError
|
|
||||||
raise KeyError
|
|
||||||
assert callback.call_count == 0
|
|
||||||
|
|
||||||
with pytest.raises(KeyError):
|
|
||||||
with utils.suppress(ZeroDivisionError, IndexError):
|
|
||||||
raise KeyError
|
|
||||||
callback()
|
|
||||||
raise ZeroDivisionError
|
|
||||||
raise IndexError
|
|
||||||
assert callback.call_count == 0
|
|
||||||
|
|
||||||
|
|
||||||
class TestPollUntil(object):
|
class TestPollUntil(object):
|
||||||
@pytest.mark.parametrize('timeout', [0, 0.0, -0.5, -1, -9999999])
|
@pytest.mark.parametrize('timeout', [0, 0.0, -0.5, -1, -9999999])
|
||||||
def test_callback_called_once_for_non_positive_timeout(self, timeout):
|
def test_callback_called_once_for_non_positive_timeout(self, timeout):
|
||||||
|
|||||||
Reference in New Issue
Block a user