<commit_before>"""
Autogenerate documentation RESTful APIs
"""
from random import randint
from os.path import join, dirname
from django.template import Context, Template
from django.db.models import fields

from devilry.simplified import SimplifiedModelApi
from devilry.simplified.utils import get_field_from_fieldname, get_clspath

#
# Fetch templates from the
#
this_dir = dirname(__file__)
def read_tpl(name):
    return open(join(this_dir, 'createdocs_tpl', '{0}.django.rst'.format(name))).read()
SEARCH_DOCS = read_tpl('search')
CREATE_DOCS = read_tpl('create')
READ_DOCS = read_tpl('read')
UPDATE_DOCS = read_tpl('update')
DELETE_DOCS = read_tpl('delete')

#
# Horrible code from here and down. Read at your own risc.
#

def get_model_clspath(model):
    modelclsname = model.__name__
    modelmodulename = model.__module__
    if modelmodulename.endswith('.' + modelclsname.lower()):
        modelmodulename = modelmodulename.rsplit('.', 1)[0]
    return '{0}.{1}'.format(modelmodulename, modelclsname)

def field_to_restfultype(field):
    if isinstance(field, fields.related.AutoField):
        return 'Integer', 15
    elif isinstance(field, fields.CharField):
        return 'String', '"myvalue"'
    elif isinstance(field, fields.TextField):
        return 'String', '"myvalue"'
    elif isinstance(field, fields.BooleanField):
        return 'Boolean', 'true'
    elif isinstance(field, fields.IntegerField):
        return 'Integer', '20'
    elif isinstance(field, fields.DateTimeField):
        return 'DateTime string (YYYY-MM-DD hh:mm:ss)', '2010-02-22 22:32:10'
    elif isinstance(field, fields.related.ManyToManyField) or isinstance(field, fields.related.RelatedObject):
        return 'Comma-separated list', '"this, is, an, example"'
    else:
        raise ValueError('Unsupported field type: {0}'.format(type(field)))

def field_to_help_text(field):
    if isinstance(field, fields.related.AutoField):
        return 'Autogenerated identifier.'
    elif isinstance(field, fields.related.ManyToManyField) or isinstance(field, fields.related.RelatedObject):
        return 'List of many values.'
    help_text = field.help_text
    if not help_text or help_text.strip() == '':
        raise ValueError('Missing help for: {0}.{1}'.format(get_clspath(field.model), field.name))
    return help_text

class Docstring(object):
    def __init__(self, docstring, restfulcls, httpmethod, itemurl):
        self.docstring = Template(docstring)
        self.restfulcls = restfulcls
        self.httpmethod = httpmethod
        self.itemurl = itemurl
        if itemurl.endswith('id**'):
            self.itemexampleurl = itemurl[:-4] + '10'
        else:
            self.itemexampleurl = itemurl
        simplified = restfulcls._meta.simplified
        model = simplified._meta.model

        self.modelclsname = model.__name__
        self.modelmodulename = model.__module__
        if self.modelmodulename.endswith('.' + self.modelclsname.lower()):
            self.modelmodulename = self.modelmodulename.rsplit('.', 1)[0]
        self.modelclspath = get_model_clspath(model)
        self.model_verbose_name = model._meta.verbose_name
        self.model_verbose_name_plural = model._meta.verbose_name_plural
        self.model = model

        self.result_fieldgroups = self._create_fieldgroup_overview(simplified._meta.resultfields.additional_fieldgroups)
        self.search_fieldgroups = self._create_fieldgroup_overview(simplified._meta.searchfields.additional_fieldgroups)
        self.searchfields = self._create_fieldinfolist(simplified._meta.searchfields.always_available_fields)
        self.resultfields = self._create_fieldinfolist(simplified._meta.resultfields.always_available_fields)
        self._create_filter_docattrs()

        self.context = Context(dict(doc=self))

    def _create_fieldnamelist(self, fieldnames):
        return ', '.join('``{0}``'.format(fieldname) for fieldname in fieldnames)

    def _fieldinfo_dict(self, field):
        modelclspath = get_model_clspath(field.model)
        help_text = field_to_help_text(field)
        fieldtype, valueexample = field_to_restfultype(field)
        return dict(help_text=help_text,
                    fieldtype=fieldtype,
                    modelclspath=modelclspath,
                    valueexample=valueexample)

    def _create_filter_docattrs(self):
        self.filters = self.restfulcls._meta.simplified._meta.filters
        self.filterspecs = []
        for filterspec in sorted(self.filters.filterspecs.values(), key=lambda s: s.fieldname):
            field = self._get_field(filterspec.fieldname)
            fs = self._fieldinfo_dict(field)
            fs['filterspec'] = filterspec
            self.filterspecs.append(fs)

        self.patternfilterspecs = sorted(self.filters.patternfilterspecs, key=lambda s: s.fieldname)
        self._create_filter_example()

    def _create_filter_example(self):
        filterexample = []
        for fs in self.filterspecs[:3]:
            filterspec = fs['filterspec']
            supported_comp = list(filterspec.supported_comp)
            compindex = randint(0, len(supported_comp)-1)
            example = '{{field:"{0}", comp:"{1}", value:{2}}}'.format(filterspec.fieldname,
                                                                supported_comp[compindex],
                                                                fs['valueexample'])
            filterexample.append(example)
        filterexample = '[{0}]'.format(',\n '.join(filterexample))
        self.filterexample = self._indent(filterexample, '        ')
        self.filterexample_for_overview = self._indent(filterexample, '                     ').lstrip()

    def _indent(self, value, indent):
        return '\n'.join('{0}{1}'.format(indent, line) for line in value.split('\n'))

    def _create_fieldinfolist(self, fieldnames):
        infolist = []
        for fieldname in fieldnames:
            info = self._fieldinfo_dict(self._get_field(fieldname))
            info['fieldname'] = fieldname
            infolist.append(info)
        return infolist

    def _create_fieldgroup_overview(self, fieldgroups):
        result = []
        for fieldgroup, fieldgroupfields in fieldgroups.iteritems():
            result.append(dict(fieldgroup=fieldgroup,
                               fieldinfolist=self._create_fieldinfolist(fieldgroupfields)))
        return result

    def get_first_para(self):
        return str(self).split('\n\n')[0].replace('\n', ' ')

    def __str__(self):
        return self.docstring.render(self.context)

    def _get_field(self, fieldname):
        return get_field_from_fieldname(self.model, fieldname, fkfield_as_idfield=True)

class Page(object):
    TPL = '''.. _{ref}:

=============================================================================
{httpmethod} {url}
=============================================================================

{docs}'''
    def __init__(self, refprefix, methodname, httpmethod, url, docs):
        self.httpmethod = httpmethod
        self.url = url
        self.docs = docs
        self.ref = '{0}_details_{1}'.format(refprefix, methodname) # id usable by rst :ref:
        self.filename = '{0}.rst'.format(self.ref)

    def __unicode__(self):
        return self.TPL.format(**self.__dict__)

    def get_filepath(self, directory):
        return join(directory, self.filename)

    def write_to_dir(self, directory):
        open(self.get_filepath(directory), 'wb').write(unicode(self).encode('utf-8'))

class IndexItem(object):
    TPL = '''

        {httpmethod} {prettyrestfulurl}
        {first_para}

'''
    def __init__(self, refprefix, methodname, httpmethod, url, docs):
        self.httpmethod = httpmethod
        self.url = url
        self.prettyrestfulurl = url
        if url.endswith('id'):
            self.prettyrestfulurl = self.prettyrestfulurl[:-2]
            self.prettyrestfulurl += 'id'
        self.first_para = docs.get_first_para()
        self.page = Page(refprefix, methodname, httpmethod, url, docs)

    def __unicode__(self):
        return self.TPL.format(pageref=self.page.ref, **self.__dict__)

    def __cmp__(self, other):
        """ Ordering: search, read, create, update, delete """
        if self.httpmethod == 'DELETE':
            return 1
        return cmp(self.httpmethod+self.url, other.httpmethod+other.url)

class IndexPageItem(object):
    TPL = '''
{modelclsname}
-------------------------------------------------------------------------

.. raw:: html

                Resource
                Description

            {indexitems}

'''
    def __init__(self, restfulcls, indexitems):
        self.restfulcls = restfulcls
        self.simplifiedclsname = self.restfulcls._meta.simplified.__name__
        self.modelclsname = self.restfulcls._meta.simplified._meta.model.__name__
        self.indexitems = indexitems

    def __unicode__(self):
        return self.TPL.format(simplifiedclsname=self.simplifiedclsname,
                               modelclsname=self.modelclsname,
                               indexitems='\n    '.join(unicode(i) for i in sorted(self.indexitems)))

    def iterpages(self):
        for indexitem in self.indexitems:
            yield indexitem.page

