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

from __future__ import print_function

import contextlib
import unittest
import numpy as np
import six

import paddle
import paddle.fluid as fluid
from paddle.fluid import core
from paddle.fluid.optimizer import SGDOptimizer, Adam
from paddle.fluid.imperative.nn import FC
from paddle.fluid.imperative.base import to_variable
from test_imperative_base import new_program_scope

class MLP(fluid.imperative.Layer):
    def __init__(self, name_scope, param_attr=None, bias_attr=None):
        super(MLP, self).__init__(name_scope)

        self._fc1 = FC(self.full_name(), 10)
        self._fc2 = FC(self.full_name(), 10)

    def forward(self, inputs):
        y = self._fc1(inputs)
        y = self._fc2(y)
        return y

class TestImperativeOptimizerBase(unittest.TestCase):
    def setUp(self):
        self.batch_num = 20

    def get_optimizer(self):
        raise NotImplementedError()

    def _check_mlp(self):
        seed = 90
        with fluid.imperative.guard():
            fluid.default_startup_program().random_seed = seed
            fluid.default_main_program().random_seed = seed

            mlp = MLP('mlp')
            optimizer = self.get_optimizer()
            train_reader = paddle.batch(
                paddle.dataset.mnist.train(), batch_size=128, drop_last=True)

            dy_param_init_value = {}
            for batch_id, data in enumerate(train_reader()):
                if batch_id >= self.batch_num:
                    break

                dy_x_data = np.array(
                    [x[0].reshape(1, 28, 28) for x in data]).astype('float32')
                y_data = np.array([x[1] for x in data]).astype('int64').reshape(
                    128, 1)

                img = to_variable(dy_x_data)
                label = to_variable(y_data)
                label._stop_gradient = True

                cost = mlp(img)
                avg_loss = fluid.layers.reduce_mean(cost)
                dy_out = avg_loss._numpy()

                if batch_id == 0:
                    for param in mlp.parameters():
                        dy_param_init_value[param.name] = param._numpy()

                avg_loss._backward()
                optimizer.minimize(avg_loss)
                mlp.clear_gradients()
                dy_param_value = {}
                for param in mlp.parameters():
                    dy_param_value[param.name] = param._numpy()

        with new_program_scope():
            fluid.default_startup_program().random_seed = seed
            fluid.default_main_program().random_seed = seed

            exe = fluid.Executor(fluid.CPUPlace(
            ) if not core.is_compiled_with_cuda() else fluid.CUDAPlace(0))

            mlp = MLP('mlp')
            optimizer = self.get_optimizer()
            train_reader = paddle.batch(
                paddle.dataset.mnist.train(), batch_size=128, drop_last=True)

            img = fluid.layers.data(
                name='pixel', shape=[1, 28, 28], dtype='float32')
            label = fluid.layers.data(name='label', shape=[1], dtype='int64')
            cost = mlp(img)
            avg_loss = fluid.layers.reduce_mean(cost)
            optimizer.minimize(avg_loss)

            # initialize params and fetch them
            static_param_init_value = {}
            static_param_name_list = []
            for param in mlp.parameters():
                static_param_name_list.append(param.name)

            out = exe.run(fluid.default_startup_program(),
                          fetch_list=static_param_name_list)

            for i in range(len(static_param_name_list)):
                static_param_init_value[static_param_name_list[i]] = out[i]

            for batch_id, data in enumerate(train_reader()):
                if batch_id >= self.batch_num:
                    break

                static_x_data = np.array(
                    [x[0].reshape(1, 28, 28) for x in data]).astype('float32')
                y_data = np.array([x[1] for x in data]).astype('int64').reshape(
                    [128, 1])

                fetch_list = [avg_loss.name]
                fetch_list.extend(static_param_name_list)
                out = exe.run(fluid.default_main_program(),
                              feed={"pixel": static_x_data,
                                    "label": y_data},
                              fetch_list=fetch_list)

                static_param_value = {}
                static_out = out[0]
                for i in range(1, len(out)):
                    static_param_value[static_param_name_list[i - 1]] = out[i]

        for key, value in six.iteritems(static_param_init_value):
            self.assertTrue(np.allclose(value, dy_param_init_value[key]))

        self.assertTrue(np.allclose(static_out, dy_out))

        for key, value in six.iteritems(static_param_value):
            self.assertTrue(np.allclose(value, dy_param_value[key], atol=1e-5))

class TestImperativeOptimizerPiecewiseDecay(TestImperativeOptimizerBase):
    def get_optimizer(self):
        bd = [3, 6, 9]
        optimizer = SGDOptimizer(learning_rate=fluid.layers.piecewise_decay(
            boundaries=bd, values=[0.1 * (0.1**i) for i in range(len(bd) + 1)]))
        return optimizer

    def test_sgd(self):
        self._check_mlp()

class TestImperativeOptimizerNaturalExpDecay(TestImperativeOptimizerBase):
    def get_optimizer(self):
        optimizer = SGDOptimizer(learning_rate=fluid.layers.natural_exp_decay(
            learning_rate=0.1,
            decay_steps=10000,
            decay_rate=0.5,
            staircase=True))
        return optimizer

    def test_sgd(self):
        self._check_mlp()

class TestImperativeOptimizerExponentialDecay(TestImperativeOptimizerBase):
    def get_optimizer(self):
        optimizer = SGDOptimizer(learning_rate=fluid.layers.exponential_decay(
            learning_rate=0.1,
            decay_steps=10000,
            decay_rate=0.5,
            staircase=True))
        return optimizer

    def test_sgd(self):
        self._check_mlp()

class TestImperativeOptimizerInverseTimeDecay(TestImperativeOptimizerBase):
    def get_optimizer(self):
        optimizer = Adam(learning_rate=fluid.layers.inverse_time_decay(
            learning_rate=0.1,
            decay_steps=10000,
            decay_rate=0.5,
            staircase=True))
        return optimizer

    def test_adam(self):
        self._check_mlp()

class TestImperativeOptimizerPolynomialDecay(TestImperativeOptimizerBase):
    def get_optimizer(self):
        optimizer = SGDOptimizer(learning_rate=fluid.layers.polynomial_decay(
            learning_rate=0.1, decay_steps=5, cycle=self.cycle))
        return optimizer

    def test_sgd_cycle(self):
        self.cycle = True
        self._check_mlp()

    def test_sgd(self):
        self.cycle = False
        self._check_mlp()

class TestImperativeOptimizerCosineDecay(TestImperativeOptimizerBase):
    def get_optimizer(self):
        optimizer = SGDOptimizer(learning_rate=fluid.layers.cosine_decay(
            learning_rate=0.1, step_each_epoch=10000, epochs=120))
        return optimizer

    def test_sgd(self):
        self._check_mlp()

class TestImperativeOptimizerNoamDecay(TestImperativeOptimizerBase):
    def get_optimizer(self):
        optimizer = SGDOptimizer(learning_rate=fluid.layers.noam_decay(
            d_model=512, warmup_steps=8000))
        return optimizer

    def test_sgd(self):
        self._check_mlp()

if __name__ == '__main__':
    unittest.main()
<commit_msg>Change atol to default value<commit_after># Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from __future__ import print_function

import contextlib
import unittest
import numpy as np
import six

import paddle
import paddle.fluid as fluid
from paddle.fluid import core
from paddle.fluid.optimizer import SGDOptimizer, Adam
from paddle.fluid.imperative.nn import FC
from paddle.fluid.imperative.base import to_variable
from test_imperative_base import new_program_scope

class MLP(fluid.imperative.Layer):
    def __init__(self, name_scope, param_attr=None, bias_attr=None):
        super(MLP, self).__init__(name_scope)

        self._fc1 = FC(self.full_name(), 10)
        self._fc2 = FC(self.full_name(), 10)

    def forward(self, inputs):
        y = self._fc1(inputs)
        y = self._fc2(y)
        return y

class TestImperativeOptimizerBase(unittest.TestCase):
    def setUp(self):
        self.batch_num = 20

    def get_optimizer(self):
        raise NotImplementedError()

    def _check_mlp(self):
        seed = 90
        with fluid.imperative.guard():
            fluid.default_startup_program().random_seed = seed
            fluid.default_main_program().random_seed = seed

            mlp = MLP('mlp')
            optimizer = self.get_optimizer()
            train_reader = paddle.batch(
                paddle.dataset.mnist.train(), batch_size=128, drop_last=True)

            dy_param_init_value = {}
            for batch_id, data in enumerate(train_reader()):
                if batch_id >= self.batch_num:
                    break

                dy_x_data = np.array(
                    [x[0].reshape(1, 28, 28) for x in data]).astype('float32')
                y_data = np.array([x[1] for x in data]).astype('int64').reshape(
                    128, 1)

                img = to_variable(dy_x_data)
                label = to_variable(y_data)
                label._stop_gradient = True

                cost = mlp(img)
                avg_loss = fluid.layers.reduce_mean(cost)
                dy_out = avg_loss._numpy()

                if batch_id == 0:
                    for param in mlp.parameters():
                        dy_param_init_value[param.name] = param._numpy()

                avg_loss._backward()
                optimizer.minimize(avg_loss)
                mlp.clear_gradients()
                dy_param_value = {}
                for param in mlp.parameters():
                    dy_param_value[param.name] = param._numpy()

        with new_program_scope():
            fluid.default_startup_program().random_seed = seed
            fluid.default_main_program().random_seed = seed

            exe = fluid.Executor(fluid.CPUPlace(
            ) if not core.is_compiled_with_cuda() else fluid.CUDAPlace(0))

            mlp = MLP('mlp')
            optimizer = self.get_optimizer()
            train_reader = paddle.batch(
                paddle.dataset.mnist.train(), batch_size=128, drop_last=True)

            img = fluid.layers.data(
                name='pixel', shape=[1, 28, 28], dtype='float32')
            label = fluid.layers.data(name='label', shape=[1], dtype='int64')
            cost = mlp(img)
            avg_loss = fluid.layers.reduce_mean(cost)
            optimizer.minimize(avg_loss)

            # initialize params and fetch them
            static_param_init_value = {}
            static_param_name_list = []
            for param in mlp.parameters():
                static_param_name_list.append(param.name)

            out = exe.run(fluid.default_startup_program(),
                          fetch_list=static_param_name_list)

            for i in range(len(static_param_name_list)):
                static_param_init_value[static_param_name_list[i]] = out[i]

            for batch_id, data in enumerate(train_reader()):
                if batch_id >= self.batch_num:
                    break

                static_x_data = np.array(
                    [x[0].reshape(1, 28, 28) for x in data]).astype('float32')
                y_data = np.array([x[1] for x in data]).astype('int64').reshape(
                    [128, 1])

                fetch_list = [avg_loss.name]
                fetch_list.extend(static_param_name_list)
                out = exe.run(fluid.default_main_program(),
                              feed={"pixel": static_x_data,
                                    "label": y_data},
                              fetch_list=fetch_list)

                static_param_value = {}
                static_out = out[0]
                for i in range(1, len(out)):
                    static_param_value[static_param_name_list[i - 1]] = out[i]

        for key, value in six.iteritems(static_param_init_value):
            self.assertTrue(np.allclose(value, dy_param_init_value[key]))

        self.assertTrue(np.allclose(static_out, dy_out))

        for key, value in six.iteritems(static_param_value):
            self.assertTrue(np.allclose(value, dy_param_value[key]))

