<commit_before>import pprint
import sys
import unittest

import test_support

class HookWatcher:
    def __init__(self):
        self.frames = []
        self.events = []

    def callback(self, frame, event, arg):
        self.add_event(event, frame)

    def add_event(self, event, frame=None):
        """Add an event to the log."""
        if frame is None:
            frame = sys._getframe(1)

        try:
            frameno = self.frames.index(frame)
        except ValueError:
            frameno = len(self.frames)
            self.frames.append(frame)

        self.events.append((frameno, event, ident(frame)))

    def get_events(self):
        """Remove calls to add_event()."""
        disallowed = [ident(self.add_event.im_func), ident(ident)]
        self.frames = None

        return [item for item in self.events if item[2] not in disallowed]

class ProfileHookTestCase(unittest.TestCase):

    def check_events(self, callable, expected):
        events = capture_events(callable)
        if events != expected:
            self.fail("Expected events:\n%s\nReceived events:\n%s"
                      % (pprint.pformat(expected), pprint.pformat(events)))

    def test_simple(self):
        def f(p):
            pass
        f_ident = ident(f)
        self.check_events(f, [(1, 'call', f_ident),
                              (1, 'return', f_ident),
                              ])

    def test_exception(self):
        def f(p):
            1/0
        f_ident = ident(f)
        self.check_events(f, [(1, 'call', f_ident),
                              (1, 'exception', f_ident),
                              (0, 'exception', protect_ident),
                              ])

    def test_caught_exception(self):
        def f(p):
            try: 1/0
            except: pass
        f_ident = ident(f)
        self.check_events(f, [(1, 'call', f_ident),
                              (1, 'exception', f_ident),
                              (1, 'return', f_ident),
                              ])

    def test_caught_nested_exception(self):
        def f(p):
            try: 1/0
            except: pass
        f_ident = ident(f)
        self.check_events(f, [(1, 'call', f_ident),
                              (1, 'exception', f_ident),
                              (1, 'return', f_ident),
                              ])

    def test_nested_exception(self):
        def f(p):
            1/0
        f_ident = ident(f)
        self.check_events(f, [(1, 'call', f_ident),
                              (1, 'exception', f_ident),
                              # This isn't what I expected:
                              (0, 'exception', protect_ident),
                              # I expected this again:
                              # (1, 'exception', f_ident),
                              ])

    def test_exception_in_except_clause(self):
        def f(p):
            1/0
        def g(p):
            try:
                f(p)
            except:
                try: f(p)
                except: pass
        f_ident = ident(f)
        g_ident = ident(g)
        self.check_events(g, [(1, 'call', g_ident),
                              (2, 'call', f_ident),
                              (2, 'exception', f_ident),
                              (1, 'exception', g_ident),
                              (3, 'call', f_ident),
                              (3, 'exception', f_ident),
                              (1, 'exception', g_ident),
                              (1, 'return', g_ident),
                              ])

    def test_exception_propogation(self):
        def f(p):
            1/0
        def g(p):
            try: f(p)
            finally: p.add_event("falling through")
        f_ident = ident(f)
        g_ident = ident(g)
        self.check_events(g, [(1, 'call', g_ident),
                              (2, 'call', f_ident),
                              (2, 'exception', f_ident),
                              (1, 'exception', g_ident),
                              (1, 'falling through', g_ident),
                              (0, 'exception', protect_ident),
                              ])

    def test_raise_twice(self):
        def f(p):
            try: 1/0
            except: 1/0
        f_ident = ident(f)
        self.check_events(f, [(1, 'call', f_ident),
                              (1, 'exception', f_ident),
                              (1, 'exception', f_ident),
                              (0, 'exception', protect_ident)
                              ])

    def test_raise_reraise(self):
        def f(p):
            try: 1/0
            except: raise
        f_ident = ident(f)
        self.check_events(f, [(1, 'call', f_ident),
                              (1, 'exception', f_ident),
                              (0, 'exception', protect_ident)
                              ])

    def test_raise(self):
        def f(p):
            raise Exception()
        f_ident = ident(f)
        self.check_events(f, [(1, 'call', f_ident),
                              (1, 'exception', f_ident),
                              (0, 'exception', protect_ident)
                              ])

def ident(function):
    if hasattr(function, "f_code"):
        code = function.f_code
    else:
        code = function.func_code
    return code.co_firstlineno, code.co_name

def protect(f, p):
    try: f(p)
    except: pass

protect_ident = ident(protect)

def capture_events(callable):
    p = HookWatcher()
    sys.setprofile(p.callback)
    protect(callable, p)
    sys.setprofile(None)
    return p.get_events()[1:-1]

def show_events(callable):
    import pprint
    pprint.pprint(capture_events(callable))

def test_main():
    test_support.run_unittest(ProfileHookTestCase)

if __name__ == "__main__":
    test_main()
<commit_msg>More test cases, including something that simulates what the profiler probably *should* be doing.<commit_after>import pprint
import sys
import unittest

import test_support

class HookWatcher:
    def __init__(self):
        self.frames = []
        self.events = []

    def callback(self, frame, event, arg):
        self.add_event(event, frame)

    def add_event(self, event, frame=None):
        """Add an event to the log."""
        if frame is None:
            frame = sys._getframe(1)

        try:
            frameno = self.frames.index(frame)
        except ValueError:
            frameno = len(self.frames)
            self.frames.append(frame)

        self.events.append((frameno, event, ident(frame)))

    def get_events(self):
        """Remove calls to add_event()."""
        disallowed = [ident(self.add_event.im_func), ident(ident)]
        self.frames = None

        return [item for item in self.events if item[2] not in disallowed]

class ProfileSimulator(HookWatcher):
    def __init__(self):
        self.stack = []
        HookWatcher.__init__(self)

    def callback(self, frame, event, arg):
        self.dispatch[event](self, frame)

    def trace_call(self, frame):
        self.add_event('call', frame)
        self.stack.append(frame)

    def trace_return(self, frame):
        self.add_event('return', frame)
        self.stack.pop()

    def trace_exception(self, frame):
        if len(self.stack) >= 2 and frame is self.stack[-2]:
            self.add_event('propogate-from', self.stack[-1])
            self.stack.pop()
        else:
            self.add_event('ignore', frame)

    dispatch = {
        'call': trace_call,
        'exception': trace_exception,
        'return': trace_return,
        }

class TestCaseBase(unittest.TestCase):
    def check_events(self, callable, expected):
        events = capture_events(callable, self.new_watcher())
        if events != expected:
            self.fail("Expected events:\n%s\nReceived events:\n%s"
                      % (pprint.pformat(expected), pprint.pformat(events)))

class ProfileHookTestCase(TestCaseBase):
    def new_watcher(self):
        return HookWatcher()

    def test_simple(self):
        def f(p):
            pass
        f_ident = ident(f)
        self.check_events(f, [(1, 'call', f_ident),
                              (1, 'return', f_ident),
                              ])

    def test_exception(self):
        def f(p):
            1/0
        f_ident = ident(f)
        self.check_events(f, [(1, 'call', f_ident),
                              (1, 'exception', f_ident),
                              (0, 'exception', protect_ident),
                              ])

    def test_caught_exception(self):
        def f(p):
            try: 1/0
            except: pass
        f_ident = ident(f)
        self.check_events(f, [(1, 'call', f_ident),
                              (1, 'exception', f_ident),
                              (1, 'return', f_ident),
                              ])

    def test_caught_nested_exception(self):
        def f(p):
            try: 1/0
            except: pass
        f_ident = ident(f)
        self.check_events(f, [(1, 'call', f_ident),
                              (1, 'exception', f_ident),
                              (1, 'return', f_ident),
                              ])

    def test_nested_exception(self):
        def f(p):
            1/0
        f_ident = ident(f)
        self.check_events(f, [(1, 'call', f_ident),
                              (1, 'exception', f_ident),
                              # This isn't what I expected:
                              (0, 'exception', protect_ident),
                              # I expected this again:
                              # (1, 'exception', f_ident),
                              ])

    def test_exception_in_except_clause(self):
        def f(p):
            1/0
        def g(p):
            try:
                f(p)
            except:
                try: f(p)
                except: pass
        f_ident = ident(f)
        g_ident = ident(g)
        self.check_events(g, [(1, 'call', g_ident),
                              (2, 'call', f_ident),
                              (2, 'exception', f_ident),
                              (1, 'exception', g_ident),
                              (3, 'call', f_ident),
                              (3, 'exception', f_ident),
                              (1, 'exception', g_ident),
                              (1, 'return', g_ident),
                              ])

    def test_exception_propogation(self):
        def f(p):
            1/0
        def g(p):
            try: f(p)
            finally: p.add_event("falling through")
        f_ident = ident(f)
        g_ident = ident(g)
        self.check_events(g, [(1, 'call', g_ident),
                              (2, 'call', f_ident),
                              (2, 'exception', f_ident),
                              (1, 'exception', g_ident),
                              (1, 'falling through', g_ident),
                              (0, 'exception', protect_ident),
                              ])

    def test_raise_twice(self):
        def f(p):
            try: 1/0
            except: 1/0
        f_ident = ident(f)
        self.check_events(f, [(1, 'call', f_ident),
                              (1, 'exception', f_ident),
                              (1, 'exception', f_ident),
                              (0, 'exception', protect_ident)
                              ])

    def test_raise_reraise(self):
        def f(p):
            try: 1/0
            except: raise
        f_ident = ident(f)
        self.check_events(f, [(1, 'call', f_ident),
                              (1, 'exception', f_ident),
                              (0, 'exception', protect_ident)
                              ])

    def test_raise(self):
        def f(p):
            raise Exception()
        f_ident = ident(f)
        self.check_events(f, [(1, 'call', f_ident),
                              (1, 'exception', f_ident),
                              (0, 'exception', protect_ident)
                              ])

class ProfileSimulatorTestCase(TestCaseBase):
    def new_watcher(self):
        return ProfileSimulator()

    def test_simple(self):
        def f(p):
            pass
        f_ident = ident(f)
        self.check_events(f, [(1, 'call', f_ident),
                              (1, 'return', f_ident),
                              ])

    def test_basic_exception(self):
        def f(p):
            1/0
        f_ident = ident(f)
        self.check_events(f, [(1, 'call', f_ident),
                              (1, 'ignore', f_ident),
                              (1, 'propogate-from', f_ident),
                              ])

def ident(function):
    if hasattr(function, "f_code"):
        code = function.f_code
    else:
        code = function.func_code
    return code.co_firstlineno, code.co_name

def protect(f, p):
    try: f(p)
    except: pass

protect_ident = ident(protect)

def capture_events(callable, p=None):
    if p is None:
        p = HookWatcher()
    sys.setprofile(p.callback)
    protect(callable, p)
    sys.setprofile(None)
    return p.get_events()[1:-1]

def show_events(callable):
    import pprint
    pprint.pprint(capture_events(callable))

def test_main():
    loader = unittest.TestLoader()
    suite = unittest.TestSuite()
    suite.addTest(loader.loadTestsFromTestCase(ProfileHookTestCase))
    suite.addTest(loader.loadTestsFromTestCase(ProfileSimulatorTestCase))
    test_support.run_suite(suite)

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


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

import json

from bottle import delete, put, get, post
from bottle import request, response

from simplenet.common.auth import handle_auth
from simplenet.common.config import config, get_logger
from simplenet.common.http_utils import (
    reply_json, create_manager, validate_input
)
from simplenet.exceptions import (
    FeatureNotImplemented, EntityNotFound,
    OperationNotPermited, FeatureNotAvailable
)

logger = get_logger()

@get('/prober')
@handle_auth
@reply_json
def generic_resources_list():
    """
    ::

      GET /prober

    Do a SQL query for DB status
    """
    manager = create_manager('base')
    try:
        _list = getattr(manager, 'prober')
        return _list()
    except AttributeError:
        raise FeatureNotAvailable()

## Generic Resource List
@get('/<resource>/list')
@handle_auth
@reply_json
def generic_resources_list(resource):
    """
    ::

      GET /<resource>/list

    Retrieves all entries from resource
    """
    manager = create_manager('base')
    try:
        _list = getattr(manager, '%s_list' % resource[:-1])
        return _list()
    except AttributeError:
        raise FeatureNotAvailable()

