<commit_before># Copyright 2015 Canonical, Ltd.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

from urwid import Text, Pile, ListBox, Columns
from subiquitycore.view import BaseView
from subiquitycore.ui.buttons import cancel_btn, done_btn, menu_btn
from subiquitycore.ui.interactive import PasswordEditor, StringEditor
from subiquitycore.ui.utils import Color, Padding
import logging

log = logging.getLogger('subiquitycore.network.network_configure_interface')

class NetworkConfigureInterfaceView(BaseView):
    def __init__(self, model, signal, iface):
        self.model = model
        self.signal = signal
        self.iface = iface
        self.iface_obj = self.model.get_interface(iface)
        self._build_widgets()
        super().__init__(ListBox(self._build_body()))

    def _build_widgets(self):
        self.ipv4_info = Pile(self._build_gateway_ipv4_info())
        self.ipv4_method = Pile(self._build_ipv4_method_buttons())
        self.ipv6_info = Pile(self._build_gateway_ipv6_info())
        self.ipv6_method = Pile(self._build_ipv6_method_buttons())
        if self.iface_obj.type == 'wlan':
            self.wifi_info = Pile(self._build_wifi_info())
            self.wifi_method = Pile(self._build_wifi_config())

    def _build_body(self):
        body = []
        if self.iface_obj.type == 'wlan':
            body.extend([
                Padding.center_79(self.wifi_info),
                Padding.center_79(self.wifi_method),
                Padding.line_break(""),
                ])
        body.extend([
            Padding.center_79(self.ipv4_info),
            Padding.center_79(self.ipv4_method),
            Padding.line_break(""),
            Padding.line_break(""),
            Padding.center_79(self.ipv6_info),
            Padding.center_79(self.ipv6_method),
            Padding.line_break(""),
            Padding.fixed_10(self._build_buttons())
        ])
        return body

    def _build_gateway_ipv4_info(self):
        addresses = self.iface_obj.ipv4_addresses
        ips = self.iface_obj.ip4
        methods = self.iface_obj.ip4_methods
        providers = self.iface_obj.ip4_providers
        dhcp4 = self.iface_obj.dhcp4

        if dhcp4:
            punct = ":" if len(ips) else "."
            p = [Text("Will use DHCP for IPv4" + punct)]

            for idx in range(len(ips)):
                if methods[idx] == "manual":
                    gw_info = (str(ips[idx]) + (" provided by ") + methods[idx])
                else:
                    gw_info = (str(ips[idx]) + (" provided by ") + methods[idx] +
                                (" from ") + providers[idx])
                p.append(Color.info_minor(Text(gw_info)))
        elif not dhcp4 and len(addresses) > 0:
            p = [Text("Will use static addresses for IPv4:")]
            for idx in range(len(addresses)):
                p.append(Color.info_minor(Text(addresses[idx])))
        else:
            p = [Text("IPv4 is not configured.")]

        return p

    def _build_gateway_ipv6_info(self):
        addresses = self.iface_obj.ipv6_addresses
        ips = self.iface_obj.ip6
        methods = self.iface_obj.ip6_methods
        providers = self.iface_obj.ip6_providers
        dhcp6 = self.iface_obj.dhcp6

        if dhcp6:
            punct = ":" if len(ips) else "."
            p = [Text("Will use DHCP for IPv6" + punct)]

            for idx in range(len(ips)):
                if methods[idx] == "manual":
                    gw_info = (str(ips[idx]) + (" provided by ") + methods[idx])
                else:
                    gw_info = (str(ips[idx]) + (" provided by ") + methods[idx] +
                                (" from ") + providers[idx])
                p.append(Color.info_minor(Text(gw_info)))
        elif not dhcp6 and len(addresses) > 0:
            p = [Text("Will use static addresses for IPv6:")]
            for idx in range(len(addresses)):
                p.append(Color.info_minor(Text(addresses[idx])))
        else:
            p = [Text("IPv6 is not configured.")]

        return p

    def _build_ipv4_method_buttons(self):
        dhcp4 = self.iface_obj.dhcp6

        button_padding = 40

        buttons = []
        btn = menu_btn(label="Use a static IPv4 configuration",
                       on_press=self.show_ipv4_configuration)
        buttons.append(Color.menu_button(btn, focus_map="menu_button focus"))
        btn = menu_btn(label="Use DHCPv4 on this interface",
                       on_press=self.enable_dhcp4)
        buttons.append(Color.menu_button(btn, focus_map="menu_button focus"))
        btn = menu_btn(label="Do not use",
                       on_press=self.clear_ipv4)
        buttons.append(Color.menu_button(btn, focus_map="menu_button focus"))

        padding = getattr(Padding, 'left_{}'.format(button_padding))
        buttons = [ padding(button) for button in buttons ]

        return buttons

    def _build_ipv6_method_buttons(self):
        dhcp6 = self.iface_obj.dhcp6

        button_padding = 40

        buttons = []
        btn = menu_btn(label="Use a static IPv6 configuration",
                       on_press=self.show_ipv6_configuration)
        buttons.append(Color.menu_button(btn, focus_map="menu_button focus"))
        btn = menu_btn(label="Use DHCPv6 on this interface",
                       on_press=self.enable_dhcp6)
        buttons.append(Color.menu_button(btn, focus_map="menu_button focus"))
        btn = menu_btn(label="Do not use",
                       on_press=self.clear_ipv6)
        buttons.append(Color.menu_button(btn, focus_map="menu_button focus"))

        padding = getattr(Padding, 'left_{}'.format(button_padding))
        buttons = [ padding(button) for button in buttons ]

        return buttons

    def _build_wifi_info(self):
        if self.iface_obj.essid is not None:
            return [Text("Will associate with '%s'" % (self.iface_obj.essid,))]
        else:
            return [Text("No access point configured.")]

    def _build_wifi_config(self):
        return [menu_btn(label="Configure WIFI settings",
                         on_press=self.show_wlan_configuration)]

    def _build_buttons(self):
        done = done_btn(on_press=self.done)

        buttons = [
            Color.button(done, focus_map='button focus'),
        ]
        return Pile(buttons)

    def update_interface(self):
        self.ipv4_info.contents = [ (obj, ('pack', None)) for obj in self._build_gateway_ipv4_info() ]
        self.ipv6_info.contents = [ (obj, ('pack', None)) for obj in self._build_gateway_ipv6_info() ]

    def clear_ipv4(self, btn):
        self.iface_obj.remove_ipv4_networks()
        self.model.set_default_v4_gateway(None, None)
        self.update_interface()

    def clear_ipv6(self, btn):
        self.iface_obj.remove_ipv6_networks()
        self.model.set_default_v6_gateway(None, None)
        self.update_interface()

    def enable_dhcp4(self, btn):
        self.clear_ipv4(btn)
        self.iface_obj.dhcp4 = True
        self.update_interface()

    def enable_dhcp6(self, btn):
        self.clear_ipv6(btn)
        self.iface_obj.dhcp6 = True
        self.update_interface()

    def show_wlan_configuration(self, btn):
        self.signal.emit_signal(
            'menu:network:main:configure-wlan-interface', self.iface)

    def show_ipv4_configuration(self, btn):
        self.signal.emit_signal(
            'menu:network:main:configure-ipv4-interface', self.iface)

    def show_ipv6_configuration(self, btn):
        log.debug("calling menu:network:main:configure-ipv6-interface")
        # TODO: implement UI for configuring static IPv6.
        # self.signal.emit_signal(
        #     'menu:network:main:configure-ipv6-interface', self.iface)

    def done(self, result):
        self.signal.prev_signal()

class NetworkConfigureWLANView(BaseView):
    def __init__(self, model, signal, iface):
        self.model = model
        self.signal = signal
        self.iface = iface
        self.iface_obj = self.model.get_interface(iface)
        self.essid_input = StringEditor(caption="")
        self.psk_input = PasswordEditor(caption="")
        self.body = [
            Padding.center_79(self._build_iface_inputs()),
            Padding.line_break(""),
            Padding.fixed_10(self._build_buttons())
        ]
        super().__init__(ListBox(self.body))

    def _build_iface_inputs(self):
        col = [
            Columns(
                [
                    ("weight", 0.2, Text("ESSID")),
                    ("weight", 0.3,
                     Color.string_input(self.essid_input,
                                        focus_map="string_input focus")),
                    ("weight", 0.5, Text("mmm"))
                ], dividechars=2
            ),
            Columns(
                [
                    ("weight", 0.2, Text("PSK")),
                    ("weight", 0.3,
                     Color.string_input(self.psk_input,
                                        focus_map="string_input focus")),
                    ("weight", 0.5, Text("nnn"))
                ], dividechars=2
            ),
        ]
        return Pile(col)

    def _build_buttons(self):
        cancel = Color.button(cancel_btn(on_press=self.cancel),
                              focus_map='button focus')
        done = Color.button(done_btn(on_press=self.done),
                            focus_map='button focus')

        buttons = [done, cancel]
        return Pile(buttons, focus_item=done)

    def done(self, btn):
        if self.essid_input.value:
            self.iface_obj.essid = self.essid_input.value
        else:
            self.iface_obj.essid = None
        self.iface_obj.wpa_psk = self.psk_input.value
        self.signal.prev_signal()

    def cancel(self, btn):
        self.signal.prev_signal()
<commit_msg>improve UI a bit<commit_after># Copyright 2015 Canonical, Ltd.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.

from urwid import Text, Pile, ListBox, Columns
from subiquitycore.view import BaseView
from subiquitycore.ui.buttons import cancel_btn, done_btn, menu_btn
from subiquitycore.ui.interactive import PasswordEditor, StringEditor
from subiquitycore.ui.utils import Color, Padding
import logging

log = logging.getLogger('subiquitycore.network.network_configure_interface')

