<commit_before># -*- coding: utf-8 -*-

# ***************************************************************************
# *                                                                         *
# *   Copyright (c) 2016 sliptonic <shopinthewoods@gmail.com>               *
# *                                                                         *
# *   This program is free software; you can redistribute it and/or modify  *
# *   it under the terms of the GNU Lesser General Public License (LGPL)    *
# *   as published by the Free Software Foundation; either version 2 of     *
# *   the License, or (at your option) any later version.                   *
# *   for detail see the LICENCE text file.                                 *
# *                                                                         *
# *   This program 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 Library General Public License for more details.                  *
# *                                                                         *
# *   You should have received a copy of the GNU Library General Public     *
# *   License along with this program; if not, write to the Free Software   *
# *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  *
# *   USA                                                                   *
# *                                                                         *
# ***************************************************************************

import FreeCAD
import PathScripts
import PathScripts.PathLog as PathLog
import traceback

from PathScripts.PathUtils import loopdetect
from PathScripts.PathUtils import horizontalEdgeLoop
from PathScripts.PathUtils import horizontalFaceLoop
from PathScripts.PathUtils import addToJob
from PathScripts.PathUtils import findParentJob

if FreeCAD.GuiUp:
    import FreeCADGui
    from PySide import QtCore
    from DraftTools import translate
else:
    def translate(ctxt, txt):
        return txt

__title__="FreeCAD Path Commands"
__author__ = "sliptonic"
__url__ = "http://www.freecadweb.org"

class _CommandSelectLoop:
    "the Path command to complete loop selection definition"
    def __init__(self):
        self.obj = None
        self.sub = []
        self.active = False

    def GetResources(self):
        return {'Pixmap': 'Path-SelectLoop',
                'MenuText': QtCore.QT_TRANSLATE_NOOP("Path_SelectLoop", "Finish Selecting Loop"),
                'ToolTip': QtCore.QT_TRANSLATE_NOOP("Path_SelectLoop", "Complete loop selection from two edges"),
                'CmdType': "ForEdit"}

    def IsActive(self):
        if bool(FreeCADGui.Selection.getSelection()) is False:
            return False
        try:
            sel = FreeCADGui.Selection.getSelectionEx()[0]
            if sel.Object == self.obj and sel.SubElementNames == self.sub:
                return self.active
            self.obj = sel.Object
            self.sub = sel.SubElementNames
            if sel.SubObjects:
                self.active = self.formsPartOfALoop(sel.Object, sel.SubObjects[0], sel.SubElementNames)
            else:
                self.active = False
            return self.active
        except Exception as exc:
            PathLog.error(exc)
            traceback.print_exc(exc)
            return False

    def Activated(self):
        sel = FreeCADGui.Selection.getSelectionEx()[0]
        obj = sel.Object
        edge1 = sel.SubObjects[0]
        if 'Face' in sel.SubElementNames[0]:
            loop = horizontalFaceLoop(sel.Object, sel.SubObjects[0], sel.SubElementNames)
            if loop:
                FreeCADGui.Selection.clearSelection()
                FreeCADGui.Selection.addSelection(sel.Object, loop)
            loopwire = []
        elif len(sel.SubObjects) == 1:
            loopwire = horizontalEdgeLoop(obj, edge1)
        else:
            edge2 = sel.SubObjects[1]
            loopwire = loopdetect(obj, edge1, edge2)

        if loopwire:
            FreeCADGui.Selection.clearSelection()
            elist = obj.Shape.Edges
            for e in elist:
                for i in loopwire.Edges:
                    if e.hashCode() == i.hashCode():
                        FreeCADGui.Selection.addSelection(obj, "Edge"+str(elist.index(e)+1))

    def formsPartOfALoop(self, obj, sub, names):
        if names[0][0:4] != 'Edge':
            if names[0][0:4] == 'Face' and horizontalFaceLoop(obj, sub, names):
                return True
            return False
        if len(sel.SubElementNames) == 1 and horizontalEdgeLoop(obj, sub):
            return True
        if names[1][0:4] != 'Edge':
            return False
        return True

if FreeCAD.GuiUp:
    FreeCADGui.addCommand('Path_SelectLoop', _CommandSelectLoop())

class _CopyOperation:
    "the Path Copy Operation command definition"
    def GetResources(self):
        return {'Pixmap': 'Path-OpCopy',
                'MenuText': QtCore.QT_TRANSLATE_NOOP("Path_OperationCopy", "Copy the operation in the job"),
                'ToolTip': QtCore.QT_TRANSLATE_NOOP("Path_OperationCopy", "Copy the operation in the job"),
                'CmdType': "ForEdit"}

    def IsActive(self):
        if bool(FreeCADGui.Selection.getSelection()) is False:
            return False
        try:
            obj = FreeCADGui.Selection.getSelectionEx()[0].Object
            return isinstance(obj.Proxy, PathScripts.PathOp.ObjectOp)
        except:
            return False

    def Activated(self):
        obj = FreeCADGui.Selection.getSelectionEx()[0].Object
        jobname = findParentJob(obj).Name
        addToJob(FreeCAD.ActiveDocument.copyObject(obj, False), jobname)

if FreeCAD.GuiUp:
    FreeCADGui.addCommand('Path_OperationCopy', _CopyOperation())

# \c findShape() is referenced from Gui/Command.cpp and used by Path.Area commands.
# Do not remove!
def findShape(shape, subname=None, subtype=None):
    '''To find a higher oder shape containing the subshape with subname.
        E.g. to find the wire containing 'Edge1' in shape,
            findShape(shape,'Edge1','Wires')
    '''
    if not subname:
        return shape
    ret = shape.getElement(subname)
    if not subtype or not ret or ret.isNull():
        return ret
    if subname.startswith('Face'):
        tp = 'Faces'
    elif subname.startswith('Edge'):
        tp = 'Edges'
    elif subname.startswith('Vertex'):
        tp = 'Vertex'
    else:
        return ret
    for obj in getattr(shape, subtype):
        for sobj in getattr(obj, tp):
            if sobj.isEqual(ret):
                return obj
    return ret
<commit_msg>Fixed refactoring errors.<commit_after># -*- coding: utf-8 -*-

# ***************************************************************************
# *                                                                         *
# *   Copyright (c) 2016 sliptonic <shopinthewoods@gmail.com>               *
# *                                                                         *
# *   This program is free software; you can redistribute it and/or modify  *
# *   it under the terms of the GNU Lesser General Public License (LGPL)    *
# *   as published by the Free Software Foundation; either version 2 of     *
# *   the License, or (at your option) any later version.                   *
# *   for detail see the LICENCE text file.                                 *
# *                                                                         *
# *   This program 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 Library General Public License for more details.                  *
# *                                                                         *
# *   You should have received a copy of the GNU Library General Public     *
# *   License along with this program; if not, write to the Free Software   *
# *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  *
# *   USA                                                                   *
# *                                                                         *
# ***************************************************************************

import FreeCAD
import PathScripts
import PathScripts.PathLog as PathLog
import traceback

from PathScripts.PathUtils import loopdetect
from PathScripts.PathUtils import horizontalEdgeLoop
from PathScripts.PathUtils import horizontalFaceLoop
from PathScripts.PathUtils import addToJob
from PathScripts.PathUtils import findParentJob

if FreeCAD.GuiUp:
    import FreeCADGui
    from PySide import QtCore
    from DraftTools import translate
else:
    def translate(ctxt, txt):
        return txt

__title__="FreeCAD Path Commands"
__author__ = "sliptonic"
__url__ = "http://www.freecadweb.org"

class _CommandSelectLoop:
    "the Path command to complete loop selection definition"
    def __init__(self):
        self.obj = None
        self.sub = []
        self.active = False

    def GetResources(self):
        return {'Pixmap': 'Path-SelectLoop',
                'MenuText': QtCore.QT_TRANSLATE_NOOP("Path_SelectLoop", "Finish Selecting Loop"),
                'ToolTip': QtCore.QT_TRANSLATE_NOOP("Path_SelectLoop", "Complete loop selection from two edges"),
                'CmdType': "ForEdit"}

    def IsActive(self):
        if bool(FreeCADGui.Selection.getSelection()) is False:
            return False
        try:
            sel = FreeCADGui.Selection.getSelectionEx()[0]
            if sel.Object == self.obj and sel.SubElementNames == self.sub:
                return self.active
            self.obj = sel.Object
            self.sub = sel.SubElementNames
            if sel.SubObjects:
                self.active = self.formsPartOfALoop(sel.Object, sel.SubObjects[0], sel.SubElementNames)
            else:
                self.active = False
            return self.active
        except Exception as exc:
            PathLog.error(exc)
            traceback.print_exc(exc)
            return False

    def Activated(self):
        sel = FreeCADGui.Selection.getSelectionEx()[0]
        obj = sel.Object
        edge1 = sel.SubObjects[0]
        if 'Face' in sel.SubElementNames[0]:
            loop = horizontalFaceLoop(sel.Object, sel.SubObjects[0], sel.SubElementNames)
            if loop:
                FreeCADGui.Selection.clearSelection()
                FreeCADGui.Selection.addSelection(sel.Object, loop)
            loopwire = []
        elif len(sel.SubObjects) == 1:
            loopwire = horizontalEdgeLoop(obj, edge1)
        else:
            edge2 = sel.SubObjects[1]
            loopwire = loopdetect(obj, edge1, edge2)

        if loopwire:
            FreeCADGui.Selection.clearSelection()
            elist = obj.Shape.Edges
            for e in elist:
                for i in loopwire.Edges:
                    if e.hashCode() == i.hashCode():
                        FreeCADGui.Selection.addSelection(obj, "Edge"+str(elist.index(e)+1))

    def formsPartOfALoop(self, obj, sub, names):
        if names[0][0:4] != 'Edge':
            if names[0][0:4] == 'Face' and horizontalFaceLoop(obj, sub, names):
                return True
            return False
        if len(names) == 1 and horizontalEdgeLoop(obj, sub):
            return True
        if len(names) == 1 or names[1][0:4] != 'Edge':
            return False
        return True

