<commit_before>from __future__ import absolute_import
import numpy as np
import scipy as sp
from six.moves import range
from six.moves import zip
from .. import backend as K

def to_categorical(y, nb_classes=None):
    '''Convert class vector (integers from 0 to nb_classes) to binary class matrix, for use with categorical_crossentropy.

    # Arguments
        y: class vector to be converted into a matrix
        nb_classes: total number of classes

    # Returns
        A binary matrix representation of the input.
    '''
    if not nb_classes:
        nb_classes = np.max(y)+1
    Y = np.zeros((len(y), nb_classes))
    for i in range(len(y)):
        Y[i, y[i]] = 1.
    return Y

def normalize(a, axis=-1, order=2):
    l2 = np.atleast_1d(np.linalg.norm(a, order, axis))
    l2[l2 == 0] = 1
    return a / np.expand_dims(l2, axis)

def binary_logloss(p, y):
    epsilon = 1e-15
    p = sp.maximum(epsilon, p)
    p = sp.minimum(1-epsilon, p)
    res = sum(y * sp.log(p) + sp.subtract(1, y) * sp.log(sp.subtract(1, p)))
    res *= -1.0/len(y)
    return res

def multiclass_logloss(P, Y):
    npreds = [P[i][Y[i]-1] for i in range(len(Y))]
    score = -(1. / len(Y)) * np.sum(np.log(npreds))
    return score

def accuracy(p, y):
    return np.mean([a == b for a, b in zip(p, y)])

def probas_to_classes(y_pred):
    if len(y_pred.shape) > 1 and y_pred.shape[1] > 1:
        return categorical_probas_to_classes(y_pred)
    return np.array([1 if p > 0.5 else 0 for p in y_pred])

def categorical_probas_to_classes(p):
    return np.argmax(p, axis=1)

def convert_kernel(kernel, dim_ordering='default'):
    '''Converts a kernel matrix (Numpy array)
    from Theano format to TensorFlow format
    (or reciprocally, since the transformation
    is its own inverse).
    '''
    if dim_ordering == 'default':
        dim_ordering = K.image_dim_ordering()
    new_kernel = np.copy(kernel)
    if kernel.ndim == 4:
        # conv 2d
        # TH kernel shape: (depth, input_depth, rows, cols)
        # TF kernel shape: (rows, cols, input_depth, depth)
        if dim_ordering == 'th':
            w = kernel.shape[2]
            h = kernel.shape[3]
            for i in range(w):
                for j in range(h):
                    new_kernel[:, :, i, j] = kernel[:, :, w - i - 1, h - j - 1]
        elif dim_ordering == 'tf':
            w = kernel.shape[0]
            h = kernel.shape[1]
            for i in range(w):
                for j in range(h):
                    new_kernel[i, j, :, :] = kernel[w - i - 1, h - j - 1, :, :]
        else:
            raise Exception('Invalid dim_ordering: ' + str(dim_ordering))
    elif kernel.ndim == 5:
        # conv 3d
        # TH kernel shape: (out_depth, input_depth, kernel_dim1, kernel_dim2, kernel_dim3)
        # TF kernel shape: (kernel_dim1, kernel_dim2, kernel_dim3, input_depth, out_depth)
        if dim_ordering == 'th':
            w = kernel.shape[2]
            h = kernel.shape[3]
            z = kernel.shape[4]
            for i in range(w):
                for j in range(h):
                    for k in range(z):
                        new_kernel[:, :, i, j, k] = kernel[:, :,
                                                           w - i - 1,
                                                           h - j - 1,
                                                           z - k - 1]
        elif dim_ordering == 'tf':
            w = kernel.shape[0]
            h = kernel.shape[1]
            z = kernel.shape[2]
            for i in range(w):
                for j in range(h):
                    for k in range(z):
                        new_kernel[i, j, k, :, :] = kernel[w - i - 1,
                                                           h - j - 1,
                                                           z - k - 1,
                                                           :, :]
        else:
            raise Exception('Invalid dim_ordering: ' + str(dim_ordering))
    else:
        raise ValueError('Invalid kernel shape:', kernel.shape)
    return new_kernel

def conv_output_length(input_length, filter_size, border_mode, stride, dilation=1):
    if input_length is None:
        return None
    assert border_mode in {'same', 'valid', 'full'}
    dilated_filter_size = filter_size + (filter_size - 1) * (dilation - 1)
    if border_mode == 'same':
        output_length = input_length
    elif border_mode == 'valid':
        output_length = input_length - dilated_filter_size + 1
    elif border_mode == 'full':
        output_length = input_length + dilated_filter_size - 1
    return (output_length + stride - 1) // stride

def conv_input_length(output_length, filter_size, border_mode, stride):
    if output_length is None:
        return None
    assert border_mode in {'same', 'valid', 'full'}
    if border_mode == 'same':
        pad = filter_size // 2
    elif border_mode == 'valid':
        pad = 0
    elif border_mode == 'full':
        pad = filter_size - 1
    return (output_length - 1) * stride - 2 * pad + filter_size
