<commit_before>import logging
import codecs
from traceback import format_exception
from datetime import datetime

from nose.plugins import Plugin

from noseachievements.data import AchievementData
from noseachievements.manager import (AchievementManager,
                                      FilterAchievementManager,
                                      default_manager)


log = logging.getLogger(__name__)


class AchievementsPlugin(Plugin):
    name = 'achievements'
    score = -1000
    default_filename = '.achievements'
    default_achievements = 'all'

    def __init__(self, achievements=default_manager, data=None):
        Plugin.__init__(self)
        if callable(achievements):
            achievements = achievements()
        if not isinstance(achievements, AchievementManager):
            achievements = AchievementManager(achievements)
        self.achievements = achievements
        self.data = AchievementData(data or {})

    def options(self, parser, env):
        Plugin.options(self, parser, env)
        parser.add_option('--achievements-file', action='store',
            default=env.get('ACHIEVEMENTS_FILE', self.default_filename),
            metavar='FILE', dest='data_filename',
            help="Load and save achievement data in FILE. "
                 "An empty string will disable loading and saving. "
                 "[ACHIEVEMENTS_FILE]")
        parser.add_option('--achievements', action='store',
            default=env.get('ACHIEVEMENTS', self.default_achievements),
            metavar='FILTER', dest='achievements',
            help="Select or exclude specific achievements or achievement "
                 "groups. [ACHIEVEMENTS]")
    
    def configure(self, options, conf):
        Plugin.configure(self, options, conf)

        self.data_filename = options.data_filename or None

        if options.achievements is not None:
            self.achievements = FilterAchievementManager(options.achievements,
                                                         self.achievements)

        self.achievements.load()
        for achievement in self.achievements:
            achievement.configure(options, conf)

    def begin(self):
        if self.data_filename:
            try:
                data_file = open(self.data_filename, 'rb')
            except IOError:
                log.debug("Failed to read achievement data from %s",
                          self.data_filename)
            else:
                data = AchievementData.load(data_file)
                data_file.close()
                log.info("Loaded achievement data from %s",
                         self.data_filename)
                history = data.pop('history', [])
                history.append(data)
                del history[:-10]
                self.data.setdefault('history', history)
                self.data.setdefault('achievements.unlocked',
                                     data.get('achievements.unlocked', {}))

        self.data.setdefault('history', [])
        self.data.setdefault('achievements.unlocked', {})
        self.data.setdefault('achievements.new', [])
        self.data.setdefault('result.tests', [])
        self.data.setdefault('result.string', '')
        self.data.setdefault('result.errors', [])
        self.data.setdefault('result.failures', [])
        self.data.setdefault('time.start', datetime.now())

    def addError(self, test, err):
        type_, value, traceback = err
        exc_string = "".join(format_exception(type_, value, traceback))
        self.data['result.string'] += 'E'
        self.data['result.errors'].append((test.address(),
                                           (type_, value, exc_string)))

    def addFailure(self, test, err):
        type_, value, traceback = err
        exc_string = "".join(format_exception(type_, value, traceback))
        self.data['result.string'] += 'F'
        self.data['result.failures'].append((test.address(),
                                             (type_, value, exc_string)))

    def addSuccess(self, test):
        self.data['result.string'] += '.'

    def afterTest(self, test):
        self.data['result.tests'].append(test.address())

    def setOutputStream(self, stream):
        self.output_stream = stream<commit_msg>Use nose.util.test_address in case tests are not nose test cases.<commit_after>import logging
import codecs
from traceback import format_exception
from datetime import datetime

from nose.plugins import Plugin
from nose.util import test_address

from noseachievements.data import AchievementData
from noseachievements.manager import (AchievementManager,
                                      FilterAchievementManager,
                                      default_manager)


log = logging.getLogger(__name__)


class AchievementsPlugin(Plugin):
    name = 'achievements'
    score = -1000
    default_filename = '.achievements'
    default_achievements = 'all'

    def __init__(self, achievements=default_manager, data=None):
        Plugin.__init__(self)
        if callable(achievements):
            achievements = achievements()
        if not isinstance(achievements, AchievementManager):
            achievements = AchievementManager(achievements)
        self.achievements = achievements
        self.data = AchievementData(data or {})

    def options(self, parser, env):
        Plugin.options(self, parser, env)
        parser.add_option('--achievements-file', action='store',
            default=env.get('ACHIEVEMENTS_FILE', self.default_filename),
            metavar='FILE', dest='data_filename',
            help="Load and save achievement data in FILE. "
                 "An empty string will disable loading and saving. "
                 "[ACHIEVEMENTS_FILE]")
        parser.add_option('--achievements', action='store',
            default=env.get('ACHIEVEMENTS', self.default_achievements),
            metavar='FILTER', dest='achievements',
            help="Select or exclude specific achievements or achievement "
                 "groups. [ACHIEVEMENTS]")
    
    def configure(self, options, conf):
        Plugin.configure(self, options, conf)

        self.data_filename = options.data_filename or None

        if options.achievements is not None:
            self.achievements = FilterAchievementManager(options.achievements,
                                                         self.achievements)

        self.achievements.load()
        for achievement in self.achievements:
            achievement.configure(options, conf)

    def begin(self):
        if self.data_filename:
            try:
                data_file = open(self.data_filename, 'rb')
            except IOError:
                log.debug("Failed to read achievement data from %s",
                          self.data_filename)
            else:
                data = AchievementData.load(data_file)
                data_file.close()
                log.info("Loaded achievement data from %s",
                         self.data_filename)
                history = data.pop('history', [])
                history.append(data)
                del history[:-10]
                self.data.setdefault('history', history)
                self.data.setdefault('achievements.unlocked',
                                     data.get('achievements.unlocked', {}))

        self.data.setdefault('history', [])
        self.data.setdefault('achievements.unlocked', {})
        self.data.setdefault('achievements.new', [])
        self.data.setdefault('result.tests', [])
        self.data.setdefault('result.string', '')
        self.data.setdefault('result.errors', [])
        self.data.setdefault('result.failures', [])
        self.data.setdefault('time.start', datetime.now())

    def addError(self, test, err):
        type_, value, traceback = err
        exc_string = "".join(format_exception(type_, value, traceback))
        self.data['result.string'] += 'E'
        self.data['result.errors'].append((test_address(test),
                                           (type_, value, exc_string)))

    def addFailure(self, test, err):
        type_, value, traceback = err
        exc_string = "".join(format_exception(type_, value, traceback))
        self.data['result.string'] += 'F'
        self.data['result.failures'].append((test_address(test),
                                             (type_, value, exc_string)))

    def addSuccess(self, test):
        self.data['result.string'] += '.'

    def afterTest(self, test):
        self.data['result.tests'].append(test_address(test))

    def setOutputStream(self, stream):
        self.output_stream = stream
{'fraction_non_alphanumeric': 0.108592575459697, 'fraction_numerical': 0.0013877645426159362, 'mean_word_length': 2.134469010511055, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 18, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3277997', 'n_tokens_mistral': 2269, 'n_tokens_neox': 2200, 'n_words': 458}