<commit_before>#!/usr/bin/env python3
#
# Copyright (c) 2015, <NAME>
# All Rights Reserved.
#
# The file is part of the lbuild project and is released under the
# 2-clause BSD license. See the file `LICENSE.txt` for the full license
# governing this code.

import os
import shutil
import logging
import itertools

import lbuild.filter
import lbuild.option

from . import utils
from . import exception
from . import repository

from .repository import Localpath
from .repository import Repository

from .exception import BlobException

LOGGER = logging.getLogger('lbuild.repository')


def verify_module_name(modulename):
    """
    Verify that the given name is a valid module name.

    Raises an exception if the name is not valid.
    """
    if len(modulename.split(":")) < 2:
        raise BlobException("Modulename '{}' must contain one or more ':' as "
                            "separator between repository and module "
                            "names".format(modulename))


class ModuleBase:

    def init(self, module):
        pass

    def prepare(self, module, options):
        pass

    def build(self, env):
        pass


class OptionNameResolver:
    """
    Option name resolver for module options.
    """
    def __init__(self, repository, module, repo_options, module_options):
        self.repository = repository
        self.module = module
        self.repo_options = repo_options
        self.module_options = module_options

    def __getitem__(self, key: str):
        option_parts = key.split(":")

        try:
            depth = len(option_parts)
            if depth < 2:
                raise exception.BlobOptionFormatException(key)
            if depth == 2:
                # Repository option
                repo, option = option_parts
                if repo == "":
                    key = "%s:%s" % (self.repository.name, option)

                return self.repo_options[key].value
            else:
                option_name = option_parts[-1]
                module_name = option_parts[0:-1]

                module_fullname_parts = self.module.fullname.split(":")
                if len(module_fullname_parts) > (depth - 1):
                    module_fullname_parts[:depth - 1]

                name = []
                for part, fill in itertools.zip_longest(module_name,
                                                        module_fullname_parts,
                                                        fillvalue=""):
                    if part == "":
                        name.append(fill)
                    else:
                        name.append(part)
                name.append(option_name)
                return self.module_options[":".join(name)].value
        except KeyError:
            raise BlobException("Unknown option name '{}' in "
                                "module '{}'".format(key, self.module.fullname))

    def __repr__(self):
        # Create representation of merged module and repository options
        options = self.module_options.copy()
        options.update(self.repo_options)

        return repr(options)

    def __len__(self):
        return len(self.module_options) + len(self.repo_options)


class Module:

    @staticmethod<commit_msg>Allow to access module options directly.<commit_after>#!/usr/bin/env python3
#
# Copyright (c) 2015, <NAME>
# All Rights Reserved.
#
# The file is part of the lbuild project and is released under the
# 2-clause BSD license. See the file `LICENSE.txt` for the full license
# governing this code.

import os
import shutil
import logging
import itertools

import lbuild.filter
import lbuild.option

from . import utils
from . import exception
from . import repository

from .repository import Localpath
from .repository import Repository

from .exception import BlobException

LOGGER = logging.getLogger('lbuild.repository')


def verify_module_name(modulename):
    """
    Verify that the given name is a valid module name.

    Raises an exception if the name is not valid.
    """
    if len(modulename.split(":")) < 2:
        raise BlobException("Modulename '{}' must contain one or more ':' as "
                            "separator between repository and module "
                            "names".format(modulename))


class ModuleBase:

    def init(self, module):
        pass

    def prepare(self, module, options):
        pass

    def build(self, env):
        pass


class OptionNameResolver:
    """
    Option name resolver for module options.
    """
    def __init__(self, repository, module, repo_options, module_options):
        self.repository = repository
        self.module = module
        self.repo_options = repo_options
        self.module_options = module_options

    def __getitem__(self, key: str):
        option_parts = key.split(":")

        try:
            depth = len(option_parts)
            if depth < 2:
                key = "{}:{}".format(self.module.fullname, key)
                return self.module_options[key].value
            if depth == 2:
                # Repository option
                repo, option = option_parts
                if repo == "":
                    key = "%s:%s" % (self.repository.name, option)

                return self.repo_options[key].value
            else:
                option_name = option_parts[-1]
                module_name = option_parts[0:-1]

                module_fullname_parts = self.module.fullname.split(":")
                if len(module_fullname_parts) > (depth - 1):
                    module_fullname_parts[:depth - 1]

                name = []
                for part, fill in itertools.zip_longest(module_name,
                                                        module_fullname_parts,
                                                        fillvalue=""):
                    if part == "":
                        name.append(fill)
                    else:
                        name.append(part)
                name.append(option_name)
                key = ":".join(name)
                return self.module_options[key].value
        except KeyError:
            raise BlobException("Unknown option name '{}' in "
                                "module '{}'".format(key, self.module.fullname))

    def __repr__(self):
        # Create representation of merged module and repository options
        options = self.module_options.copy()
        options.update(self.repo_options)

        return repr(options)

    def __len__(self):
        return len(self.module_options) + len(self.repo_options)


class Module:

    @staticmethod
{'fraction_non_alphanumeric': 0.08789712556732224, 'fraction_numerical': 0.004236006051437216, 'mean_word_length': 1.7965313028764804, 'pattern_counts': {'":': 12, '<': 9, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2079831', 'n_tokens_mistral': 1757, 'n_tokens_neox': 1663, 'n_words': 485}