<commit_before># -*- coding: utf-8 -*-
##############################################################################
#
#    OpenERP, Open Source Management Solution
#    This module copyright (C) 2013 Therp BV (<http://therp.nl>).
#
#    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 openerp.models import Model
from openerp.modules.registry import RegistryManager
from openerp import SUPERUSER_ID

class res_users(Model):
    _inherit = 'res.users'

    def _login(self, db, login, password):
        uid = super(res_users, self)._login(db, login, password)

        if uid:
            self.update_dynamic_groups(uid, db)

        return uid

    def update_dynamic_groups(self, uid, db):
        cr = self.pool._db.cursor(serialized=True)
        user = self.pool.get('res.users').browse(cr, SUPERUSER_ID, uid)
        groups_obj = self.pool.get('res.groups')
        user.write(
            {
                'groups_id': [
                    (4, dynamic_group.id)
                    if dynamic_group.eval_dynamic_group_condition(uid=uid)
                    else (3, dynamic_group.id)
                    for dynamic_group in groups_obj.browse(
                        cr, SUPERUSER_ID,
                        groups_obj.search(cr, SUPERUSER_ID,
                                          [('is_dynamic', '=', True)]))
                ],
            })
        cr.commit()
        cr.close()
<commit_msg>[FIX] actually use a nonserialized cursor<commit_after># -*- coding: utf-8 -*-
##############################################################################
#
#    OpenERP, Open Source Management Solution
#    This module copyright (C) 2013 Therp BV (<http://therp.nl>).
#
#    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 openerp.models import Model
from openerp.modules.registry import RegistryManager
from openerp import SUPERUSER_ID

class res_users(Model):
    _inherit = 'res.users'

    def _login(self, db, login, password):
        uid = super(res_users, self)._login(db, login, password)

        if uid:
            self.update_dynamic_groups(uid, db)

        return uid

    def update_dynamic_groups(self, uid, db):
        cr = self.pool._db.cursor(serialized=False)
        user = self.pool.get('res.users').browse(cr, SUPERUSER_ID, uid)
        groups_obj = self.pool.get('res.groups')
        user.write(
            {
                'groups_id': [
                    (4, dynamic_group.id)
                    if dynamic_group.eval_dynamic_group_condition(uid=uid)
                    else (3, dynamic_group.id)
                    for dynamic_group in groups_obj.browse(
                        cr, SUPERUSER_ID,
                        groups_obj.search(cr, SUPERUSER_ID,
                                          [('is_dynamic', '=', True)]))
                ],
            })
        cr.commit()
        cr.close()
<|endoftext|>


<commit_before>#!/usr/bin/python

from twisted.internet import protocol, reactor, endpoints, defer
from twisted.protocols import basic
from neopixel import *
from itertools import chain, repeat
import sys
import json
import uuid

# Local modules
from cube import CubeMode

# LED strip configuration:
LED_COUNT      = 6       # Number of LED pixels.
LED_PIN        = 18      # GPIO pin connected to the pixels (must support PWM!)
LED_FREQ_HZ    = 800000  # LED signal frequency in hertz (usually 800khz)
LED_DMA        = 5       # DMA channel to use for generating signal (try 5)
LED_BRIGHTNESS = 255     # Set to 0 for darkest and 255 for brightest

# =----------------------------------------------------------------------------=
class EdgeProtocol(basic.LineReceiver):
    def lineReceived(self, line):
        if not line.strip():
            self.transport.loseConnection()
            return

        deferred = self.factory.parse_command(line)

        def writeResponse(data):
            self.transport.write(json.dumps({
                'success': True,
                'data': data
                }) + "\r\n")
        deferred.addCallback(writeResponse)

        def onError(err):
            self.transport.write(json.dumps({
                'success': False,
                'message': err.getErrorMessage(),
                'trace': str(err)
                }) + "\r\n")
        deferred.addErrback(onError)

class EdgeFactory(protocol.Factory):
    protocol = EdgeProtocol

    def __init__(self, light_controller):
        self.light_controller = light_controller
        self.active_locks = [ None ] * 6
        self.lock_map = {}
        self.mode = CubeMode.normal

        self.dispatch = {
            'setColors':   self.set_colors,
            'getColors':   self.get_colors,
            'requestLock': self.request_lock,
            'releaseLock': self.release_lock,
            'setMode':     self.set_mode,
            'getMode':     self.get_mode
        }

    def parse_command(self, line):
        """Parse the incomming command and route to correct handler"""

        try:
            data = json.loads(line)
        except:
            return defer.fail(Exception('Unable to parse JSON payload'))

        if 'command' not in data or data['command'] not in self.dispatch:
            return defer.fail(Exception('Unknown or missing command'))

        return self.dispatch[data['command']](data)

    def set_colors(self, command):
        """
        Set the active color unless a lock is currently in place

        colors - A set of colors equal in length to the device requirements
                 null values may be used to not set that light
        mode - The minimum mode required to show these colors
        """

        if 'colors' not in command:
            return defer.fail(Exception('No colors parameter was specified'))

        if 'mode' not in command:
            command['mode'] = 'normal'

        if CubeMode.lookup[command['mode']] < self.mode:
            return defer.fail(Exception('Specified mode is less than current device mode'))

        try:
            if 'lock' in command:
                if command['lock'] not in self.active_locks:
                    return defer.fail(Exception('Lock code unknown or expired'))

                else:
                    locked_colors = [ color
                        if self.active_locks[index] == command['lock']
                        else None
                        for index, color
                        in enumerate(command['colors']) ]
                    self.light_controller.set(locked_colors, save=False)

            else:
                locked_colors = [ color
                    if self.active_locks[index]
                    else None
                    for index, color
                    in enumerate(command['colors']) ]
                active_colors = [ color
                    if not self.active_locks[index]
                    else None
                    for index, color
                    in enumerate(command['colors']) ]
                self.light_controller.set(locked_colors, show=False)
                self.light_controller.set(active_colors)

            return defer.succeed({ 'colors': self.light_controller.get() })
        except:
            return defer.fail()

    def get_colors(self, command):
        try:
            colors = self.light_controller.get()
            return defer.succeed({ 'colors': colors })
        except:
            return defer.fail()

    def request_lock(self, command):
        """
        duration - max millis to hold lock
        lights - which lights to lock, array of indices (0..5)
        """

        if 'duration' not in command:
            return defer.fail(Exception('No lock duration specified'))

        if 'lights' not in command or len(command['lights']) < 1:
            return defer.fail(Exception('No light lock set specified'))

        if 'mode' not in command:
            command['mode'] = 'normal'

        if CubeMode.lookup[command['mode']] < self.mode:
            return defer.fail(Exception('Specified mode is less than current device mode'))

        if len([ light for light in command['lights'] if self.active_locks[light] ]) > 0:
            return defer.fail(Exception('One or more requested lights already locked'))

        # Conditions met, let's award a lock
        lock_code = str(uuid.uuid4())
        for light in command['lights']:
            self.active_locks[light] = lock_code

        self.lock_map[lock_code] = reactor.callLater(
            command['duration'] / 1000.0,
            self.clear_lock,
            lock_code
        )

        return defer.succeed({
            'lock': lock_code,
            'lights': command['lights'],
            'duration': command['duration']
        })

    def release_lock(self, command):
        """
        lock - the lock code to release
        """

        if 'lock' not in command:
            return defer.fail(Exception('No lock code specified'))

        if len([ code for code in self.active_locks if code == command['lock'] ]) < 1:
            return defer.fail(Exception('Requested lock code does not exist'))

        self.clear_lock(command['lock'])
        return defer.succeed({ 'lock': command['lock'] })

    def set_mode(self, command):
        """
        Most services will want to push individual colors (weather, stock, etc)
        as "ambient" priority while animations will generally use "normal"
        level. Information such as alerts, emergency notifications, etc, may use
        the "critical" level.

        mode - The mode to set the device to
        """

        if 'mode' not in command:
            return defer.fail(Exception('No mode specified'))

        mode = CubeMode.lookup[command['mode']]
        if mode == None:
            return defer.fail(Exception('Unknown mode specified'))

        if self.mode is not mode:
            self.mode = mode

            # also, clear all locks and reset the lights
            for key in self.lock_map.keys():
                self.clear_lock(key)

            try:
                self.light_controller.reset()
            except:
                pass

        return defer.succeed({ 'mode': CubeMode.name[self.mode] })

    def get_mode(self, command):
        return defer.succeed({ 'mode': CubeMode.name[self.mode] })

    # =------------------------------------------------------------------------=

    def clear_lock(self, lock_code):
        call_id = self.lock_map.pop(lock_code, None)
        if call_id and call_id.active():
            call_id.cancel()

        colors = self.light_controller.get()
        locked_colors = [ color
            if self.active_locks[index] == lock_code
            else None
            for index, color
            in enumerate(colors) ]

        for index, code in enumerate(self.active_locks):
            if code == lock_code:
                self.active_locks[index] = None

        try:
            self.light_controller.set(locked_colors, save=False)
        except:
            pass # eat error