## Generic Resource Info
@get('/<resource>/<resource_id>/info')
@handle_auth
@reply_json
def generic_resource_info(resource, resource_id):
    """
    ::

      GET /<resource>/<resource_id>/info

    Retrieves resource information
    """
    manager = create_manager('base')
    try:
        _info = getattr(manager, '%s_info' % resource[:-1])
        return _info(resource_id)
    except AttributeError:
        raise FeatureNotAvailable()

## Generic Resource Info by name, cidr, ip
@get('/<resource>/by-<resource_type>/<resource_value>')
@handle_auth
@reply_json
def generic_resource_info_by_field(resource, resource_type, resource_value):
    """
    ::

      GET /<resource>/by-<resource_type>/<resource_value>

    Retrieves resource information by type
    """
    manager = create_manager('base')
    try:
        _info = getattr(manager, '%s_info_by_%s' % (resource[:-1], resource_type))
        return _info(resource_value)
    except AttributeError:
        raise FeatureNotAvailable()

# Generic list by parent
@get('/<resource>/list-by-<relationship_type>/<relationship_value>')
@handle_auth
@reply_json
def generic_resource_list_by_relationship(resource, relationship_type, relationship_value):
    """
    ::

      GET /<resource>/list-by-<relationship_type>/<relationship_value>

    List devices
    """
    manager = create_manager('base')
    try:
        _list = getattr(manager, '%s_list_by_%s' % (resource[:-1], relationship_type))
        return _list(relationship_value)
    except AttributeError:
        raise FeatureNotAvailable()

## Generic Resource Deletion
@delete('/<resource>/<resource_id>/delete')
@handle_auth
@reply_json
def generic_resource_delete(resource, resource_id):
    """
    ::

      DELETE /<resource>/<resource_id>/delete

    Deletes resource
    """
    manager = create_manager('base')
    try:
        _delete = getattr(manager, '%s_delete' % (resource[:-1]))
        return _delete(resource_id)
    except AttributeError:
        raise FeatureNotAvailable()

@post('/datacenters')
@handle_auth
@validate_input(name=str)
@reply_json
def datacenter_create():
    """
    ::

      POST /datacenters

    Create a new datacenter
    """
    manager = create_manager('base')
    data = request.body.readline()
    if not data:
        abort(400, 'No data received')
    data = json.loads(data)
    datacenter = manager.datacenter_create(data)
    location = "datacenters/%s" % (datacenter['id'])
    response.set_header("Location", location)
    return datacenter

@post('/datacenters/<datacenter_id>/zones')
@handle_auth
@validate_input(name=str)
@reply_json
def datacenter_zone_create(datacenter_id):
    """
    ::

      POST /datacenters/<datacenter_id>/zones

    Create a new zone in datacenter
    """
    manager = create_manager('base')
    data = request.body.readline()
    if not data:
        abort(400, 'No data received')
    data = json.loads(data)
    zone = manager.zone_create(datacenter_id, data)
    location = "zones/%s" % (zone['id'])
    response.set_header("Location", location)
    return zone

@post('/zones/<zone_id>/devices')
@handle_auth
@validate_input(name=str)
@reply_json
def zone_device_create(zone_id):
    """
    ::

      POST /zones/<zone_id>/devices

    Create a new device in zone
    """
    manager = create_manager('base')
    data = request.body.readline()
    if not data:
        abort(400, 'No data received')
    data = json.loads(data)
    device = manager.device_create(zone_id, data)
    location = "devices/%s" % (device['id'])
    response.set_header("Location", location)
    return device

@post('/zones/<zone_id>/vlans')
@handle_auth
@validate_input(name=str)
@reply_json
def zone_vlan_create(zone_id):
    """
    ::

      POST /zones/<zone_id>/vlans

    Create a new vlan in zone
    """
    manager = create_manager('base')
    data = request.body.readline()
    if not data:
        abort(400, 'No data received')
    data = json.loads(data)
    vlan = manager.vlan_create(zone_id, data)
    location = "vlans/%s" % (vlan['id'])
    response.set_header("Location", location)
    return vlan

@post('/devices/<device_id>/vlans')
@handle_auth
@validate_input(vlan_id=str)
@reply_json
def device_add_vlan(device_id):
    """
    ::

      POST /devices/<device_id>/vlans

    Attach vlan to device
    """
    manager = create_manager('base')
    data = request.body.readline()
    if not data:
        abort(400, 'No data received')
    data = json.loads(data)
    device = manager.device_add_vlan(device_id, data)
    location = "devices/relationship/%s" % (device['id'])
    response.set_header("Location", location)
    return device

@post('/devices/<device_id>/anycasts')
@handle_auth
@validate_input(anycast_id=str)
@reply_json
def device_add_anycast(device_id):
    """
    ::

      POST /devices/<device_id>/anycasts

    Attach vlan to device
    """
    manager = create_manager('base')
    data = request.body.readline()
    if not data:
        abort(400, 'No data received')
    data = json.loads(data)
    device = manager.device_add_anycast(device_id, data)
    location = "devices/relationship/%s" % (device['id'])
    response.set_header("Location", location)
    return device

@delete('/devices/<device_id>/vlans/<vlan_id>')
@handle_auth
@reply_json
def device_remove_vlan(device_id, vlan_id):
    """
    ::

      POST /devices/<device_id>/vlans/<vlan_id>

    Attach vlan to device
    """
    manager = create_manager('base')
    device = manager.device_remove_vlan(device_id, vlan_id)
    return device

@delete('/devices/<device_id>/anycasts/<anycast_id>')
@handle_auth
@reply_json
def device_remove_anycast(device_id, vlan_id):
    """
    ::

      POST /devices/<device_id>/anycasts/<anycast_id>

    Attach anycasts to device
    """
    manager = create_manager('base')
    device = manager.device_remove_anycast(device_id, anycast_id)
    return device

@post('/anycasts')
@handle_auth
@validate_input(cidr=str)
@reply_json
def anycast_create():
    """
    ::

      POST /anycasts

    Create a new anycast range
    """
    manager = create_manager('base')
    data = request.body.readline()
    if not data:
        abort(400, 'No data received')
    data = json.loads(data)
    anycast = manager.anycast_create(data)
    location = "anycasts/%s" % (anycast['id'])
    response.set_header("Location", location)
    return anycast

@post('/vlans/<vlan_id>/subnets')
@handle_auth
@validate_input(cidr=str)
@reply_json
def vlan_subnet_create(vlan_id):
    """
    ::

      POST /vlans/<vlan_id>/subnets

    Create a new subnet in vlan
    """
    manager = create_manager('base')
    data = request.body.readline()
    if not data:
        abort(400, 'No data received')
    data = json.loads(data)
    subnet = manager.subnet_create(vlan_id, data)
    location = "subnets/%s" % (subnet['id'])
    response.set_header("Location", location)
    return subnet

@post('/anycasts/<anycast_id>/anycastips')
@handle_auth
@validate_input(ip=str)
@reply_json
def anycast_anycastip_create(anycast_id):
    """
    ::

      POST /anycasts/<anycast_id>/anycastips

    Create a new ip in anycast subnet
    """
    manager = create_manager('base')
    data = request.body.readline()
    if not data:
        abort(400, 'No data received')
    data = json.loads(data)
    ip = manager.anycastip_create(anycast_id, data)
    location = "anycastips/%s" % (ip['id'])
    response.set_header("Location", location)
    return ip

@post('/subnets/<subnet_id>/ips')
@handle_auth
@validate_input(ip=str)
@reply_json
def subnet_ip_create(subnet_id):
    """
    ::

      POST /subnets/<subnet_id>/ips

    Create a new ip in subnet
    """
    manager = create_manager('base')
    data = request.body.readline()
    if not data:
        abort(400, 'No data received')
    data = json.loads(data)
    ip = manager.ip_create(subnet_id, data)
    location = "ips/%s" % (ip['id'])
    response.set_header("Location", location)
    return ip
<commit_msg>fixed typo on id<commit_after># Copyright 2012 Locaweb.
# All Rights Reserved.
#
#    Licensed under the Apache License, Version 2.0 (the "License");
#    you may not use this file except in compliance with the License.
#    You may obtain a copy of the License at
#
#        http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS,
#    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#    See the License for the specific language governing permissions and
#    limitations under the License.
#
# @author: Juliano Martinez (ncode), Locaweb.
# @author: Luiz Ozaki, Locaweb.

import json

from bottle import delete, put, get, post
from bottle import request, response

from simplenet.common.auth import handle_auth
from simplenet.common.config import config, get_logger
from simplenet.common.http_utils import (
    reply_json, create_manager, validate_input
)
from simplenet.exceptions import (
    FeatureNotImplemented, EntityNotFound,
    OperationNotPermited, FeatureNotAvailable
)

logger = get_logger()

@get('/prober')
@handle_auth
@reply_json
def generic_resources_list():
    """
    ::

      GET /prober

    Do a SQL query for DB status
    """
    manager = create_manager('base')
    try:
        _list = getattr(manager, 'prober')
        return _list()
    except AttributeError:
        raise FeatureNotAvailable()

## Generic Resource List
@get('/<resource>/list')
@handle_auth
@reply_json
def generic_resources_list(resource):
    """
    ::

      GET /<resource>/list

    Retrieves all entries from resource
    """
    manager = create_manager('base')
    try:
        _list = getattr(manager, '%s_list' % resource[:-1])
        return _list()
    except AttributeError:
        raise FeatureNotAvailable()

## Generic Resource Info
@get('/<resource>/<resource_id>/info')
@handle_auth
@reply_json
def generic_resource_info(resource, resource_id):
    """
    ::

      GET /<resource>/<resource_id>/info

    Retrieves resource information
    """
    manager = create_manager('base')
    try:
        _info = getattr(manager, '%s_info' % resource[:-1])
        return _info(resource_id)
    except AttributeError:
        raise FeatureNotAvailable()

## Generic Resource Info by name, cidr, ip
@get('/<resource>/by-<resource_type>/<resource_value>')
@handle_auth
@reply_json
def generic_resource_info_by_field(resource, resource_type, resource_value):
    """
    ::

      GET /<resource>/by-<resource_type>/<resource_value>

    Retrieves resource information by type
    """
    manager = create_manager('base')
    try:
        _info = getattr(manager, '%s_info_by_%s' % (resource[:-1], resource_type))
        return _info(resource_value)
    except AttributeError:
        raise FeatureNotAvailable()

# Generic list by parent
@get('/<resource>/list-by-<relationship_type>/<relationship_value>')
@handle_auth
@reply_json
def generic_resource_list_by_relationship(resource, relationship_type, relationship_value):
    """
    ::

      GET /<resource>/list-by-<relationship_type>/<relationship_value>

    List devices
    """
    manager = create_manager('base')
    try:
        _list = getattr(manager, '%s_list_by_%s' % (resource[:-1], relationship_type))
        return _list(relationship_value)
    except AttributeError:
        raise FeatureNotAvailable()

## Generic Resource Deletion
@delete('/<resource>/<resource_id>/delete')
@handle_auth
@reply_json
def generic_resource_delete(resource, resource_id):
    """
    ::

      DELETE /<resource>/<resource_id>/delete

    Deletes resource
    """
    manager = create_manager('base')
    try:
        _delete = getattr(manager, '%s_delete' % (resource[:-1]))
        return _delete(resource_id)
    except AttributeError:
        raise FeatureNotAvailable()

@post('/datacenters')
@handle_auth
@validate_input(name=str)
@reply_json
def datacenter_create():
    """
    ::

      POST /datacenters

    Create a new datacenter
    """
    manager = create_manager('base')
    data = request.body.readline()
    if not data:
        abort(400, 'No data received')
    data = json.loads(data)
    datacenter = manager.datacenter_create(data)
    location = "datacenters/%s" % (datacenter['id'])
    response.set_header("Location", location)
    return datacenter

