<commit_before># Copyright (C) 2009, Aleksey Lim, Simon Schampijer
# Copyright (C) 2012, Walter Bender
# Copyright (C) 2012, One Laptop Per Child
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library 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
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.

from gi.repository import Gdk
from gi.repository import Gtk
import gettext

from sugar3.graphics.toolbutton import ToolButton
from sugar3.graphics.toolbarbox import ToolbarButton
from sugar3.graphics.radiopalette import RadioPalette, RadioMenuButton
from sugar3.graphics.radiotoolbutton import RadioToolButton
from sugar3.graphics.xocolor import XoColor
from sugar3.graphics.icon import Icon
from sugar3.bundle.activitybundle import get_bundle_instance
from sugar3.graphics import style
from sugar3.graphics.palettemenu import PaletteMenuBox
from sugar3 import profile

_ = lambda msg: gettext.dgettext('sugar-toolkit-gtk3', msg)

def _create_activity_icon(metadata):
    if metadata is not None and metadata.get('icon-color'):
        color = XoColor(metadata['icon-color'])
    else:
        color = profile.get_color()

    from sugar3.activity.activity import get_bundle_path
    bundle = get_bundle_instance(get_bundle_path())
    icon = Icon(file=bundle.get_icon(), xo_color=color)

    return icon

class ActivityButton(ToolButton):

    def __init__(self, activity, **kwargs):
        ToolButton.__init__(self, **kwargs)

        icon = _create_activity_icon(activity.metadata)
        self.set_icon_widget(icon)
        icon.show()

        self.props.hide_tooltip_on_click = False
        self.palette_invoker.props.toggle_palette = True
        self.props.tooltip = activity.metadata['title']
        activity.metadata.connect('updated', self.__jobject_updated_cb)

    def __jobject_updated_cb(self, jobject):
        self.props.tooltip = jobject['title']

class ActivityToolbarButton(ToolbarButton):

    def __init__(self, activity, **kwargs):
        toolbar = ActivityToolbar(activity, orientation_left=True)

        ToolbarButton.__init__(self, page=toolbar, **kwargs)

        icon = _create_activity_icon(activity.metadata)
        self.set_icon_widget(icon)
        icon.show()

class StopButton(ToolButton):

    def __init__(self, activity, **kwargs):
        ToolButton.__init__(self, 'activity-stop', **kwargs)
        self.props.tooltip = _('Stop')
        self.props.accelerator = '<Ctrl>Q'
        self.connect('clicked', self.__stop_button_clicked_cb, activity)
        activity.add_stop_button(self)

    def __stop_button_clicked_cb(self, button, activity):
        activity.close()

class UndoButton(ToolButton):

    def __init__(self, **kwargs):
        ToolButton.__init__(self, 'edit-undo', **kwargs)
        self.props.tooltip = _('Undo')
        self.props.accelerator = '<Ctrl>Z'

class RedoButton(ToolButton):

    def __init__(self, **kwargs):
        ToolButton.__init__(self, 'edit-redo', **kwargs)
        self.props.tooltip = _('Redo')

class CopyButton(ToolButton):

    def __init__(self, **kwargs):
        ToolButton.__init__(self, 'edit-copy', **kwargs)
        self.props.tooltip = _('Copy')
        self.props.accelerator = '<Ctrl>C'

class PasteButton(ToolButton):

    def __init__(self, **kwargs):
        ToolButton.__init__(self, 'edit-paste', **kwargs)
        self.props.tooltip = _('Paste')
        self.props.accelerator = '<Ctrl>V'

class ShareButton(RadioMenuButton):

    def __init__(self, activity, **kwargs):
        palette = RadioPalette()

        self.private = RadioToolButton(
            icon_name='zoom-home')
        palette.append(self.private, _('Private'))

        self.neighborhood = RadioToolButton(
            icon_name='zoom-neighborhood',
            group=self.private)
        self._neighborhood_handle = self.neighborhood.connect(
            'clicked', self.__neighborhood_clicked_cb, activity)
        palette.append(self.neighborhood, _('My Neighborhood'))

        activity.connect('shared', self.__update_share_cb)
        activity.connect('joined', self.__update_share_cb)

        RadioMenuButton.__init__(self, **kwargs)
        self.props.palette = palette
        if activity.max_participants == 1:
            self.props.sensitive = False

    def __neighborhood_clicked_cb(self, button, activity):
        activity.share()

    def __update_share_cb(self, activity):
        self.neighborhood.handler_block(self._neighborhood_handle)
        try:
            if activity.shared_activity is not None and \
                    not activity.shared_activity.props.private:
                self.private.props.sensitive = False
                self.neighborhood.props.sensitive = False
                self.neighborhood.props.active = True
            else:
                self.private.props.sensitive = True
                self.neighborhood.props.sensitive = True
                self.private.props.active = True
        finally:
            self.neighborhood.handler_unblock(self._neighborhood_handle)

class TitleEntry(Gtk.ToolItem):

    def __init__(self, activity, **kwargs):
        Gtk.ToolItem.__init__(self)
        self.set_expand(False)

        self.entry = Gtk.Entry(**kwargs)
        self.entry.set_size_request(int(Gdk.Screen.width() / 3), -1)
        self.entry.set_text(activity.metadata['title'])
        self.entry.connect(
            'focus-out-event', self.__title_changed_cb, activity)
        self.entry.connect('button-press-event', self.__button_press_event_cb)
        self.entry.show()
        self.add(self.entry)

        activity.metadata.connect('updated', self.__jobject_updated_cb)
        activity.connect('_closing', self.__closing_cb)

    def modify_bg(self, state, color):
        Gtk.ToolItem.modify_bg(self, state, color)
        self.entry.modify_bg(state, color)

    def __jobject_updated_cb(self, jobject):
        if self.entry.has_focus():
            return
        if self.entry.get_text() == jobject['title']:
            return
        self.entry.set_text(jobject['title'])

    def __closing_cb(self, activity):
        self.save_title(activity)
        return False

    def __title_changed_cb(self, editable, event, activity):
        self.save_title(activity)
        return False

    def __button_press_event_cb(self, widget, event):
        if widget.is_focus():
            return False
        else:
            widget.grab_focus()
            widget.select_region(0, -1)
            return True

    def save_title(self, activity):
        title = self.entry.get_text()
        if title == activity.metadata['title']:
            return

        activity.metadata['title'] = title
        activity.metadata['title_set_by_user'] = '1'
        activity.save()

        activity.set_title(title)

        shared_activity = activity.get_shared_activity()
        if shared_activity is not None:
            shared_activity.props.name = title

class DescriptionItem(ToolButton):

    def __init__(self, activity, **kwargs):
        ToolButton.__init__(self, 'edit-description', **kwargs)
        self.set_tooltip(_('Description'))
        self.palette_invoker.props.toggle_palette = True
        self.palette_invoker.props.lock_palette = True
        self.props.hide_tooltip_on_click = False
        self._palette = self.get_palette()

        description_box = PaletteMenuBox()
        sw = Gtk.ScrolledWindow()
        sw.set_size_request(int(Gdk.Screen.width() / 2),
                            2 * style.GRID_CELL_SIZE)
        sw.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC)
        self._text_view = Gtk.TextView()
        self._text_view.set_left_margin(style.DEFAULT_PADDING)
        self._text_view.set_right_margin(style.DEFAULT_PADDING)
        self._text_view.set_wrap_mode(Gtk.WrapMode.WORD_CHAR)
        text_buffer = Gtk.TextBuffer()
        if 'description' in activity.metadata:
            text_buffer.set_text(activity.metadata['description'])
        self._text_view.set_buffer(text_buffer)
        self._text_view.connect('focus-out-event',
                                self.__description_changed_cb, activity)
        sw.add(self._text_view)
        description_box.append_item(sw, vertical_padding=0)
        self._palette.set_content(description_box)
        description_box.show_all()

        activity.metadata.connect('updated', self.__jobject_updated_cb)

    def set_expanded(self, expanded):
        box = self.toolbar_box
        if not box:
            return

        if not expanded:
            self.palette_invoker.notify_popdown()
            return

        if box.expanded_button is not None:
            box.expanded_button.queue_draw()
            if box.expanded_button != self:
                box.expanded_button.set_expanded(False)
        box.expanded_button = self

    def get_toolbar_box(self):
        parent = self.get_parent()
        if not hasattr(parent, 'owner'):
            return None
        return parent.owner

    toolbar_box = property(get_toolbar_box)

    def _get_text_from_buffer(self):
        buf = self._text_view.get_buffer()
        start_iter = buf.get_start_iter()
        end_iter = buf.get_end_iter()
        return buf.get_text(start_iter, end_iter, False)

    def __jobject_updated_cb(self, jobject):
        if self._text_view.has_focus():
            return
        if 'description' not in jobject:
            return
        if self._get_text_from_buffer() == jobject['description']:
            return
        buf = self._text_view.get_buffer()
        buf.set_text(jobject['description'])

    def __description_changed_cb(self, widget, event, activity):
        description = self._get_text_from_buffer()
        if 'description' in activity.metadata and \
                description == activity.metadata['description']:
            return

        activity.metadata['description'] = description
        activity.save()
        return False

class ActivityToolbar(Gtk.Toolbar):
    """The Activity toolbar with the Journal entry title and sharing button"""

    def __init__(self, activity, orientation_left=False):
        Gtk.Toolbar.__init__(self)

        self._activity = activity

        if activity.metadata:
            title_button = TitleEntry(activity)
            title_button.show()
            self.insert(title_button, -1)
            self.title = title_button.entry

        if not orientation_left:
            separator = Gtk.SeparatorToolItem()
            separator.props.draw = False
            separator.set_expand(True)
            self.insert(separator, -1)
            separator.show()

        if activity.metadata:
            description_item = DescriptionItem(activity)
            description_item.show()
            self.insert(description_item, -1)

        self.share = ShareButton(activity)
        self.share.show()
        self.insert(self.share, -1)

class EditToolbar(Gtk.Toolbar):
    """Provides the standard edit toolbar for Activities.

    Members:
        undo  -- the undo button
        redo  -- the redo button
        copy  -- the copy button
        paste -- the paste button
        separator -- A separator between undo/redo and copy/paste

    This class only provides the 'edit' buttons in a standard layout,
    your activity will need to either hide buttons which make no sense for your
    Activity, or you need to connect the button events to your own callbacks:

        ## Example from Read.activity:
        # Create the edit toolbar:
        self._edit_toolbar = EditToolbar(self._view)
        # Hide undo and redo, they're not needed
        self._edit_toolbar.undo.props.visible = False
        self._edit_toolbar.redo.props.visible = False
        # Hide the separator too:
        self._edit_toolbar.separator.props.visible = False

        # As long as nothing is selected, copy needs to be insensitive:
        self._edit_toolbar.copy.set_sensitive(False)
        # When the user clicks the button, call _edit_toolbar_copy_cb()
        self._edit_toolbar.copy.connect('clicked', self._edit_toolbar_copy_cb)

        # Add the edit toolbar:
        toolbox.add_toolbar(_('Edit'), self._edit_toolbar)
        # And make it visible:
        self._edit_toolbar.show()
    """

    def __init__(self):
        Gtk.Toolbar.__init__(self)

        self.undo = UndoButton()
        self.insert(self.undo, -1)
        self.undo.show()

        self.redo = RedoButton()
        self.insert(self.redo, -1)
        self.redo.show()

        self.separator = Gtk.SeparatorToolItem()
        self.separator.set_draw(True)
        self.insert(self.separator, -1)
        self.separator.show()

        self.copy = CopyButton()
        self.insert(self.copy, -1)
        self.copy.show()

        self.paste = PasteButton()
        self.insert(self.paste, -1)
        self.paste.show()