if FreeCAD.GuiUp:
    FreeCADGui.addCommand('Path_SelectLoop', _CommandSelectLoop())

class _CopyOperation:
    "the Path Copy Operation command definition"
    def GetResources(self):
        return {'Pixmap': 'Path-OpCopy',
                'MenuText': QtCore.QT_TRANSLATE_NOOP("Path_OperationCopy", "Copy the operation in the job"),
                'ToolTip': QtCore.QT_TRANSLATE_NOOP("Path_OperationCopy", "Copy the operation in the job"),
                'CmdType': "ForEdit"}

    def IsActive(self):
        if bool(FreeCADGui.Selection.getSelection()) is False:
            return False
        try:
            obj = FreeCADGui.Selection.getSelectionEx()[0].Object
            return isinstance(obj.Proxy, PathScripts.PathOp.ObjectOp)
        except:
            return False

    def Activated(self):
        obj = FreeCADGui.Selection.getSelectionEx()[0].Object
        jobname = findParentJob(obj).Name
        addToJob(FreeCAD.ActiveDocument.copyObject(obj, False), jobname)

if FreeCAD.GuiUp:
    FreeCADGui.addCommand('Path_OperationCopy', _CopyOperation())

# \c findShape() is referenced from Gui/Command.cpp and used by Path.Area commands.
# Do not remove!
def findShape(shape, subname=None, subtype=None):
    '''To find a higher oder shape containing the subshape with subname.
        E.g. to find the wire containing 'Edge1' in shape,
            findShape(shape,'Edge1','Wires')
    '''
    if not subname:
        return shape
    ret = shape.getElement(subname)
    if not subtype or not ret or ret.isNull():
        return ret
    if subname.startswith('Face'):
        tp = 'Faces'
    elif subname.startswith('Edge'):
        tp = 'Edges'
    elif subname.startswith('Vertex'):
        tp = 'Vertex'
    else:
        return ret
    for obj in getattr(shape, subtype):
        for sobj in getattr(obj, tp):
            if sobj.isEqual(ret):
                return obj
    return ret
<|endoftext|>


<commit_before>
<commit_msg>Update auth.py<commit_after>
import requests

def get_token_from_client_credentials():

    client_id = ctx.node.properties['client_id']
    client_secret = ctx.node.properties['password']
    tenant_id = ctx.node.properties['tenant_id']
    endpoints = 'https://login.microsoftonline.com/'+tenant_id+'/oauth2/token'
    payload = {
        'grant_type': 'client_credentials',
        'client_id': client_id,
        'client_secret': client_secret,
        'resource': constants.resource,
    }
    response = requests.post(endpoints, data=payload).json()
    token=response['access_token']
    print(token)
    return token
<|endoftext|>


<commit_before>__version__ = '0.11.1'
<commit_msg>Bumped minor version<commit_after>__version__ = '0.11.2'
<|endoftext|>


<commit_before>#-- Content-Encoding: UTF-8 --
"""
Created on 29 févr. 2012

@author: Thomas Calmant
"""

# ------------------------------------------------------------------------------

import logging
_logger = logging.getLogger(__name__)

# ------------------------------------------------------------------------------

from psem2m.component.decorators import ComponentFactory, Provides, Requires, \
    Validate, Invalidate, Instantiate
import psem2m.component.constants

# ------------------------------------------------------------------------------

SIGNAL_REQUEST_PATTERN = "/psem2m-composer-agent/request/*"
SIGNAL_CAN_HANDLE_COMPONENTS = "/psem2m-composer-agent/request/can-handle-components"
SIGNAL_INSTANTIATE_COMPONENTS = "/psem2m-composer-agent/request/instantiate-components"
SIGNAL_STOP_COMPONENTS = "/psem2m-composer-agent/request/stop-components"

SIGNAL_RESPONSE_HANDLES_COMPONENTS = "/psem2m-composer-agent/response/can-handle-components"
SIGNAL_RESPONSE_INSTANTIATE_COMPONENTS = "/psem2m-composer-agent/response/instantiate-components"
SIGNAL_RESPONSE_STOP_COMPONENTS = "/psem2m-composer-agent/response/stop-components"

# ------------------------------------------------------------------------------

@ComponentFactory("ComposerAgentFactory")
@Instantiate("ComposerAgent")
@Provides("org.psem2m.composer.Agent")
@Requires("ipopo", psem2m.component.constants.IPOPO_SERVICE_SPECIFICATION)
@Requires("directory", "org.psem2m.IsolateDirectory")
@Requires("sender", "org.psem2m.SignalSender")
@Requires("receiver", "org.psem2m.SignalReceiver")
class ComposerAgent(object):
    """
    Python Composer agent
    """
    COMPOSITE_NAME = "org.psem2m.composer.composite.name"
    HOST_ISOLATE = "org.psem2m.composer.isolate"

    def __init__(self):
        """
        Constructor
        """
        self.sender = None
        self.receiver = None
        self.directory = None
        self.ipopo = None
        self.instances = {}

    def handle_received_signal(self, name, signal_data):
        """
        Called when a composer signal is received
        """
        _logger.info("Composer Signal: %s", name)

        sender = signal_data["isolateSender"]
        data = signal_data["signalContent"]

        if name == SIGNAL_CAN_HANDLE_COMPONENTS:
            self.can_handle_components(sender, data)

        elif name == SIGNAL_INSTANTIATE_COMPONENTS:
            self.instantiate_components(sender, data)

        elif name == SIGNAL_STOP_COMPONENTS:
            self.kill_components(sender, data)

    def can_handle_components(self, sender, data):
        """
        Sends a signal to the given isolate to indicate which components can be
        instantiated here.

        @param data: An array of ComponentBean objects
        """
        current_isolate = self.directory.get_current_isolate_id()
        handled = []

        # Find all instantiable components
        for component in data:
            name = component["name"]

            host = component.get("isolate", None)
            if host and host != current_isolate:
                continue

            factory = component["type"]
            if self.ipopo.is_registered_factory(factory):
                _logger.debug("%s can be handled here", name)
                handled.append(component)

        # Send the result
        if not handled:
            handled = None

        self.sender.send_data(sender, SIGNAL_RESPONSE_HANDLES_COMPONENTS,
                              handled)

    def instantiate_components(self, sender, data):
        """
        Instantiates the given components
        """
        current_isolate = self.directory.get_current_isolate_id()

        success = []
        failure = []
        composite_name = None

        for component in data:

            if not composite_name:
                # Root name
                composite_name = component.get("rootName", None)

            # Get the name
            name = component["name"]

            # Get the type
            factory = component["type"]

            # Get the properties
            try:
                properties = component["properties"]["map"]
            except:
                properties = {}

            if not isinstance(properties, dict):
                properties = {}

            # Set up properties
            properties[ComposerAgent.COMPOSITE_NAME] = component["parentName"]
            properties[ComposerAgent.HOST_ISOLATE] = current_isolate
            properties["service.exported.interfaces"] = "*"

            try:
                instance = self.ipopo.instantiate(factory, name, properties)

                self.instances[name] = instance
                success.append(name)

            except:
                _logger.exception("EPIC FAIL !")
                failure.append(name)

        result_map = {
            "javaClass": "java.util.HashMap",
            "map": {
                    "instantiated": success,
                    "failed": failure
                }
            }

        self.sender.send_data(sender, SIGNAL_RESPONSE_INSTANTIATE_COMPONENTS,
                              result_map)

    def kill_components(self, sender, data):
        """
        Kills given components
        """
        killed = []
        unknown = []

        for component in data:
            name = component["name"]

            if self.ipopo.is_registered_instance(name):
                self.ipopo.kill(name)
                killed.append(name)

            else:
                unknown.append(name)

        result_map = {
            "javaClass": "java.util.HashMap",
            "map": {
                    "stopped": killed,
                    "unknown": unknown
                }
            }

        self.sender.send_data(sender, SIGNAL_RESPONSE_STOP_COMPONENTS,
                              result_map)

    @Validate
    def validate(self, context):
        """
        Component validated
        """
        self.instances.clear()
        self.receiver.register_listener(SIGNAL_REQUEST_PATTERN, self)

    @Invalidate
    def invalidate(self, context):
        """
        Component invalidated
        """
        self.receiver.unregister_listener(SIGNAL_REQUEST_PATTERN, self)

        # Kill active components
        if len(self.instances) > 0:
            data = []
            for compo_name in self.instances:
                data.append({"name": compo_name})

            self.kill_components("monitors", data)

        self.instances.clear()
<commit_msg>Gestion de l'apparition de Factories iPOPO<commit_after>#-- Content-Encoding: UTF-8 --
"""
Created on 29 févr. 2012

@author: Thomas Calmant
"""

# ------------------------------------------------------------------------------

import logging
from psem2m.component.ipopo import IPopoEvent
_logger = logging.getLogger(__name__)

# ------------------------------------------------------------------------------

from psem2m.component.decorators import ComponentFactory, Provides, Requires, \
    Validate, Invalidate, Instantiate
import psem2m.component.constants

# ------------------------------------------------------------------------------

SIGNAL_PREFIX = "/psem2m-composer-agent"
SIGNAL_REQUEST_PREFIX = "%s/request" % SIGNAL_PREFIX
SIGNAL_RESPONSE_PREFIX = "%s/response" % SIGNAL_PREFIX
SIGNAL_FACTORY_PREFIX = "%s/factory-state" % SIGNAL_PREFIX

SIGNAL_REQUEST_PATTERN = "%s/*" % SIGNAL_REQUEST_PREFIX
SIGNAL_CAN_HANDLE_COMPONENTS = "%s/can-handle-components" \
                               % SIGNAL_REQUEST_PREFIX
SIGNAL_INSTANTIATE_COMPONENTS = "%s/instantiate-components" \
                                % SIGNAL_REQUEST_PREFIX
SIGNAL_STOP_COMPONENTS = "%s/stop-components" % SIGNAL_REQUEST_PREFIX

