<commit_before>#!/usr/bin/env python3
#
# Copyright (c) 2019 <NAME>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

"""Runtime configuration."""

import uuid

from importlib import import_module
from pymodm import MongoModel, fields

import empower.core.serialize as serialize

from empower.main import srv_or_die


class Env(MongoModel):
    """Env class

    Attributes:
        project_id: The project identifier. Notice how there is only one Env
            instance active at any given time and that the project_id of the
            workers loaded by Env is always set to the Env's project_id
        bootstrap: the list of services to be loaded at bootstrap with their
            configuration
        storage: the configuration of the services
        services: the services
    """

    project_id = fields.UUIDField(primary_key=True)
    bootstrap = fields.DictField(required=False, blank=True)
    storage = fields.DictField(required=False, blank=True)

    def __init__(self, *args, **kwargs):

        super().__init__(*args, **kwargs)

        self.services = {}

        # Save pointer to LVAPPManager
        self.lvapp_manager = \
            srv_or_die("empower.managers.ranmanager.lvapp.lvappmanager")

        # Save pointer to VBSPManager
        self.vbsp_manager = \
            srv_or_die("empower.managers.ranmanager.vbsp.vbspmanager")

    def get_service(self, name, **kwargs):
        """Get a service.

        Return a service with the same name and parameters if already running
        or start a new one."""

        if not kwargs:
            kwargs = {}

        kwargs['service_id'] = uuid.uuid4()
        kwargs['project_id'] = self.project_id

        init_method = getattr(import_module(name), "launch")
        requested = init_method(**kwargs)

        for service in self.services.values():
            if service == requested:
                return service

        service = self.register_service(service_id=uuid.uuid4(), name=name,
                                        params=kwargs)

        return service

    def save_service_state(self, service_id):
        """Save service state."""

        service = self.services[service_id]

        self.bootstrap[str(service.service_id)] = {
            "name": service.name,
            "params": serialize.serialize(service.params)
        }

        self.storage[str(service.service_id)] = service.storage

        self.save()

    def register_service(self, service_id, name, params):
        """Register service."""

        if str(service_id) in self.services:
            raise ValueError("Service %s already registered" % service_id)

        params['service_id'] = service_id
        params['project_id'] = self.project_id

        storage = {}

        self.start_service(service_id, name, params, storage)
        self.save_service_state(service_id)

        return self.services[service_id]

    def unregister_service(self, service_id):
        """Unregister service."""

        if str(service_id) not in self.services:
            raise ValueError("Service %s not registered" % service_id)

        self.stop_service(service_id)

        del self.bootstrap[str(service_id)]
        del self.storage[str(service_id)]

        self.save()

    def reconfigure_service(self, service_id, params):
        """Reconfigure service."""

        if str(service_id) not in self.services:
            raise ValueError("Service %s not registered" % service_id)

        service = self.services[service_id]

        for param in params:

            if param not in self.bootstrap[str(service_id)]['params']:
                raise KeyError("Param %s undefined" % param)

            setattr(service, param, params[param])
<commit_msg>Do not cast to string when accessing service dictionary.<commit_after>#!/usr/bin/env python3
#
# Copyright (c) 2019 <NAME>
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

"""Runtime configuration."""

import uuid

from importlib import import_module
from pymodm import MongoModel, fields

import empower.core.serialize as serialize

from empower.main import srv_or_die


class Env(MongoModel):
    """Env class

    Attributes:
        project_id: The project identifier. Notice how there is only one Env
            instance active at any given time and that the project_id of the
            workers loaded by Env is always set to the Env's project_id
        bootstrap: the list of services to be loaded at bootstrap with their
            configuration
        storage: the configuration of the services
        services: the services
    """

    project_id = fields.UUIDField(primary_key=True)
    bootstrap = fields.DictField(required=False, blank=True)
    storage = fields.DictField(required=False, blank=True)

    def __init__(self, *args, **kwargs):

        super().__init__(*args, **kwargs)

        self.services = {}

        # Save pointer to LVAPPManager
        self.lvapp_manager = \
            srv_or_die("empower.managers.ranmanager.lvapp.lvappmanager")

        # Save pointer to VBSPManager
        self.vbsp_manager = \
            srv_or_die("empower.managers.ranmanager.vbsp.vbspmanager")

    def get_service(self, name, **kwargs):
        """Get a service.

        Return a service with the same name and parameters if already running
        or start a new one."""

        if not kwargs:
            kwargs = {}

        kwargs['service_id'] = uuid.uuid4()
        kwargs['project_id'] = self.project_id

        init_method = getattr(import_module(name), "launch")
        requested = init_method(**kwargs)

        for service in self.services.values():
            if service == requested:
                return service

        service = self.register_service(service_id=uuid.uuid4(), name=name,
                                        params=kwargs)

        return service

    def save_service_state(self, service_id):
        """Save service state."""

        service = self.services[service_id]

        self.bootstrap[str(service.service_id)] = {
            "name": service.name,
            "params": serialize.serialize(service.params)
        }

        self.storage[str(service.service_id)] = service.storage

        self.save()

    def register_service(self, service_id, name, params):
        """Register service."""

        if service_id in self.services:
            raise ValueError("Service %s already registered" % service_id)

        params['service_id'] = service_id
        params['project_id'] = self.project_id

        storage = {}

        self.start_service(service_id, name, params, storage)
        self.save_service_state(service_id)

        return self.services[service_id]

    def unregister_service(self, service_id):
        """Unregister service."""

        if service_id not in self.services:
            raise ValueError("Service %s not registered" % service_id)

        self.stop_service(service_id)

        del self.bootstrap[str(service_id)]
        del self.storage[str(service_id)]

        self.save()

    def reconfigure_service(self, service_id, params):
        """Reconfigure service."""

        if service_id not in self.services:
            raise ValueError("Service %s not registered" % service_id)

        service = self.services[service_id]

        for param in params:

            if param not in self.bootstrap[str(service_id)]['params']:
                raise KeyError("Param %s undefined" % param)

            setattr(service, param, params[param])

{'fraction_non_alphanumeric': 0.09128386336866902, 'fraction_numerical': 0.002591283863368669, 'mean_word_length': 2.6317365269461077, 'pattern_counts': {'":': 4, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '800768', 'n_tokens_mistral': 2313, 'n_tokens_neox': 2215, 'n_words': 704}