<commit_msg>TitleEntry - save title on activate (Enter)<commit_after># Copyright (C) 2009, Aleksey Lim, Simon Schampijer
# Copyright (C) 2012, Walter Bender
# Copyright (C) 2012, One Laptop Per Child
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2 of the License, or (at your option) any later version.
#
# This library 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
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the
# Free Software Foundation, Inc., 59 Temple Place - Suite 330,
# Boston, MA 02111-1307, USA.

from gi.repository import Gdk
from gi.repository import Gtk
import gettext

from sugar3.graphics.toolbutton import ToolButton
from sugar3.graphics.toolbarbox import ToolbarButton
from sugar3.graphics.radiopalette import RadioPalette, RadioMenuButton
from sugar3.graphics.radiotoolbutton import RadioToolButton
from sugar3.graphics.xocolor import XoColor
from sugar3.graphics.icon import Icon
from sugar3.bundle.activitybundle import get_bundle_instance
from sugar3.graphics import style
from sugar3.graphics.palettemenu import PaletteMenuBox
from sugar3 import profile

_ = lambda msg: gettext.dgettext('sugar-toolkit-gtk3', msg)

def _create_activity_icon(metadata):
    if metadata is not None and metadata.get('icon-color'):
        color = XoColor(metadata['icon-color'])
    else:
        color = profile.get_color()

    from sugar3.activity.activity import get_bundle_path
    bundle = get_bundle_instance(get_bundle_path())
    icon = Icon(file=bundle.get_icon(), xo_color=color)

    return icon

class ActivityButton(ToolButton):

    def __init__(self, activity, **kwargs):
        ToolButton.__init__(self, **kwargs)

        icon = _create_activity_icon(activity.metadata)
        self.set_icon_widget(icon)
        icon.show()

        self.props.hide_tooltip_on_click = False
        self.palette_invoker.props.toggle_palette = True
        self.props.tooltip = activity.metadata['title']
        activity.metadata.connect('updated', self.__jobject_updated_cb)

    def __jobject_updated_cb(self, jobject):
        self.props.tooltip = jobject['title']

class ActivityToolbarButton(ToolbarButton):

    def __init__(self, activity, **kwargs):
        toolbar = ActivityToolbar(activity, orientation_left=True)

        ToolbarButton.__init__(self, page=toolbar, **kwargs)

        icon = _create_activity_icon(activity.metadata)
        self.set_icon_widget(icon)
        icon.show()

class StopButton(ToolButton):

    def __init__(self, activity, **kwargs):
        ToolButton.__init__(self, 'activity-stop', **kwargs)
        self.props.tooltip = _('Stop')
        self.props.accelerator = '<Ctrl>Q'
        self.connect('clicked', self.__stop_button_clicked_cb, activity)
        activity.add_stop_button(self)

    def __stop_button_clicked_cb(self, button, activity):
        activity.close()

class UndoButton(ToolButton):

    def __init__(self, **kwargs):
        ToolButton.__init__(self, 'edit-undo', **kwargs)
        self.props.tooltip = _('Undo')
        self.props.accelerator = '<Ctrl>Z'

class RedoButton(ToolButton):

    def __init__(self, **kwargs):
        ToolButton.__init__(self, 'edit-redo', **kwargs)
        self.props.tooltip = _('Redo')

class CopyButton(ToolButton):

    def __init__(self, **kwargs):
        ToolButton.__init__(self, 'edit-copy', **kwargs)
        self.props.tooltip = _('Copy')
        self.props.accelerator = '<Ctrl>C'

class PasteButton(ToolButton):

    def __init__(self, **kwargs):
        ToolButton.__init__(self, 'edit-paste', **kwargs)
        self.props.tooltip = _('Paste')
        self.props.accelerator = '<Ctrl>V'

class ShareButton(RadioMenuButton):

    def __init__(self, activity, **kwargs):
        palette = RadioPalette()

        self.private = RadioToolButton(
            icon_name='zoom-home')
        palette.append(self.private, _('Private'))

        self.neighborhood = RadioToolButton(
            icon_name='zoom-neighborhood',
            group=self.private)
        self._neighborhood_handle = self.neighborhood.connect(
            'clicked', self.__neighborhood_clicked_cb, activity)
        palette.append(self.neighborhood, _('My Neighborhood'))

        activity.connect('shared', self.__update_share_cb)
        activity.connect('joined', self.__update_share_cb)

        RadioMenuButton.__init__(self, **kwargs)
        self.props.palette = palette
        if activity.max_participants == 1:
            self.props.sensitive = False

    def __neighborhood_clicked_cb(self, button, activity):
        activity.share()

    def __update_share_cb(self, activity):
        self.neighborhood.handler_block(self._neighborhood_handle)
        try:
            if activity.shared_activity is not None and \
                    not activity.shared_activity.props.private:
                self.private.props.sensitive = False
                self.neighborhood.props.sensitive = False
                self.neighborhood.props.active = True
            else:
                self.private.props.sensitive = True
                self.neighborhood.props.sensitive = True
                self.private.props.active = True
        finally:
            self.neighborhood.handler_unblock(self._neighborhood_handle)

class TitleEntry(Gtk.ToolItem):

    def __init__(self, activity, **kwargs):
        Gtk.ToolItem.__init__(self)
        self.set_expand(False)

        self.entry = Gtk.Entry(**kwargs)
        self.entry.set_size_request(int(Gdk.Screen.width() / 3), -1)
        self.entry.set_text(activity.metadata['title'])
        self.entry.connect(
            'focus-out-event', self.__title_changed_cb, activity)
        self.entry.connect('activate', self.__on_activate, activity)
        self.entry.connect('button-press-event', self.__button_press_event_cb)
        self.entry.show()
        self.add(self.entry)

        activity.metadata.connect('updated', self.__jobject_updated_cb)
        activity.connect('_closing', self.__closing_cb)

    def __on_activate(self, widget, activity):
        self.save_title(activity)
        self.entry.hide()
        self.entry.show()
        return False

    def modify_bg(self, state, color):
        Gtk.ToolItem.modify_bg(self, state, color)
        self.entry.modify_bg(state, color)

    def __jobject_updated_cb(self, jobject):
        if self.entry.has_focus():
            return
        if self.entry.get_text() == jobject['title']:
            return
        self.entry.set_text(jobject['title'])

    def __closing_cb(self, activity):
        self.save_title(activity)
        return False

    def __title_changed_cb(self, editable, event, activity):
        self.save_title(activity)
        return False

    def __button_press_event_cb(self, widget, event):
        if widget.is_focus():
            return False
        else:
            widget.grab_focus()
            widget.select_region(0, -1)
            return True

    def save_title(self, activity):
        title = self.entry.get_text()
        if title == activity.metadata['title']:
            return

        activity.metadata['title'] = title
        activity.metadata['title_set_by_user'] = '1'
        activity.save()

        activity.set_title(title)

        shared_activity = activity.get_shared_activity()
        if shared_activity is not None:
            shared_activity.props.name = title

class DescriptionItem(ToolButton):

    def __init__(self, activity, **kwargs):
        ToolButton.__init__(self, 'edit-description', **kwargs)
        self.set_tooltip(_('Description'))
        self.palette_invoker.props.toggle_palette = True
        self.palette_invoker.props.lock_palette = True
        self.props.hide_tooltip_on_click = False
        self._palette = self.get_palette()

        description_box = PaletteMenuBox()
        sw = Gtk.ScrolledWindow()
        sw.set_size_request(int(Gdk.Screen.width() / 2),
                            2 * style.GRID_CELL_SIZE)
        sw.set_policy(Gtk.PolicyType.AUTOMATIC, Gtk.PolicyType.AUTOMATIC)
        self._text_view = Gtk.TextView()
        self._text_view.set_left_margin(style.DEFAULT_PADDING)
        self._text_view.set_right_margin(style.DEFAULT_PADDING)
        self._text_view.set_wrap_mode(Gtk.WrapMode.WORD_CHAR)
        text_buffer = Gtk.TextBuffer()
        if 'description' in activity.metadata:
            text_buffer.set_text(activity.metadata['description'])
        self._text_view.set_buffer(text_buffer)
        self._text_view.connect('focus-out-event',
                                self.__description_changed_cb, activity)
        sw.add(self._text_view)
        description_box.append_item(sw, vertical_padding=0)
        self._palette.set_content(description_box)
        description_box.show_all()

        activity.metadata.connect('updated', self.__jobject_updated_cb)

    def set_expanded(self, expanded):
        box = self.toolbar_box
        if not box:
            return

        if not expanded:
            self.palette_invoker.notify_popdown()
            return

        if box.expanded_button is not None:
            box.expanded_button.queue_draw()
            if box.expanded_button != self:
                box.expanded_button.set_expanded(False)
        box.expanded_button = self

    def get_toolbar_box(self):
        parent = self.get_parent()
        if not hasattr(parent, 'owner'):
            return None
        return parent.owner

    toolbar_box = property(get_toolbar_box)

    def _get_text_from_buffer(self):
        buf = self._text_view.get_buffer()
        start_iter = buf.get_start_iter()
        end_iter = buf.get_end_iter()
        return buf.get_text(start_iter, end_iter, False)

    def __jobject_updated_cb(self, jobject):
        if self._text_view.has_focus():
            return
        if 'description' not in jobject:
            return
        if self._get_text_from_buffer() == jobject['description']:
            return
        buf = self._text_view.get_buffer()
        buf.set_text(jobject['description'])

    def __description_changed_cb(self, widget, event, activity):
        description = self._get_text_from_buffer()
        if 'description' in activity.metadata and \
                description == activity.metadata['description']:
            return

        activity.metadata['description'] = description
        activity.save()
        return False

class ActivityToolbar(Gtk.Toolbar):
    """The Activity toolbar with the Journal entry title and sharing button"""

    def __init__(self, activity, orientation_left=False):
        Gtk.Toolbar.__init__(self)

        self._activity = activity

        if activity.metadata:
            title_button = TitleEntry(activity)
            title_button.show()
            self.insert(title_button, -1)
            self.title = title_button.entry

        if not orientation_left:
            separator = Gtk.SeparatorToolItem()
            separator.props.draw = False
            separator.set_expand(True)
            self.insert(separator, -1)
            separator.show()

        if activity.metadata:
            description_item = DescriptionItem(activity)
            description_item.show()
            self.insert(description_item, -1)

        self.share = ShareButton(activity)
        self.share.show()
        self.insert(self.share, -1)

