import inspect


def library(scope=None, version=None, converters=None, doc_format=None, listener=None,
            auto_keywords=False):
    """Class decorator to control keyword discovery and other library settings.

    By default disables automatic keyword detection by setting class attribute
    ``ROBOT_AUTO_KEYWORDS = False`` to the decorated library. In that mode
    only methods decorated explicitly with the :func:`keyword` decorator become
    keywords. If that is not desired, automatic keyword discovery can be
    enabled by using ``auto_keywords=True``.

    Arguments ``scope``, ``version``, ``converters``, ``doc_format`` and ``listener``
    set library's scope, version, converters, documentation format and listener by
    using class attributes ``ROBOT_LIBRARY_SCOPE``, ``ROBOT_LIBRARY_VERSION``,
    ``ROBOT_LIBRARY_CONVERTERS``, ``ROBOT_LIBRARY_DOC_FORMAT`` and
    ``ROBOT_LIBRARY_LISTENER``, respectively. These attributes are only set if
    the related arguments are given and they override possible existing attributes
    in the decorated class.

    Examples::

        @library
        class KeywordDiscovery:

            @keyword
            def do_something(self):
                # ...

            def not_keyword(self):
                # ...


        @library(scope='GLOBAL', version='3.2')
        class LibraryConfiguration:
            # ...

    The ``@library`` decorator is new in Robot Framework 3.2.
    The ``converters`` argument is new in Robot Framework 5.0.
    """
    if inspect.isclass(scope):
        return library()(scope)

    def decorator(cls):
        if scope is not None:
            cls.ROBOT_LIBRARY_SCOPE = scope
        if version is not None:
            cls.ROBOT_LIBRARY_VERSION = version
        if converters is not None:
            cls.ROBOT_LIBRARY_CONVERTERS = converters
        if doc_format is not None:
            cls.ROBOT_LIBRARY_DOC_FORMAT = doc_format
        if listener is not None:
            cls.ROBOT_LIBRARY_LISTENER = listener
        cls.ROBOT_AUTO_KEYWORDS = auto_keywords
        return cls

    return decorator