<commit_before>from datetime import datetime

from flask import current_app
from sqlalchemy_utils.types import phone_number
from flask_store.sqla import FlaskStoreType
from sqlalchemy import UniqueConstraint

from ..extensions import db, cache
from ..utils import convert_to_dict
from ..political_data.adapters import adapt_to_target
from .constants import (CAMPAIGN_CHOICES, CAMPAIGN_NESTED_CHOICES, STRING_LEN, TWILIO_SID_LENGTH,
                        CAMPAIGN_STATUS, STATUS_PAUSED, SEGMENT_BY_CHOICES, LOCATION_CHOICES, ORDERING_CHOICES, )

class Campaign(db.Model):
    __tablename__ = 'campaign_campaign'

    id = db.Column(db.Integer, primary_key=True)
    created_time = db.Column(db.DateTime, default=datetime.utcnow)

    name = db.Column(db.String(STRING_LEN), nullable=False, unique=True)
    campaign_type = db.Column(db.String(STRING_LEN))
    campaign_state = db.Column(db.String(STRING_LEN))
    campaign_subtype = db.Column(db.String(STRING_LEN))

    segment_by = db.Column(db.String(STRING_LEN))
    locate_by = db.Column(db.String(STRING_LEN))
    target_set = db.relationship('Target', secondary='campaign_target_sets',
                                 order_by='campaign_target_sets.c.order',
                                 backref=db.backref('campaigns'))
    target_ordering = db.Column(db.String(STRING_LEN))

    allow_call_in = db.Column(db.Boolean, default=False)
    phone_number_set = db.relationship('TwilioPhoneNumber', secondary='campaign_phone_numbers',
                                       backref=db.backref('campaigns'))
    call_maximum = db.Column(db.SmallInteger, nullable=True)

    audio_recordings = db.relationship('AudioRecording', secondary='campaign_audio_recordings',
                                       backref=db.backref('campaigns'))

    status_code = db.Column(db.SmallInteger, default=STATUS_PAUSED)

    @property
    def status(self):
        return CAMPAIGN_STATUS.get(self.status_code, '')

    def __unicode__(self):
        return self.name

    def audio(self, key):
        "Convenience method for getting selected audio recordings for this campaign by key "
        campaignAudio = self._audio_query().filter(
            CampaignAudioRecording.recording.has(key=key)
        ).first()

        if campaignAudio:
            return campaignAudio.recording
        else:
            # if not defined by user, return default
            return current_app.config.CAMPAIGN_MESSAGE_DEFAULTS.get(key)

    def audio_msgs(self):
        "Convenience method for getting all selected audio recordings for this campaign"
        table = {}
        for r in self._audio_query().all():
            table[r.recording.key] = r.recording.file_url()
        return table

    def _audio_query(self):
        return CampaignAudioRecording.query.filter(
            CampaignAudioRecording.campaign_id == self.id,
            CampaignAudioRecording.selected == True)

    def campaign_type_display(self):
        "Display method for this campaign's type"
        campaign_choices = convert_to_dict(CAMPAIGN_CHOICES)
        val = ''
        if self.campaign_type:
            val = campaign_choices.get(self.campaign_type, '')

        return val

    def campaign_subtype_display(self):
        "Display method for this campaign's subtype"
        subtype_choices = convert_to_dict(CAMPAIGN_NESTED_CHOICES)
        campaign_subtypes = dict(subtype_choices[self.campaign_type])
        val = ''
        if self.campaign_subtype and self.campaign_subtype != "None":
            sub = campaign_subtypes.get(self.campaign_subtype, '')
            if self.campaign_type == 'state':
                # special case, show specific state
                val = '%s - %s' % (self.campaign_state, sub)
            else:
                val = sub
        return val

    def phone_numbers(self, region_code=None):
        "Phone numbers for this campaign, can be limited to a specified region code (ISO-2)"
        if region_code:
            # convert region_code to country_code for comparison
            country_code = phone_number.phonenumbers.country_code_for_region(region_code)
            return [n.number.national for n in self.phone_number_set if n.number.country_code == country_code]
        else:
            # return all numbers in set
            return [n.number.national for n in self.phone_number_set]

    def required_fields(self):
        """API convenience method for rendering campaigns externally
        Returns dict of parameters and data types required to place call"""
        fields = {'userPhone': 'US'}  # TODO, update for multiple countries
        if self.segment_by == 'location':
            fields.update({'userLocation': self.locate_by})
        return fields

    def segment_display(self):
        "Display method for this campaign's segmenting and locating of callers"
        val = dict(SEGMENT_BY_CHOICES)[self.segment_by]
        if self.segment_by == 'location':
            val = '%s - %s' % (val, dict(LOCATION_CHOICES)[self.locate_by])
        return val

    def targets(self):
        "Convenience method for getting list of target names and phone numbers"
        return [(s.name, str(s.number)) for s in self.target_set]

    def targets_display(self):
        "Display method for this campaign's target list if specified, or subtype (like Congress - Senate)"
        if self.target_set:
            return "
".join(["%s %s" % (t) for t in self.targets()])
        else:
            return self.campaign_subtype_display()

class CampaignTarget(db.Model):
    __tablename__ = 'campaign_target_sets'

    id = db.Column(db.Integer, primary_key=True)
    campaign_id = db.Column(db.Integer, db.ForeignKey('campaign_campaign.id'))
    target_id = db.Column(db.Integer, db.ForeignKey('campaign_target.id'))
    order = db.Column(db.Integer())

    campaign = db.relationship('Campaign', backref='campaign_targets')
    target = db.relationship('Target', backref='campaign_targets')

class CampaignPhoneNumber(db.Model):
    __tablename__ = 'campaign_phone_numbers'

    id = db.Column(db.Integer, primary_key=True)
    campaign_id = db.Column(db.Integer, db.ForeignKey('campaign_campaign.id'))
    phone_id = db.Column(db.Integer, db.ForeignKey('campaign_phone.id'), unique=False)

class Target(db.Model):
    __tablename__ = 'campaign_target'

    id = db.Column(db.Integer, primary_key=True)
    uid = db.Column(db.String(STRING_LEN), index=True, nullable=True)  # for US, this is bioguide_id
    title = db.Column(db.String(STRING_LEN), nullable=True)
    name = db.Column(db.String(STRING_LEN), nullable=False, unique=False)
    number = db.Column(phone_number.PhoneNumberType())

    def __unicode__(self):
        return self.name

    def full_name(self):
        return unicode("{} {}".format(self.title, self.name), 'utf8')

    @classmethod
    def get_uid_or_cache(cls, uid, key_prefix="us:bioguide"):
        t = Target.query.filter(Target.uid == uid).first()
        cached = False

        if not t:
            key = '%s:%s' % (key_prefix, uid)
            cache_list = cache.get(key)
            if cache_list:
                # TODO, check to ensure it is list-like
                obj = cache_list[0]
                data = adapt_to_target(obj, key_prefix)

                # create target object and save for reuse
                t = Target(**data)
                cached = True
        return t, cached

class TwilioPhoneNumber(db.Model):
    __tablename__ = 'campaign_phone'

    id = db.Column(db.Integer, primary_key=True)
    twilio_sid = db.Column(db.String(TWILIO_SID_LENGTH))
    twilio_app = db.Column(db.String(TWILIO_SID_LENGTH))
    call_in_allowed = db.Column(db.Boolean, default=False)
    number = db.Column(phone_number.PhoneNumberType())

    def __unicode__(self):
        return self.number.__unicode__()

    @classmethod
    def available_numbers(cls, limit=None):
        # returns all numbers which do not have call_in_allowed
        return TwilioPhoneNumber.query.filter_by(call_in_allowed=False).limit(limit)

class AudioRecording(db.Model):
    __tablename__ = 'campaign_recording'

    id = db.Column(db.Integer, primary_key=True)
    key = db.Column(db.String(STRING_LEN), nullable=False)

    file_storage = db.Column(FlaskStoreType(location='audio/'), nullable=True)
    text_to_speech = db.Column(db.Text)
    version = db.Column(db.Integer)
    description = db.Column(db.String(STRING_LEN))

    hidden = db.Column(db.Boolean, default=False)

    __table_args__ = (UniqueConstraint('key', 'version'),)

    def file_url(self):
        if self.file_storage:
            return self.file_storage.absolute_url
        else:
            return None

    def selected_recordings(self):
        return self.campaign_audio_recordings.filter_by(selected=True).all()

    def selected_campaigns(self):
        names = [s.campaign.name for s in self.selected_recordings()]
        return ','.join(names)

    def selected_campaign_ids(self):
        ids = [s.campaign.id for s in self.selected_recordings()]
        return ids

    def __unicode__(self):
        return "%s v%s" % (self.key, self.version)

class CampaignAudioRecording(db.Model):
    __tablename__ = 'campaign_audio_recordings'

    id = db.Column(db.Integer, primary_key=True)
    campaign_id = db.Column(db.Integer, db.ForeignKey('campaign_campaign.id'))
    recording_id = db.Column(db.Integer, db.ForeignKey('campaign_recording.id'))
    selected = db.Column(db.Boolean, default=False)

    campaign = db.relationship('Campaign')
    recording = db.relationship('AudioRecording', backref=db.backref('campaign_audio_recordings',
                                                                     lazy='dynamic'))
<commit_msg>temp call_in fix, until we have twilio apps setup<commit_after>from datetime import datetime

from flask import current_app
from sqlalchemy_utils.types import phone_number
from flask_store.sqla import FlaskStoreType
from sqlalchemy import UniqueConstraint

from ..extensions import db, cache
from ..utils import convert_to_dict
from ..political_data.adapters import adapt_to_target
from .constants import (CAMPAIGN_CHOICES, CAMPAIGN_NESTED_CHOICES, STRING_LEN, TWILIO_SID_LENGTH,
                        CAMPAIGN_STATUS, STATUS_PAUSED, SEGMENT_BY_CHOICES, LOCATION_CHOICES, ORDERING_CHOICES, )