class TestImperativeOptimizerPiecewiseDecay(TestImperativeOptimizerBase):
    def get_optimizer(self):
        bd = [3, 6, 9]
        optimizer = SGDOptimizer(learning_rate=fluid.layers.piecewise_decay(
            boundaries=bd, values=[0.1 * (0.1**i) for i in range(len(bd) + 1)]))
        return optimizer

    def test_sgd(self):
        self._check_mlp()

class TestImperativeOptimizerNaturalExpDecay(TestImperativeOptimizerBase):
    def get_optimizer(self):
        optimizer = SGDOptimizer(learning_rate=fluid.layers.natural_exp_decay(
            learning_rate=0.1,
            decay_steps=10000,
            decay_rate=0.5,
            staircase=True))
        return optimizer

    def test_sgd(self):
        self._check_mlp()

class TestImperativeOptimizerExponentialDecay(TestImperativeOptimizerBase):
    def get_optimizer(self):
        optimizer = SGDOptimizer(learning_rate=fluid.layers.exponential_decay(
            learning_rate=0.1,
            decay_steps=10000,
            decay_rate=0.5,
            staircase=True))
        return optimizer

    def test_sgd(self):
        self._check_mlp()

class TestImperativeOptimizerInverseTimeDecay(TestImperativeOptimizerBase):
    def get_optimizer(self):
        optimizer = Adam(learning_rate=fluid.layers.inverse_time_decay(
            learning_rate=0.1,
            decay_steps=10000,
            decay_rate=0.5,
            staircase=True))
        return optimizer

    def test_adam(self):
        self._check_mlp()

class TestImperativeOptimizerPolynomialDecay(TestImperativeOptimizerBase):
    def get_optimizer(self):
        optimizer = SGDOptimizer(learning_rate=fluid.layers.polynomial_decay(
            learning_rate=0.1, decay_steps=5, cycle=self.cycle))
        return optimizer

    def test_sgd_cycle(self):
        self.cycle = True
        self._check_mlp()

    def test_sgd(self):
        self.cycle = False
        self._check_mlp()

class TestImperativeOptimizerCosineDecay(TestImperativeOptimizerBase):
    def get_optimizer(self):
        optimizer = SGDOptimizer(learning_rate=fluid.layers.cosine_decay(
            learning_rate=0.1, step_each_epoch=10000, epochs=120))
        return optimizer

    def test_sgd(self):
        self._check_mlp()

class TestImperativeOptimizerNoamDecay(TestImperativeOptimizerBase):
    def get_optimizer(self):
        optimizer = SGDOptimizer(learning_rate=fluid.layers.noam_decay(
            d_model=512, warmup_steps=8000))
        return optimizer

    def test_sgd(self):
        self._check_mlp()

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


<commit_before>from pythonforandroid.toolchain import NDKRecipe, current_directory, shprint, info
from os.path import exists, join
import sh
import glob

class PygameJNIComponentsRecipe(NDKRecipe):
    version = 'master'
    url = 'https://github.com/inclement/p4a-pygame-bootstrap-components/archive/{version}.zip'
    dir_name = 'bootstrap_components'

    def prebuild_arch(self, arch):
        super(PygameJNIComponentsRecipe, self).postbuild_arch(arch)

        info('Unpacking pygame bootstrap JNI dir components')
        with current_directory(self.get_build_container_dir(arch)):
            if exists('sdl'):
                info('sdl dir exists, so it looks like the JNI components' +
                     'are already unpacked. Skipping.')
                return
            for dirn in glob.glob(join(self.get_build_dir(arch),
                                       'pygame_bootstrap_jni', '*')):
                shprint(sh.mv, dirn, './')
        info('Unpacking was successful, deleting original container dir')
        shprint(sh.rm, '-rf', self.get_build_dir(arch))

recipe = PygameJNIComponentsRecipe()
<commit_msg>Changed pygame bootstrap to pull from kivy<commit_after>from pythonforandroid.toolchain import NDKRecipe, current_directory, shprint, info
from os.path import exists, join
import sh
import glob

class PygameJNIComponentsRecipe(NDKRecipe):
    version = 'master'
    url = 'https://github.com/kivy/p4a-pygame-bootstrap-components/archive/{version}.zip'
    dir_name = 'bootstrap_components'

    def prebuild_arch(self, arch):
        super(PygameJNIComponentsRecipe, self).postbuild_arch(arch)

        info('Unpacking pygame bootstrap JNI dir components')
        with current_directory(self.get_build_container_dir(arch)):
            if exists('sdl'):
                info('sdl dir exists, so it looks like the JNI components' +
                     'are already unpacked. Skipping.')
                return
            for dirn in glob.glob(join(self.get_build_dir(arch),
                                       'pygame_bootstrap_jni', '*')):
                shprint(sh.mv, dirn, './')
        info('Unpacking was successful, deleting original container dir')
        shprint(sh.rm, '-rf', self.get_build_dir(arch))

recipe = PygameJNIComponentsRecipe()
<|endoftext|>


<commit_before>#
# ovirt-engine-setup -- ovirt engine setup
# Copyright (C) 2013 Red Hat, Inc.
#
# 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.
#

"""
Storage pool type configuration plugin
"""

import gettext
_ = lambda m: gettext.dgettext(message=m, domain='ovirt-engine-setup')

from otopi import util
from otopi import plugin

from ovirt_engine_setup import constants as osetupcons

@util.export
class Plugin(plugin.PluginBase):
    """
    Storage pool type configuration plugin
    """
    STORAGE_TYPES = {
        'nfs': 1,
        'fc': 2,
        'iscsi': 3,
        'posixfs': 6,
    }

    def __init__(self, context):
        super(Plugin, self).__init__(context=context)
        self._enabled = False

    @plugin.event(
        stage=plugin.Stages.STAGE_INIT,
    )
    def _init(self):
        self.environment.setdefault(
            osetupcons.ConfigEnv.STORAGE_TYPE,
            None
        )

    @plugin.event(
        stage=plugin.Stages.STAGE_CUSTOMIZATION,
        before=(
            osetupcons.Stages.DIALOG_TITLES_E_ENGINE,
        ),
        after=(
            osetupcons.Stages.CONFIG_APPLICATION_MODE_AVAILABLE,
            osetupcons.Stages.DIALOG_TITLES_S_ENGINE,
        ),
        condition=lambda self: self.environment[
            osetupcons.DBEnv.NEW_DATABASE
        ],
    )
    def _customization(self):
        self._enabled = True

        if self.environment[
            osetupcons.ConfigEnv.STORAGE_TYPE
        ] is None:
            if self.environment[
                osetupcons.ConfigEnv.APPLICATION_MODE
            ] == 'gluster':
                self.environment[
                    osetupcons.ConfigEnv.STORAGE_TYPE
                ] = osetupcons.Defaults.DEFAULT_CONFIG_STORAGE_TYPE
            else:
                self.environment[
                    osetupcons.ConfigEnv.STORAGE_TYPE
                ] = self.dialog.queryString(
                    name='OVESETUP_CONFIG_STORAGE_TYPE',
                    note=_(
                        'Default storage type: (@VALUES@) [@DEFAULT@]: '
                    ),
                    prompt=True,
                    validValues=(
                        'NFS',
                        'FC',
                        'ISCSI',
                        'POSIXFS'
                    ),
                    caseSensitive=False,
                    default=osetupcons.Defaults.DEFAULT_CONFIG_STORAGE_TYPE,
                )

    @plugin.event(
        stage=plugin.Stages.STAGE_MISC,
        after=(
            osetupcons.Stages.DB_CONNECTION_AVAILABLE,
        ),
        condition=lambda self: self._enabled,
    )
    def _misc(self):
        self.environment[osetupcons.DBEnv.STATEMENT].execute(
            statement="""
                select inst_update_default_storage_pool_type (%(type)s)
            """,
            args={
                'type': self.STORAGE_TYPES[
                    self.environment[osetupcons.ConfigEnv.STORAGE_TYPE]
                ],
            },
        )

# vim: expandtab tabstop=4 shiftwidth=4
<commit_msg>packaging: setup: Fix issue while installing Gluster only mode<commit_after>#
# ovirt-engine-setup -- ovirt engine setup
# Copyright (C) 2013 Red Hat, Inc.
#
# 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.
#

"""
Storage pool type configuration plugin
"""

import gettext
_ = lambda m: gettext.dgettext(message=m, domain='ovirt-engine-setup')

from otopi import util
from otopi import plugin

from ovirt_engine_setup import constants as osetupcons

