<commit_before># This file is part of Moksha.
#
# Moksha 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.
#
# Moksha 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 Moksha.  If not, see <http://www.gnu.org/licenses/>.
#
# Copyright 2008, Red Hat, Inc.
# Authors: Luke Macken <lmacken@redhat.com>

import moksha

from tg import config
from tw.api import Widget, JSLink, js_callback

from moksha.api.widgets.orbited import orbited_host, orbited_port, orbited_url

stomp_js = JSLink(link=orbited_url + '/static/protocols/stomp/stomp.js')

def stomp_subscribe(topic):
    """ Return a javascript callback that subscribes to a given topic,
        or a list of topics.
    """
    sub = 'stomp.subscribe("%s");'
    if isinstance(topic, list):
        sub = ''.join([sub % t for t in topic])
    else:
        sub = sub % topic
    return sub

class StompWidget(Widget):
    callbacks = ['onopen', 'onerror', 'onerrorframe', 'onclose',
                 'onconnectedframe', 'onmessageframe']
    params = callbacks[:] + ['topics']
    onopen = js_callback('function(){}')
    onerror = js_callback('function(error){}')
    onclose = js_callback('function(c){}')
    onerrorframe = js_callback('function(f){}')
    onmessageframe = ''
    onconnectedframe = ''
    engine_name = 'mako'
    template = """
      <script>

        /* Create a new TCPSocket & Stomp client */
        if (typeof stomp == 'undefined') {
            console.log('Creating new stomp client');

            var moksha_callbacks = new Object();

            Orbited.settings.port = %s;
            Orbited.settings.hostname = '%s';
            Orbited.settings.streaming = true;
            document.domain = document.domain;
            TCPSocket = Orbited.TCPSocket;
            stomp = new STOMPClient();
            stomp.onopen = ${onopen};
            stomp.onclose = ${onclose};
            stomp.onerror = ${onerror};
            stomp.onerrorframe = ${onerrorframe};
            stomp.onconnectedframe = function(){ ${onconnectedframe} };
            stomp.onmessageframe = function(f){
                var json = JSON.parse(f.body);
                var dest = f.headers.destination;
                if (moksha_callbacks[dest]) {
                    console.log('Running callbacks for ' + dest);
                    for (var i = 0; i < moksha_callbacks[dest].length; i++) {
                        moksha_callbacks[dest][i](json);
                    }
                }
            };

            stomp.connect('%s', %s, '%s', '%s');

        /* Utilize the existing stomp connection */
        } else {
            ${onconnectedframe}
        }

        %% for topic in topics:
            var topic = "${topic}";
            if (!moksha_callbacks[topic]) {
                moksha_callbacks[topic] = [];
            }
            moksha_callbacks[topic].push(function(json){ ${onmessageframe}; });
        %% endfor

      </script>
    """ % (orbited_port, orbited_host,
           config.get('stomp_host', 'localhost'),
           config.get('stomp_port', 61613),
           config.get('stomp_user', 'guest'),
           config.get('stomp_pass', 'guest'))

    def update_params(self, d):
        super(StompWidget, self).update_params(d)
        d.topics = []
        for callback in self.callbacks:
            if len(moksha.stomp[callback]):
                cbs = ''
                if callback == 'onmessageframe':
                    for topic in moksha.stomp[callback]:
                        d.topics.append(topic)
                        for cb in moksha.stomp[callback][topic]:
                            cbs += "%s;" % str(cb)
                else:
                    for cb in moksha.stomp[callback]:
                        if isinstance(cb, js_callback):
                            cbs += '$(%s);' % str(cb)
                        else:
                            cbs += str(cb)
                if cbs:
                    d[callback] = cbs

stomp_widget = StompWidget('stomp')
<commit_msg>remove some console javascript logging messages<commit_after># This file is part of Moksha.
#
# Moksha 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.
#
# Moksha 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 Moksha.  If not, see <http://www.gnu.org/licenses/>.
#
# Copyright 2008, Red Hat, Inc.
# Authors: Luke Macken <lmacken@redhat.com>

import moksha

from tg import config
from tw.api import Widget, JSLink, js_callback

from moksha.api.widgets.orbited import orbited_host, orbited_port, orbited_url

stomp_js = JSLink(link=orbited_url + '/static/protocols/stomp/stomp.js')

def stomp_subscribe(topic):
    """ Return a javascript callback that subscribes to a given topic,
        or a list of topics.
    """
    sub = 'stomp.subscribe("%s");'
    if isinstance(topic, list):
        sub = ''.join([sub % t for t in topic])
    else:
        sub = sub % topic
    return sub

class StompWidget(Widget):
    callbacks = ['onopen', 'onerror', 'onerrorframe', 'onclose',
                 'onconnectedframe', 'onmessageframe']
    params = callbacks[:] + ['topics']
    onopen = js_callback('function(){}')
    onerror = js_callback('function(error){}')
    onclose = js_callback('function(c){}')
    onerrorframe = js_callback('function(f){}')
    onmessageframe = ''
    onconnectedframe = ''
    engine_name = 'mako'
    template = """
      <script>

        /* Create a new TCPSocket & Stomp client */
        if (typeof stomp == 'undefined') {
            var moksha_callbacks = new Object();

            Orbited.settings.port = %s;
            Orbited.settings.hostname = '%s';
            Orbited.settings.streaming = true;
            document.domain = document.domain;
            TCPSocket = Orbited.TCPSocket;
            stomp = new STOMPClient();
            stomp.onopen = ${onopen};
            stomp.onclose = ${onclose};
            stomp.onerror = ${onerror};
            stomp.onerrorframe = ${onerrorframe};
            stomp.onconnectedframe = function(){ ${onconnectedframe} };
            stomp.onmessageframe = function(f){
                var json = JSON.parse(f.body);
                var dest = f.headers.destination;
                if (moksha_callbacks[dest]) {
                    for (var i = 0; i < moksha_callbacks[dest].length; i++) {
                        moksha_callbacks[dest][i](json);
                    }
                }
            };

            stomp.connect('%s', %s, '%s', '%s');

        /* Utilize the existing stomp connection */
        } else {
            ${onconnectedframe}
        }

        %% for topic in topics:
            var topic = "${topic}";
            if (!moksha_callbacks[topic]) {
                moksha_callbacks[topic] = [];
            }
            moksha_callbacks[topic].push(function(json){ ${onmessageframe}; });
        %% endfor

      </script>
    """ % (orbited_port, orbited_host,
           config.get('stomp_host', 'localhost'),
           config.get('stomp_port', 61613),
           config.get('stomp_user', 'guest'),
           config.get('stomp_pass', 'guest'))

    def update_params(self, d):
        super(StompWidget, self).update_params(d)
        d.topics = []
        for callback in self.callbacks:
            if len(moksha.stomp[callback]):
                cbs = ''
                if callback == 'onmessageframe':
                    for topic in moksha.stomp[callback]:
                        d.topics.append(topic)
                        for cb in moksha.stomp[callback][topic]:
                            cbs += "%s;" % str(cb)
                else:
                    for cb in moksha.stomp[callback]:
                        if isinstance(cb, js_callback):
                            cbs += '$(%s);' % str(cb)
                        else:
                            cbs += str(cb)
                if cbs:
                    d[callback] = cbs

stomp_widget = StompWidget('stomp')
<|endoftext|>


<commit_before>################################################################################
# Copyright Keith T Butler    (2013)                                           #
#                                                                              #
# This file is part of SMACT: distorter.py 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.                          #
# This program is distributed in the hope that it will be useful, but WITHOUT  #
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or        #
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for    #
# more details.                                                                #
# You should have received a copy of the GNU General Public License along with #
# this program.  If not, see <http://www.gnu.org/licenses/>.                   #
#                                                                              #
################################################################################

"""
smact.distorter: Module for generating symmetry-unique substitutions on a given sub-lattice.

As input it takes the ASE crystal object (as built by smact.builder)
and the sub-lattice on which substitutions are to be made.
There is an example of how to use the code in Example_distort.py

---------------------------------------------------------------------
TODO:
Add a functionality to check two Atoms objects against one another
for equivalence.
---------------------------------------------------------------------

"""

import ase
import copy
import smact.builder as builder
import smact.core as core
from pyspglib import spglib
from ase.lattice.spacegroup import Spacegroup
import numpy as np

#------------------------------------------------------------------------------------------
def get_sg(lattice):

    """
    Get the space-group of the system

    Args:
	lattice: the ASE crystal class
	Returns:
	sg: integer number of the spacegroup

    """
    spacegroup = spglib.get_spacegroup(lattice, symprec=1e-5)
    space_split=spacegroup.split()
    spg_num = space_split[1].replace('(','').replace(')','')
    sg = Spacegroup(int(spg_num))
    return sg
#------------------------------------------------------------------------------------------
def get_inequivalent_sites(sub_lattice, lattice):
    """Given a sub lattice, returns symmetry unique sites for substitutions

	Args:
	sub_lattice: array containing Cartesian coordinates of the sub-lattice of interest
	lattice: ASE crystal class, the total lattice
     """
    sg = get_sg(lattice)
    inequivalent_sites = []
    i = 0
    for site in sub_lattice:
        new_site = True
# Check against the existing members of the list of inequivalent sites
        for inequiv_site in inequivalent_sites:
	    if core.are_eq(site, inequiv_site) == True:
	        new_site = False
# Check against symmetry related members of the list of inequivalent sites
            equiv_inequiv_sites,junk = sg.equivalent_sites(inequiv_site)
	    for equiv_inequiv_site in equiv_inequiv_sites:
	        if core.are_eq(site, equiv_inequiv_site) == True:
	   	    new_site = False
        if new_site == True:
	    inequivalent_sites.append(site)
        i = i + 1
    return inequivalent_sites
#------------------------------------------------------------------------------------------
def make_substitution(lattice,site,new_species):
    """Change the atomic species @ site in lattice to new_species [atomic number]

    Args:
	lattice: ASE crystal class
	site: array, containing the Cartesian coordinates of the substitution site
	new_species: string, the new species

     """
    i = 0
# NBNBNBNB  It is necessary to use deepcopy for objects, otherwise changes applied to a clone
# will also apply to the parent object.
    new_lattice = copy.deepcopy(lattice)
    lattice_sites = new_lattice.get_scaled_positions()
    for lattice_site in lattice_sites:
	if core.are_eq(lattice_site, site):
	    new_lattice[i].symbol = new_species
	i = i + 1
    return new_lattice
#------------------------------------------------------------------------------------------
def build_sub_lattice(lattice,symbol):
    """Generate a sub-lattice of the lattice based on equivalent atomic species

    Args:
        lattice: ASE crystal class
	symbol: The name of the species whose sub-lattice you wish to build

    Returns:
	sub_lattice: Cartesian coordinates of the sub-lattice of symbol

    """

    sub_lattice = []
    i = 0
    atomic_labels = lattice.get_chemical_symbols()
    positions = lattice.get_scaled_positions()
    for atom in atomic_labels:
        if atom == symbol:
            sub_lattice.append(positions[i])
        i = i + 1
    return sub_lattice
#------------------------------------------------------------------------------------------