class Campaign(db.Model):
    __tablename__ = 'campaign_campaign'

    id = db.Column(db.Integer, primary_key=True)
    created_time = db.Column(db.DateTime, default=datetime.utcnow)

    name = db.Column(db.String(STRING_LEN), nullable=False, unique=True)
    campaign_type = db.Column(db.String(STRING_LEN))
    campaign_state = db.Column(db.String(STRING_LEN))
    campaign_subtype = db.Column(db.String(STRING_LEN))

    segment_by = db.Column(db.String(STRING_LEN))
    locate_by = db.Column(db.String(STRING_LEN))
    target_set = db.relationship('Target', secondary='campaign_target_sets',
                                 order_by='campaign_target_sets.c.order',
                                 backref=db.backref('campaigns'))
    target_ordering = db.Column(db.String(STRING_LEN))

    allow_call_in = db.Column(db.Boolean, default=False)
    phone_number_set = db.relationship('TwilioPhoneNumber', secondary='campaign_phone_numbers',
                                       backref=db.backref('campaigns'))
    call_maximum = db.Column(db.SmallInteger, nullable=True)

    audio_recordings = db.relationship('AudioRecording', secondary='campaign_audio_recordings',
                                       backref=db.backref('campaigns'))

    status_code = db.Column(db.SmallInteger, default=STATUS_PAUSED)

    @property
    def status(self):
        return CAMPAIGN_STATUS.get(self.status_code, '')

    def __unicode__(self):
        return self.name

    def audio(self, key):
        "Convenience method for getting selected audio recordings for this campaign by key "
        campaignAudio = self._audio_query().filter(
            CampaignAudioRecording.recording.has(key=key)
        ).first()

        if campaignAudio:
            return campaignAudio.recording
        else:
            # if not defined by user, return default
            return current_app.config.CAMPAIGN_MESSAGE_DEFAULTS.get(key)

    def audio_msgs(self):
        "Convenience method for getting all selected audio recordings for this campaign"
        table = {}
        for r in self._audio_query().all():
            table[r.recording.key] = r.recording.file_url()
        return table

    def _audio_query(self):
        return CampaignAudioRecording.query.filter(
            CampaignAudioRecording.campaign_id == self.id,
            CampaignAudioRecording.selected == True)

    def campaign_type_display(self):
        "Display method for this campaign's type"
        campaign_choices = convert_to_dict(CAMPAIGN_CHOICES)
        val = ''
        if self.campaign_type:
            val = campaign_choices.get(self.campaign_type, '')

        return val

    def campaign_subtype_display(self):
        "Display method for this campaign's subtype"
        subtype_choices = convert_to_dict(CAMPAIGN_NESTED_CHOICES)
        campaign_subtypes = dict(subtype_choices[self.campaign_type])
        val = ''
        if self.campaign_subtype and self.campaign_subtype != "None":
            sub = campaign_subtypes.get(self.campaign_subtype, '')
            if self.campaign_type == 'state':
                # special case, show specific state
                val = '%s - %s' % (self.campaign_state, sub)
            else:
                val = sub
        return val

    def phone_numbers(self, region_code=None):
        "Phone numbers for this campaign, can be limited to a specified region code (ISO-2)"
        if region_code:
            # convert region_code to country_code for comparison
            country_code = phone_number.phonenumbers.country_code_for_region(region_code)
            return [n.number.national for n in self.phone_number_set if n.number.country_code == country_code]
        else:
            # return all numbers in set
            return [n.number.national for n in self.phone_number_set]

    def required_fields(self):
        """API convenience method for rendering campaigns externally
        Returns dict of parameters and data types required to place call"""
        fields = {'userPhone': 'US'}  # TODO, update for multiple countries
        if self.segment_by == 'location':
            fields.update({'userLocation': self.locate_by})
        return fields

    def segment_display(self):
        "Display method for this campaign's segmenting and locating of callers"
        val = dict(SEGMENT_BY_CHOICES)[self.segment_by]
        if self.segment_by == 'location':
            val = '%s - %s' % (val, dict(LOCATION_CHOICES)[self.locate_by])
        return val

    def targets(self):
        "Convenience method for getting list of target names and phone numbers"
        return [(s.name, str(s.number)) for s in self.target_set]

    def targets_display(self):
        "Display method for this campaign's target list if specified, or subtype (like Congress - Senate)"
        if self.target_set:
            return "
".join(["%s %s" % (t) for t in self.targets()])
        else:
            return self.campaign_subtype_display()

class CampaignTarget(db.Model):
    __tablename__ = 'campaign_target_sets'

    id = db.Column(db.Integer, primary_key=True)
    campaign_id = db.Column(db.Integer, db.ForeignKey('campaign_campaign.id'))
    target_id = db.Column(db.Integer, db.ForeignKey('campaign_target.id'))
    order = db.Column(db.Integer())

    campaign = db.relationship('Campaign', backref='campaign_targets')
    target = db.relationship('Target', backref='campaign_targets')

class CampaignPhoneNumber(db.Model):
    __tablename__ = 'campaign_phone_numbers'

    id = db.Column(db.Integer, primary_key=True)
    campaign_id = db.Column(db.Integer, db.ForeignKey('campaign_campaign.id'))
    phone_id = db.Column(db.Integer, db.ForeignKey('campaign_phone.id'), unique=False)

class Target(db.Model):
    __tablename__ = 'campaign_target'

    id = db.Column(db.Integer, primary_key=True)
    uid = db.Column(db.String(STRING_LEN), index=True, nullable=True)  # for US, this is bioguide_id
    title = db.Column(db.String(STRING_LEN), nullable=True)
    name = db.Column(db.String(STRING_LEN), nullable=False, unique=False)
    number = db.Column(phone_number.PhoneNumberType())

    def __unicode__(self):
        return self.name

    def full_name(self):
        return unicode("{} {}".format(self.title, self.name), 'utf8')

    @classmethod
    def get_uid_or_cache(cls, uid, key_prefix="us:bioguide"):
        t = Target.query.filter(Target.uid == uid).first()
        cached = False

        if not t:
            key = '%s:%s' % (key_prefix, uid)
            cache_list = cache.get(key)
            if cache_list:
                # TODO, check to ensure it is list-like
                obj = cache_list[0]
                data = adapt_to_target(obj, key_prefix)

                # create target object and save for reuse
                t = Target(**data)
                cached = True
        return t, cached

class TwilioPhoneNumber(db.Model):
    __tablename__ = 'campaign_phone'

    id = db.Column(db.Integer, primary_key=True)
    twilio_sid = db.Column(db.String(TWILIO_SID_LENGTH))
    twilio_app = db.Column(db.String(TWILIO_SID_LENGTH))
    call_in_allowed = db.Column(db.Boolean, default=False)
    number = db.Column(phone_number.PhoneNumberType())

    def __unicode__(self):
        return self.number.__unicode__()

    @classmethod
    def available_numbers(cls, limit=None):
        # returns all numbers which do not have call_in_allowed
        #return TwilioPhoneNumber.query.filter_by(call_in_allowed=False).limit(limit)
        return TwilioPhoneNumber.query.limit(limit)

class AudioRecording(db.Model):
    __tablename__ = 'campaign_recording'

    id = db.Column(db.Integer, primary_key=True)
    key = db.Column(db.String(STRING_LEN), nullable=False)

    file_storage = db.Column(FlaskStoreType(location='audio/'), nullable=True)
    text_to_speech = db.Column(db.Text)
    version = db.Column(db.Integer)
    description = db.Column(db.String(STRING_LEN))

    hidden = db.Column(db.Boolean, default=False)

    __table_args__ = (UniqueConstraint('key', 'version'),)

    def file_url(self):
        if self.file_storage:
            return self.file_storage.absolute_url
        else:
            return None

    def selected_recordings(self):
        return self.campaign_audio_recordings.filter_by(selected=True).all()

    def selected_campaigns(self):
        names = [s.campaign.name for s in self.selected_recordings()]
        return ','.join(names)

    def selected_campaign_ids(self):
        ids = [s.campaign.id for s in self.selected_recordings()]
        return ids

    def __unicode__(self):
        return "%s v%s" % (self.key, self.version)

class CampaignAudioRecording(db.Model):
    __tablename__ = 'campaign_audio_recordings'

    id = db.Column(db.Integer, primary_key=True)
    campaign_id = db.Column(db.Integer, db.ForeignKey('campaign_campaign.id'))
    recording_id = db.Column(db.Integer, db.ForeignKey('campaign_recording.id'))
    selected = db.Column(db.Boolean, default=False)

    campaign = db.relationship('Campaign')
    recording = db.relationship('AudioRecording', backref=db.backref('campaign_audio_recordings',
                                                                     lazy='dynamic'))
<|endoftext|>


<commit_before># Copyright 2015 Novo Nordisk Foundation Center for Biosustainability, DTU.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from __future__ import absolute_import, print_function

__all__ = ['distance_based_on_molecular_formula']

def distance_based_on_molecular_formula(metabolite1, metabolite2, normalize=True):
    """Calculate the distance of two metabolites bases on the molecular formula

    Arguments
    ---------
    metabolite1 : Metabolite
        The first metabolite.
    metabolite2 : Metabolite
        The second metabolite.
    normalize : bool, optional
        If the distance should be normalized by the total number of elements in both metabolites (defaults to True).

    Returns
    -------
    float
        The distance between metabolite1 and metabolite2.
    """
    if len(metabolite1.formula.elements) == 0 or len(metabolite2.formula.elements) == 0:
        raise ValueError('Cannot calculate distance between metabolites %s and %s' % (metabolite1, metabolite2))
    elements = set(list(metabolite1.formula.elements.keys()) + list(metabolite2.formula.elements.keys()))
    distance = 0.
    for element in elements:
        distance += abs(metabolite1.formula.elements.get(element, 0) - metabolite2.formula.elements.get(element, 0))
    if normalize:
        return distance / sum(list(metabolite1.formula.elements.values()) + list(metabolite2.formula.elements.values()))
    else:
        return distance
<commit_msg>fix: it is now metabolite.elements and no longer metabolite.formula.elements<commit_after># Copyright 2015 Novo Nordisk Foundation Center for Biosustainability, DTU.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from __future__ import absolute_import, print_function

__all__ = ['distance_based_on_molecular_formula']

