<commit_before># Copyright (c) 2005-2006 rPath, Inc.
#
# This program is distributed under the terms of the Common Public License,
# version 1.0. A copy of this license should have been distributed with this
# source file in a file called LICENSE. If it is not present, the license
# is always available at http://www.rpath.com/permanent/licenses/CPL-1.0.
#
# This program 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 Common Public License for
# full details.
#

import sys
import traceback
import types

from conary import errors
from conary.lib import log

def passExceptions(f):
    f._passExceptions = True
    return f

def exceptionProtection(method, exceptionCallback):
    def wrapper(*args, **kwargs):
        if hasattr(method, '_passExceptions') and method._passExceptions:
            return method(*args, **kwargs)

        try:
            return method(*args, **kwargs)
        except Exception, e:
            exc_info = sys.exc_info()
            if errors.exceptionIsUncatchable(e):
                raise
            exceptionCallback(exc_info)

    return wrapper

class Callback(object):

    def _exceptionOccured(self, exc_info):
        etype, e, tb = exc_info
        # format the exception
        msg = '%s' % etype.__name__
        s = str(e)
        if s:
            msg += ': %s' % s
        # get the line info that raised the exception
        inner = tb.tb_next
        while inner.tb_next:
            inner = inner.tb_next
        filename = inner.tb_frame.f_code.co_filename
        linenum = inner.tb_frame.f_lineno
        log.warning('Unhandled exception occurred when invoking callback:\n'
                    '%s:%s\n'
                    ' %s', filename, linenum, msg)
        # log the full traceback if debugging (--debug=all)
        log.debug(''.join(traceback.format_exception(*exc_info)))
        if not hasattr(self, 'exceptions'):
            self.exceptions = []
        self.exceptions.append(e)

    def __getattribute__(self, name):
        item = object.__getattribute__(self, name)
        if name[0] == '_':
            return item
        elif not isinstance(item, types.MethodType):
            return item

        return exceptionProtection(item, self._exceptionOccured)

    def cancelOperation(self):
        """Return True if we should cancel the operation as soon as it is
        safely possible"""
        if not hasattr(self, 'exceptions'):
            return False
        for exc in self.exceptions:
            if hasattr(exc, 'cancelOperation'):
                return exc.cancelOperation
        return False

    def __init__(self):
        self.exceptions = []

class ChangesetCallback(Callback):

    def preparingChangeSet(self):
        pass

    def requestingChangeSet(self):
        pass

    def sendingChangeset(self, sent, total):
        pass

    def setRate(self, rate):
        self.rate = rate

    def downloadingChangeSet(self, got, need):
        pass

    def requestingFileContents(self):
        pass

    def downloadingFileContents(self, got, need):
        pass

    def setChangesetHunk(self, hunk, hunkCount):
        pass

    def checkAbort(self):
        pass

    def done(self):
        pass

    def error(self, msg, *args, **kwargs):
        """Error handling callback
        If the optional keyword argument exc_text is passed, its value should
        be printed verbatim since it is traceback information.
        """
        exc_text = kwargs.pop('exc_text', None)
        # Append the traceback to the message
        if exc_text:
            msg += "\n%s"
            args += (exc_text, )
        return log.error(msg, *args, **kwargs)

    def warning(self, msg, *args, **kwargs):
        """Warning handling callback
        If the optional keyword argument exc_text is passed, its value should
        be printed verbatim since it is traceback information.
        """
        exc_text = kwargs.pop('exc_text', None)
        # Append the traceback to the message
        if exc_text:
            msg += "\n%s"
            args += (exc_text, )
        return log.warning(msg, *args, **kwargs)

    def missingFiles(self, missingFiles):
        """This callback gets called if missing files were detected in the
        upstream server
        @param missingFiles: a list of tuples:
          (troveName, troveVersion, troveFlavor, pathId, path, fileId, version)
        """
        return False

    def __init__(self):
        Callback.__init__(self)
        self.rate = 0

class CookCallback(ChangesetCallback):

    def buildingChangeset(self):
        pass

    def findingTroves(self, num):
        pass