@post('/datacenters/<datacenter_id>/zones')
@handle_auth
@validate_input(name=str)
@reply_json
def datacenter_zone_create(datacenter_id):
    """
    ::

      POST /datacenters/<datacenter_id>/zones

    Create a new zone in datacenter
    """
    manager = create_manager('base')
    data = request.body.readline()
    if not data:
        abort(400, 'No data received')
    data = json.loads(data)
    zone = manager.zone_create(datacenter_id, data)
    location = "zones/%s" % (zone['id'])
    response.set_header("Location", location)
    return zone

@post('/zones/<zone_id>/devices')
@handle_auth
@validate_input(name=str)
@reply_json
def zone_device_create(zone_id):
    """
    ::

      POST /zones/<zone_id>/devices

    Create a new device in zone
    """
    manager = create_manager('base')
    data = request.body.readline()
    if not data:
        abort(400, 'No data received')
    data = json.loads(data)
    device = manager.device_create(zone_id, data)
    location = "devices/%s" % (device['id'])
    response.set_header("Location", location)
    return device

@post('/zones/<zone_id>/vlans')
@handle_auth
@validate_input(name=str)
@reply_json
def zone_vlan_create(zone_id):
    """
    ::

      POST /zones/<zone_id>/vlans

    Create a new vlan in zone
    """
    manager = create_manager('base')
    data = request.body.readline()
    if not data:
        abort(400, 'No data received')
    data = json.loads(data)
    vlan = manager.vlan_create(zone_id, data)
    location = "vlans/%s" % (vlan['id'])
    response.set_header("Location", location)
    return vlan

@post('/devices/<device_id>/vlans')
@handle_auth
@validate_input(vlan_id=str)
@reply_json
def device_add_vlan(device_id):
    """
    ::

      POST /devices/<device_id>/vlans

    Attach vlan to device
    """
    manager = create_manager('base')
    data = request.body.readline()
    if not data:
        abort(400, 'No data received')
    data = json.loads(data)
    device = manager.device_add_vlan(device_id, data)
    location = "devices/relationship/%s" % (device['id'])
    response.set_header("Location", location)
    return device

@post('/devices/<device_id>/anycasts')
@handle_auth
@validate_input(anycast_id=str)
@reply_json
def device_add_anycast(device_id):
    """
    ::

      POST /devices/<device_id>/anycasts

    Attach vlan to device
    """
    manager = create_manager('base')
    data = request.body.readline()
    if not data:
        abort(400, 'No data received')
    data = json.loads(data)
    device = manager.device_add_anycast(device_id, data)
    location = "devices/relationship/%s" % (device['id'])
    response.set_header("Location", location)
    return device

@delete('/devices/<device_id>/vlans/<vlan_id>')
@handle_auth
@reply_json
def device_remove_vlan(device_id, vlan_id):
    """
    ::

      POST /devices/<device_id>/vlans/<vlan_id>

    Attach vlan to device
    """
    manager = create_manager('base')
    device = manager.device_remove_vlan(device_id, vlan_id)
    return device

@delete('/devices/<device_id>/anycasts/<anycast_id>')
@handle_auth
@reply_json
def device_remove_anycast(device_id, anycast_id):
    """
    ::

      POST /devices/<device_id>/anycasts/<anycast_id>

    Attach anycasts to device
    """
    manager = create_manager('base')
    device = manager.device_remove_anycast(device_id, anycast_id)
    return device

@post('/anycasts')
@handle_auth
@validate_input(cidr=str)
@reply_json
def anycast_create():
    """
    ::

      POST /anycasts

    Create a new anycast range
    """
    manager = create_manager('base')
    data = request.body.readline()
    if not data:
        abort(400, 'No data received')
    data = json.loads(data)
    anycast = manager.anycast_create(data)
    location = "anycasts/%s" % (anycast['id'])
    response.set_header("Location", location)
    return anycast

@post('/vlans/<vlan_id>/subnets')
@handle_auth
@validate_input(cidr=str)
@reply_json
def vlan_subnet_create(vlan_id):
    """
    ::

      POST /vlans/<vlan_id>/subnets

    Create a new subnet in vlan
    """
    manager = create_manager('base')
    data = request.body.readline()
    if not data:
        abort(400, 'No data received')
    data = json.loads(data)
    subnet = manager.subnet_create(vlan_id, data)
    location = "subnets/%s" % (subnet['id'])
    response.set_header("Location", location)
    return subnet

@post('/anycasts/<anycast_id>/anycastips')
@handle_auth
@validate_input(ip=str)
@reply_json
def anycast_anycastip_create(anycast_id):
    """
    ::

      POST /anycasts/<anycast_id>/anycastips

    Create a new ip in anycast subnet
    """
    manager = create_manager('base')
    data = request.body.readline()
    if not data:
        abort(400, 'No data received')
    data = json.loads(data)
    ip = manager.anycastip_create(anycast_id, data)
    location = "anycastips/%s" % (ip['id'])
    response.set_header("Location", location)
    return ip

@post('/subnets/<subnet_id>/ips')
@handle_auth
@validate_input(ip=str)
@reply_json
def subnet_ip_create(subnet_id):
    """
    ::

      POST /subnets/<subnet_id>/ips

    Create a new ip in subnet
    """
    manager = create_manager('base')
    data = request.body.readline()
    if not data:
        abort(400, 'No data received')
    data = json.loads(data)
    ip = manager.ip_create(subnet_id, data)
    location = "ips/%s" % (ip['id'])
    response.set_header("Location", location)
    return ip
<|endoftext|>


<commit_before>import os
import struct

import numpy as np
import matplotlib.pyplot as plt

"""
Loosely inspired by http://abel.ee.ucla.edu/cvxopt/_downloads/mnist.py
which is GPL licensed.
"""

def read(path="."):
    """
    Python function for importing the MNIST data set.  It returns an iterator
    of 2-tuples with the first element being the label and the second element
    being a numpy.uint8 2D array of pixel data for the given image.
    """

    fname_img = os.path.join(path, 'train-images.idx3-ubyte')
    fname_lbl = os.path.join(path, 'train-labels.idx1-ubyte')

    # Load everything in some numpy arrays
    with open(fname_lbl, 'rb') as flbl:
        magic, num = struct.unpack(">II", flbl.read(8))
        lbl = np.fromfile(flbl, dtype=np.int8)

    with open(fname_img, 'rb') as fimg:
        magic, num, rows, cols = struct.unpack(">IIII", fimg.read(16))
        img = np.fromfile(fimg, dtype=np.uint8).reshape(len(lbl), rows, cols)

    get_img = lambda idx: (lbl[idx], img[idx])

    # Create an iterator which returns each image in turn
    for i in range(len(lbl)):
        yield get_img(i)

def show(image):
    """
    Render a given numpy.uint8 2D array of pixel data.
    """

    fig = plt.figure()
    ax = fig.add_subplot(1, 1, 1)
    imgplot = ax.imshow(image, cmap='gray')
    imgplot.set_interpolation('nearest')
    ax.xaxis.set_ticks_position('top')
    ax.yaxis.set_ticks_position('left')
    plt.show()

if __name__ == '__main__':
    img = read()

    # t = (group.sort_index(ascending=True) for key, group in img.iteritems())

    for i in img:
        show(i[1])
        break
<commit_msg>updated to read training image and testing<commit_after>import os
import struct

import numpy as np
import matplotlib.pyplot as plt
import pandas

"""
Loosely inspired by http://abel.ee.ucla.edu/cvxopt/_downloads/mnist.py
which is GPL licensed.
"""

def read(path="."):
    """
    Python function for importing the MNIST data set.  It returns an iterator
    of 2-tuples with the first element being the label and the second element
    being a numpy.uint8 2D array of pixel data for the given image.
    """

    fname_img_training = os.path.join(path, 'train-images.idx3-ubyte')
    fname_lbl_training = os.path.join(path, 'train-labels.idx1-ubyte')

    fname_img_testing = os.path.join(path, 't10k-images.idx3-ubyte')
    fname_lbl_testing = os.path.join(path, 't10k-labels.idx1-ubyte')

    # Load everything in some numpy arrays for training
    with open(fname_lbl_training, 'rb') as flbl_training:
        magic, num = struct.unpack(">II", flbl_training.read(8))
        lbl_training = np.fromfile(flbl_training, dtype=np.int8)

    with open(fname_img_training, 'rb') as fimg_training:
        magic, num, rows, cols = struct.unpack(">IIII", fimg_training.read(16))
        img_training = np.fromfile(fimg_training, dtype=np.uint8).reshape(len(lbl_training), rows, cols)

    # ----------------------------------------------------------------------------------------------------------

    # Load everything in some numpy arrays for testing
    with open(fname_lbl_testing, 'rb') as flbl_testing:
        magic, num = struct.unpack(">II", flbl_testing.read(8))
        lbl_testing = np.fromfile(flbl_testing, dtype=np.int8)

    with open(fname_img_testing, 'rb') as fimg_testing:
        magic, num, rows, cols = struct.unpack(">IIII", fimg_testing.read(16))
        img_testing = np.fromfile(fimg_testing, dtype=np.uint8).reshape(len(lbl_testing), rows, cols)

    # -----------------------------------------------------------------------------------------------------------

    images = {'training': img_training, 'testing': img_testing}

    return images

def show(image):
    """
    Render a given numpy.uint8 2D array of pixel data.
    """

    fig = plt.figure()
    ax = fig.add_subplot(1, 1, 1)
    imgplot = ax.imshow(image, cmap='gray')
    imgplot.set_interpolation('nearest')
    ax.xaxis.set_ticks_position('top')
    ax.yaxis.set_ticks_position('left')
    plt.show()

if __name__ == '__main__':
    img = read()

    for i in img['training']:
        show(i)
        break

    for i in img['testing']:
        show(i)
        break
<|endoftext|>


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

import os
import sys
import json
from random import randint
from bottle import route, request, response, run, template, error, default_app, HTTPResponse
from dicttoxml import dicttoxml
import pydenticon

def getIPAddress():
    if request.headers.get('Cf-Connecting-Ip') != None:
        ip = request.headers.get('Cf-Connecting-Ip')
    elif request.headers.get('X-Forwarded-For') != None:
        ip = request.headers.get('X-Forwarded-For')
    else:
        ip = request.get('REMOTE_ADDR')
    return ip

def getRequestHeaders():
    return request.headers.keys()

def isJSONResponse():
    if request.headers.get('Accept') == "application/json" \
    or request.path.endswith('.json'):
        response.content_type = 'application/json'
        return True

def isXMLResponse():
    if request.headers.get('Accept') == "application/xml" \
    or request.path.endswith('.xml'):
        response.content_type = 'application/xml'
        return True

@error(404)
def error404(error):
    response.status = 404
    response.content_type = 'text/plain'
    return 'Nothing here, sorry'

@route('/version')
def getVersion():
    dirname, filename = os.path.split(os.path.abspath(__file__))
    f = open(os.getenv('VERSION_PATH', dirname + '/.git/refs/heads/master'), 'r')
    content = f.read()
    response.content_type = 'text/plain'
    return content

@route('/icon')
@route('/icon/<height:int>')
@route('/icon/<height:int>/<width:int>')
def getIcon(height=100,width=100):
    response.content_type = 'image/png'
    colors = []
    for _ in range(2):
        colors.append("rgb(" + str(randint(1,255)) + "," + str(randint(1,255)) + "," + str(randint(1,255)) + ")")
    generator = pydenticon.Generator(8,8,foreground=colors)
    identicon = generator.generate(getIPAddress(), height, width)
    return identicon

@route('/headers')
@route('/headers.json')
@route('/headers.xml')
def headers():
    if isJSONResponse():
        content = {}
        for key in getRequestHeaders():
            content[key] = request.headers.get(key)
        return json.dumps(content)
    if isXMLResponse():
        content = {}
        for key in getRequestHeaders():
            content[key] = request.headers.get(key)
        return dicttoxml(content)
    else:
        content = ""
        for key in getRequestHeaders():
            content = content + "" + key + ": " + str(request.headers.get(key)) + "
"
        response.content_type = 'text/html'
        return content

@route('/')
@route('/ip')
@route('/ip.json')
@route('/ip.xml')
def ip():
    if isJSONResponse():
        content = {
            'ip': getIPAddress()
        }
        return json.dumps(content)
    elif isXMLResponse():
        content = {
            'ip': getIPAddress()
        }
        return dicttoxml(content)
    else:
        response.content_type = 'text/plain'
        return getIPAddress()