SIGNAL_RESPONSE_HANDLES_COMPONENTS = "%s/can-handle-components" \
                                     % SIGNAL_RESPONSE_PREFIX
SIGNAL_RESPONSE_INSTANTIATE_COMPONENTS = "%s/instantiate-components" \
                                         % SIGNAL_RESPONSE_PREFIX
SIGNAL_RESPONSE_STOP_COMPONENTS = "%s/stop-components" % SIGNAL_RESPONSE_PREFIX

SIGNAL_ISOLATE_ADD_FACTORY = "%s/added" % SIGNAL_FACTORY_PREFIX
SIGNAL_ISOLATE_REMOVE_FACTORY = "%s/removed" % SIGNAL_FACTORY_PREFIX
SIGNAL_ISOLATE_FACTORIES_GONE = "%s/all-gone" % SIGNAL_FACTORY_PREFIX

# ------------------------------------------------------------------------------

@ComponentFactory("ComposerAgentFactory")
@Instantiate("ComposerAgent")
@Provides("org.psem2m.composer.Agent")
@Requires("ipopo", psem2m.component.constants.IPOPO_SERVICE_SPECIFICATION)
@Requires("directory", "org.psem2m.IsolateDirectory")
@Requires("sender", "org.psem2m.SignalSender")
@Requires("receiver", "org.psem2m.SignalReceiver")
class ComposerAgent(object):
    """
    Python Composer agent
    """
    COMPOSITE_NAME = "org.psem2m.composer.composite.name"
    HOST_ISOLATE = "org.psem2m.composer.isolate"

    def __init__(self):
        """
        Constructor
        """
        self.sender = None
        self.receiver = None
        self.directory = None
        self.ipopo = None
        self.instances = {}

    def handle_received_signal(self, name, signal_data):
        """
        Called when a composer signal is received

        :param name: The signal name
        :param data: The signal data object
        """
        _logger.info("Composer Signal: %s", name)

        sender = signal_data["isolateSender"]
        data = signal_data["signalContent"]

        if name == SIGNAL_CAN_HANDLE_COMPONENTS:
            self.can_handle_components(sender, data)

        elif name == SIGNAL_INSTANTIATE_COMPONENTS:
            self.instantiate_components(sender, data)

        elif name == SIGNAL_STOP_COMPONENTS:
            self.kill_components(sender, data)

    def can_handle_components(self, sender, data):
        """
        Sends a signal to the given isolate to indicate which components can be
        instantiated here.

        :param sender: The isolate that sent the request signal
        :param data: An array of ComponentBean objects
        """
        current_isolate = self.directory.get_current_isolate_id()
        handled = []

        # Find all instantiable components
        for component in data:
            name = component["name"]

            host = component.get("isolate", None)
            if host and host != current_isolate:
                continue

            factory = component["type"]
            if self.ipopo.is_registered_factory(factory):
                _logger.debug("%s can be handled here (%s)", name, factory)
                handled.append(component)

        # Send the result
        if not handled:
            handled = None

        self.sender.send_data(sender, SIGNAL_RESPONSE_HANDLES_COMPONENTS,
                              handled)

    def instantiate_components(self, sender, data):
        """
        Instantiates the given components

        :param sender: The isolate requesting the instantiation
        :param data: The signal content
        """
        current_isolate = self.directory.get_current_isolate_id()

        success = []
        failure = []
        composite_name = None

        for component in data:

            if not composite_name:
                # Root name
                composite_name = component.get("rootName", None)

            # Get the name
            name = component["name"]

            # Get the type
            factory = component["type"]

            # Get the properties
            try:
                properties = component["properties"]["map"]
            except:
                properties = {}

            if not isinstance(properties, dict):
                properties = {}

            # Set up properties
            properties[ComposerAgent.COMPOSITE_NAME] = component["parentName"]
            properties[ComposerAgent.HOST_ISOLATE] = current_isolate
            properties["service.exported.interfaces"] = "*"

            try:
                instance = self.ipopo.instantiate(factory, name, properties)

                self.instances[name] = instance
                success.append(name)

            except:
                _logger.exception("EPIC FAIL !")
                failure.append(name)

        result_map = {"instantiated": success, "failed": failure}
        self.sender.send_data(sender, SIGNAL_RESPONSE_INSTANTIATE_COMPONENTS,
                              result_map)

    def kill_components(self, sender, data):
        """
        Kills given components

        :param sender: The isolate requesting the destruction of a component
        :param data: The signal content
        """
        killed = []
        unknown = []

        for component in data:
            name = component["name"]

            if self.ipopo.is_registered_instance(name):
                self.ipopo.kill(name)
                killed.append(name)

            else:
                unknown.append(name)

        result_map = {"stopped": killed, "unknown": unknown}
        self.sender.send_data(sender, SIGNAL_RESPONSE_STOP_COMPONENTS,
                              result_map)

    def handle_ipopo_event(self, event):
        """
        Handles an iPOPO event

        :param event: An iPOPO event
        """
        kind = event.get_kind()
        factory = event.get_factory_name()

        if kind == IPopoEvent.REGISTERED:
            # Factory registered
            self.sender.send_data("*", SIGNAL_ISOLATE_ADD_FACTORY, (factory,))

        elif kind == IPopoEvent.UNREGISTERED:
            # Factory gone
            self.sender.send_data("*", SIGNAL_ISOLATE_REMOVE_FACTORY,
                                  (factory,))

    @Validate
    def validate(self, context):
        """
        Component validated

        :param context: The bundle context
        """
        self.instances.clear()
        self.receiver.register_listener(SIGNAL_REQUEST_PATTERN, self)
        self.ipopo.add_listener(self)

    @Invalidate
    def invalidate(self, context):
        """
        Component invalidated

        :param context: The bundle context
        """
        self.ipopo.remove_listener(self)
        self.receiver.unregister_listener(SIGNAL_REQUEST_PATTERN, self)

        # Send a signal to tell others that all our factories are gone
        self.sender.send_data("*", SIGNAL_ISOLATE_FACTORIES_GONE, None)

        # Kill active components
        if len(self.instances) > 0:
            data = []
            for compo_name in self.instances:
                data.append({"name": compo_name})

            self.kill_components("monitors", data)

        self.instances.clear()
<|endoftext|>


<commit_before># regression <master> <dataFile> <modelFile> <outputDir> <mode> <opts>

import sys
import os
from numpy import *
from scipy.linalg import *
from pyspark import SparkContext
from thunder.util.dataio import *
from thunder.regression.util import *
from thunder.factorization.util import *

argsIn = sys.argv[1:]
if len(argsIn) < 5:
  print >> sys.stderr, \
  "(regression) usage: regression <master> <dataFile> <modelFile> <outputDir> <mode>"
  exit(-1)

# parse inputs
sc = SparkContext(argsIn[0], "regression")
dataFile = str(argsIn[1])
modelFile = str(argsIn[2])
outputDir = str(argsIn[3]) + "-regression"
mode = str(argsIn[4])
if not os.path.exists(outputDir) : os.makedirs(outputDir)

# parse data
lines = sc.textFile(dataFile)
data = parse(lines, "dff").cache()

# create model
model = regressionModel(modelFile,mode)

# do regression
betas = regressionFit(data,model).cache()

# get statistics
stats = betas.map(lambda x : x[2])
saveout(stats,outputDir,"stats","matlab")

# get difference weights
diff = betas.map(lambda x : x[1])
saveout(diff,outputDir,"diff","matlab")

# do PCA
comps,latent,scores = svd1(betas.map(lambda x : x[0]),3)

# write results
saveout(comps,outputDir,"comps","matlab")
saveout(latent,outputDir,"latent","matlab")
#saveout(scores,outputDir,"scores","matlab")

# compute trajectories from raw data
traj = regressionFit(data,model,comps)
saveout(traj,outputDir,"traj","matlab")

# get simple measure of response strength
#r = data.map(lambda x : norm(x-mean(x)))
#saveout(r,outputDir,"r","matlab")

# # get statistics using randomization
# if outputMode == 'stats'
# 	stats = Y.map(lambda y : getRegression(y,model)).collect()
# 	savemat(outputFile+"/"+"stats.mat",mdict={'stats':stats},oned_as='column',do_compression='true')

# # get norms of coefficients to make a contrast map
# if outputMode == 'norm' :
# 	B = Y.map(lambda y : (y,getNorm(y,model)))
# 	n = B.count()
# 	m = len(Y.first())
# 	traj = zeros((2,m))
# 	for ic in range(0,2) :
# 		traj[ic,:] = B.filter(lambda (y,b) : (b[ic] - b[1-ic])>0.01).map(lambda (y,b) : y * b[ic]).reduce(lambda x,y : x + y) / n
# 	norms = B.map(lambda (y,b) : float16(b)).collect()
# 	savemat(outputFile+"/"+"traj.mat",mdict={'traj':traj},oned_as='column',do_compression='true')
# 	savemat(outputFile+"/"+"norms.mat",mdict={'norms':norms},oned_as='column',do_compression='true')

# def getNorm(y,model) :
# 	b = getRegression(y,model)
# 	n = zeros((model.nG,))
# 	for ig in range(0,model.nG) :
# 		n[ig] = norm(b[model.g==ig])
# 	return n

<commit_msg>Minor changes<commit_after># regression <master> <dataFile> <modelFile> <outputDir> <mode> <opts>

import sys
import os
from numpy import *
from scipy.linalg import *
from pyspark import SparkContext
from thunder.util.dataio import *
from thunder.regression.util import *
from thunder.factorization.util import *

argsIn = sys.argv[1:]
if len(argsIn) < 5:
  print >> sys.stderr, \
  "(regression) usage: regression <master> <dataFile> <modelFile> <outputDir> <mode>"
  exit(-1)

# parse inputs
sc = SparkContext(argsIn[0], "regression")
dataFile = str(argsIn[1])
modelFile = str(argsIn[2])
outputDir = str(argsIn[3]) + "-regression"
mode = str(argsIn[4])
if not os.path.exists(outputDir) : os.makedirs(outputDir)

