<commit_before>#
# Copyright 2017 Sangoma Technologies Inc.
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
from builtins import object
import importlib
import pytest
from lab import config_v1


class RoleNotFound(LookupError):
    pass


class Roles(object):
    def __init__(self, config):
        self.config = config
        self.data = None
        self.loaded = {}

        self.config.hook.pytest_lab_map.call_historic(
            kwargs=dict(config=self.config, roles=self)
        )

    def load(self, config, zone=None):
        self.data = config_v1.Union(config, zone=zone)

    def enumerate(self, keys):
        try:
            for key in keys:
                result = self.loaded.get(key)
                if result:
                    yield result
        except TypeError:
            result = self.loaded.get(keys)
            if result:
                yield result

    def get(self, key, default=None):
        self.loaded.get(key, default)

    def aquire(self, key):
        if not self.data:
            raise RoleNotFound(key)

        role = self.loaded.get(key)
        if not role:
            roledata = next(self.data[key].itervalues())<commit_msg>Cache roles with weakrefs<commit_after>#
# Copyright 2017 Sangoma Technologies Inc.
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at http://mozilla.org/MPL/2.0/.
from builtins import object
import importlib
import weakref
import pytest
from lab import config_v1


class RoleNotFound(LookupError):
    pass


class Roles(object):
    def __init__(self, config):
        self.config = config
        self.data = None
        self.loaded = weakref.WeakValueDictionary()

        self.config.hook.pytest_lab_map.call_historic(
            kwargs=dict(config=self.config, roles=self)
        )

    def load(self, config, zone=None):
        self.data = config_v1.Union(config, zone=zone)

    def enumerate(self, keys):
        try:
            for key in keys:
                result = self.loaded.get(key)
                if result:
                    yield result
        except TypeError:
            result = self.loaded.get(keys)
            if result:
                yield result

    def get(self, key, default=None):
        self.loaded.get(key, default)

    def aquire(self, key):
        if not self.data:
            raise RoleNotFound(key)

        role = self.loaded.get(key)
        if not role:
            roledata = next(self.data[key].itervalues())
{'fraction_non_alphanumeric': 0.08920704845814978, 'fraction_numerical': 0.007342143906020558, 'mean_word_length': 2.1071835803876855, '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': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3350792', 'n_tokens_mistral': 819, 'n_tokens_neox': 772, 'n_words': 241}