<commit_msg>Compatibility with new spglib (name changed from pyspglib)<commit_after>################################################################################
# Copyright Keith T Butler    (2013)                                           #
#                                                                              #
# This file is part of SMACT: distorter.py 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.                          #
# This program is distributed in the hope that it will be useful, but WITHOUT  #
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or        #
# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for    #
# more details.                                                                #
# You should have received a copy of the GNU General Public License along with #
# this program.  If not, see <http://www.gnu.org/licenses/>.                   #
#                                                                              #
################################################################################

"""
smact.distorter: Module for generating symmetry-unique substitutions on a given sub-lattice.

As input it takes the ASE crystal object (as built by smact.builder)
and the sub-lattice on which substitutions are to be made.
There is an example of how to use the code in Example_distort.py

---------------------------------------------------------------------
TODO:
Add a functionality to check two Atoms objects against one another
for equivalence.
---------------------------------------------------------------------

"""

import ase
import copy
import smact.builder as builder
import smact.core as core
try:
    from pyspglib import spglib
except ImportError:
    try:
        from spglib import spglib
    except ImportError:
        raise Exception("Could not load spglib")

from ase.lattice.spacegroup import Spacegroup
import numpy as np

#------------------------------------------------------------------------------------------
def get_sg(lattice):

    """
    Get the space-group of the system

    Args:
	lattice: the ASE crystal class
	Returns:
	sg: integer number of the spacegroup

    """
    spacegroup = spglib.get_spacegroup(lattice, symprec=1e-5)
    space_split=spacegroup.split()
    spg_num = space_split[1].replace('(','').replace(')','')
    sg = Spacegroup(int(spg_num))
    return sg
#------------------------------------------------------------------------------------------
def get_inequivalent_sites(sub_lattice, lattice):
    """Given a sub lattice, returns symmetry unique sites for substitutions

	Args:
	sub_lattice: array containing Cartesian coordinates of the sub-lattice of interest
	lattice: ASE crystal class, the total lattice
     """
    sg = get_sg(lattice)
    inequivalent_sites = []
    i = 0
    for site in sub_lattice:
        new_site = True
# Check against the existing members of the list of inequivalent sites
        for inequiv_site in inequivalent_sites:
	    if core.are_eq(site, inequiv_site) == True:
	        new_site = False
# Check against symmetry related members of the list of inequivalent sites
            equiv_inequiv_sites,junk = sg.equivalent_sites(inequiv_site)
	    for equiv_inequiv_site in equiv_inequiv_sites:
	        if core.are_eq(site, equiv_inequiv_site) == True:
	   	    new_site = False
        if new_site == True:
	    inequivalent_sites.append(site)
        i = i + 1
    return inequivalent_sites
#------------------------------------------------------------------------------------------
def make_substitution(lattice,site,new_species):
    """Change the atomic species @ site in lattice to new_species [atomic number]

    Args:
	lattice: ASE crystal class
	site: array, containing the Cartesian coordinates of the substitution site
	new_species: string, the new species

     """
    i = 0
# NBNBNBNB  It is necessary to use deepcopy for objects, otherwise changes applied to a clone
# will also apply to the parent object.
    new_lattice = copy.deepcopy(lattice)
    lattice_sites = new_lattice.get_scaled_positions()
    for lattice_site in lattice_sites:
	if core.are_eq(lattice_site, site):
	    new_lattice[i].symbol = new_species
	i = i + 1
    return new_lattice
#------------------------------------------------------------------------------------------
def build_sub_lattice(lattice,symbol):
    """Generate a sub-lattice of the lattice based on equivalent atomic species

    Args:
        lattice: ASE crystal class
	symbol: The name of the species whose sub-lattice you wish to build

    Returns:
	sub_lattice: Cartesian coordinates of the sub-lattice of symbol

    """

    sub_lattice = []
    i = 0
    atomic_labels = lattice.get_chemical_symbols()
    positions = lattice.get_scaled_positions()
    for atom in atomic_labels:
        if atom == symbol:
            sub_lattice.append(positions[i])
        i = i + 1
    return sub_lattice
#------------------------------------------------------------------------------------------

<|endoftext|>


<commit_before>"""
Unified expression helpers.
"""

def evaluate(pycode, locals):
    return eval(pycode, {}, locals)

class ExpressionLocals(dict):
    """
    An object to act as the locals dictionary for the evaluation
    of envi expressions.  You may pass in an envi.symstore.resolver.SymbolResolver
    object to automagically use symbols in your expressions.
    """
    def __init__(self, symobj=None):
        dict.__init__(self)
        self.symobj = symobj

    def __getitem__(self, name):
        if self.symobj != None:
            ret = self.symobj.getSymByName(name)
            if ret != None: return ret
        return dict.__getitem__(self, name)

class MemoryExpressionLocals(ExpressionLocals):

    def __init__(self, memobj, symobj=None):
        ExpressionLocals.__init__(self, symobj=symobj)
        self.memobj = memobj
        self.update({
            'mapbase':self.mapbase,
            'maplen':self.maplen,
            'ispoi':self.ispoi,
            'mem':self.mem,
            'poi':self.poi,
            'sym':self.sym,
        })

    def sym(self, symstr):
        '''
        An easy to use utility for symbols which have un-pythonic names.

        Example x = sym('kernel32.??2@$$FYAPAXI@Z')
        '''
        return long(evaluate(symstr, self))

    def mapbase(self, address):
        """
        The expression mapbase(address) returns the base address of the
        memory mapped area containing "address"
        """
        map = self.memobj.getMemoryMap(address)
        if not map:
            raise Exception("ERROR - un-mapped address in mapbase()")
        return map[0]

    def maplen(self, address):
        """
        The expression maplen(address) returns the length of the
        memory mapped area containing "address".
        """
        map = self.memobj.getMemoryMap(address)
        if not map:
            raise Exception("ERROR - un-mapped address in maplen()")
        return map[1]

    def ispoi(self, addr):
        """
        The expression ispoi(value) returns True if the specified value
        is a valid pointer.  Otherwise, False.
        """
        return self.memobj.isValidPointer(addr)

    def mem(self, addr, size):
        """
        Read and return memory.

        Example: mem(ecx, 20)
        """
        return self.memobj.readMemory(addr, size)

    def poi(self, address):
        """
        When expression contains "poi(addr)" this will return
        the address pointed to by addr.
        """
        return self.memobj.readMemoryPtr(address)

<commit_msg>initial solution:<commit_after>"""
Unified expression helpers.
"""
class ExpressionFail(Exception):
    def __init__(self, pycode):
        Exception.__init__(self)
        self.pycode = pycode

    def __repr__(self):
        return "ExpressionFail: %r is not a valid expression in this context" % self.pycode

    def __str__(self):
        return self.__repr__()

def evaluate(pycode, locals):
    try:
        val = eval(pycode, {}, locals)
    except NameError, e:
        try:
            # check through the keys for anything we might want to replace
            keys = locals.keys()

            # sort the keys in reverse order so that longer matching strings take priority
            keys.sort(reverse=True)

            # replace the substrings with the string versions of the lookup value
            for key in keys:
                if key in pycode:
                    pval = locals.get(key)
                    pycode = pycode.replace(key, str(pval))

            val = eval(pycode, {}, locals)

        except NameError, e:
            raise ExpressionFail(pycode)

    return val

class ExpressionLocals(dict):
    """
    An object to act as the locals dictionary for the evaluation
    of envi expressions.  You may pass in an envi.symstore.resolver.SymbolResolver
    object to automagically use symbols in your expressions.
    """
    def __init__(self, symobj=None):
        dict.__init__(self)
        self.symobj = symobj

    def __getitem__(self, name):
        if self.symobj != None:
            ret = self.symobj.getSymByName(name)
            if ret != None: return ret
        return dict.__getitem__(self, name)

class MemoryExpressionLocals(ExpressionLocals):

    def __init__(self, memobj, symobj=None):
        ExpressionLocals.__init__(self, symobj=symobj)
        self.memobj = memobj
        self.update({
            'mapbase':self.mapbase,
            'maplen':self.maplen,
            'ispoi':self.ispoi,
            'mem':self.mem,
            'poi':self.poi,
            'sym':self.sym,
        })

    def sym(self, symstr):
        '''
        An easy to use utility for symbols which have un-pythonic names.

        Example x = sym('kernel32.??2@$$FYAPAXI@Z')
        '''
        return long(evaluate(symstr, self))

    def mapbase(self, address):
        """
        The expression mapbase(address) returns the base address of the
        memory mapped area containing "address"
        """
        map = self.memobj.getMemoryMap(address)
        if not map:
            raise Exception("ERROR - un-mapped address in mapbase()")
        return map[0]

    def maplen(self, address):
        """
        The expression maplen(address) returns the length of the
        memory mapped area containing "address".
        """
        map = self.memobj.getMemoryMap(address)
        if not map:
            raise Exception("ERROR - un-mapped address in maplen()")
        return map[1]

    def ispoi(self, addr):
        """
        The expression ispoi(value) returns True if the specified value
        is a valid pointer.  Otherwise, False.
        """
        return self.memobj.isValidPointer(addr)

    def mem(self, addr, size):
        """
        Read and return memory.

        Example: mem(ecx, 20)
        """
        return self.memobj.readMemory(addr, size)

    def poi(self, address):
        """
        When expression contains "poi(addr)" this will return
        the address pointed to by addr.
        """
        return self.memobj.readMemoryPtr(address)

<|endoftext|>


<commit_before>import sys
from config import dataFolder
from expyriment.misc import data_preprocessing
import numpy as np

agg = data_preprocessing.Aggregator(data_folder=dataFolder,
                                    file_name=sys.argv[1])

print 'Variable computed: '
data = {}
for variable in agg.variables:
    data[variable] = agg.get_variable_data(variable)

indexBlocks = np.unique(data['NBlock'])

for block in indexBlocks:
    print 'Block {}'.format(block)
    correctAnswers = np.logical_and(data['Picture']==data['Answers'], data['NBlock']==block)
    wrongAnswers = np.logical_and(data['Picture']!=data['Answers'], data['NBlock']==block)
    correctRT = [int(i) for i in data['RT'][correctAnswers]]
    print 'Correct answers: {}'.format(len(correctRT))
    print 'Mean correct RT: {} ms'.format(np.mean(correctRT))<commit_msg>Add config file path<commit_after>import sys, os

sys.path.append(os.path.dirname(os.getcwd()))

from config import dataFolder
from expyriment.misc import data_preprocessing
import numpy as np

agg = data_preprocessing.Aggregator(data_folder=dataFolder,
                                    file_name=sys.argv[1])

print 'Variable computed: '
data = {}
for variable in agg.variables:
    data[variable] = agg.get_variable_data(variable)

indexBlocks = np.unique(data['NBlock'])

for block in indexBlocks:
    print 'Block {}'.format(block)
    correctAnswers = np.logical_and(data['Picture']==data['Answers'], data['NBlock']==block)
    wrongAnswers = np.logical_and(data['Picture']!=data['Answers'], data['NBlock']==block)
    correctRT = [int(i) for i in data['RT'][correctAnswers]]
    print 'Correct answers: {}'.format(len(correctRT))
    print 'Mean correct RT: {} ms'.format(np.mean(correctRT))<|endoftext|>


<commit_before>from django.db import models
from common.diagrams_gpcr import DrawHelixBox, DrawSnakePlot
from residue.models import Residue

