Add a Singleton metaclass, use it with Display (#48935)

* Add a Singleton class, use it with Display

* update six import

* Move remaining failes to display singleton

* Fix rebase issues

* Singleton improvements

* Add code-smell for 'from __main__ import display'. ci_complete

* s/self/cls/g

* Add docs for no-main-display

* Address linting issues

* Add changelog fragment. ci_complete

* Implement reentrant lock for class instantiation in Singleton

* Add Display singleton porting guide
This commit is contained in:
Matt Martz 2018-11-20 17:06:51 -06:00 committed by GitHub
commit 9773a1f289
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
149 changed files with 407 additions and 766 deletions

View file

@ -236,12 +236,9 @@ Here's a simple lookup plugin implementation --- this lookup returns the content
"""
from ansible.errors import AnsibleError, AnsibleParserError
from ansible.plugins.lookup import LookupBase
from ansible.utils.display import Display
try:
from __main__ import display
except ImportError:
from ansible.utils.display import Display
display = Display()
display = Display()
class LookupModule(LookupBase):

View file

@ -0,0 +1,12 @@
Sanity Tests » no-main-display
==============================
As of Ansible 2.8, ``Display`` should no longer be imported from ``__main__``.
``Display`` is now a singleton and should be utilized like the following::
from ansible.utils.display import Display
display = Display()
There is no longer a need to attempt ``from __main__ import display`` inside
a ``try/except`` block.

View file

@ -332,8 +332,8 @@ As a simple example we are going to make a hybrid ``fileglob`` lookup plugin.
from ansible.utils import (listify_lookup_plugin_terms, path_dwim, warning)
except ImportError:
# ansible-2.0
from __main__ import display
warning = display.warning
from ansible.utils.display import Display
warning = Display().warning
class LookupModule(LookupBase):

View file

@ -124,7 +124,28 @@ Plugins
Porting custom scripts
======================
No notable changes.
Display class
-------------
As of Ansible 2.8, the ``Display`` class is now a "singleton". Instead of using ``__main__.display`` each file should
import and instantiate ``ansible.utils.display.Display`` on it's own.
**OLD** In Ansible 2.7 (and earlier) the following was used to access the ``display`` object:
.. code-block:: python
try:
from __main__ import display
except ImportError:
from ansible.utils.display import Display
display = Display()
**NEW** In Ansible 2.8 the following should be used:
.. code-block:: python
from ansible.utils.display import Display
display = Display()
Networking
==========