<commit_before># Copyright (c) 2003-2005 Maxim Sobolev. All rights reserved.
# Copyright (c) 2006-2007 Sippy Software, Inc. All rights reserved.
#
# This file is part of SIPPY, a free RFC3261 SIP stack and B2BUA.
#
# SIPPY 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 2 of the License, or
# (at your option) any later version.
#
# For a license to use the SIPPY software under conditions
# other than those described here, or to purchase support for this
# software, please contact Sippy Software, Inc. by e-mail at the
# following addresses: sales@sippysoft.com.
#
# SIPPY 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 this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
#
# $Id: SipMsg.py,v 1.6 2008/04/12 13:40:40 sobomax Exp $

from SipHeader import SipHeader
from SipGenericHF import SipGenericHF
from SipContentLength import SipContentLength
from SipContentType import SipContentType
from MsgBody import MsgBody
from ESipHeaderCSV import ESipHeaderCSV
from ESipHeaderIgnore import ESipHeaderIgnore

class SipMsg:
    headers = None
    body = None
    startline = None
    target = None
    source = None
    nated = False
    rtime = None

    def __init__(self, buf = None):
        self.headers = []
        if buf == None:
            return
        # Locate a body
        mbody = None
        for bdel in ('\r\n\r\n', '\r\r', '\n\n'):
            boff = buf.find(bdel)
            if boff != -1:
                mbody = buf[boff + len(bdel):]
                buf = buf[:boff]
                if len(mbody) == 0:
                    mbody = None
                break
        # Split message into lines and put aside start line
        lines = buf.splitlines()
        self.setSL(lines[0])
        i = 2
        while i < len(lines):
            if lines[i][0] in (' ', '\t'):
                lines[i - 1] += ' ' + lines[i].strip()
                del lines[i]
            else:
                i += 1
        # Parse headers
        for line in lines[1:]:
            try:
                self.headers.append(SipHeader(line, fixname = True))
            except ESipHeaderCSV, einst:
                for body in einst.bodys:
                    self.headers.append(SipHeader(name = einst.name, bodys = body))
            except ESipHeaderIgnore:
                continue
        if self.countHFs('via') == 0:
            raise Exception('Via HF is missed')
        if self.countHFs('content-length') > 0:
            blen = self.getHFBody('content-length').number
            if mbody == None:
                mblen = 0
            else:
                mblen = len(mbody)
            if blen == 0:
                mbody = None
                mblen = 0
            elif mbody == None or blen > mblen:
                if mbody != None and blen - mblen < 7 and mblen > 7 and mbody[-4:] == '\r\n\r\n':
                    # XXX: we should not really be doing this, but it appears to be
                    # a common off-by-one/two/.../six problem with SDPs generates by
                    # the consumer-grade devices.
                    print 'Truncated SIP body, %d bytes expected, %d received, fixing...' % (blen, mblen)
                    blen = mblen
                else:
                    # XXX: Should generate 400 Bad Request if such condition
                    # happens with request
                    raise Exception('Truncated SIP body, %d bytes expected, %d received' % (blen, mblen))
            elif blen < mblen:
                mbody = mbody[:blen]
                mblen = blen
        if mbody != None:
            ct = self.getHFs('content-type')
            if len(ct) > 0:
                self.body = MsgBody(mbody, str(ct[0].body).lower())
                self.delHFs('content-type')
                self.delHFs('content-length')
            else:
                self.body = MsgBody(mbody)

    def __str__(self):
        s = str(self.getSL()) + '\r\n'
        for header in self.headers:
            s += str(header) + '\r\n'
        if self.body != None:
            mbody = str(self.body)
            s += 'Content-Length: %d\r\n' % len(mbody)
            s += 'Content-Type: %s\r\n\r\n' % self.body.mtype
            s += mbody
        else:
            s += '\r\n'
        return s

    def setSL(self, startline):
        self.startline = startline

    def getSL(self):
        return self.startline

    def getHFs(self, name):
        return filter(lambda x: x.isName(name), self.headers)

    def countHFs(self, name):
        return len(filter(lambda x: x.isName(name), self.headers))

    def delHFs(self, name):
        self.headers = filter(lambda x: not x.isName(name), self.headers)

    def getHF(self, name):
        return filter(lambda x: x.isName(name), self.headers)[0]

    def getHFBodys(self, name):
        return map(lambda x: x.getBody(), filter(lambda x: x.isName(name), self.headers))

    def getHFBody(self, name):
        return filter(lambda x: x.isName(name), self.headers)[0].getBody()

    def replaceHeader(self, oheader, nheader):
        self.headers[self.headers.index(oheader)] = nheader

    def removeHeader(self, header):
        self.headers.remove(header)

    def appendHeader(self, header):
        self.headers.append(header)

    def appendHeaders(self, headers):
        self.headers.extend(headers)

    def insertHeaderAfter(self, iheader, header):
        self.headers.insert(self.headers.index(iheader) + 1, header)

    def insertHeaderBefore(self, iheader, header):
        self.headers.insert(self.headers.index(iheader), header)

    def getBody(self):
        return self.body

    def setBody(self, body):
        self.body = body

    def getTarget(self):
        return self.target

    def setTarget(self, address):
        self.target = address

    def getSource(self):
        return self.source

    def setSource(self, address):
        self.source = address

    def getTId(self, wCSM = False, wBRN = False):
        cseq = self.getHFBody('cseq')
        rval = [str(self.getHFBody('call-id')), self.getHFBody('from').getTag(), cseq.getCSeqNum()]
        if wCSM:
            rval.append(cseq.getCSeqMethod())
        if wBRN:
            rval.append(self.getHFBody('via').getBranch())
        return tuple(rval)

    def getTIds(self):
        call_id = str(self.getHFBody('call-id'))
        ftag = self.getHFBody('from').getTag()
        cseq, method = self.getHFBody('cseq').getCSeq()
        rval = []
        for via in self.getHFBodys('via'):
            rval.append((call_id, ftag, cseq, method, via.getBranch()))
        return tuple(rval)
<commit_msg>o Also generate exception if From or To header field are missed. They are mandatory.<commit_after># Copyright (c) 2003-2005 Maxim Sobolev. All rights reserved.
# Copyright (c) 2006-2007 Sippy Software, Inc. All rights reserved.
#
# This file is part of SIPPY, a free RFC3261 SIP stack and B2BUA.
#
# SIPPY 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 2 of the License, or
# (at your option) any later version.
#
# For a license to use the SIPPY software under conditions
# other than those described here, or to purchase support for this
# software, please contact Sippy Software, Inc. by e-mail at the
# following addresses: sales@sippysoft.com.
#
# SIPPY 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 this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
#
# $Id: SipMsg.py,v 1.7 2008/04/24 23:48:26 sobomax Exp $

from SipHeader import SipHeader
from SipGenericHF import SipGenericHF
from SipContentLength import SipContentLength
from SipContentType import SipContentType
from MsgBody import MsgBody
from ESipHeaderCSV import ESipHeaderCSV
from ESipHeaderIgnore import ESipHeaderIgnore