class Protein(models.Model):
    parent = models.ForeignKey('self', null=True)
    family = models.ForeignKey('ProteinFamily')
    species = models.ForeignKey('Species')
    source = models.ForeignKey('ProteinSource')
    residue_numbering_scheme = models.ForeignKey('residue.ResidueNumberingScheme')
    sequence_type = models.ForeignKey('ProteinSequenceType')
    states = models.ManyToManyField('ProteinState', through='ProteinConformation')
    endogenous_ligands = models.ManyToManyField('ligand.Ligand')
    web_links = models.ManyToManyField('common.WebLink')
    entry_name = models.SlugField(max_length=100, unique=True)
    accession = models.CharField(max_length=100, db_index=True, null=True)
    name = models.CharField(max_length=200)
    sequence = models.TextField()

    def __str__(self):
        if not self.entry_name:
            return self.name
        else:
            return self.entry_name

    class Meta():
        db_table = 'protein'

    def get_protein_class(self):
        tmp = self.family
        while tmp.parent.parent is not None:
            tmp = tmp.parent
        return tmp.name

    def get_helical_box(self):
        residuelist = self.sequence = Residue.objects.filter(protein_conformation__protein__entry_name=str(self)).prefetch_related('protein_segment','display_generic_number','generic_number')
        return DrawHelixBox(residuelist,self.get_protein_class(),str(self))

    def get_snake_plot(self):
        residuelist = self.sequence = Residue.objects.filter(protein_conformation__protein__entry_name=str(self)).prefetch_related('protein_segment','display_generic_number','generic_number')
        return DrawSnakePlot(residuelist,self.get_protein_class(),str(self))

    def get_protein_family(self):
        tmp = self.family
        while tmp.parent.parent.parent is not None:
            tmp = tmp.parent
        return tmp.name

class ProteinConformation(models.Model):
    protein = models.ForeignKey('Protein')
    state = models.ForeignKey('ProteinState')
    template_structure = models.ForeignKey('structure.Structure', null=True)
    protein_anomalies = models.ManyToManyField('protein.ProteinAnomaly')

    # non-database attributes
    identity = 0 # % identity to a reference sequence in an alignment
    similarity = 0 # % similarity to a reference sequence in an alignment (% BLOSUM62 score > 0)
    similarity_score = 0 # similarity score to a reference sequence in an alignment (sum of BLOSUM62 scores)
    alignment = 0 # residues formatted for use in an Alignment class

    def __str__(self):
        return self.protein.entry_name + " (" + self.state.slug + ")"

    class Meta():
        db_table = "protein_conformation"

class ProteinState(models.Model):
    slug = models.SlugField(max_length=20)
    name = models.CharField(max_length=100)

    def __str__(self):
        return self.slug

    class Meta():
        db_table = "protein_state"

class Gene(models.Model):
    proteins = models.ManyToManyField('Protein')
    species = models.ForeignKey('Species')
    name = models.CharField(max_length=100)
    position = models.SmallIntegerField()

    def __str__(self):
        return self.name

    class Meta():
        ordering = ('position', )
        db_table = 'gene'

class Species(models.Model):
    latin_name = models.CharField(max_length=100)
    common_name = models.CharField(max_length=100, blank=True)

    def __str__(self):
        return self.latin_name

    class Meta():
        db_table = 'species'

class ProteinAlias(models.Model):
    protein = models.ForeignKey('Protein')
    name = models.CharField(max_length=200)
    position = models.SmallIntegerField()

    def __str__(self):
        return self.name

    class Meta():
        ordering = ('position', )
        db_table = 'protein_alias'

class ProteinSet(models.Model):
    proteins = models.ManyToManyField('Protein')
    name = models.CharField(max_length=50)

    def __str__(self):
        return self.name

    class Meta():
        db_table = 'protein_set'

class ProteinSegment(models.Model):
    slug = models.SlugField(max_length=100)
    name = models.CharField(max_length=50)
    category = models.CharField(max_length=50)
    partial = models.BooleanField(default=False)

    def __str__(self):
        return self.slug

    class Meta():
        ordering = ('id', )
        db_table = 'protein_segment'

class ProteinSource(models.Model):
    name = models.CharField(max_length=20)

    def __str__(self):
        return self.name

    class Meta():
        db_table = 'protein_source'

class ProteinFamily(models.Model):
    parent = models.ForeignKey('self', null=True)
    slug = models.SlugField(max_length=100)
    name = models.CharField(max_length=200)

    def __str__(self):
        return self.name

    class Meta():
        db_table = 'protein_family'
        ordering = ('id', )

class ProteinSequenceType(models.Model):
    slug = models.SlugField(max_length=20)
    name = models.CharField(max_length=100)

    def __str__(self):
        return self.slug

    class Meta():
        db_table = 'protein_sequence_type'

class ProteinAnomaly(models.Model):
    anomaly_type = models.ForeignKey('ProteinAnomalyType')
    generic_number = models.ForeignKey('residue.ResidueGenericNumber')

    def __str__(self):
        return self.generic_number.label

    class Meta():
        ordering = ('generic_number__label', )
        db_table = 'protein_anomaly'

class ProteinAnomalyType(models.Model):
    slug = models.SlugField(max_length=20)
    name = models.CharField(max_length=100)

    def __str__(self):
        return self.slug

    class Meta():
        db_table = 'protein_anomaly_type'

class ProteinAnomalyRuleSet(models.Model):
    protein_anomaly = models.ForeignKey('ProteinAnomaly', related_name='rulesets')
    exclusive = models.BooleanField(default=False)

    def __str__(self):
        return self.protein_anomaly.generic_number.label

    class Meta():
        db_table = 'protein_anomaly_rule_set'
        ordering = ('id', )

class ProteinAnomalyRule(models.Model):
    rule_set = models.ForeignKey('ProteinAnomalyRuleSet', related_name='rules')
    generic_number = models.ForeignKey('residue.ResidueGenericNumber')
    amino_acid = models.CharField(max_length=1)
    negative = models.BooleanField(default=False)

    def __str__(self):
        return "{} {}".format(self.generic_number.label, self.amino_acid)

    class Meta():
        db_table = 'protein_anomaly_rule'

class ProteinFusion(models.Model):
    proteins = models.ManyToManyField('Protein', through='ProteinFusionProtein')
    name = models.CharField(max_length=100, unique=True)
    sequence = models.TextField(null=True)

    def __str__(self):
        return self.name

    class Meta():
        db_table = 'protein_fusion'

class ProteinFusionProtein(models.Model):
    protein = models.ForeignKey('Protein')
    protein_fusion = models.ForeignKey('ProteinFusion')
    segment_before = models.ForeignKey('ProteinSegment', related_name='segment_before')
    segment_after = models.ForeignKey('ProteinSegment', related_name='segment_after')

    def __str__(self):
        return self.protein.name + " " + self.protein_fusion.name

    class Meta():
        db_table = 'protein_fusion_protein'

class ProteinConformationTemplateStructure(models.Model):
    protein_conformation = models.ForeignKey('ProteinConformation')
    protein_segment = models.ForeignKey('ProteinSegment')
    structure = models.ForeignKey('structure.Structure')

    def __str__(self):
        return self.protein_conformation.protein.name + " " + self.protein_segment.slug \
        + " " + self.structure.pdb_code.index

    class Meta():
        db_table = 'protein_conformation_template_structure'<commit_msg>minor<commit_after>from django.db import models
from common.diagrams_gpcr import DrawHelixBox, DrawSnakePlot
from residue.models import Residue

class Protein(models.Model):
    parent = models.ForeignKey('self', null=True)
    family = models.ForeignKey('ProteinFamily')
    species = models.ForeignKey('Species')
    source = models.ForeignKey('ProteinSource')
    residue_numbering_scheme = models.ForeignKey('residue.ResidueNumberingScheme')
    sequence_type = models.ForeignKey('ProteinSequenceType')
    states = models.ManyToManyField('ProteinState', through='ProteinConformation')
    endogenous_ligands = models.ManyToManyField('ligand.Ligand')
    web_links = models.ManyToManyField('common.WebLink')
    entry_name = models.SlugField(max_length=100, unique=True)
    accession = models.CharField(max_length=100, db_index=True, null=True)
    name = models.CharField(max_length=200)
    sequence = models.TextField()

    def __str__(self):
        if not self.entry_name:
            return self.name
        else:
            return self.entry_name

    class Meta():
        db_table = 'protein'

    def get_protein_class(self):
        tmp = self.family
        while tmp.parent.parent is not None:
            tmp = tmp.parent
        return tmp.name

    def get_helical_box(self):
        residuelist = Residue.objects.filter(protein_conformation__protein__entry_name=str(self)).prefetch_related('protein_segment','display_generic_number','generic_number')
        return DrawHelixBox(residuelist,self.get_protein_class(),str(self))

    def get_snake_plot(self):
        residuelist = Residue.objects.filter(protein_conformation__protein__entry_name=str(self)).prefetch_related('protein_segment','display_generic_number','generic_number')
        return DrawSnakePlot(residuelist,self.get_protein_class(),str(self))

    def get_protein_family(self):
        tmp = self.family
        while tmp.parent.parent.parent is not None:
            tmp = tmp.parent
        return tmp.name

class ProteinConformation(models.Model):
    protein = models.ForeignKey('Protein')
    state = models.ForeignKey('ProteinState')
    template_structure = models.ForeignKey('structure.Structure', null=True)
    protein_anomalies = models.ManyToManyField('protein.ProteinAnomaly')

    # non-database attributes
    identity = 0 # % identity to a reference sequence in an alignment
    similarity = 0 # % similarity to a reference sequence in an alignment (% BLOSUM62 score > 0)
    similarity_score = 0 # similarity score to a reference sequence in an alignment (sum of BLOSUM62 scores)
    alignment = 0 # residues formatted for use in an Alignment class

    def __str__(self):
        return self.protein.entry_name + " (" + self.state.slug + ")"

    class Meta():
        db_table = "protein_conformation"

class ProteinState(models.Model):
    slug = models.SlugField(max_length=20)
    name = models.CharField(max_length=100)

    def __str__(self):
        return self.slug

    class Meta():
        db_table = "protein_state"

class Gene(models.Model):
    proteins = models.ManyToManyField('Protein')
    species = models.ForeignKey('Species')
    name = models.CharField(max_length=100)
    position = models.SmallIntegerField()

    def __str__(self):
        return self.name

    class Meta():
        ordering = ('position', )
        db_table = 'gene'

class Species(models.Model):
    latin_name = models.CharField(max_length=100)
    common_name = models.CharField(max_length=100, blank=True)

    def __str__(self):
        return self.latin_name

    class Meta():
        db_table = 'species'

class ProteinAlias(models.Model):
    protein = models.ForeignKey('Protein')
    name = models.CharField(max_length=200)
    position = models.SmallIntegerField()

    def __str__(self):
        return self.name

    class Meta():
        ordering = ('position', )
        db_table = 'protein_alias'

class ProteinSet(models.Model):
    proteins = models.ManyToManyField('Protein')
    name = models.CharField(max_length=50)

    def __str__(self):
        return self.name

    class Meta():
        db_table = 'protein_set'

class ProteinSegment(models.Model):
    slug = models.SlugField(max_length=100)
    name = models.CharField(max_length=50)
    category = models.CharField(max_length=50)
    partial = models.BooleanField(default=False)

    def __str__(self):
        return self.slug

    class Meta():
        ordering = ('id', )
        db_table = 'protein_segment'