def distance_based_on_molecular_formula(metabolite1, metabolite2, normalize=True):
    """Calculate the distance of two metabolites bases on the molecular formula

    Arguments
    ---------
    metabolite1 : Metabolite
        The first metabolite.
    metabolite2 : Metabolite
        The second metabolite.
    normalize : bool, optional
        If the distance should be normalized by the total number of elements in both metabolites (defaults to True).

    Returns
    -------
    float
        The distance between metabolite1 and metabolite2.
    """
    if len(metabolite1.formula.elements) == 0 or len(metabolite2.formula.elements) == 0:
        raise ValueError('Cannot calculate distance between metabolites %s and %s' % (metabolite1, metabolite2))
    elements = set(list(metabolite1.elements.keys()) + list(metabolite2.elements.keys()))
    distance = 0.
    for element in elements:
        distance += abs(metabolite1.formula.elements.get(element, 0) - metabolite2.formula.elements.get(element, 0))
    if normalize:
        return distance / sum(list(metabolite1.formula.elements.values()) + list(metabolite2.formula.elements.values()))
    else:
        return distance
<|endoftext|>


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

'''
paintSquaresOnScreen.py program

Authors: Raul Fernandez-Fernandez; Juan G. Victores

Universidad Carlos III de Madrid
'''

## @ingroup xgnitive_applications
 # \defgroup paintSquaresOnScreen
 #
 # @brief Application used to paint squares on screen, receives a [X,Y] coordinate via YARP.
 #
 # @section paintSquaresOnScreen_legal Legal
 #
 # Copyright: 2016 (C) Universidad Carlos III de Madrid
 #
 # Author: raulfdzbis, jgvictores
 #
 # CopyPolicy: This program is free software; you can redistribute it and/or modify
 # it under the terms of the GNU Lesser General Public License 3.0 or later
 #
 #

 #
 # This file can be edited at paintSquaresOnScreen

# Import a library of functions called 'pygame'
import pygame
import yarp

# Initialize the game engine
pygame.init()

# Initialize YARP
yarp.Network.init()

# Checking
if yarp.Network.checkNetwork() != True:  # let's see if there was actually a reachable YARP network
    print '[error] Please try running yarp server'  # tell the user to start one with 'yarp server' if there isn't any
    quit()

# Define some colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
BLUE = (0, 0, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)

# Set the height and width of the screen
size = (1920, 1080)
#size = (0, 0)
screen = pygame.display.set_mode(size)
#pygame.FULLSCREEN

pygame.display.set_caption("Teo painting Demo")

# Loop until the user clicks the close button.
done = False
clock = pygame.time.Clock()
screenSize=pygame.display.Info()
print(screenSize.current_w, screenSize.current_h)

#Since using 2 screens are presented we can not auto detect the size of screen
screenW=1920;
screenH=1080;

# Number of screens
scn=1

# Number of rectangles
hrect=4 #Horizontal
vrect=4 #Vertical

# Clear the screen and set the screen background
screen.fill(WHITE)

# YARP Callback
class DataProcessor(yarp.PortReader):

    def myInit(self):
        self.myMem = [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ]

    def read(self,connection):
        print("in DataProcessor.read")
        if not(connection.isValid()):
            print("Connection shutting down")
            return False
        bin = yarp.Bottle()
        bout = yarp.Bottle()
        print("Trying to read from connection")
        ok = bin.read(connection)
        #self.received=bin;

        #Lets paint the rectangle
        x=(hrect-1-bin.get(0).asInt()) #The transfromation is done to change the x axis
        y=bin.get(1).asInt()
        print("Pos rectangle")
        print x
        print y
        #if(x==-1 and y==4):
        #    print("IM CLEANING")
        #    screen.fill(WHITE)
        #else:
        #pygame.draw.rect(screen, BLUE, [x*screenSize.current_w/(hrect*scn), y*screenSize.current_h/vrect, screenSize.current_w/(hrect*scn), screenSize.current_h/vrect], 0)
	pygame.draw.rect(screen, BLUE, [x*screenW/(hrect*scn), y*screenH/vrect, screenW/(hrect*scn), screenH/vrect], 0)
        place = (3-y) + (4*x)
        print 'place',place
        print 'self.myMem',self.myMem
        print 'size',len(self.myMem)
        self.myMem[ place ] = 1
        f = open('memoryOET.txt', 'w')
        line = '%s' % ' '.join(map(str, self.myMem))
        f.write(line)
        f.close()

        if not(ok):
            print("Failed to read input")
            return False
        print("Received [%s]"%bin.toString())
        bout.addString("Received:")
        bout.append(bin)
        print("Sending [%s]"%bout.toString())
        writer = connection.getWriter()
        if writer==None:
            print("No one to reply to")
            return True
        return bout.write(writer)

p = yarp.Port()
r = DataProcessor()
r.myInit()
p.setReader(r)
p.open("/voxelOccupancyDetection/state:i");
# Loop as long as done == False
while not done:
    for event in pygame.event.get():  # User did something
        if event.type == pygame.QUIT:  # If user clicked close
            done = True  # Flag that we are done so we exit this loop

    # Go ahead and update the screen with what we've drawn.
    # This MUST happen after all the other drawing commands.
    pygame.display.flip()

    # This limits the while loop to a max of 60 times per second.
    # Leave this out and we will use all CPU we can.
    clock.tick(60)

# Be IDLE friendly
yarp.Network.fini() # disconnect from the YARP network
pygame.quit()
<commit_msg>publish percentage<commit_after>#!/usr/bin/env python

'''
paintSquaresOnScreen.py program

Authors: Raul Fernandez-Fernandez; Juan G. Victores

Universidad Carlos III de Madrid
'''

## @ingroup xgnitive_applications
 # \defgroup paintSquaresOnScreen
 #
 # @brief Application used to paint squares on screen, receives a [X,Y] coordinate via YARP.
 #
 # @section paintSquaresOnScreen_legal Legal
 #
 # Copyright: 2016 (C) Universidad Carlos III de Madrid
 #
 # Author: raulfdzbis, jgvictores
 #
 # CopyPolicy: This program is free software; you can redistribute it and/or modify
 # it under the terms of the GNU Lesser General Public License 3.0 or later
 #
 #

 #
 # This file can be edited at paintSquaresOnScreen

# Import a library of functions called 'pygame'
import pygame
import yarp

# Initialize the game engine
pygame.init()

# Initialize YARP
yarp.Network.init()

# Checking
if yarp.Network.checkNetwork() != True:  # let's see if there was actually a reachable YARP network
    print '[error] Please try running yarp server'  # tell the user to start one with 'yarp server' if there isn't any
    quit()

# Define some colors
BLACK = (0, 0, 0)
WHITE = (255, 255, 255)
BLUE = (0, 0, 255)
GREEN = (0, 255, 0)
RED = (255, 0, 0)

# Set the height and width of the screen
size = (1920, 1080)
#size = (0, 0)
screen = pygame.display.set_mode(size)
#pygame.FULLSCREEN

pygame.display.set_caption("Teo painting Demo")

# Loop until the user clicks the close button.
done = False
clock = pygame.time.Clock()
screenSize=pygame.display.Info()
print(screenSize.current_w, screenSize.current_h)

#Since using 2 screens are presented we can not auto detect the size of screen
screenW=1920;
screenH=1080;

# Number of screens
scn=1

# Number of rectangles
hrect=4 #Horizontal
vrect=4 #Vertical

# Clear the screen and set the screen background
screen.fill(WHITE)

# YARP Callback
class DataProcessor(yarp.PortReader):

    def myInit(self):
        self.myMem = [ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 ]

    def read(self,connection):
        print("in DataProcessor.read")
        if not(connection.isValid()):
            print("Connection shutting down")
            return False
        bin = yarp.Bottle()
        bout = yarp.Bottle()
        print("Trying to read from connection")
        ok = bin.read(connection)
        #self.received=bin;

        #Lets paint the rectangle
        x=(hrect-1-bin.get(0).asInt()) #The transfromation is done to change the x axis
        y=bin.get(1).asInt()
        print("Pos rectangle")
        print x
        print y
        #if(x==-1 and y==4):
        #    print("IM CLEANING")
        #    screen.fill(WHITE)
        #else:
        #pygame.draw.rect(screen, BLUE, [x*screenSize.current_w/(hrect*scn), y*screenSize.current_h/vrect, screenSize.current_w/(hrect*scn), screenSize.current_h/vrect], 0)
	pygame.draw.rect(screen, BLUE, [x*screenW/(hrect*scn), y*screenH/vrect, screenW/(hrect*scn), screenH/vrect], 0)
        place = (3-y) + (4*x)
        print 'place',place
        print 'self.myMem',self.myMem
        print 'size',len(self.myMem)
        self.myMem[ place ] = 1
        f = open('memoryOET.txt', 'w')
        line = '%s' % ' '.join(map(str, self.myMem))
        f.write(line)
        f.close()

        if not(ok):
            print("Failed to read input")
            return False
        print("Received [%s]"%bin.toString())
        bout.addString("Received:")
        bout.append(bin)
        print("Sending [%s]"%bout.toString())
        writer = connection.getWriter()
        if writer==None:
            print("No one to reply to")
            return True
        return bout.write(writer)

p = yarp.Port()
r = DataProcessor()
r.myInit()
p.setReader(r)
pout = yarp.Port()

p.open("/voxelOccupancyDetection/state:i")
pout.open("/paintSquaresOnScreen/state:o")
# Loop as long as done == False
while not done:
    for event in pygame.event.get():  # User did something
        if event.type == pygame.QUIT:  # If user clicked close
            done = True  # Flag that we are done so we exit this loop

    # Go ahead and update the screen with what we've drawn.
    # This MUST happen after all the other drawing commands.
    pygame.display.flip()

    percent = 0
    for elem in r.myMem:
        percent += elem
    percent = 100.0 * percent / float(len(r.myMem))
    out = yarp.Bottle()
    out.addDouble(percent)
    pout.write(out)

    # This limits the while loop to a max of 60 times per second.
    # Leave this out and we will use all CPU we can.
    clock.tick(60)

# Be IDLE friendly
p.close()
pout.close()
yarp.Network.fini() # disconnect from the YARP network
pygame.quit()
<|endoftext|>