class NetworkConfigureInterfaceView(BaseView):
    def __init__(self, model, signal, iface):
        self.model = model
        self.signal = signal
        self.iface = iface
        self.iface_obj = self.model.get_interface(iface)
        self._build_widgets()
        super().__init__(ListBox(self._build_body()))

    def _build_widgets(self):
        self.ipv4_info = Pile(self._build_gateway_ipv4_info())
        self.ipv4_method = Pile(self._build_ipv4_method_buttons())
        self.ipv6_info = Pile(self._build_gateway_ipv6_info())
        self.ipv6_method = Pile(self._build_ipv6_method_buttons())
        if self.iface_obj.type == 'wlan':
            self.wifi_info = Pile(self._build_wifi_info())
            self.wifi_method = Pile(self._build_wifi_config())

    def _build_body(self):
        body = []
        if self.iface_obj.type == 'wlan':
            body.extend([
                Padding.center_79(self.wifi_info),
                Padding.center_79(self.wifi_method),
                Padding.line_break(""),
                ])
        body.extend([
            Padding.center_79(self.ipv4_info),
            Padding.center_79(self.ipv4_method),
            Padding.line_break(""),
            Padding.line_break(""),
            Padding.center_79(self.ipv6_info),
            Padding.center_79(self.ipv6_method),
            Padding.line_break(""),
            Padding.fixed_10(self._build_buttons())
        ])
        return body

    def _build_gateway_ipv4_info(self):
        addresses = self.iface_obj.ipv4_addresses
        ips = self.iface_obj.ip4
        methods = self.iface_obj.ip4_methods
        providers = self.iface_obj.ip4_providers
        dhcp4 = self.iface_obj.dhcp4

        if dhcp4:
            punct = ":" if len(ips) else "."
            p = [Text("Will use DHCP for IPv4" + punct)]

            for idx in range(len(ips)):
                if methods[idx] == "manual":
                    gw_info = (str(ips[idx]) + (" provided by ") + methods[idx])
                else:
                    gw_info = (str(ips[idx]) + (" provided by ") + methods[idx] +
                                (" from ") + providers[idx])
                p.append(Color.info_minor(Text(gw_info)))
        elif not dhcp4 and len(addresses) > 0:
            p = [Text("Will use static addresses for IPv4:")]
            for idx in range(len(addresses)):
                p.append(Color.info_minor(Text(addresses[idx])))
        else:
            p = [Text("IPv4 is not configured.")]

        return p

    def _build_gateway_ipv6_info(self):
        addresses = self.iface_obj.ipv6_addresses
        ips = self.iface_obj.ip6
        methods = self.iface_obj.ip6_methods
        providers = self.iface_obj.ip6_providers
        dhcp6 = self.iface_obj.dhcp6

        if dhcp6:
            punct = ":" if len(ips) else "."
            p = [Text("Will use DHCP for IPv6" + punct)]

            for idx in range(len(ips)):
                if methods[idx] == "manual":
                    gw_info = (str(ips[idx]) + (" provided by ") + methods[idx])
                else:
                    gw_info = (str(ips[idx]) + (" provided by ") + methods[idx] +
                                (" from ") + providers[idx])
                p.append(Color.info_minor(Text(gw_info)))
        elif not dhcp6 and len(addresses) > 0:
            p = [Text("Will use static addresses for IPv6:")]
            for idx in range(len(addresses)):
                p.append(Color.info_minor(Text(addresses[idx])))
        else:
            p = [Text("IPv6 is not configured.")]

        return p

    def _build_ipv4_method_buttons(self):
        dhcp4 = self.iface_obj.dhcp6

        button_padding = 40

        buttons = []
        btn = menu_btn(label="Use a static IPv4 configuration",
                       on_press=self.show_ipv4_configuration)
        buttons.append(Color.menu_button(btn, focus_map="menu_button focus"))
        btn = menu_btn(label="Use DHCPv4 on this interface",
                       on_press=self.enable_dhcp4)
        buttons.append(Color.menu_button(btn, focus_map="menu_button focus"))
        btn = menu_btn(label="Do not use",
                       on_press=self.clear_ipv4)
        buttons.append(Color.menu_button(btn, focus_map="menu_button focus"))

        padding = getattr(Padding, 'left_{}'.format(button_padding))
        buttons = [ padding(button) for button in buttons ]

        return buttons

    def _build_ipv6_method_buttons(self):
        dhcp6 = self.iface_obj.dhcp6

        button_padding = 40

        buttons = []
        btn = menu_btn(label="Use a static IPv6 configuration",
                       on_press=self.show_ipv6_configuration)
        buttons.append(Color.menu_button(btn, focus_map="menu_button focus"))
        btn = menu_btn(label="Use DHCPv6 on this interface",
                       on_press=self.enable_dhcp6)
        buttons.append(Color.menu_button(btn, focus_map="menu_button focus"))
        btn = menu_btn(label="Do not use",
                       on_press=self.clear_ipv6)
        buttons.append(Color.menu_button(btn, focus_map="menu_button focus"))

        padding = getattr(Padding, 'left_{}'.format(button_padding))
        buttons = [ padding(button) for button in buttons ]

        return buttons

    def _build_wifi_info(self):
        if self.iface_obj.essid is not None:
            return [Text("Will associate with '%s'" % (self.iface_obj.essid,))]
        else:
            return [Text("No access point configured.")]

    def _build_wifi_config(self):
        return [Padding.left_40(menu_btn(label="Configure WIFI settings",
                                on_press=self.show_wlan_configuration))]

    def _build_buttons(self):
        done = done_btn(on_press=self.done)

        buttons = [
            Color.button(done, focus_map='button focus'),
        ]
        return Pile(buttons)

    def update_interface(self):
        self.ipv4_info.contents = [ (obj, ('pack', None)) for obj in self._build_gateway_ipv4_info() ]
        self.ipv6_info.contents = [ (obj, ('pack', None)) for obj in self._build_gateway_ipv6_info() ]

    def clear_ipv4(self, btn):
        self.iface_obj.remove_ipv4_networks()
        self.model.set_default_v4_gateway(None, None)
        self.update_interface()

    def clear_ipv6(self, btn):
        self.iface_obj.remove_ipv6_networks()
        self.model.set_default_v6_gateway(None, None)
        self.update_interface()

    def enable_dhcp4(self, btn):
        self.clear_ipv4(btn)
        self.iface_obj.dhcp4 = True
        self.update_interface()

    def enable_dhcp6(self, btn):
        self.clear_ipv6(btn)
        self.iface_obj.dhcp6 = True
        self.update_interface()

    def show_wlan_configuration(self, btn):
        self.signal.emit_signal(
            'menu:network:main:configure-wlan-interface', self.iface)

    def show_ipv4_configuration(self, btn):
        self.signal.emit_signal(
            'menu:network:main:configure-ipv4-interface', self.iface)

    def show_ipv6_configuration(self, btn):
        log.debug("calling menu:network:main:configure-ipv6-interface")
        # TODO: implement UI for configuring static IPv6.
        # self.signal.emit_signal(
        #     'menu:network:main:configure-ipv6-interface', self.iface)

    def done(self, result):
        self.signal.prev_signal()

class NetworkConfigureWLANView(BaseView):
    def __init__(self, model, signal, iface):
        self.model = model
        self.signal = signal
        self.iface = iface
        self.iface_obj = self.model.get_interface(iface)
        self.essid_input = StringEditor(caption="")
        self.psk_input = PasswordEditor(caption="")
        self.body = [
            Padding.center_79(self._build_iface_inputs()),
            Padding.line_break(""),
            Padding.fixed_10(self._build_buttons())
        ]
        super().__init__(ListBox(self.body))

    def _build_iface_inputs(self):
        col = [
            Padding.center_79(Color.info_minor(Text("Only open or WPA2 networks are supported at this time."))),
            Padding.line_break(""),
            Columns(
                [
                    ("weight", 0.2, Text("Network name:")),
                    ("weight", 0.3,
                     Color.string_input(self.essid_input,
                                        focus_map="string_input focus")),
                ], dividechars=2
            ),
            Columns(
                [
                    ("weight", 0.2, Text("Passwod:")),
                    ("weight", 0.3,
                     Color.string_input(self.psk_input,
                                        focus_map="string_input focus")),
                ], dividechars=2
            ),
        ]
        return Pile(col)

    def _build_buttons(self):
        cancel = Color.button(cancel_btn(on_press=self.cancel),
                              focus_map='button focus')
        done = Color.button(done_btn(on_press=self.done),
                            focus_map='button focus')

        buttons = [done, cancel]
        return Pile(buttons, focus_item=done)

    def done(self, btn):
        if self.iface_obj.essid is None and self.essid_input.value:
            # Turn DHCP4 on by default when specifying an ESSID for the first time...
            self.iface_obj.dhcp4 = True
        if self.essid_input.value:
            self.iface_obj.essid = self.essid_input.value
        else:
            self.iface_obj.essid = None
        self.iface_obj.wpa_psk = self.psk_input.value
        self.signal.prev_signal()

    def cancel(self, btn):
        self.signal.prev_signal()
<|endoftext|>


<commit_before>########
# Copyright (c) 2014 GigaSpaces Technologies Ltd. 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 cosmo_tester.framework.testenv import TestCase
import subprocess
import os

class TelegrafTest(TestCase):

    def test_telegraf_plugin(self):

        blueprint_path = self.copy_blueprint('resources', os.path.dirname(__file__))
        self.blueprint_yaml = os.path.join(blueprint_path, 'blueprint.yaml')

        self.upload_deploy_and_execute_install()

        outputs = self.client.deployments.outputs.get(self.test_id)
        outputs = outputs['outputs']
        ip = outputs['ip']
        self.verify_results(ip)

        self.execute_uninstall()

def verify_results(self, ip):
    cmd = "curl -G 'http://{0}:8086/query?pretty=true'" \
          " --data-urlencode \"db=monitoring_telegraf\" " \
          "--data-urlencode \"q=SELECT * from /.*/\"".format(ip)
    data = subprocess.check_output(cmd, shell=True)
    self.assertIn('cpu', data)
<commit_msg>add system tests and clean repo<commit_after>########
# Copyright (c) 2014 GigaSpaces Technologies Ltd. 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 cosmo_tester.framework.testenv import TestCase
import subprocess
import os

class TelegrafTest(TestCase):

    def test_telegraf_plugin(self):

        blueprint_path = self.copy_blueprint('resources', os.path.dirname(__file__))
        self.blueprint_yaml = os.path.join(blueprint_path, 'blueprint.yaml')

        self.upload_deploy_and_execute_install()

        outputs = self.client.deployments.outputs.get(self.test_id)
        outputs = outputs['outputs']
        ip = outputs['ip']

        self.verify_results(ip)

        self.execute_uninstall()

    def verify_results(self, ip):
        cmd = "curl -G 'http://{0}:8086/query?pretty=true'" \
              " --data-urlencode \"db=monitoring_telegraf\" " \
              "--data-urlencode \"q=SELECT * from /.*/\"".format(ip)
        data = subprocess.check_output(cmd, shell=True)
        self.assertIn('cpu', data)