class ProteinSource(models.Model):
    name = models.CharField(max_length=20)

    def __str__(self):
        return self.name

    class Meta():
        db_table = 'protein_source'

class ProteinFamily(models.Model):
    parent = models.ForeignKey('self', null=True)
    slug = models.SlugField(max_length=100)
    name = models.CharField(max_length=200)

    def __str__(self):
        return self.name

    class Meta():
        db_table = 'protein_family'
        ordering = ('id', )

class ProteinSequenceType(models.Model):
    slug = models.SlugField(max_length=20)
    name = models.CharField(max_length=100)

    def __str__(self):
        return self.slug

    class Meta():
        db_table = 'protein_sequence_type'

class ProteinAnomaly(models.Model):
    anomaly_type = models.ForeignKey('ProteinAnomalyType')
    generic_number = models.ForeignKey('residue.ResidueGenericNumber')

    def __str__(self):
        return self.generic_number.label

    class Meta():
        ordering = ('generic_number__label', )
        db_table = 'protein_anomaly'

class ProteinAnomalyType(models.Model):
    slug = models.SlugField(max_length=20)
    name = models.CharField(max_length=100)

    def __str__(self):
        return self.slug

    class Meta():
        db_table = 'protein_anomaly_type'

class ProteinAnomalyRuleSet(models.Model):
    protein_anomaly = models.ForeignKey('ProteinAnomaly', related_name='rulesets')
    exclusive = models.BooleanField(default=False)

    def __str__(self):
        return self.protein_anomaly.generic_number.label

    class Meta():
        db_table = 'protein_anomaly_rule_set'
        ordering = ('id', )

class ProteinAnomalyRule(models.Model):
    rule_set = models.ForeignKey('ProteinAnomalyRuleSet', related_name='rules')
    generic_number = models.ForeignKey('residue.ResidueGenericNumber')
    amino_acid = models.CharField(max_length=1)
    negative = models.BooleanField(default=False)

    def __str__(self):
        return "{} {}".format(self.generic_number.label, self.amino_acid)

    class Meta():
        db_table = 'protein_anomaly_rule'

class ProteinFusion(models.Model):
    proteins = models.ManyToManyField('Protein', through='ProteinFusionProtein')
    name = models.CharField(max_length=100, unique=True)
    sequence = models.TextField(null=True)

    def __str__(self):
        return self.name

    class Meta():
        db_table = 'protein_fusion'

class ProteinFusionProtein(models.Model):
    protein = models.ForeignKey('Protein')
    protein_fusion = models.ForeignKey('ProteinFusion')
    segment_before = models.ForeignKey('ProteinSegment', related_name='segment_before')
    segment_after = models.ForeignKey('ProteinSegment', related_name='segment_after')

    def __str__(self):
        return self.protein.name + " " + self.protein_fusion.name

    class Meta():
        db_table = 'protein_fusion_protein'

class ProteinConformationTemplateStructure(models.Model):
    protein_conformation = models.ForeignKey('ProteinConformation')
    protein_segment = models.ForeignKey('ProteinSegment')
    structure = models.ForeignKey('structure.Structure')

    def __str__(self):
        return self.protein_conformation.protein.name + " " + self.protein_segment.slug \
        + " " + self.structure.pdb_code.index

    class Meta():
        db_table = 'protein_conformation_template_structure'<|endoftext|>


<commit_before>import time

priv = lambda(x): x.startswith((u'192.168.', u'10.', u'172.'))
pub  = lambda(x): not priv(x)

class Machine(object):
    def __init__(self, data=None, datacenter=None, machine_id=None):
        self.id = machine_id or data.pop('id')
        self.datacenter = datacenter
        if not data:
            data = self.datacenter.raw_machine_data(self.id)
        self._save(data)

    def __str__(self):
        return self.id

    def __repr__(self):
        if self.datacenter:
            dc = str(self.datacenter)
        else:
            dc = '<None>'
        return '<{module}.{cls}: <{name}> in {dc}>'.format(module=self.__module__,
            cls=self.__class__.__name__, name=self.name, dc=dc)

    def _save(self, data):
        self.name = data.get('name')
        self.type = data.get('type')
        self.state = data.get('state')
        self.dataset = data.get('dataset')
        self.memory = data.get('memory')
        self.disk = data.get('disk')
        self.ips = data.get('ips', [])
        self.metadata = data.get('metadata', {})
        self.created = data.get('created')
        self.updated = data.get('updated')

    def refresh(self):
        data = self.datacenter.raw_machine_data(self.id)
        self._save(data)

    @property
    def path(self):
        return 'machines/{id}'.format(id=self.id)

    @property
    def public_ips(self):
        return filter(pub, self.ips)

    @property
    def private_ips(self):
        return filter(priv, self.ips)

    def stop(self):
        action = {'action': 'stop'}
        j, r = self.datacenter.request('POST', self.path, params=action)
        r.raise_for_status()

    def start(self):
        action = {'action': 'start'}
        j, r = self.datacenter.request('POST', self.path, params=action)
        r.raise_for_status()

    def reboot(self):
        action = {'action': 'reboot'}
        j, r = self.datacenter.request('POST', self.path, params=action)
        r.raise_for_status()

    def resize(self, package):
        if isinstance(package, dict):
            package = package['name']
        action = {'action': 'resize',
                  'package': package}
        j, r = self.datacenter.request('POST', self.path, params=action)
        r.raise_for_status()

    def status(self):
        self.refresh()
        return self.state

    def delete(self):
        j, r = self.datacenter.request('DELETE', self.path)
        r.raise_for_status()

    @classmethod
    def create_in_datacenter(cls, datacenter, **kwargs):
        return datacenter.create_machine(**kwargs)

    def poll_until(self, state, interval=2):
        while self.status() != state:
            time.sleep(interval)

    def poll_while(self, state, interval=2):
        while self.status() == state:
            time.sleep(interval)

    def get_metadata(self):
        j, _ = self.datacenter.request('GET', self.path + '/metadata')
        self.metadata = j
        return j

    def update_metadata(self, **kwargs):
        j, _ = self.datacenter.request('POST', self.path + '/metadata', params=kwargs)
        self.metadata = j
        return j

    def delete_metadata_at_key(self, key):
        j, r = self.datacenter.request('DELETE', self.path + '/metadata/' + key)
        r.raise_for_status()
        return self.get_metadata()

    def delete_all_metadata(self):
        j, r = self.datacenter.request('DELETE', self.path + '/metadata')
        r.raise_for_status()
        return self.get_metadata()

    def get_tags(self):
        j, _ = self.datacenter.request('GET', self.path + '/tags')
        return j

    def add_tags(self, **kwargs):
        j, _ = self.datacenter.request('POST', self.path + '/tags', params=kwargs)
        return j

    def get_tag(self, tag):
        headers = {'Accept': 'text/plain'}
        j, _ = self.datacenter.request('GET', self.path + '/tags/' + tag)
        return j

    def delete_tag(self, tag):
        j, r = self.datacenter.request('DELETE', self.path + '/tags/' + tag)
        r.raise_for_status()

    def delete_all_tags(self):
        j, r = self.datacenter.request('DELETE', self.path + '/tags')
        r.raise_for_status()

<commit_msg>snapshots: machines are complete<commit_after>import time

priv = lambda(x): x.startswith((u'192.168.', u'10.', u'172.'))
pub  = lambda(x): not priv(x)

class Machine(object):
    def __init__(self, data=None, datacenter=None, machine_id=None):
        self.id = machine_id or data.pop('id')
        self.datacenter = datacenter
        if not data:
            data = self.datacenter.raw_machine_data(self.id)
        self._save(data)

    def __str__(self):
        return self.id

    def __repr__(self):
        if self.datacenter:
            dc = str(self.datacenter)
        else:
            dc = '<None>'
        return '<{module}.{cls}: <{name}> in {dc}>'.format(module=self.__module__,
            cls=self.__class__.__name__, name=self.name, dc=dc)

    def _save(self, data):
        self.name = data.get('name')
        self.type = data.get('type')
        self.state = data.get('state')
        self.dataset = data.get('dataset')
        self.memory = data.get('memory')
        self.disk = data.get('disk')
        self.ips = data.get('ips', [])
        self.metadata = data.get('metadata', {})
        self.created = data.get('created')
        self.updated = data.get('updated')

    def refresh(self):
        data = self.datacenter.raw_machine_data(self.id)
        self._save(data)

    @property
    def path(self):
        return 'machines/{id}'.format(id=self.id)

    @property
    def public_ips(self):
        return filter(pub, self.ips)

    @property
    def private_ips(self):
        return filter(priv, self.ips)

    def stop(self):
        action = {'action': 'stop'}
        j, r = self.datacenter.request('POST', self.path, params=action)
        r.raise_for_status()

    def start(self):
        action = {'action': 'start'}
        j, r = self.datacenter.request('POST', self.path, params=action)
        r.raise_for_status()

    def reboot(self):
        action = {'action': 'reboot'}
        j, r = self.datacenter.request('POST', self.path, params=action)
        r.raise_for_status()

    def resize(self, package):
        if isinstance(package, dict):
            package = package['name']
        action = {'action': 'resize',
                  'package': package}
        j, r = self.datacenter.request('POST', self.path, params=action)
        r.raise_for_status()

    def status(self):
        self.refresh()
        return self.state

    def delete(self):
        j, r = self.datacenter.request('DELETE', self.path)
        r.raise_for_status()

    @classmethod
    def create_in_datacenter(cls, datacenter, **kwargs):
        return datacenter.create_machine(**kwargs)

    def poll_until(self, state, interval=2):
        while self.status() != state:
            time.sleep(interval)

    def poll_while(self, state, interval=2):
        while self.status() == state:
            time.sleep(interval)

    def get_metadata(self):
        j, _ = self.datacenter.request('GET', self.path + '/metadata')
        self.metadata = j
        return j

    def update_metadata(self, **kwargs):
        j, _ = self.datacenter.request('POST', self.path + '/metadata', params=kwargs)
        self.metadata = j
        return j

    def delete_metadata_at_key(self, key):
        j, r = self.datacenter.request('DELETE', self.path + '/metadata/' + key)
        r.raise_for_status()
        return self.get_metadata()

    def delete_all_metadata(self):
        j, r = self.datacenter.request('DELETE', self.path + '/metadata')
        r.raise_for_status()
        return self.get_metadata()

    def get_tags(self):
        j, _ = self.datacenter.request('GET', self.path + '/tags')
        return j

    def add_tags(self, **kwargs):
        j, _ = self.datacenter.request('POST', self.path + '/tags', params=kwargs)
        return j

    def get_tag(self, tag):
        headers = {'Accept': 'text/plain'}
        j, _ = self.datacenter.request('GET', self.path + '/tags/' + tag)
        return j

    def delete_tag(self, tag):
        j, r = self.datacenter.request('DELETE', self.path + '/tags/' + tag)
        r.raise_for_status()

    def delete_all_tags(self):
        j, r = self.datacenter.request('DELETE', self.path + '/tags')
        r.raise_for_status()

    def raw_snapshot_data(self, name):
        j, _ = self.datacenter.request('GET', self.path + '/snapshots/' + str(name))
        return j

    def snapshots(self):
        j, _ = self.datacenter.request('GET', self.path + '/snapshots')
        return [Snapshot(machine=self, data=s) for s in j]

    def create_snapshot(self, name):
        params = {'name': name}
        j, _ = self.datacenter.request('POST', self.path + '/snapshots', params=params)
        return Snapshot(machine=self, data=j, name=name)

    def start_snapshot(self, name):
        _, r = self.datacenter.request('POST', self.path + '/snapshots/' + str(name))
        r.raise_for_status()