if __name__ == '__main__':

    application = default_app()

    serverHost = os.getenv('SERVER_HOST', 'localhost')
    serverPort = os.getenv('PORT', '5000')

    # Now we're ready, so start the server
    try:
        application.run(host=serverHost, port=serverPort)
    finally:
        exit()
<commit_msg>Added exception handling to /version<commit_after>#!/usr/bin/env python

import os
import sys
import json
from random import randint
from bottle import route, request, response, run, template, error, default_app, HTTPResponse
from dicttoxml import dicttoxml
import pydenticon

def getIPAddress():
    if request.headers.get('Cf-Connecting-Ip') != None:
        ip = request.headers.get('Cf-Connecting-Ip')
    elif request.headers.get('X-Forwarded-For') != None:
        ip = request.headers.get('X-Forwarded-For')
    else:
        ip = request.get('REMOTE_ADDR')
    return ip

def getRequestHeaders():
    return request.headers.keys()

def isJSONResponse():
    if request.headers.get('Accept') == "application/json" \
    or request.path.endswith('.json'):
        response.content_type = 'application/json'
        return True

def isXMLResponse():
    if request.headers.get('Accept') == "application/xml" \
    or request.path.endswith('.xml'):
        response.content_type = 'application/xml'
        return True

@error(404)
def error404(error):
    response.status = 404
    response.content_type = 'text/plain'
    return 'Nothing here, sorry'

@route('/version')
def getVersion():
    try:
        dirname, filename = os.path.split(os.path.abspath(__file__))
        f = open(os.getenv('VERSION_PATH', dirname + '/.git/refs/heads/master'), 'r')
        content = f.read()
        response.content_type = 'text/plain'
        return content
    except:
        return "Unable to open version file."

@route('/icon')
@route('/icon/<height:int>')
@route('/icon/<height:int>/<width:int>')
def getIcon(height=100,width=100):
    response.content_type = 'image/png'
    colors = []
    for _ in range(2):
        colors.append("rgb(" + str(randint(1,255)) + "," + str(randint(1,255)) + "," + str(randint(1,255)) + ")")
    generator = pydenticon.Generator(8,8,foreground=colors)
    identicon = generator.generate(getIPAddress(), height, width)
    return identicon

@route('/headers')
@route('/headers.json')
@route('/headers.xml')
def headers():
    if isJSONResponse():
        content = {}
        for key in getRequestHeaders():
            content[key] = request.headers.get(key)
        return json.dumps(content)
    if isXMLResponse():
        content = {}
        for key in getRequestHeaders():
            content[key] = request.headers.get(key)
        return dicttoxml(content)
    else:
        content = ""
        for key in getRequestHeaders():
            content = content + "" + key + ": " + str(request.headers.get(key)) + "
"
        response.content_type = 'text/html'
        return content

@route('/')
@route('/ip')
@route('/ip.json')
@route('/ip.xml')
def ip():
    if isJSONResponse():
        content = {
            'ip': getIPAddress()
        }
        return json.dumps(content)
    elif isXMLResponse():
        content = {
            'ip': getIPAddress()
        }
        return dicttoxml(content)
    else:
        response.content_type = 'text/plain'
        return getIPAddress()

if __name__ == '__main__':

    application = default_app()

    serverHost = os.getenv('SERVER_HOST', 'localhost')
    serverPort = os.getenv('PORT', '5000')

    # Now we're ready, so start the server
    try:
        application.run(host=serverHost, port=serverPort)
    finally:
        exit()
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
"""
HunterLab Dataset
=================

Defines the *HunterLab* illuminants dataset for the
*CIE 1931 2 Degree Standard Observer* and
*CIE 1964 10 Degree Standard Observer*.

The currently implemented data has been extracted from :cite:`HunterLab2008b`,
however you may want to use different data according to the tables given in
:cite:`HunterLab2008c`.

See Also
--------
`Illuminants Jupyter Notebook
<http://nbviewer.jupyter.org/github/colour-science/colour-notebooks/\
blob/master/notebooks/colorimetry/illuminants.ipynb>`_

References
----------
-   :cite:`HunterLab2008b` : HunterLab. (2008). Hunter L,a,b Color Scale.
    Retrieved from http://www.hunterlab.se/wp-content/uploads/2012/11/\
Hunter-L-a-b.pdf
-   :cite:`HunterLab2008c` : HunterLab. (2008). Illuminant Factors in Universal
    Software and EasyMatch Coatings. Retrieved from
    https://support.hunterlab.com/hc/en-us/article_attachments/201437785/\
an02_02.pdf
"""

from __future__ import division, unicode_literals

import numpy as np
from collections import namedtuple

from colour.utilities import CaseInsensitiveMapping

__author__ = 'Colour Developers'
__copyright__ = 'Copyright (C) 2013-2018 - Colour Developers'
__license__ = 'New BSD License - http://opensource.org/licenses/BSD-3-Clause'
__maintainer__ = 'Colour Developers'
__email__ = 'colour-science@googlegroups.com'
__status__ = 'Production'

__all__ = [
    'HunterLab_Illuminant_Specification',
    'HUNTERLAB_ILLUMINANTS_CIE_1931_2_DEGREE_STANDARD_OBSERVER_DATA',
    'HUNTERLAB_ILLUMINANTS_CIE_1931_2_DEGREE_STANDARD_OBSERVER',
    'HUNTERLAB_ILLUMINANTS_CIE_1964_10_DEGREE_STANDARD_OBSERVER_DATA',
    'HUNTERLAB_ILLUMINANTS_CIE_1964_10_DEGREE_STANDARD_OBSERVER',
    'HUNTERLAB_ILLUMINANTS'
]

HunterLab_Illuminant_Specification = namedtuple(
    'HunterLab_Illuminant_Specification', ('name', 'XYZ_n', 'K_ab'))

# yapf: disable
HUNTERLAB_ILLUMINANTS_CIE_1931_2_DEGREE_STANDARD_OBSERVER_DATA = (
    ('A', np.array([109.83, 100.00, 35.55]), np.array([185.20, 38.40])),
    ('C', np.array([98.04, 100.00, 118.11]), np.array([175.00, 70.00])),
    ('D65', np.array([95.02, 100.00, 108.82]), np.array([172.30, 67.20])),
    ('D50', np.array([96.38, 100.00, 82.45]), np.array([173.51, 58.48])),
    ('D60', np.array([95.23, 100.00, 100.86]), np.array([172.47, 64.72])),
    ('D75', np.array([94.96, 100.00, 122.53]), np.array([172.22, 71.30])),
    ('F2', np.array([98.09, 100.00, 67.53]), np.array([175.00, 52.90])),
    ('TL 4', np.array([101.40, 100.00, 65.90]), np.array([178.00, 52.30])),
    ('UL 3000', np.array([107.99, 100.00, 33.91]), np.array([183.70, 37.50])))
# yapf: enable

HUNTERLAB_ILLUMINANTS_CIE_1931_2_DEGREE_STANDARD_OBSERVER = (
    CaseInsensitiveMapping({
        x[0]: HunterLab_Illuminant_Specification(*x)
        for x in HUNTERLAB_ILLUMINANTS_CIE_1931_2_DEGREE_STANDARD_OBSERVER_DATA
    }))
"""
*Hunter L,a,b* illuminant dataset for *CIE 1931 2 Degree Standard Observer*.

References
----------
-   :cite:`HunterLab2008b`
-   :cite:`HunterLab2008c`

HUNTERLAB_ILLUMINANTS_CIE_1931_2_DEGREE_STANDARD_OBSERVER :
    CaseInsensitiveMapping
"""

# yapf: disable
HUNTERLAB_ILLUMINANTS_CIE_1964_10_DEGREE_STANDARD_OBSERVER_DATA = (
    ('A', np.array([111.16, 100.00, 35.19]), np.array([186.30, 38.20])),
    ('C', np.array([97.30, 100.00, 116.14]), np.array([174.30, 69.40])),
    ('D50', np.array([96.72, 100.00, 81.45]), np.array([173.82, 58.13])),
    ('D60', np.array([95.21, 100.00, 99.60]), np.array([172.45, 64.28])),
    ('D65', np.array([94.83, 100.00, 107.38]), np.array([172.10, 66.70])),
    ('D75', np.array([94.45, 100.00, 120.70]), np.array([171.76, 70.76])),
    ('F2', np.array([102.13, 100.00, 69.37]), np.array([178.60, 53.60])),
    ('TL 4', np.array([103.82, 100.00, 66.90]), np.array([180.10, 52.70])),
    ('UL 3000', np.array([111.12, 100.00, 35.21]), np.array([186.30, 38.20])))
# yapf: enable

HUNTERLAB_ILLUMINANTS_CIE_1964_10_DEGREE_STANDARD_OBSERVER = (
    CaseInsensitiveMapping({
        x[0]: HunterLab_Illuminant_Specification(*x)
        for x in
        HUNTERLAB_ILLUMINANTS_CIE_1964_10_DEGREE_STANDARD_OBSERVER_DATA
    }))
"""
*Hunter L,a,b* illuminant dataset for *CIE 1964 10 Degree Standard Observer*.

References
----------
-   :cite:`HunterLab2008b`
-   :cite:`HunterLab2008c`

HUNTERLAB_ILLUMINANTS_CIE_1964_10_DEGREE_STANDARD_OBSERVER :
    CaseInsensitiveMapping
"""

HUNTERLAB_ILLUMINANTS = CaseInsensitiveMapping({
    'CIE 1931 2 Degree Standard Observer':
        HUNTERLAB_ILLUMINANTS_CIE_1931_2_DEGREE_STANDARD_OBSERVER,
    'CIE 1964 10 Degree Standard Observer':
        HUNTERLAB_ILLUMINANTS_CIE_1964_10_DEGREE_STANDARD_OBSERVER
})
HUNTERLAB_ILLUMINANTS.__doc__ = """
Aggregated *Hunter L,a,b* illuminant dataset.

References
----------
-   :cite:`HunterLab2008b`
-   :cite:`HunterLab2008c`

HUNTERLAB_ILLUMINANTS : CaseInsensitiveMapping
    **{'CIE 1931 2 Degree Standard Observer',
    'CIE 1964 10 Degree Standard Observer'}**

Aliases:

-   'cie_2_1931': 'CIE 1931 2 Degree Standard Observer'
-   'cie_10_1964': 'CIE 1964 10 Degree Standard Observer'
"""
HUNTERLAB_ILLUMINANTS['cie_2_1931'] = (
    HUNTERLAB_ILLUMINANTS['CIE 1931 2 Degree Standard Observer'])
HUNTERLAB_ILLUMINANTS['cie_10_1964'] = (
    HUNTERLAB_ILLUMINANTS['CIE 1964 10 Degree Standard Observer'])
<commit_msg>Code formatting.<commit_after># -*- coding: utf-8 -*-
"""
HunterLab Dataset
=================

Defines the *HunterLab* illuminants dataset for the
*CIE 1931 2 Degree Standard Observer* and
*CIE 1964 10 Degree Standard Observer*.

The currently implemented data has been extracted from :cite:`HunterLab2008b`,
however you may want to use different data according to the tables given in
:cite:`HunterLab2008c`.

See Also
--------
`Illuminants Jupyter Notebook
<http://nbviewer.jupyter.org/github/colour-science/colour-notebooks/\
blob/master/notebooks/colorimetry/illuminants.ipynb>`_

References
----------
-   :cite:`HunterLab2008b` : HunterLab. (2008). Hunter L,a,b Color Scale.
    Retrieved from http://www.hunterlab.se/wp-content/uploads/2012/11/\
Hunter-L-a-b.pdf
-   :cite:`HunterLab2008c` : HunterLab. (2008). Illuminant Factors in Universal
    Software and EasyMatch Coatings. Retrieved from
    https://support.hunterlab.com/hc/en-us/article_attachments/201437785/\
an02_02.pdf
"""

from __future__ import division, unicode_literals

import numpy as np
from collections import namedtuple

from colour.utilities import CaseInsensitiveMapping

