<commit_before># -*- coding: utf-8 -*-

'''

message.py: simple logger for Singularity python helper

The error names (prefix) and integer level are assigned as follows:

ABRT -4
ERROR -3
WARNING -2
LOG -1
INFO 1
QUIET 0
VERBOSE 2
VERBOSE1 2
VERBOSE2 3
VERBOSE3 4
DEBUG 5

VERBOSE is equivalent to VERBOSE1 (this is mirroring the C code)
and for each level, calling it corresponds to calling the class'
function for it. E.g., DEBUG --> bot.debug('This is the message!')

The following levels are to stderr:

5,4,3,2,1,-1,-2,-3,-4

The following levels are only to stdout

1

The following levels do nothing (quiet)

0

Copyright (c) 2016-2017, Vanessa Sochat. All rights reserved.

"Singularity" Copyright (c) 2016, The Regents of the University of California,
through Lawrence Berkeley National Laboratory (subject to receipt of any
required approvals from the U.S. Dept. of Energy).  All rights reserved.

This software is licensed under a customized 3-clause BSD license.  Please
consult LICENSE file distributed with the sources of this project regarding
your rights to use or distribute this software.

NOTICE.  This Software was developed under funding from the U.S. Department of
Energy and the U.S. Government consequently retains certain rights. As such,
the U.S. Government has been granted for itself and others acting on its
behalf a paid-up, nonexclusive, irrevocable, worldwide license in the Software
to reproduce, distribute copies to the public, prepare derivative works, and
perform publicly and display publicly, and to permit other to do so.

'''

import os
import sys

ABRT = -4
ERROR = -3
WARNING = -2
LOG = -1
INFO = 1
QUIET = 0
VERBOSE = VERBOSE1 = 2
VERBOSE2 = 3
VERBOSE3 = 4
DEBUG = 5