# parse data
lines = sc.textFile(dataFile)
data = parse(lines, "dff").cache()

# create model
model = regressionModel(modelFile,mode)

# do regression
betas = regressionFit(data,model).cache()

# get statistics
stats = betas.map(lambda x : x[2])
saveout(stats,outputDir,"stats","matlab")

# get difference weights
diff = betas.map(lambda x : x[1])
saveout(diff,outputDir,"diff","matlab")

# do PCA
comps,latent,scores = svd1(betas.map(lambda x : x[0]),3)

# write results
saveout(comps,outputDir,"comps","matlab")
saveout(latent,outputDir,"latent","matlab")
saveout(scores,outputDir,"scores","matlab")

# compute trajectories from raw data
traj = regressionFit(data,model,comps)
saveout(traj,outputDir,"traj","matlab")

# get simple measure of response strength
#r = data.map(lambda x : norm(x-mean(x)))
#saveout(r,outputDir,"r","matlab")

# # get statistics using randomization
# if outputMode == 'stats'
# 	stats = Y.map(lambda y : getRegression(y,model)).collect()
# 	savemat(outputFile+"/"+"stats.mat",mdict={'stats':stats},oned_as='column',do_compression='true')

# # get norms of coefficients to make a contrast map
# if outputMode == 'norm' :
# 	B = Y.map(lambda y : (y,getNorm(y,model)))
# 	n = B.count()
# 	m = len(Y.first())
# 	traj = zeros((2,m))
# 	for ic in range(0,2) :
# 		traj[ic,:] = B.filter(lambda (y,b) : (b[ic] - b[1-ic])>0.01).map(lambda (y,b) : y * b[ic]).reduce(lambda x,y : x + y) / n
# 	norms = B.map(lambda (y,b) : float16(b)).collect()
# 	savemat(outputFile+"/"+"traj.mat",mdict={'traj':traj},oned_as='column',do_compression='true')
# 	savemat(outputFile+"/"+"norms.mat",mdict={'norms':norms},oned_as='column',do_compression='true')

# def getNorm(y,model) :
# 	b = getRegression(y,model)
# 	n = zeros((model.nG,))
# 	for ig in range(0,model.nG) :
# 		n[ig] = norm(b[model.g==ig])
# 	return n

<|endoftext|>


<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-

# The path to enlightenment starts with the following:

import unittest

from koans.about_asserts import AboutAsserts
from koans.about_strings import AboutStrings
from koans.about_none import AboutNone
from koans.about_lists import AboutLists
from koans.about_list_assignments import AboutListAssignments
from koans.about_dictionaries import AboutDictionaries
from koans.about_string_manipulation import AboutStringManipulation
from koans.about_tuples import AboutTuples
from koans.about_methods import AboutMethods
from koans.about_control_statements import AboutControlStatements
from koans.about_true_and_false import AboutTrueAndFalse
from koans.about_sets import AboutSets
from koans.about_triangle_project import AboutTriangleProject
from koans.about_exceptions import AboutExceptions
from koans.about_triangle_project2 import AboutTriangleProject2
from koans.about_iteration import AboutIteration
from koans.about_comprehension import AboutComprehension
from koans.about_generators import AboutGenerators
from koans.about_lambdas import AboutLambdas
from koans.about_scoring_project import AboutScoringProject
from koans.about_classes import AboutClasses
from koans.about_new_style_classes import AboutNewStyleClasses
from koans.about_with_statements import AboutWithStatements
from koans.about_monkey_patching import AboutMonkeyPatching
from koans.about_dice_project import AboutDiceProject
from koans.about_method_bindings import AboutMethodBindings
from koans.about_decorating_with_functions import AboutDecoratingWithFunctions
from koans.about_decorating_with_classes import AboutDecoratingWithClasses
from koans.about_inheritance import AboutInheritance
from koans.about_multiple_inheritance import AboutMultipleInheritance
from koans.about_regex import AboutRegex
from koans.about_scope import AboutScope
from koans.about_modules import AboutModules
from koans.about_packages import AboutPackages
from koans.about_class_attributes import AboutClassAttributes
from koans.about_attribute_access import AboutAttributeAccess
from koans.about_deleting_objects import AboutDeletingObjects
from koans.about_proxy_object_project import *
from koans.about_extra_credit import AboutExtraCredit

def koans():
    loader = unittest.TestLoader()
    suite = unittest.TestSuite()
    loader.sortTestMethodsUsing = None
    suite.addTests(loader.loadTestsFromTestCase(AboutAsserts))
    suite.addTests(loader.loadTestsFromTestCase(AboutStrings))
    suite.addTests(loader.loadTestsFromTestCase(AboutNone))
    suite.addTests(loader.loadTestsFromTestCase(AboutLists))
    suite.addTests(loader.loadTestsFromTestCase(AboutListAssignments))
    suite.addTests(loader.loadTestsFromTestCase(AboutDictionaries))
    suite.addTests(loader.loadTestsFromTestCase(AboutStringManipulation))
    suite.addTests(loader.loadTestsFromTestCase(AboutTuples))
    suite.addTests(loader.loadTestsFromTestCase(AboutMethods))
    suite.addTests(loader.loadTestsFromTestCase(AboutControlStatements))
    suite.addTests(loader.loadTestsFromTestCase(AboutTrueAndFalse))
    suite.addTests(loader.loadTestsFromTestCase(AboutSets))
    suite.addTests(loader.loadTestsFromTestCase(AboutTriangleProject))
    suite.addTests(loader.loadTestsFromTestCase(AboutExceptions))
    suite.addTests(loader.loadTestsFromTestCase(AboutTriangleProject2))
    suite.addTests(loader.loadTestsFromTestCase(AboutIteration))
    suite.addTests(loader.loadTestsFromTestCase(AboutComprehension))
    suite.addTests(loader.loadTestsFromTestCase(AboutGenerators))
    suite.addTests(loader.loadTestsFromTestCase(AboutLambdas))
    suite.addTests(loader.loadTestsFromTestCase(AboutScoringProject))
    suite.addTests(loader.loadTestsFromTestCase(AboutClasses))
    suite.addTests(loader.loadTestsFromTestCase(AboutNewStyleClasses))
    suite.addTests(loader.loadTestsFromTestCase(AboutWithStatements))
    suite.addTests(loader.loadTestsFromTestCase(AboutMonkeyPatching))
    suite.addTests(loader.loadTestsFromTestCase(AboutDiceProject))
    suite.addTests(loader.loadTestsFromTestCase(AboutMethodBindings))
    suite.addTests(loader.loadTestsFromTestCase(AboutDecoratingWithFunctions))
    suite.addTests(loader.loadTestsFromTestCase(AboutDecoratingWithClasses))
    suite.addTests(loader.loadTestsFromTestCase(AboutInheritance))
    suite.addTests(loader.loadTestsFromTestCase(AboutMultipleInheritance))
    suite.addTests(loader.loadTestsFromTestCase(AboutScope))
    suite.addTests(loader.loadTestsFromTestCase(AboutModules))
    suite.addTests(loader.loadTestsFromTestCase(AboutPackages))
    suite.addTests(loader.loadTestsFromTestCase(AboutClassAttributes))
    suite.addTests(loader.loadTestsFromTestCase(AboutAttributeAccess))
    suite.addTests(loader.loadTestsFromTestCase(AboutDeletingObjects))
    suite.addTests(loader.loadTestsFromTestCase(AboutProxyObjectProject))
    suite.addTests(loader.loadTestsFromTestCase(TelevisionTest))
    suite.addTests(loader.loadTestsFromTestCase(AboutExtraCredit))

    return suite
<commit_msg>AboutRegex missing from collector<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-

# The path to enlightenment starts with the following:

import unittest

from koans.about_asserts import AboutAsserts
from koans.about_strings import AboutStrings
from koans.about_none import AboutNone
from koans.about_lists import AboutLists
from koans.about_list_assignments import AboutListAssignments
from koans.about_dictionaries import AboutDictionaries
from koans.about_string_manipulation import AboutStringManipulation
from koans.about_tuples import AboutTuples
from koans.about_methods import AboutMethods
from koans.about_control_statements import AboutControlStatements
from koans.about_true_and_false import AboutTrueAndFalse
from koans.about_sets import AboutSets
from koans.about_triangle_project import AboutTriangleProject
from koans.about_exceptions import AboutExceptions
from koans.about_triangle_project2 import AboutTriangleProject2
from koans.about_iteration import AboutIteration
from koans.about_comprehension import AboutComprehension
from koans.about_generators import AboutGenerators
from koans.about_lambdas import AboutLambdas
from koans.about_scoring_project import AboutScoringProject
from koans.about_classes import AboutClasses
from koans.about_new_style_classes import AboutNewStyleClasses
from koans.about_with_statements import AboutWithStatements
from koans.about_monkey_patching import AboutMonkeyPatching
from koans.about_dice_project import AboutDiceProject
from koans.about_method_bindings import AboutMethodBindings
from koans.about_decorating_with_functions import AboutDecoratingWithFunctions
from koans.about_decorating_with_classes import AboutDecoratingWithClasses
from koans.about_inheritance import AboutInheritance
from koans.about_multiple_inheritance import AboutMultipleInheritance
from koans.about_regex import AboutRegex
from koans.about_scope import AboutScope
from koans.about_modules import AboutModules
from koans.about_packages import AboutPackages
from koans.about_class_attributes import AboutClassAttributes
from koans.about_attribute_access import AboutAttributeAccess
from koans.about_deleting_objects import AboutDeletingObjects
from koans.about_proxy_object_project import *
from koans.about_extra_credit import AboutExtraCredit