__author__ = 'Colour Developers'
__copyright__ = 'Copyright (C) 2013-2018 - Colour Developers'
__license__ = 'New BSD License - http://opensource.org/licenses/BSD-3-Clause'
__maintainer__ = 'Colour Developers'
__email__ = 'colour-science@googlegroups.com'
__status__ = 'Production'

__all__ = [
    'HunterLab_Illuminant_Specification',
    'HUNTERLAB_ILLUMINANTS_CIE_1931_2_DEGREE_STANDARD_OBSERVER_DATA',
    'HUNTERLAB_ILLUMINANTS_CIE_1931_2_DEGREE_STANDARD_OBSERVER',
    'HUNTERLAB_ILLUMINANTS_CIE_1964_10_DEGREE_STANDARD_OBSERVER_DATA',
    'HUNTERLAB_ILLUMINANTS_CIE_1964_10_DEGREE_STANDARD_OBSERVER',
    'HUNTERLAB_ILLUMINANTS'
]

HunterLab_Illuminant_Specification = namedtuple(
    'HunterLab_Illuminant_Specification', ('name', 'XYZ_n', 'K_ab'))

# yapf: disable
HUNTERLAB_ILLUMINANTS_CIE_1931_2_DEGREE_STANDARD_OBSERVER_DATA = (
    ('A', np.array([109.83, 100.00, 35.55]), np.array([185.20, 38.40])),
    ('C', np.array([98.04, 100.00, 118.11]), np.array([175.00, 70.00])),
    ('D50', np.array([96.38, 100.00, 82.45]), np.array([173.51, 58.48])),
    ('D60', np.array([95.23, 100.00, 100.86]), np.array([172.47, 64.72])),
    ('D65', np.array([95.02, 100.00, 108.82]), np.array([172.30, 67.20])),
    ('D75', np.array([94.96, 100.00, 122.53]), np.array([172.22, 71.30])),
    ('F2', np.array([98.09, 100.00, 67.53]), np.array([175.00, 52.90])),
    ('TL 4', np.array([101.40, 100.00, 65.90]), np.array([178.00, 52.30])),
    ('UL 3000', np.array([107.99, 100.00, 33.91]), np.array([183.70, 37.50])))
# yapf: enable

HUNTERLAB_ILLUMINANTS_CIE_1931_2_DEGREE_STANDARD_OBSERVER = (
    CaseInsensitiveMapping({
        x[0]: HunterLab_Illuminant_Specification(*x)
        for x in HUNTERLAB_ILLUMINANTS_CIE_1931_2_DEGREE_STANDARD_OBSERVER_DATA
    }))
"""
*Hunter L,a,b* illuminant dataset for *CIE 1931 2 Degree Standard Observer*.

References
----------
-   :cite:`HunterLab2008b`
-   :cite:`HunterLab2008c`

HUNTERLAB_ILLUMINANTS_CIE_1931_2_DEGREE_STANDARD_OBSERVER :
    CaseInsensitiveMapping
"""

# yapf: disable
HUNTERLAB_ILLUMINANTS_CIE_1964_10_DEGREE_STANDARD_OBSERVER_DATA = (
    ('A', np.array([111.16, 100.00, 35.19]), np.array([186.30, 38.20])),
    ('C', np.array([97.30, 100.00, 116.14]), np.array([174.30, 69.40])),
    ('D50', np.array([96.72, 100.00, 81.45]), np.array([173.82, 58.13])),
    ('D60', np.array([95.21, 100.00, 99.60]), np.array([172.45, 64.28])),
    ('D65', np.array([94.83, 100.00, 107.38]), np.array([172.10, 66.70])),
    ('D75', np.array([94.45, 100.00, 120.70]), np.array([171.76, 70.76])),
    ('F2', np.array([102.13, 100.00, 69.37]), np.array([178.60, 53.60])),
    ('TL 4', np.array([103.82, 100.00, 66.90]), np.array([180.10, 52.70])),
    ('UL 3000', np.array([111.12, 100.00, 35.21]), np.array([186.30, 38.20])))
# yapf: enable

HUNTERLAB_ILLUMINANTS_CIE_1964_10_DEGREE_STANDARD_OBSERVER = (
    CaseInsensitiveMapping({
        x[0]: HunterLab_Illuminant_Specification(*x)
        for x in
        HUNTERLAB_ILLUMINANTS_CIE_1964_10_DEGREE_STANDARD_OBSERVER_DATA
    }))
"""
*Hunter L,a,b* illuminant dataset for *CIE 1964 10 Degree Standard Observer*.

References
----------
-   :cite:`HunterLab2008b`
-   :cite:`HunterLab2008c`

HUNTERLAB_ILLUMINANTS_CIE_1964_10_DEGREE_STANDARD_OBSERVER :
    CaseInsensitiveMapping
"""

HUNTERLAB_ILLUMINANTS = CaseInsensitiveMapping({
    'CIE 1931 2 Degree Standard Observer':
        HUNTERLAB_ILLUMINANTS_CIE_1931_2_DEGREE_STANDARD_OBSERVER,
    'CIE 1964 10 Degree Standard Observer':
        HUNTERLAB_ILLUMINANTS_CIE_1964_10_DEGREE_STANDARD_OBSERVER
})
HUNTERLAB_ILLUMINANTS.__doc__ = """
Aggregated *Hunter L,a,b* illuminant dataset.

References
----------
-   :cite:`HunterLab2008b`
-   :cite:`HunterLab2008c`

HUNTERLAB_ILLUMINANTS : CaseInsensitiveMapping
    **{'CIE 1931 2 Degree Standard Observer',
    'CIE 1964 10 Degree Standard Observer'}**

Aliases:

-   'cie_2_1931': 'CIE 1931 2 Degree Standard Observer'
-   'cie_10_1964': 'CIE 1964 10 Degree Standard Observer'
"""
HUNTERLAB_ILLUMINANTS['cie_2_1931'] = (
    HUNTERLAB_ILLUMINANTS['CIE 1931 2 Degree Standard Observer'])
HUNTERLAB_ILLUMINANTS['cie_10_1964'] = (
    HUNTERLAB_ILLUMINANTS['CIE 1964 10 Degree Standard Observer'])
<|endoftext|>


<commit_before>""" Copyright 2012, 2013 UW Information Technology, University of Washington

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
"""
from django.shortcuts import render_to_response
from django.template import RequestContext
from django.conf import settings
import oauth2
import simplejson as json
import hashlib
import urllib
from django.utils.datastructures import SortedDict
from mobility.decorators import mobile_template
from django.core.exceptions import ImproperlyConfigured
from django.core.cache import cache
from django.utils.translation import ugettext as _
import types
import re

FIVE_MINUTE_CACHE = 300

@mobile_template('spacescout_web/{mobile/}app.html')
def HomeView(request, template=None):
    # The preference order is cookie, config, then some static values
    # That fallback order will also apply if the cookie campus isn't in
    # settings.
    location = None

    if hasattr(settings, "SS_LOCATIONS"):
        m = re.match(r'^/([a-z]+)', request.path)
        if m and m.group(1) in settings.SS_LOCATIONS:
            location = m.group(1)

    if location is None:
        cookies = request.COOKIES
        if "default_location" in cookies:
            cookie_value = cookies["default_location"]
            # The format of the cookie is this, urlencoded:
            # lat,long,campus,zoom
            location = urllib.unquote(cookie_value).split(',')[2]

            if not hasattr(settings, "SS_LOCATIONS"):
                location = None

            elif not location in settings.SS_LOCATIONS:
                location = None

    if location is None:
        if hasattr(settings, 'SS_DEFAULT_LOCATION'):
            location = settings.SS_DEFAULT_LOCATION

    spaces, template_values = get_campus_data(location)

    spaces = json.dumps(spaces)

    # Default to zooming in on the UW Seattle campus if no default location is set
    if hasattr(settings, 'SS_DEFAULT_LOCATION'):
        default_location = settings.SS_DEFAULT_LOCATION
        locations = settings.SS_LOCATIONS

    if (hasattr(settings, 'SS_BUILDING_CLUSTERING_ZOOM_LEVELS') and hasattr(settings, 'SS_DISTANCE_CLUSTERING_RATIO')):
        by_building_zooms = settings.SS_BUILDING_CLUSTERING_ZOOM_LEVELS
        by_distance_ratio = settings.SS_DISTANCE_CLUSTERING_RATIO
    else:
        raise ImproperlyConfigured("You need to configure your clustering constants in settings.py or local_settings.py")

    consumer = oauth2.Consumer(key=settings.SS_WEB_OAUTH_KEY, secret=settings.SS_WEB_OAUTH_SECRET)
    client = oauth2.Client(consumer)
    headers = {
        'Content-Type': 'application/json',
        'Accept': 'application/json'
        }

    if request.user and request.user.is_authenticated():
        headers["XOAUTH_USER"] = "%s" % request.user.username

    log_shared_space_reference(request, headers, client)

    buildings = json.loads(get_building_json(client))

    favorites_json = get_favorites_json(headers, client)

    # This could probably be a template tag, but didn't seem worth it for one-time use
    #TODO: hey, actually it's probably going to be a Handlebars helper and template
    buildingdict = SortedDict()
    for building in buildings:
        try:
            if not building[0] in buildingdict.keys():  # building[0] is the first letter of the string
                buildingdict[building[0]] = []

            buildingdict[building[0]].append(building)
        except:
            pass

    params = {
        'username' : request.user.username if request.user and request.user.is_authenticated() else '',
        'center_latitude': template_values['center_latitude'],
        'center_longitude': template_values['center_longitude'],
        'zoom_level': template_values['zoom_level'],
        'locations': locations,
        'default_location': default_location,
        'by_building_zooms': by_building_zooms,
        'by_distance_ratio': by_distance_ratio,
        'buildingdict': buildingdict,
        'spaces': spaces,
        'favorites_json': favorites_json,
    }

    response = render_to_response(template, params, context_instance=RequestContext(request))
    response['Cache-Control'] = 'no-cache'
    return response

def get_key_for_search_args(search_args):
    query = []
    for key, value in search_args.items():
        query.append("%s=%s" % (key, value))

    joined = "&".join(query)

    return "space_search_%s" % hashlib.sha224(joined).hexdigest()

def get_campus_data(campus):
    spaces = fetch_open_now_for_campus(campus)
    template_values = template_values_for_campus(campus)

    return spaces, template_values

def template_values_for_campus(campus):
    if campus is None:
        return {
            # Default to zooming in on the UW Seattle campus
            'center_latitude': '47.655003',
            'center_longitude': '-122.306864',
            'zoom_level': '15',
        }

    location = settings.SS_LOCATIONS[campus]
    return {
        'center_latitude': location['CENTER_LATITUDE'],
        'center_longitude': location['CENTER_LONGITUDE'],
        'zoom_level': location['ZOOM_LEVEL'],
    }

def fetch_open_now_for_campus(campus, use_cache=True, fill_cache=False, cache_period=FIVE_MINUTE_CACHE):
    if campus is None:
        # Default to zooming in on the UW Seattle campus
        center_latitude = '47.655003'
        center_longitude = '-122.306864'
        zoom_level = '15'
        distance = '500'

    else:
        location = settings.SS_LOCATIONS[campus]
        center_latitude = location['CENTER_LATITUDE']
        center_longitude = location['CENTER_LONGITUDE']
        zoom_level = location['ZOOM_LEVEL']

        if 'DISTANCE' in location:
            distance = location['DISTANCE']
        else:
            distance = '500'

    # SPOT-1832.  Making the distance far enough that center of campus to furthest spot from the center
    # can be found
    distance = 1000
    consumer = oauth2.Consumer(key=settings.SS_WEB_OAUTH_KEY, secret=settings.SS_WEB_OAUTH_SECRET)
    client = oauth2.Client(consumer)

    search_args = {
        'center_latitude': center_latitude,
        'center_longitude': center_longitude,
        'open_now': '1',
        'distance': distance,
        'limit': '0',
    }

    return get_space_json(client, search_args, use_cache, fill_cache, cache_period)