<|endoftext|>


<commit_before>#!/usr/bin/env python3
"""Parses the GATE output."""

from __future__ import absolute_import, division, print_function
from builtins import range
from configparser import RawConfigParser  # Should work under 2.7, too
from future.moves.urllib.parse import urlencode
from io import open, BytesIO, StringIO
import logging
import os
import re
from subprocess import Popen
import time

from lxml import etree
import requests

from emLam import WORD, LEMMA

_anas_p = re.compile(r'{ana=([^}]+), feats=([^}]+])(?:, incorrect=[^,}]+)?, lemma=([^}]+)?}')

class GateError(Exception):
    pass

class Gate(object):
    """hunlp-GATE interface object."""
    def __init__(self, gate_props, restart_every=None,
                 modules='QT,HFSTLemm,ML3-PosLem-hfstcode', logger=None):
        """
        gate_props is the name of the GATE properties file. It is suppoesed to
        be in the hunlp-GATE directory.

        If restart_every is specified, the GATE server is restarted after that
        many sentences (counted from the parsed output). This is necessary
        because it (hunlp-)GATE leaking memory like there is no tomorrow.
        """
        # Opt: ML3-SSTok
        self.logger = logger or logging.getLogger('dummy')
        self.gate_props = gate_props
        self.gate_dir = os.path.dirname(gate_props)
        self.gate_url = self._gate_url()
        self.restart_every = restart_every
        self.modules = modules
        self.server = None
        self.parsed = 0
        self.__start_server()

    def __del__(self):
        # See also http://stackoverflow.com/questions/865115/how-do-i-correctly-clean-up-a-python-object
        # for ideas on how to replace __del__ with something better (?)
        self.__stop_server()

    def _gate_url(self):
        """Assembles the GATE url from the properties."""
        cp = RawConfigParser({'host': 'localhost', 'port': 8000})
        with open(self.gate_props) as inf:
            cp.readfp(StringIO(u'[GATE]\n' + inf.read()))
        return '{}:{}'.format(cp.get('GATE', 'host'), cp.get('GATE', 'port'))

    def __start_server(self):
        self.logger.debug('Starting server {}...'.format(self.gate_props))
        # TODO eat the server's output -- in this case, there is no need to wait
        self.server = Popen(['./gate-server.sh', self.gate_props],
                            cwd=self.gate_dir)
        self.parsed = 0
        time.sleep(10)
        self.logger.info('Started server {}'.format(self.gate_props))

    def __stop_server(self):
        self.logger.debug('Stopping server {}?'.format(self.gate_props))
        if self.server:
            self.logger.debug('Stopping server {}'.format(self.gate_props))
            try:
                requests.post('http://{}/exit'.format(self.gate_url))
            except:
                pass
            self.server.wait()
            self.logger.info('Stopped server {}'.format(self.gate_props))
        self.server = None

    def __restart_server(self):
        self.logger.debug('Restarting server {}'.format(self.gate_props))
        self.__stop_server()
        self.__start_server()

    def parse(self, text, anas=False):
        """Parses a text with a running GATE server."""
        if not self.server:
            self.__start_server()
        with open('/dev/shm/text-{}'.format(os.getpid()), 'wt') as outf:
            print(text, file=outf)

        url = 'http://{}/process?{}'.format(
            self.gate_url, urlencode({'run': self.modules,
                                      'text': text.encode('utf-8')}))
        try:
            reply = self.__send_request(url)
            if reply:
                with open('/dev/shm/xml-{}'.format(os.getpid()), 'wb') as outf:
                    print(reply, file=outf)
                parsed = parse_gate_xml(reply, anas)
                if self.restart_every:
                    self.parsed += len(parsed)
                    if self.parsed >= self.restart_every:
                        self.__restart_server()
                return parsed
        except GateError as ge:
            self.__stop_server()
            raise
        except:
            self.__stop_server()
            raise

    def __send_request(self, url):
        for tries in range(3):
            try:
                r = requests.post(url, timeout=30)
                if r.status_code != 200:
                    self.logger.warning(
                        'Server {} returned an illegal status code {} (try {})'.format(
                            self.gate_props, r.status_code, tries + 1))
                    r = None
            except Exception as e:
                self.logger.warning(
                    u'Exception {} while trying to access server {} (try {})'.format(
                        e, self.gate_url, tries + 1))
                r = None
            if not r:
                self.__restart_server()
            if r:
                return r.content
        else:
            self.logger.error(
                u'Number of tries exceeded with server {}, url {}'.format(
                    self.gate_props, url))
            raise GateError(
                u'Number of tries exceeded with server {}'.format(self.gate_props))

def parse_gate_xml_file(xml_file, get_anas=False):
    """
    Parses a GATE response from a file. We use a SAX(-like?) parser, because
    only iterparse() provide the huge_tree argument, and it is needed sometimes
    if the analysis for a word is too long. Much uglier than the dom-based
    solution, but what can one do?
    """
    text, sent = [], []
    token_feats = {'string': 0, 'lemma': 1, 'hfstana': 2}
    if get_anas:
        token_feats['anas'] = 3
    curr_token_feat = None
    tup = None
    annotation_id = None
    for event, node in etree.iterparse(xml_file, huge_tree=True, events=['start', 'end']):
        if event == 'start':
            if node.tag == 'Annotation':
                if node.get('Type') == 'Token':
                    tup = [None, None, None, None]
                    annotation_id = node.get('Id')
        else:  # end
            if node.tag == 'Annotation':
                if node.get('Type') == 'Token':
                    # The lemma might be None
                    if tup[LEMMA] is None:
                        tup[LEMMA] = tup[WORD]
                    if get_anas:
                        # If requested, find the analysis that matches lemma & POS
                        word, lemma, pos, anas = tup
                        if anas:
                            for ana in anas.split(';'):
                                try:
                                    a_ana, a_pos, a_lemma = _anas_p.match(ana).groups()
                                except:
                                    print(u'Strange ana {} / {} {} [{}]'.format(
                                        ana, lemma, pos, annotation_id))
                                    raise
                                if a_pos == pos and a_lemma == lemma:
                                    # This is the right analysis
                                    break
                            else:
                                a_ana = ''
                                # No matching analysis
                        else:
                            a_ana = ''
                            # Empty anas
                        tup[-1] = a_ana
                    sent.append(tup[:len(token_feats)])
                    tup = None
                elif node.get('Type') == 'Sentence':
                    text.append(sent)
                    sent = []
                annotation_id = None
            elif node.tag == 'Name' and tup and node.text in token_feats:
                curr_token_feat = node.text
            elif node.tag == 'Value' and curr_token_feat:
                tup[token_feats[curr_token_feat]] = node.text
                curr_token_feat = None
    return text

def parse_gate_xml_file_dom(xml_file, get_anas=False):
    """Parses a GATE response from a file."""
    dom = etree.parse(xml_file)
    root = dom.getroot()
    text, sent = [], []
    for a in root.xpath('./AnnotationSet/Annotation[@Type!="SpaceToken"]'):
        if a.attrib['Type'] == 'Token':
            word = a.find('Feature[Name="string"]').find('Value').text
            lemma = a.find('Feature[Name="lemma"]').find('Value').text
            pos = a.find('Feature[Name="hfstana"]').find('Value').text
            tup = [word, lemma if lemma else word, pos]
            if get_anas:
                anas = a.find('Feature[Name="anas"]').find('Value').text or ''
                if anas:
                    for ana in anas.split(';'):
                        try:
                            a_ana, a_pos, a_lemma = _anas_p.match(ana).groups()
                        except:
                            print(u'Strange ana {} / {} {}'.format(ana, lemma, pos))
                            raise
                        if a_pos == pos and a_lemma == lemma:
                            # This is the right analysis
                            tup.append(a_ana)
                            break
                    else:
                        tup.append('')
                        # print('Could not find the analysis for: {} / {} {}'.format(
                        #         anas, lemma, pos))
                else:
                    tup.append('')
                    # print('Could not find anas for {} {}'.format(
                    #     lemma, pos))
            sent.append(tup)
        else:
            text.append(sent)
            sent = []
    return text

def parse_gate_xml(xml, anas=False):
    """Parses a GATE response from memory."""
    return parse_gate_xml_file(BytesIO(xml), anas)
<commit_msg>Changed the timeout to 2 minutes.<commit_after>#!/usr/bin/env python3
"""Parses the GATE output."""

from __future__ import absolute_import, division, print_function
from builtins import range
from configparser import RawConfigParser  # Should work under 2.7, too
from future.moves.urllib.parse import urlencode
from io import open, BytesIO, StringIO
import logging
import os
import re
from subprocess import Popen
import time

from lxml import etree
import requests

from emLam import WORD, LEMMA

_anas_p = re.compile(r'{ana=([^}]+), feats=([^}]+])(?:, incorrect=[^,}]+)?, lemma=([^}]+)?}')

class GateError(Exception):
    pass