class EditToolbar(Gtk.Toolbar):
    """Provides the standard edit toolbar for Activities.

    Members:
        undo  -- the undo button
        redo  -- the redo button
        copy  -- the copy button
        paste -- the paste button
        separator -- A separator between undo/redo and copy/paste

    This class only provides the 'edit' buttons in a standard layout,
    your activity will need to either hide buttons which make no sense for your
    Activity, or you need to connect the button events to your own callbacks:

        ## Example from Read.activity:
        # Create the edit toolbar:
        self._edit_toolbar = EditToolbar(self._view)
        # Hide undo and redo, they're not needed
        self._edit_toolbar.undo.props.visible = False
        self._edit_toolbar.redo.props.visible = False
        # Hide the separator too:
        self._edit_toolbar.separator.props.visible = False

        # As long as nothing is selected, copy needs to be insensitive:
        self._edit_toolbar.copy.set_sensitive(False)
        # When the user clicks the button, call _edit_toolbar_copy_cb()
        self._edit_toolbar.copy.connect('clicked', self._edit_toolbar_copy_cb)

        # Add the edit toolbar:
        toolbox.add_toolbar(_('Edit'), self._edit_toolbar)
        # And make it visible:
        self._edit_toolbar.show()
    """

    def __init__(self):
        Gtk.Toolbar.__init__(self)

        self.undo = UndoButton()
        self.insert(self.undo, -1)
        self.undo.show()

        self.redo = RedoButton()
        self.insert(self.redo, -1)
        self.redo.show()

        self.separator = Gtk.SeparatorToolItem()
        self.separator.set_draw(True)
        self.insert(self.separator, -1)
        self.separator.show()

        self.copy = CopyButton()
        self.insert(self.copy, -1)
        self.copy.show()

        self.paste = PasteButton()
        self.insert(self.paste, -1)
        self.paste.show()
<|endoftext|>


<commit_before>import mock

from io import BytesIO
from django.core.management import call_command
from django.test import TestCase
EXPECTED_EXCEPTION_MSG = 'Error at /readviz/NA19675.cram (Individual: NA19675_1): Error accessing "/readviz/NA19675.cram" \n---- DONE ----\nChecked 1 samples\n1 failed samples: NA19675_1\n'
EXPECTED_NORMAL_MSG = 'Error at /readviz/NA19675.cram (Individual: NA19675_1): Error accessing "/readviz/NA19675.cram" \n---- DONE ----\nChecked 1 samples\n1 failed samples: NA19675_1\n'

class AddProjectTagTest(TestCase):
    fixtures = ['users', '1kg_project']

    @mock.patch('seqr.views.utils.dataset_utils.validate_alignment_dataset_path')
    def test_normal_command(self, mock_validate_path):
        mock_validate_path.return_value = ""
        out = BytesIO()
        call_command('check_bam_cram_paths', u'1kg project n\u00e5me with uni\u00e7\u00f8de', stdout=out)

        self.assertEqual(EXPECTED_NORMAL_MSG, out.getvalue())

    @mock.patch('seqr.views.utils.dataset_utils.validate_alignment_dataset_path')
    def test_exception_command(self, mock_validate_path):
        mock_validate_path.side_effect = Exception('Error accessing "/readviz/NA19675.cram"')
        out = BytesIO()
        call_command('check_bam_cram_paths', u'1kg project n\u00e5me with uni\u00e7\u00f8de', stdout=out)

        self.assertEqual(EXPECTED_EXCEPTION_MSG, out.getvalue())
<commit_msg>Updated the class name for the test.<commit_after>import mock

from io import BytesIO
from django.core.management import call_command
from django.test import TestCase
EXPECTED_EXCEPTION_MSG = 'Error at /readviz/NA19675.cram (Individual: NA19675_1): Error accessing "/readviz/NA19675.cram" \n---- DONE ----\nChecked 1 samples\n1 failed samples: NA19675_1\n'
EXPECTED_NORMAL_MSG = 'Error at /readviz/NA19675.cram (Individual: NA19675_1): Error accessing "/readviz/NA19675.cram" \n---- DONE ----\nChecked 1 samples\n1 failed samples: NA19675_1\n'

class CheckBamCramPathsTest(TestCase):
    fixtures = ['users', '1kg_project']

    @mock.patch('seqr.views.utils.dataset_utils.validate_alignment_dataset_path')
    def test_normal_command(self, mock_validate_path):
        mock_validate_path.return_value = ""
        out = BytesIO()
        call_command('check_bam_cram_paths', u'1kg project n\u00e5me with uni\u00e7\u00f8de', stdout=out)

        self.assertEqual(EXPECTED_NORMAL_MSG, out.getvalue())

    @mock.patch('seqr.views.utils.dataset_utils.validate_alignment_dataset_path')
    def test_exception_command(self, mock_validate_path):
        mock_validate_path.side_effect = Exception('Error accessing "/readviz/NA19675.cram"')
        out = BytesIO()
        call_command('check_bam_cram_paths', u'1kg project n\u00e5me with uni\u00e7\u00f8de', stdout=out)

        self.assertEqual(EXPECTED_EXCEPTION_MSG, out.getvalue())
<|endoftext|>


<commit_before>"""
TODO: Implement other error handlers - http://flask.pocoo.org/docs/0.12/patterns/errorpages/
TODO: Shit-loads of refactoring
TODO: Proper Error Handling of Entries in report_filter()
"""

from flask import Flask, render_template, request, redirect, url_for, session, flash
import psycopg2
import datetime

from config import *

# Instantiate App
app = Flask(__name__)
app.secret_key = APP_SECRET_KEY

# MARK: DATABASE FUNCTIONS
# Database Query Function
def db_query(sql_string, data_array):
	conn = psycopg2.connect(DB_LOCATION)
	cur = conn.cursor()
	print("\nThe query is:\n", sql_string, "\nThe data array is:\n", data_array)
	cur.execute(sql_string, data_array)

	# Return data as an array of dictionaries
	result = cur.fetchall()
	print('\nIF THIS IS NOT EMPTY, YOU DID IT - PROBABLY:', result)
	records = []

	# If the query returns something...
	if len(result) != 0:
		entries = result
		for row in entries:
			records.append(row)
	else:
		# No results in query
		print('\nNO RESULTS IN QUERY\n')
		return redirect(url_for('failed_query', query_string=sql_string))

	conn.commit()
	cur.close()
	conn.close()
	return records

# Date Validation Function
def validate_date(date_string):
	try:
		date = datetime.datetime.strptime(date_string, '%m/%d/%Y').date()
		return str(date)
	except ValueError:
		raise ValueError('Incorrect data format, should be MM/DD/YYYY')

# MARK: TEMPLATES
@app.route('/')
@app.route('/index')
@app.route('/index.html')
def index():
	return redirect(url_for('login'))

@app.route('/login', methods=['GET', 'POST'])
def login():
	error = None
	if request.method == 'POST':
		if False or request.form['username'] != USERNAME:
			error = 'Invalid username'
		elif False or request.form['password'] != PASSWORD:
			error = 'Invalid password'
		else:
			session['logged_in'] = True
			session['username'] = request.form['username']
			session['password'] = request.form['password']
			return redirect(url_for('report_filter'))

	return render_template('login.html')

@app.route('/logout', methods=['GET', 'POST'])
def logout():
	session['logged_in'] = False
	return render_template('logout.html')

@app.route('/report_filter', methods=['GET', 'POST'])
def report_filter():
	# Populate Facilities Drop-down menu
	fac_query = 'SELECT common_name FROM facilities;'
	conn = psycopg2.connect(DB_LOCATION)
	cur = conn.cursor()
	cur.execute(fac_query)
	list_of_single_tuples = cur.fetchall()

	facilities_list = []
	for name, in list_of_single_tuples:
		facilities_list.append(name)

	conn.commit()
	cur.close()
	conn.close()

	# If a form has been submitted
	if request.method == 'POST':
		# Validate and pass date
		try:
			validated_date = validate_date(request.form['filter_date'])
		except ValueError:
			print('VALUE ERROR:', ValueError.args)
			pass
		except TypeError or UnboundLocalError:
			print('TYPE ERROR:', 'You need to enter a date.')
			pass

		# FILTER: Inventory by 'In Transit' status
		if request.form['filter_facility'] == 'none':
			return moving_inventory(validated_date)

		# FILTER: Inventory by Facility
		else:
			return facility_inventory(validated_date)

	return render_template('report_filter.html', facilities_list=facilities_list)

@app.route('/moving_inventory', methods=['GET', 'POST'])
def moving_inventory(validated_date):
	moving_query = 'SELECT assets.asset_tag, assets.description, f1.location as location1, f2.location as location2, convoys.depart_dt, convoys.arrive_dt' \
				   ' FROM assets' \
				   ' JOIN asset_on ON assets.asset_pk = asset_on.asset_fk' \
				   ' JOIN convoys ON asset_on.convoy_fk = convoys.convoy_pk' \
				   ' JOIN facilities f1 ON convoys.src_fk = f1.facility_pk' \
				   ' JOIN facilities f2 ON convoys.dst_fk = f2.facility_pk' \
				   ' WHERE convoys.arrive_dt >= %s AND convoys.depart_dt <= %s;'

	moving_inventory_data = db_query(moving_query, [validated_date, validated_date])

	column_names = ['asset_tag', 'description', 'location1', 'location2', 'depart_dt', 'arrive_dt']
	moving_inventory_processed = []

	# TODO: Refactor by creating array of tuples to dictionary conversion function
	# If list is not empty and it's size matches the array of column headers
	if moving_inventory_data and (len(moving_inventory_data[0]) == len(column_names)):
		for record in moving_inventory_data:
			moving_inventory_processed.append(dict(zip(column_names, record)))
	else:
		print('\n\nERROR LIST OF COLUMN SIZE IS NOT THE SAME SIZE AS RECORD SIZE\n\n')

	return render_template('moving_inventory.html', date=validated_date, data=moving_inventory_processed)

@app.route('/facility_inventory', methods=['GET', 'POST'])
def facility_inventory(validated_date):
	selected_facility = request.form['filter_facility']
	facility_query = "SELECT facilities.fcode, facilities.location, assets.asset_tag, assets.description, asset_at.arrive_dt, asset_at.depart_dt" \
					 " FROM (SELECT * FROM facilities WHERE common_name = %s) as facilities" \
					 " JOIN asset_at ON facilities.facility_pk = asset_at.facility_fk" \
					 " JOIN assets ON asset_at.asset_fk = assets.asset_pk" \
					 " WHERE (asset_at.depart_dt <= %s OR asset_at.depart_dt IS NULL)" \
					 " AND asset_at.arrive_dt >= %s;"

	facility_inventory_data = db_query(facility_query, [selected_facility, validated_date, validated_date])
	print("\n\nLine 166 - facility_inventory_data:", facility_inventory_data)

	column_names = ['fcode', 'location', 'asset_tag', 'description', 'arrive_dt', ' depart_dt']
	facility_inventory_processed = []

	# TODO: Refactor by creating array of tuples to dictionary conversion function
	# If list is not empty and it's size matches the array of column headers
	if facility_inventory_data and (len(facility_inventory_data[0]) == len(column_names)):
		for record in facility_inventory_data:
			facility_inventory_processed.append(dict(zip(column_names, record)))
	else:
		print('\n\nERROR LIST OF COLUMN SIZE IS NOT THE SAME SIZE AS RECORD SIZE\n\n')

	print('\nThis is the data being passed:', facility_inventory_processed)
	return render_template('facility_inventory.html', facility=str(selected_facility), data=facility_inventory_processed, date=validated_date)

@app.route('/facility_inventory', methods=['GET'])
def failed_query(query_string):
	return render_template('failed_query.html', query=query_string)

# MARK: ERROR PAGES
@app.errorhandler(404)
def page_not_found(e):
	return render_template('404.html'), 404