class Snapshot(object):
    def __init__(self, machine=None, data=None, name=None):
        self.name = name or data.pop('name')
        self.machine = machine
        if not data:
            data = self.machine.raw_snapshot_data(self.name)
        self._save(data)

    def __str__(self):
        return self.name

    def __repr__(self):
        return '<{module}.{cls}: <{name}> on <Machine: {mach}>>'.format(module=self.__module__,
            cls=self.__class__.__name__, name=self.name, mach=str(self.machine.name))

    def _save(self, data):
        self.state = data.get('state')
        self.created = data.get('created')
        self.updated = data.get('updated')

    @property
    def path(self):
        return self.machine.path + '/snapshots/' + self.name

    def refresh(self):
        data = self.machine.raw_snapshot_data(self.name)
        self._save(data)

    def status(self):
        self.refresh()
        return self.state

    def delete(self):
        _, r = self.machine.datacenter.request('DELETE', self.path)
        r.raise_for_status()

    def start(self):
        _, r = self.machine.datacenter.request('POST', self.path)
        r.raise_for_status()

<|endoftext|>


<commit_before>
class Semaphore(object):

    """docstring for Semaphore"""
    def __init__(self, db, id, counter, value, collection_name='mongotools.semaphore'):
        self._db = db
        self._name = collection_name
        self._id = id
        self._counter = counter
        self._db[self._name].update(dict(_id=id), {'$setOnInsert':{counter:value}}, upsert=True)

    def acquire(self):
        doc = self._db[self._name].update({'_id':self._id, self._counter:{'$gt':0}}, {'$inc': {self._counter:-1} }, w=1)
        return doc['updatedExisting']

    def release(self):
        self._db[self._name].update({'_id':self._id}, {'$inc': {self._counter:1}})

<commit_msg>Handle upsert with multiple semaphores better<commit_after>
class Semaphore(object):

    """docstring for Semaphore"""
    def __init__(self, db, id, counter, value, collection_name='mongotools.semaphore'):
        self._db = db
        self._name = collection_name
        self._id = id
        self._counter = counter
        doc = self._db[self._name].find_and_modify(dict(_id=id), {'$setOnInsert':{counter:value}}, upsert=True, new=True)
        if counter not in doc:
            self._db[self._name].update({'_id':id, counter:{'$exists':False}}, {'$set': {counter:value}}, w=1)

    def acquire(self):
        doc = self._db[self._name].update({'_id':self._id, self._counter:{'$gt':0}}, {'$inc': {self._counter:-1} }, w=1)
        return doc['updatedExisting']

    def release(self):
        self._db[self._name].update({'_id':self._id}, {'$inc': {self._counter:1}})

<|endoftext|>


<commit_before>"""
Settings package is acting exactly like settings module in standard django projects.
However, settings combines two distinct things:
  (1) General project configuration, which is property of the project
    (like which application to use, URL configuration, authentication backends...)
  (2) Machine-specific environment configuration (database to use, cache URL, ...)

Thus, we're changing module into package:
  * base.py contains (1), so no adjustments there should be needed to make project
    on your machine
  * config.py contains (2) with sensible default values that should make project
    runnable on most expected machines
  * local.py contains (2) for your specific machine. File your defaults there.
"""

from base import *
from config import *

# try to import some settings from /etc/
import sys
sys.path.insert(0, '/etc/mealplanner')
try:
    from mealplanner_config import *
except ImportError:
    pass
del sys.path[0]

# load any settings for local development
try:
    from local import *
except ImportError:
    pass
<commit_msg>mealplanner -> boris<commit_after>"""
Settings package is acting exactly like settings module in standard django projects.
However, settings combines two distinct things:
  (1) General project configuration, which is property of the project
    (like which application to use, URL configuration, authentication backends...)
  (2) Machine-specific environment configuration (database to use, cache URL, ...)

Thus, we're changing module into package:
  * base.py contains (1), so no adjustments there should be needed to make project
    on your machine
  * config.py contains (2) with sensible default values that should make project
    runnable on most expected machines
  * local.py contains (2) for your specific machine. File your defaults there.
"""

from base import *
from config import *

# try to import some settings from /etc/
import sys
sys.path.insert(0, '/etc/boris')
try:
    from boris_config import *
except ImportError:
    pass
del sys.path[0]

# load any settings for local development
try:
    from local import *
except ImportError:
    pass
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
"""
@author: David Siroky (siroky@dasir.cz)
@license: MIT License (see LICENSE.txt or
          U{http://www.opensource.org/licenses/mit-license.php})
"""

VERSION = "0.5.3"
PROTOCOL_VERSION = 1
<commit_msg>- version update<commit_after># -*- coding: utf-8 -*-
"""
@author: David Siroky (siroky@dasir.cz)
@license: MIT License (see LICENSE.txt or
          U{http://www.opensource.org/licenses/mit-license.php})
"""

VERSION = "0.5.4"
PROTOCOL_VERSION = 1
<|endoftext|>


<commit_before>from mopidy.backends.base import (BaseBackend, BaseCurrentPlaylistController,
    BasePlaybackController, BasePlaybackProvider, BaseLibraryController,
    BaseStoredPlaylistsController, BaseStoredPlaylistsProvider)
from mopidy.models import Playlist

class DummyBackend(BaseBackend):
    """
    A backend which implements the backend API in the simplest way possible.
    Used in tests of the frontends.

    Handles URIs starting with ``dummy:``.
    """

    def __init__(self, *args, **kwargs):
        super(DummyBackend, self).__init__(*args, **kwargs)

        self.current_playlist = DummyCurrentPlaylistController(backend=self)

        self.library = DummyLibraryController(backend=self)

        playback_provider = DummyPlaybackProvider(backend=self)
        self.playback = DummyPlaybackController(backend=self,
            provider=playback_provider)

        stored_playlists_provider = DummyStoredPlaylistsProvider(backend=self)
        self.stored_playlists = BaseStoredPlaylistsController(backend=self,
            provider=stored_playlists_provider)

        self.uri_handlers = [u'dummy:']

class DummyCurrentPlaylistController(BaseCurrentPlaylistController):
    pass

class DummyLibraryController(BaseLibraryController):
    _library = []

    def find_exact(self, **query):
        return Playlist()

    def lookup(self, uri):
        matches = filter(lambda t: uri == t.uri, self._library)
        if matches:
            return matches[0]

    def refresh(self, uri=None):
        pass

    def search(self, **query):
        return Playlist()

class DummyPlaybackController(BasePlaybackController):
    def _trigger_started_playing_event(self):
        pass # noop

    def _trigger_stopped_playing_event(self):
        pass # noop

class DummyPlaybackProvider(BasePlaybackProvider):
    def pause(self):
        return True

    def play(self, track):
        """Pass None as track to force failure"""
        return track is not None

    def resume(self):
        return True

    def seek(self, time_position):
        return True

    def stop(self):
        return True

class DummyStoredPlaylistsProvider(BaseStoredPlaylistsProvider):
    def create(self, name):
        playlist = Playlist(name=name)
        self._playlists.append(playlist)
        return playlist

    def delete(self, playlist):
        self._playlists.remove(playlist)

    def lookup(self, uri):
        return filter(lambda p: p.uri == uri, self._playlists)

    def refresh(self):
        pass

    def rename(self, playlist, new_name):
        self._playlists[self._playlists.index(playlist)] = \
            playlist.with_(name=new_name)

    def save(self, playlist):
        self._playlists.append(playlist)
<commit_msg>Use a DummyQueue in DummyBackend to silence triggered events<commit_after>from mopidy.backends.base import (BaseBackend, BaseCurrentPlaylistController,
    BasePlaybackController, BasePlaybackProvider, BaseLibraryController,
    BaseStoredPlaylistsController, BaseStoredPlaylistsProvider)
from mopidy.models import Playlist

class DummyQueue(object):
    def __init__(self):
        self.received_messages = []

    def put(self, message):
        self.received_messages.append(message)

class DummyBackend(BaseBackend):
    """
    A backend which implements the backend API in the simplest way possible.
    Used in tests of the frontends.

    Handles URIs starting with ``dummy:``.
    """

    def __init__(self, *args, **kwargs):
        super(DummyBackend, self).__init__(*args, **kwargs)

        self.core_queue = DummyQueue()

        self.current_playlist = BaseCurrentPlaylistController(backend=self)

        self.library = DummyLibraryController(backend=self)

        playback_provider = DummyPlaybackProvider(backend=self)
        self.playback = BasePlaybackController(backend=self,
            provider=playback_provider)

        stored_playlists_provider = DummyStoredPlaylistsProvider(backend=self)
        self.stored_playlists = BaseStoredPlaylistsController(backend=self,
            provider=stored_playlists_provider)

        self.uri_handlers = [u'dummy:']

class DummyLibraryController(BaseLibraryController):
    _library = []

    def find_exact(self, **query):
        return Playlist()

    def lookup(self, uri):
        matches = filter(lambda t: uri == t.uri, self._library)
        if matches:
            return matches[0]

    def refresh(self, uri=None):
        pass

    def search(self, **query):
        return Playlist()

class DummyPlaybackProvider(BasePlaybackProvider):
    def pause(self):
        return True

    def play(self, track):
        """Pass None as track to force failure"""
        return track is not None

    def resume(self):
        return True

    def seek(self, time_position):
        return True

    def stop(self):
        return True

class DummyStoredPlaylistsProvider(BaseStoredPlaylistsProvider):
    def create(self, name):
        playlist = Playlist(name=name)
        self._playlists.append(playlist)
        return playlist

    def delete(self, playlist):
        self._playlists.remove(playlist)

    def lookup(self, uri):
        return filter(lambda p: p.uri == uri, self._playlists)

    def refresh(self):
        pass

    def rename(self, playlist, new_name):
        self._playlists[self._playlists.index(playlist)] = \
            playlist.with_(name=new_name)

    def save(self, playlist):
        self._playlists.append(playlist)
<|endoftext|>


<commit_before># Software License Agreement (BSD License)
#
# Copyright (c) 2008, Willow Garage, Inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
#  * Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#  * Redistributions in binary form must reproduce the above
#    copyright notice, this list of conditions and the following
#    disclaimer in the documentation and/or other materials provided
#    with the distribution.
#  * Neither the name of Willow Garage, Inc. nor the names of its
#    contributors may be used to endorse or promote products derived
#    from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.

PKG = 'rosrecord'
import roslib; roslib.load_manifest(PKG)
import rospy
import rosbag

class ROSRecordException(Exception):
    """
    Base exception type for rosrecord-related errors.
    """
    def __init__(self, value):
        self.value = value

    def __str__(self):
        return repr(self.value)