def koans():
    loader = unittest.TestLoader()
    suite = unittest.TestSuite()
    loader.sortTestMethodsUsing = None
    suite.addTests(loader.loadTestsFromTestCase(AboutAsserts))
    suite.addTests(loader.loadTestsFromTestCase(AboutStrings))
    suite.addTests(loader.loadTestsFromTestCase(AboutNone))
    suite.addTests(loader.loadTestsFromTestCase(AboutLists))
    suite.addTests(loader.loadTestsFromTestCase(AboutListAssignments))
    suite.addTests(loader.loadTestsFromTestCase(AboutDictionaries))
    suite.addTests(loader.loadTestsFromTestCase(AboutStringManipulation))
    suite.addTests(loader.loadTestsFromTestCase(AboutTuples))
    suite.addTests(loader.loadTestsFromTestCase(AboutMethods))
    suite.addTests(loader.loadTestsFromTestCase(AboutControlStatements))
    suite.addTests(loader.loadTestsFromTestCase(AboutTrueAndFalse))
    suite.addTests(loader.loadTestsFromTestCase(AboutSets))
    suite.addTests(loader.loadTestsFromTestCase(AboutTriangleProject))
    suite.addTests(loader.loadTestsFromTestCase(AboutExceptions))
    suite.addTests(loader.loadTestsFromTestCase(AboutTriangleProject2))
    suite.addTests(loader.loadTestsFromTestCase(AboutIteration))
    suite.addTests(loader.loadTestsFromTestCase(AboutComprehension))
    suite.addTests(loader.loadTestsFromTestCase(AboutGenerators))
    suite.addTests(loader.loadTestsFromTestCase(AboutLambdas))
    suite.addTests(loader.loadTestsFromTestCase(AboutScoringProject))
    suite.addTests(loader.loadTestsFromTestCase(AboutClasses))
    suite.addTests(loader.loadTestsFromTestCase(AboutNewStyleClasses))
    suite.addTests(loader.loadTestsFromTestCase(AboutWithStatements))
    suite.addTests(loader.loadTestsFromTestCase(AboutMonkeyPatching))
    suite.addTests(loader.loadTestsFromTestCase(AboutDiceProject))
    suite.addTests(loader.loadTestsFromTestCase(AboutMethodBindings))
    suite.addTests(loader.loadTestsFromTestCase(AboutDecoratingWithFunctions))
    suite.addTests(loader.loadTestsFromTestCase(AboutDecoratingWithClasses))
    suite.addTests(loader.loadTestsFromTestCase(AboutInheritance))
    suite.addTests(loader.loadTestsFromTestCase(AboutMultipleInheritance))
    suite.addTests(loader.loadTestsFromTestCase(AboutScope))
    suite.addTests(loader.loadTestsFromTestCase(AboutModules))
    suite.addTests(loader.loadTestsFromTestCase(AboutPackages))
    suite.addTests(loader.loadTestsFromTestCase(AboutClassAttributes))
    suite.addTests(loader.loadTestsFromTestCase(AboutAttributeAccess))
    suite.addTests(loader.loadTestsFromTestCase(AboutDeletingObjects))
    suite.addTests(loader.loadTestsFromTestCase(AboutProxyObjectProject))
    suite.addTests(loader.loadTestsFromTestCase(TelevisionTest))
    suite.addTests(loader.loadTestsFromTestCase(AboutExtraCredit))
    suite.addTests(loader.loadTestsFromTestCase(AboutRegex))

    return suite
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
source_suffix = '.rst'
master_doc = 'index'
project = 'Payment Suite'
copyright = u'2013-2014, Marc Morera'
version = ''
release = ''
html_theme = 'default'
<commit_msg>Update conf.py<commit_after># -*- coding: utf-8 -*-
source_suffix = '.rst'
master_doc = 'index'
project = 'GearmanBundle'
copyright = u'2013-2014, Marc Morera'
version = ''
release = ''
html_theme = 'default'
<|endoftext|>


<commit_before># coding:utf-8
import asyncio
import logging
import operator
from typing import Any, Callable, Type

from backoff._common import (
    _prepare_logger,
    _config_handlers,
    _log_backoff,
    _log_giveup
)
from backoff._jitter import full_jitter
from backoff import _async, _sync
from backoff._typing import (
    _Handler,
    _Jitterer,
    _MaybeCallable,
    _MaybeLogger,
    _MaybeSequence,
    _Predicate,
    _WaitGenerator,
)

def on_predicate(wait_gen: _WaitGenerator,
                 predicate: _Predicate[Any] = operator.not_,
                 *,
                 max_tries: _MaybeCallable[int] = None,
                 max_time: _MaybeCallable[float] = None,
                 jitter: _Jitterer = full_jitter,
                 on_success: _Handler = None,
                 on_backoff: _Handler = None,
                 on_giveup: _Handler = None,
                 logger: _MaybeLogger = 'backoff',
                 backoff_log_level: int = logging.INFO,
                 giveup_log_level: int = logging.ERROR,
                 **wait_gen_kwargs) -> Callable:
    """Returns decorator for backoff and retry triggered by predicate.

    Args:
        wait_gen: A generator yielding successive wait times in
            seconds.
        predicate: A function which when called on the return value of
            the target function will trigger backoff when considered
            truthily. If not specified, the default behavior is to
            backoff on falsey return values.
        max_tries: The maximum number of attempts to make before giving
            up. In the case of failure, the result of the last attempt
            will be returned. The default value of None means there
            is no limit to the number of tries. If a callable is passed,
            it will be evaluated at runtime and its return value used.
        max_time: The maximum total amount of time to try for before
            giving up. If this time expires, the result of the last
            attempt will be returned. If a callable is passed, it will
            be evaluated at runtime and its return value used.
        jitter: A function of the value yielded by wait_gen returning
            the actual time to wait. This distributes wait times
            stochastically in order to avoid timing collisions across
            concurrent clients. Wait times are jittered by default
            using the full_jitter function. Jittering may be disabled
            altogether by passing jitter=None.
        on_success: Callable (or iterable of callables) with a unary
            signature to be called in the event of success. The
            parameter is a dict containing details about the invocation.
        on_backoff: Callable (or iterable of callables) with a unary
            signature to be called in the event of a backoff. The
            parameter is a dict containing details about the invocation.
        on_giveup: Callable (or iterable of callables) with a unary
            signature to be called in the event that max_tries
            is exceeded.  The parameter is a dict containing details
            about the invocation.
        logger: Name of logger or Logger object to log to. Defaults to
            'backoff'.
        backoff_log_level: log level for the backoff event. Defaults to "INFO"
        giveup_log_level: log level for the give up event. Defaults to "ERROR"
        **wait_gen_kwargs: Any additional keyword args specified will be
            passed to wait_gen when it is initialized.  Any callable
            args will first be evaluated and their return values passed.
            This is useful for runtime configuration.
    """
    def decorate(target):
        nonlocal logger, on_success, on_backoff, on_giveup

        logger = _prepare_logger(logger)
        on_success = _config_handlers(on_success)
        on_backoff = _config_handlers(
            on_backoff,
            default_handler=_log_backoff,
            logger=logger,
            log_level=backoff_log_level
        )
        on_giveup = _config_handlers(
            on_giveup,
            default_handler=_log_giveup,
            logger=logger,
            log_level=giveup_log_level
        )

        if asyncio.iscoroutinefunction(target):
            retry = _async.retry_predicate
        else:
            retry = _sync.retry_predicate

        return retry(
            target,
            wait_gen,
            predicate,
            max_tries=max_tries,
            max_time=max_time,
            jitter=jitter,
            on_success=on_success,
            on_backoff=on_backoff,
            on_giveup=on_giveup,
            wait_gen_kwargs=wait_gen_kwargs
        )

    # Return a function which decorates a target with a retry loop.
    return decorate

def on_exception(wait_gen: _WaitGenerator,
                 exception: _MaybeSequence[Type[Exception]],
                 *,
                 max_tries: _MaybeCallable[int] = None,
                 max_time: _MaybeCallable[float] = None,
                 jitter: _Jitterer = full_jitter,
                 giveup: _Predicate[Exception] = lambda e: False,
                 on_success: _Handler = None,
                 on_backoff: _Handler = None,
                 on_giveup: _Handler = None,
                 logger: _MaybeLogger = 'backoff',
                 backoff_log_level: int = logging.INFO,
                 giveup_log_level: int = logging.ERROR,
                 **wait_gen_kwargs) -> Callable:
    """Returns decorator for backoff and retry triggered by exception.

    Args:
        wait_gen: A generator yielding successive wait times in
            seconds.
        exception: An exception type (or tuple of types) which triggers
            backoff.
        max_tries: The maximum number of attempts to make before giving
            up. Once exhausted, the exception will be allowed to escape.
            The default value of None means there is no limit to the
            number of tries. If a callable is passed, it will be
            evaluated at runtime and its return value used.
        max_time: The maximum total amount of time to try for before
            giving up. Once expired, the exception will be allowed to
            escape. If a callable is passed, it will be
            evaluated at runtime and its return value used.
        jitter: A function of the value yielded by wait_gen returning
            the actual time to wait. This distributes wait times
            stochastically in order to avoid timing collisions across
            concurrent clients. Wait times are jittered by default
            using the full_jitter function. Jittering may be disabled
            altogether by passing jitter=None.
        giveup: Function accepting an exception instance and
            returning whether or not to give up. Optional. The default
            is to always continue.
        on_success: Callable (or iterable of callables) with a unary
            signature to be called in the event of success. The
            parameter is a dict containing details about the invocation.
        on_backoff: Callable (or iterable of callables) with a unary
            signature to be called in the event of a backoff. The
            parameter is a dict containing details about the invocation.
        on_giveup: Callable (or iterable of callables) with a unary
            signature to be called in the event that max_tries
            is exceeded.  The parameter is a dict containing details
            about the invocation.
        logger: Name or Logger object to log to. Defaults to 'backoff'.
        backoff_log_level: log level for the backoff event. Defaults to "INFO"
        giveup_log_level: log level for the give up event. Defaults to "ERROR"
        **wait_gen_kwargs: Any additional keyword args specified will be
            passed to wait_gen when it is initialized.  Any callable
            args will first be evaluated and their return values passed.
            This is useful for runtime configuration.
    """
    def decorate(target):
        nonlocal logger, on_success, on_backoff, on_giveup

        logger = _prepare_logger(logger)
        on_success = _config_handlers(on_success)
        on_backoff = _config_handlers(
            on_backoff,
            default_handler=_log_backoff,
            logger=logger,
            log_level=backoff_log_level,
        )
        on_giveup = _config_handlers(
            on_giveup,
            default_handler=_log_giveup,
            logger=logger,
            log_level=giveup_log_level,
        )

        if asyncio.iscoroutinefunction(target):
            retry = _async.retry_exception
        else:
            retry = _sync.retry_exception

        return retry(
            target,
            wait_gen,
            exception,
            max_tries=max_tries,
            max_time=max_time,
            jitter=jitter,
            giveup=giveup,
            on_success=on_success,
            on_backoff=on_backoff,
            on_giveup=on_giveup,
            wait_gen_kwargs=wait_gen_kwargs
        )

    # Return a function which decorates a target with a retry loop.
    return decorate