<commit_msg>Updated doc strings for error and warning (DOC-399)<commit_after># Copyright (c) 2005-2006 rPath, Inc.
#
# This program is distributed under the terms of the Common Public License,
# version 1.0. A copy of this license should have been distributed with this
# source file in a file called LICENSE. If it is not present, the license
# is always available at http://www.rpath.com/permanent/licenses/CPL-1.0.
#
# This program 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 Common Public License for
# full details.
#

import sys
import traceback
import types

from conary import errors
from conary.lib import log

def passExceptions(f):
    f._passExceptions = True
    return f

def exceptionProtection(method, exceptionCallback):
    def wrapper(*args, **kwargs):
        if hasattr(method, '_passExceptions') and method._passExceptions:
            return method(*args, **kwargs)

        try:
            return method(*args, **kwargs)
        except Exception, e:
            exc_info = sys.exc_info()
            if errors.exceptionIsUncatchable(e):
                raise
            exceptionCallback(exc_info)

    return wrapper

class Callback(object):

    def _exceptionOccured(self, exc_info):
        etype, e, tb = exc_info
        # format the exception
        msg = '%s' % etype.__name__
        s = str(e)
        if s:
            msg += ': %s' % s
        # get the line info that raised the exception
        inner = tb.tb_next
        while inner.tb_next:
            inner = inner.tb_next
        filename = inner.tb_frame.f_code.co_filename
        linenum = inner.tb_frame.f_lineno
        log.warning('Unhandled exception occurred when invoking callback:\n'
                    '%s:%s\n'
                    ' %s', filename, linenum, msg)
        # log the full traceback if debugging (--debug=all)
        log.debug(''.join(traceback.format_exception(*exc_info)))
        if not hasattr(self, 'exceptions'):
            self.exceptions = []
        self.exceptions.append(e)

    def __getattribute__(self, name):
        item = object.__getattribute__(self, name)
        if name[0] == '_':
            return item
        elif not isinstance(item, types.MethodType):
            return item

        return exceptionProtection(item, self._exceptionOccured)

    def cancelOperation(self):
        """Return True if we should cancel the operation as soon as it is
        safely possible"""
        if not hasattr(self, 'exceptions'):
            return False
        for exc in self.exceptions:
            if hasattr(exc, 'cancelOperation'):
                return exc.cancelOperation
        return False

    def __init__(self):
        self.exceptions = []

class ChangesetCallback(Callback):

    def preparingChangeSet(self):
        pass

    def requestingChangeSet(self):
        pass

    def sendingChangeset(self, sent, total):
        pass

    def setRate(self, rate):
        self.rate = rate

    def downloadingChangeSet(self, got, need):
        pass

    def requestingFileContents(self):
        pass

    def downloadingFileContents(self, got, need):
        pass

    def setChangesetHunk(self, hunk, hunkCount):
        pass

    def checkAbort(self):
        pass

    def done(self):
        pass

    def error(self, msg, *args, **kwargs):
        """Error handling callback

        @param msg: A message to display
        @type msg: str
        @keyword exc_text: Traceback text that should be printed verbatim
        @type exc_text: str
        """
        exc_text = kwargs.pop('exc_text', None)
        # Append the traceback to the message
        if exc_text:
            msg += "\n%s"
            args += (exc_text, )
        return log.error(msg, *args, **kwargs)

    def warning(self, msg, *args, **kwargs):
        """Warning handling callback

        @param msg: A message to display
        @type msg: str
        @keyword exc_text: Traceback text that should be printed verbatim
        @type exc_text: str
        """
        exc_text = kwargs.pop('exc_text', None)
        # Append the traceback to the message
        if exc_text:
            msg += "\n%s"
            args += (exc_text, )
        return log.warning(msg, *args, **kwargs)

    def missingFiles(self, missingFiles):
        """This callback gets called if missing files were detected in the
        upstream server
        @param missingFiles: a list of tuples:
          (troveName, troveVersion, troveFlavor, pathId, path, fileId, version)
        """
        return False

    def __init__(self):
        Callback.__init__(self)
        self.rate = 0

class CookCallback(ChangesetCallback):

    def buildingChangeset(self):
        pass

    def findingTroves(self, num):
        pass

{'fraction_non_alphanumeric': 0.08689784053156147, 'fraction_numerical': 0.00321843853820598, 'mean_word_length': 2.273190621814475, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3470166', 'n_tokens_mistral': 2675, 'n_tokens_neox': 2582, 'n_words': 883}