<commit_before>#!/usr/bin/env python
from ConfigParser import SafeConfigParser
from cStringIO import StringIO
import requests
import gzip
import json
import sys
import os

_CONFIG = None
_CONFIG_FN = None
if 'VERBOSE_TESTING' in os.environ:
    try:
        _VERBOSITY_LEVEL = int(os.environ['VERBOSE_TESTING'])
    except:
        _VERBOSITY_LEVEL = 1
else:
    _VERBOSITY_LEVEL = 0
def debug(s):
    if _VERBOSITY_LEVEL > 0:
        sys.stderr.write('testing-harness: {s}\n'.format(s=s))

def config(section=None, param=None, default=None):
    '''
    Returns the config object if `section` and `param` are None, or the
        value for the requested parameter.

    If the parameter (or the section) is missing, the exception is logged and
        None is returned.
    '''
    global _CONFIG, _CONFIG_FN
    if _CONFIG is None:
        _CONFIG_FN = os.path.abspath('test.conf')
        _CONFIG = SafeConfigParser()
        _CONFIG.read(_CONFIG_FN)
        parse_argv_as_options(_CONFIG)
    if section is None and param is None:
        return _CONFIG
    try:
        v = _CONFIG.get(section, param)
        return v
    except:
        if default != None:
            return default
        else:
            sys.stderr.write('Config file "%s" does not contain option "%s" in section "%s"\n' % (_CONFIG_FN, param, section))
            return None

# Obtain command line option assignments given in the form section:parameter=option

def parse_argv_as_options(_CONFIG):
    for arg in sys.argv[1:]:
        equatands = arg.split('=')
        if len(equatands) == 2:
            sec_param = equatands[0].split(':')
            if len(sec_param) == 2:
                _CONFIG.set(sec_param[0], sec_param[1], equatands[1])
            else:
                sys.stderr.write('Command line argument %s not in form section:parameter=value' % (arg))
        else:
            sys.stderr.write('Command line argument %s not in form section:parameter=value' % (arg))

def summarize_json_response(resp):
    sys.stderr.write('Sent request to %s\n' %(resp.url))
    raise_for_status(resp)
    try:
        results = resp.json()
    except:
        print 'Non json resp is:', resp.text
        return False
    if isinstance(results, unicode) or isinstance(results, str):
        print results
        er = json.loads(results)
        print er
        print json.dumps(er, sort_keys=True, indent=4)
        sys.stderr.write('Getting JavaScript string. Object expected.\n')
        return False
    print json.dumps(results, sort_keys=True, indent=4)
    return True

def summarize_gzipped_json_response(resp):
    sys.stderr.write('Sent request to %s\n' %(resp.url))
    raise_for_status(resp)
    try:
        uncompressed = gzip.GzipFile(mode='rb', fileobj=StringIO(resp.content)).read()
        results = uncompressed
    except:
        raise
    if isinstance(results, unicode) or isinstance(results, str):
        er = json.loads(results)
        print json.dumps(er, sort_keys=True, indent=4)
        return True
    else:
        print 'Non gzipped response, but not a string is:', results
        return False

def get_obj_from_http(url,
                     verb='GET',
                     data=None,
                     headers=None):
    '''Call `url` with the http method of `verb`.
    If specified `data` is passed using json.dumps
    returns the json content of the web service or raise an HTTP error
    '''
    if headers is None:
        headers = {
            'content-type' : 'application/json',
            'accept' : 'application/json',
        }
    if data:
        resp = requests.request(verb,
                                url,
                                headers=headers,
                                data=json.dumps(data),
                                allow_redirects=True)
    else:
        resp = requests.request(verb,
                                url,
                                headers=headers,
                                allow_redirects=True)
    debug('Sent {v} to {s}\n'.format(v=verb, s=resp.url))
    debug('Got status code {c}\n'.format(c=resp.status_code))
    if resp.status_code != 200:
        debug('Full response: {r}\n'.format(r=resp.text))
        raise_for_status(resp)
    return resp.json()

def test_http_json_method(url,
                     verb='GET',
                     data=None,
                     headers=None,
                     expected_status=200,
                     expected_response=None,
                     return_bool_data=False,
                     is_json=True):
    '''Call `url` with the http method of `verb`.
    If specified `data` is passed using json.dumps
    returns True if the response:
         has the expected status code, AND
         has the expected content (if expected_response is not None)
    '''
    fail_return = (False, None) if return_bool_data else False
    if headers is None:
        headers = {
            'content-type' : 'application/json',
            'accept' : 'application/json',
        }
    if data:
        resp = requests.request(verb,
                                url,
                                headers=headers,
                                data=json.dumps(data),
                                allow_redirects=True)
    else:
        resp = requests.request(verb,
                                url,
                                headers=headers,
                                allow_redirects=True)
        debug('Sent {v} to {s}\n'.format(v=verb, s=resp.url))
    debug('Got status code {c} (expecting {e})\n'.format(c=resp.status_code,e=expected_status))
    if resp.status_code != expected_status:
        debug('Did not get expect response status. Got:\n{s}'.format(s=resp.status_code))
        debug('Full response: {r}\n'.format(r=resp.text))
        raise_for_status(resp)
        # this is required for the case when we expect a 4xx/5xx but a successful return code is returned
        return fail_return
    if expected_response is not None:
        if not is_json:
             return (True, resp.text, True) if return_bool_data else True
        try:
            results = resp.json()
            if results != expected_response:
                debug('Did not get expect response content. Got:\n{s}'.format(s=resp.text))
                return fail_return
        except:
            debug('Non json resp is:' + resp.text)
            return fail_return
        if _VERBOSITY_LEVEL > 1:
            debug(unicode(results))
    elif _VERBOSITY_LEVEL > 1:
        debug('Full response: {r}\n'.format(r=resp.text))
    if not is_json:
             return (True, resp.text, True) if return_bool_data else True
    return (True, resp.json(), True) if return_bool_data else True

def raise_for_status(resp):
    try:
        resp.raise_for_status()
    except Exception, e:
        try:
            j = resp.json()
            m = '\n    '.join(['"{k}": {v}'.format(k=k, v=v) for k, v in r.items()])
            sys.stderr.write('resp.json = {t}'.format(t=m))
        except:
            if resp.text:
                sys.stderr.write('resp.text = {t}\n'.format(t=resp.text))
        raise e

def api_is_readonly():
    return config('host', 'allowwrite', 'true') == 'false'

def exit_if_api_is_readonly(fn):
    if not api_is_readonly():
        return
    if _VERBOSITY_LEVEL > 0:
        debug('Running in read-only mode. Skipped {}'.format(fn))
    else:
        sys.stderr.write('s')
    sys.exit(0)<commit_msg>Optionally translate paths the same way apache does (for local testing)<commit_after>#!/usr/bin/env python
from ConfigParser import SafeConfigParser
from cStringIO import StringIO
import requests
import gzip
import json
import sys
import os

_CONFIG = None
_CONFIG_FN = None
if 'VERBOSE_TESTING' in os.environ:
    try:
        _VERBOSITY_LEVEL = int(os.environ['VERBOSE_TESTING'])
    except:
        _VERBOSITY_LEVEL = 1
else:
    _VERBOSITY_LEVEL = 0
def debug(s):
    if _VERBOSITY_LEVEL > 0:
        sys.stderr.write('testing-harness: {s}\n'.format(s=s))

def config(section=None, param=None, default=None):
    '''
    Returns the config object if `section` and `param` are None, or the
        value for the requested parameter.

    If the parameter (or the section) is missing, the exception is logged and
        None is returned.
    '''
    global _CONFIG, _CONFIG_FN
    if _CONFIG is None:
        _CONFIG_FN = os.path.abspath('test.conf')
        _CONFIG = SafeConfigParser()
        _CONFIG.read(_CONFIG_FN)
        parse_argv_as_options(_CONFIG)
    if section is None and param is None:
        return _CONFIG
    try:
        v = _CONFIG.get(section, param)
        return v
    except:
        if default != None:
            return default
        else:
            sys.stderr.write('Config file "%s" does not contain option "%s" in section "%s"\n' % (_CONFIG_FN, param, section))
            return None

# Obtain command line option assignments given in the form section:parameter=option

def parse_argv_as_options(_CONFIG):
    for arg in sys.argv[1:]:
        equatands = arg.split('=')
        if len(equatands) == 2:
            sec_param = equatands[0].split(':')
            if len(sec_param) == 2:
                _CONFIG.set(sec_param[0], sec_param[1], equatands[1])
            else:
                sys.stderr.write('Command line argument %s not in form section:parameter=value' % (arg))
        else:
            sys.stderr.write('Command line argument %s not in form section:parameter=value' % (arg))

def summarize_json_response(resp):
    sys.stderr.write('Sent request to %s\n' %(resp.url))
    raise_for_status(resp)
    try:
        results = resp.json()
    except:
        print 'Non json resp is:', resp.text
        return False
    if isinstance(results, unicode) or isinstance(results, str):
        print results
        er = json.loads(results)
        print er
        print json.dumps(er, sort_keys=True, indent=4)
        sys.stderr.write('Getting JavaScript string. Object expected.\n')
        return False
    print json.dumps(results, sort_keys=True, indent=4)
    return True

def summarize_gzipped_json_response(resp):
    sys.stderr.write('Sent request to %s\n' %(resp.url))
    raise_for_status(resp)
    try:
        uncompressed = gzip.GzipFile(mode='rb', fileobj=StringIO(resp.content)).read()
        results = uncompressed
    except:
        raise
    if isinstance(results, unicode) or isinstance(results, str):
        er = json.loads(results)
        print json.dumps(er, sort_keys=True, indent=4)
        return True
    else:
        print 'Non gzipped response, but not a string is:', results
        return False

def get_obj_from_http(url,
                     verb='GET',
                     data=None,
                     headers=None):
    '''Call `url` with the http method of `verb`.
    If specified `data` is passed using json.dumps
    returns the json content of the web service or raise an HTTP error
    '''
    if headers is None:
        headers = {
            'content-type' : 'application/json',
            'accept' : 'application/json',
        }
    if data:
        resp = requests.request(verb,
                                translate(url),
                                headers=headers,
                                data=json.dumps(data),
                                allow_redirects=True)
    else:
        resp = requests.request(verb,
                                translate(url),
                                headers=headers,
                                allow_redirects=True)
    debug('Sent {v} to {s}\n'.format(v=verb, s=resp.url))
    debug('Got status code {c}\n'.format(c=resp.status_code))
    if resp.status_code != 200:
        debug('Full response: {r}\n'.format(r=resp.text))
        raise_for_status(resp)
    return resp.json()