class Gate(object):
    """hunlp-GATE interface object."""
    def __init__(self, gate_props, restart_every=None,
                 modules='QT,HFSTLemm,ML3-PosLem-hfstcode', logger=None):
        """
        gate_props is the name of the GATE properties file. It is suppoesed to
        be in the hunlp-GATE directory.

        If restart_every is specified, the GATE server is restarted after that
        many sentences (counted from the parsed output). This is necessary
        because it (hunlp-)GATE leaking memory like there is no tomorrow.
        """
        # Opt: ML3-SSTok
        self.logger = logger or logging.getLogger('dummy')
        self.gate_props = gate_props
        self.gate_dir = os.path.dirname(gate_props)
        self.gate_url = self._gate_url()
        self.restart_every = restart_every
        self.modules = modules
        self.server = None
        self.parsed = 0
        self.__start_server()

    def __del__(self):
        # See also http://stackoverflow.com/questions/865115/how-do-i-correctly-clean-up-a-python-object
        # for ideas on how to replace __del__ with something better (?)
        self.__stop_server()

    def _gate_url(self):
        """Assembles the GATE url from the properties."""
        cp = RawConfigParser({'host': 'localhost', 'port': 8000})
        with open(self.gate_props) as inf:
            cp.readfp(StringIO(u'[GATE]\n' + inf.read()))
        return '{}:{}'.format(cp.get('GATE', 'host'), cp.get('GATE', 'port'))

    def __start_server(self):
        self.logger.debug('Starting server {}...'.format(self.gate_props))
        # TODO eat the server's output -- in this case, there is no need to wait
        self.server = Popen(['./gate-server.sh', self.gate_props],
                            cwd=self.gate_dir)
        self.parsed = 0
        time.sleep(10)
        self.logger.info('Started server {}'.format(self.gate_props))

    def __stop_server(self):
        self.logger.debug('Stopping server {}?'.format(self.gate_props))
        if self.server:
            self.logger.debug('Stopping server {}'.format(self.gate_props))
            try:
                requests.post('http://{}/exit'.format(self.gate_url))
            except:
                pass
            self.server.wait()
            self.logger.info('Stopped server {}'.format(self.gate_props))
        self.server = None

    def __restart_server(self):
        self.logger.debug('Restarting server {}'.format(self.gate_props))
        self.__stop_server()
        self.__start_server()

    def parse(self, text, anas=False):
        """Parses a text with a running GATE server."""
        if not self.server:
            self.__start_server()
        with open('/dev/shm/text-{}'.format(os.getpid()), 'wt') as outf:
            print(text, file=outf)

        url = 'http://{}/process?{}'.format(
            self.gate_url, urlencode({'run': self.modules,
                                      'text': text.encode('utf-8')}))
        try:
            reply = self.__send_request(url)
            if reply:
                with open('/dev/shm/xml-{}'.format(os.getpid()), 'wb') as outf:
                    print(reply, file=outf)
                parsed = parse_gate_xml(reply, anas)
                if self.restart_every:
                    self.parsed += len(parsed)
                    if self.parsed >= self.restart_every:
                        self.__restart_server()
                return parsed
        except GateError as ge:
            self.__stop_server()
            raise
        except:
            self.__stop_server()
            raise

    def __send_request(self, url):
        for tries in range(3):
            try:
                r = requests.post(url, timeout=120)
                if r.status_code != 200:
                    self.logger.warning(
                        'Server {} returned an illegal status code {} (try {})'.format(
                            self.gate_props, r.status_code, tries + 1))
                    r = None
            except Exception as e:
                self.logger.warning(
                    u'Exception {} while trying to access server {} (try {})'.format(
                        e, self.gate_url, tries + 1))
                r = None
            if not r:
                self.__restart_server()
            if r:
                return r.content
        else:
            self.logger.error(
                u'Number of tries exceeded with server {}, url {}'.format(
                    self.gate_props, url))
            raise GateError(
                u'Number of tries exceeded with server {}'.format(self.gate_props))

def parse_gate_xml_file(xml_file, get_anas=False):
    """
    Parses a GATE response from a file. We use a SAX(-like?) parser, because
    only iterparse() provide the huge_tree argument, and it is needed sometimes
    if the analysis for a word is too long. Much uglier than the dom-based
    solution, but what can one do?
    """
    text, sent = [], []
    token_feats = {'string': 0, 'lemma': 1, 'hfstana': 2}
    if get_anas:
        token_feats['anas'] = 3
    curr_token_feat = None
    tup = None
    annotation_id = None
    for event, node in etree.iterparse(xml_file, huge_tree=True, events=['start', 'end']):
        if event == 'start':
            if node.tag == 'Annotation':
                if node.get('Type') == 'Token':
                    tup = [None, None, None, None]
                    annotation_id = node.get('Id')
        else:  # end
            if node.tag == 'Annotation':
                if node.get('Type') == 'Token':
                    # The lemma might be None
                    if tup[LEMMA] is None:
                        tup[LEMMA] = tup[WORD]
                    if get_anas:
                        # If requested, find the analysis that matches lemma & POS
                        word, lemma, pos, anas = tup
                        if anas:
                            for ana in anas.split(';'):
                                try:
                                    a_ana, a_pos, a_lemma = _anas_p.match(ana).groups()
                                except:
                                    print(u'Strange ana {} / {} {} [{}]'.format(
                                        ana, lemma, pos, annotation_id))
                                    raise
                                if a_pos == pos and a_lemma == lemma:
                                    # This is the right analysis
                                    break
                            else:
                                a_ana = ''
                                # No matching analysis
                        else:
                            a_ana = ''
                            # Empty anas
                        tup[-1] = a_ana
                    sent.append(tup[:len(token_feats)])
                    tup = None
                elif node.get('Type') == 'Sentence':
                    text.append(sent)
                    sent = []
                annotation_id = None
            elif node.tag == 'Name' and tup and node.text in token_feats:
                curr_token_feat = node.text
            elif node.tag == 'Value' and curr_token_feat:
                tup[token_feats[curr_token_feat]] = node.text
                curr_token_feat = None
    return text

def parse_gate_xml_file_dom(xml_file, get_anas=False):
    """Parses a GATE response from a file."""
    dom = etree.parse(xml_file)
    root = dom.getroot()
    text, sent = [], []
    for a in root.xpath('./AnnotationSet/Annotation[@Type!="SpaceToken"]'):
        if a.attrib['Type'] == 'Token':
            word = a.find('Feature[Name="string"]').find('Value').text
            lemma = a.find('Feature[Name="lemma"]').find('Value').text
            pos = a.find('Feature[Name="hfstana"]').find('Value').text
            tup = [word, lemma if lemma else word, pos]
            if get_anas:
                anas = a.find('Feature[Name="anas"]').find('Value').text or ''
                if anas:
                    for ana in anas.split(';'):
                        try:
                            a_ana, a_pos, a_lemma = _anas_p.match(ana).groups()
                        except:
                            print(u'Strange ana {} / {} {}'.format(ana, lemma, pos))
                            raise
                        if a_pos == pos and a_lemma == lemma:
                            # This is the right analysis
                            tup.append(a_ana)
                            break
                    else:
                        tup.append('')
                        # print('Could not find the analysis for: {} / {} {}'.format(
                        #         anas, lemma, pos))
                else:
                    tup.append('')
                    # print('Could not find anas for {} {}'.format(
                    #     lemma, pos))
            sent.append(tup)
        else:
            text.append(sent)
            sent = []
    return text

def parse_gate_xml(xml, anas=False):
    """Parses a GATE response from memory."""
    return parse_gate_xml_file(BytesIO(xml), anas)
<|endoftext|>


<commit_before>import json

from django.core.management.base import BaseCommand
from django.conf import settings
from celery import Celery
from restkit import Resource

from corehq.apps.hqadmin.utils import parse_celery_workers, parse_celery_pings

class Command(BaseCommand):
    help = "Kills stale celery workers"

    def handle(self, *args, **options):
        _kill_stale_workers()

def _kill_stale_workers():
    celery_monitoring = getattr(settings, 'CELERY_FLOWER_URL', None)
    if celery_monitoring:
        cresource = Resource(celery_monitoring, timeout=3)
        t = cresource.get("api/workers", params_dict={'status': True}).body_string()
        all_workers = json.loads(t)
        expected_running, expected_stopped = parse_celery_workers(all_workers)

        celery = Celery()
        celery.config_from_object(settings)
        worker_responses = celery.control.ping(timeout=10)
        pings = parse_celery_pings(worker_responses)

        hosts_to_stop = filter(lambda hostname: hostname in pings, expected_stopped)
        celery.control.broadcast('shutdown', destination=hosts_to_stop)
<commit_msg>Only shut down if there are hosts to stop<commit_after>import json

from django.core.management.base import BaseCommand
from django.conf import settings
from celery import Celery
from restkit import Resource

from corehq.apps.hqadmin.utils import parse_celery_workers, parse_celery_pings

class Command(BaseCommand):
    help = "Kills stale celery workers"

    def handle(self, *args, **options):
        _kill_stale_workers()

def _kill_stale_workers():
    celery_monitoring = getattr(settings, 'CELERY_FLOWER_URL', None)
    if celery_monitoring:
        cresource = Resource(celery_monitoring, timeout=3)
        t = cresource.get("api/workers", params_dict={'status': True}).body_string()
        all_workers = json.loads(t)
        expected_running, expected_stopped = parse_celery_workers(all_workers)

        celery = Celery()
        celery.config_from_object(settings)
        worker_responses = celery.control.ping(timeout=10)
        pings = parse_celery_pings(worker_responses)

        hosts_to_stop = filter(lambda hostname: hostname in pings, expected_stopped)
        if hosts_to_stop:
            celery.control.broadcast('shutdown', destination=hosts_to_stop)
<|endoftext|>


<commit_before>########
# Copyright (c) 2014 GigaSpaces Technologies Ltd. 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.

import time

import requests

from cosmo_tester.framework.git_helper import clone
from cosmo_tester.framework.util import YamlPatcher
from cosmo_tester.test_suites.test_blueprints.nodecellar_test import (
    NODECELLAR_URL, OpenStackNodeCellarTestBase)