class SipMsg:
    headers = None
    body = None
    startline = None
    target = None
    source = None
    nated = False
    rtime = None

    def __init__(self, buf = None):
        self.headers = []
        if buf == None:
            return
        # Locate a body
        mbody = None
        for bdel in ('\r\n\r\n', '\r\r', '\n\n'):
            boff = buf.find(bdel)
            if boff != -1:
                mbody = buf[boff + len(bdel):]
                buf = buf[:boff]
                if len(mbody) == 0:
                    mbody = None
                break
        # Split message into lines and put aside start line
        lines = buf.splitlines()
        self.setSL(lines[0])
        i = 2
        while i < len(lines):
            if lines[i][0] in (' ', '\t'):
                lines[i - 1] += ' ' + lines[i].strip()
                del lines[i]
            else:
                i += 1
        # Parse headers
        for line in lines[1:]:
            try:
                self.headers.append(SipHeader(line, fixname = True))
            except ESipHeaderCSV, einst:
                for body in einst.bodys:
                    self.headers.append(SipHeader(name = einst.name, bodys = body))
            except ESipHeaderIgnore:
                continue
        if self.countHFs('via') == 0:
            raise Exception('Via HF is missed')
        if self.countHFs('to') == 0:
            raise Exception('To HF is missed')
        if self.countHFs('from') == 0:
            raise Exception('From HF is missed')
        if self.countHFs('content-length') > 0:
            blen = self.getHFBody('content-length').number
            if mbody == None:
                mblen = 0
            else:
                mblen = len(mbody)
            if blen == 0:
                mbody = None
                mblen = 0
            elif mbody == None:
                # XXX: Should generate 400 Bad Request if such condition
                # happens with request
                raise Exception('Missed SIP body, %d bytes expected' % blen)
            elif blen > mblen:
                if blen - mblen < 7 and mblen > 7 and mbody[-4:] == '\r\n\r\n':
                    # XXX: we should not really be doing this, but it appears to be
                    # a common off-by-one/two/.../six problem with SDPs generates by
                    # the consumer-grade devices.
                    print 'Truncated SIP body, %d bytes expected, %d received, fixing...' % (blen, mblen)
                    blen = mblen
                elif blen - mblen == 2 and mbody[-2:] == '\r\n':
                    # Missed last 2 \r\n is another common problem.
                    print 'Truncated SIP body, %d bytes expected, %d received, fixing...' % (blen, mblen)
                    mbody += '\r\n'
                else:
                    # XXX: Should generate 400 Bad Request if such condition
                    # happens with request
                    raise Exception('Truncated SIP body, %d bytes expected, %d received' % (blen, mblen))
            elif blen < mblen:
                mbody = mbody[:blen]
                mblen = blen
        if mbody != None:
            ct = self.getHFs('content-type')
            if len(ct) > 0:
                self.body = MsgBody(mbody, str(ct[0].body).lower())
                self.delHFs('content-type')
                self.delHFs('content-length')
            else:
                self.body = MsgBody(mbody)

    def __str__(self):
        s = str(self.getSL()) + '\r\n'
        for header in self.headers:
            s += str(header) + '\r\n'
        if self.body != None:
            mbody = str(self.body)
            s += 'Content-Length: %d\r\n' % len(mbody)
            s += 'Content-Type: %s\r\n\r\n' % self.body.mtype
            s += mbody
        else:
            s += '\r\n'
        return s

    def setSL(self, startline):
        self.startline = startline

    def getSL(self):
        return self.startline

    def getHFs(self, name):
        return filter(lambda x: x.isName(name), self.headers)

    def countHFs(self, name):
        return len(filter(lambda x: x.isName(name), self.headers))

    def delHFs(self, name):
        self.headers = filter(lambda x: not x.isName(name), self.headers)

    def getHF(self, name):
        return filter(lambda x: x.isName(name), self.headers)[0]

    def getHFBodys(self, name):
        return map(lambda x: x.getBody(), filter(lambda x: x.isName(name), self.headers))

    def getHFBody(self, name):
        return filter(lambda x: x.isName(name), self.headers)[0].getBody()

    def replaceHeader(self, oheader, nheader):
        self.headers[self.headers.index(oheader)] = nheader

    def removeHeader(self, header):
        self.headers.remove(header)

    def appendHeader(self, header):
        self.headers.append(header)

    def appendHeaders(self, headers):
        self.headers.extend(headers)

    def insertHeaderAfter(self, iheader, header):
        self.headers.insert(self.headers.index(iheader) + 1, header)

    def insertHeaderBefore(self, iheader, header):
        self.headers.insert(self.headers.index(iheader), header)

    def getBody(self):
        return self.body

    def setBody(self, body):
        self.body = body

    def getTarget(self):
        return self.target

    def setTarget(self, address):
        self.target = address

    def getSource(self):
        return self.source

    def setSource(self, address):
        self.source = address

    def getTId(self, wCSM = False, wBRN = False):
        cseq = self.getHFBody('cseq')
        rval = [str(self.getHFBody('call-id')), self.getHFBody('from').getTag(), cseq.getCSeqNum()]
        if wCSM:
            rval.append(cseq.getCSeqMethod())
        if wBRN:
            rval.append(self.getHFBody('via').getBranch())
        return tuple(rval)

    def getTIds(self):
        call_id = str(self.getHFBody('call-id'))
        ftag = self.getHFBody('from').getTag()
        cseq, method = self.getHFBody('cseq').getCSeq()
        rval = []
        for via in self.getHFBodys('via'):
            rval.append((call_id, ftag, cseq, method, via.getBranch()))
        return tuple(rval)
<|endoftext|>


<commit_before># Authors: Denis Engemann <denis.engemann@gmail.com>
#
# License: Simplified BSD

import os.path as op
import warnings

from nose.tools import assert_raises

from mne.decoding import GeneralizationAcrossTime
from mne import io, Epochs, read_events, pick_types
from mne.utils import requires_sklearn, run_tests_if_main
import matplotlib
matplotlib.use('Agg')  # for testing don't use X server

data_dir = op.join(op.dirname(__file__), '..', '..', 'io', 'tests', 'data')
raw_fname = op.join(data_dir, 'test_raw.fif')
event_name = op.join(data_dir, 'test-eve.fif')

warnings.simplefilter('always')  # enable b/c these tests throw warnings

# Set our plotters to test mode

tmin, tmax = -0.2, 0.5
event_id = dict(aud_l=1, vis_l=3)
event_id_gen = dict(aud_l=2, vis_l=4)

@requires_sklearn
def _get_data():
    """Aux function for testing GAT viz"""
    gat = GeneralizationAcrossTime()
    raw = io.Raw(raw_fname, preload=False)
    events = read_events(event_name)
    picks = pick_types(raw.info, meg='mag', stim=False, ecg=False,
                       eog=False, exclude='bads')
    picks = picks[1:13:3]
    decim = 30
    # Test on time generalization within one condition
    with warnings.catch_warnings(record=True):
        epochs = Epochs(raw, events, event_id, tmin, tmax, picks=picks,
                        baseline=(None, 0), preload=True, decim=decim)

    # Test default running
    gat = GeneralizationAcrossTime()
    gat.fit(epochs)
    gat.score(epochs)
    return gat

def test_gat_plot_matrix():
    """Test GAT matrix plot"""
    gat = _get_data()
    gat.plot()
    del gat.scores_
    assert_raises(RuntimeError, gat.plot)

def test_gat_plot_diagonal():
    """Test GAT diagonal plot"""
    gat = _get_data()
    gat.plot_diagonal()
    del gat.scores_
    assert_raises(RuntimeError, gat.plot)

def test_gat_plot_slice():
    """Test GAT slice plot"""
    gat = _get_data()
    gat.plot_slice(gat.train_time['times_'][0])
    # test invalid time point
    assert_raises(ValueError, gat.plot_slice, -1.)
    # test float type
    assert_raises(ValueError, gat.plot_slice, 1)
    del gat.scores_
    assert_raises(RuntimeError, gat.plot)

run_tests_if_main()
<commit_msg>add test_gat_chance_level<commit_after># Authors: Denis Engemann <denis.engemann@gmail.com>
#
# License: Simplified BSD

import os.path as op
import warnings

from nose.tools import assert_raises, assert_equals

from mne.decoding import GeneralizationAcrossTime
from mne import io, Epochs, read_events, pick_types
from mne.utils import requires_sklearn, run_tests_if_main
import matplotlib
matplotlib.use('Agg')  # for testing don't use X server

data_dir = op.join(op.dirname(__file__), '..', '..', 'io', 'tests', 'data')
raw_fname = op.join(data_dir, 'test_raw.fif')
event_name = op.join(data_dir, 'test-eve.fif')

warnings.simplefilter('always')  # enable b/c these tests throw warnings

@requires_sklearn
def _get_data(tmin=-0.2, tmax=0.5, event_id=dict(aud_l=1, vis_l=3),
              event_id_gen=dict(aud_l=2, vis_l=4)):
    """Aux function for testing GAT viz"""
    gat = GeneralizationAcrossTime()
    raw = io.Raw(raw_fname, preload=False)
    events = read_events(event_name)
    picks = pick_types(raw.info, meg='mag', stim=False, ecg=False,
                       eog=False, exclude='bads')
    picks = picks[1:13:3]
    decim = 30
    # Test on time generalization within one condition
    with warnings.catch_warnings(record=True):
        epochs = Epochs(raw, events, event_id, tmin, tmax, picks=picks,
                        baseline=(None, 0), preload=True, decim=decim)

    # Test default running
    gat = GeneralizationAcrossTime()
    gat.fit(epochs)
    gat.score(epochs)
    return gat

def test_gat_plot_matrix():
    """Test GAT matrix plot"""
    gat = _get_data()
    gat.plot()
    del gat.scores_
    assert_raises(RuntimeError, gat.plot)

def test_gat_plot_diagonal():
    """Test GAT diagonal plot"""
    gat = _get_data()
    gat.plot_diagonal()
    del gat.scores_
    assert_raises(RuntimeError, gat.plot)

def test_gat_plot_slice():
    """Test GAT slice plot"""
    gat = _get_data()
    gat.plot_slice(gat.train_time['times_'][0])
    # test invalid time point
    assert_raises(ValueError, gat.plot_slice, -1.)
    # test float type
    assert_raises(ValueError, gat.plot_slice, 1)
    del gat.scores_
    assert_raises(RuntimeError, gat.plot)

def test_gat_chance_level():
    """Test GAT plot_slice chance level"""
    chance = lambda ax: ax.get_children()[1].get_lines()[1].get_ydata()[0]
    gat = _get_data()
    ax = gat.plot_diagonal()
    assert_equals(chance(ax), .5)
    gat = _get_data(event_id=dict(aud_l=1, vis_l=3, aud_r=2, vis_r=4))
    ax = gat.plot_diagonal()
    assert_equals(chance(ax), .25)
    del gat.scores_
    assert_raises(RuntimeError, gat.plot)

run_tests_if_main()
<|endoftext|>


<commit_before># Copyright (c) 2014 Lukas Lalinsky, Wieland Hoffmann
# License: MIT, see LICENSE for details
import futures

from . import config, querying, util
from .schema import SCHEMA
from ConfigParser import Error
from collections import defaultdict, namedtuple
from functools import partial
from logging import getLogger
from urllib2 import URLError