class BagReader(object):
    """
    Bag file reader
    """
    def __init__(self, f):
        """
        Open a bag file for reading

        @param f: either a path to bag file to open for reading, or a stream to read from
        @type  f: str or file
        @raise ROSRecordException: if version not supported
        """
        try:
            self._bag = rosbag.Bag(f, 'r')
        except Exception, e:
            raise ROSRecordException(str(e))

    def close(self):
        """
        Close the bag file
        """
        try:
            if self._bag:
                self._bag.close()
        except Exception, e:
            raise ROSRecordException(str(e))

    @property
    def datatypes(self): return dict([(topic_info.topic, topic_info.datatype) for topic_info in self._bag._topic_infos.values()])

    @property
    def filename(self): return self._bag.filename

    def read_index(self):
        """
        Read the index from the file (if it exists)

        @return dict(topic, [(stamp, pos)...]): bag file index
        """
        try:
            self.index = self._bag.getIndex()
        except rosbag.ROSBagException:
            self.index = None

        return self.index

    def logplayer(self, raw=False, seek=None):
        if not self._bag:
            return

        if seek is not None:
            raise ROSRecordException('seek not supported')

        try:
            try:
                for topic, msg, t in self._bag.readMessages(raw=raw):
                    if msg is None or rospy.is_shutdown():
                        break
                    yield topic, msg, t
            except KeyboardInterrupt:
                pass # break iterator
        finally:
            self._bag.close()

    def raw_messages(self):
        """
        Generates raw messages

        @return (pos, topic, msg, t)...: tuple of position, topic, message data and time stamp
        """
        try:
            for topic, msg, t in self._bag.readMessages(raw=True):
                if msg is None or rospy.is_shutdown():
                    break
                (datatype, data, md5sum, position, pytype) = msg
                yield position, topic, msg, t

        except rosbag.ROSBagException, e:
            rospy.logerr('ROSBagException: couldn\'t read message - %s' % str(e))
        except IOError:
            rospy.logerr('IOError: couldn\'t read message')
        except KeyError:
            rospy.logerr('KeyError: couldn\'t read message')

    def load_message(self, pos, index=None):
        """
        Load message from the bag file at a given position

        @param pos: byte offset in file of start of message record
        @type  pos: int
        @return (datatype, msg, t): tuple of datatype, deserialized message, and timestamp
        """
        msgs = list(self.load_messages([pos], index))
        if len(msgs) == 0:
            return None, None, None

        return msgs[0]

    ## Returns iterator of (datatype, message, timestamp)
    def load_messages(self, positions, index=None):
        try:
            for pos in positions:
                _, raw_msg, t = self._bag._read_message(pos, True)
                if raw_msg is not None:
                    (datatype, message_data, md5, bag_pos, pytype) = raw_msg

                    msg = pytype()
                    msg.deserialize(message_data)

                    yield (datatype, msg, t)

        except rosbag.ROSBagException, e:
            rospy.logerr('ROSRecordException: couldn\'t read %s - %s' % (str(pos), str(e)))
        except IOError:
            rospy.logerr('IOError: couldn\'t read %d' % pos)
        except KeyError:
            rospy.logerr('KeyError: couldn\'t read %d' % pos)

def logplayer(f, raw=False, seek=None):
    """
    Iterator for messages in a bag file. Return type of iterator depends on value of 'raw' parameter, which
    is provided for improved iteration performance.

    If raw is False:
      - topic: topic name (str)
      - msg: Message instance (Message)
      - t: time (rospy.Time)

     If raw is True:
      - topic: topic name (str)
      - msg: (datatype, data, md5sum, bag_position).
         - datatype: msg type name
         - data: serialized message bytes.
         - bag_position: offset into the file, which can be used for seeking.
         - md5sum: md5sum of msg type for identify msg version
         - NOTE: More elements may be added to this in the future, so this should
           be unpacked assuming non-fixed length.
      - t: time (rospy.Time)

    @param filename: name of file to playback from
    @type  filename: str
    @param raw: see description for raw behavior
    @type  raw: bool
    @param seek: initial position to seek to (default None)
    @type  seek: int
    @raise ROSRecordException: if file format is unrecognized
    @return: iterator for messages. See description for iterator behavior.
    @rtype: iterator
    """
    r = BagReader(f)

    return r.logplayer(raw, seek)

class Rebagger(object):
    """
    Utility class for writing Message instances to a ROS .bag file
    """
    def __init__(self, filename):
        self._bag = rosbag.Bag(filename, 'w')

    def add(self, topic, msg, t=None, raw=False):
        """
        Add a message to the bag
        @param msg: message to add to bag
        @type  msg: Message
        @param raw: if True, msg is in raw format (msg_type, serialized_bytes, md5sum, pytype)
        @param raw: bool
        @param t: ROS time of message publication
        @type  t: U{roslib.message.Time}
        """
        try:
            self._bag.write(topic, msg, t, raw)
        except:
            raise ROSRecordException('error adding message to bag')

    def close(self):
        try:
            if self._bag:
                self._bag.close()
        except:
            raise ROSRecordException('error closing Rebagger')

def rebag(inbag, outbag, filter_fn, verbose_pattern=None, raw=False):
    """
    Filter the contents of inbag to outbag using filter_fn
    @param inbag: filename of input bag file.
    @type  inbag: str
    @param outbag: filename of output bag file. Existing bag file will be overwritten
    @type  outbag: str
    @param filter_fn: Python function that returns True if msg is to be kept
    @type  filter_fn: fn(topic, msg, time)
    @param raw: if True, msg will be kept deserialized. This is useful
    if you are removing messages that no longer exist.
    @type  raw: bool
    @param verbose_pattern: Python function that returns string to
    print for verbose debugging
    @type  verbose_pattern: fn(topic, msg, time)
    """
    if verbose_pattern:
        rebag = Rebagger(outbag)
        for topic, msg, t in logplayer(inbag, raw=raw):
            if filter_fn(topic, msg, t):
                print "MATCH", verbose_pattern(topic, msg, t)
                rebag.add(topic, msg, t, raw=raw)
                if rospy.is_shutdown():
                    break
            else:
                print "NO MATCH", verbose_pattern(topic, msg, t)
    else: #streamlined
        rebag = Rebagger(outbag)
        for topic, msg, t in logplayer(inbag, raw=raw):
            if filter_fn(topic, msg, t):
                rebag.add(topic, msg, t, raw=raw)
                if rospy.is_shutdown():
                    break

def _rebag_main():
    """
    main routine for rosrebag command-line tool
    """
    ## filter function that uses command line expression to test topic/message/time
    def expr_eval(expr):
        def eval_fn(topic, m, t):
            return eval(expr)
        return eval_fn

    parser = optparse.OptionParser(usage="""Usage: %prog in.bag out.bag filter-expression

filter-expression can be any Python-legal expression.
The following variables are available:
 * topic: name of topic
 * m: message
 * t: time of message (t.secs, t.nsecs)
""", prog='rosrebag')
    parser.add_option('--print', dest="verbose_pattern", default=None,
                      metavar="PRINT-EXPRESSION", help="Python expression to print for verbose debugging. Uses same variables as filter-expression.")

    options, args = parser.parse_args()
    if len(args) == 0:
        parser.print_usage()
        sys.exit(0)
    elif len(args) != 3:
        parser.error("invalid arguments")

    inbag, outbag, expr = args
    if options.verbose_pattern:
        verbose_pattern = expr_eval(options.verbose_pattern)
    else:
        verbose_pattern = None
    if not os.path.isfile(inbag):
        print >> sys.stderr, "cannot locate input bag file [%s]" % inbag
        sys.exit(1)

    rebag(inbag, outbag, expr_eval(expr), verbose_pattern=verbose_pattern)
<commit_msg>rosrecord: updating wrapper for change from topics to connections<commit_after># Software License Agreement (BSD License)
#
# Copyright (c) 2008, Willow Garage, Inc.
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
#
#  * Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
#  * Redistributions in binary form must reproduce the above
#    copyright notice, this list of conditions and the following
#    disclaimer in the documentation and/or other materials provided
#    with the distribution.
#  * Neither the name of Willow Garage, Inc. nor the names of its
#    contributors may be used to endorse or promote products derived
#    from this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
# LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.

PKG = 'rosrecord'
import roslib; roslib.load_manifest(PKG)
import rospy
import rosbag

class ROSRecordException(Exception):
    """
    Base exception type for rosrecord-related errors.
    """
    def __init__(self, value):
        self.value = value

    def __str__(self):
        return repr(self.value)

class BagReader(object):
    """
    Bag file reader
    """
    def __init__(self, f):
        """
        Open a bag file for reading

        @param f: either a path to bag file to open for reading, or a stream to read from
        @type  f: str or file
        @raise ROSRecordException: if version not supported
        """
        try:
            self._bag = rosbag.Bag(f, 'r')
        except Exception, e:
            raise ROSRecordException(str(e))

    def close(self):
        """
        Close the bag file
        """
        try:
            if self._bag:
                self._bag.close()
        except Exception, e:
            raise ROSRecordException(str(e))

    @property
    def datatypes(self): return dict([(info.topic, info.datatype) for info in self._bag._connections.values()])

    @property
    def filename(self): return self._bag.filename

    def read_index(self):
        """
        Read the index from the file (if it exists)

        @return dict(topic, [(stamp, pos)...]): bag file index
        """
        try:
            self.index = self._bag.getIndex()
        except rosbag.ROSBagException:
            self.index = None

        return self.index

    def logplayer(self, raw=False, seek=None):
        if not self._bag:
            return

        if seek is not None:
            raise ROSRecordException('seek not supported')

        try:
            try:
                for topic, msg, t in self._bag.readMessages(raw=raw):
                    if msg is None or rospy.is_shutdown():
                        break
                    yield topic, msg, t
            except KeyboardInterrupt:
                pass # break iterator
        finally:
            self._bag.close()

    def raw_messages(self):
        """
        Generates raw messages

        @return (pos, topic, msg, t)...: tuple of position, topic, message data and time stamp
        """
        try:
            for topic, msg, t in self._bag.readMessages(raw=True):
                if msg is None or rospy.is_shutdown():
                    break
                (datatype, data, md5sum, position, pytype) = msg
                yield position, topic, msg, t

        except rosbag.ROSBagException, e:
            rospy.logerr('ROSBagException: couldn\'t read message - %s' % str(e))
        except IOError:
            rospy.logerr('IOError: couldn\'t read message')
        except KeyError:
            rospy.logerr('KeyError: couldn\'t read message')

    def load_message(self, pos, index=None):
        """
        Load message from the bag file at a given position

        @param pos: byte offset in file of start of message record
        @type  pos: int
        @return (datatype, msg, t): tuple of datatype, deserialized message, and timestamp
        """
        msgs = list(self.load_messages([pos], index))
        if len(msgs) == 0:
            return None, None, None

        return msgs[0]

    ## Returns iterator of (datatype, message, timestamp)
    def load_messages(self, positions, index=None):
        try:
            for pos in positions:
                _, raw_msg, t = self._bag._read_message(pos, True)
                if raw_msg is not None:
                    (datatype, message_data, md5, bag_pos, pytype) = raw_msg

                    msg = pytype()
                    msg.deserialize(message_data)

                    yield (datatype, msg, t)

        except rosbag.ROSBagException, e:
            rospy.logerr('ROSRecordException: couldn\'t read %s - %s' % (str(pos), str(e)))
        except IOError:
            rospy.logerr('IOError: couldn\'t read %d' % pos)
        except KeyError:
            rospy.logerr('KeyError: couldn\'t read %d' % pos)

