<commit_before>#!/usr/bin/env python
# -- Content-Encoding: UTF-8 --
"""
Top Composer: Top status storage

Gives orders to the node composer

:author: <NAME>
:copyright: Copyright 2013, isandlaTech
:license: GPLv3
:version: 3.0.0

..

    This file is part of Cohorte.

    Cohorte is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Cohorte is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Cohorte. If not, see <http://www.gnu.org/licenses/>.
"""

# Module version
__version_info__ = (3, 0, 0)
__version__ = ".".join(str(x) for x in __version_info__)

# Documentation strings format
__docformat__ = "restructuredtext en"

# ------------------------------------------------------------------------------

# Composer
import cohorte.composer

# iPOPO Decorators
from pelix.ipopo.decorators import ComponentFactory, Provides, Instantiate

# Standard library
import uuid

# ------------------------------------------------------------------------------

@ComponentFactory()
@Provides(cohorte.composer.SERVICE_STATUS_TOP)
@Instantiate('cohorte-composer-top-status')
class TopStatusStorage(object):
    """
    Stores the state of all compositions handled by this top composer
    """
    def __init__(self):
        """
        Sets up members
        """
        # Known composition names
        self._names = set()

        # UID -> {Node -> set(RawComponent)}
        self._storage = {}


    def dump(self):
        """
        Dumps the content of the storage
        """
        lines = ['Names:']
        lines.extend('\t- {0}'.format(name) for name in self._names)
        lines.append('')
        lines.append('Storage:')
        for uid, distribution in self._storage.items():
            lines.append('\t- {0}'.format(uid))
            for node, components in distribution.items():
                lines.append('\t\t- {0}'.format(node))
                lines.extend('\t\t\t- {0}'.format(component)
                             for component in components)

        return '\n'.join(lines)


    def store(self, composition, distribution):
        """
        Stores a new distribution

        :param composition0: The RawComposition bean
        :param distribution: A {node -> set(RawComponent)} dictionary
        :return: The UID associated to this distribution
        :raise: ValueError: The composition name is already known
        """
        # Check if the composition name has already been taken
        name = composition.name
        if name in self._names:
            raise ValueError("Already used composition name: {0}".format(name))
        else:
            # Store it
            self._names.add(name)

        # Generate a UUID
        uid = uuid.uuid4()

        # Store the composition
        self._storage[uid] = distribution
        return uid


    def get(self, uid):
        """
        Retrieves the distribution with the given UID

        :param uid: UID of the composition
        :return: The stored distribution dictionary
        :raise KeyError: Unknown UID
        """
        return self._storage[uid]


    def pop(self, uid):
        """
        Retrieves the composition with the given UID

        :param uid: UID of the composition
        :return: The stored distribution dictionary
        :raise KeyError: Unknown UID
        """
        return self._storage.pop(uid)


    def get_components_for_node(self, node_name):
        """
        Retrieves the components assigned to the given node

        :param node_name: A node name
        :return: The set of RawComponent beans associated to the node,
                 or an empty list
        """
        return {component
                for distribution in self._storage.values()
                for component in distribution[node_name]}<commit_msg>Correction of the TopStatus documentation<commit_after>#!/usr/bin/env python
# -- Content-Encoding: UTF-8 --
"""
Top Composer: Top status storage

Stores the distributions computed by the Top Composer and assigns them an UUID

:author: <NAME>
:copyright: Copyright 2013, isandlaTech
:license: GPLv3
:version: 3.0.0

..

    This file is part of Cohorte.

    Cohorte is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Cohorte is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Cohorte. If not, see <http://www.gnu.org/licenses/>.
"""

# Module version
__version_info__ = (3, 0, 0)
__version__ = ".".join(str(x) for x in __version_info__)

# Documentation strings format
__docformat__ = "restructuredtext en"

# ------------------------------------------------------------------------------

# Composer
import cohorte.composer

# iPOPO Decorators
from pelix.ipopo.decorators import ComponentFactory, Provides, Instantiate

# Standard library
import uuid

# ------------------------------------------------------------------------------

@ComponentFactory()
@Provides(cohorte.composer.SERVICE_STATUS_TOP)
@Instantiate('cohorte-composer-top-status')
class TopStatusStorage(object):
    """
    Stores the state of all compositions handled by this top composer
    """
    def __init__(self):
        """
        Sets up members
        """
        # Known composition names
        self._names = set()

        # UID -> {Node -> set(RawComponent)}
        self._storage = {}


    def dump(self):
        """
        Dumps the content of the storage
        """
        lines = ['Names:']
        lines.extend('\t- {0}'.format(name) for name in self._names)
        lines.append('')
        lines.append('Storage:')
        for uid, distribution in self._storage.items():
            lines.append('\t- {0}'.format(uid))
            for node, components in distribution.items():
                lines.append('\t\t- {0}'.format(node))
                lines.extend('\t\t\t- {0}'.format(component)
                             for component in components)

        return '\n'.join(lines)


    def store(self, composition, distribution):
        """
        Stores a new distribution

        :param composition: The RawComposition bean
        :param distribution: A {node -> set(RawComponent)} dictionary
        :return: The UID associated to this distribution
        :raise: ValueError: The composition name is already known
        """
        # Check if the composition name has already been taken
        name = composition.name
        if name in self._names:
            raise ValueError("Already used composition name: {0}".format(name))
        else:
            # Store it
            self._names.add(name)

        # Generate a UUID
        uid = uuid.uuid4()

        # Store the composition
        self._storage[uid] = distribution
        return uid


    def get(self, uid):
        """
        Retrieves the distribution with the given UID

        :param uid: UID of the composition
        :return: The stored distribution dictionary
        :raise KeyError: Unknown UID
        """
        return self._storage[uid]


    def pop(self, uid):
        """
        Retrieves the composition with the given UID

        :param uid: UID of the composition
        :return: The stored distribution dictionary
        :raise KeyError: Unknown UID
        """
        return self._storage.pop(uid)


    def get_components_for_node(self, node_name):
        """
        Retrieves the components assigned to the given node

        :param node_name: A node name
        :return: The set of RawComponent beans associated to the node,
                 or an empty set
        """
        return {component
                for distribution in self._storage.values()
                for component in distribution[node_name]}
{'fraction_non_alphanumeric': 0.12060655930410251, 'fraction_numerical': 0.004584459856588691, 'mean_word_length': 2.0736994219653178, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 13, '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': '6269740', 'n_tokens_mistral': 2225, 'n_tokens_neox': 2138, 'n_words': 800}