<commit_before>#
# Kiwi: a Framework and Enhanced Widgets for Python
#
# Copyright (C) 2006 Async Open Source
#
# This library 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.
#
# This library 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 this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
# USA
#
# Author(s): <NAME> <<EMAIL>>
#            <NAME> <<EMAIL>>

import sys

try:
    from zope.interface import implements, Attribute, Interface
    # pyflakes
    implements, Attribute, Interface
except ImportError:
    class Interface(object):
        def providedBy(cls, impl):
            for iface in getattr(impl, '__interfaces__', []):
                if issubclass(iface, cls):
                    return True
            return False

        providedBy = classmethod(providedBy)

    class Attribute(object):
        def __init__(self, __name__, __doc__=''):
            self.__name__=__name__
            self.__doc__=__doc__

    def implements(iface):
        frame = sys._getframe(1)
        try:
            frame.f_locals.setdefault('__interfaces__', []).append(iface)
        finally:
            del frame

class AlreadyImplementedError(Exception):
    """Called when a utility already exists."""

class _UtilityHandler(object):
    def __init__(self):
        self._utilities = {}

    def provide(self, iface, obj):
        global _interfaces
        if not issubclass(iface, Interface):
            raise TypeError(
                "iface must be an Interface subclass and not %r" % iface)

        if iface in self._utilities:
            raise AlreadyImplementedError("%s is already implemented" % iface)
        self._utilities[iface] = obj

    def get(self, iface):
        global _interfaces
        if not issubclass(iface, Interface):
            raise TypeError(
                "iface must be an Interface subclass and not %r" % iface)

        if not iface in self._utilities:
            raise NotImplementedError("No utility provided for %r" % iface)

        return self._utilities[iface]

def provide_utility(iface, utility):
    """
    Set the utility for the named interface. If the utility is already
    set, an {AlreadyImplementedError} is raised.

    @param iface: interface to set the utility for.
    @param utility: utility providing the interface.
    """
    global _handler
    _handler.provide(iface, utility)

def get_utility(iface):
    """
    Get the utility for the named interface. If the utility is not
    available (has not been set) a {NotImplementedError} is raised.

    @param iface: interface to retrieve the utility for.
    @type iface: utility providing the interface
    """

    global _handler
    return _handler.get(iface)

_handler = _UtilityHandler()<commit_msg>Add replace argument to provide_utility<commit_after>#
# Kiwi: a Framework and Enhanced Widgets for Python
#
# Copyright (C) 2006 Async Open Source
#
# This library 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.
#
# This library 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 this library; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307
# USA
#
# Author(s): <NAME> <<EMAIL>>
#            <NAME> <<EMAIL>>

import sys

try:
    from zope.interface import implements, Attribute, Interface
    # pyflakes
    implements, Attribute, Interface
except ImportError:
    class Interface(object):
        def providedBy(cls, impl):
            for iface in getattr(impl, '__interfaces__', []):
                if issubclass(iface, cls):
                    return True
            return False

        providedBy = classmethod(providedBy)

    class Attribute(object):
        def __init__(self, __name__, __doc__=''):
            self.__name__=__name__
            self.__doc__=__doc__

    def implements(iface):
        frame = sys._getframe(1)
        try:
            frame.f_locals.setdefault('__interfaces__', []).append(iface)
        finally:
            del frame

class AlreadyImplementedError(Exception):
    """Called when a utility already exists."""

class _UtilityHandler(object):
    def __init__(self):
        self._utilities = {}

    # FIXME: How to replace a utility properly
    def provide(self, iface, obj, replace=False):
        global _interfaces
        if not issubclass(iface, Interface):
            raise TypeError(
                "iface must be an Interface subclass and not %r" % iface)

        if not replace:
            if iface in self._utilities:
                raise AlreadyImplementedError("%s is already implemented" % iface)
        self._utilities[iface] = obj

    def get(self, iface):
        global _interfaces
        if not issubclass(iface, Interface):
            raise TypeError(
                "iface must be an Interface subclass and not %r" % iface)

        if not iface in self._utilities:
            raise NotImplementedError("No utility provided for %r" % iface)

        return self._utilities[iface]

def provide_utility(iface, utility, replace=False):
    """
    Set the utility for the named interface. If the utility is already
    set, an {AlreadyImplementedError} is raised.

    @param iface: interface to set the utility for.
    @param utility: utility providing the interface.
    """
    global _handler
    _handler.provide(iface, utility, replace)

def get_utility(iface):
    """
    Get the utility for the named interface. If the utility is not
    available (has not been set) a {NotImplementedError} is raised.

    @param iface: interface to retrieve the utility for.
    @type iface: utility providing the interface
    """

    global _handler
    return _handler.get(iface)

_handler = _UtilityHandler()
{'fraction_non_alphanumeric': 0.07715311004784689, 'fraction_numerical': 0.0062799043062200955, 'mean_word_length': 2.807057484348321, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, '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': '6566193', 'n_tokens_mistral': 1832, 'n_tokens_neox': 1779, 'n_words': 718}