def logplayer(f, raw=False, seek=None):
    """
    Iterator for messages in a bag file. Return type of iterator depends on value of 'raw' parameter, which
    is provided for improved iteration performance.

    If raw is False:
      - topic: topic name (str)
      - msg: Message instance (Message)
      - t: time (rospy.Time)

     If raw is True:
      - topic: topic name (str)
      - msg: (datatype, data, md5sum, bag_position).
         - datatype: msg type name
         - data: serialized message bytes.
         - bag_position: offset into the file, which can be used for seeking.
         - md5sum: md5sum of msg type for identify msg version
         - NOTE: More elements may be added to this in the future, so this should
           be unpacked assuming non-fixed length.
      - t: time (rospy.Time)

    @param filename: name of file to playback from
    @type  filename: str
    @param raw: see description for raw behavior
    @type  raw: bool
    @param seek: initial position to seek to (default None)
    @type  seek: int
    @raise ROSRecordException: if file format is unrecognized
    @return: iterator for messages. See description for iterator behavior.
    @rtype: iterator
    """
    r = BagReader(f)

    return r.logplayer(raw, seek)

class Rebagger(object):
    """
    Utility class for writing Message instances to a ROS .bag file
    """
    def __init__(self, filename):
        self._bag = rosbag.Bag(filename, 'w')

    def add(self, topic, msg, t=None, raw=False):
        """
        Add a message to the bag
        @param msg: message to add to bag
        @type  msg: Message
        @param raw: if True, msg is in raw format (msg_type, serialized_bytes, md5sum, pytype)
        @param raw: bool
        @param t: ROS time of message publication
        @type  t: U{roslib.message.Time}
        """
        try:
            self._bag.write(topic, msg, t, raw)
        except:
            raise ROSRecordException('error adding message to bag')

    def close(self):
        try:
            if self._bag:
                self._bag.close()
        except:
            raise ROSRecordException('error closing Rebagger')

def rebag(inbag, outbag, filter_fn, verbose_pattern=None, raw=False):
    """
    Filter the contents of inbag to outbag using filter_fn
    @param inbag: filename of input bag file.
    @type  inbag: str
    @param outbag: filename of output bag file. Existing bag file will be overwritten
    @type  outbag: str
    @param filter_fn: Python function that returns True if msg is to be kept
    @type  filter_fn: fn(topic, msg, time)
    @param raw: if True, msg will be kept deserialized. This is useful
    if you are removing messages that no longer exist.
    @type  raw: bool
    @param verbose_pattern: Python function that returns string to
    print for verbose debugging
    @type  verbose_pattern: fn(topic, msg, time)
    """
    if verbose_pattern:
        rebag = Rebagger(outbag)
        for topic, msg, t in logplayer(inbag, raw=raw):
            if filter_fn(topic, msg, t):
                print "MATCH", verbose_pattern(topic, msg, t)
                rebag.add(topic, msg, t, raw=raw)
                if rospy.is_shutdown():
                    break
            else:
                print "NO MATCH", verbose_pattern(topic, msg, t)
    else: #streamlined
        rebag = Rebagger(outbag)
        for topic, msg, t in logplayer(inbag, raw=raw):
            if filter_fn(topic, msg, t):
                rebag.add(topic, msg, t, raw=raw)
                if rospy.is_shutdown():
                    break

def _rebag_main():
    """
    main routine for rosrebag command-line tool
    """
    ## filter function that uses command line expression to test topic/message/time
    def expr_eval(expr):
        def eval_fn(topic, m, t):
            return eval(expr)
        return eval_fn

    parser = optparse.OptionParser(usage="""Usage: %prog in.bag out.bag filter-expression

filter-expression can be any Python-legal expression.
The following variables are available:
 * topic: name of topic
 * m: message
 * t: time of message (t.secs, t.nsecs)
""", prog='rosrebag')
    parser.add_option('--print', dest="verbose_pattern", default=None,
                      metavar="PRINT-EXPRESSION", help="Python expression to print for verbose debugging. Uses same variables as filter-expression.")

    options, args = parser.parse_args()
    if len(args) == 0:
        parser.print_usage()
        sys.exit(0)
    elif len(args) != 3:
        parser.error("invalid arguments")

    inbag, outbag, expr = args
    if options.verbose_pattern:
        verbose_pattern = expr_eval(options.verbose_pattern)
    else:
        verbose_pattern = None
    if not os.path.isfile(inbag):
        print >> sys.stderr, "cannot locate input bag file [%s]" % inbag
        sys.exit(1)

    rebag(inbag, outbag, expr_eval(expr), verbose_pattern=verbose_pattern)
<|endoftext|>


<commit_before>#/usr/bin/env python

# Copyright (c) 2009, Giampaolo Rodola'. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

"""Common objects shared by all _ps* modules."""

from __future__ import division
import errno
import os
import socket
import stat
import sys
import warnings

from socket import AF_INET, SOCK_STREAM, SOCK_DGRAM

from psutil._compat import namedtuple, wraps

# --- constants

AF_INET6 = getattr(socket, 'AF_INET6', None)
AF_UNIX = getattr(socket, 'AF_UNIX', None)

STATUS_RUNNING = "running"
STATUS_SLEEPING = "sleeping"
STATUS_DISK_SLEEP = "disk-sleep"
STATUS_STOPPED = "stopped"
STATUS_TRACING_STOP = "tracing-stop"
STATUS_ZOMBIE = "zombie"
STATUS_DEAD = "dead"
STATUS_WAKE_KILL = "wake-kill"
STATUS_WAKING = "waking"
STATUS_IDLE = "idle"  # BSD
STATUS_LOCKED = "locked"  # BSD
STATUS_WAITING = "waiting"  # BSD

CONN_ESTABLISHED = "ESTABLISHED"
CONN_SYN_SENT = "SYN_SENT"
CONN_SYN_RECV = "SYN_RECV"
CONN_FIN_WAIT1 = "FIN_WAIT1"
CONN_FIN_WAIT2 = "FIN_WAIT2"
CONN_TIME_WAIT = "TIME_WAIT"
CONN_CLOSE = "CLOSE"
CONN_CLOSE_WAIT = "CLOSE_WAIT"
CONN_LAST_ACK = "LAST_ACK"
CONN_LISTEN = "LISTEN"
CONN_CLOSING = "CLOSING"
CONN_NONE = "NONE"

# --- functions

def usage_percent(used, total, _round=None):
    """Calculate percentage usage of 'used' against 'total'."""
    try:
        ret = (used / total) * 100
    except ZeroDivisionError:
        ret = 0
    if _round is not None:
        return round(ret, _round)
    else:
        return ret

def memoize(fun):
    """A simple memoize decorator for functions."""
    @wraps(fun)
    def wrapper(*args, **kwargs):
        key = (args, frozenset(sorted(kwargs.items())))
        try:
            return cache[key]
        except KeyError:
            ret = cache[key] = fun(*args, **kwargs)
        return ret
    cache = {}
    return wrapper

# http://code.activestate.com/recipes/576563-cached-property/
def cached_property(fun):
    """A memoize decorator for class properties."""
    @wraps(fun)
    def get(self):
        try:
            return self._cache[fun]
        except AttributeError:
            self._cache = {}
        except KeyError:
            pass
        ret = self._cache[fun] = fun(self)
        return ret
    return property(get)

# http://goo.gl/jYLvf
def deprecated(replacement=None):
    """A decorator which can be used to mark functions as deprecated."""
    def outer(fun):
        msg = "psutil.%s is deprecated" % fun.__name__
        if replacement is not None:
            msg += "; use %s instead" % replacement
        if fun.__doc__ is None:
            fun.__doc__ = msg

        @wraps(fun)
        def inner(*args, **kwargs):
            warnings.warn(msg, category=DeprecationWarning, stacklevel=2)
            return fun(*args, **kwargs)

        return inner
    return outer

def isfile_strict(path):
    """Same as os.path.isfile() but does not swallow EACCES / EPERM
    exceptions, see:
    http://mail.python.org/pipermail/python-dev/2012-June/120787.html
    """
    try:
        st = os.stat(path)
    except OSError:
        err = sys.exc_info()[1]
        if err.errno in (errno.EPERM, errno.EACCES):
            raise
        return False
    else:
        return stat.S_ISREG(st.st_mode)

# --- Process.get_connections() 'kind' parameter mapping

conn_tmap = {
    "all":   ([AF_INET, AF_INET6, AF_UNIX], [SOCK_STREAM, SOCK_DGRAM]),
    "tcp":   ([AF_INET, AF_INET6], [SOCK_STREAM]),
    "tcp4":  ([AF_INET],           [SOCK_STREAM]),
    "udp":   ([AF_INET, AF_INET6], [SOCK_DGRAM]),
    "udp4":  ([AF_INET],           [SOCK_DGRAM]),
    "inet":  ([AF_INET, AF_INET6], [SOCK_STREAM, SOCK_DGRAM]),
    "inet4": ([AF_INET],           [SOCK_STREAM, SOCK_DGRAM]),
    "inet6": ([AF_INET6],          [SOCK_STREAM, SOCK_DGRAM]),
}

if AF_INET6 is not None:
    conn_tmap.update({
        "tcp6": ([AF_INET6],       [SOCK_STREAM]),
        "udp6": ([AF_INET6],       [SOCK_DGRAM]),
    })

if AF_UNIX is not None:
    conn_tmap.update({
        "unix": ([AF_UNIX],        [SOCK_STREAM, SOCK_DGRAM]),
    })

del AF_INET, AF_INET6, AF_UNIX, SOCK_STREAM, SOCK_DGRAM, socket

# --- namedtuples

# system
nt_sysmeminfo = namedtuple('usage', 'total used free percent')
# XXX - would 'available' be better than 'free' as for virtual_memory() nt?
nt_swapmeminfo = namedtuple('swap', 'total used free percent sin sout')
nt_diskinfo = namedtuple('usage', 'total used free percent')
nt_partition = namedtuple('partition', 'device mountpoint fstype opts')
nt_net_iostat = namedtuple(
    'iostat',
    'bytes_sent bytes_recv packets_sent packets_recv errin errout dropin dropout')
nt_disk_iostat = namedtuple(
    'iostat',
    'read_count write_count read_bytes write_bytes read_time write_time')
nt_user = namedtuple('user', 'name terminal host started')

# processes
nt_meminfo = namedtuple('meminfo', 'rss vms')
nt_cputimes = namedtuple('cputimes', 'user system')
nt_openfile = namedtuple('openfile', 'path fd')
nt_thread = namedtuple('thread', 'id user_time system_time')
nt_uids = namedtuple('user', 'real effective saved')
nt_gids = namedtuple('group', 'real effective saved')
nt_io = namedtuple('io', 'read_count write_count read_bytes write_bytes')
nt_ionice = namedtuple('ionice', 'ioclass value')
nt_ctxsw = namedtuple('amount', 'voluntary involuntary')

# --- misc

