<commit_before>"""Contains a baseclass for plugins."""

###############################################################################
#
# TODO: [ ] default methods
#
###############################################################################


# standard library imports
from collections import Iterable
from functools import wraps
import logging

# related third party imports

# application specific imports
# pylint: disable=import-error
from core import subscribe_to
# pylint: enable=import-error


__version__ = "1.4.2"


# Initialize the logger
LOGGER = logging.getLogger(__name__)


class Plugin(object):
    """Baseclass, that holds the mandatory methods a plugin must support."""

    def __init__(self, name="Plugin", active=False,
                 logger=None, file_path=None, plugin_type="s"):
        """Set the plugin's attributes, if they're not set already."""
        self.name = name
        self.uid = "NO_UID"
        self.is_active = active
        if logger:
            self.logger = logger
        else:
            self.logger = LOGGER
        if file_path:
            self.path = file_path
        else:
            self.path = __file__
        self.plugin_type = plugin_type
        self.logger.info("Initialisation of the plugin complete.")

    def __str__(self):
        """Return a simple string representation of the plugin."""
        return "{} '{}', UID {}".format(
            ("Device" if self.plugin_type == "d" else "Plugin"),
            self.name,
            self.uid)

    def __repr__(self):
        """Return a verbose string representation of the plugin."""
        return "{type}\t{name:10}\tUID {uid}\tLoaded from {path}".format(
            type=("Device" if self.plugin_type == "d" else "Plugin"),
            name=self.name,
            uid=self.uid,
            path=self.path)


class Device(Plugin):
    """Baseclass, that holds the mandatory methods a device must support."""

    def __init__(self, name="Device", active=False,
                 logger=None, file_path=None, group=None):
        """Set the plugin's attributes, if they're not set already."""
        super(Device, self).__init__(name, active, logger, file_path, "d")
        self.name = name
        self.is_available = None
        self.group = group
        self.logger.info("Initialisation complete")
        self.power_on_keywords = [self.name.lower() + ".power.on"]
        self.power_off_keywords = [self.name.lower() + ".power.off"]
        if group:
            if not isinstance(group, str) and isinstance(group, Iterable):
                for key in group:
                    self.power_on_keywords.append(key.lower() + ".power.on")
                    self.power_off_keywords.append(key.lower() + ".power.off")
            else:
                self.power_on_keywords.append(group.lower() + ".power.on")
                self.power_off_keywords.append(group.lower() + ".power.off")

    def turn_on(self, func):
        @subscribe_to(self.power_on_keywords)
        @wraps(func)
        def function(*args, **kwargs):
            return func(*args, **kwargs)

        return function

    def turn_off(self, func):
        @subscribe_to(self.power_off_keywords)
        @wraps(func)
        def function(*args, **kwargs):
            return func(*args, **kwargs)

        return function<commit_msg>:repeat: changed power on-events to be more natural<commit_after>"""Contains a baseclass for plugins."""

###############################################################################
#
# TODO: [ ] default methods
#
###############################################################################


# standard library imports
from collections import Iterable
from functools import wraps
import logging

# related third party imports

# application specific imports
# pylint: disable=import-error
from core import subscribe_to
# pylint: enable=import-error


__version__ = "1.4.3"


# Initialize the logger
LOGGER = logging.getLogger(__name__)


class Plugin(object):
    """Baseclass, that holds the mandatory methods a plugin must support."""

    def __init__(self, name="Plugin", active=False,
                 logger=None, file_path=None, plugin_type="s"):
        """Set the plugin's attributes, if they're not set already."""
        self.name = name
        self.uid = "NO_UID"
        self.is_active = active
        if logger:
            self.logger = logger
        else:
            self.logger = LOGGER
        if file_path:
            self.path = file_path
        else:
            self.path = __file__
        self.plugin_type = plugin_type
        self.logger.info("Initialisation of the plugin complete.")

    def __str__(self):
        """Return a simple string representation of the plugin."""
        return "{} '{}', UID {}".format(
            ("Device" if self.plugin_type == "d" else "Plugin"),
            self.name,
            self.uid)

    def __repr__(self):
        """Return a verbose string representation of the plugin."""
        return "{type}\t{name:10}\tUID {uid}\tLoaded from {path}".format(
            type=("Device" if self.plugin_type == "d" else "Plugin"),
            name=self.name,
            uid=self.uid,
            path=self.path)


class Device(Plugin):
    """Baseclass, that holds the mandatory methods a device must support."""

    def __init__(self, name="Device", active=False,
                 logger=None, file_path=None, group=None):
        """Set the plugin's attributes, if they're not set already."""
        super(Device, self).__init__(name, active, logger, file_path, "d")
        self.name = name
        self.is_available = None
        self.group = group
        self.logger.info("Initialisation complete")
        self.power_on_keywords = ["turn.on." + self.name.lower()]
        self.power_off_keywords = ["turn.off." + self.name.lower()]
        if group:
            if not isinstance(group, str) and isinstance(group, Iterable):
                for key in group:
                    self.power_on_keywords.append("turn.on." + key.lower())
                    self.power_off_keywords.append("turn.off." + key.lower())
            else:
                self.power_on_keywords.append("turn.on." + group.lower())
                self.power_off_keywords.append("turn.off." + group.lower())

    def turn_on(self, func):
        @subscribe_to(self.power_on_keywords)
        @wraps(func)
        def function(*args, **kwargs):
            return func(*args, **kwargs)

        return function

    def turn_off(self, func):
        @subscribe_to(self.power_off_keywords)
        @wraps(func)
        def function(*args, **kwargs):
            return func(*args, **kwargs)

        return function
{'fraction_non_alphanumeric': 0.161698956780924, 'fraction_numerical': 0.0014903129657228018, 'mean_word_length': 2.50810245687402, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, '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': '210890', 'n_tokens_mistral': 1853, 'n_tokens_neox': 1782, 'n_words': 484}