<commit_before>"""
Copyright 2011 <NAME>

This file is part of Microdrop.

Microdrop 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.

Microdrop 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 Microdrop.  If not, see <http://www.gnu.org/licenses/>.
"""

import os
import traceback
import shutil
from copy import deepcopy
import logging
try:
    import cPickle as pickle
except ImportError:
    import pickle

import gtk
import numpy as np
from flatland import Form, Integer, String, Boolean
from path_helpers import path
import yaml
from pygtkhelpers.ui.extra_widgets import Directory, Enum
from pygtkhelpers.ui.extra_dialogs import text_entry_dialog
from pygst_utils.video_pipeline.window_service_proxy import WindowServiceProxy
from pygst_utils.video_source import GstVideoSourceManager
from microdrop_utility.gui import yesno
from microdrop_utility import copytree

from ..app_context import get_app
from ..dmf_device import DmfDevice
from ..logger import logger
from ..plugin_helpers import AppDataController
from ..plugin_manager import (IPlugin, SingletonPlugin, implements,
                              PluginGlobals, ScheduleRequest, emit_signal)
from .dmf_device_view import DmfDeviceView
from .. import base_path

PluginGlobals.push_env('microdrop')


class DmfDeviceOptions(object):
    def __init__(self, state_of_channels=None):
        app = get_app()
        if state_of_channels is None:
            self.state_of_channels = np.zeros(app.dmf_device.max_channel() + 1)
        else:
            self.state_of_channels = deepcopy(state_of_channels)


class DmfDeviceController(SingletonPlugin, AppDataController):
    implements(IPlugin)

    with WindowServiceProxy(59000) as w:
        video_mode_map = w.get_video_mode_map()
        if video_mode_map:
            _video_available = True
        else:
            _video_available = False
        video_mode_keys = sorted(video_mode_map.keys())
        if _video_available:
            device_key, devices = w.get_video_source_configs()

    field_list = [
        Integer.named('overlay_opacity').using(default=50, optional=True),
        Directory.named('device_directory').using(default='', optional=True),
        String.named('transform_matrix').using(default='', optional=True,
                                               properties={'show_in_gui':
                                                           False}), ]

    if _video_available:
        video_mode_enum = Enum.named('video_mode').valued(
            *video_mode_keys).using(default=video_mode_keys[0], optional=True)
        video_enabled_boolean = Boolean.named('video_enabled').using(
            default=False, optional=True, properties={'show_in_gui': True})
        recording_enabled_boolean = Boolean.named('recording_enabled').using(
            default=False, optional=True, properties={'show_in_gui': False})
        field_list.append(video_mode_enum)
        field_list.append(video_enabled_boolean)
        field_list.append(recording_enabled_boolean)

    AppFields = Form.of(*field_list)

    def __init__(self):
        self.name = "microdrop.gui.dmf_device_controller"
        self.view = DmfDeviceView(self, 'device_view')
        self.view.connect('transform-changed', self.on_transform_changed)
        self.previous_device_dir = None
        self.recording_enabled = False
        self._modified = False
        self._video_initialized = False
        self._video_enabled = False
        self._gui_initialized = False
        self._bitrate = None
        self._record_path = None
        self._recording = False

    @property
    def modified(self):
        return self._modified

    @modified.setter
    def modified(self, value):
        self._modified = value
        self.menu_save_dmf_device.set_sensitive(value)

    @property
    def video_enabled(self):
        return self._video_enabled

    @video_enabled.setter
    def video_enabled(self, value):
        if not self._video_available and value:
            raise ValueError('Video cannot be enabled with no sources.')
        self._video_enabled = value

    def on_video_started(self, device_view, start_time):
        self.set_app_values(
            dict(transform_matrix=self.get_app_value('transform_matrix')))

    def on_transform_changed(self, device_view, array):
        self.set_app_values(
            dict(transform_matrix=yaml.dump(array.tolist())))

    def on_gui_ready(self):<commit_msg>Dynamically set AppFields for DmfDeviceController<commit_after>"""
Copyright 2011 <NAME>

This file is part of Microdrop.

Microdrop 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.

Microdrop 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 Microdrop.  If not, see <http://www.gnu.org/licenses/>.
"""

