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

#  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
#
#       https://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 unicode_literals, absolute_import

import json
import unittest

import responses

from linebot import (
    LineBotApi
)
from linebot.models import (
    URITemplateAction,
    RichMenu, RichMenuSize, RichMenuArea, RichMenuBounds
)

class TestLineBotApi(unittest.TestCase):
    maxDiff = None

    def setUp(self):
        self.tested = LineBotApi('channel_secret')

        self.rich_menu_id = 'richmenu-0000000000'
        self.user_id = 'userid'
        self.rich_menu = RichMenu(
            size=RichMenuSize(
                width=2500,
                height=1686
            ),
            selected=False,
            name="nice richmenu",
            chatBarText="touch me",
            areas=[
                RichMenuArea(
                    RichMenuBounds(
                        x=0,
                        y=0,
                        width=833,
                        height=843
                    ),
                    URITemplateAction(
                        uri='line://nv/location'
                    )
                )
            ]
        )

    @responses.activate
    def test_get_rich_menu(self):
        responses.add(
            responses.GET,
            LineBotApi.DEFAULT_API_ENDPOINT + '/v2/bot/richmenu/rich_menu_id',
            json={
                "richMenuId": "rich_menu_id",
                "size": {
                    "width": 2500,
                    "height": 1686
                },
                "selected": False,
                "name": "name",
                "chatBarText": "chatBarText",
                "areas": [
                    {
                        "bounds": {
                            "x": 0,
                            "y": 0,
                            "width": 2500,
                            "height": 1686
                        },
                        "action": {
                            "type": "postback",
                            "data": "action=buy&itemid=123"
                        }
                    }
                ]
            },
            status=200
        )

        rich_menu = self.tested.get_rich_menu('rich_menu_id')
        print(rich_menu)

        request = responses.calls[0].request
        self.assertEqual(request.method, 'GET')
        self.assertEqual(
            request.url,
            LineBotApi.DEFAULT_API_ENDPOINT + '/v2/bot/richmenu/rich_menu_id'
        )

        self.assertEqual(rich_menu.rich_menu_id, 'rich_menu_id')
        self.assertEqual(rich_menu.size.width, 2500)
        self.assertEqual(rich_menu.size.height, 1686)
        self.assertEqual(rich_menu.selected, False)
        self.assertEqual(rich_menu.name, 'name')
        self.assertEqual(rich_menu.chat_bar_text, 'chatBarText')
        self.assertEqual(rich_menu.areas[0].bounds.x, 0)
        self.assertEqual(rich_menu.areas[0].bounds.y, 0)
        self.assertEqual(rich_menu.areas[0].bounds.width, 2500)
        self.assertEqual(rich_menu.areas[0].bounds.height, 1686)
        self.assertEqual(rich_menu.areas[0].action.type, 'postback')
        self.assertEqual(rich_menu.areas[0].action.data, 'action=buy&itemid=123')

    @responses.activate
    def test_create_rich_menu(self):
        responses.add(
            responses.POST,
            LineBotApi.DEFAULT_API_ENDPOINT + '/v2/bot/richmenu',
            json={"richMenuId": "richMenuId"}, status=200
        )

        rich_menu = RichMenu(
            size=RichMenuSize(
                width=2500,
                height=1686
            ),
            selected=False,
            name="nice richmenu",
            chatBarText="touch me",
            areas=[
                RichMenuArea(
                    RichMenuBounds(
                        x=0,
                        y=0,
                        width=833,
                        height=843
                    ),
                    URITemplateAction(
                        uri='line://nv/location'
                    )
                )
            ]
        )

        result = self.tested.create_rich_menu(rich_menu)

        request = responses.calls[0].request
        self.assertEqual(request.method, 'POST')
        self.assertEqual(
            request.url,
            LineBotApi.DEFAULT_API_ENDPOINT + '/v2/bot/richmenu'
        )
        self.assertEqual(result, "richMenuId")

    @responses.activate
    def test_delete_rich_menu(self):
        responses.add(
            responses.DELETE,
            LineBotApi.DEFAULT_API_ENDPOINT + '/v2/bot/richmenu/rich_menu_id',
            json={}, status=200
        )

        self.tested.delete_rich_menu('rich_menu_id')

        request = responses.calls[0].request
        self.assertEqual(request.method, 'DELETE')
        self.assertEqual(
            request.url,
            LineBotApi.DEFAULT_API_ENDPOINT + '/v2/bot/richmenu/rich_menu_id'
        )

    @responses.activate
    def test_get_rich_menu_id_of_user(self):
        responses.add(
            responses.GET,
            LineBotApi.DEFAULT_API_ENDPOINT +
            '/v2/bot/user/user_id/richmenu',
            json={"richMenuId": "richMenuId"}, status=200
        )

        result = self.tested.get_rich_menu_id_of_user('user_id')

        request = responses.calls[0].request
        self.assertEqual(request.method, 'GET')
        self.assertEqual(
            request.url,
            LineBotApi.DEFAULT_API_ENDPOINT + '/v2/bot/user/user_id/richmenu'
        )
        self.assertEqual(result, "richMenuId")

    @responses.activate
    def test_link_rich_menu_to_user(self):
        responses.add(
            responses.POST,
            LineBotApi.DEFAULT_API_ENDPOINT +
            '/v2/bot/user/user_id/richmenu/rich_menu_id',
            json={}, status=200
        )

        self.tested.link_rich_menu_to_user('user_id', 'rich_menu_id')

        request = responses.calls[0].request
        self.assertEqual(request.method, 'POST')
        self.assertEqual(
            request.url,
            LineBotApi.DEFAULT_API_ENDPOINT + '/v2/bot/user/user_id/richmenu/rich_menu_id'
        )

    @responses.activate
    def test_unlink_rich_menu_from_user(self):
        responses.add(
            responses.DELETE,
            LineBotApi.DEFAULT_API_ENDPOINT +
            '/v2/bot/user/user_id/richmenu',
            json={}, status=200
        )

        self.tested.unlink_rich_menu_from_user('user_id')

        request = responses.calls[0].request
        self.assertEqual(request.method, 'DELETE')
        self.assertEqual(
            request.url,
            LineBotApi.DEFAULT_API_ENDPOINT + '/v2/bot/user/user_id/richmenu'
        )

    @responses.activate
    def test_get_rich_menu_list(self):
        responses.add(
            responses.GET,
            LineBotApi.DEFAULT_API_ENDPOINT + '/v2/bot/richmenu/list',
            json={
                "richmenus": [
                    {
                        "richMenuId": "rich_menu_id",
                        "size": {
                            "width": 2500,
                            "height": 1686
                        },
                        "selected": False,
                        "name": "name",
                        "chatBarText": "chatBarText",
                        "areas": [
                            {
                                "bounds": {
                                    "x": 0,
                                    "y": 0,
                                    "width": 2500,
                                    "height": 1686
                                },
                                "action": {
                                    "type": "postback",
                                    "data": "action=buy&itemid=123"
                                }
                            }
                        ]
                    }
                ]
            },
            status=200
        )

        rich_menus = self.tested.get_rich_menu_list()

        request = responses.calls[0].request
        self.assertEqual(request.method, 'GET')
        self.assertEqual(
            request.url,
            LineBotApi.DEFAULT_API_ENDPOINT + '/v2/bot/richmenu/list'
        )
        self.assertEqual(rich_menus[0].rich_menu_id, 'rich_menu_id')
        self.assertEqual(rich_menus[0].size.width, 2500)
        self.assertEqual(rich_menus[0].size.height, 1686)
        self.assertEqual(rich_menus[0].selected, False)
        self.assertEqual(rich_menus[0].name, 'name')
        self.assertEqual(rich_menus[0].chat_bar_text, 'chatBarText')
        self.assertEqual(rich_menus[0].areas[0].bounds.x, 0)
        self.assertEqual(rich_menus[0].areas[0].bounds.y, 0)
        self.assertEqual(rich_menus[0].areas[0].bounds.width, 2500)
        self.assertEqual(rich_menus[0].areas[0].bounds.height, 1686)
        self.assertEqual(rich_menus[0].areas[0].action.type, 'postback')
        self.assertEqual(rich_menus[0].areas[0].action.data, 'action=buy&itemid=123')

    @responses.activate
    def test_link_rich_menu_to_users(self):
        responses.add(
            responses.POST,
            LineBotApi.DEFAULT_API_ENDPOINT +
            '/v2/bot/richmenu/bulk/link',
            json={}, status=202
        )

        self.tested.link_rich_menu_to_users(['user_id1', 'user_id2'], 'rich_menu_id')

        request = responses.calls[0].request
        self.assertEqual(request.method, 'POST')
        self.assertEqual(
            request.url,
            LineBotApi.DEFAULT_API_ENDPOINT + '/v2/bot/richmenu/bulk/link'
        )
        self.assertEqual(
            json.loads(request.body),
            {
                "richMenuId": "rich_menu_id",
                "userIds": ["user_id1", "user_id2"],
            }
        )

    @responses.activate
    def test_unlink_rich_menu_to_users(self):
        responses.add(
            responses.POST,
            LineBotApi.DEFAULT_API_ENDPOINT +
            '/v2/bot/richmenu/bulk/unlink',
            json={}, status=202
        )

        self.tested.unlink_rich_menu_from_users(['user_id1', 'user_id2'], 'rich_menu_id')

        request = responses.calls[0].request
        self.assertEqual(request.method, 'POST')
        self.assertEqual(
            request.url,
            LineBotApi.DEFAULT_API_ENDPOINT + '/v2/bot/richmenu/bulk/unlink'
        )
        self.assertEqual(
            json.loads(request.body),
            {
                "userIds": ["user_id1", "user_id2"],
            }
        )

    @responses.activate
    def test_set_default_rich_menu(self):
        responses.add(
            responses.POST,
            LineBotApi.DEFAULT_API_ENDPOINT +
            '/v2/bot/user/all/richmenu/rich_menu_id',
            json={}, status=200
        )

        self.tested.set_default_rich_menu('rich_menu_id')

        request = responses.calls[0].request
        self.assertEqual(request.method, 'POST')
        self.assertEqual(
            request.url,
            LineBotApi.DEFAULT_API_ENDPOINT + '/v2/bot/user/all/richmenu/rich_menu_id'
        )

    @responses.activate
    def test_get_default_rich_menu(self):
        responses.add(
            responses.GET,
            LineBotApi.DEFAULT_API_ENDPOINT +
            '/v2/bot/user/all/richmenu',
            json={"richMenuId": "richMenuId"}, status=200
        )

        result = self.tested.get_default_rich_menu()

        request = responses.calls[0].request
        self.assertEqual(request.method, 'GET')
        self.assertEqual(
            request.url,
            LineBotApi.DEFAULT_API_ENDPOINT + '/v2/bot/user/all/richmenu'
        )
        self.assertEqual(result, "richMenuId")

    @responses.activate
    def test_cancel_default_rich_menu(self):
        responses.add(
            responses.DELETE,
            LineBotApi.DEFAULT_API_ENDPOINT +
            '/v2/bot/user/all/richmenu',
            json={}, status=200
        )

        self.tested.cancel_default_rich_menu()

        request = responses.calls[0].request
        self.assertEqual(request.method, 'DELETE')
        self.assertEqual(
            request.url,
            LineBotApi.DEFAULT_API_ENDPOINT + '/v2/bot/user/all/richmenu'
        )

    @responses.activate
    def test_get_rich_menu_image(self):
        rich_menu_id = '1234'
        body = b'hogieoidksk'
        responses.add(
            responses.GET,
            LineBotApi.DEFAULT_API_DATA_ENDPOINT +
            '/v2/bot/richmenu/{rich_menu_id}/content'.format(rich_menu_id=rich_menu_id),
            body=body, status=200
        )

        res = self.tested.get_rich_menu_image(rich_menu_id)

        request = responses.calls[0].request
        self.assertEqual(request.method, 'GET')
        self.assertEqual(
            request.url,
            LineBotApi.DEFAULT_API_DATA_ENDPOINT +
            '/v2/bot/richmenu/{rich_menu_id}/content'.format(rich_menu_id=rich_menu_id),
        )
        self.assertEqual(
            body,
            res.content
        )

    @responses.activate
    def test_set_rich_menu_image(self):
        rich_menu_id = '1234'
        body = b'hogieoidksk'
        responses.add(
            responses.POST,
            LineBotApi.DEFAULT_API_DATA_ENDPOINT +
            '/v2/bot/richmenu/{rich_menu_id}/content'.format(rich_menu_id=rich_menu_id),
            json={}, status=200
        )

        self.tested.set_rich_menu_image(
            rich_menu_id=rich_menu_id,
            content_type='image/jpeg',
            content=body
        )

        request = responses.calls[0].request
        self.assertEqual('POST', request.method)
        self.assertEqual(
            LineBotApi.DEFAULT_API_DATA_ENDPOINT +
            '/v2/bot/richmenu/{rich_menu_id}/content'.format(rich_menu_id=rich_menu_id),
            request.url
        )