# backward compatibility layer for Process.get_connections() ntuple
class nt_connection(namedtuple('connection',
                               'fd family type laddr raddr status')):
    __slots__ = ()

    @property
    def local_address(self):
        warnings.warn("'local_address' field is deprecated; use 'laddr'"
                      "instead", category=DeprecationWarning, stacklevel=2)
        return self.laddr

    @property
    def remote_address(self):
        warnings.warn("'remote_address' field is deprecated; use 'raddr'"
                      "instead", category=DeprecationWarning, stacklevel=2)
        return self.raddr
<commit_msg>namedtuples refactoring<commit_after>#/usr/bin/env python

# Copyright (c) 2009, Giampaolo Rodola'. All rights reserved.
# Use of this source code is governed by a BSD-style license that can be
# found in the LICENSE file.

"""Common objects shared by all _ps* modules."""

from __future__ import division
import errno
import os
import socket
import stat
import sys
import warnings

from socket import AF_INET, SOCK_STREAM, SOCK_DGRAM

from psutil._compat import namedtuple, wraps

# --- constants

AF_INET6 = getattr(socket, 'AF_INET6', None)
AF_UNIX = getattr(socket, 'AF_UNIX', None)

STATUS_RUNNING = "running"
STATUS_SLEEPING = "sleeping"
STATUS_DISK_SLEEP = "disk-sleep"
STATUS_STOPPED = "stopped"
STATUS_TRACING_STOP = "tracing-stop"
STATUS_ZOMBIE = "zombie"
STATUS_DEAD = "dead"
STATUS_WAKE_KILL = "wake-kill"
STATUS_WAKING = "waking"
STATUS_IDLE = "idle"  # BSD
STATUS_LOCKED = "locked"  # BSD
STATUS_WAITING = "waiting"  # BSD

CONN_ESTABLISHED = "ESTABLISHED"
CONN_SYN_SENT = "SYN_SENT"
CONN_SYN_RECV = "SYN_RECV"
CONN_FIN_WAIT1 = "FIN_WAIT1"
CONN_FIN_WAIT2 = "FIN_WAIT2"
CONN_TIME_WAIT = "TIME_WAIT"
CONN_CLOSE = "CLOSE"
CONN_CLOSE_WAIT = "CLOSE_WAIT"
CONN_LAST_ACK = "LAST_ACK"
CONN_LISTEN = "LISTEN"
CONN_CLOSING = "CLOSING"
CONN_NONE = "NONE"

# --- functions

def usage_percent(used, total, _round=None):
    """Calculate percentage usage of 'used' against 'total'."""
    try:
        ret = (used / total) * 100
    except ZeroDivisionError:
        ret = 0
    if _round is not None:
        return round(ret, _round)
    else:
        return ret

def memoize(fun):
    """A simple memoize decorator for functions."""
    @wraps(fun)
    def wrapper(*args, **kwargs):
        key = (args, frozenset(sorted(kwargs.items())))
        try:
            return cache[key]
        except KeyError:
            ret = cache[key] = fun(*args, **kwargs)
        return ret
    cache = {}
    return wrapper

# http://code.activestate.com/recipes/576563-cached-property/
def cached_property(fun):
    """A memoize decorator for class properties."""
    @wraps(fun)
    def get(self):
        try:
            return self._cache[fun]
        except AttributeError:
            self._cache = {}
        except KeyError:
            pass
        ret = self._cache[fun] = fun(self)
        return ret
    return property(get)

# http://goo.gl/jYLvf
def deprecated(replacement=None):
    """A decorator which can be used to mark functions as deprecated."""
    def outer(fun):
        msg = "psutil.%s is deprecated" % fun.__name__
        if replacement is not None:
            msg += "; use %s instead" % replacement
        if fun.__doc__ is None:
            fun.__doc__ = msg

        @wraps(fun)
        def inner(*args, **kwargs):
            warnings.warn(msg, category=DeprecationWarning, stacklevel=2)
            return fun(*args, **kwargs)

        return inner
    return outer

def isfile_strict(path):
    """Same as os.path.isfile() but does not swallow EACCES / EPERM
    exceptions, see:
    http://mail.python.org/pipermail/python-dev/2012-June/120787.html
    """
    try:
        st = os.stat(path)
    except OSError:
        err = sys.exc_info()[1]
        if err.errno in (errno.EPERM, errno.EACCES):
            raise
        return False
    else:
        return stat.S_ISREG(st.st_mode)

# --- Process.get_connections() 'kind' parameter mapping

conn_tmap = {
    "all":   ([AF_INET, AF_INET6, AF_UNIX], [SOCK_STREAM, SOCK_DGRAM]),
    "tcp":   ([AF_INET, AF_INET6], [SOCK_STREAM]),
    "tcp4":  ([AF_INET],           [SOCK_STREAM]),
    "udp":   ([AF_INET, AF_INET6], [SOCK_DGRAM]),
    "udp4":  ([AF_INET],           [SOCK_DGRAM]),
    "inet":  ([AF_INET, AF_INET6], [SOCK_STREAM, SOCK_DGRAM]),
    "inet4": ([AF_INET],           [SOCK_STREAM, SOCK_DGRAM]),
    "inet6": ([AF_INET6],          [SOCK_STREAM, SOCK_DGRAM]),
}

if AF_INET6 is not None:
    conn_tmap.update({
        "tcp6": ([AF_INET6],       [SOCK_STREAM]),
        "udp6": ([AF_INET6],       [SOCK_DGRAM]),
    })

if AF_UNIX is not None:
    conn_tmap.update({
        "unix": ([AF_UNIX],        [SOCK_STREAM, SOCK_DGRAM]),
    })

del AF_INET, AF_INET6, AF_UNIX, SOCK_STREAM, SOCK_DGRAM, socket

# --- namedtuples

# psutil.virtual_memory()  (expanded later)
nt_sysmeminfo = namedtuple('usage', ['total', 'used', 'free', 'percent'])
# psutil.swap_memory()
nt_swapmeminfo = namedtuple('swap', ['total', 'used', 'free', 'percent',
                                     'sin', 'sout'])
# psutil.disk_usage()
nt_diskinfo = namedtuple('usage', ['total', 'used', 'free', 'percent'])
# psutil.disk_partitions()
nt_partition = namedtuple('partition', ['device', 'mountpoint', 'fstype', 'opts'])
# psutil.net_io_counters()
nt_net_iostat = namedtuple('iostat', ['bytes_sent', 'bytes_recv',
                                      'packets_sent', 'packets_recv',
                                      'errin', 'errout', 'dropin', 'dropout'])
# psutil.disk_io_counters()
nt_disk_iostat = namedtuple('iostat', ['read_count', 'write_count',
                                       'read_bytes', 'write_bytes',
                                       'read_time', 'write_time'])
# psutil.get_users()
nt_user = namedtuple('user', ['name', 'terminal', 'host', 'started'])

# psutil.Process.get_memory_info()
nt_meminfo = namedtuple('meminfo', ['rss', 'vms'])
# psutil.Process.get_cpu_times()
nt_cputimes = namedtuple('cputimes', ['user', 'system'])
# psutil.Process.get_open_files()
nt_openfile = namedtuple('openfile', ['path', 'fd'])
# psutil.Process.get_threads()
nt_thread = namedtuple('thread', ['id', 'user_time', 'system_time'])
# psutil.Process.uids
nt_uids = namedtuple('user', ['real', 'effective', 'saved'])
# psutil.Process.gids
nt_gids = namedtuple('group', ['real', 'effective', 'saved'])
# psutil.Process.get_io_counters()
nt_io = namedtuple('io', ['read_count', 'write_count',
                          'read_bytes', 'write_bytes'])
# psutil.Process.get_ionice()
nt_ionice = namedtuple('ionice', ['ioclass', 'value'])
# psutil.Process.get_ctx_switches()
nt_ctxsw = namedtuple('amount', ['voluntary', 'involuntary'])

# --- misc

# backward compatibility layer for Process.get_connections() ntuple
class nt_connection(namedtuple('connection',
                               'fd family type laddr raddr status')):
    __slots__ = ()

    @property
    def local_address(self):
        warnings.warn("'local_address' field is deprecated; use 'laddr'"
                      "instead", category=DeprecationWarning, stacklevel=2)
        return self.laddr

    @property
    def remote_address(self):
        warnings.warn("'remote_address' field is deprecated; use 'raddr'"
                      "instead", category=DeprecationWarning, stacklevel=2)
        return self.raddr
<|endoftext|>


<commit_before>from fabric.api import run, env, with_settings, quiet

@with_settings(quiet())
def get_interfaces():
    return run('ifconfig -l').strip().split()

@with_settings(quiet())
def get_realmem():
    import math
    realmem = run('sysctl -n hw.realmem').strip()
    realmem = float(realmem) / 1024 / 1024
    return 2 ** int(math.ceil(math.log(realmem, 2)))

@with_settings(quiet())
def get_devices():
    """ computes the name of the disk devices that are suitable
    installation targets by subtracting CDROM- and USB devices
    from the list of total mounts.
    """
    mounts = run('mount')
    sysctl_devices = run('sysctl -n kern.disks').strip().split()
    cd_device = env.instance.config.get('bootstrap-cd-device', 'cd0')
    if '/dev/{dev} on /rw/cdrom'.format(dev=cd_device) not in mounts:
        run('test -e /dev/{dev} && mount_cd9660 /dev/{dev} /cdrom || true'.format(dev=cd_device))
    usb_device = env.instance.config.get('bootstrap-usb-device', 'da0a')
    if '/dev/{dev} on /rw/media'.format(dev=usb_device) not in mounts:
        run('test -e /dev/{dev} && mount -o ro /dev/{dev} /media || true'.format(dev=usb_device))

    install_devices = [cd_device, usb_device]

    devices = set(sysctl_devices)
    for sysctl_device in sysctl_devices:
        for install_device in install_devices:
            if install_device.startswith(sysctl_device):
                devices.remove(sysctl_device)

    return devices
<commit_msg>The decorator doesn't work properly on repeated calls.<commit_after>from fabric.api import run, env, settings, quiet

def get_interfaces():
    with settings(quiet()):
        return run('ifconfig -l').strip().split()

def get_realmem():
    import math
    with settings(quiet()):
        realmem = run('sysctl -n hw.realmem').strip()
    realmem = float(realmem) / 1024 / 1024
    return 2 ** int(math.ceil(math.log(realmem, 2)))

def get_devices():
    """ computes the name of the disk devices that are suitable
    installation targets by subtracting CDROM- and USB devices
    from the list of total mounts.
    """
    with settings(quiet()):
        mounts = run('mount')
        sysctl_devices = run('sysctl -n kern.disks').strip().split()
    cd_device = env.instance.config.get('bootstrap-cd-device', 'cd0')
    if '/dev/{dev} on /rw/cdrom'.format(dev=cd_device) not in mounts:
        run('test -e /dev/{dev} && mount_cd9660 /dev/{dev} /cdrom || true'.format(dev=cd_device))
    usb_device = env.instance.config.get('bootstrap-usb-device', 'da0a')
    if '/dev/{dev} on /rw/media'.format(dev=usb_device) not in mounts:
        run('test -e /dev/{dev} && mount -o ro /dev/{dev} /media || true'.format(dev=usb_device))

    install_devices = [cd_device, usb_device]

    devices = set(sysctl_devices)
    for sysctl_device in sysctl_devices:
        for install_device in install_devices:
            if install_device.startswith(sysctl_device):
                devices.remove(sysctl_device)

    return devices
<|endoftext|>