@util.export
class Plugin(plugin.PluginBase):
    """
    Storage pool type configuration plugin
    """
    STORAGE_TYPES = {
        'nfs': 1,
        'fc': 2,
        'iscsi': 3,
        'posixfs': 6,
    }

    def __init__(self, context):
        super(Plugin, self).__init__(context=context)
        self._enabled = False

    @plugin.event(
        stage=plugin.Stages.STAGE_INIT,
    )
    def _init(self):
        self.environment.setdefault(
            osetupcons.ConfigEnv.STORAGE_TYPE,
            None
        )

    @plugin.event(
        stage=plugin.Stages.STAGE_CUSTOMIZATION,
        before=(
            osetupcons.Stages.DIALOG_TITLES_E_ENGINE,
        ),
        after=(
            osetupcons.Stages.CONFIG_APPLICATION_MODE_AVAILABLE,
            osetupcons.Stages.DIALOG_TITLES_S_ENGINE,
        ),
        condition=lambda self: self.environment[
            osetupcons.DBEnv.NEW_DATABASE
        ],
    )
    def _customization(self):
        self._enabled = True

        if self.environment[
            osetupcons.ConfigEnv.STORAGE_TYPE
        ] is None:
            if self.environment[
                osetupcons.ConfigEnv.APPLICATION_MODE
            ] == 'gluster':
                self.environment[
                    osetupcons.ConfigEnv.STORAGE_TYPE
                ] = osetupcons.Defaults.DEFAULT_CONFIG_STORAGE_TYPE.lower()
            else:
                self.environment[
                    osetupcons.ConfigEnv.STORAGE_TYPE
                ] = self.dialog.queryString(
                    name='OVESETUP_CONFIG_STORAGE_TYPE',
                    note=_(
                        'Default storage type: (@VALUES@) [@DEFAULT@]: '
                    ),
                    prompt=True,
                    validValues=(
                        'NFS',
                        'FC',
                        'ISCSI',
                        'POSIXFS'
                    ),
                    caseSensitive=False,
                    default=osetupcons.Defaults.DEFAULT_CONFIG_STORAGE_TYPE,
                )

    @plugin.event(
        stage=plugin.Stages.STAGE_MISC,
        after=(
            osetupcons.Stages.DB_CONNECTION_AVAILABLE,
        ),
        condition=lambda self: self._enabled,
    )
    def _misc(self):
        self.environment[osetupcons.DBEnv.STATEMENT].execute(
            statement="""
                select inst_update_default_storage_pool_type (%(type)s)
            """,
            args={
                'type': self.STORAGE_TYPES[
                    self.environment[osetupcons.ConfigEnv.STORAGE_TYPE]
                ],
            },
        )

# vim: expandtab tabstop=4 shiftwidth=4
<|endoftext|>


<commit_before>#!/usr/bin/python
# -*- coding: utf-8 -*-
'''
Created on Sep, 14 2013

.. module:: weblyzard_api.xml_content.translate
   :synopsis: translates deprecated versions of the weblyzardXml Format to the current version.

.. moduleauthor: Albert Weichselbraun <albert.weichselbraun@htwchur.ch>
'''

import logging
from lxml import etree

DOCUMENT_NAMESPACE  = {'wl': 'http://www.weblyzard.com/wl/2013#',
                       'dc': 'http://purl.org/dc/elements/1.1/',
                       'xml': 'http://www.w3.org/XML/1998/namespace',
                       }

SENTENCE_ATTRIBUTES = {
    'pos_tag_string': '{%s}%s' % (DOCUMENT_NAMESPACE['wl'], 'pos'),
    'token_indices' : '{%s}%s' % (DOCUMENT_NAMESPACE['wl'], 'token'),
    'significance'  : '{%s}%s' % (DOCUMENT_NAMESPACE['wl'], 'significance'),
    'sem_orient'    : '{%s}%s' % (DOCUMENT_NAMESPACE['wl'], 'sem_orient'),
    'md5sum'        : '{%s}%s' % (DOCUMENT_NAMESPACE['wl'], 'id'),
}.items()

logger = logging.getLogger('wl_core.xml_content')

class WeblyzardXML2005(object):
    '''
    Translates the
       http://www.weblyzard.com/wl/2005
    format into a
       http://www.weblyzard.com/wl/2013#
    compatible XML format.

    .. warning::
       This code solely relies on string substitutions and, therefore, is not
       robust against changes in the tag alignment.
    '''

    PAGE_START_TRANSLATION_TABLE = {
        'xmlns:wl="http://www.weblyzard.com/wl/2005"': 'xmlns:wl="http://www.weblyzard.com/wl/2013#" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:xml="http://www.w3.org/XML/1998/namespace"',
        'content_id="': 'wl:id="',
        'lang="'      : 'xml:lang="',
        'nilsimsa="'  : 'wl:nilsimsa="',
        'title="'     : 'dc:title="',
        'type="'      : 'dc:format="',}.items()

    PAGE_END_TRANSLATION_TABLE = { '</page>': '</wl:page>' }.items()

    SENTENCE_TRANSLATION_TABLE = {
        'pos="'         : 'wl:pos="',
        'id="'          : 'wl:id="',
        'token="'       : 'wl:token="',
        'sem_orient="'  : 'wl:sem_orient="',
        'significance="': 'wl:significance="',
    }.items()

    @classmethod
    def translate(cls, xml_content):
        '''
        ... :param xml_content: the XML data to translate
        '''
        result = []
        for line in xml_content.split("\n"):
            if line.startswith('<page '):
                result.append(cls.translate_line(line, cls.PAGE_START_TRANSLATION_TABLE))
            elif line.startswith('</page>'):
                result.append(cls.translate_line(line, cls.PAGE_END_TRANSLATION_TABLE))
            elif line.startswith('<wl:sentence '):
                result.append(cls.translate_line(line, cls.SENTENCE_TRANSLATION_TABLE))
            elif line.startswith('</wl:sentence>'):
                continue
            else:
                raise ValueError(line)

        return '\n'.join(result)

    @classmethod
    def translate_line(cls, line, translation_table):
        '''
        .. :param line: the XML line to translate
        .. :param translation_table': a list of tuples of the form
                                      [(orig, translated), ... ] used for
                                      translating the line into the new
                                      format.
        '''
        for orig, translated in translation_table:
            line = line.replace(orig, translated)
        return line

<commit_msg>Improved the robustness of the translator against newlines in the CDATA section.<commit_after>#!/usr/bin/python
# -*- coding: utf-8 -*-
'''
Created on Sep, 14 2013

.. module:: weblyzard_api.xml_content.translate
   :synopsis: translates deprecated versions of the weblyzardXml Format to the current version.

.. moduleauthor: Albert Weichselbraun <albert.weichselbraun@htwchur.ch>
'''

import logging
from lxml import etree

DOCUMENT_NAMESPACE  = {'wl': 'http://www.weblyzard.com/wl/2013#',
                       'dc': 'http://purl.org/dc/elements/1.1/',
                       'xml': 'http://www.w3.org/XML/1998/namespace',
                       }

SENTENCE_ATTRIBUTES = {
    'pos_tag_string': '{%s}%s' % (DOCUMENT_NAMESPACE['wl'], 'pos'),
    'token_indices' : '{%s}%s' % (DOCUMENT_NAMESPACE['wl'], 'token'),
    'significance'  : '{%s}%s' % (DOCUMENT_NAMESPACE['wl'], 'significance'),
    'sem_orient'    : '{%s}%s' % (DOCUMENT_NAMESPACE['wl'], 'sem_orient'),
    'md5sum'        : '{%s}%s' % (DOCUMENT_NAMESPACE['wl'], 'id'),
}.items()

logger = logging.getLogger('wl_core.xml_content')

class WeblyzardXML2005(object):
    '''
    Translates the
       http://www.weblyzard.com/wl/2005
    format into a
       http://www.weblyzard.com/wl/2013#
    compatible XML format.

    .. warning::
       This code solely relies on string substitutions and, therefore, is not
       robust against changes in the tag alignment.
    '''

    PAGE_START_TRANSLATION_TABLE = {
        'xmlns:wl="http://www.weblyzard.com/wl/2005"': 'xmlns:wl="http://www.weblyzard.com/wl/2013#" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:xml="http://www.w3.org/XML/1998/namespace"',
        'content_id="': 'wl:id="',
        'lang="'      : 'xml:lang="',
        'nilsimsa="'  : 'wl:nilsimsa="',
        'title="'     : 'dc:title="',
        'type="'      : 'dc:format="',}.items()

    PAGE_END_TRANSLATION_TABLE = { '</page>': '</wl:page>' }.items()

    SENTENCE_TRANSLATION_TABLE = {
        'pos="'         : 'wl:pos="',
        'id="'          : 'wl:id="',
        'token="'       : 'wl:token="',
        'sem_orient="'  : 'wl:sem_orient="',
        'significance="': 'wl:significance="',
    }.items()

    @classmethod
    def translate(cls, xml_content):
        '''
        ... :param xml_content: the XML data to translate
        '''
        result = []
        for line in xml_content.split(">\n"):
            line = line.strip()
            if line.startswith('<page '):
                result.append(cls.translate_line(line, cls.PAGE_START_TRANSLATION_TABLE))
            elif line.startswith('</page'):
                result.append(cls.translate_line(line, cls.PAGE_END_TRANSLATION_TABLE))
            elif line.startswith('<wl:sentence '):
                result.append(cls.translate_line(line, cls.SENTENCE_TRANSLATION_TABLE))
            elif line.startswith('</wl:sentence'):
                continue
            elif not line:
                continue
            else:
                raise ValueError(line)

        return '>\n'.join(result)

    @classmethod
    def translate_line(cls, line, translation_table):
        '''
        .. :param line: the XML line to translate
        .. :param translation_table': a list of tuples of the form
                                      [(orig, translated), ... ] used for
                                      translating the line into the new
                                      format.
        '''
        for orig, translated in translation_table:
            line = line.replace(orig, translated)
        return line

<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
from zeit.cms.tagging.interfaces import Result
from zeit.cms.workflow.interfaces import IPublishInfo
import json
import lxml.builder
import mock
import zeit.cms.tagging.interfaces
import zeit.content.rawxml.rawxml
import zeit.retresco.connection
import zeit.retresco.interfaces
import zeit.retresco.testing
import zope.component