def test_http_json_method(url,
                     verb='GET',
                     data=None,
                     headers=None,
                     expected_status=200,
                     expected_response=None,
                     return_bool_data=False,
                     is_json=True):
    '''Call `url` with the http method of `verb`.
    If specified `data` is passed using json.dumps
    returns True if the response:
         has the expected status code, AND
         has the expected content (if expected_response is not None)
    '''
    fail_return = (False, None) if return_bool_data else False
    if headers is None:
        headers = {
            'content-type' : 'application/json',
            'accept' : 'application/json',
        }
    if data:
        resp = requests.request(verb,
                                translate(url),
                                headers=headers,
                                data=json.dumps(data),
                                allow_redirects=True)
    else:
        resp = requests.request(verb,
                                translate(url),
                                headers=headers,
                                allow_redirects=True)
        debug('Sent {v} to {s}\n'.format(v=verb, s=resp.url))
    debug('Got status code {c} (expecting {e})\n'.format(c=resp.status_code,e=expected_status))
    if resp.status_code != expected_status:
        debug('Did not get expect response status. Got:\n{s}'.format(s=resp.status_code))
        debug('Full response: {r}\n'.format(r=resp.text))
        raise_for_status(resp)
        # this is required for the case when we expect a 4xx/5xx but a successful return code is returned
        return fail_return
    if expected_response is not None:
        if not is_json:
             return (True, resp.text, True) if return_bool_data else True
        try:
            results = resp.json()
            if results != expected_response:
                debug('Did not get expect response content. Got:\n{s}'.format(s=resp.text))
                return fail_return
        except:
            debug('Non json resp is:' + resp.text)
            return fail_return
        if _VERBOSITY_LEVEL > 1:
            debug(unicode(results))
    elif _VERBOSITY_LEVEL > 1:
        debug('Full response: {r}\n'.format(r=resp.text))
    if not is_json:
             return (True, resp.text, True) if return_bool_data else True
    return (True, resp.json(), True) if return_bool_data else True

def raise_for_status(resp):
    try:
        resp.raise_for_status()
    except Exception, e:
        try:
            j = resp.json()
            m = '\n    '.join(['"{k}": {v}'.format(k=k, v=v) for k, v in r.items()])
            sys.stderr.write('resp.json = {t}'.format(t=m))
        except:
            if resp.text:
                sys.stderr.write('resp.text = {t}\n'.format(t=resp.text))
        raise e

def api_is_readonly():
    return config('host', 'allowwrite', 'true') == 'false'

def exit_if_api_is_readonly(fn):
    if not api_is_readonly():
        return
    if _VERBOSITY_LEVEL > 0:
        debug('Running in read-only mode. Skipped {}'.format(fn))
    else:
        sys.stderr.write('s')
    sys.exit(0)

# Mimic the behavior of apache so that services can be tested without
# having apache running.  See opentree/deploy/setup/opentree-shared.conf

translations = [('/v2/study/', '/phylesystem/v1/study/'),
                ('/cached/', '/phylesystem/default/cached/'),
                # treemachine
                ('/v2/tree_of_life/', '/db/data/ext/tree_of_life/graphdb/'),
                ('/v2/graph/', '/db/data/ext/graph/graphdb/'),
                # taxomachine
                ('/v2/tnrs/', '/db/data/ext/tnrs_v2/graphdb/'),
                ('/v2/taxonomy/', '/db/data/ext/taxonomy/graphdb/'),
                # oti
                ('/v2/studies/', '/db/data/ext/studies/graphdb/'),
]

def translate(s):
    if config('host', 'translate', 'false') == 'true':
        for (src, dst) in translations:
            if s.startswith(src):
                return dst + s[len(src):]
    return s
<|endoftext|>


<commit_before>import re
from os import walk
from os.path import exists as path_exists
from functools import partial
from collections import namedtuple

from django.template.loader import find_template_loader
from django.template.loaders.app_directories import app_template_dirs
from django.conf import settings

def get_template_loaders_dirs():
    fs_loader_module = 'django.template.loaders.filesystem.Loader'
    app_loader_module = 'django.template.loaders.app_directories.Loader'
    loader_configs = settings.TEMPLATE_LOADERS
    dirs = ()
    loaders = []
    if fs_loader_module in loader_configs:
        dirs += settings.TEMPLATE_DIRS
        loaders.append(find_template_loader(fs_loader_module))
    if app_loader_module in loader_configs:
        dirs += app_template_dirs
        loaders.append(find_template_loader(app_loader_module))
    return loaders, dirs

TLOADERS, TDIRS = get_template_loaders_dirs()

FILENAME_RE = re.compile("""['\\"](?P<fname>[^'"]+)""")
VARIABLE_RE = re.compile("""\{\%\s*\w+\s+(?P<vname>[^\s\%]+)""")

EXTEND_RE = re.compile('\{\%\s*extends.+\%\}')
INCLUDE_RE = re.compile('\{\%\s*include.+\%\}')

INCLUDE_TAG = 'include'
EXTEND_TAG = 'extends'

PATTERNS = {
    INCLUDE_TAG: INCLUDE_RE,
    EXTEND_TAG: EXTEND_RE,
}

TemplateLine = namedtuple('TemplateLine',
    'source target line_number tag_type path')

def filter_line(patterns, line):
    for tag, pattern in patterns.items():
        if bool(pattern.search(line)):
            return tag, line
    return None, None

def path_walker(path):
    for base, _, filenames in walk(path):
        for filename in filenames:
            yield '/'.join((base, filename))

def line_reader(filename):
    try:
        with open(filename) as f:
            for line in f:
                yield line.strip()
    except (IOError, UnicodeDecodeError):
        yield ''

def filter_lines_in_path_by_patterns(path, patterns):
    filter_line_by_patterns = partial(filter_line, patterns)
    for filename in path_walker(path):
        for line_number, line in enumerate(line_reader(filename)):
            tag, line = filter_line_by_patterns(line)
            if line is not None:
                yield filename, line_number, tag, line

def find_targets(line):
    """
    Finds the full filename for a reference to a template on the line if it
    references a string. Otherwise, returns the variable name the tag refers
    to.
    """
    target_search = FILENAME_RE.search(line)
    if target_search is None:
        target_search = VARIABLE_RE.search(line)
        try:
            target_value = target_search.groups()[0]
        except IndexError:
            return None
        return 'variable:' + target_value
    else:
        try:
            target_value = target_search.groups()[0]
        except IndexError:
            return None
        for loader in TLOADERS:
            fns = loader.get_template_sources(target_value)
            for fn in fns:
                if path_exists(fn):
                    return fn

def stream_template_assocs(template_dirs, patterns):
    for path in template_dirs:
        filtered_lines = filter_lines_in_path_by_patterns(path, patterns)
        for source, line_number, tag_type, line in filtered_lines:
            target = find_targets(line)
            if target is not None:
                yield TemplateLine(source=source, target=target,
                    tag_type=tag_type, line_number=line_number, path=path)

"""
Top level generator that other modules should import and use directly
"""
get_template_line_stream = partial(
    stream_template_assocs, TDIRS, PATTERNS)

if __name__ == '__main__':
    for data in get_template_line_stream():
        print(data)
<commit_msg>Fixes #30 - Flattens TEMPLATE_LOADERS to handle CachedLoader<commit_after>import re
from os import walk
from os.path import exists as path_exists
from functools import partial
from collections import namedtuple

from django.template.loader import find_template_loader
from django.template.loaders.app_directories import app_template_dirs
from django.conf import settings

from collections import Iterable

try:
    from django.utils.six import string_types
except ImportError:
    # Django < 1.5. No Python 3 support
    string_types = basestring

def flatten(iterable):
    """ Given an iterable with nested iterables, generate a flat iterable """
    for i in iterable:
        if isinstance(i, Iterable) and not isinstance(i, string_types):
            for sub_i in flatten(i):
                yield sub_i
        else:
            yield i

def get_template_loaders_dirs():
    fs_loader_module = 'django.template.loaders.filesystem.Loader'
    app_loader_module = 'django.template.loaders.app_directories.Loader'
    loader_configs = set(flatten(settings.TEMPLATE_LOADERS))
    dirs = ()
    loaders = []
    if fs_loader_module in loader_configs:
        dirs += settings.TEMPLATE_DIRS
        loaders.append(find_template_loader(fs_loader_module))
    if app_loader_module in loader_configs:
        dirs += app_template_dirs
        loaders.append(find_template_loader(app_loader_module))
    return loaders, dirs

TLOADERS, TDIRS = get_template_loaders_dirs()

FILENAME_RE = re.compile("""['\\"](?P<fname>[^'"]+)""")
VARIABLE_RE = re.compile("""\{\%\s*\w+\s+(?P<vname>[^\s\%]+)""")

EXTEND_RE = re.compile('\{\%\s*extends.+\%\}')
INCLUDE_RE = re.compile('\{\%\s*include.+\%\}')

INCLUDE_TAG = 'include'
EXTEND_TAG = 'extends'

PATTERNS = {
    INCLUDE_TAG: INCLUDE_RE,
    EXTEND_TAG: EXTEND_RE,
}

TemplateLine = namedtuple('TemplateLine',
    'source target line_number tag_type path')

def filter_line(patterns, line):
    for tag, pattern in patterns.items():
        if bool(pattern.search(line)):
            return tag, line
    return None, None

def path_walker(path):
    for base, _, filenames in walk(path):
        for filename in filenames:
            yield '/'.join((base, filename))

def line_reader(filename):
    try:
        with open(filename) as f:
            for line in f:
                yield line.strip()
    except (IOError, UnicodeDecodeError):
        yield ''

def filter_lines_in_path_by_patterns(path, patterns):
    filter_line_by_patterns = partial(filter_line, patterns)
    for filename in path_walker(path):
        for line_number, line in enumerate(line_reader(filename)):
            tag, line = filter_line_by_patterns(line)
            if line is not None:
                yield filename, line_number, tag, line