<commit_msg>Use explicit optional types for arguments<commit_after># coding:utf-8
import asyncio
import logging
import operator
from typing import Any, Callable, Optional, Type

from backoff._common import (
    _prepare_logger,
    _config_handlers,
    _log_backoff,
    _log_giveup
)
from backoff._jitter import full_jitter
from backoff import _async, _sync
from backoff._typing import (
    _Handler,
    _Jitterer,
    _MaybeCallable,
    _MaybeLogger,
    _MaybeSequence,
    _Predicate,
    _WaitGenerator,
)

def on_predicate(wait_gen: _WaitGenerator,
                 predicate: _Predicate[Any] = operator.not_,
                 *,
                 max_tries: Optional[_MaybeCallable[int]] = None,
                 max_time: Optional[_MaybeCallable[float]] = None,
                 jitter: _Jitterer = full_jitter,
                 on_success: Optional[_Handler] = None,
                 on_backoff: Optional[_Handler] = None,
                 on_giveup: Optional[_Handler] = None,
                 logger: _MaybeLogger = 'backoff',
                 backoff_log_level: int = logging.INFO,
                 giveup_log_level: int = logging.ERROR,
                 **wait_gen_kwargs) -> Callable:
    """Returns decorator for backoff and retry triggered by predicate.

    Args:
        wait_gen: A generator yielding successive wait times in
            seconds.
        predicate: A function which when called on the return value of
            the target function will trigger backoff when considered
            truthily. If not specified, the default behavior is to
            backoff on falsey return values.
        max_tries: The maximum number of attempts to make before giving
            up. In the case of failure, the result of the last attempt
            will be returned. The default value of None means there
            is no limit to the number of tries. If a callable is passed,
            it will be evaluated at runtime and its return value used.
        max_time: The maximum total amount of time to try for before
            giving up. If this time expires, the result of the last
            attempt will be returned. If a callable is passed, it will
            be evaluated at runtime and its return value used.
        jitter: A function of the value yielded by wait_gen returning
            the actual time to wait. This distributes wait times
            stochastically in order to avoid timing collisions across
            concurrent clients. Wait times are jittered by default
            using the full_jitter function. Jittering may be disabled
            altogether by passing jitter=None.
        on_success: Callable (or iterable of callables) with a unary
            signature to be called in the event of success. The
            parameter is a dict containing details about the invocation.
        on_backoff: Callable (or iterable of callables) with a unary
            signature to be called in the event of a backoff. The
            parameter is a dict containing details about the invocation.
        on_giveup: Callable (or iterable of callables) with a unary
            signature to be called in the event that max_tries
            is exceeded.  The parameter is a dict containing details
            about the invocation.
        logger: Name of logger or Logger object to log to. Defaults to
            'backoff'.
        backoff_log_level: log level for the backoff event. Defaults to "INFO"
        giveup_log_level: log level for the give up event. Defaults to "ERROR"
        **wait_gen_kwargs: Any additional keyword args specified will be
            passed to wait_gen when it is initialized.  Any callable
            args will first be evaluated and their return values passed.
            This is useful for runtime configuration.
    """
    def decorate(target):
        nonlocal logger, on_success, on_backoff, on_giveup

        logger = _prepare_logger(logger)
        on_success = _config_handlers(on_success)
        on_backoff = _config_handlers(
            on_backoff,
            default_handler=_log_backoff,
            logger=logger,
            log_level=backoff_log_level
        )
        on_giveup = _config_handlers(
            on_giveup,
            default_handler=_log_giveup,
            logger=logger,
            log_level=giveup_log_level
        )

        if asyncio.iscoroutinefunction(target):
            retry = _async.retry_predicate
        else:
            retry = _sync.retry_predicate

        return retry(
            target,
            wait_gen,
            predicate,
            max_tries=max_tries,
            max_time=max_time,
            jitter=jitter,
            on_success=on_success,
            on_backoff=on_backoff,
            on_giveup=on_giveup,
            wait_gen_kwargs=wait_gen_kwargs
        )

    # Return a function which decorates a target with a retry loop.
    return decorate

def on_exception(wait_gen: _WaitGenerator,
                 exception: _MaybeSequence[Type[Exception]],
                 *,
                 max_tries: Optional[_MaybeCallable[int]] = None,
                 max_time: Optional[_MaybeCallable[float]] = None,
                 jitter: _Jitterer = full_jitter,
                 giveup: _Predicate[Exception] = lambda e: False,
                 on_success: Optional[_Handler] = None,
                 on_backoff: Optional[_Handler] = None,
                 on_giveup: Optional[_Handler] = None,
                 logger: _MaybeLogger = 'backoff',
                 backoff_log_level: int = logging.INFO,
                 giveup_log_level: int = logging.ERROR,
                 **wait_gen_kwargs) -> Callable:
    """Returns decorator for backoff and retry triggered by exception.

    Args:
        wait_gen: A generator yielding successive wait times in
            seconds.
        exception: An exception type (or tuple of types) which triggers
            backoff.
        max_tries: The maximum number of attempts to make before giving
            up. Once exhausted, the exception will be allowed to escape.
            The default value of None means there is no limit to the
            number of tries. If a callable is passed, it will be
            evaluated at runtime and its return value used.
        max_time: The maximum total amount of time to try for before
            giving up. Once expired, the exception will be allowed to
            escape. If a callable is passed, it will be
            evaluated at runtime and its return value used.
        jitter: A function of the value yielded by wait_gen returning
            the actual time to wait. This distributes wait times
            stochastically in order to avoid timing collisions across
            concurrent clients. Wait times are jittered by default
            using the full_jitter function. Jittering may be disabled
            altogether by passing jitter=None.
        giveup: Function accepting an exception instance and
            returning whether or not to give up. Optional. The default
            is to always continue.
        on_success: Callable (or iterable of callables) with a unary
            signature to be called in the event of success. The
            parameter is a dict containing details about the invocation.
        on_backoff: Callable (or iterable of callables) with a unary
            signature to be called in the event of a backoff. The
            parameter is a dict containing details about the invocation.
        on_giveup: Callable (or iterable of callables) with a unary
            signature to be called in the event that max_tries
            is exceeded.  The parameter is a dict containing details
            about the invocation.
        logger: Name or Logger object to log to. Defaults to 'backoff'.
        backoff_log_level: log level for the backoff event. Defaults to "INFO"
        giveup_log_level: log level for the give up event. Defaults to "ERROR"
        **wait_gen_kwargs: Any additional keyword args specified will be
            passed to wait_gen when it is initialized.  Any callable
            args will first be evaluated and their return values passed.
            This is useful for runtime configuration.
    """
    def decorate(target):
        nonlocal logger, on_success, on_backoff, on_giveup

        logger = _prepare_logger(logger)
        on_success = _config_handlers(on_success)
        on_backoff = _config_handlers(
            on_backoff,
            default_handler=_log_backoff,
            logger=logger,
            log_level=backoff_log_level,
        )
        on_giveup = _config_handlers(
            on_giveup,
            default_handler=_log_giveup,
            logger=logger,
            log_level=giveup_log_level,
        )

        if asyncio.iscoroutinefunction(target):
            retry = _async.retry_exception
        else:
            retry = _sync.retry_exception

        return retry(
            target,
            wait_gen,
            exception,
            max_tries=max_tries,
            max_time=max_time,
            jitter=jitter,
            giveup=giveup,
            on_success=on_success,
            on_backoff=on_backoff,
            on_giveup=on_giveup,
            wait_gen_kwargs=wait_gen_kwargs
        )

    # Return a function which decorates a target with a retry loop.
    return decorate
<|endoftext|>


<commit_before>r"""
Qitta analysis handlers for the Tornado webserver.

"""
# -----------------------------------------------------------------------------
# Copyright (c) 2014--, The Qiita Development Team.
#
# Distributed under the terms of the BSD 3-clause License.
#
# The full license is in the file LICENSE, distributed with this software.
# -----------------------------------------------------------------------------
from __future__ import division
from future.utils import viewitems
from tempfile import mkstemp
from os import close
from os.path import join
from itertools import product

from tornado.web import authenticated, asynchronous, HTTPError
from collections import defaultdict, Counter
from pyparsing import ParseException

from qiita_pet.handlers.base_handlers import BaseHandler
from qiita_ware.run import run_analysis
from qiita_db.user import User
from qiita_db.analysis import Analysis
from qiita_db.study import Study
from qiita_db.data import ProcessedData
from qiita_db.metadata_template import SampleTemplate
from qiita_db.job import Job, Command
from qiita_db.util import get_db_files_base_dir, get_table_cols
from qiita_db.search import QiitaStudySearch
from qiita_core.exceptions import IncompetentQiitaDeveloperError

def check_analysis_access(user, analysis_id):
    """Checks whether user has access to an analysis

    Parameters
    ----------
    user : User object
        User to check
    analysis_id : int
        Analysis to check access for

    Raises
    ------
    RuntimeError
        Tried to access analysis that user does not have access to
    """
    if analysis_id not in Analysis.get_public() + user.shared_analyses + \
            user.private_analyses:
        raise HTTPError(403, "Analysis access denied to %s" % (analysis_id))