class TMSTest(zeit.retresco.testing.FunctionalTestCase):

    def setUp(self):
        super(TMSTest, self).setUp()
        patcher = mock.patch(
            'zeit.retresco.convert.TMSRepresentation._validate')
        validate = patcher.start()
        validate.return_value = True
        self.addCleanup(patcher.stop)

    def test_extract_keywords_converts_response_to_tag_objects(self):
        self.layer['request_handler'].response_body = json.dumps({
            'rtr_persons': ['Merkel', 'Obama'],
            'rtr_locations': ['Berlin', 'Washington'],
        })
        tms = zope.component.getUtility(zeit.retresco.interfaces.ITMS)
        result = tms.extract_keywords(self.repository['testcontent'])
        self.assertEqual(['Berlin', 'Merkel', 'Obama', 'Washington'],
                         sorted([x.label for x in result]))

    def test_search_keywords_returns_a_list_of_tag_objects(self):
        self.layer['request_handler'].response_body = json.dumps({
            "entities": [{"entity_id": "e8ed9435b876196564bb86599009456cbb2aa",
                          "doc_count": 3,
                          "entity_name": "Schmerz",
                          "entity_type": "keyword"},
                         {"entity_id": "bf87d9ce8457a96fe2de1eac6d9614aa462ba",
                          "doc_count": 1,
                          "entity_name": u"Walter Schmögner",
                          "entity_type": "person"}],
            "num_found": 2})
        tms = zope.component.getUtility(zeit.retresco.interfaces.ITMS)
        result = list(tms.get_keywords('Sch'))
        self.assertEqual(2, len(result))
        self.assertTrue(zeit.cms.tagging.interfaces.ITag.providedBy(result[0]))
        self.assertEqual([u'Schmerz', u'Walter Schmögner'],
                         [x.label for x in result])

    def test_raises_technical_error_for_5xx(self):
        self.layer['request_handler'].response_code = 500
        tms = zope.component.getUtility(zeit.retresco.interfaces.ITMS)
        with self.assertRaises(zeit.retresco.interfaces.TechnicalError):
            tms.extract_keywords(self.repository['testcontent'])

    def test_raises_domain_error_for_4xx(self):
        self.layer['request_handler'].response_code = 400
        tms = zope.component.getUtility(zeit.retresco.interfaces.ITMS)
        with self.assertRaises(zeit.retresco.interfaces.TMSError):
            tms.extract_keywords(self.repository['testcontent'])

    def test_ignores_404_on_delete(self):
        self.layer['request_handler'].response_code = 404
        tms = zope.component.getUtility(zeit.retresco.interfaces.ITMS)
        tms.delete_id('any')

    def test_get_topicpage_documents_pagination(self):
        tms = zope.component.getUtility(zeit.retresco.interfaces.ITMS)
        with mock.patch.object(tms, '_request') as request:
            request.return_value = {'num_found': 0, 'docs': []}
            # Default values
            tms.get_topicpage_documents('tms-id')
            self.assertEqual(1, request.call_args[1]['params']['page'])
            self.assertEqual(25, request.call_args[1]['params']['rows'])
            # Passes through rows
            tms.get_topicpage_documents('tms-id', 0, 7)
            self.assertEqual(1, request.call_args[1]['params']['page'])
            self.assertEqual(7, request.call_args[1]['params']['rows'])
            # Calculates page from start
            tms.get_topicpage_documents('tms-id', 5, 5)
            self.assertEqual(2, request.call_args[1]['params']['page'])
            tms.get_topicpage_documents('tms-id', 10, 5)
            self.assertEqual(3, request.call_args[1]['params']['page'])

    def test_get_topicpage_pulls_up_payload_keys(self):
        self.layer['request_handler'].response_body = json.dumps({
            'num_found': 1,
            'docs': [{
                'url': '/testcontent',
                'doc_type': 'testcontenttype',
                'doc_id': 'uuid',
                'rtr_keywords': ['Berlin', 'Washington'],
                'payload': {
                    'supertitle': 'supertitle',
                    'title': 'title',
                }
            }],
        })
        tms = zope.component.getUtility(zeit.retresco.interfaces.ITMS)
        result = tms.get_topicpage_documents('tms-id')
        self.assertEqual({
            'uniqueId': 'http://xml.zeit.de/testcontent',
            'doc_type': 'testcontenttype',
            'doc_id': 'uuid',
            'rtr_keywords': ['Berlin', 'Washington'],
            'supertitle': 'supertitle',
            'title': 'title',
        }, result[0])

    def test_get_topicpages_pagination(self):
        tms = zope.component.getUtility(zeit.retresco.interfaces.ITMS)
        with mock.patch.object(tms, '_request') as request:
            request.return_value = {'num_found': 0, 'docs': []}
            # Default values
            tms.get_topicpages()
            self.assertEqual(1, request.call_args[1]['params']['page'])
            self.assertEqual(25, request.call_args[1]['params']['rows'])
            # Passes through rows
            tms.get_topicpages(0, 7)
            self.assertEqual(1, request.call_args[1]['params']['page'])
            self.assertEqual(7, request.call_args[1]['params']['rows'])
            # Calculates page from start
            tms.get_topicpages(5, 5)
            self.assertEqual(2, request.call_args[1]['params']['page'])
            tms.get_topicpages(10, 5)
            self.assertEqual(3, request.call_args[1]['params']['page'])

    def test_get_topicpages_dicts_have_id_and_title(self):
        tms = zope.component.getUtility(zeit.retresco.interfaces.ITMS)
        self.layer['request_handler'].response_body = json.dumps({
            'num_found': 1,
            'docs': [{
                'url': '/thema/mytopic',
                'doc_id': 'mytopic',
                'query_terms': ['Berlin', 'Washington'],
                'title': 'Mytopic',
                # lots of fields of the actual response omitted.
            }],
        })
        result = tms.get_topicpages()
        self.assertEqual(1, result.hits)
        self.assertEqual('mytopic', result[0]['id'])
        self.assertEqual('Mytopic', result[0]['title'])

    def test_topicpages_are_available_as_utility(self):
        tms = zope.component.getUtility(zeit.retresco.interfaces.ITMS)
        topics = zope.component.getUtility(
            zeit.cms.tagging.interfaces.ITopicpages)
        with mock.patch.object(tms, 'get_topicpages') as get:
            result = Result([{'id': 'mytopic', 'title': 'Mytopic'}])
            result.hits = 1
            get.return_value = result
            result = topics.get_topics()
            self.assertEqual(1, result.hits)
            self.assertEqual('mytopic', result[0]['id'])
            self.assertEqual('Mytopic', result[0]['title'])

    def test_get_all_topicpages_delegates_to_get_topicpages(self):
        tms = zope.component.getUtility(zeit.retresco.interfaces.ITMS)
        with mock.patch.object(tms, 'get_topicpages') as get:
            get.side_effect = [
                Result([{'id': 'mytopic', 'title': 'Mytopic'}]),
                Result(),
            ]
            result = list(tms.get_all_topicpages())
            self.assertEqual(1, len(result))
            self.assertEqual('mytopic', result[0]['id'])
            self.assertEqual('Mytopic', result[0]['title'])

class TopiclistUpdateTest(zeit.retresco.testing.FunctionalTestCase):

    def test_updates_configured_content_and_publishes(self):
        self.repository['topics'] = zeit.content.rawxml.rawxml.RawXML()
        config = zope.app.appsetup.product.getProductConfiguration(
            'zeit.retresco')
        config['topiclist'] = 'http://xml.zeit.de/topics'
        with mock.patch('zeit.retresco.connection._build_topic_xml') as xml:
            E = lxml.builder.ElementMaker()
            xml.return_value = E.topics(
                E.topic('Berlin', url_value='berlin')
            )
            zeit.retresco.connection._update_topiclist()
        topics = self.repository['topics']
        self.assertEqual(1, len(topics.xml.xpath('//topic')))
        self.assertEqual('topics', topics.xml.tag)
        self.assertEqual('Berlin', topics.xml.find('topic')[0])
        self.assertEqual(True, IPublishInfo(topics).published)
<commit_msg>Match wording of method to test.<commit_after># -*- coding: utf-8 -*-
from zeit.cms.tagging.interfaces import Result
from zeit.cms.workflow.interfaces import IPublishInfo
import json
import lxml.builder
import mock
import zeit.cms.tagging.interfaces
import zeit.content.rawxml.rawxml
import zeit.retresco.connection
import zeit.retresco.interfaces
import zeit.retresco.testing
import zope.component

