<commit_before># coding: utf-8

from barsup.core import init, ModuleContainer
from barsup.controller import Controller


class Container(ModuleContainer):

    class LibController(Controller):

        def __init__(self, lib):
            super().__init__()
            self.lib = lib

        def call(
            self,
            *,
            f: 'str',
            x: 'int',
            y: 'int'
        ) -> r'/{f:.*}':
            return self.lib.get(f)(x, y)

    def _get_entity(self, name):
        if name == 'LIB_CONT':
            return self.LibController
        else:
            return super()._get_entity(name)


config = {
    'controller': {
        'module': {
            '__realization__': 'barsup.controller.ModuleController',
            'module': 'm'
        }
    },
    'module': {
        'm': {
            '$config': {
                'controller': {
                    'math': {
                        '__realization__': 'LIB_CONT',
                        'lib': '../libs/math'
                    }
                }
            },
            '$container_clz': Container,
            '$get_config': lambda x: x
        },
        'libs': {
            '$config': {
                'lib': {
                    '__default__': {
                        '__type__': 'singleton',
                        '__realization__': 'builtins.dict'
                    },
                    'math': {
                        '$add': lambda x, y: x + y,
                        '$mul': lambda x, y: x * y
                    }
                }
            },
            '$container_clz': Container,
            '$get_config': lambda x: x
        }
    }
}


def test_module_container():
    api = init(config, container_clz=Container, get_config=lambda x: x)

    sub_api = api._container.get('module', 'm')
    assert sub_api.populate('/math/add', x=10, y=32) == 42
    assert sub_api.populate('/math/mul', x=10, y=32) == 320

    api.populate('/module/math/add', x=10, y=32) == 42<commit_msg>test for iter_apis<commit_after># coding: utf-8

from barsup.core import init, ModuleContainer, iter_apis
from barsup.controller import Controller


class Container(ModuleContainer):

    class LibController(Controller):

        def __init__(self, lib):
            super().__init__()
            self.lib = lib

        def call(
            self,
            *,
            f: 'str',
            x: 'int',
            y: 'int'
        ) -> r'/{f:.*}':
            return self.lib.get(f)(x, y)

    def _get_entity(self, name):
        if name == 'LIB_CONT':
            return self.LibController
        else:
            return super()._get_entity(name)


config = {
    'controller': {
        'module': {
            '__realization__': 'barsup.controller.ModuleController',
            'module': 'm'
        }
    },
    'module': {
        'm': {
            '$config': {
                'controller': {
                    'math': {
                        '__realization__': 'LIB_CONT',
                        'lib': '../libs/math'
                    }
                }
            },
            '$container_clz': Container,
            '$get_config': lambda x: x
        },
        'libs': {
            '$config': {
                'lib': {
                    '__default__': {
                        '__type__': 'singleton',
                        '__realization__': 'builtins.dict'
                    },
                    'math': {
                        '$add': lambda x, y: x + y,
                        '$mul': lambda x, y: x * y
                    }
                }
            },
            '$container_clz': Container,
            '$get_config': lambda x: x
        }
    }
}


def make_api():
    return init(config, container_clz=Container, get_config=lambda x: x)


def test_module_container():
    api = make_api()

    sub_api = api._container.get('module', 'm')
    assert sub_api.populate('/math/add', x=10, y=32) == 42
    assert sub_api.populate('/math/mul', x=10, y=32) == 320

    api.populate('/module/math/add', x=10, y=32) == 42


def test_iter_api():
    api = make_api()
    actions = [
        '/'.join(path + (ctl, action))
        for (path, api_) in sorted(iter_apis(api))
        for (ctl, action) in api_
    ]
    assert actions == [
        'module/call',
        'm/math/call',
    ]
{'fraction_non_alphanumeric': 0.14030261348005502, 'fraction_numerical': 0.009170105456212746, 'mean_word_length': 1.2783289817232375, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, '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': '6974857', 'n_tokens_mistral': 1316, 'n_tokens_neox': 1237, 'n_words': 268}