if __name__ == '__main__':
    unittest.main()
<commit_msg>fix deprecated url scheme in test code (#251)<commit_after># -*- coding: utf-8 -*-

#  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
#
#       https://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 unicode_literals, absolute_import

import json
import unittest

import responses

from linebot import (
    LineBotApi
)
from linebot.models import (
    URITemplateAction,
    RichMenu, RichMenuSize, RichMenuArea, RichMenuBounds
)

class TestLineBotApi(unittest.TestCase):
    maxDiff = None

    def setUp(self):
        self.tested = LineBotApi('channel_secret')

        self.rich_menu_id = 'richmenu-0000000000'
        self.user_id = 'userid'
        self.rich_menu = RichMenu(
            size=RichMenuSize(
                width=2500,
                height=1686
            ),
            selected=False,
            name="nice richmenu",
            chatBarText="touch me",
            areas=[
                RichMenuArea(
                    RichMenuBounds(
                        x=0,
                        y=0,
                        width=833,
                        height=843
                    ),
                    URITemplateAction(
                        uri='https://line.me/R/nv/location/'
                    )
                )
            ]
        )

    @responses.activate
    def test_get_rich_menu(self):
        responses.add(
            responses.GET,
            LineBotApi.DEFAULT_API_ENDPOINT + '/v2/bot/richmenu/rich_menu_id',
            json={
                "richMenuId": "rich_menu_id",
                "size": {
                    "width": 2500,
                    "height": 1686
                },
                "selected": False,
                "name": "name",
                "chatBarText": "chatBarText",
                "areas": [
                    {
                        "bounds": {
                            "x": 0,
                            "y": 0,
                            "width": 2500,
                            "height": 1686
                        },
                        "action": {
                            "type": "postback",
                            "data": "action=buy&itemid=123"
                        }
                    }
                ]
            },
            status=200
        )

        rich_menu = self.tested.get_rich_menu('rich_menu_id')
        print(rich_menu)

        request = responses.calls[0].request
        self.assertEqual(request.method, 'GET')
        self.assertEqual(
            request.url,
            LineBotApi.DEFAULT_API_ENDPOINT + '/v2/bot/richmenu/rich_menu_id'
        )

        self.assertEqual(rich_menu.rich_menu_id, 'rich_menu_id')
        self.assertEqual(rich_menu.size.width, 2500)
        self.assertEqual(rich_menu.size.height, 1686)
        self.assertEqual(rich_menu.selected, False)
        self.assertEqual(rich_menu.name, 'name')
        self.assertEqual(rich_menu.chat_bar_text, 'chatBarText')
        self.assertEqual(rich_menu.areas[0].bounds.x, 0)
        self.assertEqual(rich_menu.areas[0].bounds.y, 0)
        self.assertEqual(rich_menu.areas[0].bounds.width, 2500)
        self.assertEqual(rich_menu.areas[0].bounds.height, 1686)
        self.assertEqual(rich_menu.areas[0].action.type, 'postback')
        self.assertEqual(rich_menu.areas[0].action.data, 'action=buy&itemid=123')

    @responses.activate
    def test_create_rich_menu(self):
        responses.add(
            responses.POST,
            LineBotApi.DEFAULT_API_ENDPOINT + '/v2/bot/richmenu',
            json={"richMenuId": "richMenuId"}, status=200
        )

        rich_menu = RichMenu(
            size=RichMenuSize(
                width=2500,
                height=1686
            ),
            selected=False,
            name="nice richmenu",
            chatBarText="touch me",
            areas=[
                RichMenuArea(
                    RichMenuBounds(
                        x=0,
                        y=0,
                        width=833,
                        height=843
                    ),
                    URITemplateAction(
                        uri='https://line.me/R/nv/location/'
                    )
                )
            ]
        )

        result = self.tested.create_rich_menu(rich_menu)

        request = responses.calls[0].request
        self.assertEqual(request.method, 'POST')
        self.assertEqual(
            request.url,
            LineBotApi.DEFAULT_API_ENDPOINT + '/v2/bot/richmenu'
        )
        self.assertEqual(result, "richMenuId")

    @responses.activate
    def test_delete_rich_menu(self):
        responses.add(
            responses.DELETE,
            LineBotApi.DEFAULT_API_ENDPOINT + '/v2/bot/richmenu/rich_menu_id',
            json={}, status=200
        )

        self.tested.delete_rich_menu('rich_menu_id')

        request = responses.calls[0].request
        self.assertEqual(request.method, 'DELETE')
        self.assertEqual(
            request.url,
            LineBotApi.DEFAULT_API_ENDPOINT + '/v2/bot/richmenu/rich_menu_id'
        )

    @responses.activate
    def test_get_rich_menu_id_of_user(self):
        responses.add(
            responses.GET,
            LineBotApi.DEFAULT_API_ENDPOINT +
            '/v2/bot/user/user_id/richmenu',
            json={"richMenuId": "richMenuId"}, status=200
        )

        result = self.tested.get_rich_menu_id_of_user('user_id')

        request = responses.calls[0].request
        self.assertEqual(request.method, 'GET')
        self.assertEqual(
            request.url,
            LineBotApi.DEFAULT_API_ENDPOINT + '/v2/bot/user/user_id/richmenu'
        )
        self.assertEqual(result, "richMenuId")

    @responses.activate
    def test_link_rich_menu_to_user(self):
        responses.add(
            responses.POST,
            LineBotApi.DEFAULT_API_ENDPOINT +
            '/v2/bot/user/user_id/richmenu/rich_menu_id',
            json={}, status=200
        )

        self.tested.link_rich_menu_to_user('user_id', 'rich_menu_id')

        request = responses.calls[0].request
        self.assertEqual(request.method, 'POST')
        self.assertEqual(
            request.url,
            LineBotApi.DEFAULT_API_ENDPOINT + '/v2/bot/user/user_id/richmenu/rich_menu_id'
        )

    @responses.activate
    def test_unlink_rich_menu_from_user(self):
        responses.add(
            responses.DELETE,
            LineBotApi.DEFAULT_API_ENDPOINT +
            '/v2/bot/user/user_id/richmenu',
            json={}, status=200
        )

        self.tested.unlink_rich_menu_from_user('user_id')

        request = responses.calls[0].request
        self.assertEqual(request.method, 'DELETE')
        self.assertEqual(
            request.url,
            LineBotApi.DEFAULT_API_ENDPOINT + '/v2/bot/user/user_id/richmenu'
        )

    @responses.activate
    def test_get_rich_menu_list(self):
        responses.add(
            responses.GET,
            LineBotApi.DEFAULT_API_ENDPOINT + '/v2/bot/richmenu/list',
            json={
                "richmenus": [
                    {
                        "richMenuId": "rich_menu_id",
                        "size": {
                            "width": 2500,
                            "height": 1686
                        },
                        "selected": False,
                        "name": "name",
                        "chatBarText": "chatBarText",
                        "areas": [
                            {
                                "bounds": {
                                    "x": 0,
                                    "y": 0,
                                    "width": 2500,
                                    "height": 1686
                                },
                                "action": {
                                    "type": "postback",
                                    "data": "action=buy&itemid=123"
                                }
                            }
                        ]
                    }
                ]
            },
            status=200
        )

        rich_menus = self.tested.get_rich_menu_list()

        request = responses.calls[0].request
        self.assertEqual(request.method, 'GET')
        self.assertEqual(
            request.url,
            LineBotApi.DEFAULT_API_ENDPOINT + '/v2/bot/richmenu/list'
        )
        self.assertEqual(rich_menus[0].rich_menu_id, 'rich_menu_id')
        self.assertEqual(rich_menus[0].size.width, 2500)
        self.assertEqual(rich_menus[0].size.height, 1686)
        self.assertEqual(rich_menus[0].selected, False)
        self.assertEqual(rich_menus[0].name, 'name')
        self.assertEqual(rich_menus[0].chat_bar_text, 'chatBarText')
        self.assertEqual(rich_menus[0].areas[0].bounds.x, 0)
        self.assertEqual(rich_menus[0].areas[0].bounds.y, 0)
        self.assertEqual(rich_menus[0].areas[0].bounds.width, 2500)
        self.assertEqual(rich_menus[0].areas[0].bounds.height, 1686)
        self.assertEqual(rich_menus[0].areas[0].action.type, 'postback')
        self.assertEqual(rich_menus[0].areas[0].action.data, 'action=buy&itemid=123')

    @responses.activate
    def test_link_rich_menu_to_users(self):
        responses.add(
            responses.POST,
            LineBotApi.DEFAULT_API_ENDPOINT +
            '/v2/bot/richmenu/bulk/link',
            json={}, status=202
        )

        self.tested.link_rich_menu_to_users(['user_id1', 'user_id2'], 'rich_menu_id')

        request = responses.calls[0].request
        self.assertEqual(request.method, 'POST')
        self.assertEqual(
            request.url,
            LineBotApi.DEFAULT_API_ENDPOINT + '/v2/bot/richmenu/bulk/link'
        )
        self.assertEqual(
            json.loads(request.body),
            {
                "richMenuId": "rich_menu_id",
                "userIds": ["user_id1", "user_id2"],
            }
        )

    @responses.activate
    def test_unlink_rich_menu_to_users(self):
        responses.add(
            responses.POST,
            LineBotApi.DEFAULT_API_ENDPOINT +
            '/v2/bot/richmenu/bulk/unlink',
            json={}, status=202
        )

        self.tested.unlink_rich_menu_from_users(['user_id1', 'user_id2'], 'rich_menu_id')

        request = responses.calls[0].request
        self.assertEqual(request.method, 'POST')
        self.assertEqual(
            request.url,
            LineBotApi.DEFAULT_API_ENDPOINT + '/v2/bot/richmenu/bulk/unlink'
        )
        self.assertEqual(
            json.loads(request.body),
            {
                "userIds": ["user_id1", "user_id2"],
            }
        )

    @responses.activate
    def test_set_default_rich_menu(self):
        responses.add(
            responses.POST,
            LineBotApi.DEFAULT_API_ENDPOINT +
            '/v2/bot/user/all/richmenu/rich_menu_id',
            json={}, status=200
        )

        self.tested.set_default_rich_menu('rich_menu_id')

        request = responses.calls[0].request
        self.assertEqual(request.method, 'POST')
        self.assertEqual(
            request.url,
            LineBotApi.DEFAULT_API_ENDPOINT + '/v2/bot/user/all/richmenu/rich_menu_id'
        )

    @responses.activate
    def test_get_default_rich_menu(self):
        responses.add(
            responses.GET,
            LineBotApi.DEFAULT_API_ENDPOINT +
            '/v2/bot/user/all/richmenu',
            json={"richMenuId": "richMenuId"}, status=200
        )

        result = self.tested.get_default_rich_menu()

        request = responses.calls[0].request
        self.assertEqual(request.method, 'GET')
        self.assertEqual(
            request.url,
            LineBotApi.DEFAULT_API_ENDPOINT + '/v2/bot/user/all/richmenu'
        )
        self.assertEqual(result, "richMenuId")

    @responses.activate
    def test_cancel_default_rich_menu(self):
        responses.add(
            responses.DELETE,
            LineBotApi.DEFAULT_API_ENDPOINT +
            '/v2/bot/user/all/richmenu',
            json={}, status=200
        )

        self.tested.cancel_default_rich_menu()

        request = responses.calls[0].request
        self.assertEqual(request.method, 'DELETE')
        self.assertEqual(
            request.url,
            LineBotApi.DEFAULT_API_ENDPOINT + '/v2/bot/user/all/richmenu'
        )

    @responses.activate
    def test_get_rich_menu_image(self):
        rich_menu_id = '1234'
        body = b'hogieoidksk'
        responses.add(
            responses.GET,
            LineBotApi.DEFAULT_API_DATA_ENDPOINT +
            '/v2/bot/richmenu/{rich_menu_id}/content'.format(rich_menu_id=rich_menu_id),
            body=body, status=200
        )

        res = self.tested.get_rich_menu_image(rich_menu_id)

        request = responses.calls[0].request
        self.assertEqual(request.method, 'GET')
        self.assertEqual(
            request.url,
            LineBotApi.DEFAULT_API_DATA_ENDPOINT +
            '/v2/bot/richmenu/{rich_menu_id}/content'.format(rich_menu_id=rich_menu_id),
        )
        self.assertEqual(
            body,
            res.content
        )

    @responses.activate
    def test_set_rich_menu_image(self):
        rich_menu_id = '1234'
        body = b'hogieoidksk'
        responses.add(
            responses.POST,
            LineBotApi.DEFAULT_API_DATA_ENDPOINT +
            '/v2/bot/richmenu/{rich_menu_id}/content'.format(rich_menu_id=rich_menu_id),
            json={}, status=200
        )

        self.tested.set_rich_menu_image(
            rich_menu_id=rich_menu_id,
            content_type='image/jpeg',
            content=body
        )

        request = responses.calls[0].request
        self.assertEqual('POST', request.method)
        self.assertEqual(
            LineBotApi.DEFAULT_API_DATA_ENDPOINT +
            '/v2/bot/richmenu/{rich_menu_id}/content'.format(rich_menu_id=rich_menu_id),
            request.url
        )

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


<commit_before>'''
Cookie Jar
==========
An implementation of `CookieJar` using CouchDB as its database.

Exportable Classes: `BiscuitTin`, `RateLimitedBiscuitTin`

BiscuitTin
----------
`BiscuitTin` implements the interface decreed by `CookieJar`. It must be
instantiated with the buffered CouchDB interface parameters, at which
point it will connect to (and create, if necessary) said database on the
host and set up (again, if necessary) the required design documents to
manage the processing queue and metadata repository.

`BiscuitTin` implements `Listenable`; when a cookie is queued (i.e., on
metadata enrichment or exceptional marking), it will broadcast the
queue change to all downstream listeners.

`RateLimitedBiscuitTin` is a rate-limited version of `BiscuitTin` which
takes an additional argument, at initial position, in its constructor:
`max_requests_per_second`.

Authors
-------
* Christopher Harrison <ch12@sanger.ac.uk>

License
-------
GPLv3 or later
Copyright (c) 2015, 2016 Genome Research Limited
'''
from datetime import timedelta
from typing import Optional
from threading import Lock, Timer

from cookiemonster.common.models import Enrichment, Cookie
from cookiemonster.cookiejar.cookiejar import CookieJar
from cookiemonster.cookiejar._dbi import Sofabed, Bert, Ernie
from cookiemonster.cookiejar._rate_limiter import rate_limited

class BiscuitTin(CookieJar):
    ''' Persistent implementation of `CookieJar` '''
    def __init__(self, couchdb_url:str, couchdb_name:str, buffer_capacity:int = 100,
                                                          buffer_latency:timedelta = timedelta(milliseconds=50)):
        '''
        Constructor: Initialise the database interfaces

        @param  couchdb_url      CouchDB URL
        @param  couchdb_name     Database name
        @param  buffer_capacity  Buffer capacity
        @param  buffer_latency   Buffer latency
        '''
        super().__init__()
        self._sofa = Sofabed(couchdb_url, couchdb_name, buffer_capacity, buffer_latency)
        self._queue = Bert(self._sofa)
        self._metadata = Ernie(self._sofa)

        self._queue_lock = Lock()

    def _broadcast(self):
        '''
        Broadcast to all listeners
        This should be called on queue changes
        '''
        self.notify_listeners()

    def enrich_cookie(self, identifier: str, enrichment: Enrichment):
        self._metadata.enrich(identifier, enrichment)
        self._queue.mark_dirty(identifier)
        self._broadcast()

    def mark_as_failed(self, identifier: str, requeue_delay: timedelta=timedelta(0)):
        self._queue.mark_finished(identifier)
        self._queue.mark_dirty(identifier, requeue_delay)

        # Broadcast the change after the requeue delay
        # FIXME? Timer's interval may not be 100% accurate and may also
        # not correspond with the database server; this could go out of
        # synch... Add a tolerance??
        Timer(requeue_delay.total_seconds(), self._broadcast).start()

    def mark_as_complete(self, identifier: str):
        self._queue.mark_finished(identifier)

    def mark_for_processing(self, identifier: str):
        self._queue.mark_dirty(identifier)
        self._broadcast()

    def get_next_for_processing(self) -> Optional[Cookie]:
        with self._queue_lock:
            to_process = self._queue.dequeue()

        if to_process is None:
            return None

        cookie = Cookie(to_process)
        cookie.enrichments = self._metadata.get_metadata(to_process)

        return cookie

    def queue_length(self) -> int:
        return self._queue.queue_length()

@rate_limited
class RateLimitedBiscuitTin(BiscuitTin):
    pass
<commit_msg>Implemented `BiscuitTin.fetch_cookie`<commit_after>'''
Cookie Jar
==========
An implementation of `CookieJar` using CouchDB as its database.

Exportable Classes: `BiscuitTin`, `RateLimitedBiscuitTin`

BiscuitTin
----------
`BiscuitTin` implements the interface decreed by `CookieJar`. It must be
instantiated with the buffered CouchDB interface parameters, at which
point it will connect to (and create, if necessary) said database on the
host and set up (again, if necessary) the required design documents to
manage the processing queue and metadata repository.

`BiscuitTin` implements `Listenable`; when a cookie is queued (i.e., on
metadata enrichment or exceptional marking), it will broadcast the
queue change to all downstream listeners.

`RateLimitedBiscuitTin` is a rate-limited version of `BiscuitTin` which
takes an additional argument, at initial position, in its constructor:
`max_requests_per_second`.

Authors
-------
* Christopher Harrison <ch12@sanger.ac.uk>

License
-------
GPLv3 or later
Copyright (c) 2015, 2016 Genome Research Limited
'''
from datetime import timedelta
from typing import Optional
from threading import Lock, Timer

from cookiemonster.common.models import Enrichment, Cookie
from cookiemonster.cookiejar.cookiejar import CookieJar
from cookiemonster.cookiejar._dbi import Sofabed, Bert, Ernie
from cookiemonster.cookiejar._rate_limiter import rate_limited

class BiscuitTin(CookieJar):
    ''' Persistent implementation of `CookieJar` '''
    def __init__(self, couchdb_url:str, couchdb_name:str, buffer_capacity:int = 100,
                                                          buffer_latency:timedelta = timedelta(milliseconds=50)):
        '''
        Constructor: Initialise the database interfaces

        @param  couchdb_url      CouchDB URL
        @param  couchdb_name     Database name
        @param  buffer_capacity  Buffer capacity
        @param  buffer_latency   Buffer latency
        '''
        super().__init__()
        self._sofa = Sofabed(couchdb_url, couchdb_name, buffer_capacity, buffer_latency)
        self._queue = Bert(self._sofa)
        self._metadata = Ernie(self._sofa)

        self._queue_lock = Lock()

    def _broadcast(self):
        '''
        Broadcast to all listeners
        This should be called on queue changes
        '''
        self.notify_listeners()

    def fetch_cookie(self, identifier: str) -> Optional[Cookie]:
        _, doc = self._queue.get_by_identifier(identifier) or (None, None)

        if doc is None:
            return None

        cookie = Cookie(identifier)
        cookie.enrichments = self._metadata.get_metadata(identifier)

        return cookie

    def delete_cookie(self, identifier: str):
        pass

    def enrich_cookie(self, identifier: str, enrichment: Enrichment):
        self._metadata.enrich(identifier, enrichment)
        self._queue.mark_dirty(identifier)
        self._broadcast()

    def mark_as_failed(self, identifier: str, requeue_delay: timedelta=timedelta(0)):
        self._queue.mark_finished(identifier)
        self._queue.mark_dirty(identifier, requeue_delay)

        # Broadcast the change after the requeue delay
        # FIXME? Timer's interval may not be 100% accurate and may also
        # not correspond with the database server; this could go out of
        # synch... Add a tolerance??
        Timer(requeue_delay.total_seconds(), self._broadcast).start()

    def mark_as_complete(self, identifier: str):
        self._queue.mark_finished(identifier)

    def mark_for_processing(self, identifier: str):
        self._queue.mark_dirty(identifier)
        self._broadcast()

    def get_next_for_processing(self) -> Optional[Cookie]:
        with self._queue_lock:
            to_process = self._queue.dequeue()

        if to_process is None:
            return None

        return self.fetch_cookie(to_process)

    def queue_length(self) -> int:
        return self._queue.queue_length()

@rate_limited
class RateLimitedBiscuitTin(BiscuitTin):
    pass
<|endoftext|>


<commit_before><commit_msg>testando as 3 partes do CNPJ<commit_after><|endoftext|>


<commit_before>"""
A WebSocket server that listens to EMDR and sends notifications to any
connected users to light up systems.
"""
import zlib
import argparse
import ujson
import gevent
from gevent import pywsgi
from gevent import monkey; gevent.monkey.patch_all()
from gevent_zeromq import zmq
from geventwebsocket.handler import WebSocketHandler

##########
# Config #
##########

# The ZeroMQ socket to listen for market data from.
RECEIVER_BINDINGS = ['ipc:///tmp/announcer-sender.sock']
# The port to listen for websockets connections on.
WEBSOCKET_PORT = 9000

##################
# Command parser #
##################

parser = argparse.ArgumentParser(
    description="Runs a websocket server, relaying market data pings to " \
                "a JS-based websockets map.",
)

parser.add_argument(
    '--receiver', action='append', dest='receivers',
    help="Overrides the default receiver binding (%s)." % RECEIVER_BINDINGS[0])
parser.add_argument(
    '--wsport', action='store', dest='ws_port',
    help="Overrides the default websocket port (%s)." % WEBSOCKET_PORT)

parsed = parser.parse_args()

if parsed.receivers:
    RECEIVER_BINDINGS = parsed.receivers
if parsed.ws_port:
    WEBSOCKET_PORT = parsed.ws_port

###########
# Globals #
###########

# The receiver is a connection to a relay or announcer. We'll only form one
# of these, to save on socket count.
context = zmq.Context()
receiver = context.socket(zmq.SUB)
for binding in RECEIVER_BINDINGS:
    receiver.connect(binding)
# Disable filtering.
receiver.setsockopt(zmq.SUBSCRIBE, "")

# To save on socket count, we'll re-broadcast anything coming in via receiver
# to an inproc PUB/SUB setup. This will keep us down to one TCP connection
# that can service a large number of websocket connections.
sender = context.socket(zmq.PUB)
sender.bind('ipc:///tmp/emdr-map-rebroadcast.sock')

# Allows the IPC socket time to bind, I guess.
gevent.sleep(1)

#################
# Rebroadcaster #
#################

def rebroadcaster_worker(message):
    """
    This is ran as a greenlet that parses the incoming data, extracts the
    system numbers, and sends the system IDs over the inproc PUB socket.

    NOTE: This is not the most efficient, but will do for now.

    :param str message: A JSON market data string in Unified Uploader
        Interchange format.
    """
    market_json = zlib.decompress(message)
    market_data = ujson.loads(market_json)

    result_type = market_data['resultType']

    if result_type == 'history':
        # Pewp, we can't do anything with history messages.
        return

    # A list that will contain the IDs of systems to ping. Gets serialized
    # and sent once instead of for each ID.
    ids_to_send = []

    for rowset in market_data['rowsets']:
        for row in rowset['rows']:
            # Column 10 is systemID. I know, hard-coded colum index.
            # I've been a naughty boy.
            raw_system_id = row[10]
            if not raw_system_id:
                # Market data can sometimes lack a system ID, in edge cases.
                continue

            # Make absolutely sure this is an int.
            system_id = int(raw_system_id)
            ids_to_send.append(system_id)

    # Bombs away. Sends over the WebSocket connection.
    sender.send(ujson.dumps(ids_to_send))

def rebroadcaster_greenlet_loop():
    """
    This loop blocks the greenlet while waiting for something to come down
    the pipe. When found, fires off another greenlet to re-broadcast the message
    to any connected inproc SUB sockets.
    """
    while True:
        gevent.spawn(rebroadcaster_worker, receiver.recv())

# Fire up the rebroadcaster greenlet loop, which continuously monitors the
# incoming SUB socket for messages. When one is found, re-broadcast it over
# the inproc PUB socket, for any connected websocket sessions to pick up,
gevent.spawn(rebroadcaster_greenlet_loop)

####################################
# WebSocket app, and the fun stuff #
####################################

def websocket_worker(environ, start_response):
    """
    For each websocket connection, a websocket worker is spawned. Each worker
    connects to the in-process sender PUB socket, and waits for messages. Each
    message will be a single system ID that needs to be re-broadcasted to
    the websocket connection that is patiently waiting.

    We use an inproc SUB socket instead of separate TCP sockets to cut down
    on socket count. This also allows us to do the processing only once per
    connection.
    """
    context = zmq.Context()
    subscriber = context.socket(zmq.SUB)

    # Connect to our in-process re-broadcaster, which pumps out parsed/prepped
    # system IDs that are ready for sending.
    subscriber.connect('ipc:///tmp/emdr-map-rebroadcast.sock')
    # Disable filtering.
    subscriber.setsockopt(zmq.SUBSCRIBE, "")

    # This is a WebSocket object that we can send stuff over.
    try:
        ws = environ["wsgi.websocket"]
    except KeyError:
        # This is not a websocket connection. Redir to the map.
        start_response('301 Moved Permanently',
            [
                ('Content-Type', 'text/html'),
                ('Location', 'http://map.eve-emdr.com/'),
            ]
        )
        return ["Please see http://map.eve-emdr.com/"]

    while True:
        # This will block until messages arrive.
        system_id = subscriber.recv()
        # Send the message
        ws.send(system_id)

#################################
# Boring gevent webserver stuff #
#################################

print("=" * 80)
print("# EMDRPM WebSocket Server #".center(80))
print("-" * 80)
print("* Listening for messages from %s" % RECEIVER_BINDINGS)
print("* Listening for websockets connections on port %s" % WEBSOCKET_PORT)
print("=" * 80)

server = pywsgi.WSGIServer(
    ("", WEBSOCKET_PORT),
    websocket_worker,
    handler_class=WebSocketHandler
)

try:
    server.serve_forever()
except KeyboardInterrupt:
    print("Stopped.")<commit_msg>Only send if there is something to send.<commit_after>"""
A WebSocket server that listens to EMDR and sends notifications to any
connected users to light up systems.
"""
import zlib
import argparse
import ujson
import gevent
from gevent import pywsgi
from gevent import monkey; gevent.monkey.patch_all()
from gevent_zeromq import zmq
from geventwebsocket.handler import WebSocketHandler

##########
# Config #
##########

# The ZeroMQ socket to listen for market data from.
RECEIVER_BINDINGS = ['ipc:///tmp/announcer-sender.sock']
# The port to listen for websockets connections on.
WEBSOCKET_PORT = 9000

##################
# Command parser #
##################

parser = argparse.ArgumentParser(
    description="Runs a websocket server, relaying market data pings to " \
                "a JS-based websockets map.",
)

parser.add_argument(
    '--receiver', action='append', dest='receivers',
    help="Overrides the default receiver binding (%s)." % RECEIVER_BINDINGS[0])
parser.add_argument(
    '--wsport', action='store', dest='ws_port',
    help="Overrides the default websocket port (%s)." % WEBSOCKET_PORT)

parsed = parser.parse_args()

if parsed.receivers:
    RECEIVER_BINDINGS = parsed.receivers
if parsed.ws_port:
    WEBSOCKET_PORT = parsed.ws_port

###########
# Globals #
###########

# The receiver is a connection to a relay or announcer. We'll only form one
# of these, to save on socket count.
context = zmq.Context()
receiver = context.socket(zmq.SUB)
for binding in RECEIVER_BINDINGS:
    receiver.connect(binding)
# Disable filtering.
receiver.setsockopt(zmq.SUBSCRIBE, "")

# To save on socket count, we'll re-broadcast anything coming in via receiver
# to an inproc PUB/SUB setup. This will keep us down to one TCP connection
# that can service a large number of websocket connections.
sender = context.socket(zmq.PUB)
sender.bind('ipc:///tmp/emdr-map-rebroadcast.sock')

# Allows the IPC socket time to bind, I guess.
gevent.sleep(1)

#################
# Rebroadcaster #
#################

def rebroadcaster_worker(message):
    """
    This is ran as a greenlet that parses the incoming data, extracts the
    system numbers, and sends the system IDs over the inproc PUB socket.

    NOTE: This is not the most efficient, but will do for now.

    :param str message: A JSON market data string in Unified Uploader
        Interchange format.
    """
    market_json = zlib.decompress(message)
    market_data = ujson.loads(market_json)

    result_type = market_data['resultType']

    if result_type == 'history':
        # Pewp, we can't do anything with history messages.
        return

    # A list that will contain the IDs of systems to ping. Gets serialized
    # and sent once instead of for each ID.
    ids_to_send = []

    for rowset in market_data['rowsets']:
        for row in rowset['rows']:
            # Column 10 is systemID. I know, hard-coded colum index.
            # I've been a naughty boy.
            raw_system_id = row[10]
            if not raw_system_id:
                # Market data can sometimes lack a system ID, in edge cases.
                continue

            # Make absolutely sure this is an int.
            system_id = int(raw_system_id)
            ids_to_send.append(system_id)

    if ids_to_send:
        # Bombs away. Sends over the WebSocket connection.
        sender.send(ujson.dumps(ids_to_send))

def rebroadcaster_greenlet_loop():
    """
    This loop blocks the greenlet while waiting for something to come down
    the pipe. When found, fires off another greenlet to re-broadcast the message
    to any connected inproc SUB sockets.
    """
    while True:
        gevent.spawn(rebroadcaster_worker, receiver.recv())

# Fire up the rebroadcaster greenlet loop, which continuously monitors the
# incoming SUB socket for messages. When one is found, re-broadcast it over
# the inproc PUB socket, for any connected websocket sessions to pick up,
gevent.spawn(rebroadcaster_greenlet_loop)

####################################
# WebSocket app, and the fun stuff #
####################################

def websocket_worker(environ, start_response):
    """
    For each websocket connection, a websocket worker is spawned. Each worker
    connects to the in-process sender PUB socket, and waits for messages. Each
    message will be a single system ID that needs to be re-broadcasted to
    the websocket connection that is patiently waiting.

    We use an inproc SUB socket instead of separate TCP sockets to cut down
    on socket count. This also allows us to do the processing only once per
    connection.
    """
    context = zmq.Context()
    subscriber = context.socket(zmq.SUB)

    # Connect to our in-process re-broadcaster, which pumps out parsed/prepped
    # system IDs that are ready for sending.
    subscriber.connect('ipc:///tmp/emdr-map-rebroadcast.sock')
    # Disable filtering.
    subscriber.setsockopt(zmq.SUBSCRIBE, "")

    # This is a WebSocket object that we can send stuff over.
    try:
        ws = environ["wsgi.websocket"]
    except KeyError:
        # This is not a websocket connection. Redir to the map.
        start_response('301 Moved Permanently',
            [
                ('Content-Type', 'text/html'),
                ('Location', 'http://map.eve-emdr.com/'),
            ]
        )
        return ["Please see http://map.eve-emdr.com/"]

    while True:
        # This will block until messages arrive.
        system_id = subscriber.recv()
        # Send the message
        ws.send(system_id)

#################################
# Boring gevent webserver stuff #
#################################

print("=" * 80)
print("# EMDRPM WebSocket Server #".center(80))
print("-" * 80)
print("* Listening for messages from %s" % RECEIVER_BINDINGS)
print("* Listening for websockets connections on port %s" % WEBSOCKET_PORT)
print("=" * 80)

server = pywsgi.WSGIServer(
    ("", WEBSOCKET_PORT),
    websocket_worker,
    handler_class=WebSocketHandler
)

try:
    server.serve_forever()
except KeyboardInterrupt:
    print("Stopped.")<|endoftext|>


<commit_before>from models import Election

def find_idx_dict(dict_arr, val, property):
    if len(dict_arr) == 0:
        return -1

    for idx, el in enumerate(dict_arr):
        if el[property] == val:
            return idx
    return -1

def create_district(tree, circuit):
    idx_district = find_idx_dict(tree, circuit.district, 'name')

    if idx_district == -1:
        district = {
            'name': circuit.district,
            'url': circuit.election_type + '/district-' + circuit.district + '/',
            'children': []
        }
        tree.append(district)
        return tree[len(tree)-1]
    else:
        return tree[idx_district]

def create_circle(tree, circuit, is_first_child=False):
    children = tree if True == is_first_child else tree['children']
    url = circuit.election_type + '/' if True == is_first_child else tree['url']
    idx_circle = find_idx_dict(children, str(circuit.number_of_district), 'name')
    if idx_circle == -1:
        new_circle = {
            'name': str(circuit.number_of_district),
            'url': url + 'circle-' + str(circuit.number_of_district) + '/',
            'children': []
        }
        children.append(new_circle)
        return children[len(children)-1]
    else:
        return children[idx_circle]

def create_circuit(tree, circuit, is_first_child=False):
    children = tree if True == is_first_child else tree['children']
    url = circuit.election_type + '/' if True == is_first_child else tree['url']
    new_circuit = {
        'name': str(circuit.number_of_electoral_circuit),
        'url': url + 'circuit-' + str(circuit.number_of_electoral_circuit) + '/',
    }
    children.append(new_circuit)
    return children[len(children)-1]

def without_districts(tree, circuit):
    circle = create_circle(tree, circuit, True)
    circuit = create_circuit(circle, circuit)
    return tree

def without_circles(tree, circuit):
    district = create_district(tree, circuit)
    circuit = create_circuit(district, circuit)
    return tree

def full_tree(tree, circuit):
    district = create_district(tree, circuit)
    circle = create_circle(district, circuit)
    circuit = create_circuit(circle, circuit)
    return tree

def get_election_tree(type):
    tree = []
    for c in Election.objects.filter(election_type=type).distinct('number_of_electoral_circuit'):
        if 'district' == type:
            tree = full_tree(tree, c)
        elif 'city_council' == type:
            tree = without_districts(tree, c)
        elif type in ['president_first_turn', 'president_second_turn', 'voivodeship']:
            tree = without_circles(tree, c)
    return tree
<commit_msg>areas tree fix #3 Name prefixes added to circuits and circles names<commit_after>#!/usr/bin/python
# coding=UTF-8
from models import Election

def find_idx_dict(dict_arr, val, property):
    if len(dict_arr) == 0:
        return -1

    for idx, el in enumerate(dict_arr):
        if el[property] == val:
            return idx
    return -1

def create_district(tree, circuit):
    idx_district = find_idx_dict(tree, circuit.district, 'name')

    if idx_district == -1:
        district = {
            'name': circuit.district,
            'type': circuit.district,
            'url': circuit.election_type + '/district-' + circuit.district + '/',
            'children': []
        }
        tree.append(district)
        return tree[len(tree)-1]
    else:
        return tree[idx_district]

def create_circle(tree, circuit, is_first_child=False):
    children = tree if True == is_first_child else tree['children']
    url = circuit.election_type + '/' if True == is_first_child else tree['url']
    idx_circle = find_idx_dict(children, str(circuit.number_of_district), 'name')
    if idx_circle == -1:
        new_circle = {
            'name': 'Okręg nr ' + str(circuit.number_of_district),
            'type': str(circuit.number_of_district),
            'url': url + 'circle-' + str(circuit.number_of_district) + '/',
            'children': []
        }
        children.append(new_circle)
        return children[len(children)-1]
    else:
        return children[idx_circle]

def create_circuit(tree, circuit, is_first_child=False):
    children = tree if True == is_first_child else tree['children']
    url = circuit.election_type + '/' if True == is_first_child else tree['url']
    new_circuit = {
        'name': 'Obwód nr ' + str(circuit.number_of_electoral_circuit),
        'type': str(circuit.number_of_electoral_circuit),
        'url': url + 'circuit-' + str(circuit.number_of_electoral_circuit) + '/',
    }
    children.append(new_circuit)
    return children[len(children)-1]

def without_districts(tree, circuit):
    circle = create_circle(tree, circuit, True)
    circuit = create_circuit(circle, circuit)
    return tree

def without_circles(tree, circuit):
    district = create_district(tree, circuit)
    circuit = create_circuit(district, circuit)
    return tree

def full_tree(tree, circuit):
    district = create_district(tree, circuit)
    circle = create_circle(district, circuit)
    circuit = create_circuit(circle, circuit)
    return tree

def get_election_tree(type):
    tree = []
    for c in Election.objects.filter(election_type=type).distinct('number_of_electoral_circuit'):
        if 'district' == type:
            tree = full_tree(tree, c)
        elif 'city_council' == type:
            tree = without_districts(tree, c)
        elif type in ['president_first_turn', 'president_second_turn', 'voivodeship']:
            tree = without_circles(tree, c)
    return tree
<|endoftext|>


<commit_before>from ipykernel.comm import Comm
import ipywidgets as widgets
from ipywidgets import Widget

#################
# Utility stuff #
#################

# Taken from ipywidgets/widgets/tests/test_interaction.py
#            https://github.com/ipython/ipywidgets
# Licensed under Modified BSD.  Copyright IPython Development Team.  See:
#   https://github.com/ipython/ipywidgets/blob/master/COPYING.md

class DummyComm(Comm):
    comm_id = 'a-b-c-d'

    def open(self, *args, **kwargs):
        pass

    def send(self, *args, **kwargs):
        pass

    def close(self, *args, **kwargs):
        pass

_widget_attrs = {}
displayed = []
undefined = object()

def setup():
    _widget_attrs['_comm_default'] = getattr(Widget, '_comm_default', undefined)
    Widget._comm_default = lambda self: DummyComm()
    _widget_attrs['_ipython_display_'] = Widget._ipython_display_
    def raise_not_implemented(*args, **kwargs):
        raise NotImplementedError()
    Widget._ipython_display_ = raise_not_implemented

def teardown():
    for attr, value in _widget_attrs.items():
        if value is undefined:
            delattr(Widget, attr)
        else:
            setattr(Widget, attr, value)

def f(**kwargs):
    pass

def clear_display():
    global displayed
    displayed = []

def record_display(*args):
    displayed.extend(args)

# End code taken from ipywidgets

#####################
# Distributed stuff #
#####################

from operator import add

import pytest
from toolz import concat
from tornado import gen
from tornado.queues import Queue

from distributed.scheduler import Scheduler
from distributed.executor import Executor, wait
from distributed.utils_test import (cluster, _test_cluster, loop, inc,
        div, dec, throws)
from distributed.diagnostics import (ProgressWidget, MultiProgressWidget,
        progress)

def test_progressbar_widget(loop):
    @gen.coroutine
    def f(c, a, b):
        s = Scheduler((c.ip, c.port), loop=loop)
        yield s._sync_center()
        done = s.start()
        sched, report = Queue(), Queue(); s.handle_queues(sched, report)
        msg = yield report.get(); assert msg['op'] == 'stream-start'

        s.update_graph(dsk={'x': (inc, 1),
                            'y': (inc, 'x'),
                            'z': (inc, 'y')},
                       keys=['z'])
        progress = ProgressWidget(['z'], scheduler=s)

        while True:
            msg = yield report.get()
            if msg['op'] == 'key-in-memory' and msg['key'] == 'z':
                break

        progress._update()
        assert progress.bar.value == 1.0
        assert '3 / 3' in progress.bar_text.value

        sched.put_nowait({'op': 'close'})
        yield done

    _test_cluster(f, loop)

def test_multi_progressbar_widget(loop):
    @gen.coroutine
    def f(c, a, b):
        s = Scheduler((c.ip, c.port), loop=loop)
        yield s._sync_center()
        done = s.start()
        sched, report = Queue(), Queue(); s.handle_queues(sched, report)
        msg = yield report.get(); assert msg['op'] == 'stream-start'

        s.update_graph(dsk={'x-1': (inc, 1),
                            'x-2': (inc, 'x-1'),
                            'x-3': (inc, 'x-2'),
                            'y-1': (dec, 'x-3'),
                            'y-2': (dec, 'y-1'),
                            'e': (throws, 'y-2'),
                            'other': (inc, 123)},
                       keys=['e'])

        p = MultiProgressWidget(['e'], scheduler=s)

        assert p.keys == {'x': {'x-1', 'x-2', 'x-3'},
                          'y': {'y-1', 'y-2'},
                          'e': {'e'}}

        while True:
            msg = yield report.get()
            if msg['op'] == 'key-in-memory' and msg['key'] == 'x-3':
                break

        assert p.keys == {'x': set(),
                          'y': {'y-1', 'y-2'},
                          'e': {'e'}}

        p._update()
        assert p.bars['x'].value == 1.0
        assert p.bars['y'].value == 0.0
        assert p.bars['e'].value == 0.0
        assert '3 / 3' in p.bar_texts['x'].value
        assert '0 / 2' in p.bar_texts['y'].value
        assert '0 / 1' in p.bar_texts['e'].value

        while True:
            msg = yield report.get()
            if msg['op'] == 'key-in-memory' and msg['key'] == 'y-2':
                break

        p._update()
        assert p.bars['x'].value == 1.0
        assert p.bars['y'].value == 1.0
        assert p.bars['e'].value == 0.0

        assert p.keys == {'x': set(),
                          'y': set(),
                          'e': {'e'}}

        while True:
            msg = yield report.get()
            if msg['op'] == 'task-erred' and msg['key'] == 'e':
                break

        assert p.bars['x'].bar_style == 'success'
        assert p.bars['y'].bar_style == 'success'
        assert p.bars['e'].bar_style == 'danger'

        assert p.status == 'error'

        sched.put_nowait({'op': 'close'})
        yield done

    _test_cluster(f, loop)

def test_multi_progressbar_widget_after_close(loop):
    @gen.coroutine
    def f(c, a, b):
        s = Scheduler((c.ip, c.port), loop=loop)
        yield s._sync_center()
        done = s.start()
        sched, report = Queue(), Queue(); s.handle_queues(sched, report)
        msg = yield report.get(); assert msg['op'] == 'stream-start'

        s.update_graph(dsk={'x-1': (inc, 1),
                            'x-2': (inc, 'x-1'),
                            'x-3': (inc, 'x-2'),
                            'y-1': (dec, 'x-3'),
                            'y-2': (dec, 'y-1'),
                            'e': (throws, 'y-2'),
                            'other': (inc, 123)},
                       keys=['e'])

        while True:
            msg = yield report.get()
            if msg['op'] == 'key-in-memory' and msg['key'] == 'y-2':
                break

        p = MultiProgressWidget(['x-1', 'x-2', 'x-3'], scheduler=s)
        assert set(concat(p.all_keys.values())).issuperset({'x-1', 'x-2', 'x-3'})
        assert 'x' in p.bars

        sched.put_nowait({'op': 'close'})
        yield done

    _test_cluster(f, loop)

def test_values(loop):
    with cluster() as (c, [a, b]):
        with Executor(('127.0.0.1', c['port']), loop=loop) as e:
            L = [e.submit(inc, i) for i in range(5)]
            wait(L)
            p = MultiProgressWidget(L)
            p.start()
            assert set(p.all_keys) == {'inc'}
            assert len(p.all_keys['inc']) == 5
            assert p.status == 'finished'
            assert not p.pc.is_running()
            assert '5 / 5' in p.bar_texts['inc'].value
            assert p.bars['inc'].value == 1.0

            x = e.submit(throws, 1)
            p = MultiProgressWidget([x])
            p.start()
            assert p.status == 'error'
            assert p.bars[p.func(x.key)].value == 1.0

def test_progressbar_done(loop):
    with cluster() as (c, [a, b]):
        with Executor(('127.0.0.1', c['port']), loop=loop) as e:
            L = [e.submit(inc, i) for i in range(5)]
            wait(L)
            p = ProgressWidget(L)
            p.start()
            assert p.status == 'finished'
            assert p.bar.value == 1.0
            assert p.bar.bar_style == 'success'

            f = e.submit(throws, L)
            wait([f])

            p = ProgressWidget([f])
            p.start()
            assert p.status == 'error'
            assert p.bar.value == 0.0
            assert p.bar.bar_style == 'danger'

def test_multibar_complete(loop):
    @gen.coroutine
    def f(c, a, b):
        s = Scheduler((c.ip, c.port), loop=loop)
        yield s._sync_center()
        done = s.start()
        sched, report = Queue(), Queue(); s.handle_queues(sched, report)
        msg = yield report.get(); assert msg['op'] == 'stream-start'

        s.update_graph(dsk={'x-1': (inc, 1),
                            'x-2': (inc, 'x-1'),
                            'x-3': (inc, 'x-2'),
                            'y-1': (dec, 'x-3'),
                            'y-2': (dec, 'y-1'),
                            'e': (throws, 'y-2'),
                            'other': (inc, 123)},
                       keys=['e'])

        while True:
            msg = yield report.get()
            if msg['op'] == 'task-erred' and msg['key'] == 'e':
                break

        p = MultiProgressWidget(['e'], scheduler=s, complete=True)
        assert set(concat(p.all_keys.values())) == {'x-1', 'x-2', 'x-3', 'y-1',
                'y-2', 'e'}
        assert all(b.value == 1.0 for b in p.bars.values())
        assert p.bar_texts['x'].value == '3 / 3'
        assert p.bar_texts['y'].value == '2 / 2'

        sched.put_nowait({'op': 'close'})
        yield done

    _test_cluster(f, loop)

def test_fast(loop):
    with cluster() as (c, [a, b]):
        with Executor(('127.0.0.1', c['port']), loop=loop) as e:
            L = e.map(inc, range(100))
            L2 = e.map(dec, L)
            L3 = e.map(add, L, L2)
            p = progress(L3, multi=True, complete=True, notebook=True)
            assert set(p.all_keys) == {'inc', 'dec', 'add'}
<commit_msg>fix tests for progress widget<commit_after>from ipykernel.comm import Comm
import ipywidgets as widgets
from ipywidgets import Widget

#################
# Utility stuff #
#################

# Taken from ipywidgets/widgets/tests/test_interaction.py
#            https://github.com/ipython/ipywidgets
# Licensed under Modified BSD.  Copyright IPython Development Team.  See:
#   https://github.com/ipython/ipywidgets/blob/master/COPYING.md

class DummyComm(Comm):
    comm_id = 'a-b-c-d'

    def open(self, *args, **kwargs):
        pass

    def send(self, *args, **kwargs):
        pass

    def close(self, *args, **kwargs):
        pass

_widget_attrs = {}
displayed = []
undefined = object()

def setup():
    _widget_attrs['_comm_default'] = getattr(Widget, '_comm_default', undefined)
    Widget._comm_default = lambda self: DummyComm()
    _widget_attrs['_ipython_display_'] = Widget._ipython_display_
    def raise_not_implemented(*args, **kwargs):
        raise NotImplementedError()
    Widget._ipython_display_ = raise_not_implemented

def teardown():
    for attr, value in _widget_attrs.items():
        if value is undefined:
            delattr(Widget, attr)
        else:
            setattr(Widget, attr, value)

def f(**kwargs):
    pass

def clear_display():
    global displayed
    displayed = []

def record_display(*args):
    displayed.extend(args)

# End code taken from ipywidgets

#####################
# Distributed stuff #
#####################

from operator import add

import pytest
from toolz import concat
from tornado import gen
from tornado.queues import Queue

from distributed.scheduler import Scheduler
from distributed.executor import Executor, wait
from distributed.utils_test import (cluster, _test_cluster, loop, inc,
        div, dec, throws)
from distributed.diagnostics import (ProgressWidget, MultiProgressWidget,
        progress)

def test_progressbar_widget(loop):
    @gen.coroutine
    def f(c, a, b):
        s = Scheduler((c.ip, c.port), loop=loop)
        yield s._sync_center()
        done = s.start()
        sched, report = Queue(), Queue(); s.handle_queues(sched, report)
        msg = yield report.get(); assert msg['op'] == 'stream-start'

        s.update_graph(dsk={'x': (inc, 1),
                            'y': (inc, 'x'),
                            'z': (inc, 'y')},
                       keys=['z'])
        progress = ProgressWidget(['z'], scheduler=s)

        while True:
            msg = yield report.get()
            if msg['op'] == 'key-in-memory' and msg['key'] == 'z':
                break

        progress._update()
        assert progress.bar.value == 1.0
        assert '3 / 3' in progress.bar_text.value

        sched.put_nowait({'op': 'close'})
        yield done

    _test_cluster(f, loop)

def test_multi_progressbar_widget(loop):
    @gen.coroutine
    def f(c, a, b):
        s = Scheduler((c.ip, c.port), loop=loop)
        yield s._sync_center()
        done = s.start()
        sched, report = Queue(), Queue(); s.handle_queues(sched, report)
        msg = yield report.get(); assert msg['op'] == 'stream-start'

        s.update_graph(dsk={'x-1': (inc, 1),
                            'x-2': (inc, 'x-1'),
                            'x-3': (inc, 'x-2'),
                            'y-1': (dec, 'x-3'),
                            'y-2': (dec, 'y-1'),
                            'e': (throws, 'y-2'),
                            'other': (inc, 123)},
                       keys=['e'])

        p = MultiProgressWidget(['e'], scheduler=s)

        assert p.keys == {'x': {'x-1', 'x-2', 'x-3'},
                          'y': {'y-1', 'y-2'},
                          'e': {'e'}}

        while True:
            msg = yield report.get()
            if msg['op'] == 'key-in-memory' and msg['key'] == 'x-3':
                break

        assert p.keys == {'x': set(),
                          'y': {'y-1', 'y-2'},
                          'e': {'e'}}

        p._update()
        assert p.bars['x'].value == 1.0
        assert p.bars['y'].value == 0.0
        assert p.bars['e'].value == 0.0
        assert '3 / 3' in p.bar_texts['x'].value
        assert '0 / 2' in p.bar_texts['y'].value
        assert '0 / 1' in p.bar_texts['e'].value

        while True:
            msg = yield report.get()
            if msg['op'] == 'key-in-memory' and msg['key'] == 'y-2':
                break

        p._update()
        assert p.bars['x'].value == 1.0
        assert p.bars['y'].value == 1.0
        assert p.bars['e'].value == 0.0

        assert p.keys == {'x': set(),
                          'y': set(),
                          'e': {'e'}}

        while True:
            msg = yield report.get()
            if msg['op'] == 'task-erred' and msg['key'] == 'e':
                break

        assert p.bars['x'].bar_style == 'success'
        assert p.bars['y'].bar_style == 'success'
        assert p.bars['e'].bar_style == 'danger'

        assert p.status == 'error'

        sched.put_nowait({'op': 'close'})
        yield done

    _test_cluster(f, loop)

def test_multi_progressbar_widget_after_close(loop):
    @gen.coroutine
    def f(c, a, b):
        s = Scheduler((c.ip, c.port), loop=loop)
        yield s._sync_center()
        done = s.start()
        sched, report = Queue(), Queue(); s.handle_queues(sched, report)
        msg = yield report.get(); assert msg['op'] == 'stream-start'

        s.update_graph(dsk={'x-1': (inc, 1),
                            'x-2': (inc, 'x-1'),
                            'x-3': (inc, 'x-2'),
                            'y-1': (dec, 'x-3'),
                            'y-2': (dec, 'y-1'),
                            'e': (throws, 'y-2'),
                            'other': (inc, 123)},
                       keys=['e'])

        while True:
            msg = yield report.get()
            if msg['op'] == 'key-in-memory' and msg['key'] == 'y-2':
                break

        p = MultiProgressWidget(['x-1', 'x-2', 'x-3'], scheduler=s)
        assert set(concat(p.all_keys.values())).issuperset({'x-1', 'x-2', 'x-3'})
        assert 'x' in p.bars

        sched.put_nowait({'op': 'close'})
        yield done

    _test_cluster(f, loop)

def test_values(loop):
    with cluster() as (c, [a, b]):
        with Executor(('127.0.0.1', c['port']), loop=loop) as e:
            L = [e.submit(inc, i) for i in range(5)]
            wait(L)
            p = MultiProgressWidget(L)
            p.start()
            assert set(p.all_keys) == {'inc'}
            assert len(p.all_keys['inc']) == 5
            assert p.status == 'finished'
            assert not p.pc.is_running()
            assert '5 / 5' in p.bar_texts['inc'].value
            assert p.bars['inc'].value == 1.0

            x = e.submit(throws, 1)
            p = MultiProgressWidget([x])
            p.start()
            assert p.status == 'error'

def test_progressbar_done(loop):
    with cluster() as (c, [a, b]):
        with Executor(('127.0.0.1', c['port']), loop=loop) as e:
            L = [e.submit(inc, i) for i in range(5)]
            wait(L)
            p = ProgressWidget(L)
            p.start()
            assert p.status == 'finished'
            assert p.bar.value == 1.0
            assert p.bar.bar_style == 'success'

            f = e.submit(throws, L)
            wait([f])

            p = ProgressWidget([f])
            p.start()
            assert p.status == 'error'
            assert p.bar.value == 0.0
            assert p.bar.bar_style == 'danger'

def test_multibar_complete(loop):
    @gen.coroutine
    def f(c, a, b):
        s = Scheduler((c.ip, c.port), loop=loop)
        yield s._sync_center()
        done = s.start()
        sched, report = Queue(), Queue(); s.handle_queues(sched, report)
        msg = yield report.get(); assert msg['op'] == 'stream-start'

        s.update_graph(dsk={'x-1': (inc, 1),
                            'x-2': (inc, 'x-1'),
                            'x-3': (inc, 'x-2'),
                            'y-1': (dec, 'x-3'),
                            'y-2': (dec, 'y-1'),
                            'e': (throws, 'y-2'),
                            'other': (inc, 123)},
                       keys=['e'])

        while True:
            msg = yield report.get()
            if msg['op'] == 'task-erred' and msg['key'] == 'e':
                break

        p = MultiProgressWidget(['e'], scheduler=s, complete=True)
        assert set(concat(p.all_keys.values())) == {'x-1', 'x-2', 'x-3', 'y-1',
                'y-2', 'e'}
        assert all(b.value == 1.0 for k, b in p.bars.items() if k != 'e')
        assert '3 / 3' in p.bar_texts['x'].value
        assert '2 / 2' in p.bar_texts['y'].value

        sched.put_nowait({'op': 'close'})
        yield done

    _test_cluster(f, loop)

def test_fast(loop):
    with cluster() as (c, [a, b]):
        with Executor(('127.0.0.1', c['port']), loop=loop) as e:
            L = e.map(inc, range(100))
            L2 = e.map(dec, L)
            L3 = e.map(add, L, L2)
            p = progress(L3, multi=True, complete=True, notebook=True)
            assert set(p.all_keys) == {'inc', 'dec', 'add'}
<|endoftext|>


<commit_before>import sys

from django.core.management.base import BaseCommand
from optparse import make_option
from os.path import basename

from ...utils import to_db, transformer

def out(s):
    sys.stdout.write(s)
    sys.stdout.flush()

# TODO: Display help if unable to transform a file
# TODO: Switch to logging rather than direct output
class Command(BaseCommand):
    option_list = BaseCommand.option_list + (
        make_option('--sheet', action='store', dest='sheet', default=None,
                    help='Sheet name'),
        make_option('--row', action='store', dest='label_row', default=1,
                    help='Location of the row of labels, defaults to 1'),
        make_option('--v', action='store', dest='verbosity', default=0,
            help='1=Every record; 2=100 records; 3=500 records'),
    )

    def handle(self, *args, **kwargs):
        for filename in args:
            records = transformer.transform(filename, kwargs['sheet'],
                                            kwargs['label_row'])
            verbosity = int(kwargs['verbosity'])
            print "Processing %d records from %s" % (len(records),
                  basename(filename))

            to_denormalize = {'organizations': [], 'positions': []}
            records_remaining = len(records)

            for record in records:
                save_for_stats = to_db.save(record)
                to_denormalize = self.add_to_denormalize(save_for_stats,
                                                         to_denormalize)

                records_remaining -= 1
                if verbosity == 1:
                    out('.')
                elif verbosity == 2 and records_remaining % 100 == 0:
                    print "%s records remaining" % records_remaining
                elif verbosity >= 3 and records_remaining % 500 == 0:
                    print "%s records remaining" % records_remaining

            if verbosity == 1:
                out('\n')

            to_db.denormalize(to_denormalize)

    def add_to_denormalize(self, save_for_stats, to_denormalize):
        for organization in save_for_stats['organizations']:
            if organization not in to_denormalize['organizations']:
                to_denormalize['organizations'].append(organization)

        for position in save_for_stats['positions']:
            if position not in to_denormalize['positions']:
                to_denormalize['positions'].append(position)

        return to_denormalize
<commit_msg>Fixed some indentation to make flake8 happy<commit_after>import sys

from django.core.management.base import BaseCommand
from optparse import make_option
from os.path import basename

from ...utils import to_db, transformer

def out(s):
    sys.stdout.write(s)
    sys.stdout.flush()

# TODO: Display help if unable to transform a file
# TODO: Switch to logging rather than direct output
class Command(BaseCommand):
    option_list = BaseCommand.option_list + (
        make_option('--sheet', action='store', dest='sheet', default=None,
                    help='Sheet name'),
        make_option('--row', action='store', dest='label_row', default=1,
                    help='Location of the row of labels, defaults to 1'),
        make_option('--v', action='store', dest='verbosity', default=0,
                    help='1=Every record; 2=100 records; 3=500 records'),
    )

    def handle(self, *args, **kwargs):
        for filename in args:
            records = transformer.transform(filename, kwargs['sheet'],
                                            kwargs['label_row'])
            verbosity = int(kwargs['verbosity'])
            print "Processing %d records from %s" % (len(records),
                                                     basename(filename))

            to_denormalize = {'organizations': [], 'positions': []}
            records_remaining = len(records)

            for record in records:
                save_for_stats = to_db.save(record)
                to_denormalize = self.add_to_denormalize(save_for_stats,
                                                         to_denormalize)

                records_remaining -= 1
                if verbosity == 1:
                    out('.')
                elif verbosity == 2 and records_remaining % 100 == 0:
                    print "%s records remaining" % records_remaining
                elif verbosity >= 3 and records_remaining % 500 == 0:
                    print "%s records remaining" % records_remaining

            if verbosity == 1:
                out('\n')

            to_db.denormalize(to_denormalize)

    def add_to_denormalize(self, save_for_stats, to_denormalize):
        for organization in save_for_stats['organizations']:
            if organization not in to_denormalize['organizations']:
                to_denormalize['organizations'].append(organization)

        for position in save_for_stats['positions']:
            if position not in to_denormalize['positions']:
                to_denormalize['positions'].append(position)

        return to_denormalize
<|endoftext|>


<commit_before>#!/usr/bin/env python
"""
Copyright (c) 2015 Jonas Krehl <Jonas.Krehl@triebenberg.de>

Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
copyright notice and this permission notice appear in all copies.

THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
"""

from .Base import AtomPotential

class WentzelThomasFermi(AtomPotential):
	def __init__(self):
		raise NotImplemented

<commit_msg>the file is dead so it is removed<commit_after><|endoftext|>