class TMSTest(zeit.retresco.testing.FunctionalTestCase):

    def setUp(self):
        super(TMSTest, self).setUp()
        patcher = mock.patch(
            'zeit.retresco.convert.TMSRepresentation._validate')
        validate = patcher.start()
        validate.return_value = True
        self.addCleanup(patcher.stop)

    def test_extract_keywords_converts_response_to_tag_objects(self):
        self.layer['request_handler'].response_body = json.dumps({
            'rtr_persons': ['Merkel', 'Obama'],
            'rtr_locations': ['Berlin', 'Washington'],
        })
        tms = zope.component.getUtility(zeit.retresco.interfaces.ITMS)
        result = tms.extract_keywords(self.repository['testcontent'])
        self.assertEqual(['Berlin', 'Merkel', 'Obama', 'Washington'],
                         sorted([x.label for x in result]))

    def test_get_keywords_returns_a_list_of_tag_objects(self):
        self.layer['request_handler'].response_body = json.dumps({
            "entities": [{"entity_id": "e8ed9435b876196564bb86599009456cbb2aa",
                          "doc_count": 3,
                          "entity_name": "Schmerz",
                          "entity_type": "keyword"},
                         {"entity_id": "bf87d9ce8457a96fe2de1eac6d9614aa462ba",
                          "doc_count": 1,
                          "entity_name": u"Walter Schmögner",
                          "entity_type": "person"}],
            "num_found": 2})
        tms = zope.component.getUtility(zeit.retresco.interfaces.ITMS)
        result = list(tms.get_keywords('Sch'))
        self.assertEqual(2, len(result))
        self.assertTrue(zeit.cms.tagging.interfaces.ITag.providedBy(result[0]))
        self.assertEqual([u'Schmerz', u'Walter Schmögner'],
                         [x.label for x in result])

    def test_raises_technical_error_for_5xx(self):
        self.layer['request_handler'].response_code = 500
        tms = zope.component.getUtility(zeit.retresco.interfaces.ITMS)
        with self.assertRaises(zeit.retresco.interfaces.TechnicalError):
            tms.extract_keywords(self.repository['testcontent'])

    def test_raises_domain_error_for_4xx(self):
        self.layer['request_handler'].response_code = 400
        tms = zope.component.getUtility(zeit.retresco.interfaces.ITMS)
        with self.assertRaises(zeit.retresco.interfaces.TMSError):
            tms.extract_keywords(self.repository['testcontent'])

    def test_ignores_404_on_delete(self):
        self.layer['request_handler'].response_code = 404
        tms = zope.component.getUtility(zeit.retresco.interfaces.ITMS)
        tms.delete_id('any')

    def test_get_topicpage_documents_pagination(self):
        tms = zope.component.getUtility(zeit.retresco.interfaces.ITMS)
        with mock.patch.object(tms, '_request') as request:
            request.return_value = {'num_found': 0, 'docs': []}
            # Default values
            tms.get_topicpage_documents('tms-id')
            self.assertEqual(1, request.call_args[1]['params']['page'])
            self.assertEqual(25, request.call_args[1]['params']['rows'])
            # Passes through rows
            tms.get_topicpage_documents('tms-id', 0, 7)
            self.assertEqual(1, request.call_args[1]['params']['page'])
            self.assertEqual(7, request.call_args[1]['params']['rows'])
            # Calculates page from start
            tms.get_topicpage_documents('tms-id', 5, 5)
            self.assertEqual(2, request.call_args[1]['params']['page'])
            tms.get_topicpage_documents('tms-id', 10, 5)
            self.assertEqual(3, request.call_args[1]['params']['page'])

    def test_get_topicpage_pulls_up_payload_keys(self):
        self.layer['request_handler'].response_body = json.dumps({
            'num_found': 1,
            'docs': [{
                'url': '/testcontent',
                'doc_type': 'testcontenttype',
                'doc_id': 'uuid',
                'rtr_keywords': ['Berlin', 'Washington'],
                'payload': {
                    'supertitle': 'supertitle',
                    'title': 'title',
                }
            }],
        })
        tms = zope.component.getUtility(zeit.retresco.interfaces.ITMS)
        result = tms.get_topicpage_documents('tms-id')
        self.assertEqual({
            'uniqueId': 'http://xml.zeit.de/testcontent',
            'doc_type': 'testcontenttype',
            'doc_id': 'uuid',
            'rtr_keywords': ['Berlin', 'Washington'],
            'supertitle': 'supertitle',
            'title': 'title',
        }, result[0])

    def test_get_topicpages_pagination(self):
        tms = zope.component.getUtility(zeit.retresco.interfaces.ITMS)
        with mock.patch.object(tms, '_request') as request:
            request.return_value = {'num_found': 0, 'docs': []}
            # Default values
            tms.get_topicpages()
            self.assertEqual(1, request.call_args[1]['params']['page'])
            self.assertEqual(25, request.call_args[1]['params']['rows'])
            # Passes through rows
            tms.get_topicpages(0, 7)
            self.assertEqual(1, request.call_args[1]['params']['page'])
            self.assertEqual(7, request.call_args[1]['params']['rows'])
            # Calculates page from start
            tms.get_topicpages(5, 5)
            self.assertEqual(2, request.call_args[1]['params']['page'])
            tms.get_topicpages(10, 5)
            self.assertEqual(3, request.call_args[1]['params']['page'])

    def test_get_topicpages_dicts_have_id_and_title(self):
        tms = zope.component.getUtility(zeit.retresco.interfaces.ITMS)
        self.layer['request_handler'].response_body = json.dumps({
            'num_found': 1,
            'docs': [{
                'url': '/thema/mytopic',
                'doc_id': 'mytopic',
                'query_terms': ['Berlin', 'Washington'],
                'title': 'Mytopic',
                # lots of fields of the actual response omitted.
            }],
        })
        result = tms.get_topicpages()
        self.assertEqual(1, result.hits)
        self.assertEqual('mytopic', result[0]['id'])
        self.assertEqual('Mytopic', result[0]['title'])

    def test_topicpages_are_available_as_utility(self):
        tms = zope.component.getUtility(zeit.retresco.interfaces.ITMS)
        topics = zope.component.getUtility(
            zeit.cms.tagging.interfaces.ITopicpages)
        with mock.patch.object(tms, 'get_topicpages') as get:
            result = Result([{'id': 'mytopic', 'title': 'Mytopic'}])
            result.hits = 1
            get.return_value = result
            result = topics.get_topics()
            self.assertEqual(1, result.hits)
            self.assertEqual('mytopic', result[0]['id'])
            self.assertEqual('Mytopic', result[0]['title'])

    def test_get_all_topicpages_delegates_to_get_topicpages(self):
        tms = zope.component.getUtility(zeit.retresco.interfaces.ITMS)
        with mock.patch.object(tms, 'get_topicpages') as get:
            get.side_effect = [
                Result([{'id': 'mytopic', 'title': 'Mytopic'}]),
                Result(),
            ]
            result = list(tms.get_all_topicpages())
            self.assertEqual(1, len(result))
            self.assertEqual('mytopic', result[0]['id'])
            self.assertEqual('Mytopic', result[0]['title'])

class TopiclistUpdateTest(zeit.retresco.testing.FunctionalTestCase):

    def test_updates_configured_content_and_publishes(self):
        self.repository['topics'] = zeit.content.rawxml.rawxml.RawXML()
        config = zope.app.appsetup.product.getProductConfiguration(
            'zeit.retresco')
        config['topiclist'] = 'http://xml.zeit.de/topics'
        with mock.patch('zeit.retresco.connection._build_topic_xml') as xml:
            E = lxml.builder.ElementMaker()
            xml.return_value = E.topics(
                E.topic('Berlin', url_value='berlin')
            )
            zeit.retresco.connection._update_topiclist()
        topics = self.repository['topics']
        self.assertEqual(1, len(topics.xml.xpath('//topic')))
        self.assertEqual('topics', topics.xml.tag)
        self.assertEqual('Berlin', topics.xml.find('topic')[0])
        self.assertEqual(True, IPublishInfo(topics).published)
<|endoftext|>


<commit_before># Licensed to the StackStorm, Inc ('StackStorm') under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You 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 tempfile

import mock
from oslo_config import cfg

from st2common import service_setup
from st2common.transport.bootstrap_utils import register_exchanges
from st2common.transport.bootstrap_utils import QUEUES

from st2tests.base import CleanFilesTestCase
from st2tests import config

__all__ = [
    'ServiceSetupTestCase'
]

MOCK_LOGGING_CONFIG_INVALID_LOG_LEVEL = """
[loggers]
keys=root

[handlers]
keys=consoleHandler

[formatters]
keys=simpleConsoleFormatter

[logger_root]
level=invalid_log_level
handlers=consoleHandler

[handler_consoleHandler]
class=StreamHandler
level=DEBUG
formatter=simpleConsoleFormatter
args=(sys.stdout,)

[formatter_simpleConsoleFormatter]
class=st2common.logging.formatters.ConsoleLogFormatter
format=%(asctime)s %(levelname)s [-] %(message)s
datefmt=
""".strip()

class ServiceSetupTestCase(CleanFilesTestCase):
    def test_no_logging_config_found(self):
        def mock_get_logging_config_path():
            return ''

        config.get_logging_config_path = mock_get_logging_config_path

        expected_msg = "No section: .*"
        self.assertRaisesRegexp(Exception, expected_msg,
                                service_setup.setup, service='api',
                                config=config,
                                setup_db=False, register_mq_exchanges=False,
                                register_signal_handlers=False,
                                register_internal_trigger_types=False,
                                run_migrations=False)

    def test_invalid_log_level_friendly_error_message(self):
        _, mock_logging_config_path = tempfile.mkstemp()
        self.to_delete_files.append(mock_logging_config_path)

        with open(mock_logging_config_path, 'w') as fp:
            fp.write(MOCK_LOGGING_CONFIG_INVALID_LOG_LEVEL)

        def mock_get_logging_config_path():
            return mock_logging_config_path

        config.get_logging_config_path = mock_get_logging_config_path

        expected_msg = 'Invalid log level selected. Log level names need to be all uppercase'
        self.assertRaisesRegexp(KeyError, expected_msg,
                                service_setup.setup, service='api',
                                config=config,
                                setup_db=False, register_mq_exchanges=False,
                                register_signal_handlers=False,
                                register_internal_trigger_types=False,
                                run_migrations=False)

    @mock.patch('kombu.Queue.declare')
    def test_register_exchanges_predeclare_queues(self, mock_declare):
        # Verify that queues are correctly pre-declared if the corresponding config option is set

        # Pre-declaration is disabled
        self.assertEqual(mock_declare.call_count, 0)
        cfg.CONF.set_override(group='messaging', name='predeclare_queues', override=False)

        register_exchanges()
        self.assertEqual(mock_declare.call_count, 0)

        # Pre-declaration is enabled
        self.assertEqual(mock_declare.call_count, 0)
        cfg.CONF.set_override(group='messaging', name='predeclare_queues', override=True)

        register_exchanges()
        self.assertEqual(mock_declare.call_count, len(QUEUES))
<commit_msg>Update affected test.<commit_after># Licensed to the StackStorm, Inc ('StackStorm') under one or more
# contributor license agreements.  See the NOTICE file distributed with
# this work for additional information regarding copyright ownership.
# The ASF licenses this file to You 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 tempfile

import mock

from st2common import service_setup
from st2common.transport.bootstrap_utils import register_exchanges
from st2common.transport.bootstrap_utils import QUEUES

from st2tests.base import CleanFilesTestCase
from st2tests import config

__all__ = [
    'ServiceSetupTestCase'
]

MOCK_LOGGING_CONFIG_INVALID_LOG_LEVEL = """
[loggers]
keys=root

[handlers]
keys=consoleHandler

[formatters]
keys=simpleConsoleFormatter

[logger_root]
level=invalid_log_level
handlers=consoleHandler

[handler_consoleHandler]
class=StreamHandler
level=DEBUG
formatter=simpleConsoleFormatter
args=(sys.stdout,)

[formatter_simpleConsoleFormatter]
class=st2common.logging.formatters.ConsoleLogFormatter
format=%(asctime)s %(levelname)s [-] %(message)s
datefmt=
""".strip()