# =----------------------------------------------------------------------------=
class LightController:
    """Run the lights themselves"""

    def __init__(self, config):
        self.current_colors = [0] * config['led_count']
        self.config = config

        # Create NeoPixel object with appropriate configuration.
        self.strip = Adafruit_NeoPixel(
            config['led_count'],
            config['led_pin'],
            config['frequency'],
            config['dma_channel'],
            False, # invert signal
            config['brightness'],
            0, # channel (default 0)
            ws.WS2811_STRIP_GRB)

        # Intialize the library (must be called once before other functions).
        self.strip.begin()
        self.reset()

    def clean_color(self, value):
        if value == None: return None
        if isinstance(value, int): return value
        if isinstance(value, basestring): return self.hex_to_rgb(value)
        return None # fallback, unknown value type

    def hex_to_rgb(self, value):
        value = value.lstrip('#')
        lv = len(value)
        return Color(*(int(value[i:i + lv // 3], 16) for i in range(0, lv, lv // 3)))

    def reset(self):
        self.set([0] * self.config['led_count'])
        print 'Edge colors reset to off'

    def set(self, colors, save=True, show=True):
        if len(colors) < self.strip.numPixels():
            raise Exception('Insufficient colors specified')

        color_set = map(self.clean_color, colors[0:self.strip.numPixels()])

        for i in range(self.strip.numPixels()):
            if color_set[i] is not None:
                if show: self.strip.setPixelColor(i, color_set[i])
                if save: self.current_colors[i] = color_set[i]

        self.strip.show()

        return self.current_colors

    def get(self):
        return self.current_colors

# =----------------------------------------------------------------------------=
def main():
    print 'Starting Data Semi Cube Edge Service...'
    light_controller = LightController({
        'led_count': LED_COUNT,
        'led_pin': LED_PIN,
        'frequency': LED_FREQ_HZ,
        'dma_channel': LED_DMA,
        'brightness': LED_BRIGHTNESS
    })

    endpoints.serverFromString(reactor, "tcp:8300").listen(EdgeFactory(light_controller))
    reactor.run()
    print 'Edge Service Started'

if __name__ == '__main__':
    main()
<commit_msg>Set file width to 100chars and expand class separators with titles<commit_after>#!/usr/bin/python

from twisted.internet import protocol, reactor, endpoints, defer
from twisted.protocols import basic
from neopixel import *
from itertools import chain, repeat
import sys
import json
import uuid

# Local modules
from cube import CubeMode

# =------------------------------------------------------------------------------= Configuration =--=
# LED strip configuration:
LED_COUNT      = 6       # Number of LED pixels.
LED_PIN        = 18      # GPIO pin connected to the pixels (must support PWM!)
LED_FREQ_HZ    = 800000  # LED signal frequency in hertz (usually 800khz)
LED_DMA        = 5       # DMA channel to use for generating signal (try 5)
LED_BRIGHTNESS = 255     # Set to 0 for darkest and 255 for brightest

# =--------------------------------------------------------------------------= Network Protocol =--=
class EdgeProtocol(basic.LineReceiver):
    def lineReceived(self, line):
        if not line.strip():
            self.transport.loseConnection()
            return

        deferred = self.factory.parse_command(line)

        def writeResponse(data):
            self.transport.write(json.dumps({
                'success': True,
                'data': data
                }) + "\r\n")
        deferred.addCallback(writeResponse)

        def onError(err):
            self.transport.write(json.dumps({
                'success': False,
                'message': err.getErrorMessage(),
                'trace': str(err)
                }) + "\r\n")
        deferred.addErrback(onError)

class EdgeFactory(protocol.Factory):
    protocol = EdgeProtocol

    def __init__(self, light_controller):
        self.light_controller = light_controller
        self.active_locks = [ None ] * 6
        self.lock_map = {}
        self.mode = CubeMode.normal

        self.dispatch = {
            'setColors':   self.set_colors,
            'getColors':   self.get_colors,
            'requestLock': self.request_lock,
            'releaseLock': self.release_lock,
            'setMode':     self.set_mode,
            'getMode':     self.get_mode
        }

    def parse_command(self, line):
        """Parse the incomming command and route to correct handler"""

        try:
            data = json.loads(line)
        except:
            return defer.fail(Exception('Unable to parse JSON payload'))

        if 'command' not in data or data['command'] not in self.dispatch:
            return defer.fail(Exception('Unknown or missing command'))

        return self.dispatch[data['command']](data)

    # Protocol Commands

    def set_colors(self, command):
        """
        Set the active color unless a lock is currently in place

        colors - A set of colors equal in length to the device requirements
                 null values may be used to not set that light
        mode - The minimum mode required to show these colors
        """

        if 'colors' not in command:
            return defer.fail(Exception('No colors parameter was specified'))

        if 'mode' not in command:
            command['mode'] = 'normal'

        if CubeMode.lookup[command['mode']] < self.mode:
            return defer.fail(Exception('Specified mode is less than current device mode'))

        try:
            if 'lock' in command:
                if command['lock'] not in self.active_locks:
                    return defer.fail(Exception('Lock code unknown or expired'))

                else:
                    locked_colors = [ color
                        if self.active_locks[index] == command['lock']
                        else None
                        for index, color
                        in enumerate(command['colors']) ]
                    self.light_controller.set(locked_colors, save=False)

            else:
                locked_colors = [ color
                    if self.active_locks[index]
                    else None
                    for index, color
                    in enumerate(command['colors']) ]
                active_colors = [ color
                    if not self.active_locks[index]
                    else None
                    for index, color
                    in enumerate(command['colors']) ]
                self.light_controller.set(locked_colors, show=False)
                self.light_controller.set(active_colors)

            return defer.succeed({ 'colors': self.light_controller.get() })
        except:
            return defer.fail()

    def get_colors(self, command):
        try:
            colors = self.light_controller.get()
            return defer.succeed({ 'colors': colors })
        except:
            return defer.fail()

    def request_lock(self, command):
        """
        duration - max millis to hold lock
        lights - which lights to lock, array of indices (0..5)
        """

        if 'duration' not in command:
            return defer.fail(Exception('No lock duration specified'))

        if 'lights' not in command or len(command['lights']) < 1:
            return defer.fail(Exception('No light lock set specified'))

        if 'mode' not in command:
            command['mode'] = 'normal'

        if CubeMode.lookup[command['mode']] < self.mode:
            return defer.fail(Exception('Specified mode is less than current device mode'))

        if len([ light for light in command['lights'] if self.active_locks[light] ]) > 0:
            return defer.fail(Exception('One or more requested lights already locked'))

        # Conditions met, let's award a lock
        lock_code = str(uuid.uuid4())
        for light in command['lights']:
            self.active_locks[light] = lock_code

        self.lock_map[lock_code] = reactor.callLater(
            command['duration'] / 1000.0,
            self.clear_lock,
            lock_code
        )

        return defer.succeed({
            'lock': lock_code,
            'lights': command['lights'],
            'duration': command['duration']
        })

    def release_lock(self, command):
        """
        lock - the lock code to release
        """

        if 'lock' not in command:
            return defer.fail(Exception('No lock code specified'))

        if len([ code for code in self.active_locks if code == command['lock'] ]) < 1:
            return defer.fail(Exception('Requested lock code does not exist'))

        self.clear_lock(command['lock'])
        return defer.succeed({ 'lock': command['lock'] })

    def set_mode(self, command):
        """
        Most services will want to push individual colors (weather, stock, etc)
        as "ambient" priority while animations will generally use "normal"
        level. Information such as alerts, emergency notifications, etc, may use
        the "critical" level.

        mode - The mode to set the device to
        """

        if 'mode' not in command:
            return defer.fail(Exception('No mode specified'))

        mode = CubeMode.lookup[command['mode']]
        if mode == None:
            return defer.fail(Exception('Unknown mode specified'))

        if self.mode is not mode:
            self.mode = mode

            # also, clear all locks and reset the lights
            for key in self.lock_map.keys():
                self.clear_lock(key)

            try:
                self.light_controller.reset()
            except:
                pass

        return defer.succeed({ 'mode': CubeMode.name[self.mode] })

    def get_mode(self, command):
        return defer.succeed({ 'mode': CubeMode.name[self.mode] })

    # Helper Functions

    def clear_lock(self, lock_code):
        call_id = self.lock_map.pop(lock_code, None)
        if call_id and call_id.active():
            call_id.cancel()

        colors = self.light_controller.get()
        locked_colors = [ color
            if self.active_locks[index] == lock_code
            else None
            for index, color
            in enumerate(colors) ]

        for index, code in enumerate(self.active_locks):
            if code == lock_code:
                self.active_locks[index] = None

        try:
            self.light_controller.set(locked_colors, save=False)
        except:
            pass # eat error

# =--------------------------------------------------------------------------= Light Controller =--=
class LightController:
    """Run the lights themselves"""

    def __init__(self, config):
        self.current_colors = [0] * config['led_count']
        self.config = config

        # Create NeoPixel object with appropriate configuration.
        self.strip = Adafruit_NeoPixel(
            config['led_count'],
            config['led_pin'],
            config['frequency'],
            config['dma_channel'],
            False, # invert signal
            config['brightness'],
            0, # channel (default 0)
            ws.WS2811_STRIP_GRB)

        # Intialize the library (must be called once before other functions).
        self.strip.begin()
        self.reset()

    def clean_color(self, value):
        if value == None: return None
        if isinstance(value, int): return value
        if isinstance(value, basestring): return self.hex_to_rgb(value)
        return None # fallback, unknown value type

    def hex_to_rgb(self, value):
        value = value.lstrip('#')
        lv = len(value)
        return Color(*(int(value[i:i + lv // 3], 16) for i in range(0, lv, lv // 3)))

    def reset(self):
        self.set([0] * self.config['led_count'])
        print 'Edge colors reset to off'

    def set(self, colors, save=True, show=True):
        if len(colors) < self.strip.numPixels():
            raise Exception('Insufficient colors specified')

        color_set = map(self.clean_color, colors[0:self.strip.numPixels()])

        for i in range(self.strip.numPixels()):
            if color_set[i] is not None:
                if show: self.strip.setPixelColor(i, color_set[i])
                if save: self.current_colors[i] = color_set[i]

        self.strip.show()

        return self.current_colors

    def get(self):
        return self.current_colors

# =--------------------------------------------------------------------------------------= Main =--=
def main():
    print 'Starting Data Semi Cube Edge Service...'
    light_controller = LightController({
        'led_count': LED_COUNT,
        'led_pin': LED_PIN,
        'frequency': LED_FREQ_HZ,
        'dma_channel': LED_DMA,
        'brightness': LED_BRIGHTNESS
    })

    endpoints.serverFromString(reactor, "tcp:8300").listen(EdgeFactory(light_controller))
    reactor.run()
    print 'Edge Service Started'

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


<commit_before>"""
geopy is a Python 2 and 3 client for several popular geocoding web services.

geopy makes it easy for Python developers to locate the coordinates of
addresses, cities, countries, and landmarks across the globe using third-party
geocoders and other data sources.
"""

from geopy.point import Point
from geopy.location import Location
from geopy.geocoders import * # pylint: disable=W0401

__version__ = "1.4.0"
<commit_msg>1.5.0<commit_after>"""
geopy is a Python 2 and 3 client for several popular geocoding web services.

geopy makes it easy for Python developers to locate the coordinates of
addresses, cities, countries, and landmarks across the globe using third-party
geocoders and other data sources.

geopy is tested against CPython 2.7, CPython 3.4, and PyPy.
"""

from geopy.point import Point
from geopy.location import Location
from geopy.geocoders import * # pylint: disable=W0401

__version__ = "1.5.0"
<|endoftext|>


<commit_before># coding: utf8
from grow.common import config
from grow.common import utils
from xtermcolor import colorize
import json
import logging
import os
import platform
import semantic_version
import subprocess
import sys
import urllib
import urlparse

VERSION = config.VERSION
RELEASES_API = 'https://api.github.com/repos/grow/grow/releases'
INSTALLER_COMMAND = ('/usr/bin/python -c "$(curl -fsSL '
                     'https://raw.github.com/grow/grow/master/install.py)"')

PLATFORM = None
if 'Linux' in platform.system():
  PLATFORM = 'linux'
elif 'Darwin' in platform.system():
  PLATFORM = 'mac'

class Error(Exception):
    pass

class LatestVersionCheckError(Error):
    pass

def get_this_version():
    return VERSION

def get_latest_version():
    try:
        releases = json.loads(urllib.urlopen(RELEASES_API).read())
        for release in releases:
            if release['prerelease']:
                continue
            for each_asset in release['assets']:
                if PLATFORM in each_asset.get('name', '').lower():
                    return release['tag_name']
    except Exception as e:
        text = 'Cannot check for updates to the SDK while offline.'
        logging.error(colorize(text, ansi=198))
        raise LatestVersionCheckError(str(e))

def check_sdk_version(pod):
    sdk_version = get_this_version()
    requires_version = pod.grow_version
    if requires_version is None:
        return
    if (semantic_version.Version(sdk_version)
        not in semantic_version.Spec(requires_version)):
        text = 'ERROR! Pod requires Grow SDK version: {}'.format(requires_version)
        logging.error(colorize(text, ansi=197))
        raise LatestVersionCheckError(str(text))

def check_for_sdk_updates(auto_update_prompt=False):
    try:
        theirs = get_latest_version()
        yours = config.VERSION
    except LatestVersionCheckError:
        return
    if theirs <= yours:
        return
    url = 'https://github.com/grow/grow/releases/tag/{}'.format(theirs)
    logging.info('')
    logging.info('  Please update to the newest version of the Grow SDK.')
    logging.info('  See release notes: {}'.format(url))
    logging.info('  Your version: {}, latest version: {}'.format(
        colorize(yours, ansi=226), colorize(theirs, ansi=82)))
    if utils.is_packaged_app() and auto_update_prompt:
        if raw_input('Auto update now? [y/N]: ').lower() != 'y':
            return
        if subprocess.call(INSTALLER_COMMAND, shell=True) == 0:
            logging.info('Restarting...')
            os.execl(sys.argv[0], *sys.argv)  # Restart on successful install.
        else:
            text = (
                'In-place update failed. Update manually or use:\n'
                '  curl https://install.growsdk.org | bash')
            logging.error(text)
            sys.exit(-1)
    else:
        logging.info('  Update using: ' + colorize('pip install --upgrade grow', ansi=200))
    print ''

def get_popen_args(pod):
    node_modules_path = os.path.join(pod.root, 'node_modules', '.bin')
    env = os.environ.copy()
    env['PATH'] = str(os.environ['PATH'] + os.path.pathsep + node_modules_path)
    args = {
        'cwd': pod.root,
        'env': env,
    }
    if os.name == 'nt':
        args['shell'] = True
    return args

def install(pod, gerrit=None):
    if gerrit or has_gerrit_remote(pod) and gerrit is not False:
        install_gerrit_commit_hook(pod)
    if pod.file_exists('/package.json'):
        if pod.file_exists('/yarn.lock'):
            success = install_yarn(pod)
        else:
            success = install_npm(pod)
        if not success:
          return
    if pod.file_exists('/bower.json'):
        success = install_bower(pod)
        if not success:
          return
    if pod.file_exists('/gulpfile.js'):
        success = install_gulp(pod)

def has_gerrit_remote(pod):
    KNOWN_GERRIT_HOSTS = (
        'googlesource.com',
    )
    repo = utils.get_git_repo(pod.root)
    if repo is None:
        return False
    for remote in repo.remotes:
        url = remote.config_reader.get('url')
        result = urlparse.urlparse(url)
        if result.netloc.endswith(KNOWN_GERRIT_HOSTS):
            return True

def install_gerrit_commit_hook(pod):
    error_message = '[✘] There was an error installing the Gerrit commit hook.'
    args = get_popen_args(pod)
    curl_command = (
        'curl -sLo '
        '`git rev-parse --git-dir`/hooks/commit-msg '
        'https://gerrit-review.googlesource.com/tools/hooks/commit-msg')
    chmod_command = 'chmod +x `git rev-parse --git-dir`/hooks/commit-msg'
    process = subprocess.Popen(curl_command, shell=True, **args)
    code = process.wait()
    if code:
        pod.logger.error(error_message)
        return False
    process = subprocess.Popen(chmod_command, shell=True, **args)
    code = process.wait()
    if code:
        pod.logger.error(error_message)
        return False
    pod.logger.info('[✓] Finished: Installed Gerrit Code Review commit hook.')
    return True

def install_npm(pod):
    args = get_popen_args(pod)
    npm_status_command = 'npm --version > /dev/null 2>&1'
    npm_not_found = subprocess.call(npm_status_command, shell=True, **args) == 127
    if npm_not_found:
        if PLATFORM == 'linux':
            pod.logger.error('[✘] The "npm" command was not found.')
            pod.logger.error('    On Linux, you can install npm using: apt-get install nodejs')
        elif PLATFORM == 'mac':
            pod.logger.error('[✘] The "npm" command was not found.')
            pod.logger.error('    Using brew (https://brew.sh), you can install using: brew install node')
            pod.logger.error('    If you do not have brew, you can download Node.js from https://nodejs.org')
        else:
            pod.logger.error('[✘] The "npm" command was not found.')
            pod.logger.error('    Download Node.js from https://nodejs.org')
        return
    pod.logger.info('[✓] "npm" is installed.')
    npm_command = 'npm install'
    process = subprocess.Popen(npm_command, shell=True, **args)
    code = process.wait()
    if not code:
        pod.logger.info('[✓] Finished: npm install.')
        return True
    pod.logger.error('[✘] There was an error running "npm install".')

def install_bower(pod):
    args = get_popen_args(pod)
    bower_status_command = 'bower --version > /dev/null 2>&1'
    bower_not_found = subprocess.call(bower_status_command, shell=True, **args) == 127
    if bower_not_found:
        pod.logger.error('[✘] The "bower" command was not found.')
        pod.logger.error('    Either add bower to package.json or install globally using: sudo npm install -g bower')
        return
    pod.logger.info('[✓] "bower" is installed.')
    bower_command = 'bower install'
    process = subprocess.Popen(bower_command, shell=True, **args)
    code = process.wait()
    if not code:
        pod.logger.info('[✓] Finished: bower install.')
        return True
    pod.logger.error('[✘] There was an error running "bower install".')

def install_gulp(pod):
    args = get_popen_args(pod)
    gulp_status_command = 'gulp --version > /dev/null 2>&1'
    gulp_not_found = subprocess.call(gulp_status_command, shell=True, **args) == 127
    if gulp_not_found:
        pod.logger.error('[✘] The "gulp" command was not found.')
        pod.logger.error('    Either add gulp to package.json or install globally using: sudo npm install -g gulp')
        return
    pod.logger.info('[✓] "gulp" is installed.')
    return True

def install_yarn(pod):
    args = get_popen_args(pod)
    npm_status_command = 'yarn --version > /dev/null 2>&1'
    npm_not_found = subprocess.call(npm_status_command, shell=True, **args) == 127
    if npm_not_found:
        pod.logger.error('[✘] The "yarn" command was not found.')
        pod.logger.error('    Please install using: npm install -g yarn')
        return
    pod.logger.info('[✓] "yarn" is installed.')
    npm_command = 'yarn install'
    process = subprocess.Popen(npm_command, shell=True, **args)
    code = process.wait()
    if not code:
        pod.logger.info('[✓] Finished: yarn install.')
        return True
    pod.logger.error('[✘] There was an error running "yarn install".')
<commit_msg>Making the error when doing a sdk check more verbose.<commit_after># coding: utf8
from grow.common import config
from grow.common import utils
from xtermcolor import colorize
import json
import logging
import os
import platform
import semantic_version
import subprocess
import sys
import urllib
import urlparse

VERSION = config.VERSION
RELEASES_API = 'https://api.github.com/repos/grow/grow/releases'
INSTALLER_COMMAND = ('/usr/bin/python -c "$(curl -fsSL '
                     'https://raw.github.com/grow/grow/master/install.py)"')

PLATFORM = None
if 'Linux' in platform.system():
  PLATFORM = 'linux'
elif 'Darwin' in platform.system():
  PLATFORM = 'mac'

class Error(Exception):
    pass

class LatestVersionCheckError(Error):
    pass

def get_this_version():
    return VERSION

def get_latest_version():
    try:
        releases = json.loads(urllib.urlopen(RELEASES_API).read())
        if 'message' in releases:
            text = 'Error while downloading release information: {}'.format(
                releases['message'])
            logging.error(colorize(text, ansi=198))
            raise LatestVersionCheckError(str(text))
        for release in releases:
            if release['prerelease']:
                continue
            for each_asset in release['assets']:
                if PLATFORM in each_asset.get('name', '').lower():
                    return release['tag_name']
    except LatestVersionCheckError:
        raise
    except Exception as e:
        logging.error(colorize(str(e), ansi=198))
        text = 'Unable to check for the latest version: {}'.format(str(e))
        logging.error(colorize(text, ansi=198))
        raise LatestVersionCheckError(str(e))

def check_sdk_version(pod):
    sdk_version = get_this_version()
    requires_version = pod.grow_version
    if requires_version is None:
        return
    if (semantic_version.Version(sdk_version)
        not in semantic_version.Spec(requires_version)):
        text = 'ERROR! Pod requires Grow SDK version: {}'.format(requires_version)
        logging.error(colorize(text, ansi=197))
        raise LatestVersionCheckError(str(text))

def check_for_sdk_updates(auto_update_prompt=False):
    try:
        theirs = get_latest_version()
        yours = config.VERSION
    except LatestVersionCheckError:
        return
    if theirs <= yours:
        return
    url = 'https://github.com/grow/grow/releases/tag/{}'.format(theirs)
    logging.info('')
    logging.info('  Please update to the newest version of the Grow SDK.')
    logging.info('  See release notes: {}'.format(url))
    logging.info('  Your version: {}, latest version: {}'.format(
        colorize(yours, ansi=226), colorize(theirs, ansi=82)))
    if utils.is_packaged_app() and auto_update_prompt:
        if raw_input('Auto update now? [y/N]: ').lower() != 'y':
            return
        if subprocess.call(INSTALLER_COMMAND, shell=True) == 0:
            logging.info('Restarting...')
            os.execl(sys.argv[0], *sys.argv)  # Restart on successful install.
        else:
            text = (
                'In-place update failed. Update manually or use:\n'
                '  curl https://install.growsdk.org | bash')
            logging.error(text)
            sys.exit(-1)
    else:
        logging.info('  Update using: ' + colorize('pip install --upgrade grow', ansi=200))
    print ''

def get_popen_args(pod):
    node_modules_path = os.path.join(pod.root, 'node_modules', '.bin')
    env = os.environ.copy()
    env['PATH'] = str(os.environ['PATH'] + os.path.pathsep + node_modules_path)
    args = {
        'cwd': pod.root,
        'env': env,
    }
    if os.name == 'nt':
        args['shell'] = True
    return args

def install(pod, gerrit=None):
    if gerrit or has_gerrit_remote(pod) and gerrit is not False:
        install_gerrit_commit_hook(pod)
    if pod.file_exists('/package.json'):
        if pod.file_exists('/yarn.lock'):
            success = install_yarn(pod)
        else:
            success = install_npm(pod)
        if not success:
          return
    if pod.file_exists('/bower.json'):
        success = install_bower(pod)
        if not success:
          return
    if pod.file_exists('/gulpfile.js'):
        success = install_gulp(pod)

def has_gerrit_remote(pod):
    KNOWN_GERRIT_HOSTS = (
        'googlesource.com',
    )
    repo = utils.get_git_repo(pod.root)
    if repo is None:
        return False
    for remote in repo.remotes:
        url = remote.config_reader.get('url')
        result = urlparse.urlparse(url)
        if result.netloc.endswith(KNOWN_GERRIT_HOSTS):
            return True

def install_gerrit_commit_hook(pod):
    error_message = '[✘] There was an error installing the Gerrit commit hook.'
    args = get_popen_args(pod)
    curl_command = (
        'curl -sLo '
        '`git rev-parse --git-dir`/hooks/commit-msg '
        'https://gerrit-review.googlesource.com/tools/hooks/commit-msg')
    chmod_command = 'chmod +x `git rev-parse --git-dir`/hooks/commit-msg'
    process = subprocess.Popen(curl_command, shell=True, **args)
    code = process.wait()
    if code:
        pod.logger.error(error_message)
        return False
    process = subprocess.Popen(chmod_command, shell=True, **args)
    code = process.wait()
    if code:
        pod.logger.error(error_message)
        return False
    pod.logger.info('[✓] Finished: Installed Gerrit Code Review commit hook.')
    return True

def install_npm(pod):
    args = get_popen_args(pod)
    npm_status_command = 'npm --version > /dev/null 2>&1'
    npm_not_found = subprocess.call(npm_status_command, shell=True, **args) == 127
    if npm_not_found:
        if PLATFORM == 'linux':
            pod.logger.error('[✘] The "npm" command was not found.')
            pod.logger.error('    On Linux, you can install npm using: apt-get install nodejs')
        elif PLATFORM == 'mac':
            pod.logger.error('[✘] The "npm" command was not found.')
            pod.logger.error('    Using brew (https://brew.sh), you can install using: brew install node')
            pod.logger.error('    If you do not have brew, you can download Node.js from https://nodejs.org')
        else:
            pod.logger.error('[✘] The "npm" command was not found.')
            pod.logger.error('    Download Node.js from https://nodejs.org')
        return
    pod.logger.info('[✓] "npm" is installed.')
    npm_command = 'npm install'
    process = subprocess.Popen(npm_command, shell=True, **args)
    code = process.wait()
    if not code:
        pod.logger.info('[✓] Finished: npm install.')
        return True
    pod.logger.error('[✘] There was an error running "npm install".')

def install_bower(pod):
    args = get_popen_args(pod)
    bower_status_command = 'bower --version > /dev/null 2>&1'
    bower_not_found = subprocess.call(bower_status_command, shell=True, **args) == 127
    if bower_not_found:
        pod.logger.error('[✘] The "bower" command was not found.')
        pod.logger.error('    Either add bower to package.json or install globally using: sudo npm install -g bower')
        return
    pod.logger.info('[✓] "bower" is installed.')
    bower_command = 'bower install'
    process = subprocess.Popen(bower_command, shell=True, **args)
    code = process.wait()
    if not code:
        pod.logger.info('[✓] Finished: bower install.')
        return True
    pod.logger.error('[✘] There was an error running "bower install".')

def install_gulp(pod):
    args = get_popen_args(pod)
    gulp_status_command = 'gulp --version > /dev/null 2>&1'
    gulp_not_found = subprocess.call(gulp_status_command, shell=True, **args) == 127
    if gulp_not_found:
        pod.logger.error('[✘] The "gulp" command was not found.')
        pod.logger.error('    Either add gulp to package.json or install globally using: sudo npm install -g gulp')
        return
    pod.logger.info('[✓] "gulp" is installed.')
    return True

def install_yarn(pod):
    args = get_popen_args(pod)
    npm_status_command = 'yarn --version > /dev/null 2>&1'
    npm_not_found = subprocess.call(npm_status_command, shell=True, **args) == 127
    if npm_not_found:
        pod.logger.error('[✘] The "yarn" command was not found.')
        pod.logger.error('    Please install using: npm install -g yarn')
        return
    pod.logger.info('[✓] "yarn" is installed.')
    npm_command = 'yarn install'
    process = subprocess.Popen(npm_command, shell=True, **args)
    code = process.wait()
    if not code:
        pod.logger.info('[✓] Finished: yarn install.')
        return True
    pod.logger.error('[✘] There was an error running "yarn install".')
<|endoftext|>


<commit_before>import os
from urlparse import urljoin

import json
import hashlib
import hmac

from flask import Flask, request
import requests

app = Flask(__name__)
app.config['GITHUB_WEBHOOK_SECRET'] = os.environ.get('GITHUB_WEBHOOK_SECRET')
app.config['GITHUB_ACCESS_TOKEN'] = os.environ.get('GITHUB_ACCESS_TOKEN')

github = requests.Session()
github.headers.update({
    'Content-Type': 'application/json',
    'Authorization': 'token {}'.format(app.config['GITHUB_ACCESS_TOKEN'])
})

@app.route('/webhook/', methods=['GET', 'POST'])
def webhook():
    if request.method == 'POST':
        event = request.headers['X-GitHub-Event']
        signature = request.headers['X-Hub-Signature']

        mac = hmac.new(app.config['GITHUB_WEBHOOK_SECRET'], msg=request.data, digestmod=hashlib.sha1)
        if not 'sha1={}'.format(mac.hexdigest()) == signature:
            return 'Invalid signature', 403

        if event == 'ping':
            return json.dumps({'msg': 'hi'})
        if event == 'push':
            pending()

        return '', 201

@app.route('/repos/<user>/<repo>/statuses/<sha>', methods=['POST'])
def status(user, repo, sha):
    result = request.data

    if result:
        status = {
            'state': 'failure',
            'description': 'Linting failed',
            'context': 'linting'
        }
    else:
        status = {
            'state': 'success',
            'description': 'No style issues found',
            'context': 'linting'
        }

    response = requests.post(
        urljoin('https://api.github.com/', request.path),
        json.dumps(status)
    )

    response.raise_for_status()

def pending():
    payload = request.json
    ref = payload['head_commit']['id']

    status = {
        'state': 'pending',
        'description': 'Waiting for linting result',
        'context': 'linting'
    }
    response = github.post(
        payload['repository']['statuses_url'].format(sha=ref),
        json.dumps(status)
    )

    response.raise_for_status()
<commit_msg>Fix url for status<commit_after>import os
from urlparse import urljoin

import json
import hashlib
import hmac

from flask import Flask, request
import requests

app = Flask(__name__)
app.config['GITHUB_WEBHOOK_SECRET'] = os.environ.get('GITHUB_WEBHOOK_SECRET')
app.config['GITHUB_ACCESS_TOKEN'] = os.environ.get('GITHUB_ACCESS_TOKEN')

github = requests.Session()
github.headers.update({
    'Content-Type': 'application/json',
    'Authorization': 'token {}'.format(app.config['GITHUB_ACCESS_TOKEN'])
})

@app.route('/webhook/', methods=['GET', 'POST'])
def webhook():
    if request.method == 'POST':
        event = request.headers['X-GitHub-Event']
        signature = request.headers['X-Hub-Signature']

        mac = hmac.new(app.config['GITHUB_WEBHOOK_SECRET'], msg=request.data, digestmod=hashlib.sha1)
        if not 'sha1={}'.format(mac.hexdigest()) == signature:
            return 'Invalid signature', 403

        if event == 'ping':
            return json.dumps({'msg': 'hi'})
        if event == 'push':
            pending()

        return '', 201

@app.route('/repos/<user>/<repo>/statuses/<sha>', methods=['POST'])
def status(user, repo, sha):
    result = request.data

    if result:
        status = {
            'state': 'failure',
            'description': 'Linting failed',
            'context': 'linting'
        }
    else:
        status = {
            'state': 'success',
            'description': 'No style issues found',
            'context': 'linting'
        }

    response = requests.post(
        'https://api.github.com/repos/{user}/{repo}/statuses/{sha}'.format(
            user=user, repo=repo, sha=sha
        ),
        json.dumps(status)
    )

    response.raise_for_status()

def pending():
    payload = request.json
    ref = payload['head_commit']['id']

    status = {
        'state': 'pending',
        'description': 'Waiting for linting result',
        'context': 'linting'
    }
    response = github.post(
        payload['repository']['statuses_url'].format(sha=ref),
        json.dumps(status)
    )

    response.raise_for_status()
<|endoftext|>


<commit_before>import tweepy
import json
import pickle

class lirr():

	def __init__():
		# Load list of stops pre-ordered east -> west
		with open('stops.p') as f:
			self.stops = pickle.load(f)

		with open('access.json') as f:
			secrets = json.load(f)

		auth = tweepy.OAuthHandler(secrets['consumer_key'], secrets['consumer_secret_key'])
		auth.set_access_token(secrets['access_token'], secrets['secret_access_token'])
		self.api = tweepy.API(auth)
		self.tweets = [tweet.text for tweet in self.api.user_timeline(screen_name='lirr', count=60)]
<commit_msg>Added logic for calculating delay time, but not using cancels yet<commit_after>import tweepy
import json
import pickle
import re

class lirr():

	def __init__():
		self.tweets = []
		self.delays = []
		self.cancels = []
		self.total_delay_times = {}
		# Load list of stops pre-ordered east -> west
		with open('stops.p') as f:
			self.stops = pickle.load(f)

		# Load auth
		with open('access.json') as f:
			secrets = json.load(f)

		# Load stop:branch map
		with open('stops.json') as f:
			self.station_map = json.load(f)

		self.delay_regex = re.compile('^.* is operating \d* .* late .*$')
		self.canceled_regex = re.compile('^.*canceled.*$')

		# Authenticate with Twitter
		auth = tweepy.OAuthHandler(secrets['consumer_key'], secrets['consumer_secret_key'])
		auth.set_access_token(secrets['access_token'], secrets['secret_access_token'])
		self.api = tweepy.API(auth)

	def load_tweets():
		# Load 2 pages of tweets
		self.tweets = [tweet.text for tweet in self.api.user_timeline(screen_name='lirr', count=300, page=1)+
					   self.api.user_timeline(screen_name='lirr', count=300, page=2)]

	def get_relevant_tweets():
		for tweet in self.tweets:
			if self.delay_regex.match(tweet):
				self.delays.append(tweet)
			elif self.canceled_regex.match(tweet):
				self.cancels.append(tweet)

	def process_delay_times():
		line = ''
		for delay in self.delays:
			#get branch
			for station in station_map:
				if station in delay:
					line = station_map[station]
			#remove start/end times by starting at the 'is operating' substring
			delay = delay[delay.find('operating'):]
			delay_time = int(re.search(r'\d+',delay).group(0))
			if line in self.total_delay_times:
				total_delay_times[line] = total_delay_times[line] + delay_time
			else
				total_delay_times[line] = delay_time

	def return_delays():
		self.load_tweets()
		self.get_relevant_tweets()
		self.process_delay_times()
		return self.total_delay_times

<|endoftext|>


<commit_before>import nibabel as nib
import numpy as np

def load_nifti(filename, with_affine=False):
    """
    load image from NIFTI file
    Parameters
    ----------
    filename : str
        filename of NIFTI file
    with_affine : bool
        if True, returns affine parameters

    Returns
    -------
    data : np.ndarray
        image data
    """
    img = nib.load(filename)
    data = img.get_data()
    data = np.copy(data, order="C")
    if with_affine:
        return data, img.affine
    return data

def sample(df, n, shape):
    """
    randomly sample patch images from DataFrame

    Parameters
    ----------
    df : pd.DataFrame
        DataFrame containing name of image files
    n : int
        number of patches to extract
    shape : list
        shape of patches to extract

    Returns
    -------
    images : (n, n_channels, shape[0], shape[1], ...) ndarray
        input patches
    labels : (n, shape[0], shape[1], ...) ndarray
        label patches
    """
    N = len(df)
    assert N >= n, "n should be smaller than or equal to " + str(N)
    indices = np.random.choice(N, n, replace=False)
    image_files = df["image"][indices]
    label_files = df["label"][indices]
    images = []
    labels = []
    for image_file, label_file in zip(image_files, label_files):
        image = load_nifti(image_file)
        label = load_nifti(label_file).astype(np.int32)
        mask = np.float32(label > 0)
        slices = [slice(len_ / 2, -len_ / 2) for len_ in shape]
        mask[slices] *= 2
        indices = np.where(mask > 1.5)
        i = np.random.choice(len(indices[0]))
        slices = [
            slice(index[i] - len_ / 2, index[i] + len_ / 2)
            for index, len_ in zip(indices, shape)]
        image_patch = image[slices]
        label_patch = label[slices]
        image_patch = image_patch.transpose(3, 0, 1, 2)
        images.append(image_patch)
        labels.append(label_patch)
    images = np.array(images)
    labels = np.array(labels)
    return images, labels
<commit_msg>replaced float with int to create mask<commit_after>import nibabel as nib
import numpy as np

def load_nifti(filename, with_affine=False):
    """
    load image from NIFTI file
    Parameters
    ----------
    filename : str
        filename of NIFTI file
    with_affine : bool
        if True, returns affine parameters

    Returns
    -------
    data : np.ndarray
        image data
    """
    img = nib.load(filename)
    data = img.get_data()
    data = np.copy(data, order="C")
    if with_affine:
        return data, img.affine
    return data

def sample(df, n, shape):
    """
    randomly sample patch images from DataFrame

    Parameters
    ----------
    df : pd.DataFrame
        DataFrame containing name of image files
    n : int
        number of patches to extract
    shape : list
        shape of patches to extract

    Returns
    -------
    images : (n, n_channels, shape[0], shape[1], ...) ndarray
        input patches
    labels : (n, shape[0], shape[1], ...) ndarray
        label patches
    """
    N = len(df)
    assert N >= n, "n should be smaller than or equal to " + str(N)
    indices = np.random.choice(N, n, replace=False)
    image_files = df["image"][indices]
    label_files = df["label"][indices]
    images = []
    labels = []
    for image_file, label_file in zip(image_files, label_files):
        image = load_nifti(image_file)
        label = load_nifti(label_file).astype(np.int32)
        mask = np.int32(label > 0)
        slices = [slice(len_ / 2, -len_ / 2) for len_ in shape]
        mask[slices] *= 2
        indices = np.where(mask > 1.5)
        i = np.random.choice(len(indices[0]))
        slices = [
            slice(index[i] - len_ / 2, index[i] + len_ / 2)
            for index, len_ in zip(indices, shape)]
        image_patch = image[slices]
        label_patch = label[slices]
        image_patch = image_patch.transpose(3, 0, 1, 2)
        images.append(image_patch)
        labels.append(label_patch)
    images = np.array(images)
    labels = np.array(labels)
    return images, labels
<|endoftext|>


<commit_before>from glob import glob
import datetime as dt
from collections import Counter
import numpy as np
import netCDF4 as nc

class Stormtrack(object):
    def __init__(self, region, year, name):
	self.region  = region
	self.year  = year
	self.name  = name

class IbStormtrack(object):
    def __init__(self, year, name, ds):
	self.year  = year
	self.name  = name
	self.ds = ds

def load_stormtracks_data():
    regions = ['atlantic', 'e_pacific', 'w_pacific', 's_pacific', 's_indian', 'n_indian']
    start_year, end_year = 2000, 2013
    stormtracks = []
    cats = Counter()
    for region in regions:
	for year in range(start_year, end_year):
	    print(region, year)
	    y = str(year)
	    filenames = glob('data/stormtracks/%s/%s/*'%(region, y))

	    for fn in filenames:
		try:
		    s = load_stromtrack_data(region, year, fn)
		    stormtracks.append(s)
		    for cat in s.categories:
			cats[cat] += 1
		except Exception, e:
		    print('Could not load data for %s'%fn)
		    print(e.message)

    return stormtracks, cats

def load_ibtracks_year(year):
    y = str(year)

    filenames = glob('data/ibtracs/%s*.nc'%(y))
    return load_ibtracks_filenames(year, filenames)

def load_ibtracks_filenames(year, filenames):
    basins = Counter()
    stormtracks = []
    for fn in filenames:
	try:
	    s = load_ibtracks_data(year, fn)
	    if s.basin == 'NA':
		stormtracks.append(s)
	    basins[s.basin] += 1
	except Exception, e:
	    print('Could not load data for %s'%fn)
	    print(e.message)
    return stormtracks, basins

def ibs(array):
    return ''.join(array)

def load_ibtracks_data(year, fn):
    print(fn.split('/')[-1])
    dataset = nc.Dataset(fn)
    s = IbStormtrack(year, fn.split('/')[-1].split('.')[0], dataset)
    s.basin = ibs(dataset.variables['genesis_basin'])

    dates = []
    cats  = []
    lons  = []
    lats  = []
    for i in range(dataset.variables['nobs'][0]):
	date = dt.datetime.strptime(ibs(dataset.variables['isotime'][i]), '%Y-%m-%d %H:%M:%S')
	dates.append(date)

    s.lon = dataset.variables['lon_for_mapping'][:]
    s.lat = dataset.variables['lat_for_mapping'][:]
    if s.basin == 'NA':
	s.cls = []
	s.is_hurricane = False
	for i in range(dataset.variables['nobs'][0]):
	    cls = ibs(dataset.variables['atcf_class'][i])
	    if cls == 'HU':
		s.is_hurricane = True
	    s.cls.append(cls)

    s.lon_keys = [k for k in dataset.variables.keys() if k.find('lon') != -1]

    s.dates = np.array(dates)
    return s

def load_wilma_katrina():
    wilma_fn = 'data/ibtracs/2005289N18282.ibtracs.v03r05.nc'
    katrina_fn = 'data/ibtracs/2005236N23285.ibtracs.v03r05.nc'
    return load_ibtracks_filenames(2005, [wilma_fn, katrina_fn])

def load_ibtracks_stormtrack_data(fn):
    pass

def load_stromtrack_data(region, year, fn):
    #print(fn)
    with open(fn, 'r') as f:
	lines = f.readlines()

    track = []
    date_strs = []
    dates = []
    categories = []
    winds = []
    pressures = []

    if lines[0] == 'SKIP\n':
	raise Exception('skipping due to first line of file')

    for line in lines[3:]:
	split_line = line.split()
	lat = float(split_line[1])
	lon = float(split_line[2])
	date_str = split_line[3]
        date = dt.datetime.strptime('%s/%s'%(str(year), date_str), '%Y/%m/%d/%HZ')
	#wind = float(split_line[4])
	wind = split_line[4]
	#pressure = float(split_line[5])
	pressure = split_line[5]
	category = "_".join(split_line[6:])

	track.append((lon, lat))
	date_strs.append(date_str)
	dates.append(date)
	categories.append(category)
	winds.append(wind)
	pressures.append(pressure)

    s = Stormtrack(region, year, fn.split('/')[-1].split('.')[0])
    s.track = np.array(track)
    s.dates = np.array(dates)
    s.categories = np.array(categories)
    s.winds = np.array(winds)
    s.pressures = np.array(pressures)
    return s

if __name__ == '__main__':
    load_stormtracks_data()
<commit_msg>Get load.py working again (perhaps there was a change in netCDF4?)<commit_after>from glob import glob
import datetime as dt
from collections import Counter
import numpy as np
import netCDF4 as nc

class Stormtrack(object):
    def __init__(self, region, year, name):
	self.region  = region
	self.year  = year
	self.name  = name

class IbStormtrack(object):
    def __init__(self, year, name, ds):
	self.year  = year
	self.name  = name
	self.ds = ds

def load_stormtracks_data():
    regions = ['atlantic', 'e_pacific', 'w_pacific', 's_pacific', 's_indian', 'n_indian']
    start_year, end_year = 2000, 2013
    stormtracks = []
    cats = Counter()
    for region in regions:
	for year in range(start_year, end_year):
	    print(region, year)
	    y = str(year)
	    filenames = glob('data/stormtracks/%s/%s/*'%(region, y))

	    for fn in filenames:
		try:
		    s = load_stromtrack_data(region, year, fn)
		    stormtracks.append(s)
		    for cat in s.categories:
			cats[cat] += 1
		except Exception, e:
		    print('Could not load data for %s'%fn)
		    print(e.message)

    return stormtracks, cats

def load_ibtracks_year(year):
    y = str(year)

    filenames = glob('data/ibtracs/%s*.nc'%(y))
    return load_ibtracks_filenames(year, filenames)

def load_ibtracks_filenames(year, filenames):
    basins = Counter()
    stormtracks = []
    for fn in filenames:
	try:
	    s = load_ibtracks_data(year, fn)
	    if s.basin == 'NA':
		stormtracks.append(s)
	    basins[s.basin] += 1
	except Exception, e:
	    print('Could not load data for %s'%fn)
	    print(e.message)
    return stormtracks, basins

def ibs(array):
    return ''.join(array)

def load_ibtracks_data(year, fn):
    print(fn.split('/')[-1])
    dataset = nc.Dataset(fn)
    s = IbStormtrack(year, fn.split('/')[-1].split('.')[0], dataset)
    s.basin = ibs(dataset.variables['genesis_basin'])

    dates = []
    cats  = []
    lons  = []
    lats  = []
    for i in range(dataset.variables['nobs'].getValue()):
	date = dt.datetime.strptime(ibs(dataset.variables['isotime'][i]), '%Y-%m-%d %H:%M:%S')
	dates.append(date)

    s.lon = dataset.variables['lon_for_mapping'][:]
    s.lat = dataset.variables['lat_for_mapping'][:]
    if s.basin == 'NA':
	s.cls = []
	s.is_hurricane = False
	for i in range(dataset.variables['nobs'].getValue()):
	    cls = ibs(dataset.variables['atcf_class'][i])
	    if cls == 'HU':
		s.is_hurricane = True
	    s.cls.append(cls)

    s.lon_keys = [k for k in dataset.variables.keys() if k.find('lon') != -1]

    s.dates = np.array(dates)
    return s

def load_wilma_katrina():
    wilma_fn = 'data/ibtracs/2005289N18282.ibtracs.v03r05.nc'
    katrina_fn = 'data/ibtracs/2005236N23285.ibtracs.v03r05.nc'
    return load_ibtracks_filenames(2005, [wilma_fn, katrina_fn])

def load_ibtracks_stormtrack_data(fn):
    pass

def load_stromtrack_data(region, year, fn):
    #print(fn)
    with open(fn, 'r') as f:
	lines = f.readlines()

    track = []
    date_strs = []
    dates = []
    categories = []
    winds = []
    pressures = []

    if lines[0] == 'SKIP\n':
	raise Exception('skipping due to first line of file')

    for line in lines[3:]:
	split_line = line.split()
	lat = float(split_line[1])
	lon = float(split_line[2])
	date_str = split_line[3]
        date = dt.datetime.strptime('%s/%s'%(str(year), date_str), '%Y/%m/%d/%HZ')
	#wind = float(split_line[4])
	wind = split_line[4]
	#pressure = float(split_line[5])
	pressure = split_line[5]
	category = "_".join(split_line[6:])

	track.append((lon, lat))
	date_strs.append(date_str)
	dates.append(date)
	categories.append(category)
	winds.append(wind)
	pressures.append(pressure)

    s = Stormtrack(region, year, fn.split('/')[-1].split('.')[0])
    s.track = np.array(track)
    s.dates = np.array(dates)
    s.categories = np.array(categories)
    s.winds = np.array(winds)
    s.pressures = np.array(pressures)
    return s

if __name__ == '__main__':
    load_stormtracks_data()
<|endoftext|>


<commit_before>
from cppbuiltins import *
from libeeyore.builtins import *
from libeeyore.functionvalues import *
from libeeyore.languagevalues import *
from libeeyore.values import *
from libeeyore.builtinmodules.eeysys import EeySysArgv

def render_EeySymbol( env, value ):
    return value.name()

def render_EeyInt( env, value ):
    return str( value.value )

def render_EeyBool( env, value ):
    if value.value:
        return "true"
    else:
        return "false"

def render_EeyString( env, value ):
    return '"%s"' % value.value

def render_EeyPlus( env, value ):
    # TODO: assert they are addable and switch on how to add them
    return "(%s + %s)" % (
        value.left_value.render( env ), value.right_value.render( env ) )

def render_EeyGreaterThan( env, value ):
    # TODO: assert they are comparable
    return "(%s > %s)" % (
        value.left_value.render( env ), value.right_value.render( env ) )

def render_EeyIf( env, value ):
    # TODO: assert predicate is a bool or function returning one
    return "if( %s )\n    {\n        %s;\n    }" % (
        value.predicate.render( env ),
        "\n        ".join( c.render(env) for c in value.cmds_if_true )
        )

def render_EeyFunction( env, value ):
    raise Exception( "Don't know how to render a function yet" )

def render_EeyPrint( env, value ):
    return render_EeyFunction( env, value )

def render_EeyDef( env, value ):
    return ""

def render_EeyPass( env, value ):
    return ""

def render_EeyImport( env, value ):
    return ""

def render_EeyInit( env, value ):
    if value.is_known( env ):
        return ""
    else:
        return "%s %s = %s" % (
                value.var_type.render( env ),
                value.var_name.symbol_name,
                value.init_value.render( env ),
            )

type2string = {
    EeyBool : "bool",
    EeyInt  : "int",
    EeyVoid : "void",
    }

def render_EeyType( env, value ):
    return type2string[value.value]

def render_EeyNoneType( env, value ):
    return ""

def render_type_and_name( env, typename ):
    return "%s %s" % ( render_EeyType( env, typename[0].evaluate( env ) ),
        typename[1].symbol_name )

def render_EeyUserFunction_body( env, func_call ):
    fn = func_call.user_function.evaluate( env )

    assert( fn.__class__ == EeyUserFunction ) # TODO: handle other types

    ret = fn.ret_type.render( env )
    ret += " "
    ret += fn.name
    ret += "( "
    ret += ", ".join( render_type_and_name( env, typename ) for typename
        in fn.arg_types_and_names )
    ret += " )\n{\n"

    newenv = fn.execution_environment( env, func_call.args, False )

    # TODO: not every statement should be a return
    for body_stmt in fn.body_stmts:
        st = body_stmt.evaluate( newenv )
        if st.__class__ == EeyPass:
            continue
        ret += "    return %s;\n" % st.render( newenv )

    ret += "}\n"

    return ret

def render_EeyRuntimeUserFunction( env, value ):
    env.renderer.functions.append( render_EeyUserFunction_body(
        env, value ) )

    return ( value.user_function.name + "( " +
        ", ".join( arg.render( env ) for arg in value.args )
        + " )" )

def render_EeyFunctionCall( env, value ):

    fn = value.func.evaluate( env )

    # TODO: assert fn is callable

    return fn.call( env, value.args ).render( env )

def render_EeyReturn( env, value ):
    return "return " + value.value.render( env )

def render_EeyArrayLookup( env, value ):
    # TODO: handle large numbers
    return value.array_value.render( env ) + "[%s]" % value.index.value

def render_EeySysArgv( env, value ):
    # TODO: set up a global variable called global_argv and initialise
    #       it at the beginning of main
    return "argv"

type2renderer = {
    EeyArrayLookup          : render_EeyArrayLookup,
    EeyBool                 : render_EeyBool,
    EeyDef                  : render_EeyDef,
    EeyFunction             : render_EeyFunction,
    EeyFunctionCall         : render_EeyFunctionCall,
    EeyGreaterThan          : render_EeyGreaterThan,
    EeyIf                   : render_EeyIf,
    EeyImport               : render_EeyImport,
    EeyInit                 : render_EeyInit,
    EeyInt                  : render_EeyInt,
    EeyNoneType             : render_EeyNoneType,
    EeyPass                 : render_EeyPass,
    EeyPlus                 : render_EeyPlus,
    EeyPrint                : render_EeyPrint,
    EeyReturn               : render_EeyReturn,
    EeyRuntimeLen           : render_EeyRuntimeLen,
    EeyRuntimePrint         : render_EeyRuntimePrint,
    EeyRuntimeUserFunction  : render_EeyRuntimeUserFunction,
    EeyString               : render_EeyString,
    EeySymbol               : render_EeySymbol,
    EeySysArgv              : render_EeySysArgv,
    EeyType                 : render_EeyType,
    }

<commit_msg>Warn if a type is unknown at compile time.<commit_after>
from cppbuiltins import *
from libeeyore.builtins import *
from libeeyore.functionvalues import *
from libeeyore.languagevalues import *
from libeeyore.values import *
from libeeyore.builtinmodules.eeysys import EeySysArgv

def render_EeySymbol( env, value ):
    return value.name()

def render_EeyInt( env, value ):
    return str( value.value )

def render_EeyBool( env, value ):
    if value.value:
        return "true"
    else:
        return "false"

def render_EeyString( env, value ):
    return '"%s"' % value.value

def render_EeyPlus( env, value ):
    # TODO: assert they are addable and switch on how to add them
    return "(%s + %s)" % (
        value.left_value.render( env ), value.right_value.render( env ) )

def render_EeyGreaterThan( env, value ):
    # TODO: assert they are comparable
    return "(%s > %s)" % (
        value.left_value.render( env ), value.right_value.render( env ) )

def render_EeyIf( env, value ):
    # TODO: assert predicate is a bool or function returning one
    return "if( %s )\n    {\n        %s;\n    }" % (
        value.predicate.render( env ),
        "\n        ".join( c.render(env) for c in value.cmds_if_true )
        )

def render_EeyFunction( env, value ):
    raise Exception( "Don't know how to render a function yet" )

def render_EeyPrint( env, value ):
    return render_EeyFunction( env, value )

def render_EeyDef( env, value ):
    return ""

def render_EeyPass( env, value ):
    return ""

def render_EeyImport( env, value ):
    return ""

def render_EeyInit( env, value ):
    if value.is_known( env ):
        return ""
    else:
        return "%s %s = %s" % (
                value.var_type.render( env ),
                value.var_name.symbol_name,
                value.init_value.render( env ),
            )

type2string = {
    EeyBool : "bool",
    EeyInt  : "int",
    EeyVoid : "void",
    }

def render_EeyType( env, value ):
    if not value.is_known( env ):
        raise EeyUserErrorException( v + " should be known!  "
            + "Eeyore can't (currently) support types that are unknown at "
            + "compile time." )
        # TODO: ensure error message properly displays the unknown thing
    return type2string[value.value]

def render_EeyNoneType( env, value ):
    return ""

def render_type_and_name( env, typename ):
    return "%s %s" % ( render_EeyType( env, typename[0].evaluate( env ) ),
        typename[1].symbol_name )

def render_EeyUserFunction_body( env, func_call ):
    fn = func_call.user_function.evaluate( env )

    assert( fn.__class__ == EeyUserFunction ) # TODO: handle other types

    ret = fn.ret_type.render( env )
    ret += " "
    ret += fn.name
    ret += "( "
    ret += ", ".join( render_type_and_name( env, typename ) for typename
        in fn.arg_types_and_names )
    ret += " )\n{\n"

    newenv = fn.execution_environment( env, func_call.args, False )

    # TODO: not every statement should be a return
    for body_stmt in fn.body_stmts:
        st = body_stmt.evaluate( newenv )
        if st.__class__ == EeyPass:
            continue
        ret += "    return %s;\n" % st.render( newenv )

    ret += "}\n"

    return ret

def render_EeyRuntimeUserFunction( env, value ):
    env.renderer.functions.append( render_EeyUserFunction_body(
        env, value ) )

    return ( value.user_function.name + "( " +
        ", ".join( arg.render( env ) for arg in value.args )
        + " )" )

def render_EeyFunctionCall( env, value ):

    fn = value.func.evaluate( env )

    # TODO: assert fn is callable

    return fn.call( env, value.args ).render( env )

def render_EeyReturn( env, value ):
    return "return " + value.value.render( env )

def render_EeyArrayLookup( env, value ):
    # TODO: handle large numbers
    return value.array_value.render( env ) + "[%s]" % value.index.value

def render_EeySysArgv( env, value ):
    # TODO: set up a global variable called global_argv and initialise
    #       it at the beginning of main
    return "argv"

type2renderer = {
    EeyArrayLookup          : render_EeyArrayLookup,
    EeyBool                 : render_EeyBool,
    EeyDef                  : render_EeyDef,
    EeyFunction             : render_EeyFunction,
    EeyFunctionCall         : render_EeyFunctionCall,
    EeyGreaterThan          : render_EeyGreaterThan,
    EeyIf                   : render_EeyIf,
    EeyImport               : render_EeyImport,
    EeyInit                 : render_EeyInit,
    EeyInt                  : render_EeyInt,
    EeyNoneType             : render_EeyNoneType,
    EeyPass                 : render_EeyPass,
    EeyPlus                 : render_EeyPlus,
    EeyPrint                : render_EeyPrint,
    EeyReturn               : render_EeyReturn,
    EeyRuntimeLen           : render_EeyRuntimeLen,
    EeyRuntimePrint         : render_EeyRuntimePrint,
    EeyRuntimeUserFunction  : render_EeyRuntimeUserFunction,
    EeyString               : render_EeyString,
    EeySymbol               : render_EeySymbol,
    EeySysArgv              : render_EeySysArgv,
    EeyType                 : render_EeyType,
    }

<|endoftext|>


<commit_before>#!/usr/bin/python
import time
import sys.stdin as ladderlog
import sys.stdout as armagetron

def addHandler(command, handler):
	if not command in commands:
		commands[command] = []
	commands[command].append(handler)

def removeHandler(command, handler):
	if command in commands and handler in commands[command]:
		commands[command].remove(handler)

def setChatHandler(command, handler):
	chatcommands[command] = handler

def removeChatHandler(command):
	if command in chatcommands:
		del chatcommands[command]

def sendCommand(command):
	armagetron.write(command + '\n')

def say(message):
	sendCommand('SAY ' + message)

def consoleMessage(message):
	sendCommand('CONSOLE_MESSAGE "' + message + '"')

def centerMessage(message):
	sendCommand('CENTER_MESSAGE "' + message + '"')

def sendMessage(player, message):
	if type(player) == Player:
		sendCommand('PLAYER_MESSAGE ' + player.name + ' "' + message + '"')
	else:
		sendCommand('PLAYER_MESSAGE ' + player + ' "' + message + '"')

def pauseRound():
	sendCommand('WAIT_FOR_EXTERNAL_SCRIPT 1')

def continueRound():
	sendCommand('WAIT_FOR_EXTERNAL_SCRIPT 0')

def setRepository(address):
	sendCommand('RESOURCE_REPOSITORY_SERVER ' + address)

def setMap(resource):
	sendCommand('MAP_FILE ' + resource)

def include(config):
	sendCommand('INCLUDE ' + config)

def rinclude(config):
	sendCommand('RINCLUDE ' + config)

def reload():
	sendCommand('INCLUDE settings.cfg')
	sendCommand('INCLUDE server_info.cfg')
	sendCommand('INCLUDE settings_custom.cfg')
	sendCommand('INCLUDE script.cfg')
	sendCommand('START_NEW_MATCH')

def endRound():
	sendCommand('WIN_ZONE_MIN_LAST_DEATH 0')
	sendCommand('WIN_ZONE_MIN_ROUND_TIME 0')

def chatCommand(command):
	if command[1] in chatcommands:
		chatcommands[command[1]](command[1:])
	else:
		sendMessage(command[2], 'Unknown chat command "' + command[1] + '".')

def init(command):
	sendCommand('INCLUDE script.cfg')

def run():
	while True:
		line = ladderlog.readline()
		if not line:
			time.sleep(0.1)
			continue
		if line.startswith('QUIT'):
			break
		command = line.split()
		if command and command[0] in commands:
			for handler in commands[command[0]]:
				handler(command)

#Grid stuff

class Team:
	def __init__(self, name):
		self.name = name
		self.score = 0
		self.players = {}
		self.positions = []

class Player:
	def __init__(self, name, screenname, ip):
		self.name = name
		self.screenname = screenname
		self.ip = ip
		self.score = 0
		self.alive = False

	def sendMessage(self, message):
		sendMessage(self, message)

	def kill(self):
		sendCommand('KILL ' + self.name)

	def kick(self, reason=None):
		command = [ 'KICK', self.name ]
		if reason:
			command.append(reason)

		sendCommand(' '.join(command))

	def ban(self, time=None, reason=None):
		command = [ 'BAN', self.name ]
		if time:
			command.append(time)
		if reason:
			command.append(reason)

		sendCommand(' '.join(command))

	def banIP(self, time, reason=None):
		command = [ 'BAN_IP', self.ip ]
		if time:
			command.append(time)
			if reason:
				command.append(reason)

		sendCommand(' '.join(command))

	def declareWinner(self):
		sendCommand('DECLARE_ROUND_WINNER ' + self.name)

	def teleport(self, x, y, xdir, ydir):
		sendCommand('TELEPORT_PLAYER ' + self.name + ' ' + x + ' ' + y + ' ' + xdir + ' ' + ydir)

	def respawn(self, x, y, xdir, ydir):
		if not self.alive:
			sendCommand('RESPAWN_PLAYER ' + self.name + ' 1 ' + x + ' ' + y + ' ' + xdir + ' ' + ydir)

class Zone:
	def __init__(self, name, type, x, y, size, growth=0, xdir=0, ydir=0, interactive=None, r=None, g=None, b=None, target_size=None, rubber=None, player=None, owner=None, command=None):
		self.name = name
		self.type = type
		self.x = x
		self.y = y
		self.size = size
		self.growth = growth
		self.xdir = xdir
		self.ydir = ydir

		if interactive:
			self.interactive = interactive

		if r and g and b:
			self.r = r
			self.g = g
			self.b = b

		if target_size:
			self.target_size = target_size

		if type == 'rubber':
			self.rubber = rubber

		if type == 'zombie':
			self.player = player

		if type == 'zombieOwner':
			self.player = player
			self.owner = owner

		if type == 'target' and command != None:
			self.setCommand(command)

	def __del__(self):
		self.changeSize(0)

	def changeColor(self, r, g, b):
		self.r = r
		self.g = g
		self.b = b
		sendCommand('SET_ZONE_COLOR ' + self.name + ' ' + r + ' ' + g + ' ' + b)

	def changeExpansion(self, growth):
		self.growth = growth
		sendCommand('SET_ZONE_EXPANSION ' + self.name + ' ' + growth)

	def changePosition(self, x, y):
		self.x = x
		self.y = y
		sendCommand('SET_ZONE_POSITION ' + self.name + ' ' + x + ' ' + y)

	def changeSize(self, size, growth=None):
		self.size = size
		command = [ 'SET_ZONE_RADIUS', self.name, size ]

		if growth:
			command.append(growth)

		sendCommand(' '.join(command))

	def changeSpeed(self, xdir, ydir):
		self.xdir = xdir
		self.ydir = ydir
		sendCommand('SET_ZONE_SPEED ' + self.name + ' ' + xdir + ' ' + ydir)

	def setCommand(self, command):
		if zone.type == 'target':
			sendCommand('SET_TARGET_COMMAND ' + self.name + ' ' + command)

class Grid:
	def __init__(self):
		self.reset(None)

	def getTeam(self, name):
		if name in self.teams:
			return self.teams[name]

	def getPlayer(self, name):
		if name in self.players:
			return self.players[name]

	def getZone(self, name):
		if name in self.zones:
			return self.zones[name]

	def createZone(self, name, type, x, y, size, growth=0, xdir=0, ydir=0, interactive=None, r=None, g=None, b=None, target_size=None, rubber=None, player=None, owner=None, targetcommand=None):
		command = [ 'SPAWN_ZONE', 'n', name, type ]

		if type == 'zombie':
			if player == None:
				return None
			command.append(player)

		if type == 'zombieOwner':
			if player == None or owner == None:
				return None
			command.append(player)
			command.append(owner)

		command.extend([ x, y, size, growth, xdir, ydir ])

		if type == 'rubber':
			if rubber == None:
				return None
			command.append(rubber)

		if interactive:
			command.append(interactive)

			if r and g and b:
				command.extend([ r, g, b ])

				if target_size:
					command.append(target_size)

		sendCommand(' '.join(command))

		self.zones[name] = Zone(name, type, x, y, size, growth, xdir, ydir, interactive, r, g, b, target_size, rubber, player, owner, targetcommand)

		return self.zones[name]

	#Ladderlog commands

	def newRound(self, command):
		self.round += 1
		self.zones = {}
		for player in self.players.values():
			player.alive = True

	def newMatch(self, command):
		self.round = 1
		for team in self.teams.values():
			team.score = 0
		for player in self.players.values():
			player.score = 0

	def roundScore(self, command):
		if command[2] in self.players:
			self.players[command[2]].score += int(command[1])

	def roundScoreTeam(self, command):
		if command[2] in self.teams:
			self.teams[command[2]].score += int(command[1])

	def teamCreated(self, command):
		self.teams[command[1]] = Team(command[1])

	def teamDestroyed(self, command):
		if command[1] in self.teams:
			del self.teams[command[1]]

	def teamRenamed(self, command):
		if command[1] in self.teams:
			self.teams[command[2]] = self.teams.pop(command[1])
			self.teams[command[2]].name = command[2]

	def teamPlayerAdded(self, command):
		if command[1] in self.teams and command[2] in self.players:
			self.teams[command[1]].players[command[2]] = self.players[command[2]]

	def teamPlayerRemoved(self, command):
		if command[1] in self.teams and command[2] in self.teams[command[1]].players:
			del self.teams[command[1]].players[command[2]]

	def playerEntered(self, command):
		self.players[command[1]] = Player(command[1], command[3], command[2])

	def playerLeft(self, command):
		if command[1] in self.players:
			del self.players[command[1]]

	def playerRenamed(self, command):
		if command[1] in self.players:
			if not command[2] in self.players:
				self.players[command[2]] = self.players.pop(command[1])
				self.players[command[2]].name = command[2]
			self.players[command[2]].screenname = command[5]

	def numHumans(self, command):
		self.num_players = command[1]

	def positions(self, command):
		if command[1] in self.teams:
			self.teams[command[1]].positions = []
			for i in range(2, len(command)):
				self.teams[command[1]].positions.append(self.getPlayer(command[i]))

	def zoneSpawned(self, command):
		if command[2]:
			self.zones[command[2]] = Zone(command[2], None, command[3], command[4], None)
		else:
			self.zones[command[1]] = Zone(command[1], None, command[3], command[4], None)

	def zoneCollapsed(self, command):
		if command[1] in self.zones:
			del self.zones[command[1]]
		elif command[2] in self.zones:
			del self.zones[command[2]]

	def deathFrag(self, command):
		if command[1] in self.players:
			self.players[command[1]].alive = False

	def deathSuicide(self, command):
		if command[1] in self.players:
			self.players[command[1]].alive = False

	def deathTeamkill(self, command):
		if command[1] in self.players:
			self.players[command[1]].alive = False

	def deathBasezoneConquered(self, command):
		if command[1] in self.players:
			self.players[command[1]].alive = False

	def deathDeathzone(self, command):
		if command[1] in self.players:
			self.players[command[1]].alive = False

	def deathRubberzone(self, command):
		if command[1] in self.players:
			self.players[command[1]].alive = False

	def deathShotFrag(self, command):
		if command[1] in self.players:
			self.players[command[1]].alive = False

	def deathShotSuicide(self, command):
		if command[1] in self.players:
			self.players[command[1]].alive = False

	def deathShotTeamkill(self, command):
		if command[1] in self.players:
			self.players[command[1]].alive = False

	def reset(self, command):
		self.round = 0
		self.teams = {}
		self.num_players = 0
		self.players = {}
		self.zones = {}

grid = Grid()

commands = {	'NEW_ROUND': [ grid.newRound ],
		'NEW_MATCH': [ grid.newMatch ],
		'ROUND_SCORE': [ grid.roundScore ],
		'ROUND_SCORE_TEAM': [ grid.roundScoreTeam ],
		'TEAM_CREATED': [ grid.teamCreated ],
		'TEAM_DESTROYED': [ grid.teamDestroyed ],
		'TEAM_RENAMED': [ grid.teamRenamed ],
		'TEAM_PLAYER_ADDED': [ grid.teamPlayerAdded ],
		'TEAM_PLAYER_REMOVED': [ grid.teamPlayerRemoved ],
		'PLAYER_ENTERED': [ grid.playerEntered ],
		'PLAYER_LEFT': [ grid.playerLeft ],
		'PLAYER_RENAMED': [ grid.playerRenamed ],
		'NUM_HUMANS': [ grid.numHumans ],
		'POSITIONS': [ grid.positions ],
		'ZONE_SPAWNED': [ grid.zoneSpawned ],
		'ZONE_COLLAPSED': [ grid.zoneCollapsed ],
		'DEATH_FRAG': [ grid.deathFrag ],
		'DEATH_SUICIDE': [ grid.deathSuicide ],
		'DEATH_TEAMKILL': [ grid.deathTeamkill ],
		'DEATH_BASEZONE_CONQUERED': [ grid.deathBasezoneConquered ],
		'DEATH_DEATHZONE': [ grid.deathDeathzone ],
		'DEATH_RUBBERZONE': [ grid.deathRubberzone ],
		'DEATH_SHOT_FRAG': [ grid.deathShotFrag ],
		'DEATH_SHOT_SUICIDE': [ grid.deathShotSuicide ],
		'DEATH_SHOT_TEAMKILL': [ grid.deathShotTeamkill ],
		'GAME_END': [ grid.reset ],
		'ENCODING': [ init ],
		'INVALID_COMMAND': [ chatCommand ] }

chatcommands = {}
<commit_msg>Change variable name and use include functions over commands<commit_after>#!/usr/bin/python
import time
import sys.stdin as ladderlog
import sys.stdout as server

def addHandler(command, handler):
	if not command in commands:
		commands[command] = []
	commands[command].append(handler)

def removeHandler(command, handler):
	if command in commands and handler in commands[command]:
		commands[command].remove(handler)

def setChatHandler(command, handler):
	chatcommands[command] = handler

def removeChatHandler(command):
	if command in chatcommands:
		del chatcommands[command]

def sendCommand(command):
	server.write(command + '\n')

def say(message):
	sendCommand('SAY ' + message)

def consoleMessage(message):
	sendCommand('CONSOLE_MESSAGE "' + message + '"')

def centerMessage(message):
	sendCommand('CENTER_MESSAGE "' + message + '"')

def sendMessage(player, message):
	if type(player) == Player:
		sendCommand('PLAYER_MESSAGE ' + player.name + ' "' + message + '"')
	else:
		sendCommand('PLAYER_MESSAGE ' + player + ' "' + message + '"')

def pauseRound():
	sendCommand('WAIT_FOR_EXTERNAL_SCRIPT 1')

def continueRound():
	sendCommand('WAIT_FOR_EXTERNAL_SCRIPT 0')

def setRepository(address):
	sendCommand('RESOURCE_REPOSITORY_SERVER ' + address)

def setMap(resource):
	sendCommand('MAP_FILE ' + resource)

def include(config):
	sendCommand('INCLUDE ' + config)

def rinclude(config):
	sendCommand('RINCLUDE ' + config)

def reload():
	include('settings.cfg')
	include('server_info.cfg')
	include('settings_custom.cfg')
	include('script.cfg')
	sendCommand('START_NEW_MATCH')

def endRound():
	sendCommand('WIN_ZONE_MIN_LAST_DEATH 0')
	sendCommand('WIN_ZONE_MIN_ROUND_TIME 0')

def chatCommand(command):
	if command[1] in chatcommands:
		chatcommands[command[1]](command[1:])
	else:
		sendMessage(command[2], 'Unknown chat command "' + command[1] + '".')

def init(command):
	include('script.cfg')

def run():
	while True:
		line = ladderlog.readline()
		if not line:
			time.sleep(0.1)
			continue
		if line.startswith('QUIT'):
			break
		command = line.split()
		if command and command[0] in commands:
			for handler in commands[command[0]]:
				handler(command)

#Grid stuff

class Team:
	def __init__(self, name):
		self.name = name
		self.score = 0
		self.players = {}
		self.positions = []

class Player:
	def __init__(self, name, screenname, ip):
		self.name = name
		self.screenname = screenname
		self.ip = ip
		self.score = 0
		self.alive = False

	def sendMessage(self, message):
		sendMessage(self, message)

	def kill(self):
		sendCommand('KILL ' + self.name)

	def kick(self, reason=None):
		command = [ 'KICK', self.name ]
		if reason:
			command.append(reason)

		sendCommand(' '.join(command))

	def ban(self, time=None, reason=None):
		command = [ 'BAN', self.name ]
		if time:
			command.append(time)
		if reason:
			command.append(reason)

		sendCommand(' '.join(command))

	def banIP(self, time, reason=None):
		command = [ 'BAN_IP', self.ip ]
		if time:
			command.append(time)
			if reason:
				command.append(reason)

		sendCommand(' '.join(command))

	def declareWinner(self):
		sendCommand('DECLARE_ROUND_WINNER ' + self.name)

	def teleport(self, x, y, xdir, ydir):
		sendCommand('TELEPORT_PLAYER ' + self.name + ' ' + x + ' ' + y + ' ' + xdir + ' ' + ydir)

	def respawn(self, x, y, xdir, ydir):
		if not self.alive:
			sendCommand('RESPAWN_PLAYER ' + self.name + ' 1 ' + x + ' ' + y + ' ' + xdir + ' ' + ydir)

class Zone:
	def __init__(self, name, type, x, y, size, growth=0, xdir=0, ydir=0, interactive=None, r=None, g=None, b=None, target_size=None, rubber=None, player=None, owner=None, command=None):
		self.name = name
		self.type = type
		self.x = x
		self.y = y
		self.size = size
		self.growth = growth
		self.xdir = xdir
		self.ydir = ydir

		if interactive:
			self.interactive = interactive

		if r and g and b:
			self.r = r
			self.g = g
			self.b = b

		if target_size:
			self.target_size = target_size

		if type == 'rubber':
			self.rubber = rubber

		if type == 'zombie':
			self.player = player

		if type == 'zombieOwner':
			self.player = player
			self.owner = owner

		if type == 'target' and command != None:
			self.setCommand(command)

	def __del__(self):
		self.changeSize(0)

	def changeColor(self, r, g, b):
		self.r = r
		self.g = g
		self.b = b
		sendCommand('SET_ZONE_COLOR ' + self.name + ' ' + r + ' ' + g + ' ' + b)

	def changeExpansion(self, growth):
		self.growth = growth
		sendCommand('SET_ZONE_EXPANSION ' + self.name + ' ' + growth)

	def changePosition(self, x, y):
		self.x = x
		self.y = y
		sendCommand('SET_ZONE_POSITION ' + self.name + ' ' + x + ' ' + y)

	def changeSize(self, size, growth=None):
		self.size = size
		command = [ 'SET_ZONE_RADIUS', self.name, size ]

		if growth:
			command.append(growth)

		sendCommand(' '.join(command))

	def changeSpeed(self, xdir, ydir):
		self.xdir = xdir
		self.ydir = ydir
		sendCommand('SET_ZONE_SPEED ' + self.name + ' ' + xdir + ' ' + ydir)

	def setCommand(self, command):
		if zone.type == 'target':
			sendCommand('SET_TARGET_COMMAND ' + self.name + ' ' + command)

class Grid:
	def __init__(self):
		self.reset(None)

	def getTeam(self, name):
		if name in self.teams:
			return self.teams[name]

	def getPlayer(self, name):
		if name in self.players:
			return self.players[name]

	def getZone(self, name):
		if name in self.zones:
			return self.zones[name]

	def createZone(self, name, type, x, y, size, growth=0, xdir=0, ydir=0, interactive=None, r=None, g=None, b=None, target_size=None, rubber=None, player=None, owner=None, targetcommand=None):
		command = [ 'SPAWN_ZONE', 'n', name, type ]

		if type == 'zombie':
			if player == None:
				return None
			command.append(player)

		if type == 'zombieOwner':
			if player == None or owner == None:
				return None
			command.append(player)
			command.append(owner)

		command.extend([ x, y, size, growth, xdir, ydir ])

		if type == 'rubber':
			if rubber == None:
				return None
			command.append(rubber)

		if interactive:
			command.append(interactive)

			if r and g and b:
				command.extend([ r, g, b ])

				if target_size:
					command.append(target_size)

		sendCommand(' '.join(command))

		self.zones[name] = Zone(name, type, x, y, size, growth, xdir, ydir, interactive, r, g, b, target_size, rubber, player, owner, targetcommand)

		return self.zones[name]

	#Ladderlog commands

	def newRound(self, command):
		self.round += 1
		self.zones = {}
		for player in self.players.values():
			player.alive = True

	def newMatch(self, command):
		self.round = 1
		for team in self.teams.values():
			team.score = 0
		for player in self.players.values():
			player.score = 0

	def roundScore(self, command):
		if command[2] in self.players:
			self.players[command[2]].score += int(command[1])

	def roundScoreTeam(self, command):
		if command[2] in self.teams:
			self.teams[command[2]].score += int(command[1])

	def teamCreated(self, command):
		self.teams[command[1]] = Team(command[1])

	def teamDestroyed(self, command):
		if command[1] in self.teams:
			del self.teams[command[1]]

	def teamRenamed(self, command):
		if command[1] in self.teams:
			self.teams[command[2]] = self.teams.pop(command[1])
			self.teams[command[2]].name = command[2]

	def teamPlayerAdded(self, command):
		if command[1] in self.teams and command[2] in self.players:
			self.teams[command[1]].players[command[2]] = self.players[command[2]]

	def teamPlayerRemoved(self, command):
		if command[1] in self.teams and command[2] in self.teams[command[1]].players:
			del self.teams[command[1]].players[command[2]]

	def playerEntered(self, command):
		self.players[command[1]] = Player(command[1], command[3], command[2])

	def playerLeft(self, command):
		if command[1] in self.players:
			del self.players[command[1]]

	def playerRenamed(self, command):
		if command[1] in self.players:
			if not command[2] in self.players:
				self.players[command[2]] = self.players.pop(command[1])
				self.players[command[2]].name = command[2]
			self.players[command[2]].screenname = command[5]

	def numHumans(self, command):
		self.num_players = command[1]

	def positions(self, command):
		if command[1] in self.teams:
			self.teams[command[1]].positions = []
			for i in range(2, len(command)):
				self.teams[command[1]].positions.append(self.getPlayer(command[i]))

	def zoneSpawned(self, command):
		if command[2]:
			self.zones[command[2]] = Zone(command[2], None, command[3], command[4], None)
		else:
			self.zones[command[1]] = Zone(command[1], None, command[3], command[4], None)

	def zoneCollapsed(self, command):
		if command[1] in self.zones:
			del self.zones[command[1]]
		elif command[2] in self.zones:
			del self.zones[command[2]]

	def deathFrag(self, command):
		if command[1] in self.players:
			self.players[command[1]].alive = False

	def deathSuicide(self, command):
		if command[1] in self.players:
			self.players[command[1]].alive = False

	def deathTeamkill(self, command):
		if command[1] in self.players:
			self.players[command[1]].alive = False

	def deathBasezoneConquered(self, command):
		if command[1] in self.players:
			self.players[command[1]].alive = False

	def deathDeathzone(self, command):
		if command[1] in self.players:
			self.players[command[1]].alive = False

	def deathRubberzone(self, command):
		if command[1] in self.players:
			self.players[command[1]].alive = False

	def deathShotFrag(self, command):
		if command[1] in self.players:
			self.players[command[1]].alive = False

	def deathShotSuicide(self, command):
		if command[1] in self.players:
			self.players[command[1]].alive = False

	def deathShotTeamkill(self, command):
		if command[1] in self.players:
			self.players[command[1]].alive = False

	def reset(self, command):
		self.round = 0
		self.teams = {}
		self.num_players = 0
		self.players = {}
		self.zones = {}

grid = Grid()

commands = {	'NEW_ROUND': [ grid.newRound ],
		'NEW_MATCH': [ grid.newMatch ],
		'ROUND_SCORE': [ grid.roundScore ],
		'ROUND_SCORE_TEAM': [ grid.roundScoreTeam ],
		'TEAM_CREATED': [ grid.teamCreated ],
		'TEAM_DESTROYED': [ grid.teamDestroyed ],
		'TEAM_RENAMED': [ grid.teamRenamed ],
		'TEAM_PLAYER_ADDED': [ grid.teamPlayerAdded ],
		'TEAM_PLAYER_REMOVED': [ grid.teamPlayerRemoved ],
		'PLAYER_ENTERED': [ grid.playerEntered ],
		'PLAYER_LEFT': [ grid.playerLeft ],
		'PLAYER_RENAMED': [ grid.playerRenamed ],
		'NUM_HUMANS': [ grid.numHumans ],
		'POSITIONS': [ grid.positions ],
		'ZONE_SPAWNED': [ grid.zoneSpawned ],
		'ZONE_COLLAPSED': [ grid.zoneCollapsed ],
		'DEATH_FRAG': [ grid.deathFrag ],
		'DEATH_SUICIDE': [ grid.deathSuicide ],
		'DEATH_TEAMKILL': [ grid.deathTeamkill ],
		'DEATH_BASEZONE_CONQUERED': [ grid.deathBasezoneConquered ],
		'DEATH_DEATHZONE': [ grid.deathDeathzone ],
		'DEATH_RUBBERZONE': [ grid.deathRubberzone ],
		'DEATH_SHOT_FRAG': [ grid.deathShotFrag ],
		'DEATH_SHOT_SUICIDE': [ grid.deathShotSuicide ],
		'DEATH_SHOT_TEAMKILL': [ grid.deathShotTeamkill ],
		'GAME_END': [ grid.reset ],
		'ENCODING': [ init ],
		'INVALID_COMMAND': [ chatCommand ] }

chatcommands = {}
<|endoftext|>


<commit_before>from eventsourcing.tests.example_application_tests.base import WithExampleApplication
from eventsourcing.tests.sequenced_item_tests.test_sqlalchemy_active_record_strategy import \
    WithSQLAlchemyActiveRecordStrategies

class TestGetAllEventFromSQLAlchemy(WithSQLAlchemyActiveRecordStrategies, WithExampleApplication):

    def test(self):
        with self.construct_application() as app:
            domain_events = self.get_all_domain_events(app)
            self.assertEqual(len(domain_events), 0)

            app.register_new_example('a1', 'b1')
            app.register_new_example('a2', 'b2')
            app.register_new_example('a3', 'b3')

            domain_events = self.get_all_domain_events(app)
            self.assertEqual(len(domain_events), 3)

    def get_all_domain_events(self, app):
        es = app.version_entity_event_store
        active_records = es.active_record_strategy.filter()
        domain_events = []
        for r in active_records:
            i = es.active_record_strategy.from_active_record(r)
            e = es.sequenced_item_mapper.from_sequenced_item(i)
            domain_events.append(e)
        return domain_events
<commit_msg>Fixed test.<commit_after>from eventsourcing.tests.example_application_tests.base import WithExampleApplication
from eventsourcing.tests.sequenced_item_tests.test_sqlalchemy_active_record_strategy import \
    WithSQLAlchemyActiveRecordStrategies

class TestGetAllEventFromSQLAlchemy(WithSQLAlchemyActiveRecordStrategies, WithExampleApplication):

    def test(self):
        with self.construct_application() as app:
            es = app.version_entity_event_store
            domain_events = es.all_domain_events()
            domain_events = list(domain_events)
            self.assertEqual(len(domain_events), 0)

            app.register_new_example('a1', 'b1')
            app.register_new_example('a2', 'b2')
            app.register_new_example('a3', 'b3')

            domain_events = es.all_domain_events()
            domain_events = list(domain_events)
            self.assertEqual(len(domain_events), 3)
<|endoftext|>