logger = getLogger("sir")

def _future_callback(info, future):
    logger.info("Done: %s: %s, %s", info, future.result(), future.exception())

_FutureInfo = namedtuple("_FutureInfo", "entity lower upper")

def reindex(entities, debug=False):
    """
    Reindexes all entity types in ``entities``.

    :param entities:
    :type entities: A list of :class:`sir.schema.searchentities.SearchEntity`
                    objects
    :param bool debug:
    """
    known_entities = SCHEMA.keys()
    if entities is not None:
        _entities = []
        for e in entities:
            _entities.extend(e.split(','))
        unknown_entities = set(_entities) - set(known_entities)
        if unknown_entities:
            raise ValueError("{0} are unkown entity types".format(unknown_entities))
    else:
        _entities = known_entities

    try:
        db_uri = config.CFG.get("database", "uri")
        solr_uri = config.CFG.get("solr", "uri")
    except Error, e:
        logger.error("%s - please configure this application in the file config.ini", e.message)
        return

    db_session = util.db_session(db_uri, debug)

    query_batch_size = config.CFG.getint("sir", "query_batch_size")
    entity_to_index_func = defaultdict(list)
    for e in _entities:
        try:
            solr_connection = util.solr_connection(solr_uri, e)
        except URLError, e:
            logger.error("Establishing a connection to Solr at %s failed: %s",
                         solr_uri, e.reason)
            return

        search_entity = SCHEMA[e]
        query = querying.build_entity_query(search_entity)

        num_rows = querying.max_id_of(search_entity, db_session)
        model = search_entity.model

        try:
            importlimit = config.CFG.getint("sir", "importlimit")
            logger.info("Applying a limit of %i", importlimit)
            query = query.filter(model.id < importlimit)
        except Error, e:
            pass

        lower_bound = 0
        for upper_bound in xrange(lower_bound + query_batch_size,
                                  num_rows + query_batch_size,
                                  query_batch_size):
            logger.debug("Adding a Query for %s from %i to %i", e, lower_bound,
                         upper_bound)
            new_query = query.filter(model.id >= lower_bound).\
                filter(model.id <= upper_bound)
            info = _FutureInfo(e, lower_bound, upper_bound)
            lower_bound = upper_bound + 1
            entity_to_index_func[e].append((partial(index_entity,
                                           solr_connection=solr_connection,
                                           query=new_query),
                                           info))

    with futures.ThreadPoolExecutor(max_workers=config.CFG.getint("sir", "import_threads")) as executor:
        for e, v in entity_to_index_func.iteritems():
            for f, info in v:
                future = executor.submit(f, db_session=db_session, search_entity=SCHEMA[e])
                future.add_done_callback(partial(_future_callback, info))

def index_entity(db_session, solr_connection, query, search_entity):
    """
    Indexes a single entity type.

    :param sqlalchemy.orm.scoping.scoped_session db_session:
    :param solr.Solr solr_connection:
    :param sqlalchemy.orm.query.Query query:
    :param sir.schema.searchentities.SearchEntity search_entity:
    :raises solr.SolrException:
    """
    session = db_session()
    query = query.with_session(session)
    data = []
    batch_size = config.CFG.getint("solr", "batch_size")
    try:
        for row in query:
            data.append(query_result_to_dict(search_entity, row))
            if len(data) == batch_size:
                solr_connection.add_many(data)
                logger.info("Sent %i records to %s", len(data),
                            solr_connection.url)
                data = []
        if len(data) > 0:
            # There's some left-over data that's not large enough for a
            # complete batch.
            solr_connection.add_many(data)
            logger.info("Sent %i records to %s", len(data),
                        solr_connection.url)
    finally:
        db_session.remove()

def query_result_to_dict(entity, obj):
    """
    Converts the result of single ``query`` result into a dictionary via the
    field specification of ``entity``.

    :param sir.schema.searchentities.SearchEntity entity:
    :param obj: A :ref:`declarative <sqla:declarative_toplevel>` object.
    """
    data = {}
    for field in entity.fields:
        logger.debug("Field: %s", field.name)
        fieldname = field.name
        tempvals = set()
        for path in field.paths:
            logger.debug("Path: %s", path)
            for val in querying._iterate_path_values(path, obj):
                tempvals.add(val)
        logger.debug("Values: %s", tempvals)
        if field.transformfunc is not None:
            tempvals = field.transformfunc(tempvals)
            logger.debug("Values after applying transformfunc: %s", tempvals)
        if isinstance(tempvals, set) and len(tempvals) == 1:
            tempvals = tempvals.pop()
        data[fieldname] = tempvals
    return data
<commit_msg>USe between for upper and lower bounds<commit_after># Copyright (c) 2014 Lukas Lalinsky, Wieland Hoffmann
# License: MIT, see LICENSE for details
import futures

from . import config, querying, util
from .schema import SCHEMA
from ConfigParser import Error
from collections import defaultdict, namedtuple
from functools import partial
from logging import getLogger
from sqlalchemy import between
from urllib2 import URLError

logger = getLogger("sir")

def _future_callback(info, future):
    logger.info("Done: %s: %s, %s", info, future.result(), future.exception())

_FutureInfo = namedtuple("_FutureInfo", "entity lower upper")

def reindex(entities, debug=False):
    """
    Reindexes all entity types in ``entities``.

    :param entities:
    :type entities: A list of :class:`sir.schema.searchentities.SearchEntity`
                    objects
    :param bool debug:
    """
    known_entities = SCHEMA.keys()
    if entities is not None:
        _entities = []
        for e in entities:
            _entities.extend(e.split(','))
        unknown_entities = set(_entities) - set(known_entities)
        if unknown_entities:
            raise ValueError("{0} are unkown entity types".format(unknown_entities))
    else:
        _entities = known_entities

    try:
        db_uri = config.CFG.get("database", "uri")
        solr_uri = config.CFG.get("solr", "uri")
    except Error, e:
        logger.error("%s - please configure this application in the file config.ini", e.message)
        return

    db_session = util.db_session(db_uri, debug)

    query_batch_size = config.CFG.getint("sir", "query_batch_size")
    entity_to_index_func = defaultdict(list)
    for e in _entities:
        try:
            solr_connection = util.solr_connection(solr_uri, e)
        except URLError, e:
            logger.error("Establishing a connection to Solr at %s failed: %s",
                         solr_uri, e.reason)
            return

        search_entity = SCHEMA[e]
        query = querying.build_entity_query(search_entity)

        num_rows = querying.max_id_of(search_entity, db_session)
        model = search_entity.model

        try:
            importlimit = config.CFG.getint("sir", "importlimit")
            logger.info("Applying a limit of %i", importlimit)
            query = query.filter(model.id < importlimit)
        except Error, e:
            pass

        lower_bound = 0
        for upper_bound in xrange(lower_bound + query_batch_size,
                                  num_rows + query_batch_size,
                                  query_batch_size):
            logger.debug("Adding a Query for %s from %i to %i", e, lower_bound,
                         upper_bound)
            new_query = query.filter(model.id.between(lower_bound, upper_bound))
            info = _FutureInfo(e, lower_bound, upper_bound)
            lower_bound = upper_bound + 1
            entity_to_index_func[e].append((partial(index_entity,
                                           solr_connection=solr_connection,
                                           query=new_query),
                                           info))

    with futures.ThreadPoolExecutor(max_workers=config.CFG.getint("sir", "import_threads")) as executor:
        for e, v in entity_to_index_func.iteritems():
            for f, info in v:
                future = executor.submit(f, db_session=db_session, search_entity=SCHEMA[e])
                future.add_done_callback(partial(_future_callback, info))

def index_entity(db_session, solr_connection, query, search_entity):
    """
    Indexes a single entity type.

    :param sqlalchemy.orm.scoping.scoped_session db_session:
    :param solr.Solr solr_connection:
    :param sqlalchemy.orm.query.Query query:
    :param sir.schema.searchentities.SearchEntity search_entity:
    :raises solr.SolrException:
    """
    session = db_session()
    query = query.with_session(session)
    data = []
    batch_size = config.CFG.getint("solr", "batch_size")
    try:
        for row in query:
            data.append(query_result_to_dict(search_entity, row))
            if len(data) == batch_size:
                solr_connection.add_many(data)
                logger.info("Sent %i records to %s", len(data),
                            solr_connection.url)
                data = []
        if len(data) > 0:
            # There's some left-over data that's not large enough for a
            # complete batch.
            solr_connection.add_many(data)
            logger.info("Sent %i records to %s", len(data),
                        solr_connection.url)
    finally:
        db_session.remove()

def query_result_to_dict(entity, obj):
    """
    Converts the result of single ``query`` result into a dictionary via the
    field specification of ``entity``.

    :param sir.schema.searchentities.SearchEntity entity:
    :param obj: A :ref:`declarative <sqla:declarative_toplevel>` object.
    """
    data = {}
    for field in entity.fields:
        logger.debug("Field: %s", field.name)
        fieldname = field.name
        tempvals = set()
        for path in field.paths:
            logger.debug("Path: %s", path)
            for val in querying._iterate_path_values(path, obj):
                tempvals.add(val)
        logger.debug("Values: %s", tempvals)
        if field.transformfunc is not None:
            tempvals = field.transformfunc(tempvals)
            logger.debug("Values after applying transformfunc: %s", tempvals)
        if isinstance(tempvals, set) and len(tempvals) == 1:
            tempvals = tempvals.pop()
        data[fieldname] = tempvals
    return data