class SearchStudiesHandler(BaseHandler):
    def _parse_search_results(self, results, selsamples, meta_headers):
        """remove already selected samples from results and count metadata"""
        counts = {}
        fullcounts = {meta: defaultdict(int) for meta in meta_headers}
        for study, samples in viewitems(results):
            counts[study] = {meta: Counter()
                             for meta in meta_headers}
            topop = []
            for pos, sample in enumerate(samples):
                if study in selsamples and sample[0] in selsamples[study]:
                    topop.append(pos)
                    # still add to full counts, but not study counts
                    for pos, meta in enumerate(meta_headers):
                        fullcounts[meta][sample[pos+1]] += 1
                else:
                    for pos, meta in enumerate(meta_headers):
                        counts[study][meta][sample[pos+1]] += 1
                        fullcounts[meta][sample[pos+1]] += 1
            # remove already selected samples
            topop.sort(reverse=True)
            for pos in topop:
                samples.pop(pos)
        return results, counts, fullcounts

    def _selected_parser(self, analysis):
        """builds dictionaries of selected samples from analysis object"""
        selsamples = {}
        selproc_data = defaultdict(list)
        for proc_data_id, samps in viewitems(analysis.samples):
            study = ProcessedData(proc_data_id).study
            selproc_data[study].append(proc_data_id)
            selsamples[study] = set(samps)
        return selproc_data, selsamples

    def _parse_form_select(self):
        """parses selected checkboxes and yields the selected ones in
        format accepted by Analysis.add_samples()
        """
        # get the selected studies and datatypes for studies
        studyinfo = self.get_arguments("availstudies")
        for s in studyinfo:
            study_id, datatype = s.split("#")
            # get the processed data ids for the study
            # get new selected samples for each study and yield with proc id
            for proc_samp_combo in product(self.get_arguments(s),
                                           self.get_arguments(study_id)):
                yield proc_samp_combo

    def _parse_form_deselect(self):
        """parses selected checkboxes and returns the selected ones in
        format accepted by Analysis.remove_samples()
        """
        studyinfo = self.get_arguments("selstudies")
        proc_data = []
        samples = []
        for sid in studyinfo:
            # get the processed data ids and add it to the study
            proc_data.extend(self.get_arguments("dt%s" % sid))
            # get new selected samples for each study and add to study
            samples.extend(self.get_arguments("sel%s" % sid))
        return proc_data, samples

    @authenticated
    def get(self):
        user = self.current_user
        analysis = Analysis(int(self.get_argument("aid")))
        # make sure user has access to the analysis
        userobj = User(user)
        check_analysis_access(userobj, analysis.id)

        # get the dictionaries of selected samples and data types
        selproc_data, selsamples = self._selected_parser(analysis)

        self.render('search_studies.html', user=user, aid=analysis.id,
                    selsamples=selsamples, selproc_data=selproc_data,
                    counts={}, fullcounts={}, searchmsg="", query="",
                    results={}, availmeta=SampleTemplate.metadata_headers() +
                    get_table_cols("study"))

    @authenticated
    def post(self):
        user = self.current_user
        action = self.get_argument("action")
        # set required template variables
        results = {}
        meta_headers = []
        counts = {}
        fullcounts = {}
        query = ""
        searchmsg = ""
        selsamples = {}
        selproc_data = {}
        # get analysis and selected samples if exists, or create if necessary
        if action == "create":
            name = self.get_argument('name')
            description = self.get_argument('description')
            analysis = Analysis.create(User(user), name, description)
            analysis_id = analysis.id
            analysis.step = 2
            # fill example studies by running query for specific studies
            search = QiitaStudySearch()
            def_query = 'study_id = 1 OR study_id = 2 OR study_id = 3'
            results, meta_headers = search(def_query, user)
            results, counts, fullcounts = self._parse_search_results(
                results, selsamples, meta_headers)
        else:
            analysis_id = int(self.get_argument("analysis-id"))
            check_analysis_access(User(user), analysis_id)
            analysis = Analysis(analysis_id)
            selproc_data, selsamples = self._selected_parser(analysis)

        # run through action requested
        if action == "search":
            search = QiitaStudySearch()
            query = str(self.get_argument("query"))
            try:
                results, meta_headers = search(query, user)
            except ParseException:
                searchmsg = "Malformed search query, please read search help."

            if not results and not searchmsg:
                searchmsg = "No results found."
            else:
                results, counts, fullcounts = self._parse_search_results(
                    results, selsamples, meta_headers)

        elif action == "select":
            analysis.add_samples(self._parse_form_select())

            # rebuild the selected from database to reflect changes
            selproc_data, selsamples = self._selected_parser(analysis)

        elif action == "deselect":
            proc_data, samples = self._parse_form_deselect()
            if proc_data:
                analysis.remove_samples(proc_data=proc_data)
            if samples:
                analysis.remove_samples(samples=samples)
            if not proc_data and not samples:
                searchmsg = "Must select samples to remove from analysis!"

            # rebuild the selected from database to reflect changes
            selproc_data, selsamples = self._selected_parser(analysis)

        self.render('search_studies.html', user=user, aid=analysis_id,
                    results=results, meta_headers=meta_headers,
                    selsamples=selsamples, selproc_data=selproc_data,
                    counts=counts, fullcounts=fullcounts, searchmsg=searchmsg,
                    query=query, availmeta=SampleTemplate.metadata_headers() +
                    get_table_cols("study"))

class SelectCommandsHandler(BaseHandler):
    """Select commands to be executed"""
    @authenticated
    def get(self):
        analysis_id = int(self.get_argument('aid'))
        check_analysis_access(User(self.current_user), analysis_id)

        analysis = Analysis(analysis_id)
        data_types = analysis.data_types
        commands = Command.get_commands_by_datatype()

        self.render('select_commands.html', user=self.current_user,
                    commands=commands, data_types=data_types, aid=analysis.id)

    @authenticated
    def post(self):
        analysis = Analysis(int(self.get_argument('analysis-id')))
        analysis.step = 3
        data_types = analysis.data_types
        commands = Command.get_commands_by_datatype()
        self.render('select_commands.html', user=self.current_user,
                    commands=commands, data_types=data_types, aid=analysis.id)

class AnalysisWaitHandler(BaseHandler):
    @authenticated
    def get(self, analysis_id):
        user = self.current_user
        analysis_id = int(analysis_id)
        check_analysis_access(User(user), analysis_id)

        analysis = Analysis(analysis_id)

        commands = []
        for job in analysis.jobs:
            jobject = Job(job)
            commands.append("%s:%s" % (jobject.datatype, jobject.command[0]))

        self.render("analysis_waiting.html", user=user,
                    aid=analysis_id, aname=analysis.name,
                    commands=commands)

    @authenticated
    @asynchronous
    def post(self, analysis_id):
        user = self.current_user
        analysis_id = int(analysis_id)
        check_analysis_access(User(user), analysis_id)

        command_args = self.get_arguments("commands")
        split = [x.split("#") for x in command_args]
        analysis = Analysis(analysis_id)

        commands = []
        # HARD CODED HACKY THING FOR DEMO, FIX  Issue #164
        fp, mapping_file = mkstemp(suffix="_map_file.txt")
        close(fp)
        SampleTemplate(1).to_file(mapping_file)
        study_fps = {}
        for pd in Study(1).processed_data():
            processed = ProcessedData(pd)
            study_fps[processed.data_type()] = processed.get_filepaths()[0][0]
        for data_type, command in split:
            opts = {
                "--otu_table_fp": study_fps[data_type],
                "--mapping_fp": mapping_file
            }
            if command == "Beta Diversity" and data_type in {'16S', '18S'}:
                opts["--tree_fp"] = join(get_db_files_base_dir(), "reference",
                                         "gg_97_otus_4feb2011.tre")
            elif command == "Beta Diversity":
                opts["--parameter_fp"] = join(get_db_files_base_dir(),
                                              "reference", "params_qiime.txt")
            job = Job.create(data_type, command, analysis)
            job.options = opts
            commands.append("%s: %s" % (data_type, command))
        user = self.current_user
        self.render("analysis_waiting.html", user=user, aid=analysis_id,
                    aname=analysis.name, commands=commands)
        # fire off analysis run here
        # currently synch run so redirect done here. Will remove after demo
        run_analysis(user, analysis)

class AnalysisResultsHandler(BaseHandler):
    @authenticated
    def get(self, analysis_id):
        user = self.current_user
        analysis_id = int(analysis_id)
        check_analysis_access(User(user), analysis_id)

        analysis = Analysis(analysis_id)
        jobres = defaultdict(list)
        for job in analysis.jobs:
            jobject = Job(job)
            jobres[jobject.datatype].append((jobject.command[0],
                                             jobject.results))

        self.render("analysis_results.html", user=self.current_user,
                    jobres=jobres, aname=analysis.name,
                    basefolder=get_db_files_base_dir())

class ShowAnalysesHandler(BaseHandler):
    """Shows the user's analyses"""
    @authenticated
    def get(self):
        user_id = self.current_user
        user = User(user_id)

        analyses = [Analysis(a) for a in
                    user.shared_analyses + user.private_analyses]

        self.render("show_analyses.html", user=user_id, analyses=analyses)
<commit_msg>add comments explaining step settings<commit_after>r"""
Qitta analysis handlers for the Tornado webserver.

"""
# -----------------------------------------------------------------------------
# Copyright (c) 2014--, The Qiita Development Team.
#
# Distributed under the terms of the BSD 3-clause License.
#
# The full license is in the file LICENSE, distributed with this software.
# -----------------------------------------------------------------------------
from __future__ import division
from future.utils import viewitems
from tempfile import mkstemp
from os import close
from os.path import join
from itertools import product

from tornado.web import authenticated, asynchronous, HTTPError
from collections import defaultdict, Counter
from pyparsing import ParseException