# Application Deployment
if __name__ == '__main__':
	app.run(host='0.0.0.0', port=8080, debug=True)<commit_msg>Fix facility query - Attempt #14<commit_after>"""
TODO: Implement other error handlers - http://flask.pocoo.org/docs/0.12/patterns/errorpages/
TODO: Shit-loads of refactoring
TODO: Proper Error Handling of Entries in report_filter()
"""

from flask import Flask, render_template, request, redirect, url_for, session, flash
import psycopg2
import datetime

from config import *

# Instantiate App
app = Flask(__name__)
app.secret_key = APP_SECRET_KEY

# MARK: DATABASE FUNCTIONS
# Database Query Function
def db_query(sql_string, data_array):
	conn = psycopg2.connect(DB_LOCATION)
	cur = conn.cursor()
	print("\nThe query is:\n", sql_string, "\nThe data array is:\n", data_array)
	cur.execute(sql_string, data_array)

	# Return data as an array of dictionaries
	result = cur.fetchall()
	print('\nIF THIS IS NOT EMPTY, YOU DID IT - PROBABLY:', result)
	records = []

	# If the query returns something...
	if len(result) != 0:
		entries = result
		for row in entries:
			records.append(row)
	else:
		# No results in query
		print('\nNO RESULTS IN QUERY\n')
		# TODO: Redirect to to failed_query.html

	conn.commit()
	cur.close()
	conn.close()
	return records

# Date Validation Function
def validate_date(date_string):
	try:
		date = datetime.datetime.strptime(date_string, '%m/%d/%Y').date()
		return str(date)
	except ValueError:
		raise ValueError('Incorrect data format, should be MM/DD/YYYY')

# MARK: TEMPLATES
@app.route('/')
@app.route('/index')
@app.route('/index.html')
def index():
	return redirect(url_for('login'))

@app.route('/login', methods=['GET', 'POST'])
def login():
	error = None
	if request.method == 'POST':
		if False or request.form['username'] != USERNAME:
			error = 'Invalid username'
		elif False or request.form['password'] != PASSWORD:
			error = 'Invalid password'
		else:
			session['logged_in'] = True
			session['username'] = request.form['username']
			session['password'] = request.form['password']
			return redirect(url_for('report_filter'))

	return render_template('login.html')

@app.route('/logout', methods=['GET', 'POST'])
def logout():
	session['logged_in'] = False
	return render_template('logout.html')

@app.route('/report_filter', methods=['GET', 'POST'])
def report_filter():
	# Populate Facilities Drop-down menu
	fac_query = 'SELECT common_name FROM facilities;'
	conn = psycopg2.connect(DB_LOCATION)
	cur = conn.cursor()
	cur.execute(fac_query)
	list_of_single_tuples = cur.fetchall()

	facilities_list = []
	for name, in list_of_single_tuples:
		facilities_list.append(name)

	conn.commit()
	cur.close()
	conn.close()

	# If a form has been submitted
	if request.method == 'POST':
		# Validate and pass date
		try:
			validated_date = validate_date(request.form['filter_date'])
		except ValueError:
			print('VALUE ERROR:', ValueError.args)
			pass
		except TypeError or UnboundLocalError:
			print('TYPE ERROR:', 'You need to enter a date.')
			pass

		# FILTER: Inventory by 'In Transit' status
		if request.form['filter_facility'] == 'none':
			return moving_inventory(validated_date)

		# FILTER: Inventory by Facility
		else:
			return facility_inventory(validated_date)

	return render_template('report_filter.html', facilities_list=facilities_list)

@app.route('/moving_inventory', methods=['GET', 'POST'])
def moving_inventory(validated_date):
	moving_query = 'SELECT assets.asset_tag, assets.description, f1.location as location1, f2.location as location2, convoys.depart_dt, convoys.arrive_dt' \
				   ' FROM assets' \
				   ' JOIN asset_on ON assets.asset_pk = asset_on.asset_fk' \
				   ' JOIN convoys ON asset_on.convoy_fk = convoys.convoy_pk' \
				   ' JOIN facilities f1 ON convoys.src_fk = f1.facility_pk' \
				   ' JOIN facilities f2 ON convoys.dst_fk = f2.facility_pk' \
				   ' WHERE convoys.arrive_dt >= %s AND convoys.depart_dt <= %s;'

	moving_inventory_data = db_query(moving_query, [validated_date, validated_date])

	column_names = ['asset_tag', 'description', 'location1', 'location2', 'depart_dt', 'arrive_dt']
	moving_inventory_processed = []

	# TODO: Refactor by creating array of tuples to dictionary conversion function
	# If list is not empty and it's size matches the array of column headers
	if moving_inventory_data and (len(moving_inventory_data[0]) == len(column_names)):
		for record in moving_inventory_data:
			moving_inventory_processed.append(dict(zip(column_names, record)))
	else:
		print('\n\nERROR - LIST OF COLUMN SIZE IS NOT THE SAME SIZE AS RECORD SIZE\n\n')

	return render_template('moving_inventory.html', date=validated_date, data=moving_inventory_processed)

@app.route('/facility_inventory', methods=['GET', 'POST'])
def facility_inventory(validated_date):
	selected_facility = request.form['filter_facility']
	facility_query = "SELECT facilities.fcode, facilities.location, assets.asset_tag, assets.description, asset_at.arrive_dt, asset_at.depart_dt" \
					 " FROM (SELECT * FROM facilities WHERE common_name = %s) as facilities" \
					 " JOIN asset_at ON facilities.facility_pk = asset_at.facility_fk" \
					 " JOIN assets ON asset_at.asset_fk = assets.asset_pk" \
					 " WHERE (asset_at.depart_dt <= %s OR asset_at.depart_dt IS NULL)" \
					 " AND asset_at.arrive_dt >= %s;"

	facility_inventory_data = db_query(facility_query, [selected_facility, validated_date, validated_date])
	print("\n\nLine 166 - facility_inventory_data:", facility_inventory_data)

	column_names = ['fcode', 'location', 'asset_tag', 'description', 'arrive_dt', ' depart_dt']
	facility_inventory_processed = []

	# TODO: Refactor by creating array of tuples to dictionary conversion function
	# If list is not empty and it's size matches the array of column headers
	if facility_inventory_data and (len(facility_inventory_data[0]) == len(column_names)):
		for record in facility_inventory_data:
			facility_inventory_processed.append(dict(zip(column_names, record)))
	else:
		print('\n\nERROR - LIST OF COLUMN SIZE IS NOT THE SAME SIZE AS RECORD SIZE\n\n')

	print('\nThis is the data being passed:', facility_inventory_processed)
	return render_template('facility_inventory.html', facility=str(selected_facility), data=facility_inventory_processed, date=validated_date)

@app.route('/facility_inventory', methods=['GET'])
def failed_query(query_string):
	return render_template('failed_query.html', query=query_string)

# MARK: ERROR PAGES
@app.errorhandler(404)
def page_not_found(e):
	return render_template('404.html'), 404

# Application Deployment
if __name__ == '__main__':
	app.run(host='0.0.0.0', port=8080, debug=True)<|endoftext|>


<commit_before>import PythonQt
from PythonQt import QtCore, QtGui, QtUiTools
from ddapp import lcmUtils
from ddapp import applogic as app
from ddapp.utime import getUtime
from ddapp.timercallback import TimerCallback

import numpy as np
import math

import vtkAll as vtk

from time import time
from copy import deepcopy
from ddapp import transformUtils
import ddapp.visualization as vis
import ddapp.objectmodel as om
from ddapp import robotstate
from ddapp import botpy

import drc as lcmdrc

def addWidgetsToDict(widgets, d):

    for widget in widgets:
        if widget.objectName:
            d[str(widget.objectName)] = widget
        addWidgetsToDict(widget.children(), d)

class WidgetDict(object):

    def __init__(self, widgets):
        addWidgetsToDict(widgets, self.__dict__)

class NavigationPanel(object):

    def __init__(self, jointController, footstepDriver, playbackRobotModel, playbackJointController):

        self.jointController = jointController
        self.footstepDriver = footstepDriver
        self.playbackRobotModel = playbackRobotModel
        self.playbackJointController = playbackJointController

        loader = QtUiTools.QUiLoader()
        uifile = QtCore.QFile(':/ui/ddNavigation.ui')
        assert uifile.open(uifile.ReadOnly)

        self.widget = loader.load(uifile)

        self.ui = WidgetDict(self.widget.children())

        self.ui.captureButton.connect("clicked()", self.onCaptureButton)
        self.ui.visualizeButton.connect("clicked()", self.onVisualizeButton)
        self.ui.planButton.connect("clicked()", self.onPlanButton)

        self.goal = dict()

    ###############################
    def getSelectedGoalName(self):
        goal_name = self.ui.comboBox.currentText
        return goal_name

    def getFrameFromCombo(self):
        pose = self.goal[ self.getSelectedGoalName() ]
        #frame = transformUtils.frameFromPositionAndRPY(pose[0:3], np.degrees(pose[3:6]) )
        return pose

    def printTransform(self,t,message):
        p = t.GetPosition()
        q = t.GetOrientation()
        print "%f %f %f | %f %f %f | %s" % (p[0],p[1],p[2],q[0],q[1],q[2],message)

    def onCaptureButton(self):
        print "capture" #,self.jointController.q
        # body frame:
        #goal = self.jointController.q[0:6]

        # mid point of feet (as used by robin)
        # this assumes model publisher exists - not always true currently
        model = om.findObjectByName("robot state model")
        t_feet_mid = self.footstepDriver.getFeetMidPoint(model)
        #vis.updateFrame(t_feet_mid, "Current Goal New", parent="navigation")

        goal_name = "Goal %d" % len(self.goal)
        self.goal[goal_name] = t_feet_mid
        self.updateComboBox()

    def updateComboBox(self):
        self.ui.comboBox.clear()
        self.ui.comboBox.addItems(self.goal.keys())

    def onVisualizeButton(self):
        print "viz",self.ui.comboBox.currentText
        frame = self.getFrameFromCombo()

        #vis.showFrame(frame, self.getSelectedGoalName(), parent="navigation", scale=0.35, visible=True)
        #vis.updateFrame(frame, self.getSelectedGoalName(), parent="navigation")
        vis.updateFrame(frame, "Current Goal", parent="navigation")

    def onPlanButton(self):
        print "plan",self.ui.comboBox.currentText

        frame = self.getFrameFromCombo()
        self.footstepDriver.sendFootstepPlanRequest(frame)

def init(jointController, footstepDriver, playbackRobotModel, playbackJointController):

    global dock

    panel = NavigationPanel(jointController, footstepDriver, playbackRobotModel, playbackJointController)
    dock = app.addWidgetToDock(panel.widget)
    #dock.hide()

    return panel

<commit_msg>hide navigation panel by default<commit_after>import PythonQt
from PythonQt import QtCore, QtGui, QtUiTools
from ddapp import lcmUtils
from ddapp import applogic as app
from ddapp.utime import getUtime
from ddapp.timercallback import TimerCallback

import numpy as np
import math

import vtkAll as vtk

from time import time
from copy import deepcopy
from ddapp import transformUtils
import ddapp.visualization as vis
import ddapp.objectmodel as om
from ddapp import robotstate
from ddapp import botpy

import drc as lcmdrc

def addWidgetsToDict(widgets, d):

    for widget in widgets:
        if widget.objectName:
            d[str(widget.objectName)] = widget
        addWidgetsToDict(widget.children(), d)

class WidgetDict(object):

    def __init__(self, widgets):
        addWidgetsToDict(widgets, self.__dict__)

class NavigationPanel(object):

    def __init__(self, jointController, footstepDriver, playbackRobotModel, playbackJointController):

        self.jointController = jointController
        self.footstepDriver = footstepDriver
        self.playbackRobotModel = playbackRobotModel
        self.playbackJointController = playbackJointController

        loader = QtUiTools.QUiLoader()
        uifile = QtCore.QFile(':/ui/ddNavigation.ui')
        assert uifile.open(uifile.ReadOnly)

        self.widget = loader.load(uifile)

        self.ui = WidgetDict(self.widget.children())

        self.ui.captureButton.connect("clicked()", self.onCaptureButton)
        self.ui.visualizeButton.connect("clicked()", self.onVisualizeButton)
        self.ui.planButton.connect("clicked()", self.onPlanButton)

        self.goal = dict()

    ###############################
    def getSelectedGoalName(self):
        goal_name = self.ui.comboBox.currentText
        return goal_name

    def getFrameFromCombo(self):
        pose = self.goal[ self.getSelectedGoalName() ]
        #frame = transformUtils.frameFromPositionAndRPY(pose[0:3], np.degrees(pose[3:6]) )
        return pose

    def printTransform(self,t,message):
        p = t.GetPosition()
        q = t.GetOrientation()
        print "%f %f %f | %f %f %f | %s" % (p[0],p[1],p[2],q[0],q[1],q[2],message)

    def onCaptureButton(self):
        print "capture" #,self.jointController.q
        # body frame:
        #goal = self.jointController.q[0:6]

        # mid point of feet (as used by robin)
        # this assumes model publisher exists - not always true currently
        model = om.findObjectByName("robot state model")
        t_feet_mid = self.footstepDriver.getFeetMidPoint(model)
        #vis.updateFrame(t_feet_mid, "Current Goal New", parent="navigation")

        goal_name = "Goal %d" % len(self.goal)
        self.goal[goal_name] = t_feet_mid
        self.updateComboBox()

    def updateComboBox(self):
        self.ui.comboBox.clear()
        self.ui.comboBox.addItems(self.goal.keys())

    def onVisualizeButton(self):
        print "viz",self.ui.comboBox.currentText
        frame = self.getFrameFromCombo()

        #vis.showFrame(frame, self.getSelectedGoalName(), parent="navigation", scale=0.35, visible=True)
        #vis.updateFrame(frame, self.getSelectedGoalName(), parent="navigation")
        vis.updateFrame(frame, "Current Goal", parent="navigation")

    def onPlanButton(self):
        print "plan",self.ui.comboBox.currentText

        frame = self.getFrameFromCombo()
        self.footstepDriver.sendFootstepPlanRequest(frame)

def init(jointController, footstepDriver, playbackRobotModel, playbackJointController):

    global dock

    panel = NavigationPanel(jointController, footstepDriver, playbackRobotModel, playbackJointController)
    dock = app.addWidgetToDock(panel.widget)
    dock.hide()

    return panel

<|endoftext|>


<commit_before>import asyncio

import asyncio_mongo
from tornado.options import define, options

from wdim import client
from wdim.client.database import MongoLayer

try:
    from IPython import embed
except ImportError:
    raise ImportError('Shell requires IPython >= 0.12.0')

define('port', default=27017, help='TokuMX port')
define('db', default='127.0.0.1', help='TokuMX IP')
define('dbname', default='wdim20150921', help='The Mongo database to use')
define('namespace', default='sys_shell', help='The namespace to use')

async def get_context():
    connection = await MongoLayer.connect(options.db, options.port, options.dbname)

    assert await client.Storable.connect(connection)

    return {
        'client': client,
        # 'database': database,
        # 'mongo_connection': connection,
    }

def main(loop):
    embed(user_ns={
        'loop': loop,
        'run': lambda x: loop.run_until_complete(x),
        **loop.run_until_complete(get_context())
    })

if __name__ == '__main__':
    options.parse_command_line()
    loop = asyncio.get_event_loop()
    main(loop)
<commit_msg>Update shell init<commit_after>import asyncio

from tornado.options import define, options

from wdim import client
from wdim.client.database import MongoLayer
from wdim.client.database import ElasticSearchLayer
from wdim.client.database import CompoundWriteLayer

try:
    from IPython import embed
except ImportError:
    raise ImportError('Shell requires IPython >= 0.12.0')

define('port', default=27017, help='TokuMX port')
define('db', default='127.0.0.1', help='TokuMX IP')
define('dbname', default='wdim20150921', help='The Mongo database to use')
define('namespace', default='sys_shell', help='The namespace to use')

async def get_context():
    # connection = await MongoLayer.connect(options.db, options.port, options.dbname)

    mongo_layer = await MongoLayer.connect()
    es_layer = await ElasticSearchLayer.connect()
    connection = CompoundWriteLayer(es_layer, mongo_layer)

    assert await client.Storable.connect(connection)

    namespace = await client.Namespace.get_by_namespace('Shell')
    collection = await namespace.get_collection('first')

    return {
        'client': client,
        'namespace': namespace,
        'collection': collection,
        # 'database': database,
        # 'mongo_connection': connection,
    }

def main(loop):
    embed(user_ns={
        'loop': loop,
        'run': lambda x: loop.run_until_complete(x),
        **loop.run_until_complete(get_context())
    })

if __name__ == '__main__':
    options.parse_command_line()
    loop = asyncio.get_event_loop()
    main(loop)
<|endoftext|>


<commit_before>__author__ = 'Fredrik A. Madsen-Malmo'

from flask import Flask, flash, render_template, g, redirect, url_for

import socket

import models
import forms

import os

DEBUG = True
PORT = port = int(os.environ.get('PORT', 33507))
HOST = '0.0.0.0'

app = Flask(__name__)
app.secret_key = os.environ.get('secret_key', 'error')

# Before and after request

@app.before_request
def before_request():
    """Connect to db before each req"""
    g.db = models.DATABASE
    g.db.connect()

@app.after_request
def after_request(response):
	"""Close the db connection after each req"""
	g.db.close()

	return response

# Other functions

@app.route('/online/<ip>', methods=['POST', 'GET'])
def is_online(ip):
    try:
        target = models.Entry.get(models.Entry.ip == ip)
    except Exception:
        return 'False'

    try:
        socket.setdefaulttimeout(2.0)
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        result = sock.connect_ex((ip, int(target.port)))
    except Exception:
        target.online = False
        target.save()

        return 'False'

    if result == 0:
        target.online = True
        target.save()

        return 'True'
    else:
        target.online = False
        target.save()

        return 'False'

# Routes

@app.route('/', methods=['POST', 'GET'])
def index():
    stream = models.Entry.select().order_by(-models.Entry.online)
    form = forms.EntryForm()

    if form.validate_on_submit():
        try:
            if len(form.name.data.strip()) == 0:
                models.Entry.create(
                    ip=form.ip.data.strip(),
                    port=form.port.data.strip(),
                    online=True)
            else:
                models.Entry.create(
                    name=form.name.data.strip(),
                    ip=form.ip.data.strip(),
                    port=form.port.data.strip(),
                    online=True)

            flash('Entry created.', 'success')

        except Exception:
            pass
    else:
        pass

    return render_template('index.html', form=form, stream=stream)

# Start app

if __name__ == '__main__':
    models.initialize()

    try:
        models.Entry.get(models.Entry.ip == '93.184.204.215')
    except Exception:
        models.Entry.create(
        name='fotoply',
        ip='93.184.204.215',
        port='7077')

    app.run(debug=DEBUG, port=PORT, host=HOST)
<commit_msg>decreased timeout in app method<commit_after>__author__ = 'Fredrik A. Madsen-Malmo'

from flask import Flask, flash, render_template, g, redirect, url_for

import socket

import models
import forms

import os

DEBUG = True
PORT = port = int(os.environ.get('PORT', 33507))
HOST = '0.0.0.0'

app = Flask(__name__)
app.secret_key = os.environ.get('secret_key', 'error')

# Before and after request

@app.before_request
def before_request():
    """Connect to db before each req"""
    g.db = models.DATABASE
    g.db.connect()

@app.after_request
def after_request(response):
	"""Close the db connection after each req"""
	g.db.close()

	return response

# Other functions

@app.route('/online/<ip>', methods=['POST', 'GET'])
def is_online(ip):
    try:
        target = models.Entry.get(models.Entry.ip == ip)
    except Exception:
        return 'False'

    try:
        socket.setdefaulttimeout(1.0)
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        result = sock.connect_ex((ip, int(target.port)))
    except Exception:
        target.online = False
        target.save()

        return 'False'

    if result == 0:
        target.online = True
        target.save()

        return 'True'
    else:
        target.online = False
        target.save()

        return 'False'

# Routes

@app.route('/', methods=['POST', 'GET'])
def index():
    stream = models.Entry.select().order_by(-models.Entry.online)
    form = forms.EntryForm()

    if form.validate_on_submit():
        try:
            if len(form.name.data.strip()) == 0:
                models.Entry.create(
                    ip=form.ip.data.strip(),
                    port=form.port.data.strip(),
                    online=True)
            else:
                models.Entry.create(
                    name=form.name.data.strip(),
                    ip=form.ip.data.strip(),
                    port=form.port.data.strip(),
                    online=True)

            flash('Entry created.', 'success')

        except Exception:
            pass
    else:
        pass

    return render_template('index.html', form=form, stream=stream)

# Start app

if __name__ == '__main__':
    models.initialize()

    try:
        models.Entry.get(models.Entry.ip == '93.184.204.215')
    except Exception:
        models.Entry.create(
        name='fotoply',
        ip='93.184.204.215',
        port='7077')

    app.run(debug=DEBUG, port=PORT, host=HOST)
<|endoftext|>


<commit_before>"""
Filtering functions
"""
import os
import numpy as np
import os.path

from scipy.signal import butter, lfilter, filtfilt

from yass.batch import BatchProcessor
from yass.util import check_for_files, ExpandPath, LoadFile
from yass.preprocess.standarize import standard_deviation

# *** DEPRECATED ****
@check_for_files(
    filenames=[
        ExpandPath('output_filename'),
        LoadFile('output_filename', 'yaml')
    ],
    mode='extract',
    relative_to='output_path')
def butterworth(path_to_data,
                dtype,
                n_channels,
                data_order,
                low_frequency,
                high_factor,
                order,
                sampling_frequency,
                max_memory,
                output_path,
                output_dtype,
                output_filename='filtered.bin',
                if_file_exists='skip'):
    """Filter (butterworth) recordings in batches

    Parameters
    ----------
    path_to_data: str
        Path to recordings in binary format

    dtype: str
        Recordings dtype

    n_channels: int
        Number of channels in the recordings

    data_order: str
        Recordings order, one of ('channels', 'samples'). In a dataset with k
        observations per channel and j channels: 'channels' means first k
        contiguous observations come from channel 0, then channel 1, and so
        on. 'sample' means first j contiguous data are the first observations
        from all channels, then the second observations from all channels and
        so on

    low_frequency: int
        Low pass frequency (Hz)

    high_factor: float
        High pass factor (proportion of sampling rate)

    order: int
        Order of Butterworth filter

    sampling_frequency: int
        Recordings sampling frequency in Hz

    max_memory: str
        Max memory to use in each batch (e.g. 100MB, 1GB)

    output_path: str
        Folder to store the filtered recordings

    output_filename: str, optional
        How to name the file, defaults to filtered.bin

    output_dtype: str
        dtype for filtered data

    if_file_exists: str, optional
        One of 'overwrite', 'abort', 'skip'. If 'overwrite' it replaces the
        file if it exists, if 'abort' if raise a ValueError exception if
        the file exists, if 'skip' if skips the operation if the file
        exists

    Returns
    -------
    standarized_path: str
        Location to filtered recordings

    standarized_params: dict
        A dictionary with the parameters for the filtered recordings
        (dtype, n_channels, data_order)
    """
    # init batch processor
    bp = BatchProcessor(
        path_to_data,
        dtype,
        n_channels,
        data_order,
        max_memory,
        buffer_size=200)

    _output_path = os.path.join(output_path, output_filename)

    (path, params) = bp.multi_channel_apply(
        _butterworth,
        mode='disk',
        cleanup_function=fix_indexes,
        output_path=_output_path,
        if_file_exists=if_file_exists,
        cast_dtype=output_dtype,
        low_frequency=low_frequency,
        high_factor=high_factor,
        order=order,
        sampling_frequency=sampling_frequency)

    return path, params

# **** DEPRECATED *****
def _butterworth(ts, low_frequency, high_factor, order, sampling_frequency):
    """Butterworth filter for a one dimensional time series

    Parameters
    ----------
    ts: np.array
        T  numpy array, where T is the number of time samples
    low_frequency: int
        Low pass frequency (Hz)
    high_factor: float
        High pass factor (proportion of sampling rate)
    order: int
        Order of Butterworth filter
    sampling_frequency: int
        Sampling frequency (Hz)

    Notes
    -----
    This function can only be applied to a one dimensional array, to apply
    it to multiple channels use butterworth

    Raises
    ------
    NotImplementedError
        If a multidmensional array is passed
    """

    if ts.ndim == 1:

        (T, ) = ts.shape
        low = float(low_frequency) / sampling_frequency * 2
        high = float(high_factor) * 2
        b, a = butter(order, [low, high], btype='band')

        return lfilter(b, a, ts)

    else:

        T, C = ts.shape
        low = float(low_frequency) / sampling_frequency * 2
        high = float(high_factor) * 2
        b, a = butter(order, [low, high], btype='band')

        return lfilter(b, a, ts, 0)

# **** DEPRECATED *****
def fix_indexes(res, idx_local, idx, buffer_size):
    """Fixes indexes from detected spikes in batches

    Parameters
    ----------
    res: tuple
        A result from the butterworth
    idx_local: slice
        A slice object indicating the indices for the data (excluding buffer)
    idx: slice
        A slice object indicating the absolute location of the data
    buffer_size: int
        Buffer size
    """

    # get limits for the data (exlude indexes that have buffer data)
    data_start = idx_local[0].start
    data_end = idx_local[0].stop

    return res[data_start:data_end]

def filter_standardize_parallel(data_in, low_frequency, high_factor, order,
                                sampling_frequency, buffer_size, filename_dat,
                                n_channels):
    """ Butterworth as explained above; TO FILL IN
    """

    # Load data from disk; buffer as required:
    idx_list, chunk_idx = data_in[0], data_in[1]

    # prPurple("Loading data files for chunk: "+str(chunk_idx))

    # New indexes
    idx_start = idx_list[0]
    idx_stop = idx_list[1]

    data_start = idx_start
    data_end = idx_stop

    # *****************************************************************
    # ********** LOAD RAW RECORDING ***********************************
    # *****************************************************************

    with open(filename_dat, "rb") as fin:
        if data_start == 0:
            # Seek position and read N bytes
            recordings_1D = np.fromfile(
                fin,
                dtype='int16',
                count=(data_end + buffer_size) * n_channels)
            recordings_1D = np.hstack((np.zeros(
                buffer_size * n_channels, dtype='int16'), recordings_1D))
        else:
            fin.seek((data_start - buffer_size) * 2 * n_channels, os.SEEK_SET)
            recordings_1D = np.fromfile(
                fin,
                dtype='int16',
                count=((data_end - data_start + buffer_size * 2) * n_channels))

        # If at end of recording
        if len(recordings_1D) != ((data_end - data_start +
                                   buffer_size * 2) * n_channels):
            recordings_1D = np.hstack((recordings_1D,
                                       np.zeros(buffer_size * n_channels,
                                                dtype='int16')))

    fin.close()

    # Convert to 2D array
    recordings = recordings_1D.reshape(-1, n_channels)

    ts = recordings

    # ******************************************************************
    # *********** FILTER DATA ******************************************
    # ******************************************************************
    T, C = ts.shape

    low = float(low_frequency) / sampling_frequency * 2
    high = float(high_factor) * 2
    #b, a = butter(order, [low, high], btype='band')
    b, a = butter(order, low, btype='high', analog=False)

    output = np.zeros((T, C), 'float32')
    for c in range(C):
        output[:, c] = filtfilt(b, a, ts[:, c])

    # Fix indexes function: remove buffers
    res = output[buffer_size:data_end - data_start + buffer_size]

    # Standardize data
    sd = standard_deviation(res, sampling_frequency)
    standardized = np.divide(res, sd)

    return standardized

def filter_standardize(data_in, low_frequency, high_factor, order,
                       sampling_frequency, buffer_size, filename_dat,
                       n_channels, root_folder, output_directory):
    """Butterworth filter for a one dimensional time series

    Parameters
    ----------
    ts: np.array
        T  numpy array, where T is the number of time samples
    low_frequency: int
        Low pass frequency (Hz)
    high_factor: float
        High pass factor (proportion of sampling rate)
    order: int
        Order of Butterworth filter
    sampling_frequency: int
        Sampling frequency (Hz)

    Notes
    -----
    This function can only be applied to a one dimensional array, to apply
    it to multiple channels use butterworth

    Raises
    ------
    NotImplementedError
        If a multidmensional array is passed
    """

    # Load data from disk; buffer as required:
    idx_list, chunk_idx = data_in[0], data_in[1]

    # prPurple("Loading data files for chunk: "+str(chunk_idx))

    # New indexes
    idx_start = idx_list[0]
    idx_stop = idx_list[1]

    data_start = idx_start
    data_end = idx_stop

    # *****************************************************************
    # ********** LOAD RAW RECORDING ***********************************
    # *****************************************************************

    with open(filename_dat, "rb") as fin:
        if data_start == 0:
            # Seek position and read N bytes
            recordings_1D = np.fromfile(
                fin,
                dtype='int16',
                count=(data_end + buffer_size) * n_channels)
            recordings_1D = np.hstack((np.zeros(
                buffer_size * n_channels, dtype='int16'), recordings_1D))
        else:
            fin.seek((data_start - buffer_size) * 2 * n_channels, os.SEEK_SET)
            recordings_1D = np.fromfile(
                fin,
                dtype='int16',
                count=((data_end - data_start + buffer_size * 2) * n_channels))

        # If at end of recording
        if len(recordings_1D) != ((data_end - data_start +
                                   buffer_size * 2) * n_channels):
            recordings_1D = np.hstack((recordings_1D,
                                       np.zeros(buffer_size * n_channels,
                                                dtype='int16')))

    fin.close()

    # Convert to 2D array
    recordings = recordings_1D.reshape(-1, n_channels)

    ts = recordings

    # ******************************************************************
    # *********** FILTER DATA ******************************************
    # ******************************************************************

    if ts.ndim == 1:
        print("SINGLE CHANNEL FILTER NOTE AVAILABLE.... !")
        quit()
        (T, ) = ts.shape
        low = float(low_frequency) / sampling_frequency * 2
        high = float(high_factor) * 2
        b, a = butter(order, [low, high], btype='band')

        output = lfilter(b, a, ts)

    else:
        #T, C = ts.shape
        #low = float(low_frequency) / sampling_frequency * 2
        #high = float(high_factor) * 2
        #b, a = butter(order, [low, high], btype='band')

        #output = np.zeros((T, C), 'float32')
        # for c in range(C):
            #output[:, c] = lfilter(b, a, ts[:, c])

        T, C = ts.shape

        low = float(low_frequency) / sampling_frequency * 2
        high = float(high_factor) * 2
        b, a = butter(order, low, btype='high', analog=False)

        output = np.zeros((T, C), 'float32')
        for c in range(C):
            output[:, c] = filtfilt(b, a, ts[:, c])

    # Fix indexes function
    res = output[buffer_size:data_end - data_start + buffer_size]

    # Standardize data
    sd = standard_deviation(res, sampling_frequency)
    standardized = np.divide(res, sd)

    np.save(
        os.path.join(root_folder, output_directory,
                     "filtered_files/standardized_" + str(chunk_idx).zfill(6)),
        standardized)

    return standardized.shape

def merge_filtered_files(root_folder, output_directory):

    path = os.path.join(root_folder, output_directory, 'filtered_files/')
    filenames = os.listdir(path)
    filenames_sorted = sorted(filenames)

    f_out = os.path.join(root_folder, output_directory, "standarized.bin")
    print('...saving standardized file: ', f_out)

    f = open(f_out, 'wb')
    for fname in filenames_sorted:
        res = np.load(os.path.join(path, fname))
        res.tofile(f)
        os.remove(path + fname)
<commit_msg>updated code due to fn rename<commit_after>"""
Filtering functions
"""
import os
import numpy as np
import os.path

from scipy.signal import butter, lfilter, filtfilt

from yass.batch import BatchProcessor
from yass.util import check_for_files, ExpandPath, LoadFile
from yass.preprocess.standarize import _standard_deviation

# *** DEPRECATED ****
@check_for_files(
    filenames=[
        ExpandPath('output_filename'),
        LoadFile('output_filename', 'yaml')
    ],
    mode='extract',
    relative_to='output_path')
def butterworth(path_to_data,
                dtype,
                n_channels,
                data_order,
                low_frequency,
                high_factor,
                order,
                sampling_frequency,
                max_memory,
                output_path,
                output_dtype,
                output_filename='filtered.bin',
                if_file_exists='skip'):
    """Filter (butterworth) recordings in batches

    Parameters
    ----------
    path_to_data: str
        Path to recordings in binary format

    dtype: str
        Recordings dtype

    n_channels: int
        Number of channels in the recordings

    data_order: str
        Recordings order, one of ('channels', 'samples'). In a dataset with k
        observations per channel and j channels: 'channels' means first k
        contiguous observations come from channel 0, then channel 1, and so
        on. 'sample' means first j contiguous data are the first observations
        from all channels, then the second observations from all channels and
        so on

    low_frequency: int
        Low pass frequency (Hz)

    high_factor: float
        High pass factor (proportion of sampling rate)

    order: int
        Order of Butterworth filter

    sampling_frequency: int
        Recordings sampling frequency in Hz

    max_memory: str
        Max memory to use in each batch (e.g. 100MB, 1GB)

    output_path: str
        Folder to store the filtered recordings

    output_filename: str, optional
        How to name the file, defaults to filtered.bin

    output_dtype: str
        dtype for filtered data

    if_file_exists: str, optional
        One of 'overwrite', 'abort', 'skip'. If 'overwrite' it replaces the
        file if it exists, if 'abort' if raise a ValueError exception if
        the file exists, if 'skip' if skips the operation if the file
        exists

    Returns
    -------
    standarized_path: str
        Location to filtered recordings

    standarized_params: dict
        A dictionary with the parameters for the filtered recordings
        (dtype, n_channels, data_order)
    """
    # init batch processor
    bp = BatchProcessor(
        path_to_data,
        dtype,
        n_channels,
        data_order,
        max_memory,
        buffer_size=200)

    _output_path = os.path.join(output_path, output_filename)

    (path, params) = bp.multi_channel_apply(
        _butterworth,
        mode='disk',
        cleanup_function=fix_indexes,
        output_path=_output_path,
        if_file_exists=if_file_exists,
        cast_dtype=output_dtype,
        low_frequency=low_frequency,
        high_factor=high_factor,
        order=order,
        sampling_frequency=sampling_frequency)

    return path, params

# **** DEPRECATED *****
def _butterworth(ts, low_frequency, high_factor, order, sampling_frequency):
    """Butterworth filter for a one dimensional time series

    Parameters
    ----------
    ts: np.array
        T  numpy array, where T is the number of time samples
    low_frequency: int
        Low pass frequency (Hz)
    high_factor: float
        High pass factor (proportion of sampling rate)
    order: int
        Order of Butterworth filter
    sampling_frequency: int
        Sampling frequency (Hz)

    Notes
    -----
    This function can only be applied to a one dimensional array, to apply
    it to multiple channels use butterworth

    Raises
    ------
    NotImplementedError
        If a multidmensional array is passed
    """

    if ts.ndim == 1:

        (T, ) = ts.shape
        low = float(low_frequency) / sampling_frequency * 2
        high = float(high_factor) * 2
        b, a = butter(order, [low, high], btype='band')

        return lfilter(b, a, ts)

    else:

        T, C = ts.shape
        low = float(low_frequency) / sampling_frequency * 2
        high = float(high_factor) * 2
        b, a = butter(order, [low, high], btype='band')

        return lfilter(b, a, ts, 0)

# **** DEPRECATED *****
def fix_indexes(res, idx_local, idx, buffer_size):
    """Fixes indexes from detected spikes in batches

    Parameters
    ----------
    res: tuple
        A result from the butterworth
    idx_local: slice
        A slice object indicating the indices for the data (excluding buffer)
    idx: slice
        A slice object indicating the absolute location of the data
    buffer_size: int
        Buffer size
    """

    # get limits for the data (exlude indexes that have buffer data)
    data_start = idx_local[0].start
    data_end = idx_local[0].stop

    return res[data_start:data_end]

def filter_standardize_parallel(data_in, low_frequency, high_factor, order,
                                sampling_frequency, buffer_size, filename_dat,
                                n_channels):
    """ Butterworth as explained above; TO FILL IN
    """

    # Load data from disk; buffer as required:
    idx_list, chunk_idx = data_in[0], data_in[1]

    # prPurple("Loading data files for chunk: "+str(chunk_idx))

    # New indexes
    idx_start = idx_list[0]
    idx_stop = idx_list[1]

    data_start = idx_start
    data_end = idx_stop

    # *****************************************************************
    # ********** LOAD RAW RECORDING ***********************************
    # *****************************************************************

    with open(filename_dat, "rb") as fin:
        if data_start == 0:
            # Seek position and read N bytes
            recordings_1D = np.fromfile(
                fin,
                dtype='int16',
                count=(data_end + buffer_size) * n_channels)
            recordings_1D = np.hstack((np.zeros(
                buffer_size * n_channels, dtype='int16'), recordings_1D))
        else:
            fin.seek((data_start - buffer_size) * 2 * n_channels, os.SEEK_SET)
            recordings_1D = np.fromfile(
                fin,
                dtype='int16',
                count=((data_end - data_start + buffer_size * 2) * n_channels))

        # If at end of recording
        if len(recordings_1D) != ((data_end - data_start +
                                   buffer_size * 2) * n_channels):
            recordings_1D = np.hstack((recordings_1D,
                                       np.zeros(buffer_size * n_channels,
                                                dtype='int16')))

    fin.close()

    # Convert to 2D array
    recordings = recordings_1D.reshape(-1, n_channels)

    ts = recordings

    # ******************************************************************
    # *********** FILTER DATA ******************************************
    # ******************************************************************
    T, C = ts.shape

    low = float(low_frequency) / sampling_frequency * 2
    high = float(high_factor) * 2
    #b, a = butter(order, [low, high], btype='band')
    b, a = butter(order, low, btype='high', analog=False)

    output = np.zeros((T, C), 'float32')
    for c in range(C):
        output[:, c] = filtfilt(b, a, ts[:, c])

    # Fix indexes function: remove buffers
    res = output[buffer_size:data_end - data_start + buffer_size]

    # Standardize data
    sd = _standard_deviation(res, sampling_frequency)
    standardized = np.divide(res, sd)

    return standardized

def filter_standardize(data_in, low_frequency, high_factor, order,
                       sampling_frequency, buffer_size, filename_dat,
                       n_channels, root_folder, output_directory):
    """Butterworth filter for a one dimensional time series

    Parameters
    ----------
    ts: np.array
        T  numpy array, where T is the number of time samples
    low_frequency: int
        Low pass frequency (Hz)
    high_factor: float
        High pass factor (proportion of sampling rate)
    order: int
        Order of Butterworth filter
    sampling_frequency: int
        Sampling frequency (Hz)

    Notes
    -----
    This function can only be applied to a one dimensional array, to apply
    it to multiple channels use butterworth

    Raises
    ------
    NotImplementedError
        If a multidmensional array is passed
    """

    # Load data from disk; buffer as required:
    idx_list, chunk_idx = data_in[0], data_in[1]

    # prPurple("Loading data files for chunk: "+str(chunk_idx))

    # New indexes
    idx_start = idx_list[0]
    idx_stop = idx_list[1]

    data_start = idx_start
    data_end = idx_stop

    # *****************************************************************
    # ********** LOAD RAW RECORDING ***********************************
    # *****************************************************************

    with open(filename_dat, "rb") as fin:
        if data_start == 0:
            # Seek position and read N bytes
            recordings_1D = np.fromfile(
                fin,
                dtype='int16',
                count=(data_end + buffer_size) * n_channels)
            recordings_1D = np.hstack((np.zeros(
                buffer_size * n_channels, dtype='int16'), recordings_1D))
        else:
            fin.seek((data_start - buffer_size) * 2 * n_channels, os.SEEK_SET)
            recordings_1D = np.fromfile(
                fin,
                dtype='int16',
                count=((data_end - data_start + buffer_size * 2) * n_channels))

        # If at end of recording
        if len(recordings_1D) != ((data_end - data_start +
                                   buffer_size * 2) * n_channels):
            recordings_1D = np.hstack((recordings_1D,
                                       np.zeros(buffer_size * n_channels,
                                                dtype='int16')))

    fin.close()

    # Convert to 2D array
    recordings = recordings_1D.reshape(-1, n_channels)

    ts = recordings

    # ******************************************************************
    # *********** FILTER DATA ******************************************
    # ******************************************************************

    if ts.ndim == 1:
        print("SINGLE CHANNEL FILTER NOTE AVAILABLE.... !")
        quit()
        (T, ) = ts.shape
        low = float(low_frequency) / sampling_frequency * 2
        high = float(high_factor) * 2
        b, a = butter(order, [low, high], btype='band')

        output = lfilter(b, a, ts)

    else:
        #T, C = ts.shape
        #low = float(low_frequency) / sampling_frequency * 2
        #high = float(high_factor) * 2
        #b, a = butter(order, [low, high], btype='band')

        #output = np.zeros((T, C), 'float32')
        # for c in range(C):
            #output[:, c] = lfilter(b, a, ts[:, c])

        T, C = ts.shape

        low = float(low_frequency) / sampling_frequency * 2
        high = float(high_factor) * 2
        b, a = butter(order, low, btype='high', analog=False)

        output = np.zeros((T, C), 'float32')
        for c in range(C):
            output[:, c] = filtfilt(b, a, ts[:, c])

    # Fix indexes function
    res = output[buffer_size:data_end - data_start + buffer_size]

    # Standardize data
    sd = _standard_deviation(res, sampling_frequency)
    standardized = np.divide(res, sd)

    np.save(
        os.path.join(root_folder, output_directory,
                     "filtered_files/standardized_" + str(chunk_idx).zfill(6)),
        standardized)

    return standardized.shape

def merge_filtered_files(root_folder, output_directory):

    path = os.path.join(root_folder, output_directory, 'filtered_files/')
    filenames = os.listdir(path)
    filenames_sorted = sorted(filenames)

    f_out = os.path.join(root_folder, output_directory, "standarized.bin")
    print('...saving standardized file: ', f_out)

    f = open(f_out, 'wb')
    for fname in filenames_sorted:
        res = np.load(os.path.join(path, fname))
        res.tofile(f)
        os.remove(path + fname)
<|endoftext|>


<commit_before>#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# Copyright (C) 2005 onwards University of Deusto
# All rights reserved.
#
# This software is licensed as described in the file COPYING, which
# you should have received as part of this distribution.
#
# This software consists of contributions made by many individuals,
# listed below:
#
# Author: Jaime Irurzun <jaime.irurzun@gmail.com>
#

import os
import tempfile
import subprocess
import threading

import voodoo.log as log
from voodoo.log import logged

import voodoo.configuration as ConfigurationErrors
import weblab.experiment.devices.exc as DeviceErrors

# Quick help for debugging:
#digilentmofas = open("/tmp/digilentmofas", 'w+')
#def printar(msg):
#   digilentmofas.write(str(msg)+"\n")
#   digilentmofas.flush()

class DigilentAdept(object):

    def __init__(self, cfg_manager):
        super(DigilentAdept,self).__init__()
        self._cfg_manager= cfg_manager
        self._lock = threading.Lock()
        self._busy = False

    def _reserve_device(self):
        self._lock.acquire()
        try:
            if self._busy:
                # TODO check if it's really busy
                # Refactor this code so that it has "simple sessions"
                # so that if the program is finished, it starts programming the next device
                # Consider that this might happen with any device, so
                # DigilentAdept might be a subclass of Experiment or sth
                raise AlreadyProgrammingDeviceError(
                    "This experiment is already programming the device"
                )
            self._busy = True
        finally:
            self._lock.release()

    def _release_device(self):
        self._lock.acquire()
        self._busy = False
        self._lock.release()

    @logged()
    def program_device(self, program_path):
        file_content, digilent_adept = self._parse_configuration_to_program()

        cmd_params = file_content.replace('$FILE',program_path)
        self._reserve_device()
        try:
            res, out, err = self._execute(cmd_params, digilent_adept)
            print res, out, err
        finally:
            self._release_device()

        self._log(res,out,err)

        # TODO: this could be improved :-D
        if out.find("ERROR") >= 0:
            error_messages = [ i for i in out.split('\n') if i.find('ERROR') >= 0 ]
            error_messages += '; ' + err
            raise ProgrammingGotErrors(
                    "Digilent Adept raised errors while programming the device: %s" % error_messages
                )
        if res != 0:
            raise ProgrammingGotErrors(
                    "Digilent Adept returned %i" % res
                )

    def _execute(self, cmd_params, digilent_adept):
        # Kludge!
        full_cmd_line = digilent_adept + cmd_params.split(" ")

        log.log(DigilentAdept,log.level.Warning,"Executing %s" % full_cmd_line)

        try:
            popen = subprocess.Popen(
                full_cmd_line,
                stdin  = subprocess.PIPE,
                stdout = subprocess.PIPE,
                stderr = subprocess.PIPE
            )
        except Exception as e:
            raise ErrorProgrammingDeviceError(
                "There was an error while executing Digilent Adept: %s" % e
            )
        # TODO: make use of popen.poll to make this asynchronous
        try:
            result = popen.wait()
        except Exception as e:
            raise ErrorWaitingForProgrammingFinishedError(
                "There was an error while waiting for Digilent Adept to finish: %s" % e
            )

        try:
            stdout_result = popen.stdout.read()
            stderr_result = popen.stderr.read()
        except Exception as e:
            raise ErrorRetrievingOutputFromProgrammingProgramError(
                "There was an error while retrieving the output of Digilent Adept: %s" % e
            )
        return result, stdout_result, stderr_result

    def _parse_configuration_to_program(self):
        try:
            program_file_content = self._cfg_manager.get_value('digilent_adept_batch_content')
            digilent_adept = self._cfg_manager.get_value('digilent_adept_full_path')
        except ConfigurationErrors.KeyNotFoundError as knfe:
            raise CantFindDigilentAdeptProperty(
                    "Can't find in configuration manager the property '%s'" % knfe.key
                )
        return program_file_content, digilent_adept

    def _create_batch_file(self, content, program_path):
        fd, file_name = tempfile.mkstemp(prefix='digilent_adept_batch_file_',suffix='.cmd')
        os.write(fd, content.replace('$FILE',program_path))
        os.close(fd)
        return file_name

    def _log(self, result_code, output, stderr):
        log.log(DigilentAdept,log.level.Info,"Device programming was finished. Result code: %i\n<output>\n%s\n</output><stderr>\n%s\n</stderr>" % (
                result_code,
                output,
                stderr
            )
        )

class CantFindDigilentAdeptProperty(DeviceErrors.MisconfiguredDeviceError):
    def __init__(self, *args, **kargs):
        DeviceErrors.MisconfiguredDeviceError.__init__(self, *args, **kargs)

class AlreadyProgrammingDeviceError(DeviceErrors.AlreadyProgrammingDeviceError):
    def __init__(self, *args, **kargs):
        DeviceErrors.AlreadyProgrammingDeviceError.__init__(self, *args, **kargs)

class ErrorProgrammingDeviceError(DeviceErrors.ProgrammingDeviceError):
    def __init__(self,*args,**kargs):
        DeviceErrors.ProgrammingDeviceError.__init__(self,*args,**kargs)

class ErrorRetrievingOutputFromProgrammingProgramError(DeviceErrors.ProgrammingDeviceError):
    def __init__(self,*args,**kargs):
        DeviceErrors.ProgrammingDeviceError.__init__(self,*args,**kargs)

class ErrorWaitingForProgrammingFinishedError(DeviceErrors.ProgrammingDeviceError):
    def __init__(self,*args,**kargs):
        DeviceErrors.ProgrammingDeviceError.__init__(self,*args,**kargs)

class ProgrammingGotErrors(DeviceErrors.ProgrammingDeviceError):
    def __init__(self,*args,**kargs):
        DeviceErrors.ProgrammingDeviceError.__init__(self,*args,**kargs)
<commit_msg>Adapt digilent_adept, pressing N just in case a question is added<commit_after>#!/usr/bin/python
# -*- coding: utf-8 -*-
#
# Copyright (C) 2005 onwards University of Deusto
# All rights reserved.
#
# This software is licensed as described in the file COPYING, which
# you should have received as part of this distribution.
#
# This software consists of contributions made by many individuals,
# listed below:
#
# Author: Jaime Irurzun <jaime.irurzun@gmail.com>
#

import os
import tempfile
import subprocess
import threading

import voodoo.log as log
from voodoo.log import logged

import voodoo.configuration as ConfigurationErrors
import weblab.experiment.devices.exc as DeviceErrors

# Quick help for debugging:
#digilentmofas = open("/tmp/digilentmofas", 'w+')
#def printar(msg):
#   digilentmofas.write(str(msg)+"\n")
#   digilentmofas.flush()

class DigilentAdept(object):

    def __init__(self, cfg_manager):
        super(DigilentAdept,self).__init__()
        self._cfg_manager= cfg_manager
        self._lock = threading.Lock()
        self._busy = False

    def _reserve_device(self):
        self._lock.acquire()
        try:
            if self._busy:
                # TODO check if it's really busy
                # Refactor this code so that it has "simple sessions"
                # so that if the program is finished, it starts programming the next device
                # Consider that this might happen with any device, so
                # DigilentAdept might be a subclass of Experiment or sth
                raise AlreadyProgrammingDeviceError(
                    "This experiment is already programming the device"
                )
            self._busy = True
        finally:
            self._lock.release()

    def _release_device(self):
        self._lock.acquire()
        self._busy = False
        self._lock.release()

    @logged()
    def program_device(self, program_path):
        file_content, digilent_adept = self._parse_configuration_to_program()

        cmd_params = file_content.replace('$FILE',program_path)
        self._reserve_device()
        try:
            res, out, err = self._execute(cmd_params, digilent_adept)
            print res, out, err
        finally:
            self._release_device()

        self._log(res,out,err)

        # TODO: this could be improved :-D
        if out.find("ERROR") >= 0:
            error_messages = [ i for i in out.split('\n') if i.find('ERROR') >= 0 ]
            error_messages += '; ' + err
            raise ProgrammingGotErrors(
                    "Digilent Adept raised errors while programming the device: %s" % error_messages
                )
        if res != 0:
            raise ProgrammingGotErrors(
                    "Digilent Adept returned %i" % res
                )

    def _execute(self, cmd_params, digilent_adept):
        # Kludge!
        full_cmd_line = digilent_adept + cmd_params.split(" ")

        log.log(DigilentAdept,log.level.Warning,"Executing %s" % full_cmd_line)

        try:
            popen = subprocess.Popen(
                full_cmd_line,
                stdin  = subprocess.PIPE,
                stdout = subprocess.PIPE,
                stderr = subprocess.PIPE
            )
        except Exception as e:
            raise ErrorProgrammingDeviceError(
                "There was an error while executing Digilent Adept: %s" % e
            )
        # TODO: make use of popen.poll to make this asynchronous
        try:
            stdout, stderr = popen.communicate('N\n')
            result = popen.wait()
        except Exception as e:
            raise ErrorWaitingForProgrammingFinishedError(
                "There was an error while waiting for Digilent Adept to finish: %s" % e
            )

        return result, stdout, stderr

    def _parse_configuration_to_program(self):
        try:
            program_file_content = self._cfg_manager.get_value('digilent_adept_batch_content')
            digilent_adept = self._cfg_manager.get_value('digilent_adept_full_path')
        except ConfigurationErrors.KeyNotFoundError as knfe:
            raise CantFindDigilentAdeptProperty(
                    "Can't find in configuration manager the property '%s'" % knfe.key
                )
        return program_file_content, digilent_adept

    def _create_batch_file(self, content, program_path):
        fd, file_name = tempfile.mkstemp(prefix='digilent_adept_batch_file_',suffix='.cmd')
        os.write(fd, content.replace('$FILE',program_path))
        os.close(fd)
        return file_name

    def _log(self, result_code, output, stderr):
        log.log(DigilentAdept,log.level.Info,"Device programming was finished. Result code: %i\n<output>\n%s\n</output><stderr>\n%s\n</stderr>" % (
                result_code,
                output,
                stderr
            )
        )

class CantFindDigilentAdeptProperty(DeviceErrors.MisconfiguredDeviceError):
    def __init__(self, *args, **kargs):
        DeviceErrors.MisconfiguredDeviceError.__init__(self, *args, **kargs)

class AlreadyProgrammingDeviceError(DeviceErrors.AlreadyProgrammingDeviceError):
    def __init__(self, *args, **kargs):
        DeviceErrors.AlreadyProgrammingDeviceError.__init__(self, *args, **kargs)

class ErrorProgrammingDeviceError(DeviceErrors.ProgrammingDeviceError):
    def __init__(self,*args,**kargs):
        DeviceErrors.ProgrammingDeviceError.__init__(self,*args,**kargs)

class ErrorRetrievingOutputFromProgrammingProgramError(DeviceErrors.ProgrammingDeviceError):
    def __init__(self,*args,**kargs):
        DeviceErrors.ProgrammingDeviceError.__init__(self,*args,**kargs)

class ErrorWaitingForProgrammingFinishedError(DeviceErrors.ProgrammingDeviceError):
    def __init__(self,*args,**kargs):
        DeviceErrors.ProgrammingDeviceError.__init__(self,*args,**kargs)

class ProgrammingGotErrors(DeviceErrors.ProgrammingDeviceError):
    def __init__(self,*args,**kargs):
        DeviceErrors.ProgrammingDeviceError.__init__(self,*args,**kargs)
<|endoftext|>


<commit_before># Copyright (c) 2011 gocept gmbh & co. kg
# See also LICENSE.txt

import zeit.cms.browser.view
import zeit.edit.interfaces
import zope.security.proxy

class History(zeit.cms.browser.view.JSON):

    resource_library = 'zeit.edit.styles'
    template = 'history.jsont'

    def json(self):
        history = zeit.edit.interfaces.IUndo(
            zope.security.proxy.getObject(self.context)).history
        for entry in history:
            if entry['description'] is None:
                entry['description'] = '[none]'
            entry['tid'] = entry['tid'].encode('base64')

        return dict(
            context_url=self.url(self.context),
            history=history,
            )

class Revert(zeit.edit.browser.view.Action):

    tid = zeit.edit.browser.view.Form('tid')

    def update(self):
        zeit.edit.interfaces.IUndo(
            zope.security.proxy.getObject(self.context)).revert(
            self.tid.decode('base64'))
        self.signal(None, 'reload')
<commit_msg>Re #8787: Include text of transaction we're reverting in our undo description<commit_after># Copyright (c) 2011 gocept gmbh & co. kg
# See also LICENSE.txt

from zeit.cms.i18n import MessageFactory as _
import zeit.cms.browser.view
import zeit.edit.interfaces
import zope.security.proxy

class History(zeit.cms.browser.view.JSON):

    resource_library = 'zeit.edit.styles'
    template = 'history.jsont'

    def json(self):
        history = zeit.edit.interfaces.IUndo(
            zope.security.proxy.getObject(self.context)).history
        for entry in history:
            if entry['description'] is None:
                entry['description'] = '[none]'
            entry['tid'] = entry['tid'].encode('base64')

        return dict(
            context_url=self.url(self.context),
            history=history,
            )

class Revert(zeit.edit.browser.view.Action):

    tid = zeit.edit.browser.view.Form('tid')

    def update(self):
        tid = self.tid.decode('base64')
        undo = zeit.edit.interfaces.IUndo(
            zope.security.proxy.getObject(self.context))

        for entry in undo.history:
            if entry['tid'] == tid:
                self.undo_description = _(
                    'revert up to "${action}"', mapping=dict(
                        action=entry['description']))
                break

        undo.revert(tid)
        self.signal(None, 'reload')
<|endoftext|>