<commit_before>from abc import ABCMeta, abstractmethod

from xii.entity import Entity
from xii.store import HasStore
from xii import error, util


class Component(Entity, HasStore):
    __metaclass__ = ABCMeta

    ctype = ""
    """
    Name of the component (eg. 'node')
    """

    default_attributes = []
    """
    List of default attributes which are created if not defined in a
    definition file
    """

    required_attributes = []
    """
    Required attributes for this component
    """

    short_description = None

    def __init__(self, name, command, tpls={}):
        self._meta = {}
        Entity.__init__(self, name, parent=command, templates=tpls)

    @abstractmethod
    def fetch_metadata(self):
        """fetch metadata from component
        This method needs to be implemented to make xii work properly.

        It should fetch the metadata and return them as dict.

        For each component must set the created_at metadata!

        If no metadata is available return None

        Returns:
            A dict of key value pairs or None if not available
        """
        pass

    @abstractmethod
    def status(self):
        """get status from the component
        This method needs to be implemented by each component to make
        xii work properly.

        Returns:
            A string indicating the status
        pass
        """
        pass

    def add_meta(self, key_or_dict, value=None):
        """add metadata to component

        Value is converted to string, make sure added values
        have a usefull __repr__ method.

        Key which have been already defined are overwritten.

        Args:
            key_or_dict : name for the metadata attribute or a dict to add
            value: value you want to set
        """
        if isinstance(key_or_dict, dict):
            self._meta.update(key_or_dict)
        else:
            self._meta[key_or_dict] = str(value)


    def meta_to_xml(self):
        """get a xml representation for the metadata

        This function can be used to add metadata to libvirt based
        components more easy.

        Returns:
            The xml created or an empty string if no meta data was added
        """
        if not len(self._meta):
            return ""

        url = "https://xii-project.org/xmlns/" + self.ctype + "/1.0"
        attrs = reduce(lambda m, (k,v): m + util.create_xml_node(k, text=v),
                       self._meta.iteritems(),
                       "")<commit_msg>python3 removed argument tuple unpacking. Move to named inner function<commit_after>from abc import ABCMeta, abstractmethod
from functools import reduce

from xii.entity import Entity
from xii.store import HasStore
from xii import error, util


class Component(Entity, HasStore):
    __metaclass__ = ABCMeta

    ctype = ""
    """
    Name of the component (eg. 'node')
    """

    default_attributes = []
    """
    List of default attributes which are created if not defined in a
    definition file
    """

    required_attributes = []
    """
    Required attributes for this component
    """

    short_description = None

    def __init__(self, name, command, tpls={}):
        self._meta = {}
        Entity.__init__(self, name, parent=command, templates=tpls)

    @abstractmethod
    def fetch_metadata(self):
        """fetch metadata from component
        This method needs to be implemented to make xii work properly.

        It should fetch the metadata and return them as dict.

        For each component must set the created_at metadata!

        If no metadata is available return None

        Returns:
            A dict of key value pairs or None if not available
        """
        pass

    @abstractmethod
    def status(self):
        """get status from the component
        This method needs to be implemented by each component to make
        xii work properly.

        Returns:
            A string indicating the status
        pass
        """
        pass

    def add_meta(self, key_or_dict, value=None):
        """add metadata to component

        Value is converted to string, make sure added values
        have a usefull __repr__ method.

        Key which have been already defined are overwritten.

        Args:
            key_or_dict : name for the metadata attribute or a dict to add
            value: value you want to set
        """
        if isinstance(key_or_dict, dict):
            self._meta.update(key_or_dict)
        else:
            self._meta[key_or_dict] = str(value)

    def meta_to_xml(self):
        """get a xml representation for the metadata

        This function can be used to add metadata to libvirt based
        components more easy.

        Returns:
            The xml created or an empty string if no meta data was added
        """
        def merge(m, k_v):
            k, v = k_v
            return m + util.create_xml_node(k, text=v)

        if not len(self._meta):
            return ""

        url = "https://xii-project.org/xmlns/" + self.ctype + "/1.0"
        attrs = reduce(merge, self._meta.items(), "")
{'fraction_non_alphanumeric': 0.07120500782472614, 'fraction_numerical': 0.0009780907668231612, 'mean_word_length': 2.0671865626874624, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 10}, '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': '4079694', 'n_tokens_mistral': 1345, 'n_tokens_neox': 1300, 'n_words': 532}