from qiita_pet.handlers.base_handlers import BaseHandler
from qiita_ware.run import run_analysis
from qiita_db.user import User
from qiita_db.analysis import Analysis
from qiita_db.study import Study
from qiita_db.data import ProcessedData
from qiita_db.metadata_template import SampleTemplate
from qiita_db.job import Job, Command
from qiita_db.util import get_db_files_base_dir, get_table_cols
from qiita_db.search import QiitaStudySearch
from qiita_core.exceptions import IncompetentQiitaDeveloperError

def check_analysis_access(user, analysis_id):
    """Checks whether user has access to an analysis

    Parameters
    ----------
    user : User object
        User to check
    analysis_id : int
        Analysis to check access for

    Raises
    ------
    RuntimeError
        Tried to access analysis that user does not have access to
    """
    if analysis_id not in Analysis.get_public() + user.shared_analyses + \
            user.private_analyses:
        raise HTTPError(403, "Analysis access denied to %s" % (analysis_id))

class SearchStudiesHandler(BaseHandler):
    def _parse_search_results(self, results, selsamples, meta_headers):
        """remove already selected samples from results and count metadata"""
        counts = {}
        fullcounts = {meta: defaultdict(int) for meta in meta_headers}
        for study, samples in viewitems(results):
            counts[study] = {meta: Counter()
                             for meta in meta_headers}
            topop = []
            for pos, sample in enumerate(samples):
                if study in selsamples and sample[0] in selsamples[study]:
                    topop.append(pos)
                    # still add to full counts, but not study counts
                    for pos, meta in enumerate(meta_headers):
                        fullcounts[meta][sample[pos+1]] += 1
                else:
                    for pos, meta in enumerate(meta_headers):
                        counts[study][meta][sample[pos+1]] += 1
                        fullcounts[meta][sample[pos+1]] += 1
            # remove already selected samples
            topop.sort(reverse=True)
            for pos in topop:
                samples.pop(pos)
        return results, counts, fullcounts

    def _selected_parser(self, analysis):
        """builds dictionaries of selected samples from analysis object"""
        selsamples = {}
        selproc_data = defaultdict(list)
        for proc_data_id, samps in viewitems(analysis.samples):
            study = ProcessedData(proc_data_id).study
            selproc_data[study].append(proc_data_id)
            selsamples[study] = set(samps)
        return selproc_data, selsamples

    def _parse_form_select(self):
        """parses selected checkboxes and yields the selected ones in
        format accepted by Analysis.add_samples()
        """
        # get the selected studies and datatypes for studies
        studyinfo = self.get_arguments("availstudies")
        for s in studyinfo:
            study_id, datatype = s.split("#")
            # get the processed data ids for the study
            # get new selected samples for each study and yield with proc id
            for proc_samp_combo in product(self.get_arguments(s),
                                           self.get_arguments(study_id)):
                yield proc_samp_combo

    def _parse_form_deselect(self):
        """parses selected checkboxes and returns the selected ones in
        format accepted by Analysis.remove_samples()
        """
        studyinfo = self.get_arguments("selstudies")
        proc_data = []
        samples = []
        for sid in studyinfo:
            # get the processed data ids and add it to the study
            proc_data.extend(self.get_arguments("dt%s" % sid))
            # get new selected samples for each study and add to study
            samples.extend(self.get_arguments("sel%s" % sid))
        return proc_data, samples

    @authenticated
    def get(self):
        user = self.current_user
        analysis = Analysis(int(self.get_argument("aid")))
        # make sure user has access to the analysis
        userobj = User(user)
        check_analysis_access(userobj, analysis.id)

        # get the dictionaries of selected samples and data types
        selproc_data, selsamples = self._selected_parser(analysis)

        self.render('search_studies.html', user=user, aid=analysis.id,
                    selsamples=selsamples, selproc_data=selproc_data,
                    counts={}, fullcounts={}, searchmsg="", query="",
                    results={}, availmeta=SampleTemplate.metadata_headers() +
                    get_table_cols("study"))

    @authenticated
    def post(self):
        user = self.current_user
        action = self.get_argument("action")
        # set required template variables
        results = {}
        meta_headers = []
        counts = {}
        fullcounts = {}
        query = ""
        searchmsg = ""
        selsamples = {}
        selproc_data = {}
        # get analysis and selected samples if exists, or create if necessary
        if action == "create":
            name = self.get_argument('name')
            description = self.get_argument('description')
            analysis = Analysis.create(User(user), name, description)
            analysis_id = analysis.id
            # set to second step since this page is second step in workflow
            analysis.step = 2
            # fill example studies by running query for specific studies
            search = QiitaStudySearch()
            def_query = 'study_id = 1 OR study_id = 2 OR study_id = 3'
            results, meta_headers = search(def_query, user)
            results, counts, fullcounts = self._parse_search_results(
                results, selsamples, meta_headers)
        else:
            analysis_id = int(self.get_argument("analysis-id"))
            check_analysis_access(User(user), analysis_id)
            analysis = Analysis(analysis_id)
            selproc_data, selsamples = self._selected_parser(analysis)

        # run through action requested
        if action == "search":
            search = QiitaStudySearch()
            query = str(self.get_argument("query"))
            try:
                results, meta_headers = search(query, user)
            except ParseException:
                searchmsg = "Malformed search query, please read search help."

            if not results and not searchmsg:
                searchmsg = "No results found."
            else:
                results, counts, fullcounts = self._parse_search_results(
                    results, selsamples, meta_headers)

        elif action == "select":
            analysis.add_samples(self._parse_form_select())

            # rebuild the selected from database to reflect changes
            selproc_data, selsamples = self._selected_parser(analysis)

        elif action == "deselect":
            proc_data, samples = self._parse_form_deselect()
            if proc_data:
                analysis.remove_samples(proc_data=proc_data)
            if samples:
                analysis.remove_samples(samples=samples)
            if not proc_data and not samples:
                searchmsg = "Must select samples to remove from analysis!"

            # rebuild the selected from database to reflect changes
            selproc_data, selsamples = self._selected_parser(analysis)

        self.render('search_studies.html', user=user, aid=analysis_id,
                    results=results, meta_headers=meta_headers,
                    selsamples=selsamples, selproc_data=selproc_data,
                    counts=counts, fullcounts=fullcounts, searchmsg=searchmsg,
                    query=query, availmeta=SampleTemplate.metadata_headers() +
                    get_table_cols("study"))

class SelectCommandsHandler(BaseHandler):
    """Select commands to be executed"""
    @authenticated
    def get(self):
        analysis_id = int(self.get_argument('aid'))
        check_analysis_access(User(self.current_user), analysis_id)

        analysis = Analysis(analysis_id)
        data_types = analysis.data_types
        commands = Command.get_commands_by_datatype()

        self.render('select_commands.html', user=self.current_user,
                    commands=commands, data_types=data_types, aid=analysis.id)

    @authenticated
    def post(self):
        analysis = Analysis(int(self.get_argument('analysis-id')))
        # set to third step since this page is third step in workflow
        analysis.step = 3
        data_types = analysis.data_types
        commands = Command.get_commands_by_datatype()
        self.render('select_commands.html', user=self.current_user,
                    commands=commands, data_types=data_types, aid=analysis.id)

class AnalysisWaitHandler(BaseHandler):
    @authenticated
    def get(self, analysis_id):
        user = self.current_user
        analysis_id = int(analysis_id)
        check_analysis_access(User(user), analysis_id)

        analysis = Analysis(analysis_id)

        commands = []
        for job in analysis.jobs:
            jobject = Job(job)
            commands.append("%s:%s" % (jobject.datatype, jobject.command[0]))

        self.render("analysis_waiting.html", user=user,
                    aid=analysis_id, aname=analysis.name,
                    commands=commands)

    @authenticated
    @asynchronous
    def post(self, analysis_id):
        user = self.current_user
        analysis_id = int(analysis_id)
        check_analysis_access(User(user), analysis_id)

        command_args = self.get_arguments("commands")
        split = [x.split("#") for x in command_args]
        analysis = Analysis(analysis_id)

        commands = []
        # HARD CODED HACKY THING FOR DEMO, FIX  Issue #164
        fp, mapping_file = mkstemp(suffix="_map_file.txt")
        close(fp)
        SampleTemplate(1).to_file(mapping_file)
        study_fps = {}
        for pd in Study(1).processed_data():
            processed = ProcessedData(pd)
            study_fps[processed.data_type()] = processed.get_filepaths()[0][0]
        for data_type, command in split:
            opts = {
                "--otu_table_fp": study_fps[data_type],
                "--mapping_fp": mapping_file
            }
            if command == "Beta Diversity" and data_type in {'16S', '18S'}:
                opts["--tree_fp"] = join(get_db_files_base_dir(), "reference",
                                         "gg_97_otus_4feb2011.tre")
            elif command == "Beta Diversity":
                opts["--parameter_fp"] = join(get_db_files_base_dir(),
                                              "reference", "params_qiime.txt")
            job = Job.create(data_type, command, analysis)
            job.options = opts
            commands.append("%s: %s" % (data_type, command))
        user = self.current_user
        self.render("analysis_waiting.html", user=user, aid=analysis_id,
                    aname=analysis.name, commands=commands)
        # fire off analysis run here
        # currently synch run so redirect done here. Will remove after demo
        run_analysis(user, analysis)

class AnalysisResultsHandler(BaseHandler):
    @authenticated
    def get(self, analysis_id):
        user = self.current_user
        analysis_id = int(analysis_id)
        check_analysis_access(User(user), analysis_id)

        analysis = Analysis(analysis_id)
        jobres = defaultdict(list)
        for job in analysis.jobs:
            jobject = Job(job)
            jobres[jobject.datatype].append((jobject.command[0],
                                             jobject.results))

        self.render("analysis_results.html", user=self.current_user,
                    jobres=jobres, aname=analysis.name,
                    basefolder=get_db_files_base_dir())

class ShowAnalysesHandler(BaseHandler):
    """Shows the user's analyses"""
    @authenticated
    def get(self):
        user_id = self.current_user
        user = User(user_id)

        analyses = [Analysis(a) for a in
                    user.shared_analyses + user.private_analyses]

        self.render("show_analyses.html", user=user_id, analyses=analyses)
<|endoftext|>