class ServiceSetupTestCase(CleanFilesTestCase):
    def test_no_logging_config_found(self):
        def mock_get_logging_config_path():
            return ''

        config.get_logging_config_path = mock_get_logging_config_path

        expected_msg = "No section: .*"
        self.assertRaisesRegexp(Exception, expected_msg,
                                service_setup.setup, service='api',
                                config=config,
                                setup_db=False, register_mq_exchanges=False,
                                register_signal_handlers=False,
                                register_internal_trigger_types=False,
                                run_migrations=False)

    def test_invalid_log_level_friendly_error_message(self):
        _, mock_logging_config_path = tempfile.mkstemp()
        self.to_delete_files.append(mock_logging_config_path)

        with open(mock_logging_config_path, 'w') as fp:
            fp.write(MOCK_LOGGING_CONFIG_INVALID_LOG_LEVEL)

        def mock_get_logging_config_path():
            return mock_logging_config_path

        config.get_logging_config_path = mock_get_logging_config_path

        expected_msg = 'Invalid log level selected. Log level names need to be all uppercase'
        self.assertRaisesRegexp(KeyError, expected_msg,
                                service_setup.setup, service='api',
                                config=config,
                                setup_db=False, register_mq_exchanges=False,
                                register_signal_handlers=False,
                                register_internal_trigger_types=False,
                                run_migrations=False)

    @mock.patch('kombu.Queue.declare')
    def test_register_exchanges_predeclare_queues(self, mock_declare):
        # Verify that queues are correctly pre-declared
        self.assertEqual(mock_declare.call_count, 0)

        register_exchanges()
        self.assertEqual(mock_declare.call_count, len(QUEUES))
<|endoftext|>


<commit_before>import os.path

from UM.Application import Application
from UM.Logger import Logger
from UM.Message import Message
from UM.Mesh.WriteMeshJob import WriteMeshJob
from UM.Mesh.MeshWriter import MeshWriter
from UM.Scene.Iterator.BreadthFirstIterator import BreadthFirstIterator
from UM.OutputDevice.OutputDevice import OutputDevice
from UM.OutputDevice import OutputDeviceError
from UM.Preferences import Preferences

from UM.i18n import i18nCatalog
catalog = i18nCatalog("cura")

class RemovableDriveOutputDevice(OutputDevice):
    def __init__(self, device_id, device_name):
        super().__init__(device_id)

        self.setName(device_name)
        self.setShortDescription(catalog.i18nc("@action:button", "Save to Removable Drive"))
        self.setDescription(catalog.i18nc("@item:inlistbox", "Save to Removable Drive {0}").format(device_name))
        self.setIconName("save_sd")
        self.setPriority(1)

        self._writing = False

    def requestWrite(self, node, file_name = None, filter_by_machine = False):
        if self._writing:
            raise OutputDeviceError.DeviceBusyError()

        file_formats = Application.getInstance().getMeshFileHandler().getSupportedFileTypesWrite() #Formats supported by this application.
        if filter_by_machine:
            machine_file_formats = Application.getInstance().getMachineManager().getActiveMachineInstance().getMachineDefinition().getFileFormats()
            file_formats = list(filter(lambda file_format: file_format["mime_type"] in machine_file_formats, file_formats)) #Take the intersection between file_formats and machine_file_formats.
        if len(file_formats) == 0:
            Logger.log("e", "There are no file formats available to write with!")
            raise OutputDeviceError.WriteRequestFailedError()
        writer = Application.getInstance().getMeshFileHandler().getWriterByMimeType(file_formats[0]["mime_type"]) #Just take the first file format available.
        extension = file_format[0]["extension"]

        if file_name == None:
            for n in BreadthFirstIterator(node):
                if n.getMeshData():
                    file_name = n.getName()
                    if file_name:
                        break

        if not file_name:
            Logger.log("e", "Could not determine a proper file name when trying to write to %s, aborting", self.getName())
            raise OutputDeviceError.WriteRequestFailedError()

        if extension: #Not empty string.
            extension = "." + extension
        file_name = os.path.join(self.getId(), os.path.splitext(file_name)[0] + extension)

        try:
            Logger.log("d", "Writing to %s", file_name)
            stream = open(file_name, "wt")
            job = WriteMeshJob(writer, stream, node, MeshWriter.OutputMode.TextMode)
            job.setFileName(file_name)
            job.progress.connect(self._onProgress)
            job.finished.connect(self._onFinished)

            message = Message(catalog.i18nc("@info:progress", "Saving to Removable Drive <filename>{0}</filename>").format(self.getName()), 0, False, -1)
            message.show()

            self.writeStarted.emit(self)

            job._message = message
            self._writing = True
            job.start()
        except PermissionError as e:
            Logger.log("e", "Permission denied when trying to write to %s: %s", file_name, str(e))
            raise OutputDeviceError.PermissionDeniedError() from e
        except OSError as e:
            Logger.log("e", "Operating system would not let us write to %s: %s", file_name, str(e))
            raise OutputDeviceError.WriteRequestFailedError() from e

    def _onProgress(self, job, progress):
        if hasattr(job, "_message"):
            job._message.setProgress(progress)
        self.writeProgress.emit(self, progress)

    def _onFinished(self, job):
        if hasattr(job, "_message"):
            job._message.hide()
            job._message = None

        self._writing = False
        self.writeFinished.emit(self)
        if job.getResult():
            message = Message(catalog.i18nc("@info:status", "Saved to Removable Drive {0} as {1}").format(self.getName(), os.path.basename(job.getFileName())))
            message.addAction("eject", catalog.i18nc("@action:button", "Eject"), "eject", catalog.i18nc("@action", "Eject removable device {0}").format(self.getName()))
            message.actionTriggered.connect(self._onActionTriggered)
            message.show()
            self.writeSuccess.emit(self)
        else:
            message = Message(catalog.i18nc("@info:status", "Could not save to removable drive {0}: {1}").format(self.getName(), str(job.getError())))
            message.show()
            self.writeError.emit(self)
        job.getStream().close()

    def _onActionTriggered(self, message, action):
        if action == "eject":
            Application.getInstance().getOutputDeviceManager().getOutputDevicePlugin("RemovableDriveOutputDevice").ejectDevice(self)

<commit_msg>Repair saving to removable drive<commit_after>import os.path

from UM.Application import Application
from UM.Logger import Logger
from UM.Message import Message
from UM.Mesh.WriteMeshJob import WriteMeshJob
from UM.Mesh.MeshWriter import MeshWriter
from UM.Scene.Iterator.BreadthFirstIterator import BreadthFirstIterator
from UM.OutputDevice.OutputDevice import OutputDevice
from UM.OutputDevice import OutputDeviceError
from UM.Preferences import Preferences

from UM.i18n import i18nCatalog
catalog = i18nCatalog("cura")

class RemovableDriveOutputDevice(OutputDevice):
    def __init__(self, device_id, device_name):
        super().__init__(device_id)

        self.setName(device_name)
        self.setShortDescription(catalog.i18nc("@action:button", "Save to Removable Drive"))
        self.setDescription(catalog.i18nc("@item:inlistbox", "Save to Removable Drive {0}").format(device_name))
        self.setIconName("save_sd")
        self.setPriority(1)

        self._writing = False

    def requestWrite(self, node, file_name = None, filter_by_machine = False):
        if self._writing:
            raise OutputDeviceError.DeviceBusyError()

        file_formats = Application.getInstance().getMeshFileHandler().getSupportedFileTypesWrite() #Formats supported by this application.
        if filter_by_machine:
            machine_file_formats = Application.getInstance().getMachineManager().getActiveMachineInstance().getMachineDefinition().getFileFormats()
            file_formats = list(filter(lambda file_format: file_format["mime_type"] in machine_file_formats, file_formats)) #Take the intersection between file_formats and machine_file_formats.
        if len(file_formats) == 0:
            Logger.log("e", "There are no file formats available to write with!")
            raise OutputDeviceError.WriteRequestFailedError()
        writer = Application.getInstance().getMeshFileHandler().getWriterByMimeType(file_formats[0]["mime_type"]) #Just take the first file format available.
        extension = file_formats[0]["extension"]

        if file_name == None:
            for n in BreadthFirstIterator(node):
                if n.getMeshData():
                    file_name = n.getName()
                    if file_name:
                        break

        if not file_name:
            Logger.log("e", "Could not determine a proper file name when trying to write to %s, aborting", self.getName())
            raise OutputDeviceError.WriteRequestFailedError()

        if extension: #Not empty string.
            extension = "." + extension
        file_name = os.path.join(self.getId(), os.path.splitext(file_name)[0] + extension)

        try:
            Logger.log("d", "Writing to %s", file_name)
            stream = open(file_name, "wt")
            job = WriteMeshJob(writer, stream, node, MeshWriter.OutputMode.TextMode)
            job.setFileName(file_name)
            job.progress.connect(self._onProgress)
            job.finished.connect(self._onFinished)

            message = Message(catalog.i18nc("@info:progress", "Saving to Removable Drive <filename>{0}</filename>").format(self.getName()), 0, False, -1)
            message.show()

            self.writeStarted.emit(self)

            job._message = message
            self._writing = True
            job.start()
        except PermissionError as e:
            Logger.log("e", "Permission denied when trying to write to %s: %s", file_name, str(e))
            raise OutputDeviceError.PermissionDeniedError() from e
        except OSError as e:
            Logger.log("e", "Operating system would not let us write to %s: %s", file_name, str(e))
            raise OutputDeviceError.WriteRequestFailedError() from e

    def _onProgress(self, job, progress):
        if hasattr(job, "_message"):
            job._message.setProgress(progress)
        self.writeProgress.emit(self, progress)

    def _onFinished(self, job):
        if hasattr(job, "_message"):
            job._message.hide()
            job._message = None

        self._writing = False
        self.writeFinished.emit(self)
        if job.getResult():
            message = Message(catalog.i18nc("@info:status", "Saved to Removable Drive {0} as {1}").format(self.getName(), os.path.basename(job.getFileName())))
            message.addAction("eject", catalog.i18nc("@action:button", "Eject"), "eject", catalog.i18nc("@action", "Eject removable device {0}").format(self.getName()))
            message.actionTriggered.connect(self._onActionTriggered)
            message.show()
            self.writeSuccess.emit(self)
        else:
            message = Message(catalog.i18nc("@info:status", "Could not save to removable drive {0}: {1}").format(self.getName(), str(job.getError())))
            message.show()
            self.writeError.emit(self)
        job.getStream().close()

    def _onActionTriggered(self, message, action):
        if action == "eject":
            Application.getInstance().getOutputDeviceManager().getOutputDevicePlugin("RemovableDriveOutputDevice").ejectDevice(self)

<|endoftext|>