def find_targets(line):
    """
    Finds the full filename for a reference to a template on the line if it
    references a string. Otherwise, returns the variable name the tag refers
    to.
    """
    target_search = FILENAME_RE.search(line)
    if target_search is None:
        target_search = VARIABLE_RE.search(line)
        try:
            target_value = target_search.groups()[0]
        except IndexError:
            return None
        return 'variable:' + target_value
    else:
        try:
            target_value = target_search.groups()[0]
        except IndexError:
            return None
        for loader in TLOADERS:
            fns = loader.get_template_sources(target_value)
            for fn in fns:
                if path_exists(fn):
                    return fn

def stream_template_assocs(template_dirs, patterns):
    for path in template_dirs:
        filtered_lines = filter_lines_in_path_by_patterns(path, patterns)
        for source, line_number, tag_type, line in filtered_lines:
            target = find_targets(line)
            if target is not None:
                yield TemplateLine(source=source, target=target,
                    tag_type=tag_type, line_number=line_number, path=path)

"""
Top level generator that other modules should import and use directly
"""
get_template_line_stream = partial(
    stream_template_assocs, TDIRS, PATTERNS)

if __name__ == '__main__':
    for data in get_template_line_stream():
        print(data)
<|endoftext|>


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

import sys

from canaryd.subprocess import CalledProcessError, get_command_output

SMART_RETURN_BITS = {
    0: False,  # command line parse error
    1: 'device open failed',
    2: 'SMART command failed',
    3: 'disk failing',
    # We track thresholds via stdout
    4: True,  # pre-fail attrs <= thresh
    5: True,  # pre-fail attrs <= thresh in past
    # We track errors via stdout
    6: True,  # error log
    7: True,  # self-test errors
}

try:
    # Ensure smartctl is present & working
    get_command_output(
        'smartctl --version',
    )

except (CalledProcessError, OSError):
    print('No smartctl found!')
    sys.exit(1)

try:
    # Look for OSX disks first (OSX has /dev/sdt which breaks below)
    disks_data = get_command_output(
        'ls /dev/disk?',
        shell=True,
    )

except (CalledProcessError, OSError):
    # Look for Linux disks (/dev/sdX, /dev/hdX)
    disks_data = get_command_output(
        'ls /dev/[hs]d?',
        shell=True,
    )

# Turn into a list of disks
disks = disks_data.strip().split()

warnings = []
criticals = []

for disk in disks:
    try:
        # List health and attributes
        smart_data = get_command_output(
            'smartctl -a {0}'.format(disk),
        )

    except CalledProcessError as e:
        smart_data = e.output

        for i in SMART_RETURN_BITS:
            bit = e.returncode & i
            if bit and i in SMART_RETURN_BITS:
                return_bit = SMART_RETURN_BITS[i]

                if return_bit is False:
                    raise

                if return_bit is True:
                    continue

                criticals.append('Disk {0} error: {1}'.format(disk, return_bit))
                break

    for line in smart_data.splitlines():
        # First check SMART's overall status
        if line.startswith('SMART overall-health self-assessment test result'):
            bits = line.split()
            test_result = bits[-1]

            if test_result != 'PASSED':
                criticals.append((
                    'Disk {0} has failed test, status: {1}'
                ).format(disk, test_result))
                continue

        # Ignore any non-attribute line
        if all(type_ not in line for type_ in ('Pre-fail', 'Old_age')):
            continue

        bits = line.split()
        name = bits[1]
        value = int(bits[3])
        threshold = int(bits[5])

        # Skip where both the value and threshold are 0, like power_on_hours/etc
        if value == 0 and threshold == 0:
            continue

        # From: https://superuser.com/a/354254
        # Each Attribute also has a Threshold value (whose range is 0 to 255)
        # which is printed under the heading "THRESH". If the Normalized value
        # is less than or equal to the Threshold value, then the Attribute is
        # said to have failed. If the Attribute is a pre-failure Attribute,
        # then disk failure is imminent.
        if value == threshold:
            warnings.append((
                'Attrbute {0} on disk {1} is at the threshold ({2})'
            ).format(name, disk, threshold))

        elif value < threshold:
            criticals.append((
                'Attribute {0} on disk {1} is below the threshold ({2}/{3})'
            ).format(name, disk, value, threshold))

exit_code = 0

if warnings:
    exit_code = 1
    print('\n'.join(warnings))

if criticals:
    exit_code = 2
    print('\n'.join(criticals))

if exit_code == 0:
    print('All disks healthy')

sys.exit(exit_code)
<commit_msg>Ignore disks that don't support SMART in `disk_health.py` script.<commit_after>#!/usr/bin/env python

import sys

from canaryd.subprocess import CalledProcessError, get_command_output

SMART_RETURN_BITS = {
    0: False,  # command line parse error
    1: 'device open failed',
    2: 'SMART command failed',
    3: 'disk failing',
    # We track thresholds via stdout
    4: True,  # pre-fail attrs <= thresh
    5: True,  # pre-fail attrs <= thresh in past
    # We track errors via stdout
    6: True,  # error log
    7: True,  # self-test errors
}

try:
    # Ensure smartctl is present & working
    get_command_output(
        'smartctl --version',
    )

except (CalledProcessError, OSError):
    print('No smartctl found!')
    sys.exit(1)

try:
    # Look for OSX disks first (OSX has /dev/sdt which breaks below)
    disks_data = get_command_output(
        'ls /dev/disk?',
        shell=True,
    )

except (CalledProcessError, OSError):
    # Look for Linux disks (/dev/sdX, /dev/hdX)
    disks_data = get_command_output(
        'ls /dev/[hs]d?',
        shell=True,
    )

# Turn into a list of disks
disks = disks_data.strip().split()

warnings = []
criticals = []

for disk in disks:
    try:
        # List health and attributes
        smart_data = get_command_output(
            'smartctl -a {0}'.format(disk),
        )

    except CalledProcessError as e:
        smart_data = e.output

        # If the device doesn't support SMART not much we can do about it!
        if 'Operation not supported by device' in smart_data:
            continue

        for i in SMART_RETURN_BITS:
            bit = e.returncode & i
            if bit and i in SMART_RETURN_BITS:
                return_bit = SMART_RETURN_BITS[i]

                if return_bit is False:
                    raise

                if return_bit is True:
                    continue

                criticals.append('Disk {0} error: {1}'.format(disk, return_bit))
                break

    for line in smart_data.splitlines():
        # First check SMART's overall status
        if line.startswith('SMART overall-health self-assessment test result'):
            bits = line.split()
            test_result = bits[-1]

            if test_result != 'PASSED':
                criticals.append((
                    'Disk {0} has failed test, status: {1}'
                ).format(disk, test_result))
                continue

        # Ignore any non-attribute line
        if all(type_ not in line for type_ in ('Pre-fail', 'Old_age')):
            continue

        bits = line.split()
        name = bits[1]
        value = int(bits[3])
        threshold = int(bits[5])

        # Skip where both the value and threshold are 0, like power_on_hours/etc
        if value == 0 and threshold == 0:
            continue

        # From: https://superuser.com/a/354254
        # Each Attribute also has a Threshold value (whose range is 0 to 255)
        # which is printed under the heading "THRESH". If the Normalized value
        # is less than or equal to the Threshold value, then the Attribute is
        # said to have failed. If the Attribute is a pre-failure Attribute,
        # then disk failure is imminent.
        if value == threshold:
            warnings.append((
                'Attrbute {0} on disk {1} is at the threshold ({2})'
            ).format(name, disk, threshold))

        elif value < threshold:
            criticals.append((
                'Attribute {0} on disk {1} is below the threshold ({2}/{3})'
            ).format(name, disk, value, threshold))

exit_code = 0

if warnings:
    exit_code = 1
    print('\n'.join(warnings))

if criticals:
    exit_code = 2
    print('\n'.join(criticals))

if exit_code == 0:
    print('All disks healthy')

sys.exit(exit_code)
<|endoftext|>


<commit_before>#  Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
#  See https://llvm.org/LICENSE.txt for license information.
#  SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

try:
  import mlir.ir as ir
  import mlir.dialects.pdl as pdl
  from typing import Optional, Sequence, Union
except ImportError as e:
  raise RuntimeError("Error loading imports from extension module") from e

BoolArg = Optional[Union[bool, ir.BoolAttr]]
IntListArg = Optional[Union[Sequence[int], ir.ArrayAttr]]
StringArg = Optional[Union[str, ir.StringAttr]]

def _defaulted_ensure(f):

  def inner(value, default=None):
    assert value is not None or default is not None
    return f(default if value is None else value)

  return inner

@_defaulted_ensure
def _ensure_array_attr(value: IntListArg):
  i64 = ir.IntegerType.get_signless(64)
  if isinstance(value, Sequence):
    return ir.ArrayAttr.get([ir.IntegerAttr.get(i64, i) for i in value])
  return value

@_defaulted_ensure
def _ensure_bool_attr(value: BoolArg):
  if isinstance(value, bool):
    return ir.BoolAttr.get(value)
  return value

@_defaulted_ensure
def _ensure_string_attr(value: StringArg):
  if isinstance(value, str):
    return ir.StringAttr.get(value)
  return value

class LowerVectorsOp:
  """Specialization for the LowerVectorsOp class."""

  def __init__(self,
               *,
               stages: IntListArg = None,
               contraction_lowering: StringArg = None,
               multireduction_lowering: StringArg = None,
               transpose_lowering: StringArg = None,
               transpose_avx2_lowering: BoolArg = None,
               loc=None,
               ip=None):
    stages = _ensure_array_attr(stages, [0, 1, 2, 3, 4, 5, 6])
    contraction_lowering = _ensure_string_attr(contraction_lowering,
                                               "outerproduct")
    multireduction_lowering = _ensure_string_attr(multireduction_lowering,
                                                  "innerparallel")
    transpose_lowering = _ensure_string_attr(transpose_lowering, "eltwise")
    transpose_avx2_lowering = _ensure_bool_attr(transpose_avx2_lowering, False)

    super().__init__(stages,
                     contraction_lowering,
                     multireduction_lowering,
                     transpose_lowering,
                     transpose_avx2_lowering,
                     loc=loc,
                     ip=ip)