class IndexPage(object):
    TPL = '''.. _{ref}:

===============================================================
{indextitle}
===============================================================

.. toctree::
    :hidden:

    {toctree}

{items}'''
    def __init__(self, indexpageitems, ref, indextitle):
        self.items = '\n\n'.join(unicode(indexpageitem) for indexpageitem in indexpageitems)
        toctreerefs = []
        for indexpageitem in indexpageitems:
            for page in indexpageitem.iterpages():
                toctreerefs.append(page.ref)
        self.toctree = '\n    '.join(toctreerefs)
        self.ref = ref
        self.indextitle = indextitle

    def __unicode__(self):
        return self.TPL.format(**self.__dict__)

    def write(self, filepath):
        open(filepath, 'wb').write(unicode(self).encode('utf-8'))

class RestfulDocs(object):
    CRUD_TO_HTTP = {'create': ('POST', False, CREATE_DOCS),
                    'read': ('GET', True, READ_DOCS),
                    'update': ('PUT', True, UPDATE_DOCS),
                    'delete': ('DELETE', True, DELETE_DOCS),
                    'search': ('GET', False, SEARCH_DOCS)}
    def iter_restfulcls_methods(self, restfulcls):
        for methodname in restfulcls._meta.simplified._meta.methods:
            if methodname.startswith('insecure_'):
                continue
            method = getattr(SimplifiedModelApi, methodname)
            yield methodname

    def _get_restfulcls_docprefix(self, restfulcls):
        appname = restfulcls.__module__.split('.')[-2] # Assume restful is in appdir.restful
        return '{0}_{1}'.format(appname, restfulcls.__name__.lower())

    def iter_restfulmanager_docs(self, restfulmanager):
        for restfulcls in restfulmanager.iter_restfulclasses():
            refprefix = self._get_restfulcls_docprefix(restfulcls)
            url = restfulcls.get_rest_url()
            indexitems = []
            for methodname in self.iter_restfulcls_methods(restfulcls):
                httpmethod, hasid, docs = self.CRUD_TO_HTTP[methodname]
                if hasid:
                    itemurl = '{0}id'.format(url)
                else:
                    itemurl = url
                indexitems.append(IndexItem(refprefix, methodname, httpmethod, itemurl,
                                            Docstring(docs, restfulcls, httpmethod, itemurl)))
            yield IndexPageItem(restfulcls, indexitems)

    def restfulmanager_docs_to_rstfiles(self, directory, indexpageitem):
        for page in indexpageitem.iterpages():
            page.write_to_dir(directory)

    def create_in_directory(self, directory, indexpageref, indextitle, restfulmanager):
        indexpageitems = []
        for indexpageitem in self.iter_restfulmanager_docs(restfulmanager):
            indexpageitems.append(indexpageitem)
            self.restfulmanager_docs_to_rstfiles(directory, indexpageitem)
        indexpage = IndexPage(indexpageitems, indexpageref, indextitle)
        indexpage.write(join(directory, 'index.rst'))
<commit_msg>restful.createdocs: Fixed some links.<commit_after>"""
Autogenerate documentation RESTful APIs
"""
from random import randint
from os.path import join, dirname
from django.template import Context, Template
from django.db.models import fields

from devilry.simplified import SimplifiedModelApi
from devilry.simplified.utils import get_field_from_fieldname, get_clspath

#
# Fetch templates from the
#
this_dir = dirname(__file__)
def read_tpl(name):
    return open(join(this_dir, 'createdocs_tpl', '{0}.django.rst'.format(name))).read()
SEARCH_DOCS = read_tpl('search')
CREATE_DOCS = read_tpl('create')
READ_DOCS = read_tpl('read')
UPDATE_DOCS = read_tpl('update')
DELETE_DOCS = read_tpl('delete')

#
# Horrible code from here and down. Read at your own risc.
#

def get_model_clspath(model):
    modelclsname = model.__name__
    modelmodulename = model.__module__
    if modelmodulename.replace('_', '').endswith('.' + modelclsname.lower()): # .....assignment_group.AssignmentGroup -> AssignmentGroup
        modelmodulename = modelmodulename.rsplit('.', 1)[0]
    return '{0}.{1}'.format(modelmodulename, modelclsname)

def field_to_restfultype(field):
    if isinstance(field, fields.related.AutoField):
        return 'Integer', 15
    elif isinstance(field, fields.CharField):
        return 'String', '"myvalue"'
    elif isinstance(field, fields.TextField):
        return 'String', '"myvalue"'
    elif isinstance(field, fields.BooleanField):
        return 'Boolean', 'true'
    elif isinstance(field, fields.IntegerField):
        return 'Integer', '20'
    elif isinstance(field, fields.DateTimeField):
        return 'DateTime string (YYYY-MM-DD hh:mm:ss)', '2010-02-22 22:32:10'
    elif isinstance(field, fields.related.ManyToManyField) or isinstance(field, fields.related.RelatedObject):
        return 'Comma-separated list', '"this, is, an, example"'
    else:
        raise ValueError('Unsupported field type: {0}'.format(type(field)))

def field_to_help_text(field):
    if isinstance(field, fields.related.AutoField):
        return 'Autogenerated identifier.'
    elif isinstance(field, fields.related.ManyToManyField) or isinstance(field, fields.related.RelatedObject):
        return 'List of many values.'
    help_text = field.help_text
    if not help_text or help_text.strip() == '':
        raise ValueError('Missing help for: {0}.{1}'.format(get_clspath(field.model), field.name))
    return help_text

class Docstring(object):
    def __init__(self, docstring, restfulcls, httpmethod, itemurl):
        self.docstring = Template(docstring)
        self.restfulcls = restfulcls
        self.httpmethod = httpmethod
        self.itemurl = itemurl
        if itemurl.endswith('id**'):
            self.itemexampleurl = itemurl[:-4] + '10'
        else:
            self.itemexampleurl = itemurl
        simplified = restfulcls._meta.simplified
        model = simplified._meta.model

        self.modelclspath = get_model_clspath(model)
        self.model_verbose_name = model._meta.verbose_name
        self.model_verbose_name_plural = model._meta.verbose_name_plural
        self.model = model

        self.result_fieldgroups = self._create_fieldgroup_overview(simplified._meta.resultfields.additional_fieldgroups)
        self.search_fieldgroups = self._create_fieldgroup_overview(simplified._meta.searchfields.additional_fieldgroups)
        self.searchfields = self._create_fieldinfolist(simplified._meta.searchfields.always_available_fields)
        self.resultfields = self._create_fieldinfolist(simplified._meta.resultfields.always_available_fields)
        self._create_filter_docattrs()

        self.context = Context(dict(doc=self))

    def _create_fieldnamelist(self, fieldnames):
        return ', '.join('``{0}``'.format(fieldname) for fieldname in fieldnames)

    def _fieldinfo_dict(self, field):
        modelclspath = get_model_clspath(field.model)
        help_text = field_to_help_text(field)
        fieldtype, valueexample = field_to_restfultype(field)
        return dict(help_text=help_text,
                    fieldtype=fieldtype,
                    modelclspath=modelclspath,
                    valueexample=valueexample)

    def _create_filter_docattrs(self):
        self.filters = self.restfulcls._meta.simplified._meta.filters
        self.filterspecs = []
        for filterspec in sorted(self.filters.filterspecs.values(), key=lambda s: s.fieldname):
            field = self._get_field(filterspec.fieldname)
            fs = self._fieldinfo_dict(field)
            fs['filterspec'] = filterspec
            self.filterspecs.append(fs)

        self.patternfilterspecs = sorted(self.filters.patternfilterspecs, key=lambda s: s.fieldname)
        self._create_filter_example()

    def _create_filter_example(self):
        filterexample = []
        for fs in self.filterspecs[:3]:
            filterspec = fs['filterspec']
            supported_comp = list(filterspec.supported_comp)
            compindex = randint(0, len(supported_comp)-1)
            example = '{{field:"{0}", comp:"{1}", value:{2}}}'.format(filterspec.fieldname,
                                                                supported_comp[compindex],
                                                                fs['valueexample'])
            filterexample.append(example)
        filterexample = '[{0}]'.format(',\n '.join(filterexample))
        self.filterexample = self._indent(filterexample, '        ')
        self.filterexample_for_overview = self._indent(filterexample, '                     ').lstrip()

    def _indent(self, value, indent):
        return '\n'.join('{0}{1}'.format(indent, line) for line in value.split('\n'))

    def _create_fieldinfolist(self, fieldnames):
        infolist = []
        for fieldname in fieldnames:
            info = self._fieldinfo_dict(self._get_field(fieldname))
            info['fieldname'] = fieldname
            infolist.append(info)
        return infolist

    def _create_fieldgroup_overview(self, fieldgroups):
        result = []
        for fieldgroup, fieldgroupfields in fieldgroups.iteritems():
            result.append(dict(fieldgroup=fieldgroup,
                               fieldinfolist=self._create_fieldinfolist(fieldgroupfields)))
        return result

    def get_first_para(self):
        return str(self).split('\n\n')[0].replace('\n', ' ')

    def __str__(self):
        return self.docstring.render(self.context)

    def _get_field(self, fieldname):
        return get_field_from_fieldname(self.model, fieldname, fkfield_as_idfield=True)

class Page(object):
    TPL = '''.. _{ref}:

=============================================================================
{httpmethod} {url}
=============================================================================

{docs}'''
    def __init__(self, refprefix, methodname, httpmethod, url, docs):
        self.httpmethod = httpmethod
        self.url = url
        self.docs = docs
        self.ref = '{0}_details_{1}'.format(refprefix, methodname) # id usable by rst :ref:
        self.filename = '{0}.rst'.format(self.ref)

    def __unicode__(self):
        return self.TPL.format(**self.__dict__)

    def get_filepath(self, directory):
        return join(directory, self.filename)

    def write_to_dir(self, directory):
        open(self.get_filepath(directory), 'wb').write(unicode(self).encode('utf-8'))

class IndexItem(object):
    TPL = '''

        {httpmethod} {prettyrestfulurl}
        {first_para}

'''
    def __init__(self, refprefix, methodname, httpmethod, url, docs):
        self.httpmethod = httpmethod
        self.url = url
        self.prettyrestfulurl = url
        if url.endswith('id'):
            self.prettyrestfulurl = self.prettyrestfulurl[:-2]
            self.prettyrestfulurl += 'id'
        self.first_para = docs.get_first_para()
        self.page = Page(refprefix, methodname, httpmethod, url, docs)

    def __unicode__(self):
        return self.TPL.format(pageref=self.page.ref, **self.__dict__)

    def __cmp__(self, other):
        """ Ordering: search, read, create, update, delete """
        if self.httpmethod == 'DELETE':
            return 1
        return cmp(self.httpmethod+self.url, other.httpmethod+other.url)

class IndexPageItem(object):
    TPL = '''
{modelclsname}
-------------------------------------------------------------------------

.. raw:: html

                Resource
                Description

            {indexitems}

'''
    def __init__(self, restfulcls, indexitems):
        self.restfulcls = restfulcls
        self.simplifiedclsname = self.restfulcls._meta.simplified.__name__
        self.modelclsname = self.restfulcls._meta.simplified._meta.model.__name__
        self.indexitems = indexitems

    def __unicode__(self):
        return self.TPL.format(simplifiedclsname=self.simplifiedclsname,
                               modelclsname=self.modelclsname,
                               indexitems='\n    '.join(unicode(i) for i in sorted(self.indexitems)))

    def iterpages(self):
        for indexitem in self.indexitems:
            yield indexitem.page

class IndexPage(object):
    TPL = '''.. _{ref}:

===============================================================
{indextitle}
===============================================================

.. toctree::
    :hidden:

    {toctree}

{items}'''
    def __init__(self, indexpageitems, ref, indextitle):
        self.items = '\n\n'.join(unicode(indexpageitem) for indexpageitem in indexpageitems)
        toctreerefs = []
        for indexpageitem in indexpageitems:
            for page in indexpageitem.iterpages():
                toctreerefs.append(page.ref)
        self.toctree = '\n    '.join(toctreerefs)
        self.ref = ref
        self.indextitle = indextitle

    def __unicode__(self):
        return self.TPL.format(**self.__dict__)

    def write(self, filepath):
        open(filepath, 'wb').write(unicode(self).encode('utf-8'))

class RestfulDocs(object):
    CRUD_TO_HTTP = {'create': ('POST', False, CREATE_DOCS),
                    'read': ('GET', True, READ_DOCS),
                    'update': ('PUT', True, UPDATE_DOCS),
                    'delete': ('DELETE', True, DELETE_DOCS),
                    'search': ('GET', False, SEARCH_DOCS)}
    def iter_restfulcls_methods(self, restfulcls):
        for methodname in restfulcls._meta.simplified._meta.methods:
            if methodname.startswith('insecure_'):
                continue
            method = getattr(SimplifiedModelApi, methodname)
            yield methodname

    def _get_restfulcls_docprefix(self, restfulcls):
        appname = restfulcls.__module__.split('.')[-2] # Assume restful is in appdir.restful
        return '{0}_{1}'.format(appname, restfulcls.__name__.lower())

    def iter_restfulmanager_docs(self, restfulmanager):
        for restfulcls in restfulmanager.iter_restfulclasses():
            refprefix = self._get_restfulcls_docprefix(restfulcls)
            url = restfulcls.get_rest_url()
            indexitems = []
            for methodname in self.iter_restfulcls_methods(restfulcls):
                httpmethod, hasid, docs = self.CRUD_TO_HTTP[methodname]
                if hasid:
                    itemurl = '{0}id'.format(url)
                else:
                    itemurl = url
                indexitems.append(IndexItem(refprefix, methodname, httpmethod, itemurl,
                                            Docstring(docs, restfulcls, httpmethod, itemurl)))
            yield IndexPageItem(restfulcls, indexitems)

    def restfulmanager_docs_to_rstfiles(self, directory, indexpageitem):
        for page in indexpageitem.iterpages():
            page.write_to_dir(directory)

    def create_in_directory(self, directory, indexpageref, indextitle, restfulmanager):
        indexpageitems = []
        for indexpageitem in self.iter_restfulmanager_docs(restfulmanager):
            indexpageitems.append(indexpageitem)
            self.restfulmanager_docs_to_rstfiles(directory, indexpageitem)
        indexpage = IndexPage(indexpageitems, indexpageref, indextitle)
        indexpage.write(join(directory, 'index.rst'))
<|endoftext|>


<commit_before>"""Plugin config player."""
from mpf.core.config_player import ConfigPlayer

class PluginPlayer(ConfigPlayer):

    """Base class for a remote ConfigPlayer that is registered as a plug-in to MPF.

    This class is created on the MPF side of things.
    """

    def __repr__(self):
        """Return str representation."""
        return 'PluginPlayer.{}'.format(self.show_section)

    def get_express_config(self, value):
        """Not supported."""
        del value
        raise AssertionError("Plugin Player does not support express config")

    def register_player_events(self, config, mode=None, priority=0):
        """Register player events via BCP.

        Override this method in the base class and registers the
        config_player events to send the trigger via BCP instead of calling
        the local play() method.
        """
        event_list = list()

        for event in config:
            self.machine.bcp.add_registered_trigger_event(event)
            event_list.append(event)

        return event_list

    def unload_player_events(self, event_list):
        """Unload player events via BCP."""
        for event in event_list:
            self.machine.bcp.remove_registered_trigger_event(event)

    def play(self, settings, context, priority=0, **kwargs):
        """Trigger remote player via BCP."""
        self.machine.bcp.bcp_trigger(name='{}_play'.format(self.show_section),
                                     settings=settings, context=context,
                                     priority=priority)
        # todo do we need kwargs here? I think no?
<commit_msg>add BCP trigger for plugin_players::clear_context<commit_after>"""Plugin config player."""
from mpf.core.config_player import ConfigPlayer

class PluginPlayer(ConfigPlayer):

    """Base class for a remote ConfigPlayer that is registered as a plug-in to MPF.

    This class is created on the MPF side of things.
    """

    def __repr__(self):
        """Return str representation."""
        return 'PluginPlayer.{}'.format(self.show_section)

    def get_express_config(self, value):
        """Not supported."""
        del value
        raise AssertionError("Plugin Player does not support express config")

    def register_player_events(self, config, mode=None, priority=0):
        """Register player events via BCP.

        Override this method in the base class and registers the
        config_player events to send the trigger via BCP instead of calling
        the local play() method.
        """
        event_list = list()

        for event in config:
            self.machine.bcp.add_registered_trigger_event(event)
            event_list.append(event)

        return event_list

    def unload_player_events(self, event_list):
        """Unload player events via BCP."""
        for event in event_list:
            self.machine.bcp.remove_registered_trigger_event(event)

    def play(self, settings, context, priority=0, **kwargs):
        """Trigger remote player via BCP."""
        self.machine.bcp.bcp_trigger(name='{}_play'.format(self.show_section),
                                     settings=settings, context=context,
                                     priority=priority)

    def clear_context(self, context):
        """Clear the context at remote player via BCP."""
        self.machine.bcp.bcp_trigger(name='{}_clear'.format(self.show_section),
                                     context=context)
<|endoftext|>


<commit_before>import os
import sys
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "ms2ldaviz.settings")

import django
django.setup()

from decomposition.models import MotifSet,FeatureSet,GlobalFeature,GlobalMotifsToSets,Beta,FeatureMap
from basicviz.models import Alpha,Mass2MotifInstance,Experiment,Document,Mass2Motif,FeatureInstance

# Script to transform an experiment into a motifset for decomposition
if __name__ == '__main__':
    experiment_name = sys.argv[1]
    original_experiment = Experiment.objects.get(name = experiment_name)
    docs = Document.objects.filter(experiment = original_experiment)

    # Find out the featureset
    temp_doc = docs[0]
    fi = FeatureInstance.objects.filter(document = temp_doc)[0]
    bfs = fi.feature.featureset

    original_features = Feature.objects.filter(featureset = bfs)

    # Get the decomposition featureset - hardcoded
    fs = FeatureSet.objects.get_or_create(name='binned_005')[0]

    n_done = 0
    for localfeature in original_features:
      gf = GlobalFeature.objects.get_or_create(name = localfeature.name,
                                          min_mz = localfeature.min_mz,
                                          max_mz = localfeature.max_mz,
                                          featureset = fs)[0]
      FeatureMap.objects.get_or_create(localfeature = localfeature,
                                      globalfeature = gf)
      n_done += 1
      if n_done % 100 == 0:
          print n_done,len(original_features)

    # Create the motif links
    global_motifs = []
    original_motifs = Mass2Motif.objects.filter(experiment = original_experiment)
    for motif in massbank_motifs:
      gm = GlobalMotif.objects.get_or_create(originalmotif = motif)[0]
      global_motifs.append(gm)

    # Create the motifset and put the global motifs in it
    ms = MotifSet.objects.get_or_create(name = sys.argv[2],featureset = fs)[0]
    for gm in global_motifs:
      gms = GlobalMotifsToSets.objects.get_or_create(motif = gm,motifset = ms)

<commit_msg>added script to create motifset<commit_after>import os
import sys
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "ms2ldaviz.settings")

import django
django.setup()

from decomposition.models import MotifSet,FeatureSet,GlobalFeature,GlobalMotifsToSets,Beta,FeatureMap
from basicviz.models import Alpha,Mass2MotifInstance,Experiment,Document,Mass2Motif,FeatureInstance,Feature

# Script to transform an experiment into a motifset for decomposition
if __name__ == '__main__':
    experiment_name = sys.argv[1]
    original_experiment = Experiment.objects.get(name = experiment_name)
    docs = Document.objects.filter(experiment = original_experiment)

    # Find out the featureset
    temp_doc = docs[0]
    fi = FeatureInstance.objects.filter(document = temp_doc)[0]
    bfs = fi.feature.featureset

    original_features = Feature.objects.filter(featureset = bfs)

    # Get the decomposition featureset - hardcoded
    fs = FeatureSet.objects.get_or_create(name='binned_005')[0]

    n_done = 0
    for localfeature in original_features:
      gf = GlobalFeature.objects.get_or_create(name = localfeature.name,
                                          min_mz = localfeature.min_mz,
                                          max_mz = localfeature.max_mz,
                                          featureset = fs)[0]
      FeatureMap.objects.get_or_create(localfeature = localfeature,
                                      globalfeature = gf)
      n_done += 1
      if n_done % 100 == 0:
          print n_done,len(original_features)

    # Create the motif links
    global_motifs = []
    original_motifs = Mass2Motif.objects.filter(experiment = original_experiment)
    for motif in massbank_motifs:
      gm = GlobalMotif.objects.get_or_create(originalmotif = motif)[0]
      global_motifs.append(gm)

    # Create the motifset and put the global motifs in it
    ms = MotifSet.objects.get_or_create(name = sys.argv[2],featureset = fs)[0]
    for gm in global_motifs:
      gms = GlobalMotifsToSets.objects.get_or_create(motif = gm,motifset = ms)

<|endoftext|>


<commit_before># pylint: disable-msg=W0611, W0612, W0511,R0201
"""Tests suite for MaskedArray.
Adapted from the original test_ma by Pierre Gerard-Marchant

:author: Pierre Gerard-Marchant
:contact: pierregm_at_uga_dot_edu
:version: $Id$
"""
__author__ = "Pierre GF Gerard-Marchant ($Author$)"
__version__ = '1.0'
__revision__ = "$Revision$"
__date__     = '$Date$'

import numpy as N
from numpy import bool_, complex_, float_, int_, object_
import numpy.core.fromnumeric  as fromnumeric
import numpy.core.numeric as numeric
from numpy.testing import NumpyTest, NumpyTestCase
from numpy.testing.utils import build_err_msg

import maskedarray
import maskedarray as MA
from maskedarray import masked_array, masked, nomask

import maskedarray.testutils
from maskedarray.testutils import assert_equal, assert_array_equal

from timeseries import tseries
#reload(tseries)
from timeseries.tseries import Date, date_array_fromlist
from timeseries.tseries import time_series, TimeSeries, adjust_endpoints, mask_period

class test_creation(NumpyTestCase):
    "Base test class for MaskedArrays."
    def __init__(self, *args, **kwds):
        NumpyTestCase.__init__(self, *args, **kwds)
        dlist = ['2007-01-%02i' % i for i in range(1,16)]
        dates = date_array_fromlist(dlist)
        data = masked_array(numeric.arange(15), mask=[1,0,0,0,0]*3)
        self.d = (dlist, dates, data)

    def test_fromlist (self):
        "Base data definition."
        (dlist, dates, data) = self.d
        series = time_series(data, dlist)
        assert(isinstance(series, TimeSeries))
        assert_equal(series._mask, [1,0,0,0,0]*3)
        assert_equal(series._series, data)
        assert_equal(series._dates, date_array_fromlist(dlist))
        assert_equal(series.freq, 'D')

    def test_fromrange (self):
        "Base data definition."
        (dlist, dates, data) = self.d
        series = time_series(data, start_date=dates[0], length=15)
        assert(isinstance(series, TimeSeries))
        assert_equal(series._mask, [1,0,0,0,0]*3)
        assert_equal(series._series, data)
        assert_equal(series._dates, dates)
        assert_equal(series.freq, 'D')

    def test_fromseries (self):
        "Base data definition."
        (dlist, dates, data) = self.d
        series = time_series(data, dlist)
        dates = dates+15
        series = time_series(series, dates)
        assert(isinstance(series, TimeSeries))
        assert_equal(series._mask, [1,0,0,0,0]*3)
        assert_equal(series._series, data)
        assert_equal(series._dates, dates)
        assert_equal(series.freq, 'D')

    def test_fromdatearray(self):
        _, dates, _ = self.d
        data = dates.copy()
        data = dates

        series = time_series(data, dates)
        assert(isinstance(series, TimeSeries))
        assert_equal(series._dates, dates)
        assert_equal(series._data, data)
        assert_equal(series.freq, 'D')

        series[5] = MA.masked

        # ensure that series can be represented by a string after masking a value
        # (there was a bug before that prevented this from working when using a
        # DateArray for the data)
        strrep = str(series)

    def test_datafromlist(self):
        (_, dates, _) = self.d
        data = list(range(15))
        series = time_series(data, dates)
        assert_equal(series._data.size, 15)
#...............................................................................

class test_arithmetics(NumpyTestCase):
    "Some basic arithmetic tests"
    def __init__(self, *args, **kwds):
        NumpyTestCase.__init__(self, *args, **kwds)
        dlist = ['2007-01-%02i' % i for i in range(1,16)]
        dates = date_array_fromlist(dlist)
        data = masked_array(numeric.arange(15), mask=[1,0,0,0,0]*3)
        self.d = (time_series(data, dlist), data)

    def test_intfloat(self):
        "Test arithmetic timeseries/integers"
        (series, data) =self.d
        #
        nseries = series+1
        assert(isinstance(nseries, TimeSeries))
        assert_equal(nseries._mask, [1,0,0,0,0]*3)
        assert_equal(nseries._series, data+1)
        assert_equal(nseries._dates, series._dates)
        #
        nseries = series-1
        assert(isinstance(nseries, TimeSeries))
        assert_equal(nseries._mask, [1,0,0,0,0]*3)
        assert_equal(nseries._series, data-1)
        assert_equal(nseries._dates, series._dates)
        #
        nseries = series*1
        assert(isinstance(nseries, TimeSeries))
        assert_equal(nseries._mask, [1,0,0,0,0]*3)
        assert_equal(nseries._series, data*1)
        assert_equal(nseries._dates, series._dates)
        #
        nseries = series/1.
        assert(isinstance(nseries, TimeSeries))
        assert_equal(nseries._mask, [1,0,0,0,0]*3)
        assert_equal(nseries._series, data/1.)
        assert_equal(nseries._dates, series._dates)

    def test_intfloat_inplace(self):
        "Test int/float arithmetics in place."
        (series, data) =self.d
        nseries = series.astype(float_)
        idini = id(nseries)
        data = data.astype(float_)
        #
        nseries += 1.
        assert(isinstance(nseries, TimeSeries))
        assert_equal(nseries._mask, [1,0,0,0,0]*3)
        assert_equal(nseries._series, data+1.)
        assert_equal(nseries._dates, series._dates)
        assert_equal(id(nseries),idini)
        #
        nseries -= 1.
        assert(isinstance(nseries, TimeSeries))
        assert_equal(nseries._mask, [1,0,0,0,0]*3)
        assert_equal(nseries._series, data)
        assert_equal(nseries._dates, series._dates)
        assert_equal(id(nseries),idini)
        #
        nseries *= 2.
        assert(isinstance(nseries, TimeSeries))
        assert_equal(nseries._mask, [1,0,0,0,0]*3)
        assert_equal(nseries._series, data*2.)
        assert_equal(nseries._dates, series._dates)
        assert_equal(id(nseries),idini)
        #
        nseries /= 2.
        assert(isinstance(nseries, TimeSeries))
        assert_equal(nseries._mask, [1,0,0,0,0]*3)
        assert_equal(nseries._series, data)
        assert_equal(nseries._dates, series._dates)
        assert_equal(id(nseries),idini)
    #
    def test_updatemask(self):
        "Checks modification of mask."
        (series, data) =self.d
        assert_equal(series._mask, [1,0,0,0,0]*3)
        series.mask = nomask
        assert(series._mask is nomask)
        assert(series._series._mask is nomask)
        series._series.mask = [1,0,0]*5
        assert_equal(series._mask, [1,0,0]*5)
        assert_equal(series._series._mask, [1,0,0]*5)
        series[2] = masked
        assert_equal(series._mask, [1,0,1]+[1,0,0]*4)
        assert_equal(series._series._mask, [1,0,1]+[1,0,0]*4)
#...............................................................................

class test_getitem(NumpyTestCase):
    "Some getitem tests"
    def __init__(self, *args, **kwds):
        NumpyTestCase.__init__(self, *args, **kwds)
        dlist = ['2007-01-%02i' % i for i in range(1,16)]
        dates = date_array_fromlist(dlist)
        data = masked_array(numeric.arange(15), mask=[1,0,0,0,0]*3, dtype=float_)
        self.d = (time_series(data, dlist), data, dates)

    def test_wdate(self):
        "Tests  getitem with date as index"
        (series, data, dates) = self.d
        last_date = series[-1]._dates
        assert_equal(series[-1], series[last_date])
        assert_equal(series._dates[-1], dates[-1])
        assert_equal(series[-1]._dates, dates[-1])
        assert_equal(series[last_date]._dates, dates[-1])
        assert_equal(series._series[-1], data._data[-1])
        assert_equal(series[-1]._series, data._data[-1])
        assert_equal(series._mask[-1], data._mask[-1])
        #
        series['2007-01-06'] = 999
        assert_equal(series[5], 999)
        #
    def test_wtimeseries(self):
        "Tests getitem w/ TimeSeries as index"
        (series, data, dates) = self.d
        # Testing a basic condition on data
        cond = (series<8).filled(False)
        dseries = series[cond]
        assert_equal(dseries._data, [1,2,3,4,6,7])
        assert_equal(dseries._dates, series._dates[[1,2,3,4,6,7]])
        assert_equal(dseries._mask, nomask)
        # Testing a basic condition on dates
        series[series._dates < Date('D',string='2007-01-06')] = masked
        assert_equal(series[:5]._series._mask, [1,1,1,1,1])

    def test_wslices(self):
        "Test get/set items."
        (series, data, dates) = self.d
        # Basic slices
        assert_equal(series[3:7]._series._data, data[3:7]._data)
        assert_equal(series[3:7]._series._mask, data[3:7]._mask)
        assert_equal(series[3:7]._dates, dates[3:7])
        # Ditto
        assert_equal(series[:5]._series._data, data[:5]._data)
        assert_equal(series[:5]._series._mask, data[:5]._mask)
        assert_equal(series[:5]._dates, dates[:5])
        # With set
        series[:5] = 0
        assert_equal(series[:5]._series, [0,0,0,0,0])
        dseries = N.log(series)
        series[-5:] = dseries[-5:]
        assert_equal(series[-5:], dseries[-5:])
        # Now, using dates !
        dseries = series[series.dates[3]:series.dates[7]]
        assert_equal(dseries, series[3:7])

class test_functions(NumpyTestCase):
    "Some getitem tests"
    def __init__(self, *args, **kwds):
        NumpyTestCase.__init__(self, *args, **kwds)
        dlist = ['2007-01-%02i' % i for i in range(1,16)]
        dates = date_array_fromlist(dlist)
        data = masked_array(numeric.arange(15), mask=[1,0,0,0,0]*3)
        self.d = (time_series(data, dlist), data, dates)
    #
    def test_adjustendpoints(self):
        "Tests adjust_endpoints"
        (series, data, dates) = self.d
        dseries = adjust_endpoints(series, series.dates[0], series.dates[-1])
        assert_equal(dseries, series)
        dseries = adjust_endpoints(series, series.dates[3], series.dates[-3])
        assert_equal(dseries, series[3:-2])
        dseries = adjust_endpoints(series, end_date=Date('D', string='2007-01-31'))
        assert_equal(dseries.size, 31)
        assert_equal(dseries._mask, N.r_[series._mask, [1]*16])
        dseries = adjust_endpoints(series, end_date=Date('D', string='2007-01-06'))
        assert_equal(dseries.size, 6)
        assert_equal(dseries, series[:6])
        dseries = adjust_endpoints(series,
                                   start_date=Date('D', string='2007-01-06'),
                                   end_date=Date('D', string='2007-01-31'))
        assert_equal(dseries.size, 26)
        assert_equal(dseries._mask, N.r_[series._mask[5:], [1]*16])
    #
    def test_maskperiod(self):
        "Test mask_period"
        (series, data, dates) = self.d
        series.mask = nomask
        (start, end) = ('2007-01-06', '2007-01-12')
        mask = mask_period(series, start, end, inside=True, include_edges=True,
                           inplace=False)
        assert_equal(mask._mask, N.array([0,0,0,0,0,1,1,1,1,1,1,1,0,0,0]))
        mask = mask_period(series, start, end, inside=True, include_edges=False,
                           inplace=False)
        assert_equal(mask._mask, [0,0,0,0,0,0,1,1,1,1,1,0,0,0,0])
        mask = mask_period(series, start, end, inside=False, include_edges=True,
                           inplace=False)
        assert_equal(mask._mask, [1,1,1,1,1,1,0,0,0,0,0,1,1,1,1])
        mask = mask_period(series, start, end, inside=False, include_edges=False,
                           inplace=False)
        assert_equal(mask._mask, [1,1,1,1,1,0,0,0,0,0,0,0,1,1,1])
    #
    def pickling(self):
        "Tests pickling/unpickling"
        (series, data, dates) = self.d
        tmp = maskedarray.loads(series.dumps())
        assert_equal(tmp._data, series._data)
        assert_equal(tmp._dates, series._dates)
        assert_equal(tmp._mask, series._mask)

    def test_empty_timeseries(self):
        "Tests that empty TimeSeries are  handled properly"
        empty_ts = time_series([], freq='b')
        assert_array_equal(empty_ts, empty_ts + 1)
        assert_array_equal(empty_ts, empty_ts + empty_ts)
        assert_equal(empty_ts.start_date, None)
        assert_equal(empty_ts.end_date, None)

###############################################################################
#------------------------------------------------------------------------------
if __name__ == "__main__":
    NumpyTest().run()        <commit_msg>added tests for align_series (including a series of size zero also)<commit_after># pylint: disable-msg=W0611, W0612, W0511,R0201
"""Tests suite for MaskedArray.
Adapted from the original test_ma by Pierre Gerard-Marchant

:author: Pierre Gerard-Marchant
:contact: pierregm_at_uga_dot_edu
:version: $Id$
"""
__author__ = "Pierre GF Gerard-Marchant ($Author$)"
__version__ = '1.0'
__revision__ = "$Revision$"
__date__     = '$Date$'

import numpy as N
from numpy import bool_, complex_, float_, int_, object_
import numpy.core.fromnumeric  as fromnumeric
import numpy.core.numeric as numeric
from numpy.testing import NumpyTest, NumpyTestCase
from numpy.testing.utils import build_err_msg

import maskedarray
import maskedarray as MA
from maskedarray import masked_array, masked, nomask

import maskedarray.testutils
from maskedarray.testutils import assert_equal, assert_array_equal

from timeseries import tseries
#reload(tseries)
from timeseries.tseries import Date, date_array_fromlist
from timeseries.tseries import time_series, TimeSeries, adjust_endpoints, mask_period, align_series

class test_creation(NumpyTestCase):
    "Base test class for MaskedArrays."
    def __init__(self, *args, **kwds):
        NumpyTestCase.__init__(self, *args, **kwds)
        dlist = ['2007-01-%02i' % i for i in range(1,16)]
        dates = date_array_fromlist(dlist)
        data = masked_array(numeric.arange(15), mask=[1,0,0,0,0]*3)
        self.d = (dlist, dates, data)

    def test_fromlist (self):
        "Base data definition."
        (dlist, dates, data) = self.d
        series = time_series(data, dlist)
        assert(isinstance(series, TimeSeries))
        assert_equal(series._mask, [1,0,0,0,0]*3)
        assert_equal(series._series, data)
        assert_equal(series._dates, date_array_fromlist(dlist))
        assert_equal(series.freq, 'D')

    def test_fromrange (self):
        "Base data definition."
        (dlist, dates, data) = self.d
        series = time_series(data, start_date=dates[0], length=15)
        assert(isinstance(series, TimeSeries))
        assert_equal(series._mask, [1,0,0,0,0]*3)
        assert_equal(series._series, data)
        assert_equal(series._dates, dates)
        assert_equal(series.freq, 'D')

    def test_fromseries (self):
        "Base data definition."
        (dlist, dates, data) = self.d
        series = time_series(data, dlist)
        dates = dates+15
        series = time_series(series, dates)
        assert(isinstance(series, TimeSeries))
        assert_equal(series._mask, [1,0,0,0,0]*3)
        assert_equal(series._series, data)
        assert_equal(series._dates, dates)
        assert_equal(series.freq, 'D')

    def test_fromdatearray(self):
        _, dates, _ = self.d
        data = dates.copy()
        data = dates

        series = time_series(data, dates)
        assert(isinstance(series, TimeSeries))
        assert_equal(series._dates, dates)
        assert_equal(series._data, data)
        assert_equal(series.freq, 'D')

        series[5] = MA.masked

        # ensure that series can be represented by a string after masking a value
        # (there was a bug before that prevented this from working when using a
        # DateArray for the data)
        strrep = str(series)

    def test_datafromlist(self):
        (_, dates, _) = self.d
        data = list(range(15))
        series = time_series(data, dates)
        assert_equal(series._data.size, 15)
#...............................................................................

class test_arithmetics(NumpyTestCase):
    "Some basic arithmetic tests"
    def __init__(self, *args, **kwds):
        NumpyTestCase.__init__(self, *args, **kwds)
        dlist = ['2007-01-%02i' % i for i in range(1,16)]
        dates = date_array_fromlist(dlist)
        data = masked_array(numeric.arange(15), mask=[1,0,0,0,0]*3)
        self.d = (time_series(data, dlist), data)

    def test_intfloat(self):
        "Test arithmetic timeseries/integers"
        (series, data) =self.d
        #
        nseries = series+1
        assert(isinstance(nseries, TimeSeries))
        assert_equal(nseries._mask, [1,0,0,0,0]*3)
        assert_equal(nseries._series, data+1)
        assert_equal(nseries._dates, series._dates)
        #
        nseries = series-1
        assert(isinstance(nseries, TimeSeries))
        assert_equal(nseries._mask, [1,0,0,0,0]*3)
        assert_equal(nseries._series, data-1)
        assert_equal(nseries._dates, series._dates)
        #
        nseries = series*1
        assert(isinstance(nseries, TimeSeries))
        assert_equal(nseries._mask, [1,0,0,0,0]*3)
        assert_equal(nseries._series, data*1)
        assert_equal(nseries._dates, series._dates)
        #
        nseries = series/1.
        assert(isinstance(nseries, TimeSeries))
        assert_equal(nseries._mask, [1,0,0,0,0]*3)
        assert_equal(nseries._series, data/1.)
        assert_equal(nseries._dates, series._dates)

    def test_intfloat_inplace(self):
        "Test int/float arithmetics in place."
        (series, data) =self.d
        nseries = series.astype(float_)
        idini = id(nseries)
        data = data.astype(float_)
        #
        nseries += 1.
        assert(isinstance(nseries, TimeSeries))
        assert_equal(nseries._mask, [1,0,0,0,0]*3)
        assert_equal(nseries._series, data+1.)
        assert_equal(nseries._dates, series._dates)
        assert_equal(id(nseries),idini)
        #
        nseries -= 1.
        assert(isinstance(nseries, TimeSeries))
        assert_equal(nseries._mask, [1,0,0,0,0]*3)
        assert_equal(nseries._series, data)
        assert_equal(nseries._dates, series._dates)
        assert_equal(id(nseries),idini)
        #
        nseries *= 2.
        assert(isinstance(nseries, TimeSeries))
        assert_equal(nseries._mask, [1,0,0,0,0]*3)
        assert_equal(nseries._series, data*2.)
        assert_equal(nseries._dates, series._dates)
        assert_equal(id(nseries),idini)
        #
        nseries /= 2.
        assert(isinstance(nseries, TimeSeries))
        assert_equal(nseries._mask, [1,0,0,0,0]*3)
        assert_equal(nseries._series, data)
        assert_equal(nseries._dates, series._dates)
        assert_equal(id(nseries),idini)
    #
    def test_updatemask(self):
        "Checks modification of mask."
        (series, data) =self.d
        assert_equal(series._mask, [1,0,0,0,0]*3)
        series.mask = nomask
        assert(series._mask is nomask)
        assert(series._series._mask is nomask)
        series._series.mask = [1,0,0]*5
        assert_equal(series._mask, [1,0,0]*5)
        assert_equal(series._series._mask, [1,0,0]*5)
        series[2] = masked
        assert_equal(series._mask, [1,0,1]+[1,0,0]*4)
        assert_equal(series._series._mask, [1,0,1]+[1,0,0]*4)
#...............................................................................

class test_getitem(NumpyTestCase):
    "Some getitem tests"
    def __init__(self, *args, **kwds):
        NumpyTestCase.__init__(self, *args, **kwds)
        dlist = ['2007-01-%02i' % i for i in range(1,16)]
        dates = date_array_fromlist(dlist)
        data = masked_array(numeric.arange(15), mask=[1,0,0,0,0]*3, dtype=float_)
        self.d = (time_series(data, dlist), data, dates)

    def test_wdate(self):
        "Tests  getitem with date as index"
        (series, data, dates) = self.d
        last_date = series[-1]._dates
        assert_equal(series[-1], series[last_date])
        assert_equal(series._dates[-1], dates[-1])
        assert_equal(series[-1]._dates, dates[-1])
        assert_equal(series[last_date]._dates, dates[-1])
        assert_equal(series._series[-1], data._data[-1])
        assert_equal(series[-1]._series, data._data[-1])
        assert_equal(series._mask[-1], data._mask[-1])
        #
        series['2007-01-06'] = 999
        assert_equal(series[5], 999)
        #
    def test_wtimeseries(self):
        "Tests getitem w/ TimeSeries as index"
        (series, data, dates) = self.d
        # Testing a basic condition on data
        cond = (series<8).filled(False)
        dseries = series[cond]
        assert_equal(dseries._data, [1,2,3,4,6,7])
        assert_equal(dseries._dates, series._dates[[1,2,3,4,6,7]])
        assert_equal(dseries._mask, nomask)
        # Testing a basic condition on dates
        series[series._dates < Date('D',string='2007-01-06')] = masked
        assert_equal(series[:5]._series._mask, [1,1,1,1,1])

    def test_wslices(self):
        "Test get/set items."
        (series, data, dates) = self.d
        # Basic slices
        assert_equal(series[3:7]._series._data, data[3:7]._data)
        assert_equal(series[3:7]._series._mask, data[3:7]._mask)
        assert_equal(series[3:7]._dates, dates[3:7])
        # Ditto
        assert_equal(series[:5]._series._data, data[:5]._data)
        assert_equal(series[:5]._series._mask, data[:5]._mask)
        assert_equal(series[:5]._dates, dates[:5])
        # With set
        series[:5] = 0
        assert_equal(series[:5]._series, [0,0,0,0,0])
        dseries = N.log(series)
        series[-5:] = dseries[-5:]
        assert_equal(series[-5:], dseries[-5:])
        # Now, using dates !
        dseries = series[series.dates[3]:series.dates[7]]
        assert_equal(dseries, series[3:7])

class test_functions(NumpyTestCase):
    "Some getitem tests"
    def __init__(self, *args, **kwds):
        NumpyTestCase.__init__(self, *args, **kwds)
        dlist = ['2007-01-%02i' % i for i in range(1,16)]
        dates = date_array_fromlist(dlist)
        data = masked_array(numeric.arange(15), mask=[1,0,0,0,0]*3)
        self.d = (time_series(data, dlist), data, dates)
    #
    def test_adjustendpoints(self):
        "Tests adjust_endpoints"
        (series, data, dates) = self.d
        dseries = adjust_endpoints(series, series.dates[0], series.dates[-1])
        assert_equal(dseries, series)
        dseries = adjust_endpoints(series, series.dates[3], series.dates[-3])
        assert_equal(dseries, series[3:-2])
        dseries = adjust_endpoints(series, end_date=Date('D', string='2007-01-31'))
        assert_equal(dseries.size, 31)
        assert_equal(dseries._mask, N.r_[series._mask, [1]*16])
        dseries = adjust_endpoints(series, end_date=Date('D', string='2007-01-06'))
        assert_equal(dseries.size, 6)
        assert_equal(dseries, series[:6])
        dseries = adjust_endpoints(series,
                                   start_date=Date('D', string='2007-01-06'),
                                   end_date=Date('D', string='2007-01-31'))
        assert_equal(dseries.size, 26)
        assert_equal(dseries._mask, N.r_[series._mask[5:], [1]*16])

        empty_series = time_series([], freq='d')
        a, b = align_series(series, empty_series)
        assert_equal(a.start_date, b.start_date)
        assert_equal(a.end_date, b.end_date)

    #
    def test_maskperiod(self):
        "Test mask_period"
        (series, data, dates) = self.d
        series.mask = nomask
        (start, end) = ('2007-01-06', '2007-01-12')
        mask = mask_period(series, start, end, inside=True, include_edges=True,
                           inplace=False)
        assert_equal(mask._mask, N.array([0,0,0,0,0,1,1,1,1,1,1,1,0,0,0]))
        mask = mask_period(series, start, end, inside=True, include_edges=False,
                           inplace=False)
        assert_equal(mask._mask, [0,0,0,0,0,0,1,1,1,1,1,0,0,0,0])
        mask = mask_period(series, start, end, inside=False, include_edges=True,
                           inplace=False)
        assert_equal(mask._mask, [1,1,1,1,1,1,0,0,0,0,0,1,1,1,1])
        mask = mask_period(series, start, end, inside=False, include_edges=False,
                           inplace=False)
        assert_equal(mask._mask, [1,1,1,1,1,0,0,0,0,0,0,0,1,1,1])
    #
    def pickling(self):
        "Tests pickling/unpickling"
        (series, data, dates) = self.d
        tmp = maskedarray.loads(series.dumps())
        assert_equal(tmp._data, series._data)
        assert_equal(tmp._dates, series._dates)
        assert_equal(tmp._mask, series._mask)

    def test_empty_timeseries(self):
        "Tests that empty TimeSeries are  handled properly"
        empty_ts = time_series([], freq='b')
        assert_array_equal(empty_ts, empty_ts + 1)
        assert_array_equal(empty_ts, empty_ts + empty_ts)
        assert_equal(empty_ts.start_date, None)
        assert_equal(empty_ts.end_date, None)

###############################################################################
#------------------------------------------------------------------------------
if __name__ == "__main__":
    NumpyTest().run()        <|endoftext|>


<commit_before>import multiprocessing
import os
import signal
import time
import uuid
from dataclasses import dataclass, field
from datetime import datetime
from typing import Any, Dict, Optional, List, Union, Set, Callable

from hologram import JsonSchemaMixin
from hologram.helpers import StrEnum

import dbt.exceptions
from dbt.rpc.error import dbt_error
from dbt.rpc.task_handler import TaskHandlerState, RequestTaskHandler
from dbt.rpc.task import RemoteCallable, RemoteCallableResult

@dataclass
class TaskRow(JsonSchemaMixin):
    task_id: uuid.UUID
    request_id: Union[str, int]
    request_source: str
    method: str
    state: TaskHandlerState
    start: float
    elapsed: float
    timeout: Optional[float]

class KillResultStatus(StrEnum):
    Missing = 'missing'
    NotStarted = 'not_started'
    Killed = 'killed'
    Finished = 'finished'

@dataclass
class KillResult(JsonSchemaMixin):
    state: KillResultStatus

@dataclass
class PSResult(JsonSchemaMixin):
    rows: List[TaskRow]

class ManifestStatus(StrEnum):
    Init = 'init'
    Compiling = 'compiling'
    Ready = 'ready'
    Error = 'error'

@dataclass
class LastCompile(JsonSchemaMixin):
    status: ManifestStatus
    error: Optional[Dict[str, Any]] = None
    logs: Optional[List[Dict[str, Any]]] = None
    timestamp: datetime = field(default_factory=datetime.utcnow)
    pid: int = field(default_factory=os.getpid)

UnmanagedHandler = Callable[..., RemoteCallableResult]

class TaskManager:
    def __init__(self, args, config):
        self.args = args
        self.config = config
        self.tasks: Dict[uuid.UUID, RequestTaskHandler] = {}
        self.completed: Dict[uuid.UUID, RequestTaskHandler] = {}
        self._rpc_task_map = {}
        self._builtins: Dict[str, UnmanagedHandler] = {}
        self.last_compile = LastCompile(status=ManifestStatus.Init)
        self._lock = multiprocessing.Lock()

    def add_request(self, request_handler):
        self.tasks[request_handler.task_id] = request_handler

    def reserve_handler(self, task):
        self._rpc_task_map[task.METHOD_NAME] = None

    def add_task_handler(self, task, manifest):
        self._rpc_task_map[task.METHOD_NAME] = task(
            self.args, self.config, manifest
        )

    def rpc_task(self, method_name):
        with self._lock:
            return self._rpc_task_map[method_name]

    def ready(self):
        with self._lock:
            return self.last_compile.status == ManifestStatus.Ready

    def set_compiling(self):
        assert self.last_compile.status != ManifestStatus.Compiling, \
            f'invalid state {self.last_compile.status}'
        with self._lock:
            self.last_compile = LastCompile(status=ManifestStatus.Compiling)

    def set_compile_exception(self, exc, logs=List[Dict[str, Any]]):
        assert self.last_compile.status == ManifestStatus.Compiling, \
            f'invalid state {self.last_compile.status}'
        self.last_compile = LastCompile(
            error={'message': str(exc)},
            status=ManifestStatus.Error,
            logs=logs
        )

    def set_ready(self, logs=List[Dict[str, Any]]) -> None:
        assert self.last_compile.status == ManifestStatus.Compiling, \
            f'invalid state {self.last_compile.status}'
        self.last_compile = LastCompile(
            status=ManifestStatus.Ready,
            logs=logs
        )

    def process_status(self) -> Dict[str, Any]:
        with self._lock:
            last_compile = self.last_compile
        return last_compile.to_dict()

    def process_listing(self, active=True, completed=False) -> Dict[str, Any]:
        included_tasks = {}
        with self._lock:
            if completed:
                included_tasks.update(self.completed)
            if active:
                included_tasks.update(self.tasks)

        rows = []
        now = time.time()
        for task_handler in included_tasks.values():
            start = task_handler.started
            if start is not None:
                elapsed = now - start

            rows.append(TaskRow(
                task_handler.task_id, task_handler.request_id,
                task_handler.request_source, task_handler.method,
                task_handler.state, start, elapsed, task_handler.timeout
            ))
        rows.sort(key=lambda r: (r.state, r.start))
        result = PSResult(rows=rows)
        return result.to_dict(omit_none=False)

    def process_kill(self, task_id) -> Dict[str, Any]:

        task_id = uuid.UUID(task_id)

        status = KillResultStatus.Missing
        try:
            task = self.tasks[task_id]
        except KeyError:
            # nothing to do!
            return KillResult(status).to_dict()

        status = KillResultStatus.NotStarted

        if task.process is None:
            return KillResult(status).to_dict()
        pid = task.process.pid
        if pid is None:
            return KillResult(status).to_dict()

        if task.process.is_alive():
            os.kill(pid, signal.SIGINT)
            status = KillResultStatus.Killed
        else:
            status = KillResultStatus.Finished

        return KillResult(status).to_dict()

    def process_poll(self, task_id):
        pass

    def _rpc_builtins(self):
        if self._builtins:
            return self._builtins

        with self._lock:
            if self._builtins:  # handle a race
                return self._builtins

            methods = {
                'ps': self.process_listing,
                'status': self.process_status,
                'poll': self.process_poll,
            }
            if os.name != 'nt':
                methods['kill'] = self.process_kill

            self._builtins.update(methods)
            return self._builtins

    def compile_status(self):
        return self.last_compile.status

    def compile_status_handler(self, method_name):
        if method_name not in self._rpc_task_map:
            return None
        elif self.compile_status == ManifestStatus.Compiling:
            return self.process_currently_compiling
        elif self.compile_status == ManifestStatus.Error:
            return self.process_compilation_error
        else:
            return None

    def mark_done(self, request_handler):
        task_id = request_handler.task_id
        with self._lock:
            if task_id not in self.tasks:
                # lost a task! Maybe it was killed before it started.
                return
            self.completed[task_id] = self.tasks.pop(task_id)

    def methods(self, builtin=True) -> Set[str]:
        all_methods: Set[str] = set()
        if builtin:
            all_methods.update(self._rpc_builtins())

        with self._lock:
            all_methods.update(self._rpc_task_map)

        return all_methods

    def currently_compiling(self, *args, **kwargs):
        """Raise an RPC exception to trigger the error handler."""
        raise dbt_error(dbt.exceptions.RPCCompiling('compile in progress'))

    def compilation_error(self, *args, **kwargs):
        """Raise an RPC exception to trigger the error handler."""
        raise dbt_error(
            dbt.exceptions.RPCLoadException(self.last_compile.error)
        )

    def get_handler(
        self, method, http_request, json_rpc_request
    ) -> Optional[Union[UnmanagedHandler, RemoteCallable]]:
        # the dispatcher's keys are method names and its values are functions
        # that implement the RPC calls
        _builtins = self._rpc_builtins()
        if method in _builtins:
            return _builtins[method]
        elif method not in self._rpc_task_map:
            return None
        # if we have no manifest we want to return an error about why
        elif self.last_compile.status == ManifestStatus.Compiling:
            return self.currently_compiling
        elif self.last_compile.status == ManifestStatus.Error:
            return self.compilation_error
        else:
            return self.rpc_task(method)
<commit_msg>oops, we never use these (broken) methods<commit_after>import multiprocessing
import os
import signal
import time
import uuid
from dataclasses import dataclass, field
from datetime import datetime
from typing import Any, Dict, Optional, List, Union, Set, Callable

from hologram import JsonSchemaMixin
from hologram.helpers import StrEnum

import dbt.exceptions
from dbt.rpc.error import dbt_error
from dbt.rpc.task_handler import TaskHandlerState, RequestTaskHandler
from dbt.rpc.task import RemoteCallable, RemoteCallableResult

@dataclass
class TaskRow(JsonSchemaMixin):
    task_id: uuid.UUID
    request_id: Union[str, int]
    request_source: str
    method: str
    state: TaskHandlerState
    start: float
    elapsed: float
    timeout: Optional[float]

class KillResultStatus(StrEnum):
    Missing = 'missing'
    NotStarted = 'not_started'
    Killed = 'killed'
    Finished = 'finished'

@dataclass
class KillResult(JsonSchemaMixin):
    state: KillResultStatus

@dataclass
class PSResult(JsonSchemaMixin):
    rows: List[TaskRow]

class ManifestStatus(StrEnum):
    Init = 'init'
    Compiling = 'compiling'
    Ready = 'ready'
    Error = 'error'

@dataclass
class LastCompile(JsonSchemaMixin):
    status: ManifestStatus
    error: Optional[Dict[str, Any]] = None
    logs: Optional[List[Dict[str, Any]]] = None
    timestamp: datetime = field(default_factory=datetime.utcnow)
    pid: int = field(default_factory=os.getpid)

UnmanagedHandler = Callable[..., RemoteCallableResult]

class TaskManager:
    def __init__(self, args, config):
        self.args = args
        self.config = config
        self.tasks: Dict[uuid.UUID, RequestTaskHandler] = {}
        self.completed: Dict[uuid.UUID, RequestTaskHandler] = {}
        self._rpc_task_map = {}
        self._builtins: Dict[str, UnmanagedHandler] = {}
        self.last_compile = LastCompile(status=ManifestStatus.Init)
        self._lock = multiprocessing.Lock()

    def add_request(self, request_handler):
        self.tasks[request_handler.task_id] = request_handler

    def reserve_handler(self, task):
        self._rpc_task_map[task.METHOD_NAME] = None

    def add_task_handler(self, task, manifest):
        self._rpc_task_map[task.METHOD_NAME] = task(
            self.args, self.config, manifest
        )

    def rpc_task(self, method_name):
        with self._lock:
            return self._rpc_task_map[method_name]

    def ready(self):
        with self._lock:
            return self.last_compile.status == ManifestStatus.Ready

    def set_compiling(self):
        assert self.last_compile.status != ManifestStatus.Compiling, \
            f'invalid state {self.last_compile.status}'
        with self._lock:
            self.last_compile = LastCompile(status=ManifestStatus.Compiling)

    def set_compile_exception(self, exc, logs=List[Dict[str, Any]]):
        assert self.last_compile.status == ManifestStatus.Compiling, \
            f'invalid state {self.last_compile.status}'
        self.last_compile = LastCompile(
            error={'message': str(exc)},
            status=ManifestStatus.Error,
            logs=logs
        )

    def set_ready(self, logs=List[Dict[str, Any]]) -> None:
        assert self.last_compile.status == ManifestStatus.Compiling, \
            f'invalid state {self.last_compile.status}'
        self.last_compile = LastCompile(
            status=ManifestStatus.Ready,
            logs=logs
        )

    def process_status(self) -> Dict[str, Any]:
        with self._lock:
            last_compile = self.last_compile
        return last_compile.to_dict()

    def process_listing(self, active=True, completed=False) -> Dict[str, Any]:
        included_tasks = {}
        with self._lock:
            if completed:
                included_tasks.update(self.completed)
            if active:
                included_tasks.update(self.tasks)

        rows = []
        now = time.time()
        for task_handler in included_tasks.values():
            start = task_handler.started
            if start is not None:
                elapsed = now - start

            rows.append(TaskRow(
                task_handler.task_id, task_handler.request_id,
                task_handler.request_source, task_handler.method,
                task_handler.state, start, elapsed, task_handler.timeout
            ))
        rows.sort(key=lambda r: (r.state, r.start))
        result = PSResult(rows=rows)
        return result.to_dict(omit_none=False)

    def process_kill(self, task_id) -> Dict[str, Any]:

        task_id = uuid.UUID(task_id)

        status = KillResultStatus.Missing
        try:
            task = self.tasks[task_id]
        except KeyError:
            # nothing to do!
            return KillResult(status).to_dict()

        status = KillResultStatus.NotStarted

        if task.process is None:
            return KillResult(status).to_dict()
        pid = task.process.pid
        if pid is None:
            return KillResult(status).to_dict()

        if task.process.is_alive():
            os.kill(pid, signal.SIGINT)
            status = KillResultStatus.Killed
        else:
            status = KillResultStatus.Finished

        return KillResult(status).to_dict()

    def process_poll(self, task_id):
        pass

    def _rpc_builtins(self):
        if self._builtins:
            return self._builtins

        with self._lock:
            if self._builtins:  # handle a race
                return self._builtins

            methods = {
                'ps': self.process_listing,
                'status': self.process_status,
                'poll': self.process_poll,
            }
            if os.name != 'nt':
                methods['kill'] = self.process_kill

            self._builtins.update(methods)
            return self._builtins

    def mark_done(self, request_handler):
        task_id = request_handler.task_id
        with self._lock:
            if task_id not in self.tasks:
                # lost a task! Maybe it was killed before it started.
                return
            self.completed[task_id] = self.tasks.pop(task_id)

    def methods(self, builtin=True) -> Set[str]:
        all_methods: Set[str] = set()
        if builtin:
            all_methods.update(self._rpc_builtins())

        with self._lock:
            all_methods.update(self._rpc_task_map)

        return all_methods

    def currently_compiling(self, *args, **kwargs):
        """Raise an RPC exception to trigger the error handler."""
        raise dbt_error(dbt.exceptions.RPCCompiling('compile in progress'))

    def compilation_error(self, *args, **kwargs):
        """Raise an RPC exception to trigger the error handler."""
        raise dbt_error(
            dbt.exceptions.RPCLoadException(self.last_compile.error)
        )

    def get_handler(
        self, method, http_request, json_rpc_request
    ) -> Optional[Union[UnmanagedHandler, RemoteCallable]]:
        # the dispatcher's keys are method names and its values are functions
        # that implement the RPC calls
        _builtins = self._rpc_builtins()
        if method in _builtins:
            return _builtins[method]
        elif method not in self._rpc_task_map:
            return None
        # if we have no manifest we want to return an error about why
        elif self.last_compile.status == ManifestStatus.Compiling:
            return self.currently_compiling
        elif self.last_compile.status == ManifestStatus.Error:
            return self.compilation_error
        else:
            return self.rpc_task(method)
<|endoftext|>