def get_space_json(client, search_args, use_cache, fill_cache, cache_period):
    # We don't want the management command that fills the cache to get
    # a cached value
    if use_cache:
        cache_key = get_key_for_search_args(search_args)

        # The cache is (hopefully) filled from the load_open_now_cache
        # management command
        cached = cache.get(cache_key)
        if cached:
            return json.loads(cached)

    values = fetch_space_json(client, search_args)

    # this needs to look like a search result

    data = json.loads(values)

    # type needs to look like what /search query returns
    for space in data:
        if 'type' in space and isinstance(space['type'], types.ListType):
            typelist = []
            for t in space['type']:
                typelist.append(_(t))

            space["type"] = ','.join(typelist)

    if fill_cache:
        cache_key = get_key_for_search_args(search_args)

        cache.set(cache_key, values, cache_period)

    return data

def fetch_space_json(client, search_args):
    query = []
    for key, value in search_args.items():
        query.append("%s=%s" % (key, value))

    url = "{0}/api/v1/spot/?{1}".format(settings.SS_WEB_SERVER_HOST, "&".join(query))
    resp, content = client.request(url, 'GET')

    if resp.status == 200:
        return content

    return '[]'

#TODO: use the new buildings view instead
def get_building_json(client):
    url = "{0}/api/v1/buildings".format(settings.SS_WEB_SERVER_HOST)
    resp, content = client.request(url, 'GET')

    if resp.status == 200:
        return content

    return '[]'

#TODO: use the favorites view instead
def get_favorites_json(headers, client):
    url = "{0}/api/v1/user/me/favorites".format(settings.SS_WEB_SERVER_HOST)

    resp, content = client.request(url, method='GET', headers=headers)

    if resp.status == 200:
        return content

    return '[]'

def log_shared_space_reference(request, headers, client):
    # log shared space references
    m = re.match(r'^/space/(\d+)/.*/([a-f0-9]{32})$', request.path)
    if m:
        try:
            url = "{0}/api/v1/spot/{1}/shared".format(settings.SS_WEB_SERVER_HOST, m.group(1))

            resp, content = client.request(url,
                                           method='PUT',
                                           body=json.dumps({ 'hash': m.group(2) }),
                                           headers=headers)

            # best effort, ignore response
        except:
            pass
<commit_msg>Join multiple space types with a comma AND whitespace to encourage better formatting/linebreaks, FIXES SPOT-2101<commit_after>""" Copyright 2012, 2013 UW Information Technology, University of Washington

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
"""
from django.shortcuts import render_to_response
from django.template import RequestContext
from django.conf import settings
import oauth2
import simplejson as json
import hashlib
import urllib
from django.utils.datastructures import SortedDict
from mobility.decorators import mobile_template
from django.core.exceptions import ImproperlyConfigured
from django.core.cache import cache
from django.utils.translation import ugettext as _
import types
import re

FIVE_MINUTE_CACHE = 300

@mobile_template('spacescout_web/{mobile/}app.html')
def HomeView(request, template=None):
    # The preference order is cookie, config, then some static values
    # That fallback order will also apply if the cookie campus isn't in
    # settings.
    location = None

    if hasattr(settings, "SS_LOCATIONS"):
        m = re.match(r'^/([a-z]+)', request.path)
        if m and m.group(1) in settings.SS_LOCATIONS:
            location = m.group(1)

    if location is None:
        cookies = request.COOKIES
        if "default_location" in cookies:
            cookie_value = cookies["default_location"]
            # The format of the cookie is this, urlencoded:
            # lat,long,campus,zoom
            location = urllib.unquote(cookie_value).split(',')[2]

            if not hasattr(settings, "SS_LOCATIONS"):
                location = None

            elif not location in settings.SS_LOCATIONS:
                location = None

    if location is None:
        if hasattr(settings, 'SS_DEFAULT_LOCATION'):
            location = settings.SS_DEFAULT_LOCATION

    spaces, template_values = get_campus_data(location)

    spaces = json.dumps(spaces)

    # Default to zooming in on the UW Seattle campus if no default location is set
    if hasattr(settings, 'SS_DEFAULT_LOCATION'):
        default_location = settings.SS_DEFAULT_LOCATION
        locations = settings.SS_LOCATIONS

    if (hasattr(settings, 'SS_BUILDING_CLUSTERING_ZOOM_LEVELS') and hasattr(settings, 'SS_DISTANCE_CLUSTERING_RATIO')):
        by_building_zooms = settings.SS_BUILDING_CLUSTERING_ZOOM_LEVELS
        by_distance_ratio = settings.SS_DISTANCE_CLUSTERING_RATIO
    else:
        raise ImproperlyConfigured("You need to configure your clustering constants in settings.py or local_settings.py")

    consumer = oauth2.Consumer(key=settings.SS_WEB_OAUTH_KEY, secret=settings.SS_WEB_OAUTH_SECRET)
    client = oauth2.Client(consumer)
    headers = {
        'Content-Type': 'application/json',
        'Accept': 'application/json'
        }

    if request.user and request.user.is_authenticated():
        headers["XOAUTH_USER"] = "%s" % request.user.username

    log_shared_space_reference(request, headers, client)

    buildings = json.loads(get_building_json(client))

    favorites_json = get_favorites_json(headers, client)

    # This could probably be a template tag, but didn't seem worth it for one-time use
    #TODO: hey, actually it's probably going to be a Handlebars helper and template
    buildingdict = SortedDict()
    for building in buildings:
        try:
            if not building[0] in buildingdict.keys():  # building[0] is the first letter of the string
                buildingdict[building[0]] = []

            buildingdict[building[0]].append(building)
        except:
            pass

    params = {
        'username' : request.user.username if request.user and request.user.is_authenticated() else '',
        'center_latitude': template_values['center_latitude'],
        'center_longitude': template_values['center_longitude'],
        'zoom_level': template_values['zoom_level'],
        'locations': locations,
        'default_location': default_location,
        'by_building_zooms': by_building_zooms,
        'by_distance_ratio': by_distance_ratio,
        'buildingdict': buildingdict,
        'spaces': spaces,
        'favorites_json': favorites_json,
    }

    response = render_to_response(template, params, context_instance=RequestContext(request))
    response['Cache-Control'] = 'no-cache'
    return response

def get_key_for_search_args(search_args):
    query = []
    for key, value in search_args.items():
        query.append("%s=%s" % (key, value))

    joined = "&".join(query)

    return "space_search_%s" % hashlib.sha224(joined).hexdigest()

def get_campus_data(campus):
    spaces = fetch_open_now_for_campus(campus)
    template_values = template_values_for_campus(campus)

    return spaces, template_values

def template_values_for_campus(campus):
    if campus is None:
        return {
            # Default to zooming in on the UW Seattle campus
            'center_latitude': '47.655003',
            'center_longitude': '-122.306864',
            'zoom_level': '15',
        }

    location = settings.SS_LOCATIONS[campus]
    return {
        'center_latitude': location['CENTER_LATITUDE'],
        'center_longitude': location['CENTER_LONGITUDE'],
        'zoom_level': location['ZOOM_LEVEL'],
    }

def fetch_open_now_for_campus(campus, use_cache=True, fill_cache=False, cache_period=FIVE_MINUTE_CACHE):
    if campus is None:
        # Default to zooming in on the UW Seattle campus
        center_latitude = '47.655003'
        center_longitude = '-122.306864'
        zoom_level = '15'
        distance = '500'

    else:
        location = settings.SS_LOCATIONS[campus]
        center_latitude = location['CENTER_LATITUDE']
        center_longitude = location['CENTER_LONGITUDE']
        zoom_level = location['ZOOM_LEVEL']

        if 'DISTANCE' in location:
            distance = location['DISTANCE']
        else:
            distance = '500'

    # SPOT-1832.  Making the distance far enough that center of campus to furthest spot from the center
    # can be found
    distance = 1000
    consumer = oauth2.Consumer(key=settings.SS_WEB_OAUTH_KEY, secret=settings.SS_WEB_OAUTH_SECRET)
    client = oauth2.Client(consumer)

    search_args = {
        'center_latitude': center_latitude,
        'center_longitude': center_longitude,
        'open_now': '1',
        'distance': distance,
        'limit': '0',
    }

    return get_space_json(client, search_args, use_cache, fill_cache, cache_period)

def get_space_json(client, search_args, use_cache, fill_cache, cache_period):
    # We don't want the management command that fills the cache to get
    # a cached value
    if use_cache:
        cache_key = get_key_for_search_args(search_args)

        # The cache is (hopefully) filled from the load_open_now_cache
        # management command
        cached = cache.get(cache_key)
        if cached:
            return json.loads(cached)

    values = fetch_space_json(client, search_args)

    # this needs to look like a search result

    data = json.loads(values)

    # type needs to look like what /search query returns
    for space in data:
        if 'type' in space and isinstance(space['type'], types.ListType):
            typelist = []
            for t in space['type']:
                typelist.append(_(t))

            space["type"] = ', '.join(typelist)

    if fill_cache:
        cache_key = get_key_for_search_args(search_args)

        cache.set(cache_key, values, cache_period)

    return data

def fetch_space_json(client, search_args):
    query = []
    for key, value in search_args.items():
        query.append("%s=%s" % (key, value))

    url = "{0}/api/v1/spot/?{1}".format(settings.SS_WEB_SERVER_HOST, "&".join(query))
    resp, content = client.request(url, 'GET')

    if resp.status == 200:
        return content

    return '[]'

#TODO: use the new buildings view instead
def get_building_json(client):
    url = "{0}/api/v1/buildings".format(settings.SS_WEB_SERVER_HOST)
    resp, content = client.request(url, 'GET')

    if resp.status == 200:
        return content

    return '[]'

#TODO: use the favorites view instead
def get_favorites_json(headers, client):
    url = "{0}/api/v1/user/me/favorites".format(settings.SS_WEB_SERVER_HOST)

    resp, content = client.request(url, method='GET', headers=headers)

    if resp.status == 200:
        return content

    return '[]'

def log_shared_space_reference(request, headers, client):
    # log shared space references
    m = re.match(r'^/space/(\d+)/.*/([a-f0-9]{32})$', request.path)
    if m:
        try:
            url = "{0}/api/v1/spot/{1}/shared".format(settings.SS_WEB_SERVER_HOST, m.group(1))

            resp, content = client.request(url,
                                           method='PUT',
                                           body=json.dumps({ 'hash': m.group(2) }),
                                           headers=headers)

            # best effort, ignore response
        except:
            pass
<|endoftext|>


<commit_before>import unittest

import bolt.tasks.bolt_shell as bsh

class TestShellExecuteTask(unittest.TestCase):

    def setUp(self):
        self.subject = ShellExecuteTaskSpy()
        return super(TestShellExecuteTask, self).setUp()

    def test_configuration_cannot_be_empty(self):
        with self.assertRaises(bsh.InvalidConfigurationError):
            self.given({})

    def test_configuration_must_contain_command(self):
        with self.assertRaises(bsh.InvalidConfigurationError):
            config = {
                'arguments': ['foo']
            }
            self.given(config)

    def test_command_line_contains_no_arguments_if_none_specified(self):
        config = {
            'command': 'ls'
        }
        self.given(config)
        self.expect_command_line(['ls'])

    def test_command_line_contains_specified_arguments(self):
        config = {
            'command': 'command',
            'arguments': ['arg1', 'arg2']
        }
        self.given(config)
        self.expect_command_line(['command', 'arg1', 'arg2'])

    def test_raises_if_command_fails(self):
        with self.assertRaises(Exception):
            config = {
                'command': 'failed',
            }
            self.given(config)

    def given(self, config):
        self.result = self.subject(config=config)

    def expect_command_line(self, expected):
        self.assertListEqual(self.subject.args, expected)

class ShellExecuteTaskSpy(bsh.ShellExecuteTask):

    def __init__(self):
        self.returncode = 0
        return super(ShellExecuteTaskSpy, self).__init__()

    def _run(self):
        self.args = self.command_line
        if self.args[0] == 'failed':
            self.returncode = 1
        self.check_returncode()

    def check_returncode(self):
        if self.returncode != 0:
            raise Exception('Failed command')

if __name__=='__main__':
    unittest.main()<commit_msg>Added additional registration test for shell task<commit_after>import unittest

import bolt.tasks.bolt_shell as bsh
import _mocks as mck