class TileOp:
  """Specialization for the TileOp class."""

  def __init__(self,
               target: Union[ir.Value, ir.Operation, ir.OpView, str,
                             ir.FlatSymbolRefAttr],
               *,
               sizes: IntListArg = None,
               interchange: IntListArg = None,
               pad: BoolArg = None,
               pack_paddings: IntListArg = None,
               hoist_paddings: IntListArg = None,
               scalarize_dyn_dims: BoolArg = None,
               generalize: BoolArg = None,
               loc=None,
               ip=None):
    sizes = _ensure_array_attr(sizes, [])
    interchange = _ensure_array_attr(interchange, [])
    pad = _ensure_bool_attr(pad, False)
    pack_paddings = _ensure_array_attr(pack_paddings, [])
    hoist_paddings = _ensure_array_attr(hoist_paddings, [])
    scalarize_dyn_dims = _ensure_bool_attr(scalarize_dyn_dims, False)
    generalize = _ensure_bool_attr(generalize, False)
    operation_type = pdl.OperationType.get()

    if isinstance(target, str):
      target = ir.FlatSymbolRefAttr.get(target)

    super().__init__(
        operation_type,
        target if not isinstance(target, ir.FlatSymbolRefAttr) else None,
        target if isinstance(target, ir.FlatSymbolRefAttr) else None,
        sizes,
        interchange,
        pad,
        pack_paddings,
        hoist_paddings,
        scalarize_dyn_dims,
        generalize,
        loc=loc,
        ip=ip)

class VectorizeOp:

  def __init__(self,
               target: Union[ir.Value, ir.Operation, ir.OpView, str,
                             ir.FlatSymbolRefAttr],
               *,
               vectorize_padding: BoolArg,
               loc=None,
               ip=None):
    if isinstance(target, str):
      target = ir.FlatSymbolRefAttr(target)

    operation_type = pdl.OperationType.get()

    super().__init__(
        operation_type,
        target if not isinstance(target, ir.FlatSymbolRefAttr) else None,
        target if isinstance(target, ir.FlatSymbolRefAttr) else None,
        _ensure_bool_attr(vectorize_padding, False),
        loc=loc,
        ip=ip)

class SequenceOp:

  def __init__(self, *, loc=None, ip=None):
    super().__init__(loc=loc, ip=ip)
    self.body.blocks.append()