<commit_before># -*- coding:utf-8 -*-
#
# This file is part of OpenFisca.
# OpenFisca is a socio-fiscal microsimulation software
# Copyright © 2011 Clément Schaff, Mahdi Ben Jelloul
# Licensed under the terms of the GPL (version 3 or later) license
# (see openfisca/__init__.py for details)

from __future__ import division

import logging
import datetime

from numpy import logical_not as not_, maximum as max_, minimum as min_, ones

from openfisca_core.accessors import law
from openfisca_core.taxscales import TaxScalesTree, scale_tax_scales

from ..base import FloatCol, Individus, reference_formula, SimpleFormulaColumn, dated_function, DatedFormulaColumn

log = logging.getLogger(__name__)

# Exonération de CSG et de CRDS sur les revenus du chômage
# et des préretraites si cela abaisse ces revenus sous le smic brut
# TODO: mettre un trigger pour l'éxonération
#       des revenus du chômage sous un smic

############################################################################
# # Allocations chômage
############################################################################

def exo_csg_chom(chobrut, csg_rempl, _P):
    '''
    Indicatrice d'exonération de la CSG sur les revenus du chômage sans exo
    '''
    chonet_sans_exo = (chobrut
                        + csgchod_sans_exo(chobrut, csg_rempl, _P)
                        + csgchoi_sans_exo(chobrut, csg_rempl, _P)
                        + crdscho_sans_exo(chobrut, csg_rempl, _P))
    nbh_travail = 35 * 52 / 12  # = 151.67  # TODO: depuis 2001 mais avant ?
    cho_seuil_exo = _P.csg.chom.min_exo * nbh_travail * _P.cotsoc.gen.smic_h_b
    return (chonet_sans_exo <= cho_seuil_exo)

def csgchod_sans_exo(chobrut, csg_rempl, _P):
    '''
    CSG déductible sur les allocations chômage sans exo
    '''
    plaf_ss = _P.cotsoc.gen.plaf_ss
    csg = scale_tax_scales(TaxScalesTree('csg', _P.csg.chom), plaf_ss)
    taux_plein = csg['plein']['deduc'].calc(chobrut)
    taux_reduit = csg['reduit']['deduc'].calc(chobrut)
    csgchod = (csg_rempl == 2) * taux_reduit + (csg_rempl == 3) * taux_plein
    return -csgchod

def csgchoi_sans_exo(chobrut, csg_rempl, _P):
    '''
    CSG imposable sur les allocations chômage sans exo
    '''
    plaf_ss = _P.cotsoc.gen.plaf_ss
    csg = scale_tax_scales(TaxScalesTree('csg', _P.csg.chom), plaf_ss)
    taux_plein = csg['plein']['impos'].calc(chobrut)
    taux_reduit = csg['reduit']['impos'].calc(chobrut)
    csgchoi = (csg_rempl == 2) * taux_reduit + (csg_rempl == 3) * taux_plein
    return -csgchoi

def crdscho_sans_exo(chobrut, csg_rempl, _P):
    '''
    CRDS sur les allocations chômage sans exo
    '''
    plaf_ss = _P.cotsoc.gen.plaf_ss
    crds = scale_tax_scales(_P.crds.act, plaf_ss)  # TODO: Assiette crds éq pour les salariés et les chômeurs en 2014 mais check before
    return -crds.calc(chobrut) * (2 <= csg_rempl)

@reference_formula
class csgchod(SimpleFormulaColumn):
    column = FloatCol
    entity_class = Individus
    label = u"CSG déductible sur les allocations chômage"
    url = u"http://vosdroits.service-public.fr/particuliers/F2329.xhtml"

    def function(self, chobrut, csg_rempl, P = law):
        isexo = exo_csg_chom(chobrut, csg_rempl, P)
        csgchod = csgchod_sans_exo(chobrut, csg_rempl, P) * not_(isexo)
        return csgchod

    def get_output_period(self, period):
        return period.start.offset('first-of', 'month').period('month')

@reference_formula
class csgchoi(SimpleFormulaColumn):
    column = FloatCol
    entity_class = Individus
    label = u"CSG imposable sur les allocations chômage"
    url = u"http://vosdroits.service-public.fr/particuliers/F2329.xhtml"

    def function(self, chobrut, csg_rempl, P = law):
        isexo = exo_csg_chom(chobrut, csg_rempl, P)
        csgchoi = csgchoi_sans_exo(chobrut, csg_rempl, P) * not_(isexo)
        return csgchoi

    def get_output_period(self, period):
        return period.start.offset('first-of', 'month').period('month')

@reference_formula
class crdscho(SimpleFormulaColumn):
    column = FloatCol
    entity_class = Individus
    label = u"CRDS sur les allocations chômage"
    url = u"http://www.insee.fr/fr/methodes/default.asp?page=definitions/contrib-remb-dette-sociale.htm"

    def function(self, chobrut, csg_rempl, P = law):
        isexo = exo_csg_chom(chobrut, csg_rempl, P)
        crdscho = crdscho_sans_exo(chobrut, csg_rempl, P) * not_(isexo)
        return crdscho

    def get_output_period(self, period):
        return period.start.offset('first-of', 'month').period('month')

@reference_formula
class cho(SimpleFormulaColumn):
    column = FloatCol
    entity_class = Individus
    label = u"Allocations chômage imposables"
    url = u"http://www.insee.fr/fr/methodes/default.asp?page=definitions/chomage.htm"

    def function(self, chobrut, csgchod):
        return chobrut + csgchod

    def get_output_period(self, period):
        return period

@reference_formula
class chonet(SimpleFormulaColumn):
    column = FloatCol
    entity_class = Individus
    label = u"Allocations chômage nettes"
    url = u"http://vosdroits.service-public.fr/particuliers/N549.xhtml"

    def function(self, cho, csgchoi, crdscho):
        return cho + csgchoi + crdscho

    def get_output_period(self, period):
        return period

############################################################################
# # Pensions
############################################################################

@reference_formula
class csgrstd(SimpleFormulaColumn):
    column = FloatCol
    entity_class = Individus
    label = u"CSG déductible sur les pensions de retraite"
    url = u"https://www.lassuranceretraite.fr/cs/Satellite/PUBPrincipale/Retraites/Paiement-Votre-Retraite/Prelevements-Sociaux?packedargs=null"

    def function(self, rstbrut, csg_rempl, P = law):
        plaf_ss = P.cotsoc.gen.plaf_ss
        csg = scale_tax_scales(TaxScalesTree('csg', P.csg.retraite), plaf_ss)
        taux_plein = csg['plein']['deduc'].calc(rstbrut)
        taux_reduit = csg['reduit']['deduc'].calc(rstbrut)
        csgrstd = (csg_rempl == 3) * taux_plein + (csg_rempl == 2) * taux_reduit
        return -csgrstd

    def get_output_period(self, period):
        return period.start.offset('first-of', 'month').period('month')

@reference_formula
class csgrsti(SimpleFormulaColumn):
    column = FloatCol
    entity_class = Individus
    label = u"CSG imposable sur les pensions de retraite"
    url = u"https://www.lassuranceretraite.fr/cs/Satellite/PUBPrincipale/Retraites/Paiement-Votre-Retraite/Prelevements-Sociaux?packedargs=null"

    def function(self, rstbrut, csg_rempl, P = law):
        plaf_ss = P.cotsoc.gen.plaf_ss
        csg = scale_tax_scales(TaxScalesTree('csg', P.csg.retraite), plaf_ss)
        taux_plein = csg['plein']['impos'].calc(rstbrut)
        taux_reduit = csg['reduit']['impos'].calc(rstbrut)
        csgrsti = (csg_rempl == 3) * taux_plein + (csg_rempl == 2) * taux_reduit
        return -csgrsti

    def get_output_period(self, period):
        return period.start.offset('first-of', 'month').period('month')

@reference_formula
class crdsrst(SimpleFormulaColumn):
    column = FloatCol
    entity_class = Individus
    label = u"CRDS sur les pensions de retraite"
    url = u"http://www.pensions.bercy.gouv.fr/vous-%C3%AAtes-retrait%C3%A9-ou-pensionn%C3%A9/le-calcul-de-ma-pension/les-pr%C3%A9l%C3%A8vements-effectu%C3%A9s-sur-ma-pension"

    def function(self, rstbrut, csg_rempl, P = law):
        plaf_ss = P.cotsoc.gen.plaf_ss
        crds = scale_tax_scales(TaxScalesTree('crds', P.crds.rst), plaf_ss)
        isexo = (csg_rempl == 1)
        return -crds['rst'].calc(rstbrut) * not_(isexo)

    def get_output_period(self, period):
        return period.start.offset('first-of', 'month').period('month')

@reference_formula
class casa(DatedFormulaColumn):
    column = FloatCol
    entity_class = Individus
    label = u"Contribution additionnelle de solidarité et d'autonomie"
    url = u"http://www.service-public.fr/actualites/002691.html"

    @dated_function(datetime.date(2013, 4, 1))
    def function_2013(self, rstbrut, irpp_holder, csg_rempl, P = law):
        # TODO: replace irpp by irpp_n_2
        # TODO: utiliser la bonne période pour irpp_holder

        irpp = self.cast_from_entity_to_roles(irpp_holder)
        casa = (csg_rempl == 3) * P.prelsoc.add_ret * rstbrut * (irpp > P.ir.recouvrement.seuil)

        return -casa

    def get_output_period(self, period):
        return period.start.offset('first-of', 'month').period('month')

@reference_formula
class rst(SimpleFormulaColumn):
    column = FloatCol
    entity_class = Individus
    label = u"Pensions de retraite imposables"
    url = u"http://vosdroits.service-public.fr/particuliers/F415.xhtml"

    def function(self, rstbrut, csgrstd):
        return rstbrut + csgrstd

    def get_output_period(self, period):
        return period

@reference_formula
class rstnet(SimpleFormulaColumn):
    column = FloatCol
    entity_class = Individus
    label = u"Pensions de retraite nettes"
    url = u"http://vosdroits.service-public.fr/particuliers/N20166.xhtml"

    def function(self, rst, csgrsti, crdsrst, casa):
        return rst + csgrsti + crdsrst + casa

    def get_output_period(self, period):
        return period