<commit_msg>Fix deprecation warning in `to_categorical` (#4547)<commit_after>from __future__ import absolute_import
import numpy as np
import scipy as sp
from six.moves import range
from six.moves import zip
from .. import backend as K

def to_categorical(y, nb_classes=None):
    '''Convert class vector (integers from 0 to nb_classes) to binary class matrix, for use with categorical_crossentropy.

    # Arguments
        y: class vector to be converted into a matrix
        nb_classes: total number of classes

    # Returns
        A binary matrix representation of the input.
    '''
    y = np.array(y, dtype='int')
    if not nb_classes:
        nb_classes = np.max(y)+1
    Y = np.zeros((len(y), nb_classes))
    for i in range(len(y)):
        Y[i, y[i]] = 1.
    return Y

def normalize(a, axis=-1, order=2):
    l2 = np.atleast_1d(np.linalg.norm(a, order, axis))
    l2[l2 == 0] = 1
    return a / np.expand_dims(l2, axis)

def binary_logloss(p, y):
    epsilon = 1e-15
    p = sp.maximum(epsilon, p)
    p = sp.minimum(1-epsilon, p)
    res = sum(y * sp.log(p) + sp.subtract(1, y) * sp.log(sp.subtract(1, p)))
    res *= -1.0/len(y)
    return res

def multiclass_logloss(P, Y):
    npreds = [P[i][Y[i]-1] for i in range(len(Y))]
    score = -(1. / len(Y)) * np.sum(np.log(npreds))
    return score

def accuracy(p, y):
    return np.mean([a == b for a, b in zip(p, y)])

def probas_to_classes(y_pred):
    if len(y_pred.shape) > 1 and y_pred.shape[1] > 1:
        return categorical_probas_to_classes(y_pred)
    return np.array([1 if p > 0.5 else 0 for p in y_pred])

def categorical_probas_to_classes(p):
    return np.argmax(p, axis=1)

def convert_kernel(kernel, dim_ordering='default'):
    '''Converts a kernel matrix (Numpy array)
    from Theano format to TensorFlow format
    (or reciprocally, since the transformation
    is its own inverse).
    '''
    if dim_ordering == 'default':
        dim_ordering = K.image_dim_ordering()
    new_kernel = np.copy(kernel)
    if kernel.ndim == 4:
        # conv 2d
        # TH kernel shape: (depth, input_depth, rows, cols)
        # TF kernel shape: (rows, cols, input_depth, depth)
        if dim_ordering == 'th':
            w = kernel.shape[2]
            h = kernel.shape[3]
            for i in range(w):
                for j in range(h):
                    new_kernel[:, :, i, j] = kernel[:, :, w - i - 1, h - j - 1]
        elif dim_ordering == 'tf':
            w = kernel.shape[0]
            h = kernel.shape[1]
            for i in range(w):
                for j in range(h):
                    new_kernel[i, j, :, :] = kernel[w - i - 1, h - j - 1, :, :]
        else:
            raise Exception('Invalid dim_ordering: ' + str(dim_ordering))
    elif kernel.ndim == 5:
        # conv 3d
        # TH kernel shape: (out_depth, input_depth, kernel_dim1, kernel_dim2, kernel_dim3)
        # TF kernel shape: (kernel_dim1, kernel_dim2, kernel_dim3, input_depth, out_depth)
        if dim_ordering == 'th':
            w = kernel.shape[2]
            h = kernel.shape[3]
            z = kernel.shape[4]
            for i in range(w):
                for j in range(h):
                    for k in range(z):
                        new_kernel[:, :, i, j, k] = kernel[:, :,
                                                           w - i - 1,
                                                           h - j - 1,
                                                           z - k - 1]
        elif dim_ordering == 'tf':
            w = kernel.shape[0]
            h = kernel.shape[1]
            z = kernel.shape[2]
            for i in range(w):
                for j in range(h):
                    for k in range(z):
                        new_kernel[i, j, k, :, :] = kernel[w - i - 1,
                                                           h - j - 1,
                                                           z - k - 1,
                                                           :, :]
        else:
            raise Exception('Invalid dim_ordering: ' + str(dim_ordering))
    else:
        raise ValueError('Invalid kernel shape:', kernel.shape)
    return new_kernel

def conv_output_length(input_length, filter_size, border_mode, stride, dilation=1):
    if input_length is None:
        return None
    assert border_mode in {'same', 'valid', 'full'}
    dilated_filter_size = filter_size + (filter_size - 1) * (dilation - 1)
    if border_mode == 'same':
        output_length = input_length
    elif border_mode == 'valid':
        output_length = input_length - dilated_filter_size + 1
    elif border_mode == 'full':
        output_length = input_length + dilated_filter_size - 1
    return (output_length + stride - 1) // stride

def conv_input_length(output_length, filter_size, border_mode, stride):
    if output_length is None:
        return None
    assert border_mode in {'same', 'valid', 'full'}
    if border_mode == 'same':
        pad = filter_size // 2
    elif border_mode == 'valid':
        pad = 0
    elif border_mode == 'full':
        pad = filter_size - 1
    return (output_length - 1) * stride - 2 * pad + filter_size
<|endoftext|>


<commit_before>'''
Get the current target values for diagnosis, treatment, viral suppression,
and vaccination from the overall target goals.
'''

import numpy

from . import control_rates
from . import proportions
from . import simulation

class OneTargetZero:
    '''
    Fixed at 0.
    '''
    def __call__(self, initial_proportion, t):
        return numpy.zeros_like(t, dtype = float)

class OneTargetStatusQuo:
    '''
    Fixed at the `initial_proportion`.
    '''
    def __call__(self, initial_proportion, t):
        return initial_proportion * numpy.ones_like(t, dtype = float)

class OneTargetLinear:
    '''
    Linearly go from the `initial_proportion` to
    `max(target_value, initial_proportion)` between `time_to_start` and
    `time_to_target`.  Stay fixed at initial proportion before
    `time_to_start` and fixed at `target_value` after `time_to_target`.
    '''
    def __init__(self, target_value, time_to_start, time_to_target):
        self.target_value = target_value
        self.time_to_start = time_to_start
        self.time_to_target = time_to_target

    def __call__(self, initial_proportion, t):
        target_value_ = max(self.target_value, initial_proportion)
        amount_implemented = numpy.where(
            t < self.time_to_start, 0,
            numpy.where(
                t < self.time_to_target,
                (t - self.time_to_start)
                / (self.time_to_target - self.time_to_start),
                1))
        return (initial_proportion
                + (target_value_ - initial_proportion) * amount_implemented)

class OneTarget90(OneTargetLinear):
    '''
    Linearly go from the `initial_proportion` to the
    `target_value = max(90%, initial_proportion)` between 2015 and
    2020.  Stay fixed at `initial_proportion` before
    2015 and fixed at the `target_value` after 2020.
    '''
    target_value = 0.9
    time_to_start = 2015
    time_to_target = 2020

    def __init__(self):
        pass

class OneTarget95:
    '''
    Linearly go from the `initial_proportion` to
    `target_value_0 = max(90%, initial_proportion)` between 2015 and
    2020, then linearly go to
    `target_value_1 = max(95%, initial_proportion)` between 2020 and 2030.
    Stay fixed at `initial_proportion` before 2015 and fixed at
    `target_value_1` after 2030.
    '''
    target_value_0 = 0.9
    target_value_1 = 0.95
    time_0 = 2015
    time_1 = 2020
    time_2 = 2030

    def __call__(self, initial_proportion, t):
        target_value_0_ = max(self.target_value_0, initial_proportion)
        target_value_1_ = max(self.target_value_1, initial_proportion)
        amount_implemented_0 = numpy.where(
            t < self.time_0, 0,
            numpy.where(
                t < self.time_1,
                (t - self.time_0) / (self.time_1 - self.time_0),
                1))
        amount_implemented_1 = numpy.where(
            t < self.time_1, 0,
            numpy.where(
                t < self.time_2,
                (t - self.time_1) / (self.time_2 - self.time_1),
                1))
        return (initial_proportion
                + (target_value_0_ - initial_proportion) * amount_implemented_0
                + (target_value_1_ - target_value_0_) * amount_implemented_1)

class Target:
    '''
    Base type for target for diagnosis, treatment, viral suppression,
    and vaccination.
    '''

    diagnosed = None
    treated = None
    suppressed = None
    vaccinated = None

    vaccine_efficacy = 0

    def __call__(self, t, parameters):
        '''
        Get numerical values for the target at different points in time.
        '''
        t = numpy.asarray(t)
        initial_proportions = proportions.get(parameters.initial_conditions)
        names = initial_proportions.dtype.names
        arrays = []
        for n in names:
            targ = getattr(self, n)
            ip = getattr(initial_proportions, n)
            arrays.append(targ(ip, t))
        return numpy.rec.fromarrays(arrays, names = names)

    @classmethod
    def __str__(cls):
        return cls.__name__

    def __repr__(self):
        return '{}.{}()'.format(self.__class__.__module__,
                                self.__class__.__name__)

class Zero(Target):
    '''
    All zero.
    '''
    diagnosed = OneTargetZero()
    treated = OneTargetZero()
    suppressed = OneTargetZero()
    vaccinated = OneTargetZero()

    @staticmethod
    def __str__():
        return 'All Zeroes'

class StatusQuo(Target):
    '''
    Fixed at the initial proportion with no vaccination.
    '''
    diagnosed = OneTargetStatusQuo()
    treated = OneTargetStatusQuo()
    suppressed = OneTargetStatusQuo()
    vaccinated = OneTargetZero()

    @staticmethod
    def __str__():
        return 'Status Quo'

class UNAIDS90(Target):
    '''
    90--90--90 target with no vaccination.
    '''
    diagnosed = OneTarget90()
    treated = OneTarget90()
    suppressed = OneTarget90()
    vaccinated = OneTargetZero()

    @staticmethod
    def __str__():
        return '90–90–90'

class UNAIDS95(Target):
    '''
    95--95--95 target with no vaccination.
    '''
    diagnosed = OneTarget95()
    treated = OneTarget95()
    suppressed = OneTarget95()
    vaccinated = OneTargetZero()

    @staticmethod
    def __str__():
        return '95–95–95'

class Vaccine(Target):
    '''
    Vaccine plus the `treatment_target`.
    '''
    def __init__(self,
                 treatment_target = UNAIDS95,
                 efficacy = 0.5,
                 coverage = 0.7,
                 time_to_start = 2020,
                 time_to_fifty_percent = 2):
        self._efficacy = efficacy
        self._coverage = coverage
        self._time_to_start = time_to_start
        self._time_to_fifty_percent = time_to_fifty_percent
        self._treatment_target = treatment_target

        self.diagnosed = self._treatment_target.diagnosed
        self.treated = self._treatment_target.treated
        self.suppressed = self._treatment_target.suppressed

        # Set vaccine efficacy
        self.vaccine_efficacy = self._efficacy

        time_to_target = (self._coverage / 0.5 * self._time_to_fifty_percent
                          + self._time_to_start)
        self.vaccinated = OneTargetLinear(self._coverage,
                                          self._time_to_start,
                                          time_to_target)
        self.vaccinated.time_to_fifty_percent = self._time_to_fifty_percent

    def __repr__(self):
        if isinstance(self._treatment_target, Target):
            treatment_str = repr(self._treatment_target)
        elif issubclass(self._treatment_target, Target):
            treatment_str = '{}.{}'.format(self._treatment_target.__module__,
                                           self._treatment_target.__name__)
        else:
            raise ValueError

        params = [
            'efficacy={}'.format(self._efficacy),
            'coverage={}'.format(self._coverage),
            'time_to_start={}'.format(self._time_to_start),
            'time_to_fifty_percent={}'.format(self._time_to_fifty_percent),
            'treatment_target={}'.format(treatment_str)]

        return '{}.{}({})'.format(self.__class__.__module__,
                                  self.__class__.__name__,
                                  ', '.join(params))

    def __str__(self):
        if isinstance(self._treatment_target, Target):
            treatment_str = str(self._treatment_target)
        elif issubclass(self._treatment_target, Target):
            treatment_str = self._treatment_target.__str__()
        else:
            raise ValueError

        params = [
            'efficacy={:g}%'.format(100 * self._efficacy),
            'coverage={:g}%'.format(100 * self._coverage),
            'time_to_start={}'.format(self._time_to_start),
            'time_to_fifty_percent={}y'.format(self._time_to_fifty_percent)]

        return '{} + {}({})'.format(treatment_str,
                                    super().__str__(),
                                    ', '.join(params))

# Build each of these and each of these + vaccine.
_all_baselines = [StatusQuo(),
                  UNAIDS90(),
                  UNAIDS95()]
all_ = []
for target in _all_baselines:
    all_.extend([target,
                 Vaccine(treatment_target = target)])

_vaccine_scenarios_baselines = [StatusQuo()]
vaccine_scenarios = []
for target in _vaccine_scenarios_baselines:
    vaccine_scenarios.extend([
        target,
        Vaccine(treatment_target = target),
        Vaccine(treatment_target = target, efficacy = 0.3),
        Vaccine(treatment_target = target, efficacy = 0.7),
        Vaccine(treatment_target = target, coverage = 0.5),
        Vaccine(treatment_target = target, coverage = 0.9),
        Vaccine(treatment_target = target, time_to_start = 2025),
        Vaccine(treatment_target = target, time_to_fifty_percent = 5)])
<commit_msg>Cleanup.<commit_after>'''
Get the current target values for diagnosis, treatment, viral suppression,
and vaccination from the overall target goals.
'''

import numpy

from . import control_rates
from . import proportions
from . import simulation

class OneTargetZero:
    '''
    Fixed at 0.
    '''
    def __call__(self, initial_proportion, t):
        return numpy.zeros_like(t, dtype = float)

class OneTargetStatusQuo:
    '''
    Fixed at the `initial_proportion`.
    '''
    def __call__(self, initial_proportion, t):
        return initial_proportion * numpy.ones_like(t, dtype = float)

class OneTargetLinear:
    '''
    Linearly go from the `initial_proportion` to
    `max(target_value, initial_proportion)` between `time_to_start` and
    `time_to_target`.  Stay fixed at initial proportion before
    `time_to_start` and fixed at `target_value` after `time_to_target`.
    '''
    def __init__(self, target_value, time_to_start, time_to_target):
        self.target_value = target_value
        self.time_to_start = time_to_start
        self.time_to_target = time_to_target

    def __call__(self, initial_proportion, t):
        target_value_ = max(self.target_value, initial_proportion)
        amount_implemented = numpy.where(
            t < self.time_to_start, 0,
            numpy.where(
                t < self.time_to_target,
                (t - self.time_to_start)
                / (self.time_to_target - self.time_to_start),
                1))
        return (initial_proportion
                + (target_value_ - initial_proportion) * amount_implemented)

class OneTarget90(OneTargetLinear):
    '''
    Linearly go from the `initial_proportion` to the
    `target_value = max(90%, initial_proportion)` between 2015 and
    2020.  Stay fixed at `initial_proportion` before
    2015 and fixed at the `target_value` after 2020.
    '''
    target_value = 0.9
    time_to_start = 2015
    time_to_target = 2020

    def __init__(self):
        pass

class OneTarget95:
    '''
    Linearly go from the `initial_proportion` to
    `target_value_0 = max(90%, initial_proportion)` between 2015 and
    2020, then linearly go to
    `target_value_1 = max(95%, initial_proportion)` between 2020 and 2030.
    Stay fixed at `initial_proportion` before 2015 and fixed at
    `target_value_1` after 2030.
    '''
    target_value_0 = 0.9
    target_value_1 = 0.95
    time_0 = 2015
    time_1 = 2020
    time_2 = 2030

    def __call__(self, initial_proportion, t):
        target_value_0_ = max(self.target_value_0, initial_proportion)
        target_value_1_ = max(self.target_value_1, initial_proportion)
        amount_implemented_0 = numpy.where(
            t < self.time_0, 0,
            numpy.where(
                t < self.time_1,
                (t - self.time_0) / (self.time_1 - self.time_0),
                1))
        amount_implemented_1 = numpy.where(
            t < self.time_1, 0,
            numpy.where(
                t < self.time_2,
                (t - self.time_1) / (self.time_2 - self.time_1),
                1))
        return (initial_proportion
                + (target_value_0_ - initial_proportion) * amount_implemented_0
                + (target_value_1_ - target_value_0_) * amount_implemented_1)

class Target:
    '''
    Base type for target for diagnosis, treatment, viral suppression,
    and vaccination.
    '''

    diagnosed = None
    treated = None
    suppressed = None
    vaccinated = None

    vaccine_efficacy = 0

    def __call__(self, t, parameters):
        '''
        Get numerical values for the target at different points in time.
        '''
        t = numpy.asarray(t)
        initial_proportions = proportions.get(parameters.initial_conditions)
        names = initial_proportions.dtype.names
        arrays = []
        for n in names:
            targ = getattr(self, n)
            ip = getattr(initial_proportions, n)
            arrays.append(targ(ip, t))
        return numpy.rec.fromarrays(arrays, names = names)

    @classmethod
    def __str__(cls):
        return cls.__name__

    def __repr__(self):
        return '{}.{}()'.format(self.__class__.__module__,
                                self.__class__.__name__)

class Zero(Target):
    '''
    All zero.
    '''
    diagnosed = OneTargetZero()
    treated = OneTargetZero()
    suppressed = OneTargetZero()
    vaccinated = OneTargetZero()

    @staticmethod
    def __str__():
        return 'All Zeroes'

class StatusQuo(Target):
    '''
    Fixed at the initial proportion with no vaccination.
    '''
    diagnosed = OneTargetStatusQuo()
    treated = OneTargetStatusQuo()
    suppressed = OneTargetStatusQuo()
    vaccinated = OneTargetZero()

    @staticmethod
    def __str__():
        return 'Status Quo'

class UNAIDS90(Target):
    '''
    90--90--90 target with no vaccination.
    '''
    diagnosed = OneTarget90()
    treated = OneTarget90()
    suppressed = OneTarget90()
    vaccinated = OneTargetZero()

    @staticmethod
    def __str__():
        return '90–90–90'

class UNAIDS95(Target):
    '''
    95--95--95 target with no vaccination.
    '''
    diagnosed = OneTarget95()
    treated = OneTarget95()
    suppressed = OneTarget95()
    vaccinated = OneTargetZero()

    @staticmethod
    def __str__():
        return '95–95–95'

class Vaccine(Target):
    '''
    Vaccine plus the `treatment_target`.
    '''
    def __init__(self,
                 treatment_target = UNAIDS95,
                 efficacy = 0.5,
                 coverage = 0.7,
                 time_to_start = 2020,
                 time_to_fifty_percent = 2):
        self._efficacy = efficacy
        self._coverage = coverage
        self._time_to_start = time_to_start
        self._time_to_fifty_percent = time_to_fifty_percent
        self._treatment_target = treatment_target

        self.diagnosed = self._treatment_target.diagnosed
        self.treated = self._treatment_target.treated
        self.suppressed = self._treatment_target.suppressed

        # Set vaccine efficacy
        self.vaccine_efficacy = self._efficacy

        time_to_target = (self._coverage / 0.5 * self._time_to_fifty_percent
                          + self._time_to_start)
        self.vaccinated = OneTargetLinear(self._coverage,
                                          self._time_to_start,
                                          time_to_target)
        self.vaccinated.time_to_fifty_percent = self._time_to_fifty_percent

    def __repr__(self):
        if isinstance(self._treatment_target, Target):
            treatment_str = repr(self._treatment_target)
        elif issubclass(self._treatment_target, Target):
            treatment_str = '{}.{}'.format(self._treatment_target.__module__,
                                           self._treatment_target.__name__)
        else:
            raise ValueError

        params = [
            'efficacy={}'.format(self._efficacy),
            'coverage={}'.format(self._coverage),
            'time_to_start={}'.format(self._time_to_start),
            'time_to_fifty_percent={}'.format(self._time_to_fifty_percent),
            'treatment_target={}'.format(treatment_str)]

        return '{}.{}({})'.format(self.__class__.__module__,
                                  self.__class__.__name__,
                                  ', '.join(params))

    def __str__(self):
        if isinstance(self._treatment_target, Target):
            treatment_str = str(self._treatment_target)
        elif issubclass(self._treatment_target, Target):
            treatment_str = self._treatment_target.__str__()
        else:
            raise ValueError

        params = [
            'efficacy={:g}%'.format(100 * self._efficacy),
            'coverage={:g}%'.format(100 * self._coverage),
            'time_to_start={}'.format(self._time_to_start),
            'time_to_fifty_percent={}y'.format(self._time_to_fifty_percent)]

        return '{} + {}({})'.format(treatment_str,
                                    super().__str__(),
                                    ', '.join(params))

# Build each of these and each of these + vaccine.
_all_baselines = [StatusQuo(),
                  UNAIDS90(),
                  UNAIDS95()]

all_ = []
for target in _all_baselines:
    all_.extend([target,
                 Vaccine(treatment_target = target)])

# Build each of these and each of these + vaccine alternatives.
_vaccine_scenarios_baselines = [StatusQuo()]

vaccine_scenarios = []
for target in _vaccine_scenarios_baselines:
    vaccine_scenarios.extend([
        target,
        Vaccine(treatment_target = target),
        Vaccine(treatment_target = target, efficacy = 0.3),
        Vaccine(treatment_target = target, efficacy = 0.7),
        Vaccine(treatment_target = target, coverage = 0.5),
        Vaccine(treatment_target = target, coverage = 0.9),
        Vaccine(treatment_target = target, time_to_start = 2025),
        Vaccine(treatment_target = target, time_to_fifty_percent = 5)])
<|endoftext|>


<commit_before>from django.core.management.base import BaseCommand, CommandError
from .backup import Command as Backup
from ecolle.settings import CHEMINVERSECOLLE
import os
from urllib.request import urlopen
from urllib.error import HTTPError
import tarfile
import subprocess
from shutil import rmtree, copy as filecopy

class Command(BaseCommand):
    help = """mise à jour de e-colle"""

    def clean(self):
        """nettoie dans le cas d'un échec"""
        directory = os.path.join("..","e-colle-new")
        if os.path.isdir(directory):
            rmtree(directory, True)
        reps = [f for f in os.listdir("..") if os.path.isdir(f) and f.startswith("stephanekirsch-e-colle")]
        if len(reps) > 0:
            for rep in reps:
                rmtree(os.path.join("..",rep))
        if os.path.isfile(os.path.join("..","e-colle.tar.gz")):
            os.remove(os.path.join("..","e-colle.tar.gz"))

    def add_arguments(self, parser):
        parser.add_argument('version', nargs=1 , type=str)

    def handle(self, *args, **options):
        try:
            version = options.pop('version')[0]
            url = "https://github.com/stephanekirsch/e-colle/tarball/" + version
            try:
                with urlopen(url) as fichier, open(os.path.join("..","e-colle.tar.gz"),"wb") as fichier_zip:
                    self.stdout.write("téléchargement de la version {} de e-colle".format(version))
                    self.stdout.write("progression: 0.0%",ending="\r")
                    taille = fichier.getheader('Content-Length')
                    taille = int(taille) if taille is not None else 3000000
                    taille = int(taille)
                    CHUNK = 32*1024
                    progress = 0
                    efface = 4
                    while True:
                        chunk = fichier.read(CHUNK)
                        if not chunk:
                            break
                        else:
                            progress += CHUNK
                            progress = min(taille,progress)
                            pourcent = "progression: {:.2f}%".format(100*progress/taille)
                            self.stdout.write(pourcent, ending="\r")
                            fichier_zip.write(chunk)
                    self.stdout.write("\ntéléchargement terminé")
            except HTTPError:
                raise CommandError("la version précisée: {} n'existe pas".format(version))
            # décompression du fichier zip
            archive_zip = tarfile.open(os.path.join("..","e-colle.tar.gz"),"r:gz")
            archive_zip.extractall("..")
            # on renomme le nouveau e-colle
            cwd = os.path.split(os.getcwd())[1]
            cwd_new = "e-colle-new"
            cwd_bak = "{}-bak".format(cwd)
            reps = [f for f in os.listdir("..") if not os.path.isfile(f) and f.startswith("stephanekirsch-e-colle")]
            os.rename(os.path.join("..",reps[0]),os.path.join("..",cwd_new))
            # on récupère les données de config de l'ancien e-colle pour les passer au nouveau.
            self.stdout.write("copie des données de configuration")
            import ecolle.config as old_config
            vars = [x for x in dir(old_config) if x[:2] != "__"]
            vardict = {s:getattr(old_config,s) for s in vars}
            with open(os.path.join("..",cwd_new,"ecolle","config.py"),"wt",encoding="utf8") as fichier:
                for key, value in vardict.items():
                    if type(value) is str:
                        fichier.write("{} = '{}'\n".format(key,value))
                    else:
                        fichier.write("{} = {}\n".format(key,value))
            self.stdout.write("copie terminée")
            self.stdout.write("sauvegarde de la base de données pour avoir un point de sauvegarde en cas d'échec de la mise à jour")
            try:
                backup = Backup()
                backup.handle(backup_media = False, **options)
            except Exception as e:
                self.stdout.write("Échec de la sauvegarde de la base de données:")
                self.stdout.write(str(e))
                self.stdout.write("La sauvegarde a échoué, voulez-vous poursuivre?")
                poursuite = input("Attention si la mise à jour échoue vous risquez de ne pas pouvoir revenir en arrière (o/N)")
                if poursuite != "" and poursuite in "oO":
                    poursuite = True
                else:
                    poursuite = False
            else:
                poursuite = True
            if poursuite: # si la sauvegarde de la base de données a réussi ou si on s'en passe, on met à jour la migration
                self.stdout.write("Mise à jour de la structure de la base de données")
                os.chdir(os.path.join("..",cwd_new))
                subprocess.run(["pip3","install", "-r","requirements.txt"]) # on installe les éventuelles bilbiothèques manquantes
                subprocess.run(["python3","manage.py","migrate"])
                # on copie les fichiers media/backup:
                self.stdout.write("copie des fichiers media/backup")
                repertoires = os.path.listdir("media")
                repertoires = [os.path.join("media", file) for file in repertoires]
                repertoires = [d for d in repertoires if os.path.isdir(d)]
                for rep in repertoires:
                    source = os.path.join("media",rep)
                    target = os.path.join("..",cwd_new,"media",rep)
                    for file in os.listdir(source):
                        filecopy(os.path.join(source,file),target)
                source = "backup"
                target = os.path.join("..",cwd_new,"backup")
                for file in os.listdir(source):
                    filecopy(os.path.join(source,file),target)
                # on renomme e-colle-bak l'ancien réperoire et e-colle le nouveau
                os.rename(os.path.join("..",cwd),os.path.join("..",cwd_bak))
                os.rename(os.path.join("..",cwd_new),os.path.join("..",cwd))
        except Exception as e:
            self.stdout.write(str(e))
            self.stdout.write("la mise à jour n'a pas pu aller jusqu'au bout. Si la migration a eu lieu,\n\
                et que e-colle ne fonctionne plus à cause de la base de données\n\
                il est recommandé d'effacer le répertoire 'e-colle',\n\
                renommer 'e-colle' le répertoire 'e-colle-bak', effacer la base de données,\n\
                la recréer et d'utiliser les commandes migrate et restore.")
        else:
            self.stdout.write("la mise a jour a été effectuée avec succès.\n\
                l'ancien répertoire e-colle s'appelle désormais e-colle-bak\n\
                Si jamais ça ne fonctionne pas, vous pouvez revenir à l'état précédent\n\
                en effaçant le répertoire 'e-colle', renommant 'e-colle' le répertoire 'e-colle-bak'\n\
                effaçant la base de données, la recréer et utiliser les commandes migrate et restore\n\
                Pour que les modifications soient prises en compte en production,\n\
                il faut redémarrer le logiciel serveur.")
        finally:
            self.stdout.write("nettoyage")
            self.clean()

<commit_msg>correction bug update<commit_after>from django.core.management.base import BaseCommand, CommandError
from .backup import Command as Backup
from ecolle.settings import CHEMINVERSECOLLE
import os
from urllib.request import urlopen
from urllib.error import HTTPError
import tarfile
import subprocess
from shutil import rmtree, copy as filecopy

class Command(BaseCommand):
    help = """mise à jour de e-colle"""

    def clean(self):
        """nettoie dans le cas d'un échec"""
        directory = os.path.join("..","e-colle-new")
        if os.path.isdir(directory):
            rmtree(directory, True)
        reps = [f for f in os.listdir("..") if os.path.isdir(f) and f.startswith("stephanekirsch-e-colle")]
        if len(reps) > 0:
            for rep in reps:
                rmtree(os.path.join("..",rep))
        if os.path.isfile(os.path.join("..","e-colle.tar.gz")):
            os.remove(os.path.join("..","e-colle.tar.gz"))

    def add_arguments(self, parser):
        parser.add_argument('version', nargs=1 , type=str)

    def handle(self, *args, **options):
        try:
            version = options.pop('version')[0]
            url = "https://github.com/stephanekirsch/e-colle/tarball/" + version
            try:
                with urlopen(url) as fichier, open(os.path.join("..","e-colle.tar.gz"),"wb") as fichier_zip:
                    self.stdout.write("téléchargement de la version {} de e-colle".format(version))
                    self.stdout.write("progression: 0.0%",ending="\r")
                    taille = fichier.getheader('Content-Length')
                    taille = int(taille) if taille is not None else 3000000
                    taille = int(taille)
                    CHUNK = 32*1024
                    progress = 0
                    efface = 4
                    while True:
                        chunk = fichier.read(CHUNK)
                        if not chunk:
                            break
                        else:
                            progress += CHUNK
                            progress = min(taille,progress)
                            pourcent = "progression: {:.2f}%".format(100*progress/taille)
                            self.stdout.write(pourcent, ending="\r")
                            fichier_zip.write(chunk)
                    self.stdout.write("\ntéléchargement terminé")
            except HTTPError:
                raise CommandError("la version précisée: {} n'existe pas".format(version))
            # décompression du fichier zip
            archive_zip = tarfile.open(os.path.join("..","e-colle.tar.gz"),"r:gz")
            archive_zip.extractall("..")
            # on renomme le nouveau e-colle
            cwd = os.path.split(os.getcwd())[1]
            cwd_new = "e-colle-new"
            cwd_bak = "{}-bak".format(cwd)
            reps = [f for f in os.listdir("..") if not os.path.isfile(f) and f.startswith("stephanekirsch-e-colle")]
            os.rename(os.path.join("..",reps[0]),os.path.join("..",cwd_new))
            # on récupère les données de config de l'ancien e-colle pour les passer au nouveau.
            self.stdout.write("copie des données de configuration")
            import ecolle.config as old_config
            vars = [x for x in dir(old_config) if x[:2] != "__"]
            vardict = {s:getattr(old_config,s) for s in vars}
            with open(os.path.join("..",cwd_new,"ecolle","config.py"),"wt",encoding="utf8") as fichier:
                for key, value in vardict.items():
                    if type(value) is str:
                        fichier.write("{} = '{}'\n".format(key,value))
                    else:
                        fichier.write("{} = {}\n".format(key,value))
            self.stdout.write("copie terminée")
            self.stdout.write("sauvegarde de la base de données pour avoir un point de sauvegarde en cas d'échec de la mise à jour")
            try:
                backup = Backup()
                backup.handle(backup_media = False, **options)
            except Exception as e:
                self.stdout.write("Échec de la sauvegarde de la base de données:")
                self.stdout.write(str(e))
                self.stdout.write("La sauvegarde a échoué, voulez-vous poursuivre?")
                poursuite = input("Attention si la mise à jour échoue vous risquez de ne pas pouvoir revenir en arrière (o/N)")
                if poursuite != "" and poursuite in "oO":
                    poursuite = True
                else:
                    poursuite = False
            else:
                poursuite = True
            if poursuite: # si la sauvegarde de la base de données a réussi ou si on s'en passe, on met à jour la migration
                self.stdout.write("Mise à jour de la structure de la base de données")
                os.chdir(os.path.join("..",cwd_new))
                subprocess.run(["pip3","install", "-r","requirements.txt"]) # on installe les éventuelles bilbiothèques manquantes
                subprocess.run(["python3","manage.py","migrate"])
                # on copie les fichiers media/backup:
                self.stdout.write("copie des fichiers media/backup")
                repertoires = os.listdir("media")
                repertoires = [os.path.join("media", file) for file in repertoires]
                repertoires = [d for d in repertoires if os.path.isdir(d)]
                for rep in repertoires:
                    source = os.path.join("media",rep)
                    target = os.path.join("..",cwd_new,"media",rep)
                    for file in os.listdir(source):
                        filecopy(os.path.join(source,file),target)
                source = "backup"
                target = os.path.join("..",cwd_new,"backup")
                for file in os.listdir(source):
                    filecopy(os.path.join(source,file),target)
                # on renomme e-colle-bak l'ancien réperoire et e-colle le nouveau
                os.rename(os.path.join("..",cwd),os.path.join("..",cwd_bak))
                os.rename(os.path.join("..",cwd_new),os.path.join("..",cwd))
        except Exception as e:
            self.stdout.write(str(e))
            self.stdout.write("la mise à jour n'a pas pu aller jusqu'au bout. Si la migration a eu lieu,\n\
                et que e-colle ne fonctionne plus à cause de la base de données\n\
                il est recommandé d'effacer le répertoire 'e-colle',\n\
                renommer 'e-colle' le répertoire 'e-colle-bak', effacer la base de données,\n\
                la recréer et d'utiliser les commandes migrate et restore.")
        else:
            self.stdout.write("la mise a jour a été effectuée avec succès.\n\
                l'ancien répertoire e-colle s'appelle désormais e-colle-bak\n\
                Si jamais ça ne fonctionne pas, vous pouvez revenir à l'état précédent\n\
                en effaçant le répertoire 'e-colle', renommant 'e-colle' le répertoire 'e-colle-bak'\n\
                effaçant la base de données, la recréer et utiliser les commandes migrate et restore\n\
                Pour que les modifications soient prises en compte en production,\n\
                il faut redémarrer le logiciel serveur.")
        finally:
            self.stdout.write("nettoyage")
            self.clean()

<|endoftext|>


<commit_before>import click, json, sys, quadkey

@click.command()
@click.argument('assets_file')
def cli(assets_file):
    """Given a lighting alert for a specific area"""
    with open(assets_file) as f:
        assets_data = json.load(f)
    assets_dict = {}  # save the data as a dict format to improve execution efficiency
    for asset in assets_data:
        try:
            if asset["quadKey"] not in assets_dict:
                assets_dict[asset["quadKey"]] = [(asset["assetOwner"], asset["assetName"])]
            else:
                assets_dict[asset["quadKey"]].append((asset["assetOwner"], asset["assetName"]))
        except Exception as e:
            click.echo(click.style("Invalid asset input: "+str(asset),fg = 'red'))
    """Since quadkey might not be accurate enough
    there might be more than one asset owners sharing a same quadkey
    """
    for line in sys.stdin:
        lightning_data = json.loads(line)
        try:
            if lightning_data["flashType"] == 9: # exclude flashType is 'heartbeat'
                continue
            latitude = lightning_data['latitude']
            longitude = lightning_data['longitude']
            qk = quadkey.from_geo((latitude, longitude), 12) # conver lightning data to quadkey
            if qk.key in assets_dict:
                asset_tuples = assets_dict[qk.key]
                for asset in asset_tuples:
                    click.echo('lightning alert for %s : %s' % (asset[0], asset[1]))
                del assets_dict[qk.key] # to prevent alerting several times
        except Exception as e:
            click.echo(click.style("Invalid strike input: "+str(line), fg = 'red'))
<commit_msg>Update cli<commit_after>import click, json, sys, quadkey

@click.command()
@click.argument('assets_file')
def cli(assets_file):
    """Given a lighting alert for a specific area"""
    with open(assets_file) as f:
        assets_data = json.load(f)
    assets_dict = {}  # save the data as a dict format to improve execution efficiency
    count_aline = 0
    for asset in assets_data:
        count_aline += 1
        try:
            if asset["quadKey"] not in assets_dict:
                assets_dict[asset["quadKey"]] = [(asset["assetOwner"], asset["assetName"])]
            else:
                assets_dict[asset["quadKey"]].append((asset["assetOwner"], asset["assetName"]))
        except Exception as e:
            click.echo(click.style("Invalid asset input: "+str(asset)+ " in line %s" % count_aline,fg = 'red'))
    """Since quadkey might not be accurate enough
    there might be more than one asset owners sharing a same quadkey
    """
    count_lline = 0
    for line in sys.stdin:
        lightning_data = json.loads(line)
        count_lline += 1
        try:
            if lightning_data["flashType"] == 9: # exclude flashType is 'heartbeat'
                continue
            latitude = lightning_data['latitude']
            longitude = lightning_data['longitude']
            qk = quadkey.from_geo((latitude, longitude), 12) # conver lightning data to quadkey
            if qk.key in assets_dict:
                asset_tuples = assets_dict[qk.key]
                for asset in asset_tuples:
                    click.echo('lightning alert for %s : %s' % (asset[0], asset[1]))
                del assets_dict[qk.key] # to prevent alerting several times
        except Exception as e:
            click.echo(click.style("Invalid strike input: "+str(line) + " in line %s" % count_lline, fg = 'red'))
<|endoftext|>


<commit_before>import os
import logging
import tempfile

from collections import namedtuple

from job import Job
from io import Io, system
from app import PluginManager

logger = logging.getLogger(__name__)

Depend = namedtuple("Depend", ["dependent", "dependOn", "type", "args"])

class DependType(object):
    All = "DependAll"
    ByTask = "ByTask"

class LayerAspect(type):

    def __call__(cls, *args, **kwargs):
        """
        Intercepts the creation of layer objects and assigns
        them to the current job, of there is one.  The current job
        is set when a job is loaded via script.
        """
        layer = super(LayerAspect, cls).__call__(*args, **kwargs)

        if Job.Current:
            Job.Current.addLayer(layer)

        layer.afterInit()
        return layer

class Layer(object):
    """
    A base class which implments the core functionality of
    an executable entity.
    """
    __metaclass__ = LayerAspect

    def __init__(self, name, **args):
        self.__name = name
        self.__args = args

        self.__job = None
        self.__req_args = []
        self.__depends = []
        self.__setups = []
        self.__outputs = {}
        self.__inputs = {}

        self.__handleDependArg()

    def getName(self):
        return self.__name

    def setArg(self, name, value):
        self.__args[name] = value

    def getArg(self, name, default=None):
        return self.__args.get(name, default)

    def isArgSet(self, name):
        return self.__args.has_key(name)

    def requireArg(self, name, types=None):
        self.__req_args.append((name, types))

    def dependOn(self, other, args=None):
        self.__depends.append(Depend(self, other, DependType.ByTask, args))

    def dependAll(self, other, args=None):
        self.__depends.append(Depend(self, other, DependType.All, args))

    def getDepends(self):
        return list(self.__depends)

    def getJob(self):
        return self.__job

    def putData(self, name, data):
        self.__job.getArchive().putData(
            "%s/%s" % (self.__name, name), data)

    def getData(self, name):
        self.__job.getArchive().getData(
            "%s/%s" % (self.__name, name))

    def setJob(self, job):
        self.__job = job

    def getOutput(self, name):
        return self.__outputs[name]

    def getInput(self, name):
        return self.__inputs[name]

    def getOutputs(self):
        return self.__outputs.values()

    def getInputs(self):
        return self.__inputs.values()

    def addInput(self, name, path, attrs=None):
        self.__inputs[name] = Io(name, path, attrs)

    def addOutput(self, name, path, attrs=None):
        self.__outputs[name] = Io(name, path, attrs)

    def getSetupTasks(self):
        return list(self.__setups)

    def addSetupTask(self, task):
        self.__setups.append(task)

    def system(self, cmd):
        system(cmd)

    def afterInit(self):
        self._afterInit()
        PluginManager.runAfterInit(self)

    def setup(self):
        self._setup()
        PluginManager.runSetup(self)

    def beforeExecute(self):
        self._beforeExecute()
        PluginManager.runBeforeExecute(self)

    def execute(self, *args):
        self._execute(*args)

    def afterExecute(self):
        self._afterExecute()
        PluginManager.runAfterExecute(self)

    def getTempDir(self):
        return tempfile.gettempdir()

    def getDir(self):
        return self.__job.getArchive().getPath(self.getName())

    def system(self, cmd):
        system(cmd)

    def _afterInit(self):
        pass

    def _setup(self):
        pass

    def _beforeExecute(self):
        pass

    def _execute(self):
        pass

    def _afterExecute(self):
        pass

    def __handleDependArg(self):
        """
        Handles the dependOn kwarg passed in via the constructor.

        foo = Layer("foo", dependOn=["bar", "bing:all"])
        Layer("zing", dependOn=[(foo, Layer.DependAll)])
        """
        if not self.isArgSet("depend"):
            return

        depends = self.getArg("depend")
        if not isinstance(depends, (list,tuple)):
            depends = [depends]

        for dep in depends:
            if isinstance(dep, (tuple, list)):
                self.dependOn(LayerDepend(self, dep[0], dep[1]))
            else:
                onLayer = str(dep)
                if onLayer.endswith(":all"):
                    self.dependAll(onLayer.split(":")[0])
                else:
                    self.dependOn(onLayer)

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

class Task(Layer):
    """
    Tasks are indiviudal processes with no-frame range.  Tasks must be parented
    to a layer.
    """
    def __init__(self, name, **args):
        Layer.__init__(self, name, **args)

class SetupTask(Task):

    def __init__(self, layer, **args):
        Task.__init__(self, "%s_setup" % layer.getName(), **args)
        self.__layer = layer
        layer.dependOn(self, DependType.All)

        self.setArg("group", "setups")

    def getLayer(self):
        return self.__layer

<commit_msg>Call before and after execute  around execute.<commit_after>import os
import logging
import tempfile

from collections import namedtuple

from job import Job
from io import Io, system
from app import PluginManager

logger = logging.getLogger(__name__)

Depend = namedtuple("Depend", ["dependent", "dependOn", "type", "args"])

class DependType(object):
    All = "DependAll"
    ByTask = "ByTask"

class LayerAspect(type):

    def __call__(cls, *args, **kwargs):
        """
        Intercepts the creation of layer objects and assigns
        them to the current job, of there is one.  The current job
        is set when a job is loaded via script.
        """
        layer = super(LayerAspect, cls).__call__(*args, **kwargs)

        if Job.Current:
            Job.Current.addLayer(layer)

        layer.afterInit()
        return layer

class Layer(object):
    """
    A base class which implments the core functionality of
    an executable entity.
    """
    __metaclass__ = LayerAspect

    def __init__(self, name, **args):
        self.__name = name
        self.__args = args

        self.__job = None
        self.__req_args = []
        self.__depends = []
        self.__setups = []
        self.__outputs = {}
        self.__inputs = {}

        self.__handleDependArg()

    def getName(self):
        return self.__name

    def setArg(self, name, value):
        self.__args[name] = value

    def getArg(self, name, default=None):
        return self.__args.get(name, default)

    def isArgSet(self, name):
        return self.__args.has_key(name)

    def requireArg(self, name, types=None):
        self.__req_args.append((name, types))

    def dependOn(self, other, args=None):
        self.__depends.append(Depend(self, other, DependType.ByTask, args))

    def dependAll(self, other, args=None):
        self.__depends.append(Depend(self, other, DependType.All, args))

    def getDepends(self):
        return list(self.__depends)

    def getJob(self):
        return self.__job

    def putData(self, name, data):
        self.__job.getArchive().putData(
            "%s/%s" % (self.__name, name), data)

    def getData(self, name):
        self.__job.getArchive().getData(
            "%s/%s" % (self.__name, name))

    def setJob(self, job):
        self.__job = job

    def getOutput(self, name):
        return self.__outputs[name]

    def getInput(self, name):
        return self.__inputs[name]

    def getOutputs(self):
        return self.__outputs.values()

    def getInputs(self):
        return self.__inputs.values()

    def addInput(self, name, path, attrs=None):
        self.__inputs[name] = Io(name, path, attrs)

    def addOutput(self, name, path, attrs=None):
        self.__outputs[name] = Io(name, path, attrs)

    def getSetupTasks(self):
        return list(self.__setups)

    def addSetupTask(self, task):
        self.__setups.append(task)

    def system(self, cmd):
        system(cmd)

    def afterInit(self):
        self._afterInit()
        PluginManager.runAfterInit(self)

    def setup(self):
        self._setup()
        PluginManager.runSetup(self)

    def beforeExecute(self):
        self._beforeExecute()
        PluginManager.runBeforeExecute(self)

    def execute(self, *args):
        self.beforeExecute()
        self._execute(*args)
        self.afterExecute()

    def afterExecute(self):
        self._afterExecute()
        PluginManager.runAfterExecute(self)

    def getTempDir(self):
        return tempfile.gettempdir()

    def getDir(self):
        return self.__job.getArchive().getPath(self.getName())

    def system(self, cmd):
        system(cmd)

    def _afterInit(self):
        pass

    def _setup(self):
        pass

    def _beforeExecute(self):
        pass

    def _execute(self):
        pass

    def _afterExecute(self):
        pass

    def __handleDependArg(self):
        """
        Handles the dependOn kwarg passed in via the constructor.

        foo = Layer("foo", dependOn=["bar", "bing:all"])
        Layer("zing", dependOn=[(foo, Layer.DependAll)])
        """
        if not self.isArgSet("depend"):
            return

        depends = self.getArg("depend")
        if not isinstance(depends, (list,tuple)):
            depends = [depends]

        for dep in depends:
            if isinstance(dep, (tuple, list)):
                self.dependOn(LayerDepend(self, dep[0], dep[1]))
            else:
                onLayer = str(dep)
                if onLayer.endswith(":all"):
                    self.dependAll(onLayer.split(":")[0])
                else:
                    self.dependOn(onLayer)

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

class Task(Layer):
    """
    Tasks are indiviudal processes with no-frame range.  Tasks must be parented
    to a layer.
    """
    def __init__(self, name, **args):
        Layer.__init__(self, name, **args)

class SetupTask(Task):

    def __init__(self, layer, **args):
        Task.__init__(self, "%s_setup" % layer.getName(), **args)
        self.__layer = layer
        layer.dependOn(self, DependType.All)

        self.setArg("group", "setups")

    def getLayer(self):
        return self.__layer

<|endoftext|>


<commit_before># Copyright 2014,  Jeff Buttars,  A10 Networks.
#
#    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.

import acos_client.errors as acos_errors
import acos_client.v30.base as base

from virtual_port import VirtualPort

class VirtualServer(base.BaseV30):
    url_prefix = '/slb/virtual-server/'

    @property
    def vport(self):
        return VirtualPort(self.client)

    def all(self):
        return self._get(self.url_prefix)

    def get(self, name):
        return self._get(self.url_prefix + name)

    def _set(self, name, ip_address=None, status='stats-data-enable',
             update=False):
        params = {
            "virtual-server": self.minimal_dict({
                "name": name,
                "ip-address": ip_address,
                "stats-data-action": status,
            }),
        }

        if not update:
            name = ''

        self._post(self.url_prefix + name, params)

    def create(self, name, ip_address, status='stats-data-enable'):
        try:
            self.get(name)
        except acos_errors.NotFound:
            pass
        else:
            raise acos_errors.Exists

        self._set(name, ip_address, status)

    def update(self, name, ip_address=None, status='stats-data-enable'):
        self._set(name, ip_address, status, update=True)

    def delete(self, name):
        self._delete(self.url_prefix + name)

    def stats(self, name=''):
        # resp = self._get(self.url_prefix + name + '/stats/')
        resp = self._get(self.url_prefix + name + '/stats')
        return resp
        # raise acos_errors.NotImplemented()
<commit_msg>comment out stats-data-action<commit_after># Copyright 2014,  Jeff Buttars,  A10 Networks.
#
#    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.

import acos_client.errors as acos_errors
import acos_client.v30.base as base

from virtual_port import VirtualPort

class VirtualServer(base.BaseV30):
    url_prefix = '/slb/virtual-server/'

    @property
    def vport(self):
        return VirtualPort(self.client)

    def all(self):
        return self._get(self.url_prefix)

    def get(self, name):
        return self._get(self.url_prefix + name)

    def _set(self, name, ip_address=None, status='stats-data-enable',
             update=False):
        params = {
            "virtual-server": self.minimal_dict({
                "name": name,
                "ip-address": ip_address,
                #"stats-data-action": status,
            }),
        }

        if not update:
            name = ''

        self._post(self.url_prefix + name, params)

    def create(self, name, ip_address, status='stats-data-enable'):
        try:
            self.get(name)
        except acos_errors.NotFound:
            pass
        else:
            raise acos_errors.Exists

        self._set(name, ip_address, status)

    def update(self, name, ip_address=None, status='stats-data-enable'):
        self._set(name, ip_address, status, update=True)

    def delete(self, name):
        self._delete(self.url_prefix + name)

    def stats(self, name=''):
        # resp = self._get(self.url_prefix + name + '/stats/')
        resp = self._get(self.url_prefix + name + '/stats')
        return resp
        # raise acos_errors.NotImplemented()
<|endoftext|>


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

#
# This file is part of LiteX-Boards.
#
# Copyright (c) 2020 Florent Kermarrec <florent@enjoy-digital.fr>
# SPDX-License-Identifier: BSD-2-Clause

import os
import argparse
import sys

from migen import *
from migen.genlib.resetsync import AsyncResetSynchronizer

from litex_boards.platforms import ecpix5

from litex.build.lattice.trellis import trellis_args, trellis_argdict

from litex.soc.cores.clock import *
from litex.soc.integration.soc_core import *
from litex.soc.integration.builder import *

from litedram.modules import MT41K256M16
from litedram.phy import ECP5DDRPHY

from liteeth.phy.ecp5rgmii import LiteEthPHYRGMII

# CRG ----------------------------------------------------------------------------------------------

class _CRG(Module):
    def __init__(self, platform, sys_clk_freq):
        self.rst = Signal()
        self.clock_domains.cd_init    = ClockDomain()
        self.clock_domains.cd_por     = ClockDomain(reset_less=True)
        self.clock_domains.cd_sys     = ClockDomain()
        self.clock_domains.cd_sys2x   = ClockDomain()
        self.clock_domains.cd_sys2x_i = ClockDomain(reset_less=True)

        # # #

        self.stop  = Signal()
        self.reset = Signal()

        # Clk / Rst
        clk100 = platform.request("clk100")
        rst_n  = platform.request("rst_n")

        # Power on reset
        por_count = Signal(16, reset=2**16-1)
        por_done  = Signal()
        self.comb += self.cd_por.clk.eq(clk100)
        self.comb += por_done.eq(por_count == 0)
        self.sync.por += If(~por_done, por_count.eq(por_count - 1))

        # PLL
        self.submodules.pll = pll = ECP5PLL()
        self.comb += pll.reset.eq(~por_done | ~rst_n | self.rst)
        pll.register_clkin(clk100, 100e6)
        pll.create_clkout(self.cd_sys2x_i, 2*sys_clk_freq)
        pll.create_clkout(self.cd_init, 25e6)
        self.specials += [
            Instance("ECLKSYNCB",
                i_ECLKI = self.cd_sys2x_i.clk,
                i_STOP  = self.stop,
                o_ECLKO = self.cd_sys2x.clk),
            Instance("CLKDIVF",
                p_DIV     = "2.0",
                i_ALIGNWD = 0,
                i_CLKI    = self.cd_sys2x.clk,
                i_RST     = self.reset,
                o_CDIVX   = self.cd_sys.clk),
            AsyncResetSynchronizer(self.cd_sys,   ~pll.locked | self.reset | self.rst),
            AsyncResetSynchronizer(self.cd_sys2x, ~pll.locked | self.reset | self.rst),
        ]

# BaseSoC ------------------------------------------------------------------------------------------

class BaseSoC(SoCCore):
    def __init__(self, sys_clk_freq=int(75e6), with_ethernet=False, **kwargs):
        platform = ecpix5.Platform(toolchain="trellis")

        # SoCCore ----------------------------------------------------------------------------------
        SoCCore.__init__(self, platform, sys_clk_freq,
            ident          = "LiteX SoC on ECPIX-5",
            ident_version  = True,
            **kwargs)

        # CRG --------------------------------------------------------------------------------------
        self.submodules.crg = _CRG(platform, sys_clk_freq)

        # DDR3 SDRAM -------------------------------------------------------------------------------
        if not self.integrated_main_ram_size:
            self.submodules.ddrphy = ECP5DDRPHY(
                platform.request("ddram"),
                sys_clk_freq=sys_clk_freq)
            self.add_csr("ddrphy")
            self.comb += self.crg.stop.eq(self.ddrphy.init.stop)
            self.comb += self.crg.reset.eq(self.ddrphy.init.reset)
            self.add_sdram("sdram",
                phy                     = self.ddrphy,
                module                  = MT41K256M16(sys_clk_freq, "1:2"),
                origin                  = self.mem_map["main_ram"],
                size                    = kwargs.get("max_sdram_size", 0x40000000),
                l2_cache_size           = kwargs.get("l2_size", 8192),
                l2_cache_min_data_width = kwargs.get("min_l2_data_width", 128),
                l2_cache_reverse        = True
            )

        # Ethernet ---------------------------------------------------------------------------------
        if with_ethernet:
            self.submodules.ethphy = LiteEthPHYRGMII(
                clock_pads = self.platform.request("eth_clocks"),
                pads       = self.platform.request("eth"))
            self.add_csr("ethphy")
            self.add_ethernet(phy=self.ethphy)

        # Leds (Disable...) ------------------------------------------------------------------------
        for i in range(4):
            rgb_led_pads = platform.request("rgb_led", i)
            for c in "rgb":
                self.comb += getattr(rgb_led_pads, c).eq(1)

# Build --------------------------------------------------------------------------------------------

def main():
    parser = argparse.ArgumentParser(description="LiteX SoC on ECPIX-5")
    parser.add_argument("--build",         action="store_true", help="Build bitstream")
    parser.add_argument("--load",          action="store_true", help="Load bitstream")
    parser.add_argument("--sys-clk-freq",  default=75e6,        help="System clock frequency (default: 75MHz)")
    parser.add_argument("--with-sdcard",   action="store_true", help="Enable SDCard support")
    parser.add_argument("--with-ethernet", action="store_true", help="Enable Ethernet support")
    builder_args(parser)
    soc_core_args(parser)
    trellis_args(parser)
    args = parser.parse_args()

    soc = BaseSoC(
        sys_clk_freq  = int(float(args.sys_clk_freq)),
        with_ethernet = args.with_ethernet,
        **soc_core_argdict(args)
    )
    if args.with_sdcard:
        soc.add_sdcard()
    builder = Builder(soc, **builder_argdict(args))
    builder.build(**trellis_argdict(args), run=args.build)

    if args.load:
        prog = soc.platform.create_programmer()
        prog.load_bitstream(os.path.join(builder.gateware_dir, soc.build_name + ".svf"))

if __name__ == "__main__":
    main()
<commit_msg>targets/ecpix5/eth: set rx_delay to 0ns (tested with netboot on R01).<commit_after>#!/usr/bin/env python3

#
# This file is part of LiteX-Boards.
#
# Copyright (c) 2020 Florent Kermarrec <florent@enjoy-digital.fr>
# SPDX-License-Identifier: BSD-2-Clause

import os
import argparse
import sys

from migen import *
from migen.genlib.resetsync import AsyncResetSynchronizer

from litex_boards.platforms import ecpix5

from litex.build.lattice.trellis import trellis_args, trellis_argdict

from litex.soc.cores.clock import *
from litex.soc.integration.soc_core import *
from litex.soc.integration.builder import *

from litedram.modules import MT41K256M16
from litedram.phy import ECP5DDRPHY

from liteeth.phy.ecp5rgmii import LiteEthPHYRGMII

# CRG ----------------------------------------------------------------------------------------------

class _CRG(Module):
    def __init__(self, platform, sys_clk_freq):
        self.rst = Signal()
        self.clock_domains.cd_init    = ClockDomain()
        self.clock_domains.cd_por     = ClockDomain(reset_less=True)
        self.clock_domains.cd_sys     = ClockDomain()
        self.clock_domains.cd_sys2x   = ClockDomain()
        self.clock_domains.cd_sys2x_i = ClockDomain(reset_less=True)

        # # #

        self.stop  = Signal()
        self.reset = Signal()

        # Clk / Rst
        clk100 = platform.request("clk100")
        rst_n  = platform.request("rst_n")

        # Power on reset
        por_count = Signal(16, reset=2**16-1)
        por_done  = Signal()
        self.comb += self.cd_por.clk.eq(clk100)
        self.comb += por_done.eq(por_count == 0)
        self.sync.por += If(~por_done, por_count.eq(por_count - 1))

        # PLL
        self.submodules.pll = pll = ECP5PLL()
        self.comb += pll.reset.eq(~por_done | ~rst_n | self.rst)
        pll.register_clkin(clk100, 100e6)
        pll.create_clkout(self.cd_sys2x_i, 2*sys_clk_freq)
        pll.create_clkout(self.cd_init, 25e6)
        self.specials += [
            Instance("ECLKSYNCB",
                i_ECLKI = self.cd_sys2x_i.clk,
                i_STOP  = self.stop,
                o_ECLKO = self.cd_sys2x.clk),
            Instance("CLKDIVF",
                p_DIV     = "2.0",
                i_ALIGNWD = 0,
                i_CLKI    = self.cd_sys2x.clk,
                i_RST     = self.reset,
                o_CDIVX   = self.cd_sys.clk),
            AsyncResetSynchronizer(self.cd_sys,   ~pll.locked | self.reset | self.rst),
            AsyncResetSynchronizer(self.cd_sys2x, ~pll.locked | self.reset | self.rst),
        ]

# BaseSoC ------------------------------------------------------------------------------------------

class BaseSoC(SoCCore):
    def __init__(self, sys_clk_freq=int(75e6), with_ethernet=False, **kwargs):
        platform = ecpix5.Platform(toolchain="trellis")

        # SoCCore ----------------------------------------------------------------------------------
        SoCCore.__init__(self, platform, sys_clk_freq,
            ident          = "LiteX SoC on ECPIX-5",
            ident_version  = True,
            **kwargs)

        # CRG --------------------------------------------------------------------------------------
        self.submodules.crg = _CRG(platform, sys_clk_freq)

        # DDR3 SDRAM -------------------------------------------------------------------------------
        if not self.integrated_main_ram_size:
            self.submodules.ddrphy = ECP5DDRPHY(
                platform.request("ddram"),
                sys_clk_freq=sys_clk_freq)
            self.add_csr("ddrphy")
            self.comb += self.crg.stop.eq(self.ddrphy.init.stop)
            self.comb += self.crg.reset.eq(self.ddrphy.init.reset)
            self.add_sdram("sdram",
                phy                     = self.ddrphy,
                module                  = MT41K256M16(sys_clk_freq, "1:2"),
                origin                  = self.mem_map["main_ram"],
                size                    = kwargs.get("max_sdram_size", 0x40000000),
                l2_cache_size           = kwargs.get("l2_size", 8192),
                l2_cache_min_data_width = kwargs.get("min_l2_data_width", 128),
                l2_cache_reverse        = True
            )

        # Ethernet ---------------------------------------------------------------------------------
        if with_ethernet:
            self.submodules.ethphy = LiteEthPHYRGMII(
                clock_pads = self.platform.request("eth_clocks"),
                pads       = self.platform.request("eth"),
                rx_delay   = 0e-9)
            self.add_csr("ethphy")
            self.add_ethernet(phy=self.ethphy)

        # Leds (Disable...) ------------------------------------------------------------------------
        for i in range(4):
            rgb_led_pads = platform.request("rgb_led", i)
            for c in "rgb":
                self.comb += getattr(rgb_led_pads, c).eq(1)

# Build --------------------------------------------------------------------------------------------

def main():
    parser = argparse.ArgumentParser(description="LiteX SoC on ECPIX-5")
    parser.add_argument("--build",         action="store_true", help="Build bitstream")
    parser.add_argument("--load",          action="store_true", help="Load bitstream")
    parser.add_argument("--sys-clk-freq",  default=75e6,        help="System clock frequency (default: 75MHz)")
    parser.add_argument("--with-sdcard",   action="store_true", help="Enable SDCard support")
    parser.add_argument("--with-ethernet", action="store_true", help="Enable Ethernet support")
    builder_args(parser)
    soc_core_args(parser)
    trellis_args(parser)
    args = parser.parse_args()

    soc = BaseSoC(
        sys_clk_freq  = int(float(args.sys_clk_freq)),
        with_ethernet = args.with_ethernet,
        **soc_core_argdict(args)
    )
    if args.with_sdcard:
        soc.add_sdcard()
    builder = Builder(soc, **builder_argdict(args))
    builder.build(**trellis_argdict(args), run=args.build)

    if args.load:
        prog = soc.platform.create_programmer()
        prog.load_bitstream(os.path.join(builder.gateware_dir, soc.build_name + ".svf"))

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


<commit_before># -*- coding: utf-8 -*-
# Copyright <2016> Akretion
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
{
    "name": "Keychain",
    "summary": "Store accounts and credentials",
    "version": "9.0.1.0.0",
    "category": "Uncategorized",
    "website": "https://akretion.com/",
    "author": "Akretion, Odoo Community Association (OCA)",
    "license": "AGPL-3",
    "application": False,
    "installable": True,
    "external_dependencies": {
        "python": [
            'cryptography'],
    },
    "depends": [
        "base",
        "base_setup",
    ],
    "data": [
        "security/ir.model.access.csv",
        'views/keychain_view.xml'
    ],
}
<commit_msg>Update __openerp__.py<commit_after># -*- coding: utf-8 -*-
# Copyright <2016> Akretion
# License AGPL-3.0 or later (http://www.gnu.org/licenses/agpl).
{
    "name": "Keychain",
    "summary": "Store accounts and credentials",
    "version": "9.0.1.0.0",
    "category": "Uncategorized",
    "website": "https://akretion.com/",
    "author": "Akretion, Odoo Community Association (OCA)",
    "license": "AGPL-3",
    "application": False,
    "installable": True,
    "external_dependencies": {
        "python": [
            'cryptography'],
    },
    "depends": [
        "base_setup",
    ],
    "data": [
        "security/ir.model.access.csv",
        'views/keychain_view.xml'
    ],
}
<|endoftext|>


<commit_before># vim: tabstop=4 shiftwidth=4 softtabstop=4
# Copyright (c) 2010-2011 OpenStack, LLC.
#
# 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.

"""
Service that stores identities and issues and manages tokens

HEADERS
-------
HTTP_ is a standard http header
HTTP_X is an extended http header

> Coming in from initial call
HTTP_X_AUTH_TOKEN   : the client token being passed in
HTTP_X_STORAGE_TOKEN: the client token being passed in (legacy Rackspace use)
                      to support cloud files
> Used for communication between components
www-authenticate    : only used if this component is being used remotely
HTTP_AUTHORIZATION  : basic auth password used to validate the connection

> What we add to the request for use by the OpenStack service
HTTP_X_AUTHORIZATION: the client identity being passed in

"""
import functools
import logging
import os
import sys
import httplib
import json

import routes
from webob import Response
from webob import Request
from webob.exc import (HTTPNotFound,
                       HTTPConflict,
                       HTTPBadRequest)

POSSIBLE_TOPDIR = os.path.normpath(os.path.join(os.path.abspath(sys.argv[0]),
                                   os.pardir,
                                   os.pardir))
if os.path.exists(os.path.join(POSSIBLE_TOPDIR, 'keystone', '__init__.py')):
    sys.path.insert(0, POSSIBLE_TOPDIR)

from queryext import exthandler
from keystone.common import wsgi
import keystone.logic.service as serv
import keystone.logic.types.auth as auth

service = serv.IDMService()

def is_xml_response(req):
    if not "Accept" in req.headers:
        return False
    return req.content_type == "application/xml"

def get_normalized_request_content(model, req):
    """initialize a model from json/xml contents of request body"""

    if  req.content_type == "application/xml":
        ret = model.from_xml(req.body)
    elif req.content_type == "application/json":
        ret = model.from_json(req.body)
    else:
        raise fault.IDMFault("I don't understand the content type ", code=415)
    return ret

def send_result(code, req, result):
    content = None
    Response.content_type = None
    if result:
        if is_xml_response(req):
            content = result.to_xml()
            Response.content_type = "application/xml"
        else:
            content = result.to_json()
            Response.content_type = "application/json"
    Response.status = code
    if code > 399:
       #return bottle.abort(code, content)
       return;
    return content

class Controller(wsgi.Controller):

    def __init__(self, options):
        self.options = options

    def authenticate(self, req):
        creds = get_normalized_request_content(auth.PasswordCredentials, req)
        return send_result(200, req, service.authenticate(creds))

class Auth_API(wsgi.Router):
    """WSGI entry point for all Keystone Auth API requests."""

    def __init__(self, options):
        self.options = options
        mapper = routes.Mapper()
        controller = Controller(options)
        mapper.connect("/v1.0/token", controller=controller, action="authenticate")
        super(Auth_API, self).__init__(mapper)

def app_factory(global_conf, **local_conf):
    """paste.deploy app factory for creating Glance API server apps"""
    try:
        conf = global_conf.copy()
        conf.update(local_conf)
    except Exception as err:
        print err
    return Auth_API(conf)
<commit_msg>modified Resposne to resp=Response()<commit_after># vim: tabstop=4 shiftwidth=4 softtabstop=4
# Copyright (c) 2010-2011 OpenStack, LLC.
#
# 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.

"""
Service that stores identities and issues and manages tokens

HEADERS
-------
HTTP_ is a standard http header
HTTP_X is an extended http header

> Coming in from initial call
HTTP_X_AUTH_TOKEN   : the client token being passed in
HTTP_X_STORAGE_TOKEN: the client token being passed in (legacy Rackspace use)
                      to support cloud files
> Used for communication between components
www-authenticate    : only used if this component is being used remotely
HTTP_AUTHORIZATION  : basic auth password used to validate the connection

> What we add to the request for use by the OpenStack service
HTTP_X_AUTHORIZATION: the client identity being passed in

"""
import functools
import logging
import os
import sys
import httplib
import json

import routes
from webob import Response
from webob import Request
from webob.exc import (HTTPNotFound,
                       HTTPConflict,
                       HTTPBadRequest)

POSSIBLE_TOPDIR = os.path.normpath(os.path.join(os.path.abspath(sys.argv[0]),
                                   os.pardir,
                                   os.pardir))
if os.path.exists(os.path.join(POSSIBLE_TOPDIR, 'keystone', '__init__.py')):
    sys.path.insert(0, POSSIBLE_TOPDIR)

from queryext import exthandler
from keystone.common import wsgi
import keystone.logic.service as serv
import keystone.logic.types.auth as auth

service = serv.IDMService()

def is_xml_response(req):
    if not "Accept" in req.headers:
        return False
    return req.content_type == "application/xml"

def get_normalized_request_content(model, req):
    """initialize a model from json/xml contents of request body"""

    if  req.content_type == "application/xml":
        ret = model.from_xml(req.body)
    elif req.content_type == "application/json":
        ret = model.from_json(req.body)
    else:
        raise fault.IDMFault("I don't understand the content type ", code=415)
    return ret

def send_result(code, req, result):
    content = None
    resp=Response()
    resp.content_type = None
    if result:
        if is_xml_response(req):
            content = result.to_xml()
            resp.content_type = "application/xml"
        else:
            content = result.to_json()
            resp.content_type = "application/json"
    resp.status = code
    if code > 399:
       #return bottle.abort(code, content)
       return;
    return content

class Controller(wsgi.Controller):

    def __init__(self, options):
        self.options = options

    def authenticate(self, req):
        creds = get_normalized_request_content(auth.PasswordCredentials, req)
        return send_result(200, req, service.authenticate(creds))

class Auth_API(wsgi.Router):
    """WSGI entry point for all Keystone Auth API requests."""

    def __init__(self, options):
        self.options = options
        mapper = routes.Mapper()
        controller = Controller(options)
        mapper.connect("/v1.0/token", controller=controller, action="authenticate")
        super(Auth_API, self).__init__(mapper)

def app_factory(global_conf, **local_conf):
    """paste.deploy app factory for creating Glance API server apps"""
    try:
        conf = global_conf.copy()
        conf.update(local_conf)
    except Exception as err:
        print err
    return Auth_API(conf)
<|endoftext|>


<commit_before>from jinja2 import Template
import codecs
import re
import string
import random

class RDL():

    def __init__(self,server_url,datasource,query,template='template.rdl'):
        self.query = query
        self.server_url = server_url
        self.datasource = datasource
        self.template = template

        self.fields = []
        self.paramters = []

        self.init_parameters()
        self.init_fields()

    def get_text(self,):
        with open(self.template, 'r') as template_file:
            template = Template(template_file.read().decode('utf8'))
            return template.render(
                query=self.query,
                data_source_reference=self.datasource,
                data_source_id=self.gen_id(),
                fields=self.fields,
                parameters=self.parameters,
                server_url = self.server_url,
                report_id = self.gen_id(),
            )

    def write_file(self,filename):
            with open(filename, 'w') as out_file:
                out_file.write(codecs.BOM_UTF8)

                out_file.write(self.get_text())

    def init_fields(self):
        m = re.match(r'select(.*?)from', self.query, flags=re.IGNORECASE|re.MULTILINE|re.DOTALL)

        for field in m.group(1).strip().split(','):
            names = field.split(' as ')

            name = names[0]

            if len(names) > 1:
                name = names[1]

            name = name.replace('\'','')
            name = name.strip()

            self.fields.append({
                "name":name
            })

    def init_parameters(self):
        matches = re.findall(r'@.*?[ \n\r]', self.query, flags=re.MULTILINE)

        self.parameters = set([m.strip().replace('@','') for m in matches])

    def gen_id(self,size=16,chars=string.ascii_uppercase + string.digits):
        return ''.join(random.choice(chars) for _ in range(size))

if __name__ == '__main__':
    import sys

    try:
        server_url = str(sys.argv[1])
        datasource = '/' + str(sys.argv[2])
        query = str(sys.stdin.read())
    except:
        print 'Usage: %s [server_url] [datasource]\nSQL query is supplied on stdin' % (sys.argv[0],)
        exit(1)

    rdl = RDL(server_url,datasource,query)

    rdl.write_file("output.rdl")
    print "Output written to output.rdl"<commit_msg>Fixed issue with complex field functions<commit_after>from jinja2 import Template
import codecs
import re
import string
import random

class RDL():

    def __init__(self,server_url,datasource,query,template='template.rdl'):
        self.query = query
        self.server_url = server_url
        self.datasource = datasource
        self.template = template

        self.fields = []
        self.paramters = []

        self.init_parameters()
        self.init_fields()

    def get_text(self,):
        with open(self.template, 'r') as template_file:
            template = Template(template_file.read().decode('utf8'))
            return template.render(
                query=self.query,
                data_source_reference=self.datasource,
                data_source_id=self.gen_id(),
                fields=self.fields,
                parameters=self.parameters,
                server_url = self.server_url,
                report_id = self.gen_id(),
            )

    def write_file(self,filename):
            with open(filename, 'w') as out_file:
                out_file.write(codecs.BOM_UTF8)

                out_file.write(self.get_text())

    def init_fields(self):
        m = re.match(r'select(.*?)from', self.query, flags=re.IGNORECASE|re.MULTILINE|re.DOTALL)

        for field in m.group(1).strip().split(',\n'):
            names = field.split(' as ')

            name = names[0]

            if len(names) > 1:
                name = names[1]

            name = name.replace('\'','')
            name = name.strip()

            self.fields.append({
                "name":name
            })

    def init_parameters(self):
        matches = re.findall(r'@.*?[ \n\r]', self.query, flags=re.MULTILINE)

        self.parameters = set([m.strip().replace('@','') for m in matches])

    def gen_id(self,size=16,chars=string.ascii_uppercase + string.digits):
        return ''.join(random.choice(chars) for _ in range(size))

if __name__ == '__main__':
    import sys

    try:
        server_url = str(sys.argv[1])
        datasource = '/' + str(sys.argv[2])
        query = str(sys.stdin.read())
    except:
        print 'Usage: %s [server_url] [datasource]\nSQL query is supplied on stdin' % (sys.argv[0],)
        exit(1)

    rdl = RDL(server_url,datasource,query)

    rdl.write_file("output.rdl")
    print "Output written to output.rdl"<|endoftext|>


<commit_before>import pygame, math, sys

class Key():
    def __init__(self, image, pos, size):
        self.baseImage = pygame.image.load(image)
        if size != None:
            self.resize(size)
        else:
            self.image = self.baseImage
        self.rect = self.imag.get_rect()
        self.place(pos)
        self.living = True

    def place(self, pos):
        self.rect.center = pos

    def resize (self, size):
        self.image = pygame.transform.scale

    def distance (self, pt):
        x1 = self.rect.center[0]
        y1 = self.rect.center[0]
        y2 = pt[0]
        x2 = pt[0]
        return math.sqrt (((x2-x1)**2) + ((y2-y1)**2))

    def playerCollide (self, other):
        if (self.rect.right > other.rect.left
            and self.rect.left < other.rect.right):
            if (self.rect.bottom > other.rect.top
            and self.rect.top < other.rect.bottom):
                self.living = False
            return True
        return False
<commit_msg>i don't know what i did<commit_after>import pygame, math, sys

class Key():
    def __init__(self, image, pos, size):
        self.baseImage = pygame.image.load(image)
        if size != None:
            self.resize(size)
        else:
            self.image = self.baseImage
        self.rect = self.imag.get_rect()
        self.place(pos)
        levelFile = "RSC/Props/Key Images/" + letter + "key" + ".png"
        self.living = True

    def place(self, pos):
        self.rect.center = pos

    def resize (self, size):
        self.image = pygame.transform.scale

    def distance (self, pt):
        x1 = self.rect.center[0]
        y1 = self.rect.center[0]
        y2 = pt[0]
        x2 = pt[0]
        return math.sqrt (((x2-x1)**2) + ((y2-y1)**2))

    def playerCollide (self, other):
        if (self.rect.right > other.rect.left
            and self.rect.left < other.rect.right):
            if (self.rect.bottom > other.rect.top
            and self.rect.top < other.rect.bottom):
                self.living = False
            return True
        return False
<|endoftext|>