class OpenStackAutohealNodeCellarTest(OpenStackNodeCellarTestBase):

    AUTOHEAL_GROUP_YAML = {
        'autohealing_group': {
            'members': ['nodejs_host'],
            'policies': {
                'simple_autoheal_policy': {
                    'type': 'cloudify.policies.types.host_failure',
                    'properties': {
                        'service': ['example']
                    },
                    'triggers': {
                        'auto_heal_trigger': {
                            'type':
                                'cloudify.policies.triggers.execute_workflow',
                            'parameters': {
                                'workflow': 'heal',
                                'allow_custom_parameters': True,
                                'workflow_parameters': {
                                    'node_id': {
                                        'get_property':
                                            ['SELF', 'failing_node']
                                    },
                                    'diagnose_value': {
                                        'get_property': ['SELF', 'diagnose']
                                    },
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    def _test_nodecellar_impl(self, blueprint_file):
        self.repo_dir = clone(NODECELLAR_URL, self.workdir)
        self.blueprint_yaml = self.repo_dir / blueprint_file

        self.modify_blueprint()

        before_install, after_install = self.upload_deploy_and_execute_install(
            inputs=self.get_inputs()
        )

        self.post_install_assertions(before_install, after_install)

        self.kill_nodejs_vm()

        # make sure nodecellar is down
        self.assert_nodecellar_down(self.public_ip)

        self.wait_for_autoheal(after_install)

        after_autoheal = self.get_manager_state()

        self.post_autoheal_assertions(after_install, after_autoheal)

        self.execute_uninstall()

        self.post_uninstall_assertions()

    def kill_nodejs_vm(self, timeout=10):
        end = time.time() + timeout
        nova_controller, _, _ = self.env.handler.openstack_clients()
        srv = [s for s in nova_controller.servers.list() if 'nodejs' in s.name]
        self.assertEqual(len(srv), 1)
        srv = srv[0]
        srv.delete()
        while time.time() < end and srv in nova_controller.servers.list():
            time.sleep(1)

    def get_autoheal_execution(self):
        executions = self.client.executions.list(
            deployment_id=self.deployment_id)
        for e in executions:
            if e.workflow_id == 'heal':
                return e
        return None

    def wait_for_autoheal(self, before, timeout=1200):
        end = time.time() + timeout
        autoheal_execution = None

        while time.time() < end:
            autoheal_execution = self.get_autoheal_execution()
            if autoheal_execution is not None:
                break
            time.sleep(10)

        self.assertIsNotNone(autoheal_execution, msg="Timed out waiting "
                                                     "for auto-heal workflow")
        self.wait_for_execution(autoheal_execution, end - time.time())

    def assert_nodecellar_down(self, public_ip):
        with self.assertRaises(requests.ConnectionError):
            requests.get('http://{0}:8080'.format(self.public_ip))

    def post_autoheal_assertions(self, before_state, after_state):
        delta = self.get_manager_state_delta(before_state, after_state)
        for key in ['blueprints', 'deployments', 'node_state',
                    'nodes', 'deployment_nodes']:
            self.assertEqual(len(delta[key]), 0)
        self.assert_nodecellar_working(self.public_ip)

    def modify_blueprint(self):
        with YamlPatcher(self.blueprint_yaml) as patch:
            patch.merge_obj('groups', self.AUTOHEAL_GROUP_YAML)
            patch.merge_obj("node_types.nodecellar\\.nodes\\.MonitoredServer."
                            'interfaces.cloudify\\.interfaces\\.monitoring.'
                            'start.inputs.collectors_config.default',
                            {'ExampleCollector': {}})
            # TODO: Remove this link replacement when suitable
            imports = patch._get_object_by_path('imports')
            imports = [
                imp if 'types.yaml' not in imp
                else 'https://raw.githubusercontent.com/cloudify-cosmo/cloudify-manager/master'
                     '/resources/rest-service/cloudify/types/types.yaml'
                for imp in imports
            ]
            patch.set_value('imports', imports)
        print self.blueprint_yaml

    def get_inputs(self):
        return {
            'image': self.env.ubuntu_image_id,
            'flavor': self.env.small_flavor_id,
            'agent_user': 'ubuntu'
        }

    def test_openstack_autoheal_nodecellar(self):
        self._test_openstack_nodecellar('openstack-blueprint.yaml')
<commit_msg>One TODO resolved<commit_after>########
# Copyright (c) 2014 GigaSpaces Technologies Ltd. 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.

import time

import requests

from cosmo_tester.framework.git_helper import clone
from cosmo_tester.framework.util import YamlPatcher
from cosmo_tester.test_suites.test_blueprints.nodecellar_test import (
    NODECELLAR_URL, OpenStackNodeCellarTestBase)

class OpenStackAutohealNodeCellarTest(OpenStackNodeCellarTestBase):

    AUTOHEAL_GROUP_YAML = {
        'autohealing_group': {
            'members': ['nodejs_host'],
            'policies': {
                'simple_autoheal_policy': {
                    'type': 'cloudify.policies.types.host_failure',
                    'properties': {
                        'service': ['example']
                    },
                    'triggers': {
                        'auto_heal_trigger': {
                            'type':
                                'cloudify.policies.triggers.execute_workflow',
                            'parameters': {
                                'workflow': 'heal',
                                'allow_custom_parameters': True,
                                'workflow_parameters': {
                                    'node_id': {
                                        'get_property':
                                            ['SELF', 'failing_node']
                                    },
                                    'diagnose_value': {
                                        'get_property': ['SELF', 'diagnose']
                                    },
                                }
                            }
                        }
                    }
                }
            }
        }
    }

    def _test_nodecellar_impl(self, blueprint_file):
        self.repo_dir = clone(NODECELLAR_URL, self.workdir)
        self.blueprint_yaml = self.repo_dir / blueprint_file

        self.modify_blueprint()

        before_install, after_install = self.upload_deploy_and_execute_install(
            inputs=self.get_inputs()
        )

        self.post_install_assertions(before_install, after_install)

        self.kill_nodejs_vm()

        # make sure nodecellar is down
        self.assert_nodecellar_down(self.public_ip)

        self.wait_for_autoheal(after_install)

        after_autoheal = self.get_manager_state()

        self.post_autoheal_assertions(after_install, after_autoheal)

        self.execute_uninstall()

        self.post_uninstall_assertions()

    def kill_nodejs_vm(self, timeout=10):
        end = time.time() + timeout
        nova_controller, _, _ = self.env.handler.openstack_clients()
        srv = [s for s in nova_controller.servers.list() if 'nodejs' in s.name]
        self.assertEqual(len(srv), 1)
        srv = srv[0]
        srv.delete()
        while time.time() < end and srv in nova_controller.servers.list():
            time.sleep(1)

    def get_autoheal_execution(self):
        executions = self.client.executions.list(
            deployment_id=self.deployment_id)
        for e in executions:
            if e.workflow_id == 'heal':
                return e
        return None

    def wait_for_autoheal(self, before, timeout=1200):
        end = time.time() + timeout
        autoheal_execution = None

        while time.time() < end:
            autoheal_execution = self.get_autoheal_execution()
            if autoheal_execution is not None:
                break
            time.sleep(10)

        self.assertIsNotNone(autoheal_execution, msg="Timed out waiting "
                                                     "for auto-heal workflow")
        self.wait_for_execution(autoheal_execution, end - time.time())

    def assert_nodecellar_down(self, public_ip):
        with self.assertRaises(requests.ConnectionError):
            requests.get('http://{0}:8080'.format(self.public_ip))

    def post_autoheal_assertions(self, before_state, after_state):
        delta = self.get_manager_state_delta(before_state, after_state)
        for key in ['blueprints', 'deployments', 'node_state',
                    'nodes', 'deployment_nodes']:
            self.assertEqual(len(delta[key]), 0)
        self.assert_nodecellar_working(self.public_ip)

    def modify_blueprint(self):
        with YamlPatcher(self.blueprint_yaml) as patch:
            patch.merge_obj('groups', self.AUTOHEAL_GROUP_YAML)
            patch.merge_obj("node_types.nodecellar\\.nodes\\.MonitoredServer."
                            'interfaces.cloudify\\.interfaces\\.monitoring.'
                            'start.inputs.collectors_config.default',
                            {'ExampleCollector': {}})
        print self.blueprint_yaml

    def get_inputs(self):
        return {
            'image': self.env.ubuntu_image_id,
            'flavor': self.env.small_flavor_id,
            'agent_user': 'ubuntu'
        }

    def test_openstack_autoheal_nodecellar(self):
        self._test_openstack_nodecellar('openstack-blueprint.yaml')
<|endoftext|>


<commit_before># Copyright (c) 2015 Ultimaker B.V.
# Cura is released under the terms of the AGPLv3 or higher.

from UM.Application import Application #To get the machine manager to create the new profile in.
from UM.Settings.Profile import Profile
from UM.Settings.ProfileReader import ProfileReader
from UM.Logger import Logger
import re #Regular expressions for parsing escape characters in the settings.

##  A class that reads profile data from g-code files.
#
#   It reads the profile data from g-code files and stores it in a new profile.
#   This class currently does not process the rest of the g-code in any way.
class GCodeProfileReader(ProfileReader):
    ##  The file format version of the serialised g-code.
    #
    #   It can only read settings with the same version as the version it was
    #   written with. If the file format is changed in a way that breaks reverse
    #   compatibility, increment this version number!
    version = 1

    ##  Dictionary that defines how characters are escaped when embedded in
    #   g-code.
    #
    #   Note that the keys of this dictionary are regex strings. The values are
    #   not.
    escape_characters = {
        re.escape("\\\\"): "\\", #The escape character.
        re.escape("\\n"): "\n",  #Newlines. They break off the comment.
        re.escape("\\r"): "\r"   #Carriage return. Windows users may need this for visualisation in their editors.
    }

    ##  Initialises the g-code reader as a profile reader.
    def __init__(self):
        super().__init__()

    ##  Reads a g-code file, loading the profile from it.
    #
    #   \param file_name The name of the file to read the profile from.
    #   \return The profile that was in the specified file, if any. If the
    #   specified file was no g-code or contained no parsable profile, \code
    #   None \endcode is returned.
    def read(self, file_name):
        if file_name.split(".")[-1] != "gcode":
            return None

        prefix = ";SETTING_" + str(GCodeProfileReader.version) + " "
        prefix_length = len(prefix)

        # Loading all settings from the file.
        # They are all at the end, but Python has no reverse seek any more since Python3.
        # TODO: Consider moving settings to the start?
        serialised = ""  # Will be filled with the serialised profile.
        try:
            with open(file_name) as f:
                for line in f:
                    if line.startswith(prefix):
                        # Remove the prefix and the newline from the line and add it to the rest.
                        serialised += line[prefix_length : -1]
        except IOError as e:
            Logger.log("e", "Unable to open file %s for reading: %s", file_name, str(e))
            return None

        # Un-escape the serialised profile.
        pattern = re.compile("|".join(GCodeProfileReader.escape_characters.keys()))

        # Perform the replacement with a regular expression.
        serialised = pattern.sub(lambda m: GCodeProfileReader.escape_characters[re.escape(m.group(0))], serialised)

        # Apply the changes to the current profile.
        profile = Profile(machine_manager = Application.getInstance().getMachineManager(), read_only = False)
        try:
            profile.unserialise(serialised)
            profile.setType(None)  # Force type to none so it's correctly added.
            profile.setReadOnly(False)
            profile.setDirty(True)
        except Exception as e:  # Not a valid g-code file.
            Logger.log("e", "Unable to serialise the profile: %s", str(e))
            return None
        return profile<commit_msg>CURA-1615: Updating GCodeProfileReader I got the plugin most of all working. At least the "successfully" imported profile XY" dialog appears. But sadly the profile does not appear in the list of profiles after that. I can only guess something is blocking here. Additionally it should be noted that G-Code exported from Cura 2.1.x does not work here anymore on Cura 2.2.x.<commit_after># Copyright (c) 2015 Ultimaker B.V.
# Cura is released under the terms of the AGPLv3 or higher.

import os
import re #Regular expressions for parsing escape characters in the settings.

from UM.Application import Application #To get the machine manager to create the new profile in.
from UM.Settings.InstanceContainer import InstanceContainer
from UM.Logger import Logger

from cura.ProfileReader import ProfileReader

##  A class that reads profile data from g-code files.
#
#   It reads the profile data from g-code files and stores it in a new profile.
#   This class currently does not process the rest of the g-code in any way.
class GCodeProfileReader(ProfileReader):
    ##  The file format version of the serialized g-code.
    #
    #   It can only read settings with the same version as the version it was
    #   written with. If the file format is changed in a way that breaks reverse
    #   compatibility, increment this version number!
    version = 1

    ##  Dictionary that defines how characters are escaped when embedded in
    #   g-code.
    #
    #   Note that the keys of this dictionary are regex strings. The values are
    #   not.
    escape_characters = {
        re.escape("\\\\"): "\\", #The escape character.
        re.escape("\\n"): "\n",  #Newlines. They break off the comment.
        re.escape("\\r"): "\r"   #Carriage return. Windows users may need this for visualisation in their editors.
    }

    ##  Initialises the g-code reader as a profile reader.
    def __init__(self):
        super().__init__()

    ##  Reads a g-code file, loading the profile from it.
    #
    #   \param file_name The name of the file to read the profile from.
    #   \return The profile that was in the specified file, if any. If the
    #   specified file was no g-code or contained no parsable profile, \code
    #   None \endcode is returned.
    def read(self, file_name):
        if file_name.split(".")[-1] != "gcode":
            return None

        prefix = ";SETTING_" + str(GCodeProfileReader.version) + " "
        prefix_length = len(prefix)

        # Loading all settings from the file.
        # They are all at the end, but Python has no reverse seek any more since Python3.
        # TODO: Consider moving settings to the start?
        serialized = ""  # Will be filled with the serialized profile.
        try:
            with open(file_name) as f:
                for line in f:
                    if line.startswith(prefix):
                        # Remove the prefix and the newline from the line and add it to the rest.
                        serialized += line[prefix_length : -1]
        except IOError as e:
            Logger.log("e", "Unable to open file %s for reading: %s", file_name, str(e))
            return None

        # Un-escape the serialized profile.
        pattern = re.compile("|".join(GCodeProfileReader.escape_characters.keys()))

        # Perform the replacement with a regular expression.
        serialized = pattern.sub(lambda m: GCodeProfileReader.escape_characters[re.escape(m.group(0))], serialized)
        Logger.log("i", "Serialized the following from %s: %s" %(file_name, repr(serialized)))

        # Create an empty profile with the name of the G-code file
        profile = InstanceContainer(os.path.basename(os.path.splitext(file_name)[0]))
        profile.addMetaDataEntry("type", "quality")
        try:
            profile.deserialize(serialized)
            profile.setReadOnly(False)
        except Exception as e:  # Not a valid g-code file.
            Logger.log("e", "Unable to serialise the profile: %s", str(e))
            return None

        return profile<|endoftext|>


<commit_before># This file is part of LiSE, a framework for life simulation games.
# Copyright (c) 2013 Zachary Spector,  zacharyspector@gmail.com
from gamepiece import GamePiece
from kivy.properties import (
    ListProperty,
    ObjectProperty)
from kivy.clock import Clock
from kivy.logger import Logger

"""Widgets to represent places. Pawns move around on top of these."""

class Spot(GamePiece):
    """The icon that represents a Place.

    The Spot is located on the Board that represents the same
    Dimension that the underlying Place is in. Its coordinates are
    relative to its Board, not necessarily the window the Board is in.

    """
    demands = ["board"]
    provides = ["place"]
    postlude = [
        "CREATE VIEW place AS "
        "SELECT thing.host AS host, thing_loc.location AS place, "
        "thing_loc.branch AS branch, thing_loc.tick AS tick "
        "FROM thing "
        "JOIN thing_loc ON "
        "thing.character=thing_loc.character AND "
        "thing.name=thing_loc.name "
        "WHERE thing_loc.location NOT IN "
        "(SELECT name FROM portal UNION SELECT name FROM thing) "
        "UNION "
        "SELECT portal.host AS host, portal_loc.origin AS place, "
        "portal_loc.branch AS branch, portal_loc.tick AS tick "
        "FROM portal JOIN portal_loc ON "
        "portal.character=portal_loc.character AND "
        "portal.name=portal_loc.name "
        "UNION "
        "SELECT portal.host AS host, portal_loc.destination AS place, "
        "portal_loc.branch AS branch, portal_loc.tick AS tick "
        "FROM portal JOIN portal_loc ON "
        "portal.character=portal_loc.character AND "
        "portal.name=portal_loc.name "
        "UNION "
        "SELECT spot.host AS host, spot.place AS place, "
        "spot.branch AS branch, spot.tick AS tick FROM spot "
        "WHERE observer='Omniscient';",
        "CREATE VIEW place_facade AS "
        "SELECT thing.host AS host, thing_loc_facade.location AS place, "
        "thing_loc_facade.branch AS branch, thing_loc_facade.tick AS tick "
        "FROM thing JOIN thing_loc_facade ON "
        "thing.character=thing_loc_facade.observed AND "
        "thing.name=thing_loc_facade.name "
        "UNION "
        "SELECT portal.host AS host, portal_loc_facade.origin AS place, "
        "portal_loc_facade.branch AS branch, portal_loc_facade.tick AS tick "
        "FROM portal JOIN portal_loc_facade ON "
        "portal.character=portal_loc_facade.observed AND "
        "portal.name=portal_loc_facade.name "
        "UNION "
        "SELECT portal.host AS host, portal_loc_facade.destination AS place, "
        "portal_loc_facade.branch AS branch, portal_loc_facade.tick AS tick "
        "FROM portal JOIN portal_loc_facade ON "
        "portal.character=portal_loc_facade.observed AND "
        "portal.name=portal_loc_facade.name "
        "UNION "
        "SELECT spot.host AS host, spot.place AS place, "
        "spot.branch AS branch, spot.tick AS tick "
        "FROM spot WHERE observer<>'Omniscient';"]
    tables = [
        ("spot", {
            "columns": {
                "observer": "text not null default 'Omniscient'",
                "host": "text not null default 'Physical'",
                "place": "text not null",
                "layer": "integer not null default 0",
                "branch": "integer not null default 0",
                "tick": "integer not null default 0",
                "graphic": "text not null default 'default_spot'",
                "interactive": "boolean default 1"},
            "primary_key": (
                "observer", "host", "place",
                "branch", "tick"),
            "foreign_keys": {
                "observer, host": ("board", "observer, observed"),
                "graphic": ("graphic", "name")}}),
        ("spot_coords", {
            "columns": {
                "observer": "text not null default 'Omniscient'",
                "host": "text not null default 'Physical'",
                "place": "text not null",
                "branch": "integer not null default 0",
                "tick": "integer not null default 0",
                "x": "float not null",
                "y": "float not null"},
            "primary_key": (
                "observer", "host", "place", "branch", "tick"),
            "foreign_keys": {
                "observer, host, place": (
                    "spot", "observer, host, place")}})]
    place = ObjectProperty()
    board = ObjectProperty()
    bone = ObjectProperty()
    _touch = ObjectProperty(None, allownone=True)
    pawns_here = ListProperty([])

    def __init__(self, **kwargs):
        if 'board' in kwargs and 'closet' not in kwargs:
            kwargs['closet'] = kwargs['board'].host.closet
        kwargs['bone'] = kwargs['closet'].skeleton[u'spot'][
            unicode(kwargs['board'].facade.observer)][
            unicode(kwargs['place'].character)][
            unicode(kwargs['place'])][
            kwargs['closet'].branch].value_during(
            kwargs['closet'].tick)
        kwargs['graphic_name'] = kwargs['bone'].graphic
        kwargs['imgs'] = kwargs['closet'].get_game_piece(
            kwargs['bone'].graphic).imgs
        super(Spot, self).__init__(**kwargs)
        self.closet.register_time_listener(self.handle_time)
        self.board.spotdict[unicode(self.place)] = self
        self.bind(
            pawns_here=self.upd_pawns_here,
            center=self.upd_pawns_here)

    def __str__(self):
        """Return the name of my :class:`Place`."""
        return str(self.place)

    def __unicode__(self):
        """Return the name of my :class:`Place`."""
        return unicode(self.place)

    def upd_texs(self, *args):
        super(Spot, self).upd_texs(*args)

    def handle_time(self, b, t):
        self.bone = self.get_bone(b, t)
        self.graphic_name = self.bone.graphic
        self.repos(b, t)

    def repos(self, b, t):
        if not self.graphic_bone:
            Clock.schedule_once(lambda dt: self.repos(b, t), 0)
            return
        bone = self.get_coord_bone(b, t)
        x = bone.x + self.graphic_bone.offset_x
        y = bone.y + self.graphic_bone.offset_y
        self.pos = (x, y)

    def upd_pawns_here(self, *args):
        for pawn in self.pawns_here:
            pawn.pos = self.center

    def sanetime(self, branch, tick):
        return self.board.facade.sanetime(branch, tick)

    def get_bone(self, branch=None, tick=None):
        (branch, tick) = self.sanetime(branch, tick)
        return self.closet.skeleton[u"spot"][
            unicode(self.board.facade.observer)][
            unicode(self.board.host)][
            unicode(self.place)][branch].value_during(tick)

    def get_coord_bone(self, branch=None, tick=None):
        (branch, tick) = self.sanetime(branch, tick)
        return self.board.facade.closet.skeleton[u"spot_coords"][
            unicode(self.board.facade.observer)][
            unicode(self.board.host)][unicode(self.place)][
            branch].value_during(tick)

    def get_coords(self, branch=None, tick=None, default=None):
        """Return a pair of coordinates for where I should be on my board,
        either now, or at the given point in time.

        """
        try:
            bone = self.get_coord_bone(branch, tick)
            if bone is None:
                raise KeyError
            else:
                return (bone.x, bone.y)
        except KeyError:
            if default is not None:
                self.set_coords(*default)
                return default

    def set_coords(self, x, y, branch=None, tick=None):
        """Set my coordinates on the :class:`Board`.

        Optional arguments may be used to set my coordinates as of
        some time other than "right now".

        """
        (branch, tick) = self.sanetime(branch, tick)
        bone = self.get_coord_bone(branch, tick)
        self.closet.set_bone(bone._replace(
            x=x, y=y,
            branch=branch, tick=tick))

    def set_center_coords(self, x, y, branch=None, tick=None):
        self.set_coords(x - self.width / 2, y - self.height / 2,
                        branch, tick)

    def new_branch(self, parent, branch=None, tick=None):
        """Copy all the stuff from the parent to the child branch as of the
        given tick.

        """
        (branch, tick) = self.sanetime(branch, tick)
        prev = None
        started = False
        for skel in (
                self.board.host.closet.skeleton[u"spot"][
                    unicode(self.board.facade.observer)][
                    unicode(self.place.character)][
                    unicode(self.place)],
                self.board.host.closet.skeleton[u"spot_coords"][
                    unicode(self.board.facade.observer)][
                    unicode(self.place.character)][
                    unicode(self.place)]):
            for bone in skel.iterbones():
                if bone.tick >= tick:
                    yield bone._replace(branch=branch)
                    if (
                            not started and prev is not None and
                            bone.tick > tick and prev.tick < tick):
                        yield prev._replace(
                            branch=branch,
                            tick=tick)
                    started = True
                    prev = bone

    def on_touch_down(self, touch):
        if touch.grab_current:
            return
        if not self.collide_point(touch.x, touch.y):
            return
        touch.grab(self)
        touch.ud['spot'] = self
        self._touch = touch
        return True

    def on_touch_move(self, touch):
        if "portaling" in touch.ud:
            touch.ungrab(self)
            return
        if touch.grab_current is not self:
            return
        if self._touch is not touch:
            return
        self.center = touch.pos

    def on_touch_up(self, touch):
        if self._touch:
            self.set_coords(*self.pos)
        self._touch = None
        return super(Spot, self).on_touch_up(touch)

    def __repr__(self):
        return "{}@({},{})".format(self.place.name, self.x, self.y)
<commit_msg>unable to reproduce that last bug<commit_after># This file is part of LiSE, a framework for life simulation games.
# Copyright (c) 2013 Zachary Spector,  zacharyspector@gmail.com
from gamepiece import GamePiece
from kivy.properties import (
    ListProperty,
    ObjectProperty)
from kivy.clock import Clock
from kivy.logger import Logger

"""Widgets to represent places. Pawns move around on top of these."""

class Spot(GamePiece):
    """The icon that represents a Place.

    The Spot is located on the Board that represents the same
    Dimension that the underlying Place is in. Its coordinates are
    relative to its Board, not necessarily the window the Board is in.

    """
    demands = ["board"]
    provides = ["place"]
    postlude = [
        "CREATE VIEW place AS "
        "SELECT thing.host AS host, thing_loc.location AS place, "
        "thing_loc.branch AS branch, thing_loc.tick AS tick "
        "FROM thing "
        "JOIN thing_loc ON "
        "thing.character=thing_loc.character AND "
        "thing.name=thing_loc.name "
        "WHERE thing_loc.location NOT IN "
        "(SELECT name FROM portal UNION SELECT name FROM thing) "
        "UNION "
        "SELECT portal.host AS host, portal_loc.origin AS place, "
        "portal_loc.branch AS branch, portal_loc.tick AS tick "
        "FROM portal JOIN portal_loc ON "
        "portal.character=portal_loc.character AND "
        "portal.name=portal_loc.name "
        "UNION "
        "SELECT portal.host AS host, portal_loc.destination AS place, "
        "portal_loc.branch AS branch, portal_loc.tick AS tick "
        "FROM portal JOIN portal_loc ON "
        "portal.character=portal_loc.character AND "
        "portal.name=portal_loc.name "
        "UNION "
        "SELECT spot.host AS host, spot.place AS place, "
        "spot.branch AS branch, spot.tick AS tick FROM spot "
        "WHERE observer='Omniscient';",
        "CREATE VIEW place_facade AS "
        "SELECT thing.host AS host, thing_loc_facade.location AS place, "
        "thing_loc_facade.branch AS branch, thing_loc_facade.tick AS tick "
        "FROM thing JOIN thing_loc_facade ON "
        "thing.character=thing_loc_facade.observed AND "
        "thing.name=thing_loc_facade.name "
        "UNION "
        "SELECT portal.host AS host, portal_loc_facade.origin AS place, "
        "portal_loc_facade.branch AS branch, portal_loc_facade.tick AS tick "
        "FROM portal JOIN portal_loc_facade ON "
        "portal.character=portal_loc_facade.observed AND "
        "portal.name=portal_loc_facade.name "
        "UNION "
        "SELECT portal.host AS host, portal_loc_facade.destination AS place, "
        "portal_loc_facade.branch AS branch, portal_loc_facade.tick AS tick "
        "FROM portal JOIN portal_loc_facade ON "
        "portal.character=portal_loc_facade.observed AND "
        "portal.name=portal_loc_facade.name "
        "UNION "
        "SELECT spot.host AS host, spot.place AS place, "
        "spot.branch AS branch, spot.tick AS tick "
        "FROM spot WHERE observer<>'Omniscient';"]
    tables = [
        ("spot", {
            "columns": {
                "observer": "text not null default 'Omniscient'",
                "host": "text not null default 'Physical'",
                "place": "text not null",
                "layer": "integer not null default 0",
                "branch": "integer not null default 0",
                "tick": "integer not null default 0",
                "graphic": "text not null default 'default_spot'",
                "interactive": "boolean default 1"},
            "primary_key": (
                "observer", "host", "place",
                "branch", "tick"),
            "foreign_keys": {
                "observer, host": ("board", "observer, observed"),
                "graphic": ("graphic", "name")}}),
        ("spot_coords", {
            "columns": {
                "observer": "text not null default 'Omniscient'",
                "host": "text not null default 'Physical'",
                "place": "text not null",
                "branch": "integer not null default 0",
                "tick": "integer not null default 0",
                "x": "float not null",
                "y": "float not null"},
            "primary_key": (
                "observer", "host", "place", "branch", "tick"),
            "foreign_keys": {
                "observer, host, place": (
                    "spot", "observer, host, place")}})]
    place = ObjectProperty()
    board = ObjectProperty()
    bone = ObjectProperty()
    _touch = ObjectProperty(None, allownone=True)
    pawns_here = ListProperty([])

    def __init__(self, **kwargs):
        if 'board' in kwargs and 'closet' not in kwargs:
            kwargs['closet'] = kwargs['board'].host.closet
        kwargs['bone'] = kwargs['closet'].skeleton[u'spot'][
            unicode(kwargs['board'].facade.observer)][
            unicode(kwargs['place'].character)][
            unicode(kwargs['place'])][
            kwargs['closet'].branch].value_during(
            kwargs['closet'].tick)
        kwargs['graphic_name'] = kwargs['bone'].graphic
        kwargs['imgs'] = kwargs['closet'].get_game_piece(
            kwargs['bone'].graphic).imgs
        super(Spot, self).__init__(**kwargs)
        self.closet.register_time_listener(self.handle_time)
        self.board.spotdict[unicode(self.place)] = self
        self.bind(
            pawns_here=self.upd_pawns_here,
            center=self.upd_pawns_here)

    def __str__(self):
        """Return the name of my :class:`Place`."""
        return str(self.place)

    def __unicode__(self):
        """Return the name of my :class:`Place`."""
        return unicode(self.place)

    def upd_texs(self, *args):
        super(Spot, self).upd_texs(*args)

    def handle_time(self, b, t):
        self.bone = self.get_bone(b, t)
        self.graphic_name = self.bone.graphic
        self.repos(b, t)

    def repos(self, b, t):
        if not self.graphic_bone:
            Clock.schedule_once(lambda dt: self.repos(b, t), 0)
            return
        bone = self.get_coord_bone(b, t)
        x = bone.x + self.graphic_bone.offset_x
        y = bone.y + self.graphic_bone.offset_y
        self.pos = (x, y)

    def upd_pawns_here(self, *args):
        for pawn in self.pawns_here:
            pawn.pos = self.center

    def sanetime(self, branch, tick):
        return self.board.facade.sanetime(branch, tick)

    def get_bone(self, branch=None, tick=None):
        (branch, tick) = self.sanetime(branch, tick)
        return self.closet.skeleton[u"spot"][
            unicode(self.board.facade.observer)][
            unicode(self.board.host)][
            unicode(self.place)][branch].value_during(tick)

    def get_coord_bone(self, branch=None, tick=None):
        (branch, tick) = self.sanetime(branch, tick)
        return self.board.facade.closet.skeleton[u"spot_coords"][
            unicode(self.board.facade.observer)][
            unicode(self.board.host)][unicode(self.place)][
            branch].value_during(tick)

    def get_coords(self, branch=None, tick=None, default=None):
        """Return a pair of coordinates for where I should be on my board,
        either now, or at the given point in time.

        """
        try:
            bone = self.get_coord_bone(branch, tick)
            if bone is None:
                raise KeyError
            else:
                return (bone.x, bone.y)
        except KeyError:
            if default is not None:
                self.set_coords(*default)
                return default

    def set_coords(self, x, y, branch=None, tick=None):
        """Set my coordinates on the :class:`Board`.

        Optional arguments may be used to set my coordinates as of
        some time other than "right now".

        """
        (branch, tick) = self.sanetime(branch, tick)
        bone = self.get_coord_bone(branch, tick)
        self.closet.set_bone(bone._replace(
            x=x, y=y,
            branch=branch, tick=tick))

    def set_center_coords(self, x, y, branch=None, tick=None):
        self.set_coords(x - self.width / 2, y - self.height / 2,
                        branch, tick)

    def new_branch(self, parent, branch=None, tick=None):
        """Copy all the stuff from the parent to the child branch as of the
        given tick.

        """
        (branch, tick) = self.sanetime(branch, tick)
        prev = None
        started = False
        for skel in (
                self.board.host.closet.skeleton[u"spot"][
                    unicode(self.board.facade.observer)][
                    unicode(self.place.character)][
                    unicode(self.place)],
                self.board.host.closet.skeleton[u"spot_coords"][
                    unicode(self.board.facade.observer)][
                    unicode(self.place.character)][
                    unicode(self.place)]):
            for bone in skel.iterbones():
                if bone.tick >= tick:
                    yield bone._replace(branch=branch)
                    if (
                            not started and prev is not None and
                            bone.tick > tick and prev.tick < tick):
                        yield prev._replace(
                            branch=branch,
                            tick=tick)
                    started = True
                    prev = bone

    def on_touch_down(self, touch):
        Logger.debug("{}.on_touch_down".format(self))
        if touch.grab_current:
            Logger.debug("touch.grab_current")
            return
        if not self.collide_point(touch.x, touch.y):
            Logger.debug("{} does not collide {}".format(
                self, touch.pos))
            return
        Logger.debug("{} collides {}".format(self, touch.pos))
        touch.grab(self)
        touch.ud['spot'] = self
        self._touch = touch
        return True

    def on_touch_move(self, touch):
        if "portaling" in touch.ud:
            touch.ungrab(self)
            return
        if touch.grab_current is not self:
            return
        if self._touch is not touch:
            return
        self.center = touch.pos

    def on_touch_up(self, touch):
        if self._touch:
            self.set_coords(*self.pos)
        self._touch = None
        return super(Spot, self).on_touch_up(touch)

    def __repr__(self):
        return "{}@({},{})".format(self.place.name, self.x, self.y)
<|endoftext|>


<commit_before>######################################################################
#  This file should be kept compatible with Python 2.3, see PEP 291. #
######################################################################

# The most useful windows datatypes
from ctypes import *

BYTE = c_byte
WORD = c_ushort
DWORD = c_ulong

BOOLEAN = BYTE
BOOL = c_long
VARIANT_BOOL = c_short

ULONG = c_ulong
LONG = c_long

# in the windows header files, these are structures.
LARGE_INTEGER = c_longlong
ULARGE_INTEGER = c_ulonglong

LPCOLESTR = LPOLESTR = OLESTR = c_wchar_p
LPCWSTR = LPWSTR = c_wchar_p
LPCSTR = LPSTR = c_char_p

WPARAM = c_uint
LPARAM = c_long

ATOM = WORD
LANGID = WORD

COLORREF = DWORD
LGRPID = DWORD
LCTYPE = DWORD

LCID = DWORD

################################################################
# HANDLE types
HANDLE = c_ulong # in the header files: void *

HACCEL = HANDLE
HBITMAP = HANDLE
HBRUSH = HANDLE
HCOLORSPACE = HANDLE
HDC = HANDLE
HDESK = HANDLE
HDWP = HANDLE
HENHMETAFILE = HANDLE
HFONT = HANDLE
HGDIOBJ = HANDLE
HGLOBAL = HANDLE
HHOOK = HANDLE
HICON = HANDLE
HINSTANCE = HANDLE
HKEY = HANDLE
HKL = HANDLE
HLOCAL = HANDLE
HMENU = HANDLE
HMETAFILE = HANDLE
HMODULE = HANDLE
HMONITOR = HANDLE
HPALETTE = HANDLE
HPEN = HANDLE
HRGN = HANDLE
HRSRC = HANDLE
HSTR = HANDLE
HTASK = HANDLE
HWINSTA = HANDLE
HWND = HANDLE
SC_HANDLE = HANDLE
SERVICE_STATUS_HANDLE = HANDLE

################################################################
# Some important structure definitions

class RECT(Structure):
    _fields_ = [("left", c_long),
                ("top", c_long),
                ("right", c_long),
                ("bottom", c_long)]
tagRECT = _RECTL = RECTL = RECT

class _SMALL_RECT(Structure):
    _fields_ = [('Left', c_short),
                ('Top', c_short),
                ('Right', c_short),
                ('Bottom', c_short)]
SMALL_RECT = _SMALL_RECT

class _COORD(Structure):
    _fields_ = [('X', c_short),
                ('Y', c_short)]

class POINT(Structure):
    _fields_ = [("x", c_long),
                ("y", c_long)]
tagPOINT = _POINTL = POINTL = POINT

class SIZE(Structure):
    _fields_ = [("cx", c_long),
                ("cy", c_long)]
tagSIZE = SIZEL = SIZE

def RGB(red, green, blue):
    return red + (green << 8) + (blue << 16)

class FILETIME(Structure):
    _fields_ = [("dwLowDateTime", DWORD),
                ("dwHighDateTime", DWORD)]
_FILETIME = FILETIME

class MSG(Structure):
    _fields_ = [("hWnd", HWND),
                ("message", c_uint),
                ("wParam", WPARAM),
                ("lParam", LPARAM),
                ("time", DWORD),
                ("pt", POINT)]
tagMSG = MSG
MAX_PATH = 260

class WIN32_FIND_DATAA(Structure):
    _fields_ = [("dwFileAttributes", DWORD),
                ("ftCreationTime", FILETIME),
                ("ftLastAccessTime", FILETIME),
                ("ftLastWriteTime", FILETIME),
                ("nFileSizeHigh", DWORD),
                ("nFileSizeLow", DWORD),
                ("dwReserved0", DWORD),
                ("dwReserved1", DWORD),
                ("cFileName", c_char * MAX_PATH),
                ("cAlternameFileName", c_char * 14)]

class WIN32_FIND_DATAW(Structure):
    _fields_ = [("dwFileAttributes", DWORD),
                ("ftCreationTime", FILETIME),
                ("ftLastAccessTime", FILETIME),
                ("ftLastWriteTime", FILETIME),
                ("nFileSizeHigh", DWORD),
                ("nFileSizeLow", DWORD),
                ("dwReserved0", DWORD),
                ("dwReserved1", DWORD),
                ("cFileName", c_wchar * MAX_PATH),
                ("cAlternameFileName", c_wchar * 14)]
<commit_msg>Add back WCHAR, UINT, DOUBLE, _LARGE_INTEGER, _ULARGE_INTEGER. VARIANT_BOOL is a special _ctypes data type, not c_short.<commit_after>######################################################################
#  This file should be kept compatible with Python 2.3, see PEP 291. #
######################################################################

# The most useful windows datatypes
from ctypes import *

BYTE = c_byte
WORD = c_ushort
DWORD = c_ulong

WCHAR = c_wchar
UINT = c_uint

DOUBLE = c_double

BOOLEAN = BYTE
BOOL = c_long

from ctypes import _SimpleCData
class VARIANT_BOOL(_SimpleCData):
    _type_ = "v"
    def __repr__(self):
        return "%s(%r)" % (self.__class__.__name__, self.value)

ULONG = c_ulong
LONG = c_long

# in the windows header files, these are structures.
_LARGE_INTEGER = LARGE_INTEGER = c_longlong
_ULARGE_INTEGER = ULARGE_INTEGER = c_ulonglong

LPCOLESTR = LPOLESTR = OLESTR = c_wchar_p
LPCWSTR = LPWSTR = c_wchar_p
LPCSTR = LPSTR = c_char_p

WPARAM = c_uint
LPARAM = c_long

ATOM = WORD
LANGID = WORD

COLORREF = DWORD
LGRPID = DWORD
LCTYPE = DWORD

LCID = DWORD

################################################################
# HANDLE types
HANDLE = c_ulong # in the header files: void *

HACCEL = HANDLE
HBITMAP = HANDLE
HBRUSH = HANDLE
HCOLORSPACE = HANDLE
HDC = HANDLE
HDESK = HANDLE
HDWP = HANDLE
HENHMETAFILE = HANDLE
HFONT = HANDLE
HGDIOBJ = HANDLE
HGLOBAL = HANDLE
HHOOK = HANDLE
HICON = HANDLE
HINSTANCE = HANDLE
HKEY = HANDLE
HKL = HANDLE
HLOCAL = HANDLE
HMENU = HANDLE
HMETAFILE = HANDLE
HMODULE = HANDLE
HMONITOR = HANDLE
HPALETTE = HANDLE
HPEN = HANDLE
HRGN = HANDLE
HRSRC = HANDLE
HSTR = HANDLE
HTASK = HANDLE
HWINSTA = HANDLE
HWND = HANDLE
SC_HANDLE = HANDLE
SERVICE_STATUS_HANDLE = HANDLE

################################################################
# Some important structure definitions

class RECT(Structure):
    _fields_ = [("left", c_long),
                ("top", c_long),
                ("right", c_long),
                ("bottom", c_long)]
tagRECT = _RECTL = RECTL = RECT

class _SMALL_RECT(Structure):
    _fields_ = [('Left', c_short),
                ('Top', c_short),
                ('Right', c_short),
                ('Bottom', c_short)]
SMALL_RECT = _SMALL_RECT

class _COORD(Structure):
    _fields_ = [('X', c_short),
                ('Y', c_short)]

class POINT(Structure):
    _fields_ = [("x", c_long),
                ("y", c_long)]
tagPOINT = _POINTL = POINTL = POINT

class SIZE(Structure):
    _fields_ = [("cx", c_long),
                ("cy", c_long)]
tagSIZE = SIZEL = SIZE

def RGB(red, green, blue):
    return red + (green << 8) + (blue << 16)

class FILETIME(Structure):
    _fields_ = [("dwLowDateTime", DWORD),
                ("dwHighDateTime", DWORD)]
_FILETIME = FILETIME

class MSG(Structure):
    _fields_ = [("hWnd", HWND),
                ("message", c_uint),
                ("wParam", WPARAM),
                ("lParam", LPARAM),
                ("time", DWORD),
                ("pt", POINT)]
tagMSG = MSG
MAX_PATH = 260

class WIN32_FIND_DATAA(Structure):
    _fields_ = [("dwFileAttributes", DWORD),
                ("ftCreationTime", FILETIME),
                ("ftLastAccessTime", FILETIME),
                ("ftLastWriteTime", FILETIME),
                ("nFileSizeHigh", DWORD),
                ("nFileSizeLow", DWORD),
                ("dwReserved0", DWORD),
                ("dwReserved1", DWORD),
                ("cFileName", c_char * MAX_PATH),
                ("cAlternameFileName", c_char * 14)]

class WIN32_FIND_DATAW(Structure):
    _fields_ = [("dwFileAttributes", DWORD),
                ("ftCreationTime", FILETIME),
                ("ftLastAccessTime", FILETIME),
                ("ftLastWriteTime", FILETIME),
                ("nFileSizeHigh", DWORD),
                ("nFileSizeLow", DWORD),
                ("dwReserved0", DWORD),
                ("dwReserved1", DWORD),
                ("cFileName", c_wchar * MAX_PATH),
                ("cAlternameFileName", c_wchar * 14)]
<|endoftext|>