<commit_before># This file is part of fedmsg.
# Copyright (C) 2012 Red Hat, Inc.
#
# fedmsg is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# fedmsg is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with fedmsg; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
#
# Authors:  <NAME> <<EMAIL>>
#
""" Fedora Messaging Client API """

import inspect
import threading

import fedmsg.core
import fedmsg.config

__local = threading.local()

__all__ = [
    'init',
    'send_message',
    'publish',
    'subscribe',
    'destroy',
    '__local',
]


def init(**kw):
    """ Initialize an instance of :class:`fedmsg.core.FedMsgContext`.

    The config is loaded with :func:`fedmsg.config.load_config` and updated
    by any keyword arguments.  This config is used to initialize the context
    object.

    The object is stored in a thread local as
    :data:`fedmsg.__local.__context`.
    """

    if getattr(__local, '__context', None):
        raise ValueError("fedmsg already initialized")

    # Read config from CLI args and a config file
    config = fedmsg.config.load_config([], None)

    # Override the defaults with whatever the user explicitly passes in.
    config.update(kw)

    __local.__context = fedmsg.core.FedMsgContext(**config)
    return __local.__context


def API_function(doc=None):
    def api_function(func):
        scrub = inspect.getargspec(func).args

        def _wrapper(*args, **kw):
            if not hasattr(__local, '__context'):
                kw = kw.copy()

                for arg in scrub:
                    if arg in kw:
                        del kw[arg]

                init(**kw)
                assert(__local.__context)

            return func(*args, **kw)

        if not doc:
            _wrapper.__doc__ = func.__doc__
        else:
            _wrapper.__doc__ = doc

        _wrapper.__name__ = func.__name__
        return _wrapper

    return api_function


@API_function(doc=fedmsg.core.FedMsgContext.publish.__doc__)
def publish(topic=None, msg=None, **kw):
    return __local.__context.publish(topic, msg, **kw)

# This is old-school, and deprecated.
send_message = publish


@API_function(doc=fedmsg.core.FedMsgContext.destroy.__doc__)
def destroy(**kw):
    if hasattr(__local, '__context'):
        __local.__context.destroy()
        __local.__context = None


@API_function(doc=fedmsg.core.FedMsgContext.tail_messages.__doc__)
def tail_messages(**kw):
    for item in __local.__context.tail_messages(**kw):
        yield item
<commit_msg>Typofix.  Don't scrub args from the original call to func.<commit_after># This file is part of fedmsg.
# Copyright (C) 2012 Red Hat, Inc.
#
# fedmsg is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# fedmsg is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with fedmsg; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
#
# Authors:  <NAME> <<EMAIL>>
#
""" Fedora Messaging Client API """

import inspect
import threading

import fedmsg.core
import fedmsg.config

__local = threading.local()

__all__ = [
    'init',
    'send_message',
    'publish',
    'subscribe',
    'destroy',
    '__local',
]


def init(**kw):
    """ Initialize an instance of :class:`fedmsg.core.FedMsgContext`.

    The config is loaded with :func:`fedmsg.config.load_config` and updated
    by any keyword arguments.  This config is used to initialize the context
    object.

    The object is stored in a thread local as
    :data:`fedmsg.__local.__context`.
    """

    if getattr(__local, '__context', None):
        raise ValueError("fedmsg already initialized")

    # Read config from CLI args and a config file
    config = fedmsg.config.load_config([], None)

    # Override the defaults with whatever the user explicitly passes in.
    config.update(kw)

    __local.__context = fedmsg.core.FedMsgContext(**config)
    return __local.__context


def API_function(doc=None):
    def api_function(func):
        scrub = inspect.getargspec(func).args

        def _wrapper(*args, **kw):
            if not hasattr(__local, '__context'):
                config_overrides = kw.copy()

                for arg in scrub:
                    if arg in config_overrides:
                        del config_overrides[arg]

                init(**config_overrides)
                assert(__local.__context)

            return func(*args, **kw)

        if not doc:
            _wrapper.__doc__ = func.__doc__
        else:
            _wrapper.__doc__ = doc

        _wrapper.__name__ = func.__name__
        return _wrapper

    return api_function


@API_function(doc=fedmsg.core.FedMsgContext.publish.__doc__)
def publish(topic=None, msg=None, **kw):
    return __local.__context.publish(topic, msg, **kw)

# This is old-school, and deprecated.
send_message = publish


@API_function(doc=fedmsg.core.FedMsgContext.destroy.__doc__)
def destroy(**kw):
    if hasattr(__local, '__context'):
        __local.__context.destroy()
        __local.__context = None


@API_function(doc=fedmsg.core.FedMsgContext.tail_messages.__doc__)
def tail_messages(**kw):
    for item in __local.__context.tail_messages(**kw):
        yield item

{'fraction_non_alphanumeric': 0.09317776697455842, 'fraction_numerical': 0.005509641873278237, 'mean_word_length': 2.994822006472492, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1829932', 'n_tokens_mistral': 1800, 'n_tokens_neox': 1708, 'n_words': 611}