class TestShellExecuteTask(unittest.TestCase):

    def setUp(self):
        self.subject = ShellExecuteTaskSpy()
        return super(TestShellExecuteTask, self).setUp()

    def test_configuration_cannot_be_empty(self):
        with self.assertRaises(bsh.InvalidConfigurationError):
            self.given({})

    def test_configuration_must_contain_command(self):
        with self.assertRaises(bsh.InvalidConfigurationError):
            config = {
                'arguments': ['foo']
            }
            self.given(config)

    def test_command_line_contains_no_arguments_if_none_specified(self):
        config = {
            'command': 'ls'
        }
        self.given(config)
        self.expect_command_line(['ls'])

    def test_command_line_contains_specified_arguments(self):
        config = {
            'command': 'command',
            'arguments': ['arg1', 'arg2']
        }
        self.given(config)
        self.expect_command_line(['command', 'arg1', 'arg2'])

    def test_raises_if_command_fails(self):
        with self.assertRaises(Exception):
            config = {
                'command': 'failed',
            }
            self.given(config)

    def given(self, config):
        self.result = self.subject(config=config)

    def expect_command_line(self, expected):
        self.assertListEqual(self.subject.args, expected)

class ShellExecuteTaskSpy(bsh.ShellExecuteTask):

    def __init__(self):
        self.returncode = 0
        return super(ShellExecuteTaskSpy, self).__init__()

    def _run(self):
        self.args = self.command_line
        if self.args[0] == 'failed':
            self.returncode = 1
        self.check_returncode()

    def check_returncode(self):
        if self.returncode != 0:
            raise Exception('Failed command')

class TestRegisterTasks(unittest.TestCase):

    def test_registers_shell(self):
        registry = mck.TaskRegistryDouble()
        bsh.register_tasks(registry)
        self.assertTrue(registry.contains('shell'))

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


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

"""
Kay framework.

:copyright: (c) 2009 by Kay Team, see AUTHORS for more details.
:license: BSD, see LICENSE for more details.
"""

import os
import sys

__version__ = '0.1'

KAY_DIR = os.path.abspath(os.path.dirname(__file__))
PROJECT_DIR = os.path.dirname(KAY_DIR)
LIB_DIR = os.path.join(KAY_DIR, 'lib')

def setup_env(manage_py_env=False):
  """Configures app engine environment for command-line apps."""
  # Try to import the appengine code from the system path.
  try:
    from google.appengine.api import apiproxy_stub_map
  except ImportError, e:
    # Not on the system path. Build a list of alternative paths where it
    # may be. First look within the project for a local copy, then look for
    # where the Mac OS SDK installs it.
    paths = [os.path.join(PROJECT_DIR, '.google_appengine'),
             '/usr/local/google_appengine']
    for path in os.environ.get('PATH', '').replace(';', ':').split(':'):
      path = path.rstrip(os.sep)
      if path.endswith('google_appengine'):
        paths.append(path)
    if os.name in ('nt', 'dos'):
      prefix = '%(PROGRAMFILES)s' % os.environ
      paths.append(prefix + r'\Google\google_appengine')
    # Loop through all possible paths and look for the SDK dir.
    SDK_PATH = None
    for sdk_path in paths:
      sdk_path = os.path.realpath(sdk_path)
      if os.path.exists(sdk_path):
        SDK_PATH = sdk_path
        break
    if SDK_PATH is None:
      # The SDK could not be found in any known location.
      sys.stderr.write('The Google App Engine SDK could not be found!\n'
                       'Visit http://code.google.com/p/app-engine-patch/'
                       ' for installation instructions.\n')
      sys.exit(1)
    # Add the SDK and the libraries within it to the system path.
    EXTRA_PATHS = [SDK_PATH]
    lib = os.path.join(SDK_PATH, 'lib')
    # Automatically add all packages in the SDK's lib folder:
    for dir in os.listdir(lib):
      path = os.path.join(lib, dir)
      # Package can be under 'lib/<pkg>/<pkg>/' or 'lib/<pkg>/lib/<pkg>/'
      detect = (os.path.join(path, dir), os.path.join(path, 'lib', dir))
      for path in detect:
        if os.path.isdir(path):
          EXTRA_PATHS.append(os.path.dirname(path))
          break
    sys.path = EXTRA_PATHS + sys.path
    from google.appengine.api import apiproxy_stub_map
  setup()

  if not manage_py_env:
    return
  print 'Running on Kay-%s' % __version__

def setup():
  setup_syspath()
  from kay.conf import settings
  from google.appengine.ext import db
  from google.appengine.ext.db import polymodel

  class _meta(object):
    __slots__ = ('app_label', 'module_name', '_db_table', 'abstract')
    def __init__(self, model):
      try:
        self.app_label = model.__module__.split('.')[-2]
      except IndexError:
        raise ValueError('Kay expects models (here: %s.%s) to be defined in their own apps!' % (model.__module__, model.__name__))
      self.module_name = model.__name__.lower()
      self.abstract = model is db.Model

    def _set_db_table(self, db_table):
      self._db_table = db_table

    def _get_db_table(self):
      if getattr(settings, 'ADD_APP_PREFIX_TO_KIND', True):
        if hasattr(self, '_db_table'):
          return self._db_table
        return '%s_%s' % (self.app_label, self.module_name)
      return self.object_name
    db_table = property(_get_db_table, _set_db_table)

  def _initialize_model(cls):
    cls._meta = _meta(cls)

  old_propertied_class_init = db.PropertiedClass.__init__
  def __init__(cls, name, bases, attrs, map_kind=True):
    """
    Just add _meta to db.Model.
    """
    _initialize_model(cls)
    old_propertied_class_init(cls, name, bases, attrs,
                              not cls._meta.abstract)
  db.PropertiedClass.__init__ = __init__

  old_poly_init = polymodel.PolymorphicClass.__init__
  def __init__(cls, name, bases, attrs):
    if polymodel.PolyModel not in bases:
      _initialize_model(cls)
    old_poly_init(cls, name, bases, attrs)
  polymodel.PolymorphicClass.__init__ = __init__

  @classmethod
  def kind(cls):
    return cls._meta.db_table
  db.Model.kind = kind

def setup_syspath():
  if not PROJECT_DIR in sys.path:
    sys.path = [PROJECT_DIR] + sys.path
  if not LIB_DIR in sys.path:
    sys.path = [LIB_DIR] + sys.path
<commit_msg>Change the version.<commit_after># -*- coding: utf-8 -*-

"""
Kay framework.

:copyright: (c) 2009 by Kay Team, see AUTHORS for more details.
:license: BSD, see LICENSE for more details.
"""

import os
import sys

__version__ = "0.1.0b3"

KAY_DIR = os.path.abspath(os.path.dirname(__file__))
PROJECT_DIR = os.path.dirname(KAY_DIR)
LIB_DIR = os.path.join(KAY_DIR, 'lib')

def setup_env(manage_py_env=False):
  """Configures app engine environment for command-line apps."""
  # Try to import the appengine code from the system path.
  try:
    from google.appengine.api import apiproxy_stub_map
  except ImportError, e:
    # Not on the system path. Build a list of alternative paths where it
    # may be. First look within the project for a local copy, then look for
    # where the Mac OS SDK installs it.
    paths = [os.path.join(PROJECT_DIR, '.google_appengine'),
             '/usr/local/google_appengine']
    for path in os.environ.get('PATH', '').replace(';', ':').split(':'):
      path = path.rstrip(os.sep)
      if path.endswith('google_appengine'):
        paths.append(path)
    if os.name in ('nt', 'dos'):
      prefix = '%(PROGRAMFILES)s' % os.environ
      paths.append(prefix + r'\Google\google_appengine')
    # Loop through all possible paths and look for the SDK dir.
    SDK_PATH = None
    for sdk_path in paths:
      sdk_path = os.path.realpath(sdk_path)
      if os.path.exists(sdk_path):
        SDK_PATH = sdk_path
        break
    if SDK_PATH is None:
      # The SDK could not be found in any known location.
      sys.stderr.write('The Google App Engine SDK could not be found!\n'
                       'Visit http://code.google.com/p/app-engine-patch/'
                       ' for installation instructions.\n')
      sys.exit(1)
    # Add the SDK and the libraries within it to the system path.
    EXTRA_PATHS = [SDK_PATH]
    lib = os.path.join(SDK_PATH, 'lib')
    # Automatically add all packages in the SDK's lib folder:
    for dir in os.listdir(lib):
      path = os.path.join(lib, dir)
      # Package can be under 'lib/<pkg>/<pkg>/' or 'lib/<pkg>/lib/<pkg>/'
      detect = (os.path.join(path, dir), os.path.join(path, 'lib', dir))
      for path in detect:
        if os.path.isdir(path):
          EXTRA_PATHS.append(os.path.dirname(path))
          break
    sys.path = EXTRA_PATHS + sys.path
    from google.appengine.api import apiproxy_stub_map
  setup()

  if not manage_py_env:
    return
  print 'Running on Kay-%s' % __version__

def setup():
  setup_syspath()
  from kay.conf import settings
  from google.appengine.ext import db
  from google.appengine.ext.db import polymodel

  class _meta(object):
    __slots__ = ('app_label', 'module_name', '_db_table', 'abstract')
    def __init__(self, model):
      try:
        self.app_label = model.__module__.split('.')[-2]
      except IndexError:
        raise ValueError('Kay expects models (here: %s.%s) to be defined in their own apps!' % (model.__module__, model.__name__))
      self.module_name = model.__name__.lower()
      self.abstract = model is db.Model

    def _set_db_table(self, db_table):
      self._db_table = db_table

    def _get_db_table(self):
      if getattr(settings, 'ADD_APP_PREFIX_TO_KIND', True):
        if hasattr(self, '_db_table'):
          return self._db_table
        return '%s_%s' % (self.app_label, self.module_name)
      return self.object_name
    db_table = property(_get_db_table, _set_db_table)

  def _initialize_model(cls):
    cls._meta = _meta(cls)

  old_propertied_class_init = db.PropertiedClass.__init__
  def __init__(cls, name, bases, attrs, map_kind=True):
    """
    Just add _meta to db.Model.
    """
    _initialize_model(cls)
    old_propertied_class_init(cls, name, bases, attrs,
                              not cls._meta.abstract)
  db.PropertiedClass.__init__ = __init__

  old_poly_init = polymodel.PolymorphicClass.__init__
  def __init__(cls, name, bases, attrs):
    if polymodel.PolyModel not in bases:
      _initialize_model(cls)
    old_poly_init(cls, name, bases, attrs)
  polymodel.PolymorphicClass.__init__ = __init__

  @classmethod
  def kind(cls):
    return cls._meta.db_table
  db.Model.kind = kind

def setup_syspath():
  if not PROJECT_DIR in sys.path:
    sys.path = [PROJECT_DIR] + sys.path
  if not LIB_DIR in sys.path:
    sys.path = [LIB_DIR] + sys.path
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
from views import *
from flask import (Blueprint, current_app, request, g, url_for, make_response,
                   render_template, redirect, jsonify)
from models.model_test import TestUser, flush
from wsgi import app

@app.route('/style')
def style():
    return render_template('test/style.html')

@app.route('/creative')
def creative():
    return render_template('test/creative.html')

@app.route('/gray')
def gray():
    return render_template('test/gray.html')
<commit_msg>fix index<commit_after># -*- coding: utf-8 -*-
from views import *
from flask import (Blueprint, current_app, request, g, url_for, make_response,
                   render_template, redirect, jsonify)
from models.model_test import TestUser, flush
from wsgi import app

@app.route('/')
def index():
    return render_template('test/style.html')

@app.route('/style')
def style():
    return render_template('test/style.html')

@app.route('/creative')
def creative():
    return render_template('test/creative.html')

@app.route('/gray')
def gray():
    return render_template('test/gray.html')
<|endoftext|>


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

"""
Kay framework.

:Copyright: (c) 2009 Accense Technology, Inc.
                     Takashi Matsuo <tmatsuo@candit.jp>,
                     Ian Lewis <IanMLewis@gmail.com>
                     All rights reserved.
:license: BSD, see LICENSE for more details.
"""