import os
import traceback
import shutil
from copy import deepcopy
import logging
try:
    import cPickle as pickle
except ImportError:
    import pickle

import gtk
import numpy as np
from flatland import Form, Integer, String, Boolean
from path_helpers import path
import yaml
from pygtkhelpers.ui.extra_widgets import Directory, Enum
from pygtkhelpers.ui.extra_dialogs import text_entry_dialog
from pygst_utils.video_pipeline.window_service_proxy import WindowServiceProxy
from pygst_utils.video_source import GstVideoSourceManager
from microdrop_utility.gui import yesno
from microdrop_utility import copytree

from ..app_context import get_app
from ..dmf_device import DmfDevice
from ..logger import logger
from ..plugin_helpers import AppDataController
from ..plugin_manager import (IPlugin, SingletonPlugin, implements,
                              PluginGlobals, ScheduleRequest, emit_signal)
from .dmf_device_view import DmfDeviceView
from .. import base_path

PluginGlobals.push_env('microdrop')


class DmfDeviceOptions(object):
    def __init__(self, state_of_channels=None):
        app = get_app()
        if state_of_channels is None:
            self.state_of_channels = np.zeros(app.dmf_device.max_channel() + 1)
        else:
            self.state_of_channels = deepcopy(state_of_channels)


class DmfDeviceController(SingletonPlugin, AppDataController):
    implements(IPlugin)

    def __init__(self):
        self.name = "microdrop.gui.dmf_device_controller"
        self.view = DmfDeviceView(self, 'device_view')
        self.view.connect('transform-changed', self.on_transform_changed)
        self.previous_device_dir = None
        self.recording_enabled = False
        self._modified = False
        self._video_initialized = False
        self._video_enabled = False
        self._gui_initialized = False
        self._bitrate = None
        self._record_path = None
        self._recording = False
        self._AppFields = None
        self._video_available = False

    @property
    def AppFields(self):
        if self._AppFields is None:
            self._AppFields = self._populate_app_fields()
        return self._AppFields

    def _populate_app_fields(self):
        with WindowServiceProxy(59000) as w:
            self.video_mode_map = w.get_video_mode_map()
            if self.video_mode_map:
                self._video_available = True
            else:
                self._video_available = False
            self.video_mode_keys = sorted(self.video_mode_map.keys())
            if self._video_available:
                self.device_key, self.devices = w.get_video_source_configs()

        field_list = [
            Integer.named('overlay_opacity').using(default=50, optional=True),
            Directory.named('device_directory').using(default='', optional=True),
            String.named('transform_matrix').using(default='', optional=True,
                                                properties={'show_in_gui':
                                                            False}), ]

        if self._video_available:
            video_mode_enum = Enum.named('video_mode').valued(
                *self.video_mode_keys).using(default=self.video_mode_keys[0],
                                             optional=True)
            video_enabled_boolean = Boolean.named('video_enabled').using(
                default=False, optional=True, properties={'show_in_gui': True})
            recording_enabled_boolean = Boolean.named('recording_enabled').using(
                default=False, optional=True, properties={'show_in_gui': False})
            field_list.append(video_mode_enum)
            field_list.append(video_enabled_boolean)
            field_list.append(recording_enabled_boolean)
        return Form.of(*field_list)

    @property
    def modified(self):
        return self._modified

    @modified.setter
    def modified(self, value):
        self._modified = value
        self.menu_save_dmf_device.set_sensitive(value)

    @property
    def video_enabled(self):
        return self._video_enabled

    @video_enabled.setter
    def video_enabled(self, value):
        if not self._video_available and value:
            raise ValueError('Video cannot be enabled with no sources.')
        self._video_enabled = value

    def on_video_started(self, device_view, start_time):
        self.set_app_values(
            dict(transform_matrix=self.get_app_value('transform_matrix')))

    def on_transform_changed(self, device_view, array):
        self.set_app_values(
            dict(transform_matrix=yaml.dump(array.tolist())))

    def on_gui_ready(self):
{'fraction_non_alphanumeric': 0.07473344419446347, 'fraction_numerical': 0.0027389220385405457, 'mean_word_length': 3.0363205684958547, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6068237', 'n_tokens_mistral': 2819, 'n_tokens_neox': 2760, 'n_words': 730}