class SingularityMessage:

    def __init__(self,MESSAGELEVEL=None):
        self.level = get_logging_level()
        self.history = []
        self.errorStream = sys.stderr
        self.outputStream = sys.stdout
        self.colorize = self.useColor()
        self.colors = {ABRT:"\033[31m",
                       ERROR: "\033[91m",
                       WARNING:"\033[93m",
                       LOG:"\033[95m",
                       INFO:"\033[94m",
                       DEBUG:"\033[36m",
                       'OFF':"\033[0m"}

    # Colors --------------------------------------------

    def useColor(self):
        '''useColor will determine if color should be added
        to a print. Will check if being run in a terminal, and
        if has support for asci'''
        COLORIZE = get_user_color_preference()
        if COLORIZE is not None:
            return COLORIZE
        streams = [self.errorStream,self.outputStream]
        for stream in streams:
            if not hasattr(stream, 'isatty'):
                return False
            if not stream.isatty():
                return False
        return True

    def addColor(self,level,text):
        '''addColor to the prompt (usually prefix) if terminal
        supports, and specified to do so'''
        if self.colorize:
            if level in self.colors:
                text = "%s%s%s" %(self.colors[level],
                                  text,
                                  self.colors["OFF"])
        return text

    def emitError(self,level):
        '''determine if a level should print to
        stderr, includes all levels but INFO and QUIET'''
        if level in [ABRT,
                     ERROR,
                     WARNING,
                     LOG,
                     VERBOSE,
                     VERBOSE1,
                     VERBOSE2,
                     VERBOSE3,
                     DEBUG ]:
            return True
        return False

    def emitOutput(self,level):
        '''determine if a level should print to stdout
        only includes INFO'''
        if level in [INFO]:
            return True
        return False

    def isEnabledFor(self,messageLevel):
        '''check if a messageLevel is enabled to emit a level
        '''
        if messageLevel <= self.level:
            return True
        return False

    def emit(self,level,message,prefix=None):
        '''emit is the main function to print the message
        optionally with a prefix
        :param level: the level of the message
        :param message: the message to print
        :param prefix: a prefix for the message
        '''

        if prefix is not None:
            prefix = self.addColor(level,"%s " %(prefix))
        else:
            prefix = ""
            message = self.addColor(level,message)

        # Add the prefix
        message = "%s%s" %(prefix,message)

        if not message.endswith('\n'):
            message = "%s\n" %message

        # If the level is quiet, only print to error
        if self.level == QUIET:
            pass

        # Otherwise if in range print to stdout and stderr
        elif self.isEnabledFor(level):
            if self.emitError(level):
                self.write(self.errorStream,message)
            else:
                self.write(self.outputStream,message)

        # Add all log messages to history
        self.history.append(message)

    def write(self,stream,message):
        '''write will write a message to a stream,
        first checking the encoding
        '''
        if isinstance(message,bytes):
            message = message.decode('utf-8')
        stream.write(message)

    def get_logs(self,join_newline=True):
        ''''get_logs will return the complete history, joined by newline
        (default) or as is.
        '''
        if join_newline:
            return '\n'.join(self.history)
        return self.history

    def show_progress(self,iteration,total,length=40,min_level=0,prefix=None,
                      carriage_return=True,suffix=None,symbol=None):
        '''create a terminal progress bar, default bar shows for verbose+
        :param iteration: current iteration (Int)
        :param total: total iterations (Int)
        :param length: character length of bar (Int)
        '''
        percent = 100 * (iteration / float(total))
        progress = int(length * iteration // total)

        if suffix is None:
            suffix = ''

        if prefix is None:
            prefix = 'Progress'
        if self.colorize:
            prefix = "\033[1m" + prefix + "\033[0m"

        # Download sizes can be imperfect, setting carriage_return to False
        # and writing newline with caller cleans up the UI
        if percent >= 100:
            percent = 100
            progress = length

        if symbol is None:
            if self.colorize:
                symbol = ' '
            else:
                symbol = "="

        if progress < length:
            if self.colorize:
                bar = "\033[42m" + symbol * progress + "\033[0m" + '|' + '-' * (length - progress - 1)
            else:
                bar = symbol * progress + '|' + '-' * (length - progress - 1)
        else:
            if self.colorize:
                bar = "\033[42m" + symbol * progress + "\033[0m" + '-' * (length - progress)
            else:
                bar = symbol * progress + '-' * (length - progress)

        # Only show progress bar for level > min_level
        if self.level > min_level:
            percent = "%5s" %("{0:.1f}").format(percent)
            output = '\r' + prefix +  " |%s| %s%s %s" % (bar, percent, '%', suffix)
            sys.stdout.write(output),
            if iteration == total and carriage_return:
                sys.stdout.write('\n')
            sys.stdout.flush()

    def abort(self,message):
        self.emit(ABRT,message,'ABRT')

    def error(self,message):
        self.emit(ERROR,message,'ERROR')

    def warning(self,message):
        self.emit(WARNING,message,'WARNING')

    def log(self,message):
        self.emit(LOG,message,'LOG')

    def info(self,message):
        self.emit(INFO,message)

    def verbose(self,message):
        self.emit(VERBOSE,message,"VERBOSE")

    def verbose1(self,message):
        self.emit(VERBOSE,message,"VERBOSE1")

    def verbose2(self,message):
        self.emit(VERBOSE2,message,'VERBOSE2')

    def verbose3(self,message):
        self.emit(VERBOSE3,message,'VERBOSE3')

    def debug(self,message):
        self.emit(DEBUG,message,'DEBUG')

    def is_quiet(self):
        '''is_quiet returns true if the level is under 1
        '''
        if self.level < 1:
            return False
        return True

def get_logging_level():
    '''get_logging_level will configure a logging to standard out based on the user's
    selected level, which should be in an environment variable called
    SINGULARITY_MESSAGELEVEL. if SINGULARITY_MESSAGELEVEL is not set, the maximum level
    (5) is assumed (all messages). levels from
    https://github.com/singularityware/singularity/blob/master/src/lib/message.h

    #define ABRT -4
    #define ERROR -3
    #define WARNING -2
    #define LOG -1
    #define INFO 1

    implied define: QUIET 0

    #define VERBOSE 2
    #define VERBOSE1 2
    #define VERBOSE2 3
    #define VERBOSE3 4
    #define DEBUG 5
    '''

    return int(os.environ.get("SINGULARITY_MESSAGELEVEL",5))

def get_user_color_preference():
    COLORIZE = os.environ.get('SINGULARITY_COLORIZE',None)
    if COLORIZE is not None:
        COLORIZE = convert2boolean(COLORIZE)
    return COLORIZE

def convert2boolean(arg):
  '''convert2boolean is used for environmental variables that must be
  returned as boolean'''
  if not isinstance(arg,bool):
      return arg.lower() in ("yes", "true", "t", "1","y")
  return arg

bot = SingularityMessage()
<commit_msg>updating the message colorization<commit_after># -*- coding: utf-8 -*-

'''

message.py: simple logger for Singularity python helper

The error names (prefix) and integer level are assigned as follows:

ABRT -4
ERROR -3
WARNING -2
LOG -1
INFO 1
QUIET 0
VERBOSE 2
VERBOSE1 2
VERBOSE2 3
VERBOSE3 4
DEBUG 5

VERBOSE is equivalent to VERBOSE1 (this is mirroring the C code)
and for each level, calling it corresponds to calling the class'
function for it. E.g., DEBUG --> bot.debug('This is the message!')

The following levels are to stderr:

5,4,3,2,1,-1,-2,-3,-4

The following levels are only to stdout

1

The following levels do nothing (quiet)

0

Copyright (c) 2016-2017, Vanessa Sochat. All rights reserved.

"Singularity" Copyright (c) 2016, The Regents of the University of California,
through Lawrence Berkeley National Laboratory (subject to receipt of any
required approvals from the U.S. Dept. of Energy).  All rights reserved.

This software is licensed under a customized 3-clause BSD license.  Please
consult LICENSE file distributed with the sources of this project regarding
your rights to use or distribute this software.

NOTICE.  This Software was developed under funding from the U.S. Department of
Energy and the U.S. Government consequently retains certain rights. As such,
the U.S. Government has been granted for itself and others acting on its
behalf a paid-up, nonexclusive, irrevocable, worldwide license in the Software
to reproduce, distribute copies to the public, prepare derivative works, and
perform publicly and display publicly, and to permit other to do so.

'''

import os
import sys

ABRT = -4
ERROR = -3
WARNING = -2
LOG = -1
INFO = 1
QUIET = 0
VERBOSE = VERBOSE1 = 2
VERBOSE2 = 3
VERBOSE3 = 4
DEBUG = 5

class SingularityMessage:

    def __init__(self,MESSAGELEVEL=None):
        self.level = get_logging_level()
        self.history = []
        self.errorStream = sys.stderr
        self.outputStream = sys.stdout
        self.colorize = self.useColor()
        self.colors = {ABRT:"\033[31m",    # dark red
                       ERROR: "\033[91m",  # red
                       WARNING:"\033[93m", # dark yellow
                       LOG:"\033[95m",     # purple
                       DEBUG:"\033[36m",   # cyan
                       'OFF':"\033[0m"}    # end sequence

    # Colors --------------------------------------------

    def useColor(self):
        '''useColor will determine if color should be added
        to a print. Will check if being run in a terminal, and
        if has support for asci'''
        COLORIZE = get_user_color_preference()
        if COLORIZE is not None:
            return COLORIZE
        streams = [self.errorStream,self.outputStream]
        for stream in streams:
            if not hasattr(stream, 'isatty'):
                return False
            if not stream.isatty():
                return False
        return True

    def addColor(self,level,text):
        '''addColor to the prompt (usually prefix) if terminal
        supports, and specified to do so'''
        if self.colorize:
            if level in self.colors:
                text = "%s%s%s" %(self.colors[level],
                                  text,
                                  self.colors["OFF"])
        return text

    def emitError(self,level):
        '''determine if a level should print to
        stderr, includes all levels but INFO and QUIET'''
        if level in [ABRT,
                     ERROR,
                     WARNING,
                     LOG,
                     VERBOSE,
                     VERBOSE1,
                     VERBOSE2,
                     VERBOSE3,
                     DEBUG ]:
            return True
        return False

    def emitOutput(self,level):
        '''determine if a level should print to stdout
        only includes INFO'''
        if level in [INFO]:
            return True
        return False

    def isEnabledFor(self,messageLevel):
        '''check if a messageLevel is enabled to emit a level
        '''
        if messageLevel <= self.level:
            return True
        return False

    def emit(self,level,message,prefix=None):
        '''emit is the main function to print the message
        optionally with a prefix
        :param level: the level of the message
        :param message: the message to print
        :param prefix: a prefix for the message
        '''

        if prefix is not None:
            prefix = self.addColor(level,"%s " %(prefix))
        else:
            prefix = ""
            message = self.addColor(level,message)

        # Add the prefix
        message = "%s%s" %(prefix,message)

        if not message.endswith('\n'):
            message = "%s\n" %message

        # If the level is quiet, only print to error
        if self.level == QUIET:
            pass

        # Otherwise if in range print to stdout and stderr
        elif self.isEnabledFor(level):
            if self.emitError(level):
                self.write(self.errorStream,message)
            else:
                self.write(self.outputStream,message)

        # Add all log messages to history
        self.history.append(message)

    def write(self,stream,message):
        '''write will write a message to a stream,
        first checking the encoding
        '''
        if isinstance(message,bytes):
            message = message.decode('utf-8')
        stream.write(message)

    def get_logs(self,join_newline=True):
        ''''get_logs will return the complete history, joined by newline
        (default) or as is.
        '''
        if join_newline:
            return '\n'.join(self.history)
        return self.history

    def show_progress(self,iteration,total,length=40,min_level=0,prefix=None,
                      carriage_return=True,suffix=None,symbol=None):
        '''create a terminal progress bar, default bar shows for verbose+
        :param iteration: current iteration (Int)
        :param total: total iterations (Int)
        :param length: character length of bar (Int)
        '''
        percent = 100 * (iteration / float(total))
        progress = int(length * iteration // total)

        if suffix is None:
            suffix = ''

        if prefix is None:
            prefix = 'Progress'
        if self.colorize:
            prefix = "\033[1m" + prefix + "\033[0m"

        # Download sizes can be imperfect, setting carriage_return to False
        # and writing newline with caller cleans up the UI
        if percent >= 100:
            percent = 100
            progress = length

        if symbol is None:
            symbol = "="

        if progress < length:
            bar = symbol * progress + '|' + '-' * (length - progress - 1)
        else:
            bar = symbol * progress + '-' * (length - progress)

        # Only show progress bar for level > min_level
        if self.level > min_level:
            percent = "%5s" %("{0:.1f}").format(percent)
            output = '\r' + prefix +  " |%s| %s%s %s" % (bar, percent, '%', suffix)
            sys.stdout.write(output),
            if iteration == total and carriage_return:
                sys.stdout.write('\n')
            sys.stdout.flush()

    def abort(self,message):
        self.emit(ABRT,message,'ABRT')

    def error(self,message):
        self.emit(ERROR,message,'ERROR')

    def warning(self,message):
        self.emit(WARNING,message,'WARNING')

    def log(self,message):
        self.emit(LOG,message,'LOG')

    def info(self,message):
        self.emit(INFO,message)

    def verbose(self,message):
        self.emit(VERBOSE,message,"VERBOSE")

    def verbose1(self,message):
        self.emit(VERBOSE,message,"VERBOSE1")

    def verbose2(self,message):
        self.emit(VERBOSE2,message,'VERBOSE2')

    def verbose3(self,message):
        self.emit(VERBOSE3,message,'VERBOSE3')

    def debug(self,message):
        self.emit(DEBUG,message,'DEBUG')

    def is_quiet(self):
        '''is_quiet returns true if the level is under 1
        '''
        if self.level < 1:
            return False
        return True

def get_logging_level():
    '''get_logging_level will configure a logging to standard out based on the user's
    selected level, which should be in an environment variable called
    SINGULARITY_MESSAGELEVEL. if SINGULARITY_MESSAGELEVEL is not set, the maximum level
    (5) is assumed (all messages). levels from
    https://github.com/singularityware/singularity/blob/master/src/lib/message.h

    #define ABRT -4
    #define ERROR -3
    #define WARNING -2
    #define LOG -1
    #define INFO 1

    implied define: QUIET 0

    #define VERBOSE 2
    #define VERBOSE1 2
    #define VERBOSE2 3
    #define VERBOSE3 4
    #define DEBUG 5
    '''

    return int(os.environ.get("SINGULARITY_MESSAGELEVEL",5))

def get_user_color_preference():
    COLORIZE = os.environ.get('SINGULARITY_COLORIZE',None)
    if COLORIZE is not None:
        COLORIZE = convert2boolean(COLORIZE)
    return COLORIZE

def convert2boolean(arg):
  '''convert2boolean is used for environmental variables that must be
  returned as boolean'''
  if not isinstance(arg,bool):
      return arg.lower() in ("yes", "true", "t", "1","y")
  return arg

bot = SingularityMessage()
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-

import numpy as np
from pandas import Timestamp, DateOffset
from datetime import datetime
import copy

class BasicOption(object):
    def __init__(self):
        self._underlying_close_price = None
        self._dividend = 0.0
        self._expiry_date = None
        self._trade_date = None
        self._forward_price = None
        self._exercise_type = 'european'
        self._zero_rate = None

    def set_exercise_type(self, exercise_type):
        self._exercise_type = exercise_type
        return self

    def set_time_to_maturity(self, time_to_maturity_in_days):
        if self.get_evaluation_date() is None:
            self.set_evaluation_date(Timestamp(datetime.now().strftime('%Y-%m-%d')))
        self.set_maturity_date(self.get_evaluation_date() + DateOffset(days=time_to_maturity_in_days))
        return self

    def set_forward_price(self, forward_price):
        self._forward_price = forward_price
        return self

    def set_underlying_close_price(self, underlying_price):
        self._underlying_close_price = underlying_price
        return self

    def set_zero_rate(self, risk_free_rate):
        self._zero_rate = risk_free_rate
        return self

    def set_dividend(self, dividend):
        self._dividend = float(dividend)
        return self

    def set_evaluation_date(self, evaluation_date):
        self._trade_date = Timestamp(evaluation_date)
        return self

    def set_maturity_date(self, maturity_date):
        self._expiry_date = Timestamp(maturity_date)
        return self

    def get_underlying_close_price(self):
        return self._underlying_close_price

    def get_zero_rate(self):
        return self._zero_rate

    def get_dividend(self):
        return self._dividend

    def get_evaluation_date(self):
        return self._trade_date

    def get_expiry_date(self):
        return self._expiry_date

    def get_discount_bond_price(self):
        return 1.0 / np.exp(self.get_zero_rate() * self.get_time_to_maturity())

    def get_exercise_type(self):
        return self._exercise_type

    def get_duration(self):
        """
        :return: time in days
        """
        return (self.get_expiry_date() - self.get_evaluation_date()).days

    def get_forward_price(self):
        if self._forward_price is None:
            return self.get_underlying_close_price() * np.exp(
                (self.get_zero_rate() - self.get_dividend()) * self.get_time_to_maturity())
        else:
            return self._forward_price

    def get_time_to_maturity(self, annualization_factor=365):
        return self.get_duration() / annualization_factor

    def to_dict(self):
        res = {'underlying_close_price': self._underlying_close_price,
               'dividend': self._dividend,
               'expiry_date': self._expiry_date,
               'trade_date': self._trade_date,
               'forward_price': self._forward_price,
               'zero_rate': self._zero_rate,
               'exercise_type': self._exercise_type}
        return res

    def serialize(self):
        return self.to_dict()

    def deserialize(self, serial_dict):
        self._underlying_close_price = serial_dict['underlying_close_price']
        self._dividend = serial_dict['dividend']
        self._expiry_date = serial_dict['expiry_date']
        self._trade_date = serial_dict['trade_date']
        self._forward_price = serial_dict['forward_price']
        self._zero_rate = serial_dict['zero_rate']
        self._exercise_type = serial_dict['exercise_type']

    def copy(self):
        return copy.deepcopy(self)
<commit_msg>add serialization functionality<commit_after># -*- coding: utf-8 -*-

import copy
from datetime import datetime

import numpy as np
from pandas import Timestamp, DateOffset

class BasicOption(object):
    def __init__(self):
        self._underlying_close_price = None
        self._dividend = 0.0
        self._expiry_date = None
        self._trade_date = None
        self._forward_price = None
        self._exercise_type = 'european'
        self._zero_rate = None

    def set_exercise_type(self, exercise_type):
        self._exercise_type = exercise_type
        return self

    def set_time_to_maturity(self, time_to_maturity_in_days):
        if self.get_evaluation_date() is None:
            self.set_evaluation_date(Timestamp(datetime.now().strftime('%Y-%m-%d')))
        self.set_maturity_date(self.get_evaluation_date() + DateOffset(days=time_to_maturity_in_days))
        return self

    def set_forward_price(self, forward_price):
        self._forward_price = forward_price
        return self

    def set_underlying_close_price(self, underlying_price):
        self._underlying_close_price = underlying_price
        return self

    def set_zero_rate(self, risk_free_rate):
        self._zero_rate = risk_free_rate
        return self

    def set_dividend(self, dividend):
        self._dividend = float(dividend)
        return self

    def set_evaluation_date(self, evaluation_date):
        self._trade_date = Timestamp(evaluation_date)
        return self

    def set_maturity_date(self, maturity_date):
        self._expiry_date = Timestamp(maturity_date)
        return self

    def get_underlying_close_price(self):
        return self._underlying_close_price

    def get_zero_rate(self):
        return self._zero_rate

    def get_dividend(self):
        return self._dividend

    def get_evaluation_date(self):
        return self._trade_date

    def get_expiry_date(self):
        return self._expiry_date

    def get_discount_bond_price(self):
        return 1.0 / np.exp(self.get_zero_rate() * self.get_time_to_maturity())

    def get_exercise_type(self):
        return self._exercise_type

    def get_duration(self):
        """
        :return: time in days
        """
        return (self.get_expiry_date() - self.get_evaluation_date()).days

    def get_forward_price(self):
        if self._forward_price is None:
            return self.get_underlying_close_price() * np.exp(
                (self.get_zero_rate() - self.get_dividend()) * self.get_time_to_maturity())
        else:
            return self._forward_price

    def get_time_to_maturity(self, annualization_factor=365):
        return self.get_duration() / annualization_factor

    def to_dict(self):
        res = {'underlying_close_price': self._underlying_close_price,
               'dividend': self._dividend,
               'expiry_date': self._expiry_date,
               'trade_date': self._trade_date,
               'forward_price': self._forward_price,
               'zero_rate': self._zero_rate,
               'exercise_type': self._exercise_type}
        return res

    def serialize(self):
        return self.to_dict()

    def deserialize(self, serial_dict):
        self._underlying_close_price = serial_dict['underlying_close_price']
        self._dividend = serial_dict['dividend']
        self._expiry_date = serial_dict['expiry_date']
        self._trade_date = serial_dict['trade_date']
        self._forward_price = serial_dict['forward_price']
        self._zero_rate = serial_dict['zero_rate']
        self._exercise_type = serial_dict['exercise_type']

    def copy(self):
        return copy.deepcopy(self)
<|endoftext|>


<commit_before>import cv2

def get_map(image):
    print 'Inside get_map.'

if __name__ == '__main__':
    print('Inside Main.')
    # image_path = ''
    # img = cv2.imread(image_path, cv2.IMREAD_COLOR)
    # get_map(img)
<commit_msg>Update<commit_after>import cv2
import matplotlib.pyplot as plt

def get_map(image):
    print 'Inside get_map.'

def display_image(image):
    plt.axis('off')
    plt.imshow(image)
    plt.draw()

if __name__ == '__main__':
    print('Inside Main.')
    image_path = '/home/cs17mtech01001/workspace/SDD-RFCN-python/caffe/examples/images/cat.jpg'
    img = cv2.imread(image_path, cv2.IMREAD_COLOR)

    display_image(img)
    get_map(img)

<|endoftext|>


<commit_before>#!/usr/bin/env python
# -*- coding: utf-8 -*-
# (C) 2015  Jean Nassar
# Released under BSD version 4
"""
Beep when pose does not change for `TIMEOUT_TIME`

"""
import os

from geometry_msgs.msg import PoseStamped
import pygame
import rospy

TIMEOUT = 0.25  # seconds
SPIRIT_ROOT = os.path.expanduser("~/catkin_ws/src/spirit")

class Beeper(object):
    def __init__(self):
        pygame.init()
        pygame.mixer.music.load(os.path.join(SPIRIT_ROOT, "audio/beep.wav"))

        self.subscriber = rospy.Subscriber("/ardrone/pose", PoseStamped,
                                           self.callback, queue_size=1)

        self.connected = False
        self.last_updated = None

        self._last_pose = None
        self._tracking = True
        self._start_time = rospy.Time.now()

        rospy.loginfo("Waiting for a connection")

    def beep(self):
        """
        Beeps an audio file.

        """
        pygame.mixer.music.play()

    def callback(self, pose):
        if not self.connected:
            rospy.loginfo("Connection active")
            self.connected = True

        if self._last_pose is not None and pose is not None:
            # TODO (masasin): `self._last_pose.pose != pose.pose` always True
            if self._last_pose.pose == pose.pose:
                if self.last_updated is None:
                    reference_time = self._start_time
                else:
                    reference_time = self.last_updated

                if ((rospy.Time.now() - reference_time).to_sec() > TIMEOUT and
                        self.tracking):
                    self.tracking = False
            else:
                self.last_updated = pose.header.stamp
                if not self.tracking:
                    self.tracking = True

        self._last_pose = pose

    @property
    def tracking(self):
        return self._tracking

    @tracking.setter
    def tracking(self, value):
        self._tracking = value
        if not value:
            if self.last_updated is None:
                rospy.logwarn("AR.Drone is not being tracked! "
                              "Please check your setup.")
            else:
                rospy.logwarn("Tracking lost!")
                self.beep()

        elif self.connected:
            rospy.loginfo("Tracking reacquired")

def shutdown_hook():
    """Runs on shutdown."""
    pygame.quit()

def main():
    rospy.init_node("beeper", anonymous=True)
    rospy.on_shutdown(shutdown_hook)
    Beeper()
    rospy.spin()

if __name__ == "__main__":
    main()
<commit_msg>Separate tracking logic for testability.<commit_after>#!/usr/bin/env python
# -*- coding: utf-8 -*-
# (C) 2015  Jean Nassar
# Released under BSD version 4
"""
Beep when pose does not change for `TIMEOUT_TIME`

"""
import os

from geometry_msgs.msg import PoseStamped
import pygame
import rospy

TIMEOUT = 0.25  # seconds
SPIRIT_ROOT = os.path.expanduser("~/catkin_ws/src/spirit")

class Beeper(object):
    def __init__(self):
        pygame.init()
        pygame.mixer.music.load(os.path.join(SPIRIT_ROOT, "audio/beep.wav"))

        self.subscriber = rospy.Subscriber("/ardrone/pose", PoseStamped,
                                           self.callback, queue_size=1)

        self.connected = False
        self.last_updated = None

        self._last_pose = None
        self._tracking = True
        self._start_time = rospy.Time.now()

        rospy.loginfo("Waiting for a connection")

    def beep(self):
        """
        Beeps an audio file.

        """
        pygame.mixer.music.play()

    def callback(self, pose):
        if not self.connected:
            rospy.loginfo("Connection active")
            self.connected = True

        if self._last_pose is not None and pose is not None:
            # TODO (masasin): `self._last_pose.pose != pose.pose` always True
            if self._last_pose.pose == pose.pose:
                if self.last_updated is None:
                    reference_time = self._start_time
                else:
                    reference_time = self.last_updated

                if ((rospy.Time.now() - reference_time).to_sec() > TIMEOUT and
                        self.tracking):
                    self.tracking = False
            else:
                self.last_updated = pose.header.stamp
                if not self.tracking:
                    self.tracking = True

        self._last_pose = pose

    @property
    def tracking(self):
        return self._tracking

    @tracking.setter
    def tracking(self, value):
        self._tracking = value
        if not value:
            self._handle_no_tracking()

        elif self.connected:
            self._handle_tracking()

    def _handle_no_tracking(self):
        if self.last_updated is None:
            rospy.logwarn("AR.Drone is not being tracked! "
                          "Please check your setup.")
        else:
            rospy.logwarn("Tracking lost!")
            self.beep()

    def _handle_tracking(self):
        rospy.loginfo("Tracking reacquired")

def shutdown_hook():
    """Runs on shutdown."""
    pygame.quit()

def main():
    rospy.init_node("beeper", anonymous=True)
    rospy.on_shutdown(shutdown_hook)
    Beeper()
    rospy.spin()

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


<commit_before>from datetime import datetime
from corehq.apps.products.models import SQLProduct
from dimagi.utils.dates import force_to_date
import fluff
import re
import logging
from custom.intrahealth import get_location_by_type, PRODUCT_MAPPING, get_domain, PRODUCT_NAMES,\
    get_loc_from_case, COMMANDE_XMLNSES, OPERATEUR_XMLNSES

def get_value_from_path(dictionary, path, default_value=None):
    value = dictionary
    for key in path.split('/'):
        try:
            value = value[key]
        except Exception:
            return default_value
    return value

def form_date(form):
    return form.received_on

def real_date(form):
    return form.form['real_date']

def numeric_value(val):
    if val is None:
        return 0
    number = 0
    try:
        number = int(val)
    except ValueError:
        logging.info('Bad value type: %s' % val)
    return number

def get_product_id(product_name, domain):
    try:
        return SQLProduct.objects.get(name=product_name, domain=domain).product_id
    except SQLProduct.DoesNotExist:
        k = PRODUCT_NAMES.get(product_name.lower())
        if k:
            return SQLProduct.objects.get(name__iexact=k,
                                          domain=domain).product_id

def _locations_per_type(domain, loc_type, location):
    return (location.sql_location.get_descendants(include_self=True)
            .filter(domain=domain, location_type__name=loc_type, is_archived=False).count())

class PPSRegistered(fluff.Calculator):
    @fluff.date_emitter
    def total_for_region(self, form):
        loc = get_location_by_type(form=form, type=u'r\xe9gion')
        count = _locations_per_type(form.domain, 'PPS', loc)
        yield {
            'date': form_date(form),
            'value': count
        }

    @fluff.date_emitter
    def total_for_district(self, form):
        loc = get_location_by_type(form=form, type=u'district')
        count = _locations_per_type(form.domain, 'PPS', loc)
        yield {
            'date': form_date(form),
            'value': count
        }

class PPSPlaned(fluff.Calculator):
    @fluff.date_emitter
    def total(self, form):
        yield {
            'date': form_date(form),
            'value': 0
        }

class TauxCalculator(fluff.Calculator):

    def __init__(self, property_name):
        super(TauxCalculator, self).__init__()
        self.property_name = property_name

    def _process_v1_form(self, form):
        num_products = numeric_value(form.get_data('form/num_products'))
        print num_products
        if num_products > 1:
            for product in (form.get_data('form/products') or []):
                received_month_inner = product.get('receivedMonthInner')
                amount_ordered = numeric_value(product.get(self.property_name))
                product_name = product.get('productName')
                if received_month_inner and amount_ordered is not None and product_name:
                    yield {
                        'date': received_month_inner,
                        'value': amount_ordered,
                        'group_by': [product_name, get_product_id(product_name, get_domain(form))]
                    }
        else:
            received_month_inner = form.get_data('form/products/receivedMonthInner')
            amount_ordered = numeric_value(form.get_data('form/products/{}}'.format(self.property_name)))
            product_name = form.get_data('form/products/productName')
            if received_month_inner and amount_ordered and product_name:
                yield {
                    'date': received_month_inner,
                    'value': amount_ordered,
                    'group_by': [product_name, get_product_id(product_name, get_domain(form))]
                }

    def _process_v2_form(self, form):
        for product in (form.get_data('form/products/item') or []):
            received_month_inner = product.get('receivedMonthInner')
            amount_ordered = numeric_value(get_value_from_path(product, 'qlist/{}'.format(self.property_name)))
            product_name = product.get('productName')
            if received_month_inner is not None and amount_ordered is not None and product_name is not None:
                yield {
                    'date': received_month_inner,
                    'value': amount_ordered,
                    'group_by': [product_name, get_product_id(product_name, get_domain(form))]
                }

    @fluff.date_emitter
    def total(self, form):
        if form.xmlns == COMMANDE_XMLNSES[2]:
            result = self._process_v2_form(form)
        else:
            result = self._process_v1_form(form)

        return result

class PPSConsumption(fluff.Calculator):

    def __init__(self, field='actual_consumption'):
        super(PPSConsumption, self).__init__()
        self.field = field

    def _process_v1_form(self, form):
        if 'num_products' in form.form and form.form['num_products'] > 1:
            for product in form.form['products']:
                if 'real_date' in form.form and form.form['real_date'] and 'product_name' in product:
                    yield {
                        'date': real_date(form),
                        'value': product[self.field],
                        'group_by': [product['product_name'],
                                     get_product_id(product['product_name'], get_domain(form))]
                    }
        elif 'real_date' in form.form and form.form['real_date'] and 'product_name' in form.form['products']:
            yield {
                'date': real_date(form),
                'value': form.form['products'][self.field],
                'group_by': [form.get_data('form/products/product_name'),
                             get_product_id(form.get_data('form/products/product_name'), get_domain(form))]
            }

    def _process_v2_form(self, form):
        real_date = form.get_data('form/real_date')
        if not real_date:
            return

        for item in form.get_data('form/products/item', []):
            yield {
                'date': real_date,
                'value': numeric_value(get_value_from_path(item, 'is-relevant/{}'.format(self.field))),
                'group_by': [item['product_name'], get_product_id(item['product_name'], get_domain(form))]
            }

    @fluff.date_emitter
    def total(self, form):
        if form.xmlns == OPERATEUR_XMLNSES[1]:
            result = self._process_v2_form(form)
        else:
            result = self._process_v1_form(form)
        return result

class RupturesDeStocks(fluff.Calculator):

    def __init__(self, field='pps_stocked_out'):
        super(RupturesDeStocks, self).__init__()
        self.field = field

    @fluff.date_emitter
    def total(self, form):
        for k, v in form.form.iteritems():
            if re.match("^rupture.*hv$", k):
                if 'date_rapportage' in form.form and form.form['date_rapportage']:
                    product_name = PRODUCT_NAMES.get(PRODUCT_MAPPING[k[8:-3]].lower())
                    if product_name is not None:
                        try:
                            prd = SQLProduct.objects.get(name__iexact=product_name,
                                                         domain=get_domain(form))
                            yield {
                                'date': form.form['date_rapportage'],
                                'value': v,
                                'group_by': [PRODUCT_MAPPING[k[8:-3]], prd.product_id]
                            }
                        except SQLProduct.DoesNotExist:
                            pass

class RecapPassage(fluff.Calculator):

    def _process_field(self, form, getter):
        if form.xmlns == OPERATEUR_XMLNSES[1]:
            for item in form.get_data('form/products/item'):
                if 'is-relevant' is not item:
                    continue
                item = item['is-relevant']
                val = numeric_value(item)
                yield {
                    'date': real_date(form),
                    "value": val,
                    "group_by": [item['product_name'],
                                 get_product_id(item['product_name'], get_domain(form))]
                }

        if 'num_products' in form.form and int(form.form['num_products']) > 1:
            for product in form.form['products']:
                if 'real_date' in form.form and form.form['real_date'] and 'product_name' in product:
                    val = numeric_value(getter(product))
                    yield {
                        'date': real_date(form),
                        "value": val,
                        "group_by": [product['product_name'],
                                     get_product_id(product['product_name'], get_domain(form))]
                    }
        elif 'real_date' in form.form and form.form['real_date'] and 'product_name' in form.form['products']:
            yield {
                'date': real_date(form),
                'value': getter(form.form['products']),
                'group_by': [form.form['products']['product_name'],
                             get_product_id(form.form['products']['product_name'], get_domain(form))]
            }

    @fluff.date_emitter
    def old_stock_total(self, form):
        return self._process_field(form, lambda x: x['old_stock_total'])

    @fluff.date_emitter
    def total_stock(self, form):
        return self._process_field(form, lambda x: x['total_stock'])

    @fluff.date_emitter
    def livraison(self, form):
        if form.xmlns == OPERATEUR_XMLNSES[1]:
            return self._process_field(form, lambda x: x['display_stock_total'] - x['total_stock'])
        else:
            return self._process_field(form, lambda x: x['top_up']['transfer']['entry']['value']['@quantity'])

    @fluff.date_emitter
    def display_total_stock(self, form):
        return self._process_field(form, lambda x: x['display_total_stock'])

    @fluff.date_emitter
    def old_stock_pps(self, form):
        return self._process_field(form, lambda x: x['old_stock_pps'])

    @fluff.date_emitter
    def outside_receipts_amount(self, form):
        return self._process_field(form, lambda x: x['outside_receipts_amt'])

    @fluff.date_emitter
    def actual_consumption(self, form):
        return self._process_field(form, lambda x: x['actual_consumption'])

    @fluff.date_emitter
    def billed_consumption(self, form):
        return self._process_field(form, lambda x: x['billed_consumption'])

    @fluff.date_emitter
    def pps_restant(self, form):
        return self._process_field(form, lambda x: x['pps_stock'] if numeric_value(x['pps_stock']) >= 0 else 0)

    @fluff.date_emitter
    def loss_amt(self, form):
        return self._process_field(form, lambda x: x['loss_amt'])

class DureeMoyenneLivraison(fluff.Calculator):

    @fluff.date_emitter
    def total(self, form):
        value = form.form['duree_moyenne_livraison'] if form.form['duree_moyenne_livraison'] else 0
        yield {
            'date': form.form['mois_visite'],
            'value': value
        }

def check_prop(prop, case):
    return case.get_case_property(prop) and case.get_case_property('date_du')

def get_district_name(case):
    return get_loc_from_case(case).name

def get_date_du(case):
    date = case.get_case_property('date_du')
    if type(date) is not datetime:
        return force_to_date(date)
    return date

class Recouvrement(fluff.Calculator):

    @fluff.date_emitter
    def amount_to_pay(self, case):
        if check_prop('quantite_reale_a_payer', case):
            value = case.get_case_property('quantite_reale_a_payer')

            yield {
                'date': get_date_du(case),
                'value': int(value),
                'group_by': [get_district_name(case),
                             case.get_case_property('domain')]
            }

    @fluff.date_emitter
    def amount_paid(self, case):
        if check_prop('montant_paye', case):
            value = case.get_case_property('montant_paye')

            yield {
                'date': get_date_du(case),
                'value': int(value),
                'group_by': [get_district_name(case),
                             case.get_case_property('domain')]
            }

    @fluff.date_emitter
    def in_30_days(self, case):
        if check_prop('payee_trent_jour', case):
            value = case.get_case_property('payee_trent_jour')

            yield {
                'date': get_date_du(case),
                'value': int(value),
                'group_by': [get_district_name(case),
                             case.get_case_property('domain')]
            }

    @fluff.date_emitter
    def in_3_months(self, case):
        if check_prop('payee_trois_mois', case):
            value = case.get_case_property('payee_trois_mois')

            yield {
                'date': get_date_du(case),
                'value': int(value),
                'group_by': [get_district_name(case),
                             case.get_case_property('domain')]
            }

    @fluff.date_emitter
    def in_year(self, case):
        if check_prop('payee_un_an', case):
            value = case.get_case_property('payee_un_an')

            yield {
                'date': get_date_du(case),
                'value': int(value),
                'group_by': [get_district_name(case),
                             case.get_case_property('domain')]
            }
<commit_msg>Fixed bug in calculator<commit_after>from datetime import datetime
from corehq.apps.products.models import SQLProduct
from dimagi.utils.dates import force_to_date
import fluff
import re
import logging
from custom.intrahealth import get_location_by_type, PRODUCT_MAPPING, get_domain, PRODUCT_NAMES,\
    get_loc_from_case, COMMANDE_XMLNSES, OPERATEUR_XMLNSES

def get_value_from_path(dictionary, path, default_value=None):
    value = dictionary
    for key in path.split('/'):
        try:
            value = value[key]
        except Exception:
            return default_value
    return value

def form_date(form):
    return form.received_on

def real_date(form):
    return form.form['real_date']

def numeric_value(val):
    if val is None:
        return 0
    number = 0
    try:
        number = int(val)
    except ValueError:
        logging.info('Bad value type: %s' % val)
    return number

def get_product_id(product_name, domain):
    try:
        return SQLProduct.objects.get(name=product_name, domain=domain).product_id
    except SQLProduct.DoesNotExist:
        k = PRODUCT_NAMES.get(product_name.lower())
        if k:
            return SQLProduct.objects.get(name__iexact=k,
                                          domain=domain).product_id

def _locations_per_type(domain, loc_type, location):
    return (location.sql_location.get_descendants(include_self=True)
            .filter(domain=domain, location_type__name=loc_type, is_archived=False).count())

class PPSRegistered(fluff.Calculator):
    @fluff.date_emitter
    def total_for_region(self, form):
        loc = get_location_by_type(form=form, type=u'r\xe9gion')
        count = _locations_per_type(form.domain, 'PPS', loc)
        yield {
            'date': form_date(form),
            'value': count
        }

    @fluff.date_emitter
    def total_for_district(self, form):
        loc = get_location_by_type(form=form, type=u'district')
        count = _locations_per_type(form.domain, 'PPS', loc)
        yield {
            'date': form_date(form),
            'value': count
        }

class PPSPlaned(fluff.Calculator):
    @fluff.date_emitter
    def total(self, form):
        yield {
            'date': form_date(form),
            'value': 0
        }

class TauxCalculator(fluff.Calculator):

    def __init__(self, property_name):
        super(TauxCalculator, self).__init__()
        self.property_name = property_name

    def _process_v1_form(self, form):
        num_products = numeric_value(form.get_data('form/num_products'))
        print num_products
        if num_products > 1:
            for product in (form.get_data('form/products') or []):
                received_month_inner = product.get('receivedMonthInner')
                amount_ordered = numeric_value(product.get(self.property_name))
                product_name = product.get('productName')
                if received_month_inner and amount_ordered is not None and product_name:
                    yield {
                        'date': received_month_inner,
                        'value': amount_ordered,
                        'group_by': [product_name, get_product_id(product_name, get_domain(form))]
                    }
        else:
            received_month_inner = form.get_data('form/products/receivedMonthInner')
            amount_ordered = numeric_value(form.get_data('form/products/{}}'.format(self.property_name)))
            product_name = form.get_data('form/products/productName')
            if received_month_inner and amount_ordered and product_name:
                yield {
                    'date': received_month_inner,
                    'value': amount_ordered,
                    'group_by': [product_name, get_product_id(product_name, get_domain(form))]
                }

    def _process_v2_form(self, form):
        for product in (form.get_data('form/products/item') or []):
            received_month_inner = product.get('receivedMonthInner')
            amount_ordered = numeric_value(get_value_from_path(product, 'qlist/{}'.format(self.property_name)))
            product_name = product.get('productName')
            if received_month_inner is not None and amount_ordered is not None and product_name is not None:
                yield {
                    'date': received_month_inner,
                    'value': amount_ordered,
                    'group_by': [product_name, get_product_id(product_name, get_domain(form))]
                }

    @fluff.date_emitter
    def total(self, form):
        if form.xmlns == COMMANDE_XMLNSES[2]:
            result = self._process_v2_form(form)
        else:
            result = self._process_v1_form(form)

        return result

class PPSConsumption(fluff.Calculator):

    def __init__(self, field='actual_consumption'):
        super(PPSConsumption, self).__init__()
        self.field = field

    def _process_v1_form(self, form):
        if 'num_products' in form.form and form.form['num_products'] > 1:
            for product in form.form['products']:
                if 'real_date' in form.form and form.form['real_date'] and 'product_name' in product:
                    yield {
                        'date': real_date(form),
                        'value': product[self.field],
                        'group_by': [product['product_name'],
                                     get_product_id(product['product_name'], get_domain(form))]
                    }
        elif 'real_date' in form.form and form.form['real_date'] and 'product_name' in form.form['products']:
            yield {
                'date': real_date(form),
                'value': form.form['products'][self.field],
                'group_by': [form.get_data('form/products/product_name'),
                             get_product_id(form.get_data('form/products/product_name'), get_domain(form))]
            }

    def _process_v2_form(self, form):
        real_date = form.get_data('form/real_date')
        if not real_date:
            return

        for item in (form.get_data('form/products/item') or []):
            yield {
                'date': real_date,
                'value': numeric_value(get_value_from_path(item, 'is-relevant/{}'.format(self.field))),
                'group_by': [item['product_name'], get_product_id(item['product_name'], get_domain(form))]
            }

    @fluff.date_emitter
    def total(self, form):
        if form.xmlns == OPERATEUR_XMLNSES[1]:
            result = self._process_v2_form(form)
        else:
            result = self._process_v1_form(form)
        return result

class RupturesDeStocks(fluff.Calculator):

    def __init__(self, field='pps_stocked_out'):
        super(RupturesDeStocks, self).__init__()
        self.field = field

    @fluff.date_emitter
    def total(self, form):
        for k, v in form.form.iteritems():
            if re.match("^rupture.*hv$", k):
                if 'date_rapportage' in form.form and form.form['date_rapportage']:
                    product_name = PRODUCT_NAMES.get(PRODUCT_MAPPING[k[8:-3]].lower())
                    if product_name is not None:
                        try:
                            prd = SQLProduct.objects.get(name__iexact=product_name,
                                                         domain=get_domain(form))
                            yield {
                                'date': form.form['date_rapportage'],
                                'value': v,
                                'group_by': [PRODUCT_MAPPING[k[8:-3]], prd.product_id]
                            }
                        except SQLProduct.DoesNotExist:
                            pass

class RecapPassage(fluff.Calculator):

    def _process_field(self, form, getter):
        if form.xmlns == OPERATEUR_XMLNSES[1]:
            for item in form.get_data('form/products/item'):
                if 'is-relevant' is not item:
                    continue
                item = item['is-relevant']
                val = numeric_value(item)
                yield {
                    'date': real_date(form),
                    "value": val,
                    "group_by": [item['product_name'],
                                 get_product_id(item['product_name'], get_domain(form))]
                }

        if 'num_products' in form.form and int(form.form['num_products']) > 1:
            for product in form.form['products']:
                if 'real_date' in form.form and form.form['real_date'] and 'product_name' in product:
                    val = numeric_value(getter(product))
                    yield {
                        'date': real_date(form),
                        "value": val,
                        "group_by": [product['product_name'],
                                     get_product_id(product['product_name'], get_domain(form))]
                    }
        elif 'real_date' in form.form and form.form['real_date'] and 'product_name' in form.form['products']:
            yield {
                'date': real_date(form),
                'value': getter(form.form['products']),
                'group_by': [form.form['products']['product_name'],
                             get_product_id(form.form['products']['product_name'], get_domain(form))]
            }

    @fluff.date_emitter
    def old_stock_total(self, form):
        return self._process_field(form, lambda x: x['old_stock_total'])

    @fluff.date_emitter
    def total_stock(self, form):
        return self._process_field(form, lambda x: x['total_stock'])

    @fluff.date_emitter
    def livraison(self, form):
        if form.xmlns == OPERATEUR_XMLNSES[1]:
            return self._process_field(form, lambda x: x['display_stock_total'] - x['total_stock'])
        else:
            return self._process_field(form, lambda x: x['top_up']['transfer']['entry']['value']['@quantity'])

    @fluff.date_emitter
    def display_total_stock(self, form):
        return self._process_field(form, lambda x: x['display_total_stock'])

    @fluff.date_emitter
    def old_stock_pps(self, form):
        return self._process_field(form, lambda x: x['old_stock_pps'])

    @fluff.date_emitter
    def outside_receipts_amount(self, form):
        return self._process_field(form, lambda x: x['outside_receipts_amt'])

    @fluff.date_emitter
    def actual_consumption(self, form):
        return self._process_field(form, lambda x: x['actual_consumption'])

    @fluff.date_emitter
    def billed_consumption(self, form):
        return self._process_field(form, lambda x: x['billed_consumption'])

    @fluff.date_emitter
    def pps_restant(self, form):
        return self._process_field(form, lambda x: x['pps_stock'] if numeric_value(x['pps_stock']) >= 0 else 0)

    @fluff.date_emitter
    def loss_amt(self, form):
        return self._process_field(form, lambda x: x['loss_amt'])

class DureeMoyenneLivraison(fluff.Calculator):

    @fluff.date_emitter
    def total(self, form):
        value = form.form['duree_moyenne_livraison'] if form.form['duree_moyenne_livraison'] else 0
        yield {
            'date': form.form['mois_visite'],
            'value': value
        }

def check_prop(prop, case):
    return case.get_case_property(prop) and case.get_case_property('date_du')

def get_district_name(case):
    return get_loc_from_case(case).name

def get_date_du(case):
    date = case.get_case_property('date_du')
    if type(date) is not datetime:
        return force_to_date(date)
    return date

class Recouvrement(fluff.Calculator):

    @fluff.date_emitter
    def amount_to_pay(self, case):
        if check_prop('quantite_reale_a_payer', case):
            value = case.get_case_property('quantite_reale_a_payer')

            yield {
                'date': get_date_du(case),
                'value': int(value),
                'group_by': [get_district_name(case),
                             case.get_case_property('domain')]
            }

    @fluff.date_emitter
    def amount_paid(self, case):
        if check_prop('montant_paye', case):
            value = case.get_case_property('montant_paye')

            yield {
                'date': get_date_du(case),
                'value': int(value),
                'group_by': [get_district_name(case),
                             case.get_case_property('domain')]
            }

    @fluff.date_emitter
    def in_30_days(self, case):
        if check_prop('payee_trent_jour', case):
            value = case.get_case_property('payee_trent_jour')

            yield {
                'date': get_date_du(case),
                'value': int(value),
                'group_by': [get_district_name(case),
                             case.get_case_property('domain')]
            }

    @fluff.date_emitter
    def in_3_months(self, case):
        if check_prop('payee_trois_mois', case):
            value = case.get_case_property('payee_trois_mois')

            yield {
                'date': get_date_du(case),
                'value': int(value),
                'group_by': [get_district_name(case),
                             case.get_case_property('domain')]
            }

    @fluff.date_emitter
    def in_year(self, case):
        if check_prop('payee_un_an', case):
            value = case.get_case_property('payee_un_an')

            yield {
                'date': get_date_du(case),
                'value': int(value),
                'group_by': [get_district_name(case),
                             case.get_case_property('domain')]
            }
<|endoftext|>


<commit_before>import os

from .base import BASE_DIR

ASYNC = False

IPV4_PRIVACY_MASK = 16
IPV6_PRIVACY_MASK = 32

AUTH_SIGNUP = False

AUTH_WORKFLOW = None

AUTH_DETAIL_KEYS = []

METADATA_COLUMN_PERMISSIONS = False

QUERY_ANONYMOUS = False

QUERY_USER_DATABASE_PREFIX = 'daiquiri_user_'

QUERY_QUOTA = {
    'anonymous': '100Mb',
    'user': '100Mb',
    'users': {},
    'groups': {}
}

QUERY_QUEUES = [
    {
        'key': 'default',
        'label': 'Default',
        'timeout': 300,
        'priority': 1
    }
]

QUERY_LANGUAGES = [
    {
        'key': 'adql',
        'version': 2.0,
        'label': 'ADQL',
        'description': '',
        'quote_char': '"'
    }
]

QUERY_FORMS = [
    {
        'key': 'sql',
        'label': 'SQL query',
        'service': 'query/js/forms/sql.js',
        'template': 'query/query_form_sql.html'
    },
    {
        'key': 'box',
        'label': 'Box search',
        'service': 'query/js/forms/box.js',
        'template': 'query/query_form_box.html'
    },
    {
        'key': 'cone',
        'label': 'Cone search',
        'service': 'query/js/forms/cone.js',
        'template': 'query/query_form_cone.html'
    }
]

QUERY_DROPDOWNS = [
    {
        'key': 'simbad',
        'service': 'query/js/dropdowns/simbad.js',
        'template': 'query/query_dropdown_simbad.html',
        'options': {
            'url': 'http://simbad.u-strasbg.fr/simbad/sim-id'
        }
    },
    {
        'key': 'vizier',
        'service': 'query/js/dropdowns/vizier.js',
        'template': 'query/query_dropdown_vizier.html',
        'options': {
            'url': 'http://vizier.u-strasbg.fr/viz-bin/votable',
            'catalogs': ['I/322A', 'I/259']
        }
    }
]

QUERY_DOWNLOAD_DIR = os.path.join(BASE_DIR, 'download')
QUERY_DEFAULT_DOWNLOAD_FORMAT = 'votable'
QUERY_DOWNLOAD_FORMATS = [
    {
        'key': 'csv',
        'extension': 'csv',
        'content_type': 'text/csv',
        'label': 'Comma separated Values',
        'help': 'A text file with a line for each row of the table. The fields are delimited by a comma and quoted by double quotes. Use this option for a later import into a spreadsheed application or a custom script. Use this option if you are unsure what to use.'
    },
    {
        'key': 'votable',
        'extension': 'votable.xml',
        'content_type': 'application/xml',
        'label': 'IVOA VOTable XML file - TABLEDATA serialization',
        'help': 'A XML file using the IVOA VOTable format. Use this option if you intend to use VO compatible software to further process the data.'
    },
    {
        'key': 'votable-binary',
        'extension': 'votable.binary.xml',
        'content_type': 'application/xml',
        'label': 'IVOA VOTable XML file - BINARY serialization',
        'help': 'A XML file using the IVOA VOTable format (BINARY Serialization). Use this option if you intend to use VO compatible software to process the data and prefer the use of a binary file.'
    },
    {
        'key': 'votable-binary2',
        'extension': 'votable.binary2.xml',
        'content_type': 'application/xml',
        'label': 'IVOA VOTable XML file - BINARY 2 serialization',
        'help': 'A XML file using the IVOA VOTable format (BINARY2 Serialization). Use this option if you intend to use VO compatible software to process the data and prefer the use of a binary file.'
    }
]

SERVE_DOWNLOAD_DIR = os.path.join(BASE_DIR, 'download')

UWS_RESOURCES = []

WORDPRESS_URL = '/cms/'
WORDPRESS_CLI = '/opt/wp-cli/wp'
WORDPRESS_PATH = '/opt/wordpress'
<commit_msg>add cutout settings<commit_after>import os

from .base import BASE_DIR

ASYNC = False

IPV4_PRIVACY_MASK = 16
IPV6_PRIVACY_MASK = 32

AUTH_SIGNUP = False

AUTH_WORKFLOW = None

AUTH_DETAIL_KEYS = []

CUTOUT_ADAPTER = 'daiquiri.cutout.utils.CutoutAdapter'

CUTOUT_ANONYMOUS = False

METADATA_COLUMN_PERMISSIONS = False

QUERY_ANONYMOUS = False

QUERY_USER_DATABASE_PREFIX = 'daiquiri_user_'

QUERY_QUOTA = {
    'anonymous': '100Mb',
    'user': '100Mb',
    'users': {},
    'groups': {}
}

QUERY_QUEUES = [
    {
        'key': 'default',
        'label': 'Default',
        'timeout': 300,
        'priority': 1
    }
]

QUERY_LANGUAGES = [
    {
        'key': 'adql',
        'version': 2.0,
        'label': 'ADQL',
        'description': '',
        'quote_char': '"'
    }
]

QUERY_FORMS = [
    {
        'key': 'sql',
        'label': 'SQL query',
        'service': 'query/js/forms/sql.js',
        'template': 'query/query_form_sql.html'
    },
    {
        'key': 'box',
        'label': 'Box search',
        'service': 'query/js/forms/box.js',
        'template': 'query/query_form_box.html'
    },
    {
        'key': 'cone',
        'label': 'Cone search',
        'service': 'query/js/forms/cone.js',
        'template': 'query/query_form_cone.html'
    }
]

QUERY_DROPDOWNS = [
    {
        'key': 'simbad',
        'service': 'query/js/dropdowns/simbad.js',
        'template': 'query/query_dropdown_simbad.html',
        'options': {
            'url': 'http://simbad.u-strasbg.fr/simbad/sim-id'
        }
    },
    {
        'key': 'vizier',
        'service': 'query/js/dropdowns/vizier.js',
        'template': 'query/query_dropdown_vizier.html',
        'options': {
            'url': 'http://vizier.u-strasbg.fr/viz-bin/votable',
            'catalogs': ['I/322A', 'I/259']
        }
    }
]

QUERY_DOWNLOAD_DIR = os.path.join(BASE_DIR, 'download')
QUERY_DEFAULT_DOWNLOAD_FORMAT = 'votable'
QUERY_DOWNLOAD_FORMATS = [
    {
        'key': 'csv',
        'extension': 'csv',
        'content_type': 'text/csv',
        'label': 'Comma separated Values',
        'help': 'A text file with a line for each row of the table. The fields are delimited by a comma and quoted by double quotes. Use this option for a later import into a spreadsheed application or a custom script. Use this option if you are unsure what to use.'
    },
    {
        'key': 'votable',
        'extension': 'votable.xml',
        'content_type': 'application/xml',
        'label': 'IVOA VOTable XML file - TABLEDATA serialization',
        'help': 'A XML file using the IVOA VOTable format. Use this option if you intend to use VO compatible software to further process the data.'
    },
    {
        'key': 'votable-binary',
        'extension': 'votable.binary.xml',
        'content_type': 'application/xml',
        'label': 'IVOA VOTable XML file - BINARY serialization',
        'help': 'A XML file using the IVOA VOTable format (BINARY Serialization). Use this option if you intend to use VO compatible software to process the data and prefer the use of a binary file.'
    },
    {
        'key': 'votable-binary2',
        'extension': 'votable.binary2.xml',
        'content_type': 'application/xml',
        'label': 'IVOA VOTable XML file - BINARY 2 serialization',
        'help': 'A XML file using the IVOA VOTable format (BINARY2 Serialization). Use this option if you intend to use VO compatible software to process the data and prefer the use of a binary file.'
    }
]

SERVE_DOWNLOAD_DIR = os.path.join(BASE_DIR, 'download')

UWS_RESOURCES = []

WORDPRESS_URL = '/cms/'
WORDPRESS_CLI = '/opt/wp-cli/wp'
WORDPRESS_PATH = '/opt/wordpress'
<|endoftext|>


<commit_before>import unittest
import mock
import base64

from tentacle.dht.routing_table import sha1_id

from tentacle.dht import DHT, DHTResponse

class TestDht(unittest.TestCase):

    def setUp(self):
        pass

    # test okay ping response
    def test_ping_01(self):
        # given
        dht = DHT(id_=sha1_id("salt"))
        dht.__create_socket__ = mock.Mock(return_value = None)
        dht.__send_request__ = mock.Mock(return_value = DHTResponse("d1:rd2:id20:mnopqrstuvwxyz123456e1:t2:aa1:y1:re"))

        # when
        result = dht.ping()

        # result
        self.assertEqual("aa", result.transaction_id())
        self.assertTrue(result.is_response())
        self.assertFalse(result.is_query())
        self.assertFalse(result.is_error())

        dic = result.data()
        self.assertEquals("mnopqrstuvwxyz123456", dic['id'])

    # response is real data
    def test_find_node_01(self):
        # given
        dht = DHT(id_=sha1_id("salt"))
        data = base64.b64decode("ZDI6aXA2OjJH1ov8ODE6cmQyOmlkMjA6HbzsI8Zpc1H/Suwpzbqr8vvjRmc1Om5vZGVzNDE2OrOrNbI9GjLI6NHwBTAGDm0MBwGVJUygHJKOn0rEaSOaULMo/u1Afw+X1Kx9Qwq2O7DHLO9b+W0Ap234h2XQmB/m83EiQTK6AbJ8zTFCD3NE622nFPnpiP39xDSfNBbzUR7UBQ3a1ts0gFvoFSKFlQFX2Kd+sj38y8MgoaxPo21McW3cDw/7SZw0R5ypPiy9BNpTbGQzTAK+3k/i+oYBFdHZhQ3e+lYygjlNJc7KtsXKXO1dRUN3NU7JFgICDVphT/GcFQn6i/gtoBoFgeUQVV1yCdEEnO4z5qHs4VFlOBcQy5UqlkOmMh/PqiYcR9iQNuH2ZE4NnBqgoooC6U68st6iF0di2HI5A9M7sKu5l03VuMRzNI3JCZIf2KLwT59VcAviVtAkIkb4kLKbhDnap1TYAx+1OMLqH4S1c80wM7Im3z+h2eT67Bk+mLkMUOm11i/GLHEmg+2eZ2k8oTW8nvfQeuHRuSdPFkNMlnacB1QjGFm4YENjhN1+EU8ixe1l8lxjV3tmCMTeO5l6mJ2bR/b14v3cwoImjU1KsAw7MvBxZTE6dDI6YWExOnkxOnJl")

        dht.__create_socket__ = mock.Mock(return_value = None)
        dht.__send_request__ = mock.Mock(return_value = DHTResponse(data))

        # when
        result = dht.find_node(dht._id)

        # then
        self.assertEqual("aa", result.transaction_id())
        self.assertTrue(result.is_response())
        self.assertFalse(result.is_query())
        self.assertFalse(result.is_error())

        root = dht._routing_table._root
        self.assertEquals(7, len(root._left._bucket._nodes))
        self.assertIsNone(root._left._left)
        self.assertIsNone(root._left._right)

        self.assertEquals(6, len(root._right._left._bucket._nodes))
        self.assertEquals(3, len(root._right._right._bucket._nodes))

if __name__ == '__main__':
    unittest.main()<commit_msg>updated ping test to use real data<commit_after>import unittest
import mock
import base64

from tentacle.dht.routing_table import sha1_id

from tentacle.dht import DHT, DHTResponse

class TestDht(unittest.TestCase):

    def setUp(self):
        pass

    # test okay ping response
    def test_ping_01(self):
        # given
        dht = DHT(id_=sha1_id("salt"))
        data = base64.b64decode("ZDI6aXA2OjJH1ovP/TE6cmQyOmlkMjA6HbzsI8Zpc1H/Suwpzbqr8vvjRmdlMTp0MjphYTE6eTE6cmU=")
        dht.__create_socket__ = mock.Mock(return_value = None)
        dht.__send_request__ = mock.Mock(return_value = DHTResponse(data))

        # when
        result = dht.ping()

        # result
        self.assertEqual("aa", result.transaction_id())
        self.assertTrue(result.is_response())
        self.assertFalse(result.is_query())
        self.assertFalse(result.is_error())

        dic = result.data()
        self.assertEquals("1DBCEC23C6697351FF4AEC29CDBAABF2FBE34667", base64.b16encode(dic['id']))

    # response is real data
    def test_find_node_01(self):
        # given
        dht = DHT(id_=sha1_id("salt"))
        data = base64.b64decode("ZDI6aXA2OjJH1ov8ODE6cmQyOmlkMjA6HbzsI8Zpc1H/Suwpzbqr8vvjRmc1Om5vZGVzNDE2OrOrNbI9GjLI6NHwBTAGDm0MBwGVJUygHJKOn0rEaSOaULMo/u1Afw+X1Kx9Qwq2O7DHLO9b+W0Ap234h2XQmB/m83EiQTK6AbJ8zTFCD3NE622nFPnpiP39xDSfNBbzUR7UBQ3a1ts0gFvoFSKFlQFX2Kd+sj38y8MgoaxPo21McW3cDw/7SZw0R5ypPiy9BNpTbGQzTAK+3k/i+oYBFdHZhQ3e+lYygjlNJc7KtsXKXO1dRUN3NU7JFgICDVphT/GcFQn6i/gtoBoFgeUQVV1yCdEEnO4z5qHs4VFlOBcQy5UqlkOmMh/PqiYcR9iQNuH2ZE4NnBqgoooC6U68st6iF0di2HI5A9M7sKu5l03VuMRzNI3JCZIf2KLwT59VcAviVtAkIkb4kLKbhDnap1TYAx+1OMLqH4S1c80wM7Im3z+h2eT67Bk+mLkMUOm11i/GLHEmg+2eZ2k8oTW8nvfQeuHRuSdPFkNMlnacB1QjGFm4YENjhN1+EU8ixe1l8lxjV3tmCMTeO5l6mJ2bR/b14v3cwoImjU1KsAw7MvBxZTE6dDI6YWExOnkxOnJl")

        dht.__create_socket__ = mock.Mock(return_value = None)
        dht.__send_request__ = mock.Mock(return_value = DHTResponse(data))

        # when
        result = dht.find_node(dht._id)

        # then
        self.assertEqual("aa", result.transaction_id())
        self.assertTrue(result.is_response())
        self.assertFalse(result.is_query())
        self.assertFalse(result.is_error())

        root = dht._routing_table._root
        self.assertEquals(7, len(root._left._bucket._nodes))
        self.assertIsNone(root._left._left)
        self.assertIsNone(root._left._right)

        self.assertEquals(6, len(root._right._left._bucket._nodes))
        self.assertEquals(3, len(root._right._right._bucket._nodes))

if __name__ == '__main__':
    unittest.main()<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
#
# Copyright 2015 Simone Campagna
#
# 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.
#

__author__ = "Simone Campagna"
__all__ = [
    'ValidationResult',
]

import collections

class ValidationResult(object):
    WARNING_MODE_LOG = 'log'
    WARNING_MODE_ERROR = 'error'
    WARNING_MODE_IGNORE = 'ignore'
    WARNING_MODES = (WARNING_MODE_LOG, WARNING_MODE_ERROR, WARNING_MODE_IGNORE)
    WARNING_MODE_DEFAULT = WARNING_MODE_LOG

    ERROR_MODE_LOG = 'log'
    ERROR_MODE_RAISE = 'raise'
    ERROR_MODES = (ERROR_MODE_LOG, ERROR_MODE_RAISE)
    ERROR_MODE_DEFAULT = ERROR_MODE_LOG

    Entry = collections.namedtuple('Entry', ('exc_type', 'message'))
    def __init__(self, logger, warning_mode=WARNING_MODE_DEFAULT, error_mode=ERROR_MODE_DEFAULT):
        self._failing_invoices = set()
        self.logger = logger
        self._errors = collections.OrderedDict()
        self._warnings = collections.OrderedDict()

        if error_mode is None:
            error_mode = self.ERROR_MODE_DEFAULT

        if error_mode == self.ERROR_MODE_LOG:
            self._function_error = self._add_error
        elif error_mode == self.ERROR_MODE_RAISE:
            self._function_error = self._add_critical
        else:
            raise ValueError("error_mode {!r} non valido (i valori leciti sono {})".format(error_mode, '|'.join(self.ERROR_MODES)))

        if warning_mode is None:
            warning_mode = self.WARNING_MODE_DEFAULT

        if warning_mode == self.WARNING_MODE_LOG:
            self._function_warning = self._add_warning
        elif warning_mode == self.WARNING_MODE_ERROR:
            self._function_warning = self._function_error
        elif warning_mode == self.WARNING_MODE_IGNORE:
            self._function_warning = self._ignore
        else:
            raise ValueError("warning_mode {!r} non valido (i valori leciti sono {})".format(warning_mode, '|'.join(self.WARNING_MODES)))

        self.warning_mode = warning_mode
        self.error_mode = error_mode

    def filter_validated_invoices(self, invoices):
        validated_invoices = []
        for invoice in invoices:
            if not invoice.doc_filename in self._failing_invoices:
                validated_invoices.append(invoice)
        return validated_invoices

    def failing_invoices(self):
        return self._failing_invoices

    def _add_critical(self, invoice, exc_type, message):
        self._errors.setdefault(invoice.doc_filename, []).append(self.Entry(exc_type, message))
        self._failing_invoices.add(invoice.doc_filename)
        self.logger.critical(message)
        raise exc_type(message)

    def _add_error(self, invoice, exc_type, message):
        self._errors.setdefault(invoice.doc_filename, []).append(self.Entry(exc_type, message))
        self._failing_invoices.add(invoice.doc_filename)
        self.logger.error(message)

    def _add_warning(self, invoice, exc_type, message):
        self._warnings.setdefault(invoice.doc_filename, []).append(self.Entry(exc_type, message))
        self.logger.warning(message)

    def _ignore(self, invoice, exc_type, message):
        pass

    def add_error(self, invoice, exc_type, message):
        self._function_error(invoice, exc_type, message)

    def add_warning(self, invoice, exc_type, message):
        self._function_warning(invoice, exc_type, message)

    def __bool__(self):
        return len(self._errors) == 0

    def num_errors(self):
        return sum(len(l) for l in self._errors.values())

    def num_warnings(self):
        return sum(len(l) for l in self._warnings.values())

    def errors(self):
        return self._errors

    def warnings(self):
        return self._warnings

    def log_critical(self, invoice, exc_type, message):
        self.logger.critical(message)
        self.add_error(invoice, exc_type, message)

    def log_error(self, invoice, exc_type, message):
        self.logger.error(message)
        self.add_error(invoice, exc_type, message)

    def log_warning(self, invoice, exc_type, message):
        self.logger.warning(message)
        self.add_warning(invoice, exc_type, message)

    def log_ignore(self, invoice, exc_type, message):
        pass
<commit_msg>unused functions removed<commit_after># -*- coding: utf-8 -*-
#
# Copyright 2015 Simone Campagna
#
# 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.
#

__author__ = "Simone Campagna"
__all__ = [
    'ValidationResult',
]

import collections

class ValidationResult(object):
    WARNING_MODE_LOG = 'log'
    WARNING_MODE_ERROR = 'error'
    WARNING_MODE_IGNORE = 'ignore'
    WARNING_MODES = (WARNING_MODE_LOG, WARNING_MODE_ERROR, WARNING_MODE_IGNORE)
    WARNING_MODE_DEFAULT = WARNING_MODE_LOG

    ERROR_MODE_LOG = 'log'
    ERROR_MODE_RAISE = 'raise'
    ERROR_MODES = (ERROR_MODE_LOG, ERROR_MODE_RAISE)
    ERROR_MODE_DEFAULT = ERROR_MODE_LOG

    Entry = collections.namedtuple('Entry', ('exc_type', 'message'))
    def __init__(self, logger, warning_mode=WARNING_MODE_DEFAULT, error_mode=ERROR_MODE_DEFAULT):
        self._failing_invoices = set()
        self.logger = logger
        self._errors = collections.OrderedDict()
        self._warnings = collections.OrderedDict()

        if error_mode is None: # pragma: no cover
            error_mode = self.ERROR_MODE_DEFAULT

        if error_mode == self.ERROR_MODE_LOG:
            self._function_error = self._add_error
        elif error_mode == self.ERROR_MODE_RAISE:
            self._function_error = self._add_critical
        else: # pragma: no cover
            raise ValueError("error_mode {!r} non valido (i valori leciti sono {})".format(error_mode, '|'.join(self.ERROR_MODES)))

        if warning_mode is None: # pragma: no cover
            warning_mode = self.WARNING_MODE_DEFAULT

        if warning_mode == self.WARNING_MODE_LOG:
            self._function_warning = self._add_warning
        elif warning_mode == self.WARNING_MODE_ERROR:
            self._function_warning = self._function_error
        elif warning_mode == self.WARNING_MODE_IGNORE:
            self._function_warning = self._ignore
        else: # pragma: no cover
            raise ValueError("warning_mode {!r} non valido (i valori leciti sono {})".format(warning_mode, '|'.join(self.WARNING_MODES)))

        self.warning_mode = warning_mode
        self.error_mode = error_mode

    def filter_validated_invoices(self, invoices):
        validated_invoices = []
        for invoice in invoices:
            if not invoice.doc_filename in self._failing_invoices:
                validated_invoices.append(invoice)
        return validated_invoices

    def failing_invoices(self):
        return self._failing_invoices

    def _add_critical(self, invoice, exc_type, message):
        self._errors.setdefault(invoice.doc_filename, []).append(self.Entry(exc_type, message))
        self._failing_invoices.add(invoice.doc_filename)
        self.logger.critical(message)
        raise exc_type(message)

    def _add_error(self, invoice, exc_type, message):
        self._errors.setdefault(invoice.doc_filename, []).append(self.Entry(exc_type, message))
        self._failing_invoices.add(invoice.doc_filename)
        self.logger.error(message)

    def _add_warning(self, invoice, exc_type, message):
        self._warnings.setdefault(invoice.doc_filename, []).append(self.Entry(exc_type, message))
        self.logger.warning(message)

    def _ignore(self, invoice, exc_type, message):
        pass

    def add_error(self, invoice, exc_type, message):
        self._function_error(invoice, exc_type, message)

    def add_warning(self, invoice, exc_type, message):
        self._function_warning(invoice, exc_type, message)

    def __bool__(self):
        return len(self._errors) == 0

    def num_errors(self):
        return sum(len(l) for l in self._errors.values())

    def num_warnings(self):
        return sum(len(l) for l in self._warnings.values())

    def errors(self):
        return self._errors

    def warnings(self):
        return self._warnings
<|endoftext|>


<commit_before>

def validate_direction(direction):
    splitted = direction.split(':')
    direction_name = direction
    if len(splitted) > 2:
        return False
    if len(splitted) <= 2:
        direction_name = splitted[0]
    return direction_name in (
        "Across",
        "Down",
        "Diagonal",
        "Diagonal Up",
        "Diagonal Down Left",
        "Diagonal Up Left",
        "Zones",
        "Clues",
    )
<commit_msg>Minor change in logic for direction name calculation<commit_after>

def validate_direction(direction):
    splitted = direction.split(':')
    if len(splitted) > 2:
        return False
    direction_name = direction
    if len(splitted) <= 2:
        direction_name = splitted[0]
    return direction_name in (
        "Across",
        "Down",
        "Diagonal",
        "Diagonal Up",
        "Diagonal Down Left",
        "Diagonal Up Left",
        "Zones",
        "Clues",
    )
<|endoftext|>


<commit_before>import os
import tee
import pkg_resources

from os.path import abspath, join, isfile

from python import Python
from process import Process
from configparser import ConfigParser
from chdir import chdir
from exit import err_exit, warn

class Setuptools(object):
    """Interface to setuptools."""

    def __init__(self, process=None):
        self.process = process or Process(env=self.get_env())
        self.python = Python()

    def get_env(self):
        # Make sure setuptools and its extensions are found if mkrelease
        # has been installed with zc.buildout
        path = []
        for name in ('setuptools', 'setuptools-hg', 'setuptools-git',
                     'setuptools-subversion'):
            try:
                dist = pkg_resources.get_distribution(name)
            except pkg_resources.DistributionNotFound:
                continue
            path.append(dist.location)
        env = os.environ.copy()
        env['PYTHONPATH'] = ':'.join(path)
        env['HG_SETUPTOOLS_FORCE_CMD'] = '1'
        return env

    def is_valid_package(self, dir):
        return isfile(join(dir, 'setup.py'))

    def check_valid_package(self, dir):
        if not self.is_valid_package(dir):
            err_exit('No setup.py found in %(dir)s' % locals())

    @chdir
    def get_package_info(self, dir, develop=False):
        python = self.python
        rc, lines = self.process.popen(
            '"%(python)s" setup.py --name --version' % locals(), echo=False)
        if rc == 0 and len(lines) == 2:
            name, version = lines
            if develop:
                parser = ConfigParser(warn)
                parser.read('setup.cfg')
                version += parser.get('egg_info', 'tag_build', '').strip()
            return name, pkg_resources.safe_version(version)
        err_exit('Bad setup.py')

    @chdir
    def run_egg_info(self, dir, infoflags, scmtype='', quiet=False):
        if not self.process.quiet:
            print 'running egg_info'

        echo = tee.After('running egg_info')
        if quiet:
            echo = tee.And(echo, tee.StartsWith('running'))

        rc, lines = self._run_setup_py(
            ['egg_info'] + infoflags,
            echo=echo,
            scmtype=scmtype)

        if rc == 0:
            filename = self._parse_egg_info_results(lines)
            if filename and isfile(filename):
                return abspath(filename)
        err_exit('egg_info failed')

    @chdir
    def run_dist(self, dir, infoflags, distcmd, distflags, scmtype='', quiet=False):
        if not self.process.quiet:
            print 'running', distcmd

        echo = tee.After('running %(distcmd)s' % locals())
        if quiet:
            echo = tee.And(echo, tee.StartsWith('running'))

        rc, lines = self._run_setup_py(
            ['egg_info'] + infoflags + [distcmd] + distflags,
            echo=echo,
            scmtype=scmtype)

        if rc == 0:
            filename = self._parse_dist_results(lines)
            if filename and isfile(filename):
                return abspath(filename)
        err_exit('%(distcmd)s failed' % locals())

    @chdir
    def run_register(self, dir, infoflags, location, scmtype='', quiet=False):
        if not self.process.quiet:
            print 'running register'

        echo = tee.After('running register')
        if quiet:
            echo = tee.And(echo, tee.Not(tee.StartsWith('Registering')))

        serverflags = ['--repository="%s"' % location]

        rc, lines = self._run_setup_py(
            ['egg_info'] + infoflags + ['register'] + serverflags,
            echo=echo,
            scmtype=scmtype)

        if rc == 0:
            if self._parse_register_results(lines):
                return rc
        err_exit('register failed')

    @chdir
    def run_upload(self, dir, infoflags, distcmd, distflags, location, uploadflags, scmtype='', quiet=False):
        if not self.process.quiet:
            print 'running upload'

        echo = tee.After('running upload')
        if quiet:
            echo = tee.And(echo, tee.Not(tee.StartsWith('Submitting')))

        serverflags = ['--repository="%s"' % location]

        rc, lines = self._run_setup_py(
            ['egg_info'] + infoflags + [distcmd] + distflags +
            ['upload'] + serverflags + uploadflags,
            echo=echo,
            scmtype=scmtype)

        if rc == 0:
            if self._parse_upload_results(lines):
                return rc
        err_exit('upload failed')

    def _run_setup_py(self, args, echo=True, echo2=True, scmtype=''):
        """Run setup.py with monkey-patched setuptools.

        The patch forces setuptools to only use file-finders for the
        selected 'scmtype'.

        'args' contains the *list* of arguments that should be passed
        to setup.py.

        If 'scmtype' is the empty string, the patch is not applied.
        """
        python = self.python

        if scmtype:
            patched = SCM_CHOOSER % locals()
            setup_py = '-c"%(patched)s"' % locals()
        else:
            setup_py = 'setup.py %s' % ' '.join(args)

        rc, lines = self.process.popen(
            '"%(python)s" -B %(setup_py)s' % locals(), echo=echo, echo2=echo2)

        return rc, lines

    def _parse_egg_info_results(self, lines):
        for line in lines:
            if line.startswith("writing manifest file '"):
                return line.split("'")[1]
        return ''

    def _parse_dist_results(self, lines):
        # This relies on --formats=zip or --formats=egg
        for line in lines:
            if line.startswith("creating '") and "' and adding '" in line:
                return line.split("'")[1]
        return ''

    def _parse_register_results(self, lines):
        current, expect = None, 'running register'
        for line in lines:
            if line == expect:
                if line != 'Server response (200): OK':
                    current, expect = expect, 'Server response (200): OK'
                else:
                    if current == 'running register':
                        return True
        return False

    def _parse_upload_results(self, lines):
        current, expect = None, 'running upload'
        for line in lines:
            if line == expect:
                if line != 'Server response (200): OK':
                    current, expect = expect, 'Server response (200): OK'
                else:
                    if current == 'running upload':
                        return True
        return False

SCM_CHOOSER = """\
import os, sys
import distutils
import pkg_resources

from os.path import basename

class pythonpath_off(object):
    def __enter__(self):
        self.saved = os.environ.get('PYTHONPATH', '')
        if self.saved:
            del os.environ['PYTHONPATH']
    def __exit__(self, *ignored):
        if self.saved:
            os.environ['PYTHONPATH'] = self.saved

def walk_revctrl(dirname=''):
    file_finder = None
    items = []
    for ep in pkg_resources.iter_entry_points('setuptools.file_finders'):
        if %(scmtype)r == ep.name:
            distutils.log.info('using %%s file-finder', ep.name)
            file_finder = ep.load()
            finder_items = []
            with pythonpath_off():
                for item in file_finder(dirname):
                    if not basename(item).startswith(('.svn', '.hg', '.git')):
                        finder_items.append(item)
            distutils.log.info('%%d files found', len(finder_items))
            items.extend(finder_items)
    if file_finder is None:
        print >>sys.stderr, 'No %(scmtype)s file-finder; ' \
            'setuptools-%(scmtype)s extension missing?'
        sys.exit(1)
    if not items:
        sys.exit(1)
    return items

import setuptools.command.egg_info
setuptools.command.egg_info.walk_revctrl = walk_revctrl

sys.argv = ['setup.py'] + %(args)r
import setup
"""

<commit_msg>Rename constant SCM_CHOOSER -> WALK_REVCTRL.<commit_after>import os
import tee
import pkg_resources

from os.path import abspath, join, isfile

from python import Python
from process import Process
from configparser import ConfigParser
from chdir import chdir
from exit import err_exit, warn

class Setuptools(object):
    """Interface to setuptools."""

    def __init__(self, process=None):
        self.process = process or Process(env=self.get_env())
        self.python = Python()

    def get_env(self):
        # Make sure setuptools and its extensions are found if mkrelease
        # has been installed with zc.buildout
        path = []
        for name in ('setuptools', 'setuptools-hg', 'setuptools-git',
                     'setuptools-subversion'):
            try:
                dist = pkg_resources.get_distribution(name)
            except pkg_resources.DistributionNotFound:
                continue
            path.append(dist.location)
        env = os.environ.copy()
        env['PYTHONPATH'] = ':'.join(path)
        env['HG_SETUPTOOLS_FORCE_CMD'] = '1'
        return env

    def is_valid_package(self, dir):
        return isfile(join(dir, 'setup.py'))

    def check_valid_package(self, dir):
        if not self.is_valid_package(dir):
            err_exit('No setup.py found in %(dir)s' % locals())

    @chdir
    def get_package_info(self, dir, develop=False):
        python = self.python
        rc, lines = self.process.popen(
            '"%(python)s" setup.py --name --version' % locals(), echo=False)
        if rc == 0 and len(lines) == 2:
            name, version = lines
            if develop:
                parser = ConfigParser(warn)
                parser.read('setup.cfg')
                version += parser.get('egg_info', 'tag_build', '').strip()
            return name, pkg_resources.safe_version(version)
        err_exit('Bad setup.py')

    @chdir
    def run_egg_info(self, dir, infoflags, scmtype='', quiet=False):
        if not self.process.quiet:
            print 'running egg_info'

        echo = tee.After('running egg_info')
        if quiet:
            echo = tee.And(echo, tee.StartsWith('running'))

        rc, lines = self._run_setup_py(
            ['egg_info'] + infoflags,
            echo=echo,
            scmtype=scmtype)

        if rc == 0:
            filename = self._parse_egg_info_results(lines)
            if filename and isfile(filename):
                return abspath(filename)
        err_exit('egg_info failed')

    @chdir
    def run_dist(self, dir, infoflags, distcmd, distflags, scmtype='', quiet=False):
        if not self.process.quiet:
            print 'running', distcmd

        echo = tee.After('running %(distcmd)s' % locals())
        if quiet:
            echo = tee.And(echo, tee.StartsWith('running'))

        rc, lines = self._run_setup_py(
            ['egg_info'] + infoflags + [distcmd] + distflags,
            echo=echo,
            scmtype=scmtype)

        if rc == 0:
            filename = self._parse_dist_results(lines)
            if filename and isfile(filename):
                return abspath(filename)
        err_exit('%(distcmd)s failed' % locals())

    @chdir
    def run_register(self, dir, infoflags, location, scmtype='', quiet=False):
        if not self.process.quiet:
            print 'running register'

        echo = tee.After('running register')
        if quiet:
            echo = tee.And(echo, tee.Not(tee.StartsWith('Registering')))

        serverflags = ['--repository="%s"' % location]

        rc, lines = self._run_setup_py(
            ['egg_info'] + infoflags + ['register'] + serverflags,
            echo=echo,
            scmtype=scmtype)

        if rc == 0:
            if self._parse_register_results(lines):
                return rc
        err_exit('register failed')

    @chdir
    def run_upload(self, dir, infoflags, distcmd, distflags, location, uploadflags, scmtype='', quiet=False):
        if not self.process.quiet:
            print 'running upload'

        echo = tee.After('running upload')
        if quiet:
            echo = tee.And(echo, tee.Not(tee.StartsWith('Submitting')))

        serverflags = ['--repository="%s"' % location]

        rc, lines = self._run_setup_py(
            ['egg_info'] + infoflags + [distcmd] + distflags +
            ['upload'] + serverflags + uploadflags,
            echo=echo,
            scmtype=scmtype)

        if rc == 0:
            if self._parse_upload_results(lines):
                return rc
        err_exit('upload failed')

    def _run_setup_py(self, args, echo=True, echo2=True, scmtype=''):
        """Run setup.py with monkey-patched setuptools.

        The patch forces setuptools to only use file-finders for the
        selected 'scmtype'.

        'args' contains the *list* of arguments that should be passed
        to setup.py.

        If 'scmtype' is the empty string, the patch is not applied.
        """
        python = self.python

        if scmtype:
            patched = WALK_REVCTRL % locals()
            setup_py = '-c"%(patched)s"' % locals()
        else:
            setup_py = 'setup.py %s' % ' '.join(args)

        rc, lines = self.process.popen(
            '"%(python)s" -B %(setup_py)s' % locals(), echo=echo, echo2=echo2)

        return rc, lines

    def _parse_egg_info_results(self, lines):
        for line in lines:
            if line.startswith("writing manifest file '"):
                return line.split("'")[1]
        return ''

    def _parse_dist_results(self, lines):
        # This relies on --formats=zip or --formats=egg
        for line in lines:
            if line.startswith("creating '") and "' and adding '" in line:
                return line.split("'")[1]
        return ''

    def _parse_register_results(self, lines):
        current, expect = None, 'running register'
        for line in lines:
            if line == expect:
                if line != 'Server response (200): OK':
                    current, expect = expect, 'Server response (200): OK'
                else:
                    if current == 'running register':
                        return True
        return False

    def _parse_upload_results(self, lines):
        current, expect = None, 'running upload'
        for line in lines:
            if line == expect:
                if line != 'Server response (200): OK':
                    current, expect = expect, 'Server response (200): OK'
                else:
                    if current == 'running upload':
                        return True
        return False

WALK_REVCTRL = """\
import os, sys
import distutils
import pkg_resources

from os.path import basename

class pythonpath_off(object):
    def __enter__(self):
        self.saved = os.environ.get('PYTHONPATH', '')
        if self.saved:
            del os.environ['PYTHONPATH']
    def __exit__(self, *ignored):
        if self.saved:
            os.environ['PYTHONPATH'] = self.saved

def walk_revctrl(dirname=''):
    file_finder = None
    items = []
    for ep in pkg_resources.iter_entry_points('setuptools.file_finders'):
        if %(scmtype)r == ep.name:
            distutils.log.info('using %%s file-finder', ep.name)
            file_finder = ep.load()
            finder_items = []
            with pythonpath_off():
                for item in file_finder(dirname):
                    if not basename(item).startswith(('.svn', '.hg', '.git')):
                        finder_items.append(item)
            distutils.log.info('%%d files found', len(finder_items))
            items.extend(finder_items)
    if file_finder is None:
        print >>sys.stderr, 'No %(scmtype)s file-finder; ' \
            'setuptools-%(scmtype)s extension missing?'
        sys.exit(1)
    if not items:
        sys.exit(1)
    return items

import setuptools.command.egg_info
setuptools.command.egg_info.walk_revctrl = walk_revctrl

sys.argv = ['setup.py'] + %(args)r
import setup
"""

<|endoftext|>