<commit_msg>Fix LinalgTransform Python bindings attribute construction (#250)<commit_after>#  Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
#  See https://llvm.org/LICENSE.txt for license information.
#  SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

try:
  import mlir.ir as ir
  import mlir.dialects.pdl as pdl
  from typing import Optional, Sequence, Union
except ImportError as e:
  raise RuntimeError("Error loading imports from extension module") from e

BoolArg = Optional[Union[bool, ir.BoolAttr]]
IntListArg = Optional[Union[Sequence[int], ir.ArrayAttr]]
StringArg = Optional[Union[str, ir.StringAttr]]

def _defaulted_ensure(f):

  def inner(value, default=None):
    assert value is not None or default is not None
    return f(default if value is None else value)

  return inner

@_defaulted_ensure
def _ensure_array_attr(value: IntListArg):
  i64 = ir.IntegerType.get_signless(64)
  if isinstance(value, Sequence):
    return ir.ArrayAttr.get([ir.IntegerAttr.get(i64, i) for i in value])
  return value

@_defaulted_ensure
def _ensure_bool_attr(value: BoolArg):
  if isinstance(value, bool):
    return ir.BoolAttr.get(value)
  return value

@_defaulted_ensure
def _ensure_string_attr(value: StringArg):
  if isinstance(value, str):
    return ir.StringAttr.get(value)
  return value

class LowerVectorsOp:
  """Specialization for the LowerVectorsOp class."""

  def __init__(self,
               *,
               stages: IntListArg = None,
               contraction_lowering: StringArg = None,
               multireduction_lowering: StringArg = None,
               transpose_lowering: StringArg = None,
               transpose_avx2_lowering: BoolArg = None,
               loc=None,
               ip=None):
    stages = _ensure_array_attr(stages, [0, 1, 2, 3, 4, 5, 6])
    contraction_lowering = _ensure_string_attr(contraction_lowering,
                                               "outerproduct")
    multireduction_lowering = _ensure_string_attr(multireduction_lowering,
                                                  "innerparallel")
    transpose_lowering = _ensure_string_attr(transpose_lowering, "eltwise")
    transpose_avx2_lowering = _ensure_bool_attr(transpose_avx2_lowering, False)

    super().__init__(stages,
                     contraction_lowering,
                     multireduction_lowering,
                     transpose_lowering,
                     transpose_avx2_lowering,
                     loc=loc,
                     ip=ip)

class TileOp:
  """Specialization for the TileOp class."""

  def __init__(self,
               target: Union[ir.Value, ir.Operation, ir.OpView, str,
                             ir.FlatSymbolRefAttr],
               *,
               sizes: IntListArg = None,
               interchange: IntListArg = None,
               pad: BoolArg = None,
               pack_paddings: IntListArg = None,
               hoist_paddings: IntListArg = None,
               scalarize_dyn_dims: BoolArg = None,
               generalize: BoolArg = None,
               loc=None,
               ip=None):
    sizes = _ensure_array_attr(sizes, [])
    interchange = _ensure_array_attr(interchange, [])
    pad = _ensure_bool_attr(pad, False)
    pack_paddings = _ensure_array_attr(pack_paddings, [])
    hoist_paddings = _ensure_array_attr(hoist_paddings, [])
    scalarize_dyn_dims = _ensure_bool_attr(scalarize_dyn_dims, False)
    generalize = _ensure_bool_attr(generalize, False)
    operation_type = pdl.OperationType.get()

    if isinstance(target, str):
      target = ir.FlatSymbolRefAttr.get(target)

    super().__init__(
        operation_type,
        target if not isinstance(target, ir.FlatSymbolRefAttr) else None,
        target if isinstance(target, ir.FlatSymbolRefAttr) else None,
        sizes,
        interchange,
        pad,
        pack_paddings,
        hoist_paddings,
        scalarize_dyn_dims,
        generalize,
        loc=loc,
        ip=ip)

class VectorizeOp:

  def __init__(self,
               target: Union[ir.Value, ir.Operation, ir.OpView, str,
                             ir.FlatSymbolRefAttr],
               *,
               vectorize_padding: BoolArg,
               loc=None,
               ip=None):
    if isinstance(target, str):
      target = ir.FlatSymbolRefAttr.get(target)

    operation_type = pdl.OperationType.get()

    super().__init__(
        operation_type,
        target if not isinstance(target, ir.FlatSymbolRefAttr) else None,
        target if isinstance(target, ir.FlatSymbolRefAttr) else None,
        _ensure_bool_attr(vectorize_padding, False),
        loc=loc,
        ip=ip)

class SequenceOp:

  def __init__(self, *, loc=None, ip=None):
    super().__init__(loc=loc, ip=ip)
    self.body.blocks.append()
<|endoftext|>


<commit_before>#import pytz
import six
import sqlalchemy as sa
from wtforms import Form
from wtforms.validators import InputRequired, DataRequired
from wtforms.form import FormMeta
from wtforms_components import (
    DateRange,
    NumberRangeField,
    PhoneNumberField,
    SelectField,
    SelectMultipleField,
    Unique,
)
from .utils import (
    is_date_column,
    is_integer_column,
    is_scalar,
    null_or_int,
    null_or_unicode,
    ClassMap
)
from .exc import (
    AttributeTypeException,
    InvalidAttributeException,
    UnknownTypeException
)
from .fields import ModelFieldList, ModelFormField
from .generator import FormGenerator

__all__ = (
    AttributeTypeException,
    DateRange,
    InvalidAttributeException,
    ModelFieldList,
    ModelFormField,
    NumberRangeField,
    PhoneNumberField,
    SelectField,
    SelectMultipleField,
    Unique,
    UnknownTypeException,
    is_date_column,
    is_integer_column,
    is_scalar,
    null_or_int,
    null_or_unicode,
)

__version__ = '0.9.3'

class ModelFormMeta(FormMeta):
    """Meta class that overrides WTForms base meta class. The primary purpose
    of this class is allowing ModelForms use special configuration params under
    the 'Meta' class namespace.

    ModelForm classes inherit parent's Meta class properties.
    """
    def __init__(cls, *args, **kwargs):
        bases = []
        for class_ in cls.__mro__:
            if 'Meta' in class_.__dict__:
                bases.append(getattr(class_, 'Meta'))

        cls.Meta = type('Meta', tuple(bases), {})

        FormMeta.__init__(cls, *args, **kwargs)

        if hasattr(cls.Meta, 'model') and cls.Meta.model:
            generator = cls.Meta.form_generator(cls)
            generator.create_form(cls)

def model_form_factory(base=Form, meta=ModelFormMeta, **defaults):
    """Creates new model form, with given base class."""

    class ModelForm(six.with_metaclass(meta, base)):
        """
        A function that returns SQLAlchemy session. This should be
        assigned if you wish to use Unique validator. If you are using
        Flask-SQLAlchemy along with WTForms-Alchemy you don't need to
        set this.
        """
        get_session = None

        class Meta:
            model = None

            default = None

            #: Whether or not to skip unknown types. If this is set to True,
            #: fields with types that are not present in FormGenerator type map
            #: will be silently excluded from the generated form.
            #:
            #: By default this is set to False, meaning unknown types throw
            #: exceptions when encountered.
            skip_unknown_types = defaults.get('skip_unknown_types', False)

            #: Whether or not to assign all fields as optional, useful when
            #: creating update forms for patch requests
            all_fields_optional = defaults.get('all_fields_optional', False)

            validators = defaults.get('validators', {})

            #: A dict with keys as field names and values as field arguments.
            field_args = defaults.get('field_args', {})

            #: A dict with keys as field names and values as widget options.
            widget_options = defaults.get('widget_options', {})

            #: Whether or not to include only indexed fields.
            only_indexed_fields = defaults.get('only_indexed_fields', False)

            #: Whether or not to include primary keys.
            include_primary_keys = defaults.get('include_primary_keys', False)

            #: Whether or not to include foreign keys. By default this is False
            #: indicating that foreign keys are not included in the generated
            #: form.
            include_foreign_keys = defaults.get('include_foreign_keys', False)

            #: Whether or not to strip string fields
            strip_string_fields = defaults.get('strip_string_fields', False)

            #: Whether or not to include datetime columns that have a default
            #: value. A good example is created_at column which has a default
            #: value of datetime.utcnow.
            include_datetimes_with_default = defaults.get(
                'include_datetimes_with_default', False
            )

            #: The default validator to be used for not nullable columns. Set
            #: this to `None` if you wish to disable it.
            not_null_validator = defaults.get(
                'not_null_validator',
                InputRequired()
            )

            #: A dictionary that overrides not null validation on type level.
            #: Keys should be valid SQLAlchemy types and values should be valid
            #: WTForms validators.
            not_null_validator_type_map = defaults.get(
                'not_null_validator_type_map',
                ClassMap(
                    [(sa.String, [InputRequired(), DataRequired()])]
                )
            )

            #: Which form generator to use. Only override this if you have a
            #: valid form generator which you want to use instead of the
            #: default one.
            form_generator = defaults.get(
                'form_generator', FormGenerator
            )

            #: Default date format
            date_format = defaults.get('date_format', '%Y-%m-%d')

            #: Default datetime format
            datetime_format = defaults.get(
                'datetime_format', '%Y-%m-%d %H:%M:%S'
            )

            #: Dictionary of SQLAlchemy types as keys and WTForms field classes
            #: as values. The key value pairs of this dictionary override
            #: the key value pairs of FormGenerator.TYPE_MAP.
            #:
            #: Using this configuration option one can easily configure the
            #: type conversion in class level.
            type_map = defaults.get('type_map', ClassMap())

            #: Additional fields to include in the generated form.
            include = defaults.get('include', [])

            #: List of fields to exclude from the generated form.
            exclude = []

            #: List of fields to only include in the generated form.
            only = []

        def __init__(self, *args, **kwargs):
            """Sets object as form attribute."""

            self._obj = kwargs.get('obj', None)
            super(ModelForm, self).__init__(*args, **kwargs)

    return ModelForm

ModelForm = model_form_factory(Form)

class ModelCreateForm(ModelForm):
    pass

class ModelUpdateForm(ModelForm):
    class Meta:
        all_fields_optional = True
        assign_required = False

class ModelSearchForm(ModelForm):
    class Meta:
        all_fields_optional = True
        only_indexed_fields = True
        include_primary_keys = True
<commit_msg>Get -> pop<commit_after>#import pytz
import six
import sqlalchemy as sa
from wtforms import Form
from wtforms.validators import InputRequired, DataRequired
from wtforms.form import FormMeta
from wtforms_components import (
    DateRange,
    NumberRangeField,
    PhoneNumberField,
    SelectField,
    SelectMultipleField,
    Unique,
)
from .utils import (
    is_date_column,
    is_integer_column,
    is_scalar,
    null_or_int,
    null_or_unicode,
    ClassMap
)
from .exc import (
    AttributeTypeException,
    InvalidAttributeException,
    UnknownTypeException
)
from .fields import ModelFieldList, ModelFormField
from .generator import FormGenerator

__all__ = (
    AttributeTypeException,
    DateRange,
    InvalidAttributeException,
    ModelFieldList,
    ModelFormField,
    NumberRangeField,
    PhoneNumberField,
    SelectField,
    SelectMultipleField,
    Unique,
    UnknownTypeException,
    is_date_column,
    is_integer_column,
    is_scalar,
    null_or_int,
    null_or_unicode,
)

__version__ = '0.9.3'

class ModelFormMeta(FormMeta):
    """Meta class that overrides WTForms base meta class. The primary purpose
    of this class is allowing ModelForms use special configuration params under
    the 'Meta' class namespace.

    ModelForm classes inherit parent's Meta class properties.
    """
    def __init__(cls, *args, **kwargs):
        bases = []
        for class_ in cls.__mro__:
            if 'Meta' in class_.__dict__:
                bases.append(getattr(class_, 'Meta'))

        cls.Meta = type('Meta', tuple(bases), {})

        FormMeta.__init__(cls, *args, **kwargs)

        if hasattr(cls.Meta, 'model') and cls.Meta.model:
            generator = cls.Meta.form_generator(cls)
            generator.create_form(cls)

def model_form_factory(base=Form, meta=ModelFormMeta, **defaults):
    """Creates new model form, with given base class."""

    class ModelForm(six.with_metaclass(meta, base)):
        """
        A function that returns SQLAlchemy session. This should be
        assigned if you wish to use Unique validator. If you are using
        Flask-SQLAlchemy along with WTForms-Alchemy you don't need to
        set this.
        """
        get_session = None

        class Meta:
            model = None

            default = None

            #: Whether or not to skip unknown types. If this is set to True,
            #: fields with types that are not present in FormGenerator type map
            #: will be silently excluded from the generated form.
            #:
            #: By default this is set to False, meaning unknown types throw
            #: exceptions when encountered.
            skip_unknown_types = defaults.pop('skip_unknown_types', False)

            #: Whether or not to assign all fields as optional, useful when
            #: creating update forms for patch requests
            all_fields_optional = defaults.pop('all_fields_optional', False)

            validators = defaults.pop('validators', {})

            #: A dict with keys as field names and values as field arguments.
            field_args = defaults.pop('field_args', {})

            #: A dict with keys as field names and values as widget options.
            widget_options = defaults.pop('widget_options', {})

            #: Whether or not to include only indexed fields.
            only_indexed_fields = defaults.pop('only_indexed_fields', False)

            #: Whether or not to include primary keys.
            include_primary_keys = defaults.pop('include_primary_keys', False)

            #: Whether or not to include foreign keys. By default this is False
            #: indicating that foreign keys are not included in the generated
            #: form.
            include_foreign_keys = defaults.pop('include_foreign_keys', False)

            #: Whether or not to strip string fields
            strip_string_fields = defaults.pop('strip_string_fields', False)

            #: Whether or not to include datetime columns that have a default
            #: value. A good example is created_at column which has a default
            #: value of datetime.utcnow.
            include_datetimes_with_default = defaults.pop(
                'include_datetimes_with_default', False
            )

            #: The default validator to be used for not nullable columns. Set
            #: this to `None` if you wish to disable it.
            not_null_validator = defaults.pop(
                'not_null_validator',
                InputRequired()
            )

            #: A dictionary that overrides not null validation on type level.
            #: Keys should be valid SQLAlchemy types and values should be valid
            #: WTForms validators.
            not_null_validator_type_map = defaults.pop(
                'not_null_validator_type_map',
                ClassMap(
                    [(sa.String, [InputRequired(), DataRequired()])]
                )
            )

            #: Which form generator to use. Only override this if you have a
            #: valid form generator which you want to use instead of the
            #: default one.
            form_generator = defaults.pop(
                'form_generator', FormGenerator
            )

            #: Default date format
            date_format = defaults.pop('date_format', '%Y-%m-%d')

            #: Default datetime format
            datetime_format = defaults.pop(
                'datetime_format', '%Y-%m-%d %H:%M:%S'
            )

            #: Dictionary of SQLAlchemy types as keys and WTForms field classes
            #: as values. The key value pairs of this dictionary override
            #: the key value pairs of FormGenerator.TYPE_MAP.
            #:
            #: Using this configuration option one can easily configure the
            #: type conversion in class level.
            type_map = defaults.pop('type_map', ClassMap())

            #: Additional fields to include in the generated form.
            include = defaults.pop('include', [])

            #: List of fields to exclude from the generated form.
            exclude = defaults.pop('exclude', [])

            #: List of fields to only include in the generated form.
            only = defaults.pop('only', [])

        def __init__(self, *args, **kwargs):
            """Sets object as form attribute."""

            self._obj = kwargs.get('obj', None)
            super(ModelForm, self).__init__(*args, **kwargs)

    return ModelForm

ModelForm = model_form_factory(Form)

class ModelCreateForm(ModelForm):
    pass

class ModelUpdateForm(ModelForm):
    class Meta:
        all_fields_optional = True
        assign_required = False

class ModelSearchForm(ModelForm):
    class Meta:
        all_fields_optional = True
        only_indexed_fields = True
        include_primary_keys = True
<|endoftext|>


<commit_before>#!/usr/bin/env python
# This app sends messages to dbus signalling visibility of the onboard on-screen keyboard.

import os
import rospy
from std_msgs.msg import Bool

def callback(data):
  # TODO: use python dbus bindings instead of dbus cli
  if data.data:
    os.system('dbus-send --type=method_call --dest=org.onboard.Onboard /org/onboard/Onboard/Keyboard org.onboard.Onboard.Keyboard.Show')
  else:
    os.system('dbus-send --type=method_call --dest=org.onboard.Onboard /org/onboard/Onboard/Keyboard org.onboard.Onboard.Keyboard.Hide')

def listener():
  rospy.init_node('onboard_listener')
  rospy.Subscriber('onboard/visibility', Bool, callback)

  rospy.spin()

if __name__=='__main__':
  listener()
<commit_msg>onboard: dconf load before every SHOW event.<commit_after>#!/usr/bin/env python
# This app sends messages to dbus signalling visibility of the onboard on-screen keyboard.

import os
import rospy
from std_msgs.msg import Bool

def callback(data):
  # TODO: use python dbus bindings instead of dbus cli
  if data.data:
    os.system('dconf load /org/onboard/ < ' + os.getenv('HOME') + '/etc/onboard.dconf')
    os.system('dbus-send --type=method_call --dest=org.onboard.Onboard /org/onboard/Onboard/Keyboard org.onboard.Onboard.Keyboard.Show')
  else:
    os.system('dbus-send --type=method_call --dest=org.onboard.Onboard /org/onboard/Onboard/Keyboard org.onboard.Onboard.Keyboard.Hide')

def listener():
  rospy.init_node('onboard_listener')
  rospy.Subscriber('onboard/visibility', Bool, callback)

  rospy.spin()

if __name__=='__main__':
  listener()
<|endoftext|>