<|endoftext|>


<commit_before>#!/usr/bin/python3
import Room, hero, Character, objects, inventory, userInput
import readline, traceback, sqlite3
from gameEvents import EventManager
from musicPlayer import MusicMenu
from menus import Menu, MenuOption
from sqlTable import SQLTable

global sqlDir
sqlDir = "gameFiles"

def loadSQLFile(db, fileName):
	sqlLines = []
	with open(fileName, 'r') as f:
		for line in f:
			sqlLines.append(line)
	db.executescript('\n'.join(sqlLines))

class GameTable(SQLTable):
	def __init__(self, db):
		SQLTable.__init__(self, db)
		self.table = 'game'
		self.codeName = 'code'
		self.stage = self.elementTable.addElement(title = 'Game Stage', name = 'stage', value = None, elementType = 'INT')

class GameCommands(object):
	'''
	self.currRoom is the room the character is in
	self.inspection is the object, item, room, character that is being looked at. Each of these needs to have a dictionary of possible commands. The games superset of commands always apply.
	'''
	def __init__(self, db):
		self.db = db
		self.currRoom = None
		self.inspection = None
		self.buckPasser = None
		self.commands = {
			'start':userInput.Command(func=self.startMenu, takesArgs=False, descrip = 'Start Menu'),
			'help':userInput.Command(func=self.printHelp, takesArgs=False, descrip = 'No one can save you now'),
			'move':userInput.Command(func=self.move, takesArgs=True, descrip = 'Move to a neighboring room'),
			'describe':userInput.Command(func=self.describe, takesArgs=True, descrip = 'Description of a person or thing'),
			'talk':userInput.Command(func=self.talkTo, takesArgs=True, descrip = 'Really need this fucking explained you dim wit?'),
			'items':userInput.Command(func=self.listItems, takesArgs=True, descrip = 'List what you have on you'),
			'search':userInput.Command(func=self.search, takesArgs=True, descrip = 'Root around for something'),
			'look':userInput.Command(func=self.look, takesArgs=False, descrip = 'Look around'),
			'mute':userInput.Command(func=self._mute, takesArgs=False, hide = True, descrip = 'Mute the sound'),
			'inspect':userInput.Command(func=self._inspect, takesArgs=True, hide = False, descrip = 'Inspect an object')
			}

		self.neighbors = None

	def _getObject(self, objName = None):
		if self.currRoom.objects == None:
			return False
		try:
			self.inspection = [obj for obj in self.currRoom.objects if obj.objName.value.lower() == objName.lower()][0]
			return True
		except IndexError:
			raise UserWarning('No Object Found')

	def _getCharacter(self, charName = None):
		if self.currRoom.characters == None:
			return False
		try:
			self.inspection = [character for character in self.currRoom.characters if character.charName.value.lower() == charName.lower()][0]
			return True
		except IndexError:
			raise UserWarning('No Character Found')

	def _getRoom(self, roomName = None):
		try:
			code = self.neighbors[roomName.title()]
		except KeyError:
			return False
		self.inspection = Room.Room(self.db)
		self.inspection.setCode(code)

	def _getItem(self, itemName = None):
		if self.buckPasser.inventory == None or self.buckPasser.inventory.items ==  None or len(self.buckPasser.inventory.items) < 1:
			#print('You ain\'t got shit')
			return False
		try:
			self.inspection = [item for item in self.buckPasser.inventory.items if item.item.subType.value.lower() == itemName.lower()][0]
			return True
		except IndexError:
			raise UserWarning('No Item Found')

	def __runCommand(self, command = None, args = None):
		try:
			if(args == None):
				func = self.inspection.__dict__['commands'][command].run()
			else:
				func = self.inspection.__dict__['commands'][command].run(args)
		except KeyError:
			pass

	def __makeCommand(self, subject = None, command = None, args = None, onObject = False, onCharacter = False, onItem = False):
		if type(subject) in [list, set, tuple]:
			subject = ' '.join(subject).lower().strip()
		try:
			#set the type of items its supposed to search for
			if onObject:
				try:
					if self._getObject(subject):
						self.__runCommand(command = command, args = args)
				except UserWarning:
					pass
			if onCharacter:
				try:
					if self._getCharacter(subject):
						self.__runCommand(command = command, args = args)
				except UserWarning:
					pass

		except UserWarning as uw:
			print(uw)
			userInput.printToScreen("I don't know what you're jabbering about you driveling idiot")

	def printCommands(self):
		userInput.printToScreen("Some of the Available Commands:")

		maxLen = max([len(command) for command in self.commands if not self.commands[command].hide])
		for command in self.commands:
			if not self.commands[command].hide:
				userInput.printToScreen('{0}{1} -> {2.descrip}'.format(' '*(maxLen - len(command)), command, self.commands[command]))

	def printHelp(self):
		userInput.printToScreen('\n\nNo one can help your sorry ass, just go score some blow.\n\n')
		self.printCommands()

	def describe(self, subject = None):
		if self.currRoom.objects is None and self.currRoom.characters is None:
			userInput.printToScreen("Theres nothing here")
			return

		if subject is None:
			options = []
			if self.currRoom.objects is not None:
				options.extend(obj.objName.value for obj in self.currRoom.objects)

			if self.currRoom.characters is not None:
				options.extend(char.charName.value for char in self.currRoom.characters)

			if len(options) == 1:
				subject = options
			else:
				subject = [userInput.printSelectGetOption(options = options, cursor = 'Describe What?> ')]
				if subject[0] is None:
					return
		self.__makeCommand(subject = subject, command = 'describe', onObject = True, onCharacter = True)

	def _inspect(self, subject = None):
		if self.currRoom.objects is None:
			userInput.printToScreen("Theres nothing here")
			return

		if subject is None:
			if len(self.currRoom.objects) == 1:
				subject = self.currRoom.objects[0].objName.value
			else:
				options = [obj.objName.value for obj in self.currRoom.objects]
				subject = [userInput.printSelectGetOption(options = options, cursor = 'Inspect What?> ')]
				if subject[0] is None:
					return
		self.__makeCommand(subject = subject, command = 'inspect', onObject = True)

	def talkTo(self, charName = None):
		if self.currRoom.characters == None:
			userInput.printToScreen("No one is here")
			return
		elif len(self.currRoom.characters) == 1:
			charName = self.currRoom.characters[0].charName.value

		if type(charName) in [tuple, list]:
			charName = ' '.join(charName).upper()
		elif charName == None:
			charNames = [char.charName.value for char in self.currRoom.characters]
			charName = userInput.printSelectGetOption(options = charNames, cursor = 'To Whom?> ', exitPrompt = 'No one')
			if charName is None:
				return
		char = [char for char in self.currRoom.characters if char.charName.value.upper() == charName.upper()]
		if len(char) > 0:
			char[0].talk()

	def getRoomNeighbors(self):
		room = Room.Room(self.db)
		self.neighbors = {}
		for code in userInput.parseCSVNumString(self.currRoom.neighbors.value):
			room.setCode(code)
			room.readFromDB(self.stage)
			self.neighbors.update({room.roomName.value : int(code)})

	def setupNewRoom(self):
		'''
		Sets up self.currRoom properly
		'''
		self.currRoom.loadRoom(self.stage)
		self.currRoom.inventory.charInventory = self.buckPasser.inventory
		self.currRoom.inventory.refreshList()

	def move(self, room = None):
		'''
		change current room to an available neighbor.
		if the room code is a valid room, change the currRoom object to the corresponding room and print the room description
		'''
		from operator import itemgetter
		if room == None:
			options = ['Stay Put']
			options.extend(room[0] for room in sorted(self.neighbors.items(), key=itemgetter(1)))#print with lowest room code first
			selection = userInput.printSelect(options = options, cursor = 'To Where?> ')
			if(selection == 0):
				return
			else:
				room = options[selection]
		else:
			try:
				room = ' '.join(room).title()
			except AttributeError:
				return

		if room in self.neighbors.keys():
			self.currRoom.writeRoom()
			self._getRoom(room)

			self.currRoom = self.inspection
			self.setupNewRoom()
			self.currRoom.look()

			self.getRoomNeighbors()
			self.buckPasser.inventory.roomInventory = self.currRoom.inventory
			self.currRoom.inventory.charInventory = self.buckPasser.inventory
			self.currRoom.inventory.refreshList()
		else:
			raise UserWarning("{} is not a valid neighbor".format(room))

	def listItems(self):
		self.buckPasser.listItems()

	def search(self):
		self.currRoom.commands['search'].run()

	def look(self):
		self.currRoom.look()