<commit_msg>Désactive la CASA sur les retraites<commit_after># -*- coding:utf-8 -*-
#
# This file is part of OpenFisca.
# OpenFisca is a socio-fiscal microsimulation software
# Copyright © 2011 Clément Schaff, Mahdi Ben Jelloul
# Licensed under the terms of the GPL (version 3 or later) license
# (see openfisca/__init__.py for details)

from __future__ import division

import logging
import datetime

from numpy import logical_not as not_, maximum as max_, minimum as min_, ones

from openfisca_core.accessors import law
from openfisca_core.taxscales import TaxScalesTree, scale_tax_scales

from ..base import FloatCol, Individus, reference_formula, SimpleFormulaColumn, dated_function, DatedFormulaColumn

log = logging.getLogger(__name__)

# Exonération de CSG et de CRDS sur les revenus du chômage
# et des préretraites si cela abaisse ces revenus sous le smic brut
# TODO: mettre un trigger pour l'éxonération
#       des revenus du chômage sous un smic

############################################################################
# # Allocations chômage
############################################################################

def exo_csg_chom(chobrut, csg_rempl, _P):
    '''
    Indicatrice d'exonération de la CSG sur les revenus du chômage sans exo
    '''
    chonet_sans_exo = (chobrut
                        + csgchod_sans_exo(chobrut, csg_rempl, _P)
                        + csgchoi_sans_exo(chobrut, csg_rempl, _P)
                        + crdscho_sans_exo(chobrut, csg_rempl, _P))
    nbh_travail = 35 * 52 / 12  # = 151.67  # TODO: depuis 2001 mais avant ?
    cho_seuil_exo = _P.csg.chom.min_exo * nbh_travail * _P.cotsoc.gen.smic_h_b
    return (chonet_sans_exo <= cho_seuil_exo)

def csgchod_sans_exo(chobrut, csg_rempl, _P):
    '''
    CSG déductible sur les allocations chômage sans exo
    '''
    plaf_ss = _P.cotsoc.gen.plaf_ss
    csg = scale_tax_scales(TaxScalesTree('csg', _P.csg.chom), plaf_ss)
    taux_plein = csg['plein']['deduc'].calc(chobrut)
    taux_reduit = csg['reduit']['deduc'].calc(chobrut)
    csgchod = (csg_rempl == 2) * taux_reduit + (csg_rempl == 3) * taux_plein
    return -csgchod

def csgchoi_sans_exo(chobrut, csg_rempl, _P):
    '''
    CSG imposable sur les allocations chômage sans exo
    '''
    plaf_ss = _P.cotsoc.gen.plaf_ss
    csg = scale_tax_scales(TaxScalesTree('csg', _P.csg.chom), plaf_ss)
    taux_plein = csg['plein']['impos'].calc(chobrut)
    taux_reduit = csg['reduit']['impos'].calc(chobrut)
    csgchoi = (csg_rempl == 2) * taux_reduit + (csg_rempl == 3) * taux_plein
    return -csgchoi

def crdscho_sans_exo(chobrut, csg_rempl, _P):
    '''
    CRDS sur les allocations chômage sans exo
    '''
    plaf_ss = _P.cotsoc.gen.plaf_ss
    crds = scale_tax_scales(_P.crds.act, plaf_ss)  # TODO: Assiette crds éq pour les salariés et les chômeurs en 2014 mais check before
    return -crds.calc(chobrut) * (2 <= csg_rempl)

@reference_formula
class csgchod(SimpleFormulaColumn):
    column = FloatCol
    entity_class = Individus
    label = u"CSG déductible sur les allocations chômage"
    url = u"http://vosdroits.service-public.fr/particuliers/F2329.xhtml"

    def function(self, chobrut, csg_rempl, P = law):
        isexo = exo_csg_chom(chobrut, csg_rempl, P)
        csgchod = csgchod_sans_exo(chobrut, csg_rempl, P) * not_(isexo)
        return csgchod

    def get_output_period(self, period):
        return period.start.offset('first-of', 'month').period('month')

@reference_formula
class csgchoi(SimpleFormulaColumn):
    column = FloatCol
    entity_class = Individus
    label = u"CSG imposable sur les allocations chômage"
    url = u"http://vosdroits.service-public.fr/particuliers/F2329.xhtml"

    def function(self, chobrut, csg_rempl, P = law):
        isexo = exo_csg_chom(chobrut, csg_rempl, P)
        csgchoi = csgchoi_sans_exo(chobrut, csg_rempl, P) * not_(isexo)
        return csgchoi

    def get_output_period(self, period):
        return period.start.offset('first-of', 'month').period('month')

@reference_formula
class crdscho(SimpleFormulaColumn):
    column = FloatCol
    entity_class = Individus
    label = u"CRDS sur les allocations chômage"
    url = u"http://www.insee.fr/fr/methodes/default.asp?page=definitions/contrib-remb-dette-sociale.htm"

    def function(self, chobrut, csg_rempl, P = law):
        isexo = exo_csg_chom(chobrut, csg_rempl, P)
        crdscho = crdscho_sans_exo(chobrut, csg_rempl, P) * not_(isexo)
        return crdscho

    def get_output_period(self, period):
        return period.start.offset('first-of', 'month').period('month')

@reference_formula
class cho(SimpleFormulaColumn):
    column = FloatCol
    entity_class = Individus
    label = u"Allocations chômage imposables"
    url = u"http://www.insee.fr/fr/methodes/default.asp?page=definitions/chomage.htm"

    def function(self, chobrut, csgchod):
        return chobrut + csgchod

    def get_output_period(self, period):
        return period

@reference_formula
class chonet(SimpleFormulaColumn):
    column = FloatCol
    entity_class = Individus
    label = u"Allocations chômage nettes"
    url = u"http://vosdroits.service-public.fr/particuliers/N549.xhtml"

    def function(self, cho, csgchoi, crdscho):
        return cho + csgchoi + crdscho

    def get_output_period(self, period):
        return period

############################################################################
# # Pensions
############################################################################

@reference_formula
class csgrstd(SimpleFormulaColumn):
    column = FloatCol
    entity_class = Individus
    label = u"CSG déductible sur les pensions de retraite"
    url = u"https://www.lassuranceretraite.fr/cs/Satellite/PUBPrincipale/Retraites/Paiement-Votre-Retraite/Prelevements-Sociaux?packedargs=null"

    def function(self, rstbrut, csg_rempl, P = law):
        plaf_ss = P.cotsoc.gen.plaf_ss
        csg = scale_tax_scales(TaxScalesTree('csg', P.csg.retraite), plaf_ss)
        taux_plein = csg['plein']['deduc'].calc(rstbrut)
        taux_reduit = csg['reduit']['deduc'].calc(rstbrut)
        csgrstd = (csg_rempl == 3) * taux_plein + (csg_rempl == 2) * taux_reduit
        return -csgrstd

    def get_output_period(self, period):
        return period.start.offset('first-of', 'month').period('month')

@reference_formula
class csgrsti(SimpleFormulaColumn):
    column = FloatCol
    entity_class = Individus
    label = u"CSG imposable sur les pensions de retraite"
    url = u"https://www.lassuranceretraite.fr/cs/Satellite/PUBPrincipale/Retraites/Paiement-Votre-Retraite/Prelevements-Sociaux?packedargs=null"

    def function(self, rstbrut, csg_rempl, P = law):
        plaf_ss = P.cotsoc.gen.plaf_ss
        csg = scale_tax_scales(TaxScalesTree('csg', P.csg.retraite), plaf_ss)
        taux_plein = csg['plein']['impos'].calc(rstbrut)
        taux_reduit = csg['reduit']['impos'].calc(rstbrut)
        csgrsti = (csg_rempl == 3) * taux_plein + (csg_rempl == 2) * taux_reduit
        return -csgrsti

    def get_output_period(self, period):
        return period.start.offset('first-of', 'month').period('month')

@reference_formula
class crdsrst(SimpleFormulaColumn):
    column = FloatCol
    entity_class = Individus
    label = u"CRDS sur les pensions de retraite"
    url = u"http://www.pensions.bercy.gouv.fr/vous-%C3%AAtes-retrait%C3%A9-ou-pensionn%C3%A9/le-calcul-de-ma-pension/les-pr%C3%A9l%C3%A8vements-effectu%C3%A9s-sur-ma-pension"

    def function(self, rstbrut, csg_rempl, P = law):
        plaf_ss = P.cotsoc.gen.plaf_ss
        crds = scale_tax_scales(TaxScalesTree('crds', P.crds.rst), plaf_ss)
        isexo = (csg_rempl == 1)
        return -crds['rst'].calc(rstbrut) * not_(isexo)

    def get_output_period(self, period):
        return period.start.offset('first-of', 'month').period('month')

@reference_formula
class casa(DatedFormulaColumn):
    column = FloatCol
    entity_class = Individus
    label = u"Contribution additionnelle de solidarité et d'autonomie"
    url = u"http://www.service-public.fr/actualites/002691.html"

    @dated_function(datetime.date(2013, 4, 1))
    def function_2013(self, rstbrut, irpp_holder, csg_rempl, P = law):
        # TODO: replace irpp by irpp_n_2
        # TODO: utiliser la bonne période pour irpp_holder

        irpp = self.cast_from_entity_to_roles(irpp_holder)
        casa = (csg_rempl == 3) * P.prelsoc.add_ret * rstbrut * (irpp > P.ir.recouvrement.seuil)

        return -casa

    def get_output_period(self, period):
        return period.start.offset('first-of', 'month').period('month')

@reference_formula
class rst(SimpleFormulaColumn):
    column = FloatCol
    entity_class = Individus
    label = u"Pensions de retraite imposables"
    url = u"http://vosdroits.service-public.fr/particuliers/F415.xhtml"

    def function(self, rstbrut, csgrstd):
        return rstbrut + csgrstd

    def get_output_period(self, period):
        return period

@reference_formula
class rstnet(SimpleFormulaColumn):
    column = FloatCol
    entity_class = Individus
    label = u"Pensions de retraite nettes"
    url = u"http://vosdroits.service-public.fr/particuliers/N20166.xhtml"

    # def function(self, rst, csgrsti, crdsrst, casa):
        # return rst + csgrsti + crdsrst + casa
    def function(self, rst, csgrsti, crdsrst):
        return rst + csgrsti + crdsrst

    def get_output_period(self, period):
        return period
<|endoftext|>