<commit_before>"""DropController-class."""

import os
import time
import glib
import utils
from pyee import EventEmitter
from DropPygtkView import DPV
from Experiment import Experiment
from yapsy.PluginManager import PluginManager
from plugins import DropPluginLocator


class DropController(EventEmitter):
    """Main controller of the class. Views+ui separated to different files."""

    def __init__(self):
        """Constructor."""
        # run the superclass constructor
        EventEmitter.__init__(self)

        # Model initialization code
        self.participant_id = ""
        self.experiment_file = None
        self.sensors = []
        self.experiment = None

        # define important directories for external (not program code) files
        homedir = os.environ['HOME']
        drop_home = os.path.join(homedir, "Documents", "drop_data")
        self.savedir = os.path.join(drop_home, "recordings")
        self.experimentdir = os.path.join(drop_home, 'experiments')
        self.mediadir = os.path.join(drop_home, 'media')
        self.plugindir = os.path.join(drop_home, 'plugins')

        # check that saving, experiment etc directories are present
        utils.dircheck(self.savedir)
        utils.dircheck(self.experimentdir)
        utils.dircheck(self.mediadir)

        # temporary? keyboard-contigency list
        self.keyboard_contigency = []

        # initialize plugin manager
        self.pluginmanager = PluginManager(plugin_locator=DropPluginLocator())
        self.pluginmanager.setPluginPlaces([self.plugindir])
        self.pluginmanager.collectPlugins()

        # initialize pygtk-view
        self.ec = DPV(self, self.mediadir, self.experimentdir)
        self.ec.main()

    def addsensor(self, sensor_name):
        """Callback for addeeg-button."""
        # TODO: missing feature
        print sensor_name
        pass

    def get_sensors(self):
        """Return list of sensors."""
        return self.sensors

    def on_recorder_error(self, msg):
        """Sensor error-handler."""
        self.emit("error", msg)

    def on_recorder_created(self, rhandle):
        """Callback for sensor initialization."""
        self.sensors.append(rhandle)
        self.ec.add_recorder(rhandle)
        self.emit("sensorcount_changed")

        # add model to hear calls from recorders, such as data_condition met
        self.add_model(rhandle)

    def recorder_action(self, sensor_id, action_id):
        """Perform action that is listed on sensors control elements."""
        for sensor in self.sensors:
            if sensor.get_sensor_id() == sensor_id:
                sensor.action(action_id)

    def remove_sensor(self, sensor_id):
        """Disconnect the sensor with the provided sensor_id."""<commit_msg>Implemented add_sensor function for DropController.<commit_after>"""DropController-class."""

import os
import time
import glib
import utils
from pyee import EventEmitter
from DropPygtkView import DPV
from Experiment import Experiment
from yapsy.PluginManager import PluginManager
from plugins import DropPluginLocator


class DropController(EventEmitter):
    """Main controller of the class. Views+ui separated to different files."""

    def __init__(self):
        """Constructor."""
        # run the superclass constructor
        EventEmitter.__init__(self)

        # Model initialization code
        self.participant_id = ""
        self.experiment_file = None
        self.sensors = []
        self.experiment = None

        # define important directories for external (not program code) files
        homedir = os.environ['HOME']
        drop_home = os.path.join(homedir, "Documents", "drop_data")
        self.rootdir = drop_home
        self.savedir = os.path.join(drop_home, "recordings")
        self.experimentdir = os.path.join(drop_home, 'experiments')
        self.mediadir = os.path.join(drop_home, 'media')
        self.plugindir = os.path.join(drop_home, 'plugins')

        # check that saving, experiment etc directories are present
        utils.dircheck(self.savedir)
        utils.dircheck(self.experimentdir)
        utils.dircheck(self.mediadir)

        # temporary? keyboard-contigency list
        self.keyboard_contigency = []

        # initialize plugin manager
        self.pluginmanager = PluginManager(plugin_locator=DropPluginLocator())
        self.pluginmanager.setPluginPlaces([self.plugindir])
        self.pluginmanager.collectPlugins()

        # initialize pygtk-view
        self.ec = DPV(self, self.mediadir, self.experimentdir)
        self.ec.main()

    def addsensor(self, sensor_name):
        """Callback for Add sensor -button."""
        # TODO: Improve APIs for plugins
        plugin_info = self.pluginmanager.getPluginByName(sensor_name)
        plugin_info.plugin_object.get_sensor(self.rootdir,
                                             self.savedir,
                                             self.on_recorder_created,
                                             self.on_recorder_error)

    def get_sensors(self):
        """Return list of sensors."""
        return self.sensors

    def on_recorder_error(self, msg):
        """Sensor error-handler."""
        self.emit("error", msg)

    def on_recorder_created(self, rhandle):
        """Callback for sensor initialization."""
        self.sensors.append(rhandle)
        self.ec.add_recorder(rhandle)
        self.emit("sensorcount_changed")

        # add model to hear calls from recorders, such as data_condition met
        self.add_model(rhandle)

    def recorder_action(self, sensor_id, action_id):
        """Perform action that is listed on sensors control elements."""
        for sensor in self.sensors:
            if sensor.get_sensor_id() == sensor_id:
                sensor.action(action_id)

    def remove_sensor(self, sensor_id):
        """Disconnect the sensor with the provided sensor_id."""
{'fraction_non_alphanumeric': 0.09363359295631561, 'fraction_numerical': 0.0, 'mean_word_length': 2.6666666666666665, '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': 6, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3975565', 'n_tokens_mistral': 1631, 'n_tokens_neox': 1583, 'n_words': 400}