import os
import sys

import settings

__version__ = "0.7.0"

KAY_DIR = os.path.abspath(os.path.dirname(__file__))
LIB_DIR = os.path.join(KAY_DIR, 'lib')
PROJECT_DIR = os.path.abspath(os.path.dirname(settings.__file__))
PROJECT_LIB_DIR = os.path.join(PROJECT_DIR, 'lib')

def setup_env(manage_py_env=False):
  """Configures app engine environment for command-line apps."""
  # Try to import the appengine code from the system path.
  try:
    from google.appengine.api import apiproxy_stub_map
  except ImportError, e:
    # Not on the system path. Build a list of alternative paths where it
    # may be. First look within the project for a local copy, then look for
    # where the Mac OS SDK installs it.
    paths = [os.path.join(PROJECT_DIR, '.google_appengine'),
             '/usr/local/google_appengine']
    for path in os.environ.get('PATH', '').replace(';', ':').split(':'):
      path = path.rstrip(os.sep)
      if path.endswith('google_appengine'):
        paths.append(path)
    if os.name in ('nt', 'dos'):
      prefix = '%(PROGRAMFILES)s' % os.environ
      paths.append(prefix + r'\Google\google_appengine')
    # Loop through all possible paths and look for the SDK dir.
    SDK_PATH = None
    for sdk_path in paths:
      sdk_path = os.path.realpath(sdk_path)
      if os.path.exists(sdk_path):
        SDK_PATH = sdk_path
        break
    if SDK_PATH is None:
      # The SDK could not be found in any known location.
      sys.stderr.write('The Google App Engine SDK could not be found!\n'
                       'Please visit http://kay-docs.shehas.net/'
                       ' for installation instructions.\n')
      sys.exit(1)
    # Add the SDK and the libraries within it to the system path.
    EXTRA_PATHS = [SDK_PATH]
    lib = os.path.join(SDK_PATH, 'lib')
    # Automatically add all packages in the SDK's lib folder:
    for dir in os.listdir(lib):
      path = os.path.join(lib, dir)
      # Package can be under 'lib/<pkg>/<pkg>/' or 'lib/<pkg>/lib/<pkg>/'
      detect = (os.path.join(path, dir), os.path.join(path, 'lib', dir))
      for path in detect:
        if os.path.isdir(path):
          EXTRA_PATHS.append(os.path.dirname(path))
          break
    sys.path = EXTRA_PATHS + sys.path
    # corresponds with another google package
    if sys.modules.has_key('google'):
      del sys.modules['google']
    from google.appengine.api import apiproxy_stub_map
  setup()

  if not manage_py_env:
    return
  print 'Running on Kay-%s' % __version__

def setup():
  setup_syspath()
  from kay.conf import settings
  from google.appengine.ext import db
  from google.appengine.ext.db import polymodel

  class _meta(object):
    __slots__ = ('object_name', 'app_label', 'module_name', '_db_table', 'abstract')
    def __init__(self, model):
      try:
        self.app_label = model.__module__.split('.')[-2]
      except IndexError:
        raise ValueError('Kay expects models (here: %s.%s) to be defined in their own apps!' % (model.__module__, model.__name__))
      self.module_name = model.__name__.lower()
      self.abstract = model is db.Model
      self.object_name = model.__name__

    def _set_db_table(self, db_table):
      self._db_table = db_table

    def _get_db_table(self):
      if getattr(settings, 'ADD_APP_PREFIX_TO_KIND', True):
        if hasattr(self, '_db_table'):
          return self._db_table
        return '%s_%s' % (self.app_label, self.module_name)
      return self.object_name
    db_table = property(_get_db_table, _set_db_table)

  def _initialize_model(cls):
    cls._meta = _meta(cls)

  old_propertied_class_init = db.PropertiedClass.__init__
  def __init__(cls, name, bases, attrs, map_kind=True):
    """
    Just add _meta to db.Model.
    """
    _initialize_model(cls)
    old_propertied_class_init(cls, name, bases, attrs,
                              not cls._meta.abstract)
  db.PropertiedClass.__init__ = __init__

  old_poly_init = polymodel.PolymorphicClass.__init__
  def __init__(cls, name, bases, attrs):
    if polymodel.PolyModel not in bases:
      _initialize_model(cls)
    old_poly_init(cls, name, bases, attrs)
  polymodel.PolymorphicClass.__init__ = __init__

  @classmethod
  def kind(cls):
    return cls._meta.db_table
  db.Model.kind = kind

def setup_syspath():
  if not PROJECT_DIR in sys.path:
    sys.path = [PROJECT_DIR] + sys.path
  if not LIB_DIR in sys.path:
    sys.path = [LIB_DIR] + sys.path
  if not PROJECT_LIB_DIR in sys.path:
    sys.path = [PROJECT_LIB_DIR] + sys.path
<commit_msg>Changed the version number. Allow users to define Models anywhere.<commit_after># -*- coding: utf-8 -*-

"""
Kay framework.

:Copyright: (c) 2009 Accense Technology, Inc.
                     Takashi Matsuo <tmatsuo@candit.jp>,
                     Ian Lewis <IanMLewis@gmail.com>
                     All rights reserved.
:license: BSD, see LICENSE for more details.
"""

import os
import sys
import logging

import settings

__version__ = "0.8.0"

KAY_DIR = os.path.abspath(os.path.dirname(__file__))
LIB_DIR = os.path.join(KAY_DIR, 'lib')
PROJECT_DIR = os.path.abspath(os.path.dirname(settings.__file__))
PROJECT_LIB_DIR = os.path.join(PROJECT_DIR, 'lib')

def setup_env(manage_py_env=False):
  """Configures app engine environment for command-line apps."""
  # Try to import the appengine code from the system path.
  try:
    from google.appengine.api import apiproxy_stub_map
  except ImportError, e:
    # Not on the system path. Build a list of alternative paths where it
    # may be. First look within the project for a local copy, then look for
    # where the Mac OS SDK installs it.
    paths = [os.path.join(PROJECT_DIR, '.google_appengine'),
             '/usr/local/google_appengine']
    for path in os.environ.get('PATH', '').replace(';', ':').split(':'):
      path = path.rstrip(os.sep)
      if path.endswith('google_appengine'):
        paths.append(path)
    if os.name in ('nt', 'dos'):
      prefix = '%(PROGRAMFILES)s' % os.environ
      paths.append(prefix + r'\Google\google_appengine')
    # Loop through all possible paths and look for the SDK dir.
    SDK_PATH = None
    for sdk_path in paths:
      sdk_path = os.path.realpath(sdk_path)
      if os.path.exists(sdk_path):
        SDK_PATH = sdk_path
        break
    if SDK_PATH is None:
      # The SDK could not be found in any known location.
      sys.stderr.write('The Google App Engine SDK could not be found!\n'
                       'Please visit http://kay-docs.shehas.net/'
                       ' for installation instructions.\n')
      sys.exit(1)
    # Add the SDK and the libraries within it to the system path.
    EXTRA_PATHS = [SDK_PATH]
    lib = os.path.join(SDK_PATH, 'lib')
    # Automatically add all packages in the SDK's lib folder:
    for dir in os.listdir(lib):
      path = os.path.join(lib, dir)
      # Package can be under 'lib/<pkg>/<pkg>/' or 'lib/<pkg>/lib/<pkg>/'
      detect = (os.path.join(path, dir), os.path.join(path, 'lib', dir))
      for path in detect:
        if os.path.isdir(path):
          EXTRA_PATHS.append(os.path.dirname(path))
          break
    sys.path = EXTRA_PATHS + sys.path
    # corresponds with another google package
    if sys.modules.has_key('google'):
      del sys.modules['google']
    from google.appengine.api import apiproxy_stub_map
  setup()

  if not manage_py_env:
    return
  print 'Running on Kay-%s' % __version__

def setup():
  setup_syspath()
  from kay.conf import settings
  from google.appengine.ext import db
  from google.appengine.ext.db import polymodel

  class _meta(object):
    __slots__ = ('object_name', 'app_label', 'module_name', '_db_table',
                 'abstract')
    def __init__(self, model):
      try:
        self.app_label = model.__module__.split('.')[-2]
      except IndexError:
        logging.warn('Kay expects models (here: %s.%s) to be defined in their'
                     ' own apps!' % (model.__module__, model.__name__))
        self.app_label = None
      self.module_name = model.__name__.lower()
      self.abstract = model is db.Model
      self.object_name = model.__name__

    def _set_db_table(self, db_table):
      self._db_table = db_table

    def _get_db_table(self):
      if getattr(settings, 'ADD_APP_PREFIX_TO_KIND', True):
        if hasattr(self, '_db_table'):
          return self._db_table
        return '%s_%s' % (self.app_label, self.module_name)
      return self.object_name
    db_table = property(_get_db_table, _set_db_table)

  def _initialize_model(cls):
    cls._meta = _meta(cls)

  old_propertied_class_init = db.PropertiedClass.__init__
  def __init__(cls, name, bases, attrs, map_kind=True):
    """
    Just add _meta to db.Model.
    """
    _initialize_model(cls)
    old_propertied_class_init(cls, name, bases, attrs,
                              not cls._meta.abstract)
  db.PropertiedClass.__init__ = __init__

  old_poly_init = polymodel.PolymorphicClass.__init__
  def __init__(cls, name, bases, attrs):
    if polymodel.PolyModel not in bases:
      _initialize_model(cls)
    old_poly_init(cls, name, bases, attrs)
  polymodel.PolymorphicClass.__init__ = __init__

  @classmethod
  def kind(cls):
    return cls._meta.db_table
  db.Model.kind = kind

def setup_syspath():
  if not PROJECT_DIR in sys.path:
    sys.path = [PROJECT_DIR] + sys.path
  if not LIB_DIR in sys.path:
    sys.path = [LIB_DIR] + sys.path
  if not PROJECT_LIB_DIR in sys.path:
    sys.path = [PROJECT_LIB_DIR] + sys.path
<|endoftext|>


<commit_before>import OpenPNM as op
import scipy as sp

class CubicDualTest:

    def test_generation(self):
        net = op.Network.CubicDual(shape=[5, 5, 5], label_1='primary',
                                   label_2='secondary')
        assert net.Np == 285
        assert net.Nt == 1052
        assert net.num_pores('primary') == 125
        assert net.num_pores('secondary') == 125
        assert net.num_pores('surface') == 250
        assert net.num_throats('primary') == 300
        assert net.num_throats('secondary') == 240
        assert net.num_throats('surface') == 96
        assert net.num_throats('interconnect') == 512

    def test_add_boundary_pores(self):
        net = op.Network.CubicDual(shape=[5, 5, 5], label_1='primary',
                                   label_2='secondary')
        Ps = net.pores(labels=['surface', 'bottom'], mode='intersection')
        net.add_boundary_pores(pores=Ps, offset=[0, 0, -0.5])
        Ps2 = net.pores(labels=['boundary'], mode='intersection')
        assert Ps.size == Ps2.size
        assert sp.any(sp.in1d(Ps, Ps2)) == False
<commit_msg>Cleaned up a few tests<commit_after>import OpenPNM as op
import scipy as sp

class CubicDualTest:

    def test_generation(self):
        net = op.Network.CubicDual(shape=[5, 5, 5], label_1='primary',
                                   label_2='secondary')
        assert net.Np == 285
        assert net.Nt == 1052
        assert net.num_pores('primary') == 125
        assert net.num_pores('secondary') == 160
        assert net.num_pores('surface') == 250
        assert net.num_throats('primary') == 300
        assert net.num_throats('secondary') == 240
        assert net.num_throats('surface') == 96
        assert net.num_throats('interconnect') == 512

    def test_add_boundary_pores(self):
        net = op.Network.CubicDual(shape=[5, 5, 5], label_1='primary',
                                   label_2='secondary')
        Ps = net.pores(labels=['surface', 'bottom'], mode='intersection')
        net.add_boundary_pores(pores=Ps, offset=[0, 0, -0.5])
        Ps2 = net.pores(labels=['boundary'], mode='intersection')
        assert Ps.size == Ps2.size
        assert ~sp.any(sp.in1d(Ps, Ps2))
<|endoftext|>