class GameMenu(Menu):
	def __init__(self, db):
		Menu.__init__(self, db, title =  'Start Menu', description="", cursor = " Start Menu> ")
		self.addOption(MenuOption(db = db, title = "Quit Game", description="Exit Game", commit = True, clear=True, action = self._exit))
		self.addOption(MenuOption(db = db, title = "Save", description="", commit = True, clear=True, action = self._save))
		self.addOption(MenuOption(db = db, title = "Load", description="Load a previous save", commit = False, clear=True, action=self._load))
		self.addOption(MenuOption(db = db, title = "Music", description="Play some sultry tunes", commit = False, clear=True, action=self.music))

	def startMenu(self):
		self.runMenu()
		self.currRoom.look()

class StartGame(Menu):
	def __init__(self):
		Menu.__init__(self, db = None, title =  'Detective Buck Passer', description="", cursor = "Game Menu> ")
		self.addOption(MenuOption(db = None, title = "New Game", description="It's your own funeral", commit = False, action = self._newGame))
		self.addOption(MenuOption(db = None, title = "Load", description="Load a previous save", commit = False, action=self._loadGame))

	def _newGame(self, dbFile = None):
		import os
		if dbFile is None:
			# Get a new game name
			dbFile = "game_{}.db".format(userInput.inputUniversal('Enter a save name> ').upper().replace(' ','_').strip())

		if os.path.isfile(dbFile):
			raise UserWarning("File name {} exists".format(dbFile))

		db = sqlite3.connect(dbFile)
		# make the new db ensuring its not writing over another file of the same name
		sqlFiles = ['sqlStructure.sql', 'items.sql', 'events.sql', 'stage0.sql']
		for sqlFile in sqlFiles:
			loadSQLFile(db = db, fileName = os.path.join(sqlDir, sqlFile))
		db.commit()
		return dbFile

	def _loadGame(self):
		import glob
		# Print a list of the available saves, have them choose one and load it in
		dbs = glob.glob('game_*.db')
		if len(dbs) < 1:
			print('No Saved Games')
			return

		dbFile = userInput.printSelectGetOption(options = dbs, cursor = "Choose a saved game> ")
		if dbFile is not None:
			db = sqlite3.connect(dbFile)
			db.commit()
			return dbFile

class Game(GameCommands, GameMenu):
	def __init__(self, dbFile):
		import os
		self.stage = None
		self.musicProcess = None
		os.stderr = open('log.log', 'w+')
		self.db = sqlite3.connect(dbFile)
		GameCommands.__init__(self, self.db)
		GameMenu.__init__(self, self.db)
		self.musicMenu = MusicMenu(self.db, self.musicProcess)
		self.eventManager = EventManager(self.db)
		self._load()

	def _loadStage(self):
		import os
		loadSQLFile(db = self.db, fileName = os.path.join(sqlDir, 'stage{}.sql'.format(self.stage)))

	def _exit(self):
		self._save()
		self._mute()
		exit(0)

	def _load(self):
		gameDB = GameTable(self.db)
		gameDB.setCode(0)
		gameDB.readFromDB()
		self.stage = gameDB.stage.value
		self.__setupGame()

	def _save(self):
		self.db.commit()

	def _mute(self):
		try:
			self.musicMenu.musicProcess.terminate()
		except:
			pass

	def music(self):
		self.musicMenu.runMenu()

	def __setupGame(self):
		self.buckPasser = hero.Hero(self.db)

		self.currRoom = Room.Room(self.db)
		self.currRoom.setCode(0) # you always start in your apartment on loading
		self.setupNewRoom()

		self.buckPasser.inventory.roomInventory = self.currRoom.inventory
		self.getRoomNeighbors()

	def setStage(self, stage):
		self.stage = stage
		self._loadStage()

	def checkStage(self):
		'''
		Here the we look at the position and inventory of the character, if a stage change event has occurred, incriment the stage attribute
		'''
		stageCheck = self.eventManager.checkGameEvent(self.buckPasser.inventory, self.currRoom)
		if(stageCheck is not None):
			self.setStage(stageCheck)
		self._save()

	def run(self):
		try:
			self.currRoom.look()

			lastRoom = self.currRoom
			while True:
				try:
					if lastRoom != self.currRoom:
						lastRoom.writeRoom()#saves in nontemporary way (until save)
						lastRoom = self.currRoom
					self.checkStage()#check to see if a game event has occured, make something happen if it has
					userInput.userInput(self.commands, userInput.inputUniversal('  > '))
				except:
					print(traceback.format_exc())
					exit(1)
		finally:
			self.db.close()

def run():
	import os
	start = StartGame()
	dbFile = start.runMenu()

	if dbFile is not None:
		gameObj = Game(dbFile)
		gameObj.linepad = userInput.getTerminalSize()[0]
		gameObj.run()

if __name__ == "__main__":
	start = StartGame()
	fdb = 'game.db'
	os.system("rm %s"%fdb)
	dbFile = start._newGame(fdb)
<commit_msg>changed search to not take arguments<commit_after>#!/usr/bin/python3
import Room, hero, Character, objects, inventory, userInput
import readline, traceback, sqlite3
from gameEvents import EventManager
from musicPlayer import MusicMenu
from menus import Menu, MenuOption
from sqlTable import SQLTable

global sqlDir
sqlDir = "gameFiles"

def loadSQLFile(db, fileName):
	sqlLines = []
	with open(fileName, 'r') as f:
		for line in f:
			sqlLines.append(line)
	db.executescript('\n'.join(sqlLines))

class GameTable(SQLTable):
	def __init__(self, db):
		SQLTable.__init__(self, db)
		self.table = 'game'
		self.codeName = 'code'
		self.stage = self.elementTable.addElement(title = 'Game Stage', name = 'stage', value = None, elementType = 'INT')

class GameCommands(object):
	'''
	self.currRoom is the room the character is in
	self.inspection is the object, item, room, character that is being looked at. Each of these needs to have a dictionary of possible commands. The games superset of commands always apply.
	'''
	def __init__(self, db):
		self.db = db
		self.currRoom = None
		self.inspection = None
		self.buckPasser = None
		self.commands = {
			'start':userInput.Command(func=self.startMenu, takesArgs=False, descrip = 'Start Menu'),
			'help':userInput.Command(func=self.printHelp, takesArgs=False, hide = True, descrip = 'No one can save you now'),
			'move':userInput.Command(func=self.move, takesArgs=True, descrip = 'Move to a neighboring room'),
			'describe':userInput.Command(func=self.describe, takesArgs=True, descrip = 'Description of a person or thing'),
			'talk':userInput.Command(func=self.talkTo, takesArgs=True, descrip = 'Really need this fucking explained you dim wit?'),
			'items':userInput.Command(func=self.listItems, takesArgs=False, descrip = 'List what you have on you'),
			'search':userInput.Command(func=self.search, takesArgs=True, descrip = 'Root around for something'),
			'look':userInput.Command(func=self.look, takesArgs=False, descrip = 'Look around'),
			'mute':userInput.Command(func=self._mute, takesArgs=False, hide = True, descrip = 'Mute the sound'),
			'inspect':userInput.Command(func=self._inspect, takesArgs=True, hide = False, descrip = 'Inspect an object')
			}

		self.neighbors = None

	def _getObject(self, objName = None):
		if self.currRoom.objects == None:
			return False
		try:
			self.inspection = [obj for obj in self.currRoom.objects if obj.objName.value.lower() == objName.lower()][0]
			return True
		except IndexError:
			raise UserWarning('No Object Found')

	def _getCharacter(self, charName = None):
		if self.currRoom.characters == None:
			return False
		try:
			self.inspection = [character for character in self.currRoom.characters if character.charName.value.lower() == charName.lower()][0]
			return True
		except IndexError:
			raise UserWarning('No Character Found')

	def _getRoom(self, roomName = None):
		try:
			code = self.neighbors[roomName.title()]
		except KeyError:
			return False
		self.inspection = Room.Room(self.db)
		self.inspection.setCode(code)

	def _getItem(self, itemName = None):
		if self.buckPasser.inventory == None or self.buckPasser.inventory.items ==  None or len(self.buckPasser.inventory.items) < 1:
			#print('You ain\'t got shit')
			return False
		try:
			self.inspection = [item for item in self.buckPasser.inventory.items if item.item.subType.value.lower() == itemName.lower()][0]
			return True
		except IndexError:
			raise UserWarning('No Item Found')

	def __runCommand(self, command = None, args = None):
		try:
			if(args == None):
				func = self.inspection.__dict__['commands'][command].run()
			else:
				func = self.inspection.__dict__['commands'][command].run(args)
		except KeyError:
			pass

	def __makeCommand(self, subject = None, command = None, args = None, onObject = False, onCharacter = False, onItem = False):
		if type(subject) in [list, set, tuple]:
			subject = ' '.join(subject).lower().strip()
		try:
			#set the type of items its supposed to search for
			if onObject:
				try:
					if self._getObject(subject):
						self.__runCommand(command = command, args = args)
				except UserWarning:
					pass
			if onCharacter:
				try:
					if self._getCharacter(subject):
						self.__runCommand(command = command, args = args)
				except UserWarning:
					pass

		except UserWarning as uw:
			print(uw)
			userInput.printToScreen("I don't know what you're jabbering about you driveling idiot")

	def printCommands(self):
		userInput.printToScreen("Some of the Available Commands:")

		maxLen = max([len(command) for command in self.commands if not self.commands[command].hide])
		for command in self.commands:
			if not self.commands[command].hide:
				userInput.printToScreen('{0}{1} -> {2.descrip}'.format(' '*(maxLen - len(command)), command, self.commands[command]))

	def printHelp(self):
		userInput.printToScreen('\n\nNo one can help your sorry ass, just go score some blow.\n\n')
		self.printCommands()

	def describe(self, subject = None):
		if self.currRoom.objects is None and self.currRoom.characters is None:
			userInput.printToScreen("Theres nothing here")
			return

		if subject is None:
			options = []
			if self.currRoom.objects is not None:
				options.extend(obj.objName.value for obj in self.currRoom.objects)

			if self.currRoom.characters is not None:
				options.extend(char.charName.value for char in self.currRoom.characters)

			if len(options) == 1:
				subject = options
			else:
				subject = [userInput.printSelectGetOption(options = options, cursor = 'Describe What?> ')]
				if subject[0] is None:
					return
		self.__makeCommand(subject = subject, command = 'describe', onObject = True, onCharacter = True)

	def _inspect(self, subject = None):
		if self.currRoom.objects is None:
			userInput.printToScreen("Theres nothing here")
			return

		if subject is None:
			if len(self.currRoom.objects) == 1:
				subject = self.currRoom.objects[0].objName.value
			else:
				options = [obj.objName.value for obj in self.currRoom.objects]
				subject = [userInput.printSelectGetOption(options = options, cursor = 'Inspect What?> ')]
				if subject[0] is None:
					return
		self.__makeCommand(subject = subject, command = 'inspect', onObject = True)

	def talkTo(self, charName = None):
		if self.currRoom.characters == None:
			userInput.printToScreen("No one is here")
			return
		elif len(self.currRoom.characters) == 1:
			charName = self.currRoom.characters[0].charName.value

		if type(charName) in [tuple, list]:
			charName = ' '.join(charName).upper()
		elif charName == None:
			charNames = [char.charName.value for char in self.currRoom.characters]
			charName = userInput.printSelectGetOption(options = charNames, cursor = 'To Whom?> ', exitPrompt = 'No one')
			if charName is None:
				return
		char = [char for char in self.currRoom.characters if char.charName.value.upper() == charName.upper()]
		if len(char) > 0:
			char[0].talk()

	def getRoomNeighbors(self):
		room = Room.Room(self.db)
		self.neighbors = {}
		for code in userInput.parseCSVNumString(self.currRoom.neighbors.value):
			room.setCode(code)
			room.readFromDB(self.stage)
			self.neighbors.update({room.roomName.value : int(code)})

	def setupNewRoom(self):
		'''
		Sets up self.currRoom properly
		'''
		self.currRoom.inventory.charInventory = self.buckPasser.inventory
		self.currRoom.inventory.refreshList()
		self.currRoom.loadRoom(self.stage)

	def move(self, room = None):
		'''
		change current room to an available neighbor.
		if the room code is a valid room, change the currRoom object to the corresponding room and print the room description
		'''
		from operator import itemgetter
		if room == None:
			options = ['Stay Put']
			options.extend(room[0] for room in sorted(self.neighbors.items(), key=itemgetter(1)))#print with lowest room code first
			selection = userInput.printSelect(options = options, cursor = 'To Where?> ')
			if(selection == 0):
				return
			else:
				room = options[selection]
		else:
			try:
				room = ' '.join(room).title()
			except AttributeError:
				return

		if room in self.neighbors.keys():
			self.currRoom.writeRoom()
			self._getRoom(room)

			self.currRoom = self.inspection
			self.setupNewRoom()
			self.currRoom.look()

			self.getRoomNeighbors()
			self.buckPasser.inventory.roomInventory = self.currRoom.inventory
			self.currRoom.inventory.charInventory = self.buckPasser.inventory
			self.currRoom.inventory.refreshList()
		else:
			raise UserWarning("{} is not a valid neighbor".format(room))

	def listItems(self):
		self.buckPasser.listItems()

	def search(self):
		self.currRoom.commands['search'].run()

	def look(self):
		self.currRoom.look()

class GameMenu(Menu):
	def __init__(self, db):
		Menu.__init__(self, db, title =  'Start Menu', description="", cursor = " Start Menu> ")
		self.addOption(MenuOption(db = db, title = "Quit Game", description="Exit Game", commit = True, clear=True, action = self._exit))
		self.addOption(MenuOption(db = db, title = "Save", description="", commit = True, clear=True, action = self._save))
		self.addOption(MenuOption(db = db, title = "Load", description="Load a previous save", commit = False, clear=True, action=self._load))
		self.addOption(MenuOption(db = db, title = "Music", description="Play some sultry tunes", commit = False, clear=True, action=self.music))

	def startMenu(self):
		self.runMenu()
		self.currRoom.look()

class StartGame(Menu):
	def __init__(self):
		Menu.__init__(self, db = None, title =  'Detective Buck Passer', description="", cursor = "Game Menu> ")
		self.addOption(MenuOption(db = None, title = "New Game", description="It's your own funeral", commit = False, action = self._newGame))
		self.addOption(MenuOption(db = None, title = "Load", description="Load a previous save", commit = False, action=self._loadGame))

	def _newGame(self, dbFile = None):
		import os
		if dbFile is None:
			# Get a new game name
			dbFile = "game_{}.db".format(userInput.inputUniversal('Enter a save name> ').upper().replace(' ','_').strip())

		if os.path.isfile(dbFile):
			raise UserWarning("File name {} exists".format(dbFile))

		db = sqlite3.connect(dbFile)
		# make the new db ensuring its not writing over another file of the same name
		sqlFiles = ['sqlStructure.sql', 'items.sql', 'events.sql', 'stage0.sql']
		for sqlFile in sqlFiles:
			loadSQLFile(db = db, fileName = os.path.join(sqlDir, sqlFile))
		db.commit()
		return dbFile

	def _loadGame(self):
		import glob
		# Print a list of the available saves, have them choose one and load it in
		dbs = glob.glob('game_*.db')
		if len(dbs) < 1:
			print('No Saved Games')
			return

		dbFile = userInput.printSelectGetOption(options = dbs, cursor = "Choose a saved game> ")
		if dbFile is not None:
			db = sqlite3.connect(dbFile)
			db.commit()
			return dbFile

class Game(GameCommands, GameMenu):
	def __init__(self, dbFile):
		import os
		self.stage = None
		self.musicProcess = None
		os.stderr = open('log.log', 'w+')
		self.db = sqlite3.connect(dbFile)
		GameCommands.__init__(self, self.db)
		GameMenu.__init__(self, self.db)
		self.musicMenu = MusicMenu(self.db, self.musicProcess)
		self.eventManager = EventManager(self.db)
		self._load()

	def _loadStage(self):
		import os
		loadSQLFile(db = self.db, fileName = os.path.join(sqlDir, 'stage{}.sql'.format(self.stage)))

	def _exit(self):
		self._save()
		self._mute()
		exit(0)

	def _load(self):
		gameDB = GameTable(self.db)
		gameDB.setCode(0)
		gameDB.readFromDB()
		self.stage = gameDB.stage.value
		self.__setupGame()

	def _save(self):
		self.db.commit()

	def _mute(self):
		try:
			self.musicMenu.musicProcess.terminate()
		except:
			pass

	def music(self):
		self.musicMenu.runMenu()

	def __setupGame(self):
		self.buckPasser = hero.Hero(self.db)

		self.currRoom = Room.Room(self.db)
		self.currRoom.setCode(0) # you always start in your apartment on loading
		self.setupNewRoom()

		self.buckPasser.inventory.roomInventory = self.currRoom.inventory
		self.getRoomNeighbors()

	def setStage(self, stage):
		self.stage = stage
		self._loadStage()

	def checkStage(self):
		'''
		Here the we look at the position and inventory of the character, if a stage change event has occurred, incriment the stage attribute
		'''
		stageCheck = self.eventManager.checkGameEvent(self.buckPasser.inventory, self.currRoom)
		if(stageCheck is not None):
			self.setStage(stageCheck)
		self._save()

	def run(self):
		try:
			self.currRoom.look()

			lastRoom = self.currRoom
			while True:
				try:
					if lastRoom != self.currRoom:
						lastRoom.writeRoom()#saves in nontemporary way (until save)
						lastRoom = self.currRoom
					self.checkStage()#check to see if a game event has occured, make something happen if it has
					userInput.userInput(self.commands, userInput.inputUniversal('  > '))
				except:
					print(traceback.format_exc())
					exit(1)
		finally:
			self.db.close()

def run():
	import os
	start = StartGame()
	dbFile = start.runMenu()

	if dbFile is not None:
		gameObj = Game(dbFile)
		gameObj.linepad = userInput.getTerminalSize()[0]
		gameObj.run()

if __name__ == "__main__":
	start = StartGame()
	fdb = 'game.db'
	os.system("rm %s"%fdb)
	dbFile = start._newGame(fdb)
<|endoftext|>


<commit_before># jadsh

import os
import sys
import subprocess
import importlib
import re
import shlex
from jadsh.prompt import Prompt
from jadsh.constants import *

class Shell():

    def __init__(self, prompt = Prompt(), status = SHELL_STATUS_RUN):
        self.status = status
        self.prompt = prompt
        self.builtins = {}
        self.history = []

        self.loop()

    def loop(self):
        while self.status == SHELL_STATUS_RUN:
            self.title("jadsh " + os.getcwd())

            # Draw the prompt
            self.prompt.draw()

            # Grab user input
            try:
                user_input = sys.stdin.readline()
            except KeyboardInterrupt:
                sys.stdout.write("\n")
                continue
            except IOError:
                user_input = "exit"

            if not user_input:
                user_input = "exit"

            # Help the user
            if self.syntax_check(user_input) == False:
                continue

            # Allow commands to be split (so user can enter multiple commands at once)
            commands = re.split('[;]+', user_input)
            for cmd in commands:
                # Get tokens from user input
                tokens = self.tokenize(cmd)

                # Execute command
                try:
                    self.status = self.execute(tokens)
                except OSError as e:
                    print(self.hilite("jadsh error: ") + str(e))
                    return
                except KeyboardInterrupt:
                    continue

    def execute(self, tokens):
        if len(tokens) == 0: return SHELL_STATUS_RUN

        command = tokens[0]
        args = tokens[1:]

        # Check if builtin command
        if self.builtin(command):
            return self.builtins[command].execute(self, *args)

        # Fork to child process
        pid = os.fork()

        if pid == 0:
            os.execvp(command, tokens)
        elif pid > 0:
            while True:
                wpid, status = os.waitpid(pid, 0)

                if os.WIFEXITED(status) or os.WIFSIGNALED(status):
                    break

        self.history.append({ "command": command, "args": args })

        # Assume all went well, continue
        return SHELL_STATUS_RUN

    def builtin(self, command):
        if command in self.builtins: return True
        try:
            mod = importlib.import_module("jadsh.builtins." + command)
            obj = getattr(mod, command)()
            self.builtins[command] = obj
            return True
        except ImportError:
            return False

    def tokenize(self, command):
        return shlex.split(command)

    def hilite(self, string, status = False, bold = False):
        attr = []
        if status:
            # green
            attr.append('32')
        else:
            # red
            attr.append('31')
        if bold:
            attr.append('1')
        return '\x1b[%sm%s\x1b[0m' % (';'.join(attr), string)

    def syntax_check(self, user_input):
        if "&&" in user_input:
            print(self.hilite("Unsupported use of &&.") + " In jadsh, please use 'COMMAND; and COMMAND'")
            return False

    def title(self, title):
        sys.stdout.write("\x1b]2;" + title + "\x07")

if __name__ == "__main__":
    shell = Shell()<commit_msg>Remove launching shell directly<commit_after># jadsh

import os
import sys
import subprocess
import importlib
import re
import shlex
from jadsh.prompt import Prompt
from jadsh.constants import *

class Shell():

    def __init__(self, prompt = Prompt(), status = SHELL_STATUS_RUN):
        self.status = status
        self.prompt = prompt
        self.builtins = {}
        self.history = []

        self.loop()

    def loop(self):
        while self.status == SHELL_STATUS_RUN:
            self.title("jadsh " + os.getcwd())

            # Draw the prompt
            self.prompt.draw()

            # Grab user input
            try:
                user_input = sys.stdin.readline()
            except KeyboardInterrupt:
                sys.stdout.write("\n")
                continue
            except IOError:
                user_input = "exit"

            if not user_input:
                user_input = "exit"

            # Help the user
            if self.syntax_check(user_input) == False:
                continue

            # Allow commands to be split (so user can enter multiple commands at once)
            commands = re.split('[;]+', user_input)
            for cmd in commands:
                # Get tokens from user input
                tokens = self.tokenize(cmd)

                # Execute command
                try:
                    self.status = self.execute(tokens)
                except OSError as e:
                    print(self.hilite("jadsh error: ") + str(e))
                    return
                except KeyboardInterrupt:
                    continue

    def execute(self, tokens):
        if len(tokens) == 0: return SHELL_STATUS_RUN

        command = tokens[0]
        args = tokens[1:]

        # Check if builtin command
        if self.builtin(command):
            return self.builtins[command].execute(self, *args)

        # Fork to child process
        pid = os.fork()

        if pid == 0:
            os.execvp(command, tokens)
        elif pid > 0:
            while True:
                wpid, status = os.waitpid(pid, 0)

                if os.WIFEXITED(status) or os.WIFSIGNALED(status):
                    break

        self.history.append({ "command": command, "args": args })

        # Assume all went well, continue
        return SHELL_STATUS_RUN

    def builtin(self, command):
        if command in self.builtins: return True
        try:
            mod = importlib.import_module("jadsh.builtins." + command)
            obj = getattr(mod, command)()
            self.builtins[command] = obj
            return True
        except ImportError:
            return False

    def tokenize(self, command):
        return shlex.split(command)

    def hilite(self, string, status = False, bold = False):
        attr = []
        if status:
            # green
            attr.append('32')
        else:
            # red
            attr.append('31')
        if bold:
            attr.append('1')
        return '\x1b[%sm%s\x1b[0m' % (';'.join(attr), string)

    def syntax_check(self, user_input):
        if "&&" in user_input:
            print(self.hilite("Unsupported use of &&.") + " In jadsh, please use 'COMMAND; and COMMAND'")
            return False

    def title(self, title):
        sys.stdout.write("\x1b]2;" + title + "\x07")<|endoftext|>


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

"""
Implementation of the STIM map from [PAI18]
.. [PAI18]
   | Paret et al, 1018
   | **STIM map: detection map for exoplanets imaging beyond asymptotic Gaussian residual speckle noise**
   | *submitted in Monthly Notices of the Royal Astronomical Society*
"""
__author__ = 'Benoit Pairet'
__all__ = ['compute_stim_map',
           'compute_inverse_stim_map']

import numpy as np
from ..preproc import cube_derotate
from ..var import get_circle

def compute_stim_map(cube_der):
    """
    Computes the STIM detection map.

    Parameters
    ----------
    cube_der : 3d numpy ndarray
        Input de-rotated cube, e.g. output 'residuals_cube_' from
        ``vip_hci.pca.pca``.

    Returns
    -------
    detection_map : 2d ndarray
        STIM detection map.
    """
    t, n, _ = cube_der.shape
    mu = np.mean(cube_der, axis=0)
    sigma = np.sqrt(np.var(cube_der, axis=0))
    detection_map = np.divide(mu, sigma, out=np.zeros_like(mu),
                              where=sigma != 0)
    return get_circle(detection_map, int(np.round(n/2.)))

def compute_inverse_stim_map(cube, angle_list):
    """
    Computes the STIM detection map.

    Parameters
    ----------
    cube : 3d numpy ndarray
        Non de-rotated residuals from reduction algorithm, eg. output residuals
        from ``vip_hci.pca.pca``.
    angle_list : numpy ndarray, 1d
        Corresponding parallactic angle for each frame.

    Returns
    -------
    inverse_stim_map : 2d ndarray
        Inverse STIM detection map.
    """
    t, n, _ = cube.shape
    cube_inv_der = cube_derotate(cube, -angle_list)
    inverse_stim_map = compute_stim_map(cube_inv_der)
    return inverse_stim_map<commit_msg>Added options for imlib and interpolation for derotation involved in inverse_STIM_map<commit_after>#! /usr/bin/env python

"""
Implementation of the STIM map from [PAI18]
.. [PAI18]
   | Paret et al, 1018
   | **STIM map: detection map for exoplanets imaging beyond asymptotic Gaussian residual speckle noise**
   | *submitted in Monthly Notices of the Royal Astronomical Society*
"""
__author__ = 'Benoit Pairet'
__all__ = ['compute_stim_map',
           'compute_inverse_stim_map']

import numpy as np
from ..preproc import cube_derotate
from ..var import get_circle

def compute_stim_map(cube_der):
    """
    Computes the STIM detection map.

    Parameters
    ----------
    cube_der : 3d numpy ndarray
        Input de-rotated cube, e.g. output 'residuals_cube_' from
        ``vip_hci.pca.pca``.

    Returns
    -------
    detection_map : 2d ndarray
        STIM detection map.
    """
    t, n, _ = cube_der.shape
    mu = np.mean(cube_der, axis=0)
    sigma = np.sqrt(np.var(cube_der, axis=0))
    detection_map = np.divide(mu, sigma, out=np.zeros_like(mu),
                              where=sigma != 0)
    return get_circle(detection_map, int(np.round(n/2.)))

def compute_inverse_stim_map(cube, angle_list, imlib='opencv',
                             interpolation='lanczos4'):
    """
    Computes the STIM detection map.

    Parameters
    ----------
    cube : 3d numpy ndarray
        Non de-rotated residuals from reduction algorithm, eg. output residuals
        from ``vip_hci.pca.pca``.
    angle_list : numpy ndarray, 1d
        Corresponding parallactic angle for each frame.
    imlib: str, opt
        See description of vip_hci.preproc.frame_derotate()
    interpolation: str, opt
        See description of vip_hci.preproc.frame_derotate()

    Returns
    -------
    inverse_stim_map : 2d ndarray
        Inverse STIM detection map.
    """
    t, n, _ = cube.shape
    cube_inv_der = cube_derotate(cube, -angle_list, imlib=imlib,
                                 interpolation=interpolation)
    inverse_stim_map = compute_stim_map(cube_inv_der)
    return inverse_stim_map<|endoftext|>


<commit_before>import os
import sys
import json
import logging
from cerberus import Validator

from virtool.utils import to_bool

logger = logging.getLogger(__name__)

def get_default_boolean(default):
    return {"type": "boolean", "coerce": to_bool, "default": default}

def get_default_integer(default):
    return {"type": "integer", "coerce": int, "default": default}

DEFAULT_ANALYSIS_PROC = get_default_integer(6)
DEFAULT_ANALYSIS_MEM = get_default_integer(16)
DEFAULT_ANALYSIS_INST = get_default_integer(6)

SCHEMA = {
    # File paths
    "data_path": {"type": "string", "default": "data"},
    "watch_path": {"type": "string", "default": "watch"},

    # Host resource limits
    "proc": get_default_integer(8),
    "mem": get_default_integer(16),

    # Task-specific settings
    "dummy_proc": get_default_integer(1),
    "dummy_mem": get_default_integer(1),
    "dummy_inst": get_default_integer(5),
    "pathoscope_bowtie_proc": DEFAULT_ANALYSIS_PROC,
    "pathoscope_bowtie_mem": DEFAULT_ANALYSIS_MEM,
    "pathoscope_bowtie_inst": DEFAULT_ANALYSIS_INST,
    "nuvs_proc": DEFAULT_ANALYSIS_PROC,
    "nuvs_mem": DEFAULT_ANALYSIS_MEM,
    "nuvs_inst": DEFAULT_ANALYSIS_INST,
    "create_sample_proc": get_default_integer(4),
    "create_sample_mem": get_default_integer(4),
    "create_sample_inst": get_default_integer(3),
    "create_subtraction_proc": get_default_integer(2),
    "create_subtraction_mem": get_default_integer(4),
    "create_subtraction_inst": get_default_integer(2),
    "rebuild_index_proc": get_default_integer(2),
    "rebuild_index_mem": get_default_integer(4),
    "rebuild_index_inst": get_default_integer(1),

    # Samples
    "sample_group": {"type": "string", "default": "none"},
    "sample_group_read": get_default_boolean(True),
    "sample_group_write": get_default_boolean(False),
    "sample_all_read": get_default_boolean(True),
    "sample_all_write": get_default_boolean(False),
    "sample_unique_names": get_default_boolean(True),

    # MongoDB
    "db_name": {"type": "string", "default": "virtool"},
    "db_host": {"type": "string", "default": "localhost"},
    "db_port": get_default_integer(27017),

    # HTTP Server
    "server_host": {"type": "string", "default": "localhost"},
    "server_port": get_default_integer(9950),

    # Github
    "github_token": {"type": "string", "default": None, "nullable": True},
    "github_username": {"type": "string", "default": None, "nullable": True},
    "software_repo": {"type": "string", "default": "virtool/virtool"},
    "database_repo": {"type": "string", "default": "virtool/virtool-database"},

    # SSL
    "use_ssl": get_default_boolean(False),
    "cert_path": {"type": "string", "default": ""},
    "key_path": {"type": "string", "default": ""},

    # Virus settings
    "restrict_source_types": get_default_boolean(True),
    "allowed_source_types": {"type": "list", "default": ["isolate", "genotype"]},

    # Analysis settings
    "use_internal_control": get_default_boolean(False),
    "internal_control_id": {"type": "string", "default": ""}
}

class Settings:

    def __init__(self, loop):
        self.loop = loop
        self.data = None
        self.path = "./settings.json"

    def get(self, key):
        return self.data[key]

    def set(self, key, value):
        self.data[key] = value
        return self.data

    async def load(self):
        await self.loop.run_in_executor(None, self.load_from_file)

    def load_from_file(self):
        """ Load a JSON settings file. If the path cannot be found, generate a new settings file. """
        content = {}

        try:
            # Open file and parse JSON into dictionary.
            with open(self.path, "r") as settings_file:
                string = settings_file.read()
                content = json.loads(string)
        except IOError:
            pass

        v = Validator(SCHEMA, purge_unknown=True)

        if not v.validate(content):
            raise ValueError("Could not validate settings file", v.errors)

        self.data = v.document
        write_to_file(self.data, self.path)

    async def write(self):
        await self.loop.run_in_executor(None, write_to_file, self.data, self.path)

    def as_dict(self):
        return dict(self.data)

def write_to_file(settings_dict, path=None):
    path = path or os.path.join(sys.path[0], "settings.json")

    with open(path, "w") as settings_file:
        json.dump(settings_dict, settings_file, indent=4, sort_keys=True)
<commit_msg>Add some more default source types to app_settings.py<commit_after>import os
import sys
import json
import logging
from cerberus import Validator

from virtool.utils import to_bool

logger = logging.getLogger(__name__)

def get_default_boolean(default):
    return {"type": "boolean", "coerce": to_bool, "default": default}

def get_default_integer(default):
    return {"type": "integer", "coerce": int, "default": default}

DEFAULT_ANALYSIS_PROC = get_default_integer(6)
DEFAULT_ANALYSIS_MEM = get_default_integer(16)
DEFAULT_ANALYSIS_INST = get_default_integer(6)

SCHEMA = {
    # File paths
    "data_path": {"type": "string", "default": "data"},
    "watch_path": {"type": "string", "default": "watch"},

    # Host resource limits
    "proc": get_default_integer(8),
    "mem": get_default_integer(16),

    # Task-specific settings
    "dummy_proc": get_default_integer(1),
    "dummy_mem": get_default_integer(1),
    "dummy_inst": get_default_integer(5),
    "pathoscope_bowtie_proc": DEFAULT_ANALYSIS_PROC,
    "pathoscope_bowtie_mem": DEFAULT_ANALYSIS_MEM,
    "pathoscope_bowtie_inst": DEFAULT_ANALYSIS_INST,
    "nuvs_proc": DEFAULT_ANALYSIS_PROC,
    "nuvs_mem": DEFAULT_ANALYSIS_MEM,
    "nuvs_inst": DEFAULT_ANALYSIS_INST,
    "create_sample_proc": get_default_integer(4),
    "create_sample_mem": get_default_integer(4),
    "create_sample_inst": get_default_integer(3),
    "create_subtraction_proc": get_default_integer(2),
    "create_subtraction_mem": get_default_integer(4),
    "create_subtraction_inst": get_default_integer(2),
    "rebuild_index_proc": get_default_integer(2),
    "rebuild_index_mem": get_default_integer(4),
    "rebuild_index_inst": get_default_integer(1),

    # Samples
    "sample_group": {"type": "string", "default": "none"},
    "sample_group_read": get_default_boolean(True),
    "sample_group_write": get_default_boolean(False),
    "sample_all_read": get_default_boolean(True),
    "sample_all_write": get_default_boolean(False),
    "sample_unique_names": get_default_boolean(True),

    # MongoDB
    "db_name": {"type": "string", "default": "virtool"},
    "db_host": {"type": "string", "default": "localhost"},
    "db_port": get_default_integer(27017),

    # HTTP Server
    "server_host": {"type": "string", "default": "localhost"},
    "server_port": get_default_integer(9950),

    # Github
    "github_token": {"type": "string", "default": None, "nullable": True},
    "github_username": {"type": "string", "default": None, "nullable": True},
    "software_repo": {"type": "string", "default": "virtool/virtool"},
    "database_repo": {"type": "string", "default": "virtool/virtool-database"},

    # SSL
    "use_ssl": get_default_boolean(False),
    "cert_path": {"type": "string", "default": ""},
    "key_path": {"type": "string", "default": ""},

    # Virus settings
    "restrict_source_types": get_default_boolean(True),
    "allowed_source_types": {"type": "list", "default": ["clone", "culture", "genotype", "isolate", "strain"]},

    # Analysis settings
    "use_internal_control": get_default_boolean(False),
    "internal_control_id": {"type": "string", "default": ""}
}

class Settings:

    def __init__(self, loop):
        self.loop = loop
        self.data = None
        self.path = "./settings.json"

    def get(self, key):
        return self.data[key]

    def set(self, key, value):
        self.data[key] = value
        return self.data

    async def load(self):
        await self.loop.run_in_executor(None, self.load_from_file)

    def load_from_file(self):
        """ Load a JSON settings file. If the path cannot be found, generate a new settings file. """
        content = {}

        try:
            # Open file and parse JSON into dictionary.
            with open(self.path, "r") as settings_file:
                string = settings_file.read()
                content = json.loads(string)
        except IOError:
            pass

        v = Validator(SCHEMA, purge_unknown=True)

        if not v.validate(content):
            raise ValueError("Could not validate settings file", v.errors)

        self.data = v.document
        write_to_file(self.data, self.path)

    async def write(self):
        await self.loop.run_in_executor(None, write_to_file, self.data, self.path)

    def as_dict(self):
        return dict(self.data)

def write_to_file(settings_dict, path=None):
    path = path or os.path.join(sys.path[0], "settings.json")

    with open(path, "w") as settings_file:
        json.dump(settings_dict, settings_file, indent=4, sort_keys=True)
<|endoftext|>