<commit_before>#!/usr/bin/env python
"""

Build F90 module support for f2py2e.

Copyright 2000 Pearu Peterson all rights reserved,
Pearu Peterson <pearu@ioc.ee>
Permission to use, modify, and distribute this software is given under the
terms of the NumPy License.

NO WARRANTY IS EXPRESSED OR IMPLIED.  USE AT YOUR OWN RISK.
$Date: 2005/02/03 19:30:23 $
Pearu Peterson
"""

__version__ = "$Revision: 1.27 $"[10:-1]

f2py_version='See `f2py -v`'

import pprint.pprint as show
import sys.stderr.write as errmess
import sys.stdout.write as outmess
import time
import types
import copy

from auxfuncs import *
import numpy as np
import capi_maps
import cfuncs
import rules
import func2subr
from crackfortran import undo_rmbadname, undo_rmbadname1

options={}

def findf90modules(m):
    if ismodule(m): return [m]
    if not hasbody(m): return []
    ret = []
    for b in m['body']:
        if ismodule(b): ret.append(b)
        else: ret=ret+findf90modules(b)
    return ret

fgetdims1 = """\
      external f2pysetdata
      logical ns
      integer r,i,j
      integer(%d) s(*)
      ns = .FALSE.
      if (allocated(d)) then
         do i=1,r
            if ((size(d,i).ne.s(i)).and.(s(i).ge.0)) then
               ns = .TRUE.
            end if
         end do
         if (ns) then
            deallocate(d)
         end if
      end if
      if ((.not.allocated(d)).and.(s(1).ge.1)) then""" % np.intp().itemsize

fgetdims2="""\
      end if
      if (allocated(d)) then
         do i=1,r
            s(i) = size(d,i)
         end do
      end if
      flag = 1
      call f2pysetdata(d,allocated(d))"""

fgetdims2_sa="""\
      end if
      if (allocated(d)) then
         do i=1,r
            s(i) = size(d,i)
         end do
         !s(r) must be equal to len(d(1))
      end if
      flag = 2
      call f2pysetdata(d,allocated(d))"""

def buildhooks(pymod):
    global fgetdims1,fgetdims2
    ret = {'f90modhooks':[],'initf90modhooks':[],'body':[],
           'need':['F_FUNC','arrayobject.h'],
           'separatorsfor':{'includes0':'\n','includes':'\n'},
           'docs':['"Fortran 90/95 modules:\\n"'],
           'latexdoc':[]}
    fhooks=['']
    def fadd(line,s=fhooks): s[0] = '%s\n      %s'%(s[0],line)
    doc = ['']
    def dadd(line,s=doc): s[0] = '%s\n%s'%(s[0],line)
    for m in findf90modules(pymod):
        sargs,fargs,efargs,modobjs,notvars,onlyvars=[],[],[],[],[m['name']],[]
        sargsp = []
        ifargs = []
        mfargs = []
        if hasbody(m):
            for b in m['body']: notvars.append(b['name'])
        for n in m['vars'].keys():
            var = m['vars'][n]
            if (n not in notvars) and (not l_or(isintent_hide,isprivate)(var)):
                onlyvars.append(n)
                mfargs.append(n)
        outmess('\t\tConstructing F90 module support for "%s"...\n'%(m['name']))
        if onlyvars:
            outmess('\t\t  Variables: %s\n'%(' '.join(onlyvars)))
        chooks=['']
        def cadd(line,s=chooks): s[0] = '%s\n%s'%(s[0],line)
        ihooks=['']
        def iadd(line,s=ihooks): s[0] = '%s\n%s'%(s[0],line)

        vrd=capi_maps.modsign2map(m)
        cadd('static FortranDataDef f2py_%s_def[] = {'%(m['name']))
        dadd('\\subsection{Fortran 90/95 module \\texttt{%s}}\n'%(m['name']))
        if hasnote(m):
            note = m['note']
            if type(note) is type([]): note='\n'.join(note)
            dadd(note)
        if onlyvars:
            dadd('\\begin{description}')
        for n in onlyvars:
            var = m['vars'][n]
            modobjs.append(n)
            ct = capi_maps.getctype(var)
            at = capi_maps.c2capi_map[ct]
            dm = capi_maps.getarrdims(n,var)
            dms = dm['dims'].replace('*','-1').strip()
            dms = dms.replace(':','-1').strip()
            if not dms: dms='-1'
            use_fgetdims2 = fgetdims2
            if isstringarray(var):
                if 'charselector' in var and 'len' in var['charselector']:
                    cadd('\t{"%s",%s,{{%s,%s}},%s},'\
                         %(undo_rmbadname1(n),dm['rank'],dms,var['charselector']['len'],at))
                    use_fgetdims2 = fgetdims2_sa
                else:
                    cadd('\t{"%s",%s,{{%s}},%s},'%(undo_rmbadname1(n),dm['rank'],dms,at))
            else:
                cadd('\t{"%s",%s,{{%s}},%s},'%(undo_rmbadname1(n),dm['rank'],dms,at))
            dadd('\\item[]{{}\\verb@%s@{}}'%(capi_maps.getarrdocsign(n,var)))
            if hasnote(var):
                note = var['note']
                if type(note) is type([]): note='\n'.join(note)
                dadd('--- %s'%(note))
            if isallocatable(var):
                fargs.append('f2py_%s_getdims_%s'%(m['name'],n))
                efargs.append(fargs[-1])
                sargs.append('void (*%s)(int*,int*,void(*)(char*,int*),int*)'%(n))
                sargsp.append('void (*)(int*,int*,void(*)(char*,int*),int*)')
                iadd('\tf2py_%s_def[i_f2py++].func = %s;'%(m['name'],n))
                fadd('subroutine %s(r,s,f2pysetdata,flag)'%(fargs[-1]))
                fadd('use %s, only: d => %s\n'%(m['name'],undo_rmbadname1(n)))
                fadd('integer flag\n')
                fhooks[0]=fhooks[0]+fgetdims1
                dms = eval('range(1,%s+1)'%(dm['rank']))
                fadd(' allocate(d(%s))\n'%(','.join(map(lambda i:'s(%s)'%i,dms))))
                fhooks[0]=fhooks[0]+use_fgetdims2
                fadd('end subroutine %s'%(fargs[-1]))
            else:
                fargs.append(n)
                sargs.append('char *%s'%(n))
                sargsp.append('char*')
                iadd('\tf2py_%s_def[i_f2py++].data = %s;'%(m['name'],n))
        if onlyvars:
            dadd('\\end{description}')
        if hasbody(m):
            for b in m['body']:
                if not isroutine(b):
                    print 'Skipping',b['block'],b['name']
                    continue
                modobjs.append('%s()'%(b['name']))
                b['modulename'] = m['name']
                api,wrap=rules.buildapi(b)
                if isfunction(b):
                    fhooks[0]=fhooks[0]+wrap
                    fargs.append('f2pywrap_%s_%s'%(m['name'],b['name']))
                    #efargs.append(fargs[-1])
                    ifargs.append(func2subr.createfuncwrapper(b,signature=1))
                else:
                    fargs.append(b['name'])
                    mfargs.append(fargs[-1])
                    #if '--external-modroutines' in options and options['--external-modroutines']:
                    #    outmess('\t\t\tapplying --external-modroutines for %s\n'%(b['name']))
                    #     efargs.append(fargs[-1])
                api['externroutines']=[]
                ar=applyrules(api,vrd)
                ar['docs']=[]
                ar['docshort']=[]
                ret=dictappend(ret,ar)
                cadd('\t{"%s",-1,{{-1}},0,NULL,(void *)f2py_rout_#modulename#_%s_%s,doc_f2py_rout_#modulename#_%s_%s},'%(b['name'],m['name'],b['name'],m['name'],b['name']))
                sargs.append('char *%s'%(b['name']))
                sargsp.append('char *')
                iadd('\tf2py_%s_def[i_f2py++].data = %s;'%(m['name'],b['name']))
        cadd('\t{NULL}\n};\n')
        iadd('}')
        ihooks[0]='static void f2py_setup_%s(%s) {\n\tint i_f2py=0;%s'%(m['name'],','.join(sargs),ihooks[0])
        if '_' in m['name']:
            F_FUNC='F_FUNC_US'
        else:
            F_FUNC='F_FUNC'
        iadd('extern void %s(f2pyinit%s,F2PYINIT%s)(void (*)(%s));'\
             %(F_FUNC,m['name'],m['name'].upper(),','.join(sargsp)))
        iadd('static void f2py_init_%s(void) {'%(m['name']))
        iadd('\t%s(f2pyinit%s,F2PYINIT%s)(f2py_setup_%s);'\
             %(F_FUNC,m['name'],m['name'].upper(),m['name']))
        iadd('}\n')
        ret['f90modhooks']=ret['f90modhooks']+chooks+ihooks
        ret['initf90modhooks']=['\tPyDict_SetItemString(d, "%s", PyFortranObject_New(f2py_%s_def,f2py_init_%s));'%(m['name'],m['name'],m['name'])]+ret['initf90modhooks']
        fadd('')
        fadd('subroutine f2pyinit%s(f2pysetupfunc)'%(m['name']))
        #fadd('use %s'%(m['name']))
        if mfargs:
            for a in undo_rmbadname(mfargs):
                fadd('use %s, only : %s'%(m['name'],a))
        if ifargs:
            fadd(' '.join(['interface']+ifargs))
            fadd('end interface')
        fadd('external f2pysetupfunc')
        if efargs:
            for a in undo_rmbadname(efargs):
                fadd('external %s'%(a))
        fadd('call f2pysetupfunc(%s)'%(','.join(undo_rmbadname(fargs))))
        fadd('end subroutine f2pyinit%s\n'%(m['name']))

        dadd('\n'.join(ret['latexdoc']).replace(r'\subsection{',r'\subsubsection{'))

        ret['latexdoc']=[]
        ret['docs'].append('"\t%s --- %s"'%(m['name'],
                                            ','.join(undo_rmbadname(modobjs))))

    ret['routine_defs']=''
    ret['doc']=[]
    ret['docshort']=[]
    ret['latexdoc']=doc[0]
    if len(ret['docs'])<=1: ret['docs']=''
    return ret,fhooks[0]
<commit_msg>braino<commit_after>#!/usr/bin/env python
"""

Build F90 module support for f2py2e.

Copyright 2000 Pearu Peterson all rights reserved,
Pearu Peterson <pearu@ioc.ee>
Permission to use, modify, and distribute this software is given under the
terms of the NumPy License.

NO WARRANTY IS EXPRESSED OR IMPLIED.  USE AT YOUR OWN RISK.
$Date: 2005/02/03 19:30:23 $
Pearu Peterson
"""

__version__ = "$Revision: 1.27 $"[10:-1]

f2py_version='See `f2py -v`'

import copy
import pprint
import sys
import time
import types
errmess=sys.stderr.write
outmess=sys.stdout.write
show=pprint.pprint

from auxfuncs import *
import numpy as np
import capi_maps
import cfuncs
import rules
import func2subr
from crackfortran import undo_rmbadname, undo_rmbadname1

options={}

def findf90modules(m):
    if ismodule(m): return [m]
    if not hasbody(m): return []
    ret = []
    for b in m['body']:
        if ismodule(b): ret.append(b)
        else: ret=ret+findf90modules(b)
    return ret

fgetdims1 = """\
      external f2pysetdata
      logical ns
      integer r,i,j
      integer(%d) s(*)
      ns = .FALSE.
      if (allocated(d)) then
         do i=1,r
            if ((size(d,i).ne.s(i)).and.(s(i).ge.0)) then
               ns = .TRUE.
            end if
         end do
         if (ns) then
            deallocate(d)
         end if
      end if
      if ((.not.allocated(d)).and.(s(1).ge.1)) then""" % np.intp().itemsize

fgetdims2="""\
      end if
      if (allocated(d)) then
         do i=1,r
            s(i) = size(d,i)
         end do
      end if
      flag = 1
      call f2pysetdata(d,allocated(d))"""

fgetdims2_sa="""\
      end if
      if (allocated(d)) then
         do i=1,r
            s(i) = size(d,i)
         end do
         !s(r) must be equal to len(d(1))
      end if
      flag = 2
      call f2pysetdata(d,allocated(d))"""

def buildhooks(pymod):
    global fgetdims1,fgetdims2
    ret = {'f90modhooks':[],'initf90modhooks':[],'body':[],
           'need':['F_FUNC','arrayobject.h'],
           'separatorsfor':{'includes0':'\n','includes':'\n'},
           'docs':['"Fortran 90/95 modules:\\n"'],
           'latexdoc':[]}
    fhooks=['']
    def fadd(line,s=fhooks): s[0] = '%s\n      %s'%(s[0],line)
    doc = ['']
    def dadd(line,s=doc): s[0] = '%s\n%s'%(s[0],line)
    for m in findf90modules(pymod):
        sargs,fargs,efargs,modobjs,notvars,onlyvars=[],[],[],[],[m['name']],[]
        sargsp = []
        ifargs = []
        mfargs = []
        if hasbody(m):
            for b in m['body']: notvars.append(b['name'])
        for n in m['vars'].keys():
            var = m['vars'][n]
            if (n not in notvars) and (not l_or(isintent_hide,isprivate)(var)):
                onlyvars.append(n)
                mfargs.append(n)
        outmess('\t\tConstructing F90 module support for "%s"...\n'%(m['name']))
        if onlyvars:
            outmess('\t\t  Variables: %s\n'%(' '.join(onlyvars)))
        chooks=['']
        def cadd(line,s=chooks): s[0] = '%s\n%s'%(s[0],line)
        ihooks=['']
        def iadd(line,s=ihooks): s[0] = '%s\n%s'%(s[0],line)

        vrd=capi_maps.modsign2map(m)
        cadd('static FortranDataDef f2py_%s_def[] = {'%(m['name']))
        dadd('\\subsection{Fortran 90/95 module \\texttt{%s}}\n'%(m['name']))
        if hasnote(m):
            note = m['note']
            if type(note) is type([]): note='\n'.join(note)
            dadd(note)
        if onlyvars:
            dadd('\\begin{description}')
        for n in onlyvars:
            var = m['vars'][n]
            modobjs.append(n)
            ct = capi_maps.getctype(var)
            at = capi_maps.c2capi_map[ct]
            dm = capi_maps.getarrdims(n,var)
            dms = dm['dims'].replace('*','-1').strip()
            dms = dms.replace(':','-1').strip()
            if not dms: dms='-1'
            use_fgetdims2 = fgetdims2
            if isstringarray(var):
                if 'charselector' in var and 'len' in var['charselector']:
                    cadd('\t{"%s",%s,{{%s,%s}},%s},'\
                         %(undo_rmbadname1(n),dm['rank'],dms,var['charselector']['len'],at))
                    use_fgetdims2 = fgetdims2_sa
                else:
                    cadd('\t{"%s",%s,{{%s}},%s},'%(undo_rmbadname1(n),dm['rank'],dms,at))
            else:
                cadd('\t{"%s",%s,{{%s}},%s},'%(undo_rmbadname1(n),dm['rank'],dms,at))
            dadd('\\item[]{{}\\verb@%s@{}}'%(capi_maps.getarrdocsign(n,var)))
            if hasnote(var):
                note = var['note']
                if type(note) is type([]): note='\n'.join(note)
                dadd('--- %s'%(note))
            if isallocatable(var):
                fargs.append('f2py_%s_getdims_%s'%(m['name'],n))
                efargs.append(fargs[-1])
                sargs.append('void (*%s)(int*,int*,void(*)(char*,int*),int*)'%(n))
                sargsp.append('void (*)(int*,int*,void(*)(char*,int*),int*)')
                iadd('\tf2py_%s_def[i_f2py++].func = %s;'%(m['name'],n))
                fadd('subroutine %s(r,s,f2pysetdata,flag)'%(fargs[-1]))
                fadd('use %s, only: d => %s\n'%(m['name'],undo_rmbadname1(n)))
                fadd('integer flag\n')
                fhooks[0]=fhooks[0]+fgetdims1
                dms = eval('range(1,%s+1)'%(dm['rank']))
                fadd(' allocate(d(%s))\n'%(','.join(map(lambda i:'s(%s)'%i,dms))))
                fhooks[0]=fhooks[0]+use_fgetdims2
                fadd('end subroutine %s'%(fargs[-1]))
            else:
                fargs.append(n)
                sargs.append('char *%s'%(n))
                sargsp.append('char*')
                iadd('\tf2py_%s_def[i_f2py++].data = %s;'%(m['name'],n))
        if onlyvars:
            dadd('\\end{description}')
        if hasbody(m):
            for b in m['body']:
                if not isroutine(b):
                    print 'Skipping',b['block'],b['name']
                    continue
                modobjs.append('%s()'%(b['name']))
                b['modulename'] = m['name']
                api,wrap=rules.buildapi(b)
                if isfunction(b):
                    fhooks[0]=fhooks[0]+wrap
                    fargs.append('f2pywrap_%s_%s'%(m['name'],b['name']))
                    #efargs.append(fargs[-1])
                    ifargs.append(func2subr.createfuncwrapper(b,signature=1))
                else:
                    fargs.append(b['name'])
                    mfargs.append(fargs[-1])
                    #if '--external-modroutines' in options and options['--external-modroutines']:
                    #    outmess('\t\t\tapplying --external-modroutines for %s\n'%(b['name']))
                    #     efargs.append(fargs[-1])
                api['externroutines']=[]
                ar=applyrules(api,vrd)
                ar['docs']=[]
                ar['docshort']=[]
                ret=dictappend(ret,ar)
                cadd('\t{"%s",-1,{{-1}},0,NULL,(void *)f2py_rout_#modulename#_%s_%s,doc_f2py_rout_#modulename#_%s_%s},'%(b['name'],m['name'],b['name'],m['name'],b['name']))
                sargs.append('char *%s'%(b['name']))
                sargsp.append('char *')
                iadd('\tf2py_%s_def[i_f2py++].data = %s;'%(m['name'],b['name']))
        cadd('\t{NULL}\n};\n')
        iadd('}')
        ihooks[0]='static void f2py_setup_%s(%s) {\n\tint i_f2py=0;%s'%(m['name'],','.join(sargs),ihooks[0])
        if '_' in m['name']:
            F_FUNC='F_FUNC_US'
        else:
            F_FUNC='F_FUNC'
        iadd('extern void %s(f2pyinit%s,F2PYINIT%s)(void (*)(%s));'\
             %(F_FUNC,m['name'],m['name'].upper(),','.join(sargsp)))
        iadd('static void f2py_init_%s(void) {'%(m['name']))
        iadd('\t%s(f2pyinit%s,F2PYINIT%s)(f2py_setup_%s);'\
             %(F_FUNC,m['name'],m['name'].upper(),m['name']))
        iadd('}\n')
        ret['f90modhooks']=ret['f90modhooks']+chooks+ihooks
        ret['initf90modhooks']=['\tPyDict_SetItemString(d, "%s", PyFortranObject_New(f2py_%s_def,f2py_init_%s));'%(m['name'],m['name'],m['name'])]+ret['initf90modhooks']
        fadd('')
        fadd('subroutine f2pyinit%s(f2pysetupfunc)'%(m['name']))
        #fadd('use %s'%(m['name']))
        if mfargs:
            for a in undo_rmbadname(mfargs):
                fadd('use %s, only : %s'%(m['name'],a))
        if ifargs:
            fadd(' '.join(['interface']+ifargs))
            fadd('end interface')
        fadd('external f2pysetupfunc')
        if efargs:
            for a in undo_rmbadname(efargs):
                fadd('external %s'%(a))
        fadd('call f2pysetupfunc(%s)'%(','.join(undo_rmbadname(fargs))))
        fadd('end subroutine f2pyinit%s\n'%(m['name']))

        dadd('\n'.join(ret['latexdoc']).replace(r'\subsection{',r'\subsubsection{'))

        ret['latexdoc']=[]
        ret['docs'].append('"\t%s --- %s"'%(m['name'],
                                            ','.join(undo_rmbadname(modobjs))))

    ret['routine_defs']=''
    ret['doc']=[]
    ret['docshort']=[]
    ret['latexdoc']=doc[0]
    if len(ret['docs'])<=1: ret['docs']=''
    return ret,fhooks[0]
<|endoftext|>


<commit_before># Copyright 2016 Mycroft AI, Inc.
#
# This file is part of Mycroft Core.
#
# Mycroft Core is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Mycroft Core is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Mycroft Core.  If not, see <http://www.gnu.org/licenses/>.

import subprocess
import sys
from threading import Thread, Lock
import re

from mycroft.client.speech.listener import RecognizerLoop
from mycroft.configuration import ConfigurationManager
from mycroft.messagebus.client.ws import WebsocketClient
from mycroft.messagebus.message import Message
from mycroft.tts import tts_factory
from mycroft.util.log import getLogger
from mycroft.util import kill, connected

logger = getLogger("SpeechClient")
client = None
tts = tts_factory.create()
mutex = Lock()
loop = None

config = ConfigurationManager.get()

def handle_record_begin():
    logger.info("Begin Recording...")
    client.emit(Message('recognizer_loop:record_begin'))

def handle_record_end():
    logger.info("End Recording...")
    client.emit(Message('recognizer_loop:record_end'))

def handle_wakeword(event):
    logger.info("Wakeword Detected: " + event['utterance'])
    client.emit(Message('recognizer_loop:wakeword', event))

def handle_utterance(event):
    logger.info("Utterance: " + str(event['utterances']))
    client.emit(Message('recognizer_loop:utterance', event))

def mute_and_speak(utterance):
    mutex.acquire()
    client.emit(Message("recognizer_loop:audio_output_start"))
    try:
        logger.info("Speak: " + utterance)
        loop.mute()
        tts.execute(utterance)
    finally:
        loop.unmute()
        mutex.release()
        client.emit(Message("recognizer_loop:audio_output_end"))

def handle_multi_utterance_intent_failure(event):
    logger.info("Failed to find intent on multiple intents.")
    # TODO: Localize
    mute_and_speak("Sorry, I didn't catch that. Please rephrase your request.")

def handle_speak(event):
    utterance = event.metadata['utterance']
    chunks = re.split(r'(?<!\w\.\w.)(?<![A-Z][a-z]\.)(?<=\.|\?)\s', utterance)
    for chunk in chunks:
        mute_and_speak(chunk)

def handle_sleep(event):
    loop.sleep()

def handle_wake_up(event):
    loop.awaken()

def handle_stop(event):
    kill([config.get('tts').get('module')])

def connect():
    client.run_forever()

def main():
    global client
    global loop
    client = WebsocketClient()
    device_index = config.get('speech_client').get('device_index')
    if device_index:
        device_index = int(device_index)
    loop = RecognizerLoop(device_index=device_index)
    loop.on('recognizer_loop:utterance', handle_utterance)
    loop.on('recognizer_loop:record_begin', handle_record_begin)
    loop.on('recognizer_loop:wakeword', handle_wakeword)
    loop.on('recognizer_loop:record_end', handle_record_end)
    loop.on('speak', handle_speak)
    client.on('speak', handle_speak)
    client.on(
        'multi_utterance_intent_failure',
        handle_multi_utterance_intent_failure)
    client.on('recognizer_loop:sleep', handle_sleep)
    client.on('recognizer_loop:wake_up', handle_wake_up)
    client.on('mycroft.stop', handle_stop)
    event_thread = Thread(target=connect)
    event_thread.setDaemon(True)
    event_thread.start()

    subprocess.call('echo "eyes.reset" >/dev/ttyAMA0', shell=True)

    try:
        loop.run()
    except KeyboardInterrupt, e:
        logger.exception(e)
        event_thread.exit()
        sys.exit()

if __name__ == "__main__":
    main()
<commit_msg>Fixed issue #334 with a try: clause<commit_after># Copyright 2016 Mycroft AI, Inc.
#
# This file is part of Mycroft Core.
#
# Mycroft Core is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Mycroft Core is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Mycroft Core.  If not, see <http://www.gnu.org/licenses/>.

import subprocess
import sys
from threading import Thread, Lock
import re

from mycroft.client.speech.listener import RecognizerLoop
from mycroft.configuration import ConfigurationManager
from mycroft.messagebus.client.ws import WebsocketClient
from mycroft.messagebus.message import Message
from mycroft.tts import tts_factory
from mycroft.util.log import getLogger
from mycroft.util import kill, connected

logger = getLogger("SpeechClient")
client = None
tts = tts_factory.create()
mutex = Lock()
loop = None

config = ConfigurationManager.get()

def handle_record_begin():
    logger.info("Begin Recording...")
    client.emit(Message('recognizer_loop:record_begin'))

def handle_record_end():
    logger.info("End Recording...")
    client.emit(Message('recognizer_loop:record_end'))

def handle_wakeword(event):
    logger.info("Wakeword Detected: " + event['utterance'])
    client.emit(Message('recognizer_loop:wakeword', event))

def handle_utterance(event):
    logger.info("Utterance: " + str(event['utterances']))
    client.emit(Message('recognizer_loop:utterance', event))

def mute_and_speak(utterance):
    mutex.acquire()
    client.emit(Message("recognizer_loop:audio_output_start"))
    try:
        logger.info("Speak: " + utterance)
        loop.mute()
        tts.execute(utterance)
    finally:
        loop.unmute()
        mutex.release()
        client.emit(Message("recognizer_loop:audio_output_end"))

def handle_multi_utterance_intent_failure(event):
    logger.info("Failed to find intent on multiple intents.")
    # TODO: Localize
    mute_and_speak("Sorry, I didn't catch that. Please rephrase your request.")

def handle_speak(event):
    utterance = event.metadata['utterance']
    chunks = re.split(r'(?<!\w\.\w.)(?<![A-Z][a-z]\.)(?<=\.|\?)\s', utterance)
    for chunk in chunks:
        mute_and_speak(chunk)

def handle_sleep(event):
    loop.sleep()

def handle_wake_up(event):
    loop.awaken()

def handle_stop(event):
    kill([config.get('tts').get('module')])

def connect():
    client.run_forever()

def main():
    global client
    global loop
    client = WebsocketClient()
    device_index = config.get('speech_client').get('device_index')
    if device_index:
        device_index = int(device_index)
    loop = RecognizerLoop(device_index=device_index)
    loop.on('recognizer_loop:utterance', handle_utterance)
    loop.on('recognizer_loop:record_begin', handle_record_begin)
    loop.on('recognizer_loop:wakeword', handle_wakeword)
    loop.on('recognizer_loop:record_end', handle_record_end)
    loop.on('speak', handle_speak)
    client.on('speak', handle_speak)
    client.on(
        'multi_utterance_intent_failure',
        handle_multi_utterance_intent_failure)
    client.on('recognizer_loop:sleep', handle_sleep)
    client.on('recognizer_loop:wake_up', handle_wake_up)
    client.on('mycroft.stop', handle_stop)
    event_thread = Thread(target=connect)
    event_thread.setDaemon(True)
    event_thread.start()

    try:
        subprocess.call('echo "eyes.reset" >/dev/ttyAMA0', shell=True)
    except:
        pass

    try:
        loop.run()
    except KeyboardInterrupt, e:
        logger.exception(e)
        event_thread.exit()
        sys.exit()

if __name__ == "__main__":
    main()
<|endoftext|>


<commit_before>"""Test fixtures."""

import lxml
import pytest
from napalm_base.test import conftest as parent_conftest

from napalm_base.test.double import BaseTestDouble

from napalm_junos import junos

@pytest.fixture(scope='class')
def set_device_parameters(request):
    """Set up the class."""
    def fin():
        request.cls.device.close()
    request.addfinalizer(fin)

    request.cls.driver = junos.JunOSDriver
    request.cls.patched_driver = PatchedJunOSDriver
    request.cls.vendor = 'junos'
    parent_conftest.set_device_parameters(request)

def pytest_generate_tests(metafunc):
    """Generate test cases dynamically."""
    parent_conftest.pytest_generate_tests(metafunc, __file__)

class PatchedJunOSDriver(junos.JunOSDriver):
    """Patched JunOS Driver."""

    def __init__(self, hostname, username, password, timeout=60, optional_args=None):
        super(self.__class__, self).__init__(hostname, username, password, timeout, optional_args)

        self.patched_attrs = ['device']
        self.device = FakeJunOSDevice()

    def lock(self):
        pass

    def unlock(self):
        pass

    def is_alive(self):
        return {
            'is_alive': True  # always alive during the tests...
        }

class FakeJunOSDevice(BaseTestDouble):

    def __init__(self):
        self.rpc = FakeRPCObject(self)
        self._conn = FakeConnection(self.rpc)
        self.ON_JUNOS = True  # necessary for fake devices
        self.facts = {
            'domain': None,
            'hostname': 'vsrx',
            'ifd_style': 'CLASSIC',
            '2RE': False,
            'serialnumber': 'beb914a9cca3',
            'fqdn': 'vsrx',
            'virtual': True,
            'switch_style': 'NONE',
            'version': '12.1X47-D20.7',
            'HOME': '/cf/var/home/vagrant',
            'srx_cluster': False,
            'model': 'FIREFLY-PERIMETER',
            'RE0': {
                'status': 'Testing',
                'last_reboot_reason': 'Router rebooted after a normal shutdown.',
                'model': 'FIREFLY-PERIMETER RE',
                'up_time': '1 hour, 13 minutes, 37 seconds'
            },
            'vc_capable': False,
            'personality': 'SRX_BRANCH'
        }

    def open(self):
        pass

    def close(self):
        pass

    def bind(*args, **kvargs):
        pass

    def cli(self, command=''):
        filename = '{safe_command}.txt'.format(safe_command=self.sanitize_text(command))
        fielpath = self.find_file(filename)
        return self.read_txt_file(fielpath)

class FakeRPCObject:

    """
    Fake RPC caller.
    """

    def __init__(self, device):
        self._device = device

    def __getattr__(self, item):
        self.item = item
        return self

    def response(self, **rpc_args):
        instance = rpc_args.pop('instance', '')

        filename = '{item}{instance}.xml'.format(
            item=self.item, instance=instance
        )
        filepathpath = self._device.find_file(filename)
        xml_string = self._device.read_txt_file(filepathpath)

        return lxml.etree.fromstring(xml_string)

    def get_config(self, get_cmd=None, filter_xml=None, options={}):

        # get_cmd is an XML tree that requests a specific part of the config
        # E.g.: <configuration><protocols><bgp><group/></bgp></protocols></configuration>

        if get_cmd is not None:
            get_cmd_str = lxml.etree.tostring(get_cmd)
            filename = self._device.sanitize_text(get_cmd_str)

        # no get_cmd means it should mock the eznc get_config
        else:
            filename = 'get_config__' + '__'.join(
                ['{0}_{1}'.format(k, v) for k, v in options.items()]
            )

        filename = '{filename}.xml'.format(filename=filename[0:150])
        filepathpath = self._device.find_file(filename)
        xml_string = self._device.read_txt_file(filepathpath)

        return lxml.etree.fromstring(xml_string)

    __call__ = response

class FakeConnectionRPCObject:

    """
    Will make fake RPC requests that usually are directly made via netconf.
    """

    def __init__(self, rpc):
        self._rpc = rpc

    def response(self, non_std_command=None):
        class RPCReply:
            def __init__(self, reply):
                self._NCElement__doc = reply
        rpc_reply = RPCReply(self._rpc.get_config(get_cmd=non_std_command))
        return rpc_reply

    __call__ = response

class FakeConnection:

    def __init__(self, rpc):
        self.rpc = FakeConnectionRPCObject(rpc)
<commit_msg>Remove hardcoded lock and unlock<commit_after>"""Test fixtures."""

import lxml
import pytest
from napalm_base.test import conftest as parent_conftest

from napalm_base.test.double import BaseTestDouble

from napalm_junos import junos

@pytest.fixture(scope='class')
def set_device_parameters(request):
    """Set up the class."""
    def fin():
        request.cls.device.close()
    request.addfinalizer(fin)

    request.cls.driver = junos.JunOSDriver
    request.cls.patched_driver = PatchedJunOSDriver
    request.cls.vendor = 'junos'
    parent_conftest.set_device_parameters(request)

def pytest_generate_tests(metafunc):
    """Generate test cases dynamically."""
    parent_conftest.pytest_generate_tests(metafunc, __file__)

class PatchedJunOSDriver(junos.JunOSDriver):
    """Patched JunOS Driver."""

    def __init__(self, hostname, username, password, timeout=60, optional_args=None):
        optional_args['config_lock'] = False  # to not try lock on open()
        super(self.__class__, self).__init__(hostname, username, password, timeout, optional_args)

        self.patched_attrs = ['device']
        self.device = FakeJunOSDevice()

    def is_alive(self):
        return {
            'is_alive': True  # always alive during the tests...
        }

class FakeJunOSDevice(BaseTestDouble):

    def __init__(self):
        self.rpc = FakeRPCObject(self)
        self._conn = FakeConnection(self.rpc)
        self.ON_JUNOS = True  # necessary for fake devices
        self.facts = {
            'domain': None,
            'hostname': 'vsrx',
            'ifd_style': 'CLASSIC',
            '2RE': False,
            'serialnumber': 'beb914a9cca3',
            'fqdn': 'vsrx',
            'virtual': True,
            'switch_style': 'NONE',
            'version': '12.1X47-D20.7',
            'HOME': '/cf/var/home/vagrant',
            'srx_cluster': False,
            'model': 'FIREFLY-PERIMETER',
            'RE0': {
                'status': 'Testing',
                'last_reboot_reason': 'Router rebooted after a normal shutdown.',
                'model': 'FIREFLY-PERIMETER RE',
                'up_time': '1 hour, 13 minutes, 37 seconds'
            },
            'vc_capable': False,
            'personality': 'SRX_BRANCH'
        }

    def open(self):
        pass

    def close(self):
        pass

    def bind(*args, **kvargs):
        pass

    def cli(self, command=''):
        filename = '{safe_command}.txt'.format(safe_command=self.sanitize_text(command))
        fielpath = self.find_file(filename)
        return self.read_txt_file(fielpath)

class FakeRPCObject:

    """
    Fake RPC caller.
    """

    def __init__(self, device):
        self._device = device

    def __getattr__(self, item):
        self.item = item
        return self

    def response(self, **rpc_args):
        instance = rpc_args.pop('instance', '')

        filename = '{item}{instance}.xml'.format(
            item=self.item, instance=instance
        )
        filepathpath = self._device.find_file(filename)
        xml_string = self._device.read_txt_file(filepathpath)

        return lxml.etree.fromstring(xml_string)

    def get_config(self, get_cmd=None, filter_xml=None, options={}):

        # get_cmd is an XML tree that requests a specific part of the config
        # E.g.: <configuration><protocols><bgp><group/></bgp></protocols></configuration>

        if get_cmd is not None:
            get_cmd_str = lxml.etree.tostring(get_cmd)
            filename = self._device.sanitize_text(get_cmd_str)

        # no get_cmd means it should mock the eznc get_config
        else:
            filename = 'get_config__' + '__'.join(
                ['{0}_{1}'.format(k, v) for k, v in options.items()]
            )

        filename = '{filename}.xml'.format(filename=filename[0:150])
        filepathpath = self._device.find_file(filename)
        xml_string = self._device.read_txt_file(filepathpath)

        return lxml.etree.fromstring(xml_string)

    __call__ = response

class FakeConnectionRPCObject:

    """
    Will make fake RPC requests that usually are directly made via netconf.
    """

    def __init__(self, rpc):
        self._rpc = rpc

    def response(self, non_std_command=None):
        class RPCReply:
            def __init__(self, reply):
                self._NCElement__doc = reply
        rpc_reply = RPCReply(self._rpc.get_config(get_cmd=non_std_command))
        return rpc_reply

    __call__ = response

class FakeConnection:

    def __init__(self, rpc):
        self.rpc = FakeConnectionRPCObject(rpc)
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
"""
Created on Mon Nov 24 21:23:28 2014

@author: polbaladas
"""

import numpy as np
import matplotlib.pyplot as plt

alpha = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q',
'r','s','t','u','v','w','x','y','z']

number = ['1','2','3','4','5','6','7','8','9','0']

def frequency(text, plot_bool):
    c = text
    letters = []
    letters_count = [0]*27
    numbers = []
    numbers_count = [None]*27
    i = 0

    while i<len(alpha):
        letters_count[i] = c.count(alpha[i])
        i+=1

    i = 0
    while i<len(number):
        numbers_count[i] = c.count(number[i])
        i+=1

    if(plot_bool):
        fig = plt.figure()
        width = .35
        ind = np.arange(len(letters_count))
        plt.bar(ind, letters_count)
        plt.xticks(ind + width/2, alpha)
        return(letters_count)
    else:
        return(letters_count)

<commit_msg>Added Caps analyse<commit_after># -*- coding: utf-8 -*-
"""
Created on Mon Nov 24 21:23:28 2014

@author: polbaladas
"""

import numpy as np
import matplotlib.pyplot as plt
import string

alpha = string.ascii_lowercase
alpha_cap = string.ascii_uppercase
number = ['1','2','3','4','5','6','7','8','9','0']

def frequency(text, plot_bool):
    c = text
    letters_count = [0]*27
    numbers_count = [None]*27
    i = 0

    while i<len(alpha):
        letters_count[i] = c.count(alpha[i])+c.count(alpha_cap[i])
        i+=1

    #i = 0
    #while i<len(number):
    #    numbers_count[i] = c.count(number[i])
    #    i+=1

    if(plot_bool):
        fig = plt.figure()
        width = .35
        ind = np.arange(len(letters_count))
        plt.bar(ind, letters_count)
        plt.xticks(ind + width/2, alpha)
        return(letters_count)
    else:
        return(letters_count)

<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
__version__ = '0.1'

HOOK_EVENTS = {
    # 'any.event.name': 'App.Model.Action' (created/updated/deleted)
    'customer.created': 'silver.Customer.created+',
    'customer.updated': 'silver.Customer.updated+',
    'customer.deleted': 'silver.Customer.deleted+',

    'plan.created': 'silver.Plan.created+',
    'plan.updated': 'silver.Plan.updated+',
    'plan.deleted': 'silver.Plan.deleted+',

    'subscription.created': 'silver.Subscription.created+',
    'subscription.updated': 'silver.Subscription.updated+',
    'subscription.deleted': 'silver.Subscription.deleted+',

    'provider.created': 'silver.Provider.created+',
    'provider.updated': 'silver.Provider.updated+',
    'provider.deleted': 'silver.Provider.deleted+',
}

REQUIRED_APPS = [
    # Required apps
    'rest_framework',
    'rest_hooks',
    'international',
    'django_fsm',
    'django_filters',
]
<commit_msg>Add invoice/proforma hooks to __init__.py<commit_after># -*- coding: utf-8 -*-
__version__ = '0.1'

HOOK_EVENTS = {
    # 'any.event.name': 'App.Model.Action' (created/updated/deleted)
    'customer.created': 'silver.Customer.created+',
    'customer.updated': 'silver.Customer.updated+',
    'customer.deleted': 'silver.Customer.deleted+',

    'plan.created': 'silver.Plan.created+',
    'plan.updated': 'silver.Plan.updated+',
    'plan.deleted': 'silver.Plan.deleted+',

    'subscription.created': 'silver.Subscription.created+',
    'subscription.updated': 'silver.Subscription.updated+',
    'subscription.deleted': 'silver.Subscription.deleted+',

    'provider.created': 'silver.Provider.created+',
    'provider.updated': 'silver.Provider.updated+',
    'provider.deleted': 'silver.Provider.deleted+',

    'invoice.created': 'silver.Invoice.created+',
    'invoice.updated': 'silver.Invoice.updated+',
    'invoice.deleted': 'silver.Invoice.deleted+',

    'proforma.created': 'silver.Proforma.created+',
    'proforma.updated': 'silver.Proforma.updated+',
    'proforma.deleted': 'silver.Proforma.deleted+',
}

REQUIRED_APPS = [
    # Required apps
    'rest_framework',
    'rest_hooks',
    'international',
    'django_fsm',
    'django_filters',
]
<|endoftext|>


<commit_before>##############################################################################
# Copyright (c) 2013-2018, Lawrence Livermore National Security, LLC.
# Produced at the Lawrence Livermore National Laboratory.
#
# This file is part of Spack.
# Created by Todd Gamblin, tgamblin@llnl.gov, All rights reserved.
# LLNL-CODE-647188
#
# For details, see https://github.com/spack/spack
# Please also see the NOTICE and LICENSE files for our notice and the LGPL.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License (as
# published by the Free Software Foundation) version 2.1, February 1999.
#
# 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 terms and
# conditions of the GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
##############################################################################

from spack import *
import sys

class Hwloc(AutotoolsPackage):
    """The Hardware Locality (hwloc) software project.

    The Portable Hardware Locality (hwloc) software package
    provides a portable abstraction (across OS, versions,
    architectures, ...) of the hierarchical topology of modern
    architectures, including NUMA memory nodes, sockets, shared
    caches, cores and simultaneous multithreading. It also gathers
    various system attributes such as cache and memory information
    as well as the locality of I/O devices such as network
    interfaces, InfiniBand HCAs or GPUs. It primarily aims at
    helping applications with gathering information about modern
    computing hardware so as to exploit it accordingly and
    efficiently.
    """

    homepage = "http://www.open-mpi.org/projects/hwloc/"
    url      = "http://www.open-mpi.org/software/hwloc/v1.9/downloads/hwloc-1.9.tar.gz"
    list_url = "http://www.open-mpi.org/software/hwloc/"
    list_depth = 2

    version('2.0.0', '027e6928ae0b5b64c821d0a71a61cd82')
    version('1.11.9', '4d5f5da8b1d09731d82e865ecf3fa399')
    version('1.11.8', 'a0fa1c9109a4d8b4b6568e62cc9b6e30')
    version('1.11.7', '867a5266675e5bf1ef4ab66c459653f8')
    version('1.11.6', 'b4e95eadd2fbdb6d40bbd96be6f03c84')
    version('1.11.5', '8f5fe6a9be2eb478409ad5e640b2d3ba')
    version('1.11.4', 'b6f23eb59074fd09fdd84905d50b103d')
    version('1.11.3', 'c1d36a9de6028eac1d18ea4782ef958f')
    version('1.11.2', 'e4ca55c2a5c5656da4a4e37c8fc51b23')
    version('1.11.1', 'feb4e416a1b25963ed565d8b42252fdc')
    version('1.9',    '1f9f9155682fe8946a97c08896109508')

    variant('cuda', default=False, description="Support CUDA devices")
    variant('libxml2', default=True, description="Build with libxml2")
    variant('pci', default=(sys.platform != 'darwin'),
            description="Support analyzing devices on PCI bus")
    variant('shared', default=True, description="Build shared libraries")
    variant(
        'cairo',
        default=False,
        description='Enable the Cairo back-end of hwloc\'s lstopo command'
    )

    depends_on('pkgconfig', type='build')

    depends_on('cuda', when='+cuda')
    depends_on('libpciaccess', when='+pci')
    depends_on('libxml2', when='+libxml2')
    depends_on('cairo', when='+cairo')
    depends_on('numactl', when='@:1.11.9 platform=linux')

    def url_for_version(self, version):
        return "http://www.open-mpi.org/software/hwloc/v%s/downloads/hwloc-%s.tar.gz" % (version.up_to(2), version)

    def configure_args(self):
        args = [
            # Disable OpenCL, since hwloc might pick up an OpenCL
            # library at build time that is then not found at run time
            # (Alternatively, we could require OpenCL as dependency.)
            "--disable-opencl",
        ]
        if '@2.0.0:' in self.spec:
            args.append('--enable-netloc')

        args.extend(self.enable_or_disable('cairo'))
        args.extend(self.enable_or_disable('cuda'))
        args.extend(self.enable_or_disable('libxml2'))
        args.extend(self.enable_or_disable('pci'))
        args.extend(self.enable_or_disable('shared'))

        return args
<commit_msg>hwloc: add v 2.0.1 (#7724)<commit_after>##############################################################################
# Copyright (c) 2013-2018, Lawrence Livermore National Security, LLC.
# Produced at the Lawrence Livermore National Laboratory.
#
# This file is part of Spack.
# Created by Todd Gamblin, tgamblin@llnl.gov, All rights reserved.
# LLNL-CODE-647188
#
# For details, see https://github.com/spack/spack
# Please also see the NOTICE and LICENSE files for our notice and the LGPL.
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License (as
# published by the Free Software Foundation) version 2.1, February 1999.
#
# 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 terms and
# conditions of the GNU Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
##############################################################################

from spack import *
import sys

class Hwloc(AutotoolsPackage):
    """The Hardware Locality (hwloc) software project.

    The Portable Hardware Locality (hwloc) software package
    provides a portable abstraction (across OS, versions,
    architectures, ...) of the hierarchical topology of modern
    architectures, including NUMA memory nodes, sockets, shared
    caches, cores and simultaneous multithreading. It also gathers
    various system attributes such as cache and memory information
    as well as the locality of I/O devices such as network
    interfaces, InfiniBand HCAs or GPUs. It primarily aims at
    helping applications with gathering information about modern
    computing hardware so as to exploit it accordingly and
    efficiently.
    """

    homepage = "http://www.open-mpi.org/projects/hwloc/"
    url      = "http://www.open-mpi.org/software/hwloc/v1.9/downloads/hwloc-1.9.tar.gz"
    list_url = "http://www.open-mpi.org/software/hwloc/"
    list_depth = 2

    version('2.0.1',  '442b2482bb5b81983ed256522aadbf94')
    version('2.0.0',  '027e6928ae0b5b64c821d0a71a61cd82')
    version('1.11.9', '4d5f5da8b1d09731d82e865ecf3fa399')
    version('1.11.8', 'a0fa1c9109a4d8b4b6568e62cc9b6e30')
    version('1.11.7', '867a5266675e5bf1ef4ab66c459653f8')
    version('1.11.6', 'b4e95eadd2fbdb6d40bbd96be6f03c84')
    version('1.11.5', '8f5fe6a9be2eb478409ad5e640b2d3ba')
    version('1.11.4', 'b6f23eb59074fd09fdd84905d50b103d')
    version('1.11.3', 'c1d36a9de6028eac1d18ea4782ef958f')
    version('1.11.2', 'e4ca55c2a5c5656da4a4e37c8fc51b23')
    version('1.11.1', 'feb4e416a1b25963ed565d8b42252fdc')
    version('1.9',    '1f9f9155682fe8946a97c08896109508')

    variant('cuda', default=False, description="Support CUDA devices")
    variant('libxml2', default=True, description="Build with libxml2")
    variant('pci', default=(sys.platform != 'darwin'),
            description="Support analyzing devices on PCI bus")
    variant('shared', default=True, description="Build shared libraries")
    variant(
        'cairo',
        default=False,
        description='Enable the Cairo back-end of hwloc\'s lstopo command'
    )

    depends_on('pkgconfig', type='build')

    depends_on('cuda', when='+cuda')
    depends_on('libpciaccess', when='+pci')
    depends_on('libxml2', when='+libxml2')
    depends_on('cairo', when='+cairo')
    depends_on('numactl', when='@:1.11.9 platform=linux')

    def url_for_version(self, version):
        return "http://www.open-mpi.org/software/hwloc/v%s/downloads/hwloc-%s.tar.gz" % (version.up_to(2), version)

    def configure_args(self):
        args = [
            # Disable OpenCL, since hwloc might pick up an OpenCL
            # library at build time that is then not found at run time
            # (Alternatively, we could require OpenCL as dependency.)
            "--disable-opencl",
        ]
        if '@2.0.0:' in self.spec:
            args.append('--enable-netloc')

        args.extend(self.enable_or_disable('cairo'))
        args.extend(self.enable_or_disable('cuda'))
        args.extend(self.enable_or_disable('libxml2'))
        args.extend(self.enable_or_disable('pci'))
        args.extend(self.enable_or_disable('shared'))

        return args
<|endoftext|>


<commit_before>from collections import OrderedDict, defaultdict
from copy import deepcopy
from enum import Enum
from simfile.types import Simfile, Chart
from typing import DefaultDict, Dict, List, Optional, Type, TypeVar, Union
from .sm import SMChart, SMSimfile
from .ssc import SSCChart, SSCSimfile

__all__ = [
    'InvalidProperty', 'InvalidPropertyBehavior', 'InvalidPropertyException',
    'sm_to_ssc', 'ssc_to_sm',
]

DEFAULT_PROPERTIES: DefaultDict[str, str] = defaultdict(lambda: '', {
    'TIMESIGNATURES': '0.000=4=4',
    'TICKCOUNTS': '0.000=4',
    'COMBOS': '0.000=1',
    'SPEEDS': '0.000=1.000=0.000=0',
    'SCROLLS': '0.000=1.000',
    'LABELS': '0.000=Song Start',
})

class InvalidProperty(Enum):
    MUST_IGNORE = 1
    METADATA = 2
    FILE_PATH = 3
    GAMEPLAY_EVENT = 4
    TIMING_DATA = 5

TYPE_TO_INVALID_PROPERTIES: Dict[Type, Dict[InvalidProperty, List[str]]] = {
    SMSimfile: {
        InvalidProperty.MUST_IGNORE: ['VERSION'],
        InvalidProperty.METADATA: ['ORIGIN', 'TIMESIGNATURES', 'LABELS'],
        InvalidProperty.FILE_PATH: [
            'PREVIEWVID', 'JACKET', 'CDIMAGE', 'DISCIMAGE',
        ],
        InvalidProperty.GAMEPLAY_EVENT: [
            'TICKCOUNTS', 'COMBOS', 'SPEEDS', 'SCROLLS', 'FAKES',
        ],
        InvalidProperty.TIMING_DATA: ['DELAYS', 'WARPS'],
    },
    SMChart: {
        InvalidProperty.METADATA: [
            'CHARTNAME', 'CHARTSTYLE', 'CREDIT', 'DISPLAYBPM',
            'TIMESIGNATURES', 'LABELS',
        ],
        InvalidProperty.GAMEPLAY_EVENT: [
            'TICKCOUNTS', 'COMBOS', 'SPEEDS', 'SCROLLS', 'FAKES',
        ],
        InvalidProperty.TIMING_DATA: [
            'OFFSET', 'BPMS', 'STOPS', 'DELAYS', 'WARPS',
        ],
    },
}

class InvalidPropertyBehavior(Enum):
    COPY_ANYWAY = 1
    IGNORE = 2
    ERROR_UNLESS_DEFAULT = 3
    ERROR = 4

InvalidPropertyBehaviorMapping = Dict[InvalidProperty, InvalidPropertyBehavior]

INVALID_PROPERTY_BEHAVIORS: InvalidPropertyBehaviorMapping = {
    InvalidProperty.MUST_IGNORE: InvalidPropertyBehavior.IGNORE,
    InvalidProperty.METADATA: InvalidPropertyBehavior.IGNORE,
    InvalidProperty.FILE_PATH: InvalidPropertyBehavior.IGNORE,
    InvalidProperty.GAMEPLAY_EVENT: InvalidPropertyBehavior.ERROR_UNLESS_DEFAULT,
    InvalidProperty.TIMING_DATA: InvalidPropertyBehavior.ERROR,
}

class InvalidPropertyException(Exception):
    # TODO: human-friendly string
    pass

_CONVERT_TYPE = TypeVar(
    '_CONVERT_TYPE', SMSimfile, SSCSimfile, SMChart, SSCChart, Simfile, Chart,
)
_CONVERT_SIMFILE = TypeVar('_CONVERT_SIMFILE', SMSimfile, SSCSimfile)
_CONVERT_CHART = TypeVar('_CONVERT_CHART', SMChart, SSCChart)

def _should_copy_property(
    property: str,
    value: str,
    invalid_properties: Dict[InvalidProperty, List[str]],
    invalid_property_behaviors: InvalidPropertyBehaviorMapping
) -> bool:
    for invalid_property, properties in invalid_properties.items():
        if property in properties:
            behavior = invalid_property_behaviors.get(invalid_property) \
                or INVALID_PROPERTY_BEHAVIORS[invalid_property]
            if behavior == InvalidPropertyBehavior.COPY_ANYWAY:
                return True
            if behavior == InvalidPropertyBehavior.IGNORE:
                return False
            if behavior == InvalidPropertyBehavior.ERROR_UNLESS_DEFAULT:
                if value.strip() != DEFAULT_PROPERTIES[property]:
                    return False
            raise InvalidPropertyException(
                invalid_property,
                property,
            )
    return True

def _copy_properties(
    source: _CONVERT_TYPE,
    output: _CONVERT_TYPE,
    output_type: Type[_CONVERT_TYPE],
    invalid_property_behaviors: InvalidPropertyBehaviorMapping,
) -> None:
    invalid_properties = TYPE_TO_INVALID_PROPERTIES.get(output_type, {})
    for property, value in source.items():
        if _should_copy_property(
            property,
            value,
            invalid_properties,
            invalid_property_behaviors,
        ):
            output[property] = value

def _convert(
    simfile: Simfile,
    output_simfile_type: Type[_CONVERT_SIMFILE],
    output_chart_type: Type[_CONVERT_CHART],
    simfile_template: Optional[_CONVERT_SIMFILE] = None,
    chart_template: Optional[_CONVERT_CHART] = None,
    invalid_property_behaviors: InvalidPropertyBehaviorMapping = {}
) -> _CONVERT_SIMFILE:
    output_simfile = deepcopy(simfile_template) or output_simfile_type.blank()

    _copy_properties(
        source=simfile,
        output=output_simfile,
        output_type=output_simfile_type,
        invalid_property_behaviors=invalid_property_behaviors,
    )

    for _chart in simfile.charts:
        chart: Chart = _chart # typing workaround
        output_chart = deepcopy(chart_template) or output_chart_type.blank()
        if isinstance(chart, SMSimfile):
            output_chart.stepstype = chart.stepstype
            output_chart.description = chart.description
            output_chart.difficulty = chart.difficulty
            output_chart.meter = chart.meter
            output_chart.radarvalues = chart.radarvalues
            output_chart.notes = chart.notes
        else:
            _copy_properties(
                source=chart,
                output=output_chart,
                output_type=output_chart_type,
                invalid_property_behaviors=invalid_property_behaviors,
            )
        output_simfile.charts.append(output_chart)

    return output_simfile

def sm_to_ssc(
    sm_simfile: SMSimfile,
    *,
    simfile_template: Optional[SSCSimfile] = None,
    chart_template: Optional[SSCChart] = None,
    invalid_property_behaviors: InvalidPropertyBehaviorMapping = {},
) -> SSCSimfile:
    return _convert(
        simfile=sm_simfile,
        output_simfile_type=SSCSimfile,
        output_chart_type=SSCChart,
        simfile_template=simfile_template,
        chart_template=chart_template,
        invalid_property_behaviors=invalid_property_behaviors,
    )

def ssc_to_sm(
    ssc_simfile: SSCSimfile,
    *,
    simfile_template: Optional[SMSimfile] = None,
    chart_template: Optional[SMChart] = None,
    invalid_property_behaviors: InvalidPropertyBehaviorMapping = {},
) -> SMSimfile:
    return _convert(
        simfile=ssc_simfile,
        output_simfile_type=SMSimfile,
        output_chart_type=SMChart,
        simfile_template=simfile_template,
        chart_template=chart_template,
        invalid_property_behaviors=invalid_property_behaviors,
    )<commit_msg>convert: tweak enums, add documentation<commit_after>from collections import OrderedDict, defaultdict
from copy import deepcopy
from enum import Enum
from simfile.types import Simfile, Chart
from typing import DefaultDict, Dict, List, Optional, Type, TypeVar, Union
from .sm import SMChart, SMSimfile
from .ssc import SSCChart, SSCSimfile

__all__ = [
    'KnownProperty', 'InvalidPropertyBehavior', 'InvalidPropertyException',
    'sm_to_ssc', 'ssc_to_sm',
]

DEFAULT_PROPERTIES: DefaultDict[str, str] = defaultdict(lambda: '', {
    'TIMESIGNATURES': '0.000=4=4',
    'TICKCOUNTS': '0.000=4',
    'COMBOS': '0.000=1',
    'SPEEDS': '0.000=1.000=0.000=0',
    'SCROLLS': '0.000=1.000',
    'LABELS': '0.000=Song Start',
})

class KnownProperty(Enum):
    """
    Types of known properties.

    These mirror the lists of known properties documented in
    :class:`simfile.base.BaseSimfile` and
    :class:`simfile.ssc.SSCSimfile`.
    """
    SSC_VERSION = 1
    METADATA = 2
    FILE_PATH = 3
    GAMEPLAY_EVENT = 4
    TIMING_DATA = 5

INVALID_PROPERTIES: Dict[Type, Dict[KnownProperty, List[str]]] = {
    SMSimfile: {
        KnownProperty.SSC_VERSION: ['VERSION'],
        KnownProperty.METADATA: ['ORIGIN', 'TIMESIGNATURES', 'LABELS'],
        KnownProperty.FILE_PATH: [
            'PREVIEWVID', 'JACKET', 'CDIMAGE', 'DISCIMAGE',
        ],
        KnownProperty.GAMEPLAY_EVENT: [
            'TICKCOUNTS', 'COMBOS', 'SPEEDS', 'SCROLLS', 'FAKES',
        ],
        KnownProperty.TIMING_DATA: ['DELAYS', 'WARPS'],
    },
    SMChart: {
        KnownProperty.METADATA: [
            'CHARTNAME', 'CHARTSTYLE', 'CREDIT', 'DISPLAYBPM',
            'TIMESIGNATURES', 'LABELS',
        ],
        KnownProperty.GAMEPLAY_EVENT: [
            'TICKCOUNTS', 'COMBOS', 'SPEEDS', 'SCROLLS', 'FAKES',
        ],
        KnownProperty.TIMING_DATA: [
            'OFFSET', 'BPMS', 'STOPS', 'DELAYS', 'WARPS',
        ],
    },
}

class InvalidPropertyBehavior(Enum):
    """
    How to handle an invalid property during conversion.

    If a known property can't be converted to the destination type...

    * `COPY_ANYWAY`: copy the property regardless
    * `IGNORE`: do not copy the property
    * `ERROR_UNLESS_DEFAULT`: raise :class:`InvalidPropertyException`
      unless its value is the default value
    * `ERROR`: raise :class:`InvalidPropertyException` regardless of
      the property's value

    The "default value" for most properties is an empty string. If the
    destination type's :code:`.blank` output has a non-empty value for
    the property, that value is considered the default instead (except
    for `BPMS`).
    """
    COPY_ANYWAY = 1
    IGNORE = 2
    ERROR_UNLESS_DEFAULT = 3
    ERROR = 4

InvalidPropertyBehaviorMapping = Dict[KnownProperty, InvalidPropertyBehavior]

INVALID_PROPERTY_BEHAVIORS: InvalidPropertyBehaviorMapping = {
    KnownProperty.SSC_VERSION: InvalidPropertyBehavior.IGNORE,
    KnownProperty.METADATA: InvalidPropertyBehavior.IGNORE,
    KnownProperty.FILE_PATH: InvalidPropertyBehavior.IGNORE,
    KnownProperty.GAMEPLAY_EVENT: InvalidPropertyBehavior.ERROR_UNLESS_DEFAULT,
    KnownProperty.TIMING_DATA: InvalidPropertyBehavior.ERROR,
}

class InvalidPropertyException(Exception):
    """
    Raised by conversion functions if a property cannot be converted.
    """
    # TODO: human-friendly string
    pass

_CONVERT_TYPE = TypeVar(
    '_CONVERT_TYPE', SMSimfile, SSCSimfile, SMChart, SSCChart, Simfile, Chart,
)
_CONVERT_SIMFILE = TypeVar('_CONVERT_SIMFILE', SMSimfile, SSCSimfile)
_CONVERT_CHART = TypeVar('_CONVERT_CHART', SMChart, SSCChart)

def _should_copy_property(
    property: str,
    value: str,
    invalid_properties: Dict[KnownProperty, List[str]],
    invalid_property_behaviors: InvalidPropertyBehaviorMapping
) -> bool:
    for invalid_property, properties in invalid_properties.items():
        if property in properties:
            behavior = invalid_property_behaviors.get(invalid_property) \
                or INVALID_PROPERTY_BEHAVIORS[invalid_property]
            if behavior == InvalidPropertyBehavior.COPY_ANYWAY:
                return True
            if behavior == InvalidPropertyBehavior.IGNORE:
                return False
            if behavior == InvalidPropertyBehavior.ERROR_UNLESS_DEFAULT:
                if value.strip() != DEFAULT_PROPERTIES[property]:
                    return False
            raise InvalidPropertyException(
                invalid_property,
                property,
            )
    return True

def _copy_properties(
    source: _CONVERT_TYPE,
    output: _CONVERT_TYPE,
    output_type: Type[_CONVERT_TYPE],
    invalid_property_behaviors: InvalidPropertyBehaviorMapping,
) -> None:
    invalid_properties = INVALID_PROPERTIES.get(output_type, {})
    for property, value in source.items():
        if _should_copy_property(
            property,
            value,
            invalid_properties,
            invalid_property_behaviors,
        ):
            output[property] = value

def _convert(
    simfile: Simfile,
    output_simfile_type: Type[_CONVERT_SIMFILE],
    output_chart_type: Type[_CONVERT_CHART],
    simfile_template: Optional[_CONVERT_SIMFILE] = None,
    chart_template: Optional[_CONVERT_CHART] = None,
    invalid_property_behaviors: InvalidPropertyBehaviorMapping = {}
) -> _CONVERT_SIMFILE:
    output_simfile = deepcopy(simfile_template) or output_simfile_type.blank()

    _copy_properties(
        source=simfile,
        output=output_simfile,
        output_type=output_simfile_type,
        invalid_property_behaviors=invalid_property_behaviors,
    )

    for _chart in simfile.charts:
        chart: Chart = _chart # typing workaround
        output_chart = deepcopy(chart_template) or output_chart_type.blank()
        if isinstance(chart, SMSimfile):
            output_chart.stepstype = chart.stepstype
            output_chart.description = chart.description
            output_chart.difficulty = chart.difficulty
            output_chart.meter = chart.meter
            output_chart.radarvalues = chart.radarvalues
            output_chart.notes = chart.notes
        else:
            _copy_properties(
                source=chart,
                output=output_chart,
                output_type=output_chart_type,
                invalid_property_behaviors=invalid_property_behaviors,
            )
        output_simfile.charts.append(output_chart)

    return output_simfile

def sm_to_ssc(
    sm_simfile: SMSimfile,
    *,
    simfile_template: Optional[SSCSimfile] = None,
    chart_template: Optional[SSCChart] = None,
) -> SSCSimfile:
    """
    Convert an SM simfile to an equivalent SSC simfile.

    `simfile_template` and `chart_template` can optionally be provided
    to define the initial simfile and chart prior to copying properties
    from the source object. If they are not provided,
    :meth:`simfile.sm.SMSimfile.blank` and
    :meth:`simfile.sm.SMChart.blank` will supply the template objects.
    """
    return _convert(
        simfile=sm_simfile,
        output_simfile_type=SSCSimfile,
        output_chart_type=SSCChart,
        simfile_template=simfile_template,
        chart_template=chart_template,
        invalid_property_behaviors={},
    )

def ssc_to_sm(
    ssc_simfile: SSCSimfile,
    *,
    simfile_template: Optional[SMSimfile] = None,
    chart_template: Optional[SMChart] = None,
    invalid_property_behaviors: InvalidPropertyBehaviorMapping = {},
) -> SMSimfile:
    """
    Convert an SSC simfile to an equivalent SM simfile.

    `simfile_template` and `chart_template` can optionally be provided
    to define the initial simfile and chart prior to copying properties
    from the source object. If they are not provided,
    :meth:`simfile.ssc.SSCSimfile.blank` and
    :meth:`simfile.ssc.SSCChart.blank` will supply the template objects.

    Not all SSC properties are valid for SM simfiles, and some of these
    properties may be crucial for the simfile to behave as intended
    during gameplay. By default, invalid properties are ignored unless:

    * The property is a gameplay event and has a non-default value, or
    * The property contains timing data.

    These behaviors can be overridden by supplying the
    `invalid_property_behaviors` parameter, which maps
    :class:`KnownProperty` to :class:`InvalidPropertyBehavior` values.
    This mapping need not cover every :class:`KnownProperty`; any
    missing values will fall back to the default mapping described
    above.
    """
    return _convert(
        simfile=ssc_simfile,
        output_simfile_type=SMSimfile,
        output_chart_type=SMChart,
        simfile_template=simfile_template,
        chart_template=chart_template,
        invalid_property_behaviors=invalid_property_behaviors,
    )<|endoftext|>


<commit_before>"""
Tests for LAMMPS IO
"""
import eex
import numpy as np
import pytest
import pandas as pd
import eex_find_files

def test_lammps_read():

    fname = eex_find_files.get_example_filename("lammps", "data.spce")

    dl = eex.datalayer.DataLayer("test_lammps_read")

    data = eex.translators.lammps.read_lammps_file(dl, fname)

    # Check on the data dictionary
    assert data["sizes"]["atoms"] == 600
    assert data["sizes"]["bonds"] == 400
    assert data["sizes"]["angles"] == 200
    assert data["sizes"]["angle types"] == 1

    assert data["dimensions"]["xlo"] == -12.362
    assert data["dimensions"]["xhi"] == 12.362

    # Check Atoms
    atoms = dl.get_atoms(["atom_type", "charge"])
    assert atoms.shape[0] == 600
    assert np.allclose(np.unique(atoms["atom_type"]), [1, 2])
    assert np.allclose(np.unique(atoms["charge"]), [-0.8476,  0.4238])

    # Check Bonds
    bonds = dl.get_bonds()
    assert bonds.shape[0] == 400
    assert np.allclose(np.unique(bonds["bond_type"]), [1])

    # Check Angles
    angles = dl.get_angles()
    assert angles.shape[0] == 200
    assert np.allclose(np.unique(angles["angle_type"]), [1])

<commit_msg>FIX modify test_lammps_read path for spce water example<commit_after>"""
Tests for LAMMPS IO
"""
import eex
import numpy as np
import pytest
import pandas as pd
import eex_find_files

def test_lammps_read():

    fname = eex_find_files.get_example_filename("lammps", "SPCE/data.spce")

    dl = eex.datalayer.DataLayer("test_lammps_read")

    data = eex.translators.lammps.read_lammps_file(dl, fname)

    # Check on the data dictionary
    assert data["sizes"]["atoms"] == 600
    assert data["sizes"]["bonds"] == 400
    assert data["sizes"]["angles"] == 200
    assert data["sizes"]["angle types"] == 1

    assert data["dimensions"]["xlo"] == -12.362
    assert data["dimensions"]["xhi"] == 12.362

    # Check Atoms
    atoms = dl.get_atoms(["atom_type", "charge"])
    assert atoms.shape[0] == 600
    assert np.allclose(np.unique(atoms["atom_type"]), [1, 2])
    assert np.allclose(np.unique(atoms["charge"]), [-0.8476,  0.4238])

    # Check Bonds
    bonds = dl.get_bonds()
    assert bonds.shape[0] == 400
    assert np.allclose(np.unique(bonds["bond_type"]), [1])

    # Check Angles
    angles = dl.get_angles()
    assert angles.shape[0] == 200
    assert np.allclose(np.unique(angles["angle_type"]), [1])

<|endoftext|>


<commit_before>
# Run setup.py install in sources/python to build source files.

from DeepRTS.python import scenario
from DeepRTS.Engine import Random
if __name__ == "__main__":
    episodes = 1000
    random_play = True

    env = scenario.GoldCollectOnePlayerFifteen({})

    env.game.set_max_fps(99999999)
    env.game.set_max_ups(99999999)

    env.calculate_optimal_play()

    for episode in range(episodes):
        print("Episode: %s, FPS: %s, UPS: %s" % (episode, env.game.get_fps(), env.game.get_ups()))

        terminal = False
        state = env.reset()

        while not terminal:

            action = Random.action() - 1  # TODO AI Goes here
            next_state, reward, terminal, _ = env.step(action)

<commit_msg>Initial commit<commit_after>
# Run setup.py install in sources/python to build source files.

from DeepRTS.python import scenario
from DeepRTS.Engine import Random
if __name__ == "__main__":
    episodes = 10
    random_play = True

    env = scenario.GoldCollectOnePlayerFifteen({})

    env.game.set_max_fps(99999999)
    env.game.set_max_ups(99999999)

    env.calculate_optimal_play()

    for episode in range(episodes):
        print("Episode: %s, FPS: %s, UPS: %s" % (episode, env.game.get_fps(), env.game.get_ups()))

        terminal = False
        state = env.reset()

        while not terminal:

            action = Random.action() - 1  # TODO AI Goes here
            next_state, reward, terminal, _ = env.step(action)

<|endoftext|>


<commit_before>import os
from django.db import models
from core.models import PlCoreBase
from core.models import Site
from core.models.site import SitePrivilege
from core.models import User
from core.models import Role
from core.models import Controller,ControllerLinkManager,ControllerLinkDeletionManager
from core.models import ServiceClass
from core.models.serviceclass import get_default_serviceclass
from core.models import Tag
from django.contrib.contenttypes import generic
from core.models import Service
from core.models import Controller
from core.models import Flavor, Image
from core.models.plcorebase import StrippedCharField
from django.core.exceptions import PermissionDenied, ValidationError

# Create your models here.

class Slice(PlCoreBase):
    name = StrippedCharField(unique=True, help_text="The Name of the Slice", max_length=80)
    enabled = models.BooleanField(default=True, help_text="Status for this Slice")
    omf_friendly = models.BooleanField(default=False)
    description=models.TextField(blank=True,help_text="High level description of the slice and expected activities", max_length=1024)
    slice_url = models.URLField(blank=True, max_length=512)
    site = models.ForeignKey(Site, related_name='slices', help_text="The Site this Slice belongs to")
    max_slivers = models.IntegerField(default=10)
    service = models.ForeignKey(Service, related_name='slices', null=True, blank=True)
    network = StrippedCharField(default="Private Only",null=True, blank=True, max_length=256)
    tags = generic.GenericRelation(Tag)
    serviceClass = models.ForeignKey(ServiceClass, related_name = "slices", null=True, default=get_default_serviceclass)
    creator = models.ForeignKey(User, related_name='slices', blank=True, null=True)

    # for tenant view
    default_flavor = models.ForeignKey(Flavor, related_name = "slices", null=True, blank=True)
    default_image = models.ForeignKey(Image, related_name = "slices", null=True, blank=True);
    mount_data_sets = StrippedCharField(default="GenBank",null=True, blank=True, max_length=256)

    def __unicode__(self):  return u'%s' % (self.name)

    @property
    def slicename(self):
        return "%s_%s" % (self.site.login_base, self.name)

    def save(self, *args, **kwds):
        site = Site.objects.get(id=self.site.id)
        # allow preexisting slices to keep their original name for now
        if not self.id and not self.name.startswith(site.login_base):
            raise ValidationError('slice name must begin with %s' % site.login_base)

        if self.name == site.login_base+"_":
            raise ValidationError('slice name is too short')

        if " " in self.name:
            raise ValidationError('slice name must not contain spaces')

        if self.serviceClass is None:
            # We allowed None=True for serviceClass because Django evolution
            # will fail unless it is allowed. But, we we really don't want it to
            # ever save None, so fix it up here.
            self.serviceClass = ServiceClass.get_default()

        # set creator on first save
        if not self.creator and hasattr(self, 'caller'):
            self.creator = self.caller

        # only admins change a slice's creator
        if 'creator' in self.changed_fields and \
            (not hasattr(self, 'caller') or not self.caller.is_admin):

            if (self._initial["creator"]==None) and (self.creator==getattr(self,"caller",None)):
                # it's okay if the creator is being set by the caller to
                # himeself on a new slice object.
                pass
            else:
                raise PermissionDenied("Insufficient privileges to change slice creator")

        if not self.creator:
            raise ValidationError('slice has no creator')

        super(Slice, self).save(*args, **kwds)

    def can_update(self, user):
        return user.can_update_slice(self)

    @staticmethod
    def select_by_user(user):
        if user.is_admin:
            qs = Slice.objects.all()
        else:
            # users can see slices they belong to
            slice_ids = [sp.slice.id for sp in SlicePrivilege.objects.filter(user=user)]
            # pis can see slices at their sites
            sites = [sp.site for sp in SitePrivilege.objects.filter(user=user)\
                        if sp.role.role == 'pi']
            slice_ids.extend([s.id for s in Slice.objects.filter(site__in=sites)])
            qs = Slice.objects.filter(id__in=slice_ids)
        return qs

    def delete(self, *args, **kwds):
        # delete networks associated with this slice
        from core.models.network import Network
        nets = Network.objects.filter(slices=self)
        nets.delete()
        # delete slice controllers
        slice_controllers = ControllerSlice.objects.filter(slice=self)
        slice_controllers.delete()
        # delete slice privilege
        slice_privileges = SlicePrivilege.objects.filter(slice=self)
        slice_privileges.delete()
        # continue with normal delete
        super(Slice, self).delete(*args, **kwds)

class SliceRole(PlCoreBase):
    ROLE_CHOICES = (('admin','Admin'),('default','Default'))

    role = StrippedCharField(choices=ROLE_CHOICES, unique=True, max_length=30)

    def __unicode__(self):  return u'%s' % (self.role)

class SlicePrivilege(PlCoreBase):
    user = models.ForeignKey('User', related_name='sliceprivileges')
    slice = models.ForeignKey('Slice', related_name='sliceprivileges')
    role = models.ForeignKey('SliceRole',related_name='sliceprivileges')

    class Meta:
        unique_together = ('user', 'slice', 'role')

    def __unicode__(self):  return u'%s %s %s' % (self.slice, self.user, self.role)

    def save(self, *args, **kwds):
        if not self.user.is_active:
            raise PermissionDenied, "Cannot modify role(s) of a disabled user"
        super(SlicePrivilege, self).delete(*args, **kwds)

    def can_update(self, user):
        return user.can_update_slice(self.slice)

    @staticmethod
    def select_by_user(user):
        if user.is_admin:
            qs = SlicePrivilege.objects.all()
        else:
            sp_ids = [sp.id for sp in SlicePrivilege.objects.filter(user=user)]
            qs = SlicePrivilege.objects.filter(id__in=sp_ids)
        return qs

class ControllerSlice(PlCoreBase):
    objects = ControllerLinkManager()
    deleted_objects = ControllerLinkDeletionManager()

    controller = models.ForeignKey(Controller, related_name='controllerslices')
    slice = models.ForeignKey(Slice, related_name='controllerslices')
    tenant_id = StrippedCharField(null=True, blank=True, max_length=200, help_text="Keystone tenant id")

    class Meta:
        unique_together = ('controller', 'slice')

    def __unicode__(self):  return u'%s %s'  % (self.slice, self.controller)

    @staticmethod
    def select_by_user(user):
        if user.is_admin:
            qs = ControllerSlice.objects.all()
        else:
            slices = Slice.select_by_user(user)
            qs = ControllerSlice.objects.filter(slice__in=slices)
        return qs

    def get_cpu_stats(self):
        filter = 'project_id=%s'%self.tenant_id
        return monitor.get_meter('cpu',filter,None)

    def get_bw_stats(self):
        filter = 'project_id=%s'%self.tenant_id
        return monitor.get_meter('network.outgoing.bytes',filter,None)

    def get_node_stats(self):
        return len(self.slice.slivers)
<commit_msg>fix wrong method called in SlicePrivilege.save<commit_after>import os
from django.db import models
from core.models import PlCoreBase
from core.models import Site
from core.models.site import SitePrivilege
from core.models import User
from core.models import Role
from core.models import Controller,ControllerLinkManager,ControllerLinkDeletionManager
from core.models import ServiceClass
from core.models.serviceclass import get_default_serviceclass
from core.models import Tag
from django.contrib.contenttypes import generic
from core.models import Service
from core.models import Controller
from core.models import Flavor, Image
from core.models.plcorebase import StrippedCharField
from django.core.exceptions import PermissionDenied, ValidationError

# Create your models here.

class Slice(PlCoreBase):
    name = StrippedCharField(unique=True, help_text="The Name of the Slice", max_length=80)
    enabled = models.BooleanField(default=True, help_text="Status for this Slice")
    omf_friendly = models.BooleanField(default=False)
    description=models.TextField(blank=True,help_text="High level description of the slice and expected activities", max_length=1024)
    slice_url = models.URLField(blank=True, max_length=512)
    site = models.ForeignKey(Site, related_name='slices', help_text="The Site this Slice belongs to")
    max_slivers = models.IntegerField(default=10)
    service = models.ForeignKey(Service, related_name='slices', null=True, blank=True)
    network = StrippedCharField(default="Private Only",null=True, blank=True, max_length=256)
    tags = generic.GenericRelation(Tag)
    serviceClass = models.ForeignKey(ServiceClass, related_name = "slices", null=True, default=get_default_serviceclass)
    creator = models.ForeignKey(User, related_name='slices', blank=True, null=True)

    # for tenant view
    default_flavor = models.ForeignKey(Flavor, related_name = "slices", null=True, blank=True)
    default_image = models.ForeignKey(Image, related_name = "slices", null=True, blank=True);
    mount_data_sets = StrippedCharField(default="GenBank",null=True, blank=True, max_length=256)

    def __unicode__(self):  return u'%s' % (self.name)

    @property
    def slicename(self):
        return "%s_%s" % (self.site.login_base, self.name)

    def save(self, *args, **kwds):
        site = Site.objects.get(id=self.site.id)
        # allow preexisting slices to keep their original name for now
        if not self.id and not self.name.startswith(site.login_base):
            raise ValidationError('slice name must begin with %s' % site.login_base)

        if self.name == site.login_base+"_":
            raise ValidationError('slice name is too short')

        if " " in self.name:
            raise ValidationError('slice name must not contain spaces')

        if self.serviceClass is None:
            # We allowed None=True for serviceClass because Django evolution
            # will fail unless it is allowed. But, we we really don't want it to
            # ever save None, so fix it up here.
            self.serviceClass = ServiceClass.get_default()

        # set creator on first save
        if not self.creator and hasattr(self, 'caller'):
            self.creator = self.caller

        # only admins change a slice's creator
        if 'creator' in self.changed_fields and \
            (not hasattr(self, 'caller') or not self.caller.is_admin):

            if (self._initial["creator"]==None) and (self.creator==getattr(self,"caller",None)):
                # it's okay if the creator is being set by the caller to
                # himeself on a new slice object.
                pass
            else:
                raise PermissionDenied("Insufficient privileges to change slice creator")

        if not self.creator:
            raise ValidationError('slice has no creator')

        super(Slice, self).save(*args, **kwds)

    def can_update(self, user):
        return user.can_update_slice(self)

    @staticmethod
    def select_by_user(user):
        if user.is_admin:
            qs = Slice.objects.all()
        else:
            # users can see slices they belong to
            slice_ids = [sp.slice.id for sp in SlicePrivilege.objects.filter(user=user)]
            # pis can see slices at their sites
            sites = [sp.site for sp in SitePrivilege.objects.filter(user=user)\
                        if sp.role.role == 'pi']
            slice_ids.extend([s.id for s in Slice.objects.filter(site__in=sites)])
            qs = Slice.objects.filter(id__in=slice_ids)
        return qs

    def delete(self, *args, **kwds):
        # delete networks associated with this slice
        from core.models.network import Network
        nets = Network.objects.filter(slices=self)
        nets.delete()
        # delete slice controllers
        slice_controllers = ControllerSlice.objects.filter(slice=self)
        slice_controllers.delete()
        # delete slice privilege
        slice_privileges = SlicePrivilege.objects.filter(slice=self)
        slice_privileges.delete()
        # continue with normal delete
        super(Slice, self).delete(*args, **kwds)

class SliceRole(PlCoreBase):
    ROLE_CHOICES = (('admin','Admin'),('default','Default'))

    role = StrippedCharField(choices=ROLE_CHOICES, unique=True, max_length=30)

    def __unicode__(self):  return u'%s' % (self.role)

class SlicePrivilege(PlCoreBase):
    user = models.ForeignKey('User', related_name='sliceprivileges')
    slice = models.ForeignKey('Slice', related_name='sliceprivileges')
    role = models.ForeignKey('SliceRole',related_name='sliceprivileges')

    class Meta:
        unique_together = ('user', 'slice', 'role')

    def __unicode__(self):  return u'%s %s %s' % (self.slice, self.user, self.role)

    def save(self, *args, **kwds):
        if not self.user.is_active:
            raise PermissionDenied, "Cannot modify role(s) of a disabled user"
        super(SlicePrivilege, self).save(*args, **kwds)

    def can_update(self, user):
        return user.can_update_slice(self.slice)

    @staticmethod
    def select_by_user(user):
        if user.is_admin:
            qs = SlicePrivilege.objects.all()
        else:
            sp_ids = [sp.id for sp in SlicePrivilege.objects.filter(user=user)]
            qs = SlicePrivilege.objects.filter(id__in=sp_ids)
        return qs

class ControllerSlice(PlCoreBase):
    objects = ControllerLinkManager()
    deleted_objects = ControllerLinkDeletionManager()

    controller = models.ForeignKey(Controller, related_name='controllerslices')
    slice = models.ForeignKey(Slice, related_name='controllerslices')
    tenant_id = StrippedCharField(null=True, blank=True, max_length=200, help_text="Keystone tenant id")

    class Meta:
        unique_together = ('controller', 'slice')

    def __unicode__(self):  return u'%s %s'  % (self.slice, self.controller)

    @staticmethod
    def select_by_user(user):
        if user.is_admin:
            qs = ControllerSlice.objects.all()
        else:
            slices = Slice.select_by_user(user)
            qs = ControllerSlice.objects.filter(slice__in=slices)
        return qs

    def get_cpu_stats(self):
        filter = 'project_id=%s'%self.tenant_id
        return monitor.get_meter('cpu',filter,None)

    def get_bw_stats(self):
        filter = 'project_id=%s'%self.tenant_id
        return monitor.get_meter('network.outgoing.bytes',filter,None)

    def get_node_stats(self):
        return len(self.slice.slivers)
<|endoftext|>


<commit_before># Copyright 2013-2019 Lawrence Livermore National Security, LLC and other
# Spack Project Developers. See the top-level COPYRIGHT file for details.
#
# SPDX-License-Identifier: (Apache-2.0 OR MIT)

from spack import *
import os
import sys

class Hypre(Package):
    """Hypre is a library of high performance preconditioners that
       features parallel multigrid methods for both structured and
       unstructured grid problems."""

    homepage = "http://computation.llnl.gov/project/linear_solvers/software.php"
    url      = "https://github.com/LLNL/hypre/archive/v2.14.0.tar.gz"
    git      = "https://github.com/LLNL/hypre.git"

    maintainers = ['ulrikeyang', 'balay']

    version('develop', branch='master')
    version('2.15.1', '877002d49f38b6a1434955baf79eff35')
    version('2.15.0', '4645acc49141069cae1d53de96107a08')
    version('2.14.0', 'ecde5cc807ec45bfb647e9f28d2eaea1')
    version('2.13.0', '4b688a5c15b6b5e3de5e045ae081b89b')
    version('2.12.1', 'c6fcb6d7e57cec1c7ce4a44da885068c')
    version('2.11.2', 'd507943a1a3ce5681c3308e2f3a6dd34')
    version('2.11.1', '3f02ef8fd679239a6723f60b7f796519')
    version('2.10.1', 'dc048c4cabb3cd549af72591474ad674')
    version('2.10.0b', '768be38793a35bb5d055905b271f5b8e')
    version('xsdk-0.2.0', tag='xsdk-0.2.0')

    # Versions 2.13.0 and later can be patched to build shared
    # libraries on Darwin; the patch for this capability does not
    # apply to version 2.12.1 and earlier due to changes in the build system
    # between versions 2.12.1 and 2.13.0.
    variant('shared', default=(sys.platform != 'darwin'),
            description="Build shared library (disables static library)")
    # SuperluDist have conflicting headers with those in Hypre
    variant('internal-superlu', default=True,
            description="Use internal Superlu routines")
    variant('superlu-dist', default=False,
            description='Activates support for SuperluDist')
    variant('int64', default=False,
            description="Use 64bit integers")
    variant('mpi', default=True, description='Enable MPI support')
    variant('debug', default=False,
            description='Build debug instead of optimized version')

    # Patch to add ppc64le in config.guess
    patch('ibm-ppc64le.patch', when='@:2.11.1')

    # Patch to build shared libraries on Darwin
    patch('darwin-shared-libs-for-hypre-2.13.0.patch', when='+shared@2.13.0 platform=darwin')
    patch('darwin-shared-libs-for-hypre-2.14.0.patch', when='+shared@2.14.0 platform=darwin')
    patch('superlu-dist-link-2.15.0.patch', when='+superlu-dist @2.15:')
    patch('superlu-dist-link-2.14.0.patch', when='+superlu-dist @:2.14.0')

    depends_on("mpi", when='+mpi')
    depends_on("blas")
    depends_on("lapack")
    depends_on('superlu-dist', when='+superlu-dist+mpi')

    # Patch to build shared libraries on Darwin does not apply to
    # versions before 2.13.0
    conflicts("+shared@:2.12.99 platform=darwin")

    def url_for_version(self, version):
        if version >= Version('2.12.0'):
            url = 'https://github.com/LLNL/hypre/archive/v{0}.tar.gz'
        else:
            url = 'http://computation.llnl.gov/project/linear_solvers/download/hypre-{0}.tar.gz'

        return url.format(version)

    def install(self, spec, prefix):
        # Note: --with-(lapack|blas)_libs= needs space separated list of names
        lapack = spec['lapack'].libs
        blas = spec['blas'].libs

        configure_args = [
            '--prefix=%s' % prefix,
            '--with-lapack-libs=%s' % ' '.join(lapack.names),
            '--with-lapack-lib-dirs=%s' % ' '.join(lapack.directories),
            '--with-blas-libs=%s' % ' '.join(blas.names),
            '--with-blas-lib-dirs=%s' % ' '.join(blas.directories)
        ]

        if '+mpi' in self.spec:
            os.environ['CC'] = spec['mpi'].mpicc
            os.environ['CXX'] = spec['mpi'].mpicxx
            os.environ['F77'] = spec['mpi'].mpif77
            configure_args.append('--with-MPI')
        else:
            configure_args.append('--without-MPI')

        if '+int64' in self.spec:
            configure_args.append('--enable-bigint')

        if '+shared' in self.spec:
            configure_args.append("--enable-shared")

        if '~internal-superlu' in self.spec:
            configure_args.append("--without-superlu")
            # MLI and FEI do not build without superlu on Linux
            configure_args.append("--without-mli")
            configure_args.append("--without-fei")

        if 'superlu-dist' in self.spec:
            configure_args.append('--with-dsuperlu-include=%s' %
                                  spec['superlu-dist'].prefix.include)
            configure_args.append('--with-dsuperlu-lib=%s' %
                                  spec['superlu-dist'].libs)
            configure_args.append('--with-dsuperlu')

        if '+debug' in self.spec:
            configure_args.append("--enable-debug")
        else:
            configure_args.append("--disable-debug")

        # Hypre's source is staged under ./src so we'll have to manually
        # cd into it.
        with working_dir("src"):
            configure(*configure_args)

            make()
            if self.run_tests:
                make("check")
                make("test")
                Executable(join_path('test', 'ij'))()
                sstruct = Executable(join_path('test', 'struct'))
                sstruct()
                sstruct('-in', 'test/sstruct.in.default', '-solver', '40',
                        '-rhsone')
            make("install")

    @property
    def headers(self):
        """Export the main hypre header, HYPRE.h; all other headers can be found
        in the same directory.
        Sample usage: spec['hypre'].headers.cpp_flags
        """
        hdrs = find_headers('HYPRE', self.prefix.include, recursive=False)
        return hdrs or None

    @property
    def libs(self):
        """Export the hypre library.
        Sample usage: spec['hypre'].libs.ld_flags
        """
        search_paths = [[self.prefix.lib, False], [self.prefix.lib64, False],
                        [self.prefix, True]]
        is_shared = '+shared' in self.spec
        for path, recursive in search_paths:
            libs = find_libraries('libHYPRE', root=path,
                                  shared=is_shared, recursive=recursive)
            if libs:
                return libs
        return None
<commit_msg>hypre: add @osborn9 to maintainers (#11233)<commit_after># Copyright 2013-2019 Lawrence Livermore National Security, LLC and other
# Spack Project Developers. See the top-level COPYRIGHT file for details.
#
# SPDX-License-Identifier: (Apache-2.0 OR MIT)

from spack import *
import os
import sys

class Hypre(Package):
    """Hypre is a library of high performance preconditioners that
       features parallel multigrid methods for both structured and
       unstructured grid problems."""

    homepage = "http://computation.llnl.gov/project/linear_solvers/software.php"
    url      = "https://github.com/LLNL/hypre/archive/v2.14.0.tar.gz"
    git      = "https://github.com/LLNL/hypre.git"

    maintainers = ['ulrikeyang', 'osborn9', 'balay']

    version('develop', branch='master')
    version('2.15.1', '877002d49f38b6a1434955baf79eff35')
    version('2.15.0', '4645acc49141069cae1d53de96107a08')
    version('2.14.0', 'ecde5cc807ec45bfb647e9f28d2eaea1')
    version('2.13.0', '4b688a5c15b6b5e3de5e045ae081b89b')
    version('2.12.1', 'c6fcb6d7e57cec1c7ce4a44da885068c')
    version('2.11.2', 'd507943a1a3ce5681c3308e2f3a6dd34')
    version('2.11.1', '3f02ef8fd679239a6723f60b7f796519')
    version('2.10.1', 'dc048c4cabb3cd549af72591474ad674')
    version('2.10.0b', '768be38793a35bb5d055905b271f5b8e')
    version('xsdk-0.2.0', tag='xsdk-0.2.0')

    # Versions 2.13.0 and later can be patched to build shared
    # libraries on Darwin; the patch for this capability does not
    # apply to version 2.12.1 and earlier due to changes in the build system
    # between versions 2.12.1 and 2.13.0.
    variant('shared', default=(sys.platform != 'darwin'),
            description="Build shared library (disables static library)")
    # SuperluDist have conflicting headers with those in Hypre
    variant('internal-superlu', default=True,
            description="Use internal Superlu routines")
    variant('superlu-dist', default=False,
            description='Activates support for SuperluDist')
    variant('int64', default=False,
            description="Use 64bit integers")
    variant('mpi', default=True, description='Enable MPI support')
    variant('debug', default=False,
            description='Build debug instead of optimized version')

    # Patch to add ppc64le in config.guess
    patch('ibm-ppc64le.patch', when='@:2.11.1')

    # Patch to build shared libraries on Darwin
    patch('darwin-shared-libs-for-hypre-2.13.0.patch', when='+shared@2.13.0 platform=darwin')
    patch('darwin-shared-libs-for-hypre-2.14.0.patch', when='+shared@2.14.0 platform=darwin')
    patch('superlu-dist-link-2.15.0.patch', when='+superlu-dist @2.15:')
    patch('superlu-dist-link-2.14.0.patch', when='+superlu-dist @:2.14.0')

    depends_on("mpi", when='+mpi')
    depends_on("blas")
    depends_on("lapack")
    depends_on('superlu-dist', when='+superlu-dist+mpi')

    # Patch to build shared libraries on Darwin does not apply to
    # versions before 2.13.0
    conflicts("+shared@:2.12.99 platform=darwin")

    def url_for_version(self, version):
        if version >= Version('2.12.0'):
            url = 'https://github.com/LLNL/hypre/archive/v{0}.tar.gz'
        else:
            url = 'http://computation.llnl.gov/project/linear_solvers/download/hypre-{0}.tar.gz'

        return url.format(version)

    def install(self, spec, prefix):
        # Note: --with-(lapack|blas)_libs= needs space separated list of names
        lapack = spec['lapack'].libs
        blas = spec['blas'].libs

        configure_args = [
            '--prefix=%s' % prefix,
            '--with-lapack-libs=%s' % ' '.join(lapack.names),
            '--with-lapack-lib-dirs=%s' % ' '.join(lapack.directories),
            '--with-blas-libs=%s' % ' '.join(blas.names),
            '--with-blas-lib-dirs=%s' % ' '.join(blas.directories)
        ]

        if '+mpi' in self.spec:
            os.environ['CC'] = spec['mpi'].mpicc
            os.environ['CXX'] = spec['mpi'].mpicxx
            os.environ['F77'] = spec['mpi'].mpif77
            configure_args.append('--with-MPI')
        else:
            configure_args.append('--without-MPI')

        if '+int64' in self.spec:
            configure_args.append('--enable-bigint')

        if '+shared' in self.spec:
            configure_args.append("--enable-shared")

        if '~internal-superlu' in self.spec:
            configure_args.append("--without-superlu")
            # MLI and FEI do not build without superlu on Linux
            configure_args.append("--without-mli")
            configure_args.append("--without-fei")

        if 'superlu-dist' in self.spec:
            configure_args.append('--with-dsuperlu-include=%s' %
                                  spec['superlu-dist'].prefix.include)
            configure_args.append('--with-dsuperlu-lib=%s' %
                                  spec['superlu-dist'].libs)
            configure_args.append('--with-dsuperlu')

        if '+debug' in self.spec:
            configure_args.append("--enable-debug")
        else:
            configure_args.append("--disable-debug")

        # Hypre's source is staged under ./src so we'll have to manually
        # cd into it.
        with working_dir("src"):
            configure(*configure_args)

            make()
            if self.run_tests:
                make("check")
                make("test")
                Executable(join_path('test', 'ij'))()
                sstruct = Executable(join_path('test', 'struct'))
                sstruct()
                sstruct('-in', 'test/sstruct.in.default', '-solver', '40',
                        '-rhsone')
            make("install")

    @property
    def headers(self):
        """Export the main hypre header, HYPRE.h; all other headers can be found
        in the same directory.
        Sample usage: spec['hypre'].headers.cpp_flags
        """
        hdrs = find_headers('HYPRE', self.prefix.include, recursive=False)
        return hdrs or None

    @property
    def libs(self):
        """Export the hypre library.
        Sample usage: spec['hypre'].libs.ld_flags
        """
        search_paths = [[self.prefix.lib, False], [self.prefix.lib64, False],
                        [self.prefix, True]]
        is_shared = '+shared' in self.spec
        for path, recursive in search_paths:
            libs = find_libraries('libHYPRE', root=path,
                                  shared=is_shared, recursive=recursive)
            if libs:
                return libs
        return None
<|endoftext|>