<commit_before>""" SINGAPORE GOV API FUNCTIONS """

import os
import json
import requests
from haversine import haversine

def connnect_gov_api(url_string):
    """ Conntect to Gov and return request object """
    data_gov_api = str(os.environ.get('DATAGOV'))
    headers = {'api-key': data_gov_api}
    request = requests.get(url_string, headers=headers)

    return request

def taxi_get(send_long, send_lat):
    """ Get Taxi Updates """
    connnect_gov_api_r = connnect_gov_api('https://api.data.gov.sg/v1/transport/taxi-availability')
    data = json.loads(connnect_gov_api_r.text)

    # Create inputs
    current = (send_long, send_lat)
    count_number = 0
    thres = 0.2

    # Count Number of Taxis <= thres
    collect_taxis = []
    for coord in data['features'][0]['geometry']['coordinates']:
        if haversine(current, coord) <= thres:
            count_number += 1
            collect_taxis.append(coord)

    if count_number > 0:

        # Convert location to string
        current_str = ''
        for x in current[::-1]:
            current_str += str(x) + ","
        current_str = current_str[:-1] + "|flag-YOU-lg"

        # Taxis!
        taxi_str = '||'
        for y in collect_taxis:
            taxi_str_t = ''
            for x in y[::-1]:
                taxi_str_t += str(x) + ","
            taxi_str += taxi_str_t[:-1] + "||"
        taxi_str = taxi_str[:-2]

        # Create Map
        key = str(os.environ.get('MAPQUEST'))

        url = "https://beta.mapquestapi.com/staticmap/v5/map?key="+ key + "&locations=" + current_str + \
            taxi_str + "&type=dark&scalebar=true|bottom&size=@2x&zoom=16&defaultMarker=circle-start-sm"

        return {'count_number': count_number, 'url':url}

    else:

        return {'count_number': count_number, 'url':None}

def traffic_get(location):
    """ Get Traffic Updates """

    connnect_gov_api_r = connnect_gov_api('https://api.data.gov.sg/v1/transport/traffic-images')
    data = json.loads(connnect_gov_api_r.text)

    if len(location) == 0:
        return 'Please enter either traffic Woodlands or traffic Tuas'
    else:
        location = str(location).upper()

    if location == 'TUAS':
        target_ = '4703' # Tuas
    elif location == 'WOODLANDS':
        target_ = '2701' # Woodlands
    else:
        return 'Sorry for now only understooded either Tuas or Woodlands!'

    # Get required data
    for data_ in data['items'][0]['cameras']:
        if (data_['camera_id']) == target_:
            img_url = data_['image']
            timestampp = data_['timestamp'][:19].replace("T", " ")

    # Create Response
    final_string = "The " + location.title() + " Checkpoint Situation at " + \
                   timestampp + " is like that la \n\n"

    final_string = final_string + 'Traffic Image!'
    return final_string

def psi3hour_get():
    """ Get Latest Singapore PSI """

    connnect_gov_api_r = connnect_gov_api('https://api.data.gov.sg/v1/environment/psi')
    data = json.loads(connnect_gov_api_r.text)

    # Load data into Dictionary and get reading
    hourly = data['items'][0]['readings']['psi_three_hourly']
    timestampp = data['items'][0]['timestamp'][:19].replace("T", " ")

    # Create Response
    final_string = "The 3 hourly PSI Reading at " + timestampp + " is actually \n\n"

    for key in sorted(hourly):
        final_string  =  final_string + (str(key) + " " + \
                                        str(hourly[key]) + "\n")
    return final_string + "\nHotspots at our neighbours there are like that!"

def weathernow_get():
    """ Get Latest Singapore Weather """
    connnect_gov_api_r = connnect_gov_api(
        'https://api.data.gov.sg/v1/environment/24-hour-weather-forecast')
    data = json.loads(connnect_gov_api_r.text)

    # Load data into Dictionary and get reading
    forecast = data['items'][0]['general']['forecast']
    high_ = data['items'][0]['general']['temperature']['high']
    low_ = data['items'][0]['general']['temperature']['low']

    # Create Response
    final_string = "In General the weather will be looking like " + forecast + \
                    " with a high of " + str(high_) + \
                    "°C and a low of " + str(low_) + "°C\n\nForecast Next 12 Hrs\n\n"

    # Add 12 hr cast
    nowcast = data['items'][0]['periods'][0]['regions']
    for key in sorted(nowcast):
        final_string  =  final_string + (str(key) + \
                                        " - " + str(nowcast[key]) + "\n")
    final_string = final_string + "\nForecast Tomorrow\n\n"

    # Add 24 hr cast
    nowcast = data['items'][0]['periods'][1]['regions']
    for key in sorted(nowcast):
        final_string  =  final_string + (str(key) + " - " + str(nowcast[key]) + "\n")

    return final_string + "\nShow you radarrrr somemore! Got colour means raining!"
<commit_msg>changed to 24hr<commit_after>""" SINGAPORE GOV API FUNCTIONS """

import os
import json
import requests
from haversine import haversine

def connnect_gov_api(url_string):
    """ Conntect to Gov and return request object """
    data_gov_api = str(os.environ.get('DATAGOV'))
    headers = {'api-key': data_gov_api}
    request = requests.get(url_string, headers=headers)

    return request

def taxi_get(send_long, send_lat):
    """ Get Taxi Updates """
    connnect_gov_api_r = connnect_gov_api('https://api.data.gov.sg/v1/transport/taxi-availability')
    data = json.loads(connnect_gov_api_r.text)

    # Create inputs
    current = (send_long, send_lat)
    count_number = 0
    thres = 0.2

    # Count Number of Taxis <= thres
    collect_taxis = []
    for coord in data['features'][0]['geometry']['coordinates']:
        if haversine(current, coord) <= thres:
            count_number += 1
            collect_taxis.append(coord)

    if count_number > 0:

        # Convert location to string
        current_str = ''
        for x in current[::-1]:
            current_str += str(x) + ","
        current_str = current_str[:-1] + "|flag-YOU-lg"

        # Taxis!
        taxi_str = '||'
        for y in collect_taxis:
            taxi_str_t = ''
            for x in y[::-1]:
                taxi_str_t += str(x) + ","
            taxi_str += taxi_str_t[:-1] + "||"
        taxi_str = taxi_str[:-2]

        # Create Map
        key = str(os.environ.get('MAPQUEST'))

        url = "https://beta.mapquestapi.com/staticmap/v5/map?key="+ key + "&locations=" + current_str + \
            taxi_str + "&type=dark&scalebar=true|bottom&size=@2x&zoom=16&defaultMarker=circle-start-sm"

        return {'count_number': count_number, 'url':url}

    else:

        return {'count_number': count_number, 'url':None}

def traffic_get(location):
    """ Get Traffic Updates """

    connnect_gov_api_r = connnect_gov_api('https://api.data.gov.sg/v1/transport/traffic-images')
    data = json.loads(connnect_gov_api_r.text)

    if len(location) == 0:
        return 'Please enter either traffic Woodlands or traffic Tuas'
    else:
        location = str(location).upper()

    if location == 'TUAS':
        target_ = '4703' # Tuas
    elif location == 'WOODLANDS':
        target_ = '2701' # Woodlands
    else:
        return 'Sorry for now only understooded either Tuas or Woodlands!'

    # Get required data
    for data_ in data['items'][0]['cameras']:
        if (data_['camera_id']) == target_:
            img_url = data_['image']
            timestampp = data_['timestamp'][:19].replace("T", " ")

    # Create Response
    final_string = "The " + location.title() + " Checkpoint Situation at " + \
                   timestampp + " is like that la \n\n"

    final_string = final_string + 'Traffic Image!'
    return final_string

def psi3hour_get():
    """ Get Latest Singapore PSI """

    connnect_gov_api_r = connnect_gov_api('https://api.data.gov.sg/v1/environment/psi')
    data = json.loads(connnect_gov_api_r.text)

    # Load data into Dictionary and get reading
    hourly = data['items'][0]['readings']['psi_twenty_four_hourly']
    timestampp = data['items'][0]['timestamp'][:19].replace("T", " ")

    # Create Response
    final_string = "The 24 hourly PSI Reading at " + timestampp + " is actually \n\n"

    for key in sorted(hourly):
        final_string  =  final_string + (str(key) + " " + \
                                        str(hourly[key]) + "\n")
    return final_string + "\nHotspots at our neighbours there are like that!"

def weathernow_get():
    """ Get Latest Singapore Weather """
    connnect_gov_api_r = connnect_gov_api(
        'https://api.data.gov.sg/v1/environment/24-hour-weather-forecast')
    data = json.loads(connnect_gov_api_r.text)

    # Load data into Dictionary and get reading
    forecast = data['items'][0]['general']['forecast']
    high_ = data['items'][0]['general']['temperature']['high']
    low_ = data['items'][0]['general']['temperature']['low']

    # Create Response
    final_string = "In General the weather will be looking like " + forecast + \
                    " with a high of " + str(high_) + \
                    "°C and a low of " + str(low_) + "°C\n\nForecast Next 12 Hrs\n\n"

    # Add 12 hr cast
    nowcast = data['items'][0]['periods'][0]['regions']
    for key in sorted(nowcast):
        final_string  =  final_string + (str(key) + \
                                        " - " + str(nowcast[key]) + "\n")
    final_string = final_string + "\nForecast Tomorrow\n\n"

    # Add 24 hr cast
    nowcast = data['items'][0]['periods'][1]['regions']
    for key in sorted(nowcast):
        final_string  =  final_string + (str(key) + " - " + str(nowcast[key]) + "\n")

    return final_string + "\nShow you radarrrr somemore! Got colour means raining!"
<|endoftext|>


<commit_before>import unittest

import numpy

from worldengine.simulations.hydrology import WatermapSimulation
from worldengine.model.world import World, Size, GenerationParameters

class TestSimulation(unittest.TestCase):

    # The hydrology simulation indirectly calls the global rng.
    # We want different implementations of _watermap
    # and internally called functions (especially random_land)
    # to show the same rng behaviour and not contamine the state of the global rng
    # should anyone else happen to rely on it.
    # This does only test that watermap leaves the rng in the state that the
    # original implementation would have left it in and that a very small sample
    # of results is the same as the original results.
    # It does not test if the implementation is actually correct.
    # In fact it is hard to tell how "correctness" of a Monte Carlo process should
    # be judged.
    # Should it ever be decided that a "correct" implementation needs fewer or
    # more calls to the rng, relative to the n parameter,
    # of course compatibility will be broken.
    # Implicitly this is tested by the blessed images but it might be useful to
    # have a finer grained picture.

    def test_watermap_rng_stabilty(self):

        seed=12345
        numpy.random.seed(seed)

        size = Size(16,8)

        ocean = numpy.fromfunction(lambda y, x: y==x, (size.height, size.width))

        percipitation = numpy.ones((size.height, size.width))

        elevation = numpy.fromfunction(lambda y, x: y*x, (size.height, size.width))

        t = numpy.zeros(5)

        w = World("watermap",  size, seed, GenerationParameters(0, 1.0, 0))
        w.ocean = ocean
        w.precipitation = (percipitation, t)
        w.elevation = (elevation, t)

        d = numpy.random.randint(0,100)
        self.assertEqual(d, 98)

        data, t = WatermapSimulation._watermap(w, 200)

        self.assertAlmostEqual(data[4,4], 0.0)
        self.assertAlmostEqual(data[3,5], 4.20750776)

        d = numpy.random.randint(0,100)
        self.assertEqual(d, 59)

    def test_random_land_returns_only_land(self):

        size = Size(100,90)

        ocean = numpy.fromfunction(lambda y, x: y>x, (size.height, size.width))

        w = World("random_land",  size, 0, GenerationParameters(0, 1.0, 0))
        w.ocean = ocean

        num_samples = 1000

        land_indices = w.random_land(num_samples)

        for i in range(0, num_samples, 2):
            self.assertFalse(ocean[land_indices[i+1],land_indices[i]])

if __name__ == '__main__':
    unittest.main()<commit_msg>more missing whitespaces<commit_after>import unittest

import numpy

from worldengine.simulations.hydrology import WatermapSimulation
from worldengine.model.world import World, Size, GenerationParameters

class TestSimulation(unittest.TestCase):

    # The hydrology simulation indirectly calls the global rng.
    # We want different implementations of _watermap
    # and internally called functions (especially random_land)
    # to show the same rng behaviour and not contamine the state of the global rng
    # should anyone else happen to rely on it.
    # This does only test that watermap leaves the rng in the state that the
    # original implementation would have left it in and that a very small sample
    # of results is the same as the original results.
    # It does not test if the implementation is actually correct.
    # In fact it is hard to tell how "correctness" of a Monte Carlo process should
    # be judged.
    # Should it ever be decided that a "correct" implementation needs fewer or
    # more calls to the rng, relative to the n parameter,
    # of course compatibility will be broken.
    # Implicitly this is tested by the blessed images but it might be useful to
    # have a finer grained picture.

    def test_watermap_rng_stabilty(self):

        seed=12345
        numpy.random.seed(seed)

        size = Size(16,8)

        ocean = numpy.fromfunction(lambda y, x: y==x, (size.height, size.width))

        percipitation = numpy.ones((size.height, size.width))

        elevation = numpy.fromfunction(lambda y, x: y*x, (size.height, size.width))

        t = numpy.zeros(5)

        w = World("watermap",  size, seed, GenerationParameters(0, 1.0, 0))
        w.ocean = ocean
        w.precipitation = (percipitation, t)
        w.elevation = (elevation, t)

        d = numpy.random.randint(0,100)
        self.assertEqual(d, 98)

        data, t = WatermapSimulation._watermap(w, 200)

        self.assertAlmostEqual(data[4,4], 0.0)
        self.assertAlmostEqual(data[3,5], 4.20750776)

        d = numpy.random.randint(0,100)
        self.assertEqual(d, 59)

    def test_random_land_returns_only_land(self):

        size = Size(100,90)

        ocean = numpy.fromfunction(lambda y, x: y>x, (size.height, size.width))

        w = World("random_land",  size, 0, GenerationParameters(0, 1.0, 0))
        w.ocean = ocean

        num_samples = 1000

        land_indices = w.random_land(num_samples)

        for i in range(0, num_samples, 2):
            self.assertFalse(ocean[land_indices[i+1],land_indices[i]])

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


<commit_before># Copyright (c) 2012 Jeppe Ledet-Pedersen <jlp@satlab.org>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

import sys
import errno
import time
import struct
import usb

class Bluebox(object):
	# USB VID/PID
	VENDOR  		= 0x1d50
	PRODUCT 		= 0x6666

	# Data Endpoints
	DATA_IN	     = (usb.util.ENDPOINT_IN  | 1)
	DATA_OUT     = (usb.util.ENDPOINT_OUT | 2)

	# RF Control
	REQUEST_REGISTER	= 0x01
	REQUEST_FREQUENCY 	= 0x02
	REQUEST_MODINDEX	= 0x03
	REQUEST_CSMA_RSSI	= 0x04
	REQUEST_POWER		= 0x05
	REQUEST_AFC		= 0x06
	REQUEST_IFBW		= 0x07
	REQUEST_TRAINING	= 0x08
	REQUEST_SYNCWORD	= 0x09
	REQUEST_RXTX_MODE	= 0x0A
	REQUEST_BITRATE		= 0x0B

	# Bootloader Control
	REQUEST_DFU		= 0xFF

	# Registers
	REGISTER_N		= 0
	REGISTER_VCO_OSC	= 1
	REGISTER_TXMOD		= 2
	REGISTER_TXRXCLK	= 3
	REGISTER_DEMOD		= 4
	REGISTER_IFFILTER	= 5
	REGISTER_IFFINECAL	= 6
	REGISTER_READBACK	= 7
	REGISTER_PWRDWN		= 8
	REGISTER_AGC		= 9
	REGISTER_AFC		= 10
	REGISTER_SWD		= 11
	REGISTER_SWDTHRESHOLD	= 12
	REGISTER_3FSK4FSK	= 13
	REGISTER_TESTDAC	= 14
	REGISTER_TESTMODE	= 15

	# Readback select
	READBACK_RSSI		= 0x0014
	READBACK_VERSION	= 0x001c
	READBACK_AFC		= 0x0016

	# Testmode values
	TESTMODE_OFF		= 0
	TESTMODE_PATTERN_CARR	= 1
	TESTMODE_PATTERN_HIGH	= 2
	TESTMODE_PATTERN_LOW	= 3
	TESTMODE_PATTERN_1010	= 4
	TESTMODE_PATTERN_PN9	= 5
	TESTMODE_PATTERN_SWD	= 6

	# Sync word
	SW_LENGTH_12BIT		= 0
	SW_LENGTH_16BIT		= 1
	SW_LENGTH_20BIT		= 2
	SW_LENGTH_24BIT		= 3
	SW_TOLERANCE_0BER	= 0
	SW_TOLERANCE_1BER	= 1
	SW_TOLERANCE_2BER	= 2
	SW_TOLERANCE_3BER	= 3

	# Data length and format
	DATALEN 		= 506
	DATAEPSIZE		= 512
	DATAFMT			= "<HHH{0}s".format(DATALEN)

	def __init__(self, wait=False):
		self.dev = usb.core.find(idVendor=self.VENDOR, idProduct=self.PRODUCT)
		if self.dev is None:
			if not wait:
				raise Exception("Device not found")
			print("waiting for device ...")
			while self.dev is None:
				time.sleep(0.1)
				self.dev = usb.core.find(idVendor=self.VENDOR, idProduct=self.PRODUCT)

		if self.dev.is_kernel_driver_active(0) is True:
			self.dev.detach_kernel_driver(0)

		self.dev.set_configuration()

		self.manufacturer = usb.util.get_string(self.dev, 100, self.dev.iManufacturer)
		self.product      = usb.util.get_string(self.dev, 100, self.dev.iProduct)
		self.serial       = usb.util.get_string(self.dev, 100, self.dev.iSerialNumber)
		self.bus          = self.dev.bus
		self.address      = self.dev.address

	def _ctrl_write(self, request, data, wValue=0, wIndex=0, timeout=1000):
		bmRequestType = usb.util.build_request_type(
					usb.util.CTRL_OUT,
					usb.util.CTRL_TYPE_CLASS,
					usb.util.CTRL_RECIPIENT_INTERFACE)
		self.dev.ctrl_transfer(bmRequestType, request, wValue, wIndex, data, timeout)

	def _ctrl_read(self, request, length, wValue=0, wIndex=0, timeout=1000):
		bmRequestType = usb.util.build_request_type(
					usb.util.CTRL_IN,
					usb.util.CTRL_TYPE_CLASS,
					usb.util.CTRL_RECIPIENT_INTERFACE)
		return self.dev.ctrl_transfer(bmRequestType, request, wValue, wIndex, length, timeout)

	def reg_read(self, reg):
		return struct.unpack("<I", self._ctrl_read(self.REQUEST_REGISTER, 4, wValue=reg))[0] & 0xffff

	def reg_write(self, reg, value):
		value = struct.pack("<I", value | reg)
		self._ctrl_write(self.REQUEST_REGISTER, value, wValue=reg)

	def set_frequency(self, freq):
		freq = struct.pack("<I", freq)
		self._ctrl_write(self.REQUEST_FREQUENCY, freq)

	def get_frequency(self):
		freq = self._ctrl_read(self.REQUEST_FREQUENCY, 4)
		freq = struct.unpack("<I", freq)[0]
		return freq

	def set_modindex(self, mod):
		mod = struct.pack("<B", mod)
		self._ctrl_write(self.REQUEST_MODINDEX, mod)

	def get_modindex(self):
		mod = self._ctrl_read(self.REQUEST_MODINDEX, 1)
		mod = struct.unpack("<B", mod)[0]
		return mod

	def set_bitrate(self, br):
		br = struct.pack("<H", br)
		self._ctrl_write(self.REQUEST_BITRATE, br)

	def get_bitrate(self):
		br = self._ctrl_read(self.REQUEST_BITRATE, 2)
		br = struct.unpack("<H", br)[0]
		return br

	def power(self, dbm):
		pass

	def ifbw(self, ifbw):
		pass

	def syncword(self, word, tol):
		pass

	def set_training(self, training):
		training = struct.pack("<B", training)
		self._ctrl_write(self.REQUEST_TRAINING, training)

	def get_training(self):
		training = self._ctrl_read(self.REQUEST_TRAINING, 1)
		training = struct.unpack("<B", training)[0]
		return training

	def set_training_ms(self, ms):
		bitrate = self.get_bitrate()
		trainbytes = (ms * bitrate) / 1000 / 8
		self.training(trainbytes)

	def get_training_ms(self):
		bitrate = self.get_bitrate()
		trainbytes = self.get_training()
		return (trainbytes * 8 * 1000) / bitrate

	def version(self):
		return self.reg_read(self.READBACK_VERSION)

	def rssi(self):
		gain_correction = (86, 0, 0, 0, 58, 38, 24, 0, 0, 0, 0, 0, 0, 0, 0, 0);
		rb = self.reg_read(self.READBACK_RSSI)
		rssi = rb & 0x7f
		gc = (rb & 0x780) >> 7
		dbm = ((rssi + gain_correction[gc]) * 0.5) - 130;
		return int(round(dbm))

	def testmode(self, mode):
		self.reg_write(self.REGISTER_TESTMODE, mode << 8)

	def tx_mode(self):
		self._ctrl_write(self.REQUEST_RXTX_MODE, None, wValue=1)

	def rx_mode(self):
		self._ctrl_write(self.REQUEST_RXTX_MODE, None, wValue=0)

	def dfu(self):
		try: self._ctrl_write(self.REQUEST_DFU, None, 0)
		except: pass

	def transmit(self, text, timeout=10000):
		if len(text) > self.DATALEN:
			raise Exception("Data too long")
		data = struct.pack(self.DATAFMT, len(text), 0, 0, text)
		self.dev.write(self.DATA_OUT, data, timeout=timeout)

	def receive(self):
		ret = None
		while ret is None:
			try:
				ret = self.dev.read(self.DATA_IN, self.DATAEPSIZE, 0, timeout=1000)
				size, progress, flags, data = struct.unpack(self.DATAFMT, ret)
				data = data[0:size]
			except usb.core.USBError as e:
				if e.errno != errno.ETIMEDOUT:
					raise
		return data
<commit_msg>bbctl: do not try to detach kernel driver on Windows<commit_after># Copyright (c) 2012 Jeppe Ledet-Pedersen <jlp@satlab.org>
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

import sys
import platform
import errno
import time
import struct
import usb

class Bluebox(object):
	# USB VID/PID
	VENDOR  		= 0x1d50
	PRODUCT 		= 0x6666

	# Data Endpoints
	DATA_IN	     = (usb.util.ENDPOINT_IN  | 1)
	DATA_OUT     = (usb.util.ENDPOINT_OUT | 2)

	# RF Control
	REQUEST_REGISTER	= 0x01
	REQUEST_FREQUENCY 	= 0x02
	REQUEST_MODINDEX	= 0x03
	REQUEST_CSMA_RSSI	= 0x04
	REQUEST_POWER		= 0x05
	REQUEST_AFC		= 0x06
	REQUEST_IFBW		= 0x07
	REQUEST_TRAINING	= 0x08
	REQUEST_SYNCWORD	= 0x09
	REQUEST_RXTX_MODE	= 0x0A
	REQUEST_BITRATE		= 0x0B

	# Bootloader Control
	REQUEST_DFU		= 0xFF

	# Registers
	REGISTER_N		= 0
	REGISTER_VCO_OSC	= 1
	REGISTER_TXMOD		= 2
	REGISTER_TXRXCLK	= 3
	REGISTER_DEMOD		= 4
	REGISTER_IFFILTER	= 5
	REGISTER_IFFINECAL	= 6
	REGISTER_READBACK	= 7
	REGISTER_PWRDWN		= 8
	REGISTER_AGC		= 9
	REGISTER_AFC		= 10
	REGISTER_SWD		= 11
	REGISTER_SWDTHRESHOLD	= 12
	REGISTER_3FSK4FSK	= 13
	REGISTER_TESTDAC	= 14
	REGISTER_TESTMODE	= 15

	# Readback select
	READBACK_RSSI		= 0x0014
	READBACK_VERSION	= 0x001c
	READBACK_AFC		= 0x0016

	# Testmode values
	TESTMODE_OFF		= 0
	TESTMODE_PATTERN_CARR	= 1
	TESTMODE_PATTERN_HIGH	= 2
	TESTMODE_PATTERN_LOW	= 3
	TESTMODE_PATTERN_1010	= 4
	TESTMODE_PATTERN_PN9	= 5
	TESTMODE_PATTERN_SWD	= 6

	# Sync word
	SW_LENGTH_12BIT		= 0
	SW_LENGTH_16BIT		= 1
	SW_LENGTH_20BIT		= 2
	SW_LENGTH_24BIT		= 3
	SW_TOLERANCE_0BER	= 0
	SW_TOLERANCE_1BER	= 1
	SW_TOLERANCE_2BER	= 2
	SW_TOLERANCE_3BER	= 3

	# Data length and format
	DATALEN 		= 506
	DATAEPSIZE		= 512
	DATAFMT			= "<HHH{0}s".format(DATALEN)

	def __init__(self, wait=False):
		self.dev = usb.core.find(idVendor=self.VENDOR, idProduct=self.PRODUCT)
		if self.dev is None:
			if not wait:
				raise Exception("Device not found")
			print("waiting for device ...")
			while self.dev is None:
				time.sleep(0.1)
				self.dev = usb.core.find(idVendor=self.VENDOR, idProduct=self.PRODUCT)

		if platform.system() != "Windows" and  self.dev.is_kernel_driver_active(0) is True:
			self.dev.detach_kernel_driver(0)

		self.dev.set_configuration()

		self.manufacturer = usb.util.get_string(self.dev, 100, self.dev.iManufacturer)
		self.product      = usb.util.get_string(self.dev, 100, self.dev.iProduct)
		self.serial       = usb.util.get_string(self.dev, 100, self.dev.iSerialNumber)
		self.bus          = self.dev.bus
		self.address      = self.dev.address

	def _ctrl_write(self, request, data, wValue=0, wIndex=0, timeout=1000):
		bmRequestType = usb.util.build_request_type(
					usb.util.CTRL_OUT,
					usb.util.CTRL_TYPE_CLASS,
					usb.util.CTRL_RECIPIENT_INTERFACE)
		self.dev.ctrl_transfer(bmRequestType, request, wValue, wIndex, data, timeout)

	def _ctrl_read(self, request, length, wValue=0, wIndex=0, timeout=1000):
		bmRequestType = usb.util.build_request_type(
					usb.util.CTRL_IN,
					usb.util.CTRL_TYPE_CLASS,
					usb.util.CTRL_RECIPIENT_INTERFACE)
		return self.dev.ctrl_transfer(bmRequestType, request, wValue, wIndex, length, timeout)

	def reg_read(self, reg):
		return struct.unpack("<I", self._ctrl_read(self.REQUEST_REGISTER, 4, wValue=reg))[0] & 0xffff

	def reg_write(self, reg, value):
		value = struct.pack("<I", value | reg)
		self._ctrl_write(self.REQUEST_REGISTER, value, wValue=reg)

	def set_frequency(self, freq):
		freq = struct.pack("<I", freq)
		self._ctrl_write(self.REQUEST_FREQUENCY, freq)

	def get_frequency(self):
		freq = self._ctrl_read(self.REQUEST_FREQUENCY, 4)
		freq = struct.unpack("<I", freq)[0]
		return freq

	def set_modindex(self, mod):
		mod = struct.pack("<B", mod)
		self._ctrl_write(self.REQUEST_MODINDEX, mod)

	def get_modindex(self):
		mod = self._ctrl_read(self.REQUEST_MODINDEX, 1)
		mod = struct.unpack("<B", mod)[0]
		return mod

	def set_bitrate(self, br):
		br = struct.pack("<H", br)
		self._ctrl_write(self.REQUEST_BITRATE, br)

	def get_bitrate(self):
		br = self._ctrl_read(self.REQUEST_BITRATE, 2)
		br = struct.unpack("<H", br)[0]
		return br

	def power(self, dbm):
		pass

	def ifbw(self, ifbw):
		pass

	def syncword(self, word, tol):
		pass

	def set_training(self, training):
		training = struct.pack("<B", training)
		self._ctrl_write(self.REQUEST_TRAINING, training)

	def get_training(self):
		training = self._ctrl_read(self.REQUEST_TRAINING, 1)
		training = struct.unpack("<B", training)[0]
		return training

	def set_training_ms(self, ms):
		bitrate = self.get_bitrate()
		trainbytes = (ms * bitrate) / 1000 / 8
		self.training(trainbytes)

	def get_training_ms(self):
		bitrate = self.get_bitrate()
		trainbytes = self.get_training()
		return (trainbytes * 8 * 1000) / bitrate

	def version(self):
		return self.reg_read(self.READBACK_VERSION)

	def rssi(self):
		gain_correction = (86, 0, 0, 0, 58, 38, 24, 0, 0, 0, 0, 0, 0, 0, 0, 0);
		rb = self.reg_read(self.READBACK_RSSI)
		rssi = rb & 0x7f
		gc = (rb & 0x780) >> 7
		dbm = ((rssi + gain_correction[gc]) * 0.5) - 130;
		return int(round(dbm))

	def testmode(self, mode):
		self.reg_write(self.REGISTER_TESTMODE, mode << 8)

	def tx_mode(self):
		self._ctrl_write(self.REQUEST_RXTX_MODE, None, wValue=1)

	def rx_mode(self):
		self._ctrl_write(self.REQUEST_RXTX_MODE, None, wValue=0)

	def dfu(self):
		try: self._ctrl_write(self.REQUEST_DFU, None, 0)
		except: pass

	def transmit(self, text, timeout=10000):
		if len(text) > self.DATALEN:
			raise Exception("Data too long")
		data = struct.pack(self.DATAFMT, len(text), 0, 0, text)
		self.dev.write(self.DATA_OUT, data, timeout=timeout)

	def receive(self):
		ret = None
		while ret is None:
			try:
				ret = self.dev.read(self.DATA_IN, self.DATAEPSIZE, 0, timeout=1000)
				size, progress, flags, data = struct.unpack(self.DATAFMT, ret)
				data = data[0:size]
			except usb.core.USBError as e:
				if e.errno != errno.ETIMEDOUT:
					raise
		return data
<|endoftext|>


<commit_before>#!/usr/bin/env python
#-*- coding: utf-8 -*-

"""
Packages to use across bot programs
"""

import discord
from discord.ext import commands
import asyncio
from sys import argv
from subprocess import getstatusoutput
from time import strftime, gmtime
from os import mkdir
from os.path import isdir, join

# Bot local data information
BOT_FOLDER = "botdata"

# shortcut to unpack a syscall from getstatusoutput
call = lambda p: getstatusoutput(p)[1]

def setup_bot_data(bot_name, logger):
    """
    Set up the bot data folder in the root location
    If if can't create the folders, return False
    else return True (so our bot can continue)
    """
    try:
        if not isdir(BOT_FOLDER):
            logger("Setting up root bot folder")
            mkdir(BOT_FOLDER)
        if not isdir(join(BOT_FOLDER, bot_name)):
            logger("Setting up {} data folder")
            mkdir(join(BOT_FOLDER, bot_name))
    except Exception as ex:
        logger("Fail: {}".format(ex))
        return False
    return True

def read_key(filename):
    """Read a key file which contains bot tokens"""
    with open(filename, 'r') as f:
        return f.read()

def pre_text(string):
    return "```{}```".format(string)

def create_logger(bot_name):
    """
    This sets up a logger to be used in the bot threading
    this ambiguates bot output as to see which bot crashes
    rather than output being boggled together

    [dumb-bot: 10:32:48] I crashed
    [another-bot: 10:32:49] I didn't
    """
    def log_func(data_string):
        print("[{}: {}] {}".format(bot_name, strftime("%H:%M:%S", gmtime()), data_string))
    return log_func

<commit_msg>Fixing string error<commit_after>#!/usr/bin/env python
#-*- coding: utf-8 -*-

"""
Packages to use across bot programs
"""

import discord
from discord.ext import commands
import asyncio
from sys import argv
from subprocess import getstatusoutput
from time import strftime, gmtime
from os import mkdir
from os.path import isdir, join

# Bot local data information
BOT_FOLDER = "botdata"

# shortcut to unpack a syscall from getstatusoutput
call = lambda p: getstatusoutput(p)[1]

def setup_bot_data(bot_name, logger):
    """
    Set up the bot data folder in the root location
    If if can't create the folders, return False
    else return True (so our bot can continue)
    """
    try:
        if not isdir(BOT_FOLDER):
            logger("Setting up root bot folder")
            mkdir(BOT_FOLDER)
        if not isdir(join(BOT_FOLDER, bot_name)):
            logger("Setting up {} data folder".format(bot_name))
            mkdir(join(BOT_FOLDER, bot_name))
    except Exception as ex:
        logger("Fail: {}".format(ex))
        return False
    return True

def read_key(filename):
    """Read a key file which contains bot tokens"""
    with open(filename, 'r') as f:
        return f.read()

def pre_text(string):
    return "```{}```".format(string)

def create_logger(bot_name):
    """
    This sets up a logger to be used in the bot threading
    this ambiguates bot output as to see which bot crashes
    rather than output being boggled together

    [dumb-bot: 10:32:48] I crashed
    [another-bot: 10:32:49] I didn't
    """
    def log_func(data_string):
        print("[{}: {}] {}".format(bot_name, strftime("%H:%M:%S", gmtime()), data_string))
    return log_func

<|endoftext|>


<commit_before>
import uuid
from rl_coach.orchestrators.deploy import Deploy, DeployParameters
from kubernetes import client, config

class KubernetesParameters(DeployParameters):

    def __init__(self, name: str, image: str, command: list(), arguments: list() = list(),  synchronized: bool = False,
                 num_workers: int = 1, kubeconfig: str = None, namespace: str = None, redis_ip: str = None,
                 redis_port: int = None, redis_db: int = 0, nfs_server: str = None, nfs_path: str = None,
                 checkpoint_dir: str = '/checkpoint'):
        self.image = image
        self.synchronized = synchronized
        self.command = command
        self.arguments = arguments
        self.kubeconfig = kubeconfig
        self.num_workers = num_workers
        self.namespace = namespace
        self.redis_ip = redis_ip
        self.redis_port = redis_port
        self.redis_db = redis_db
        self.nfs_server = nfs_server
        self.nfs_path = nfs_path
        self.checkpoint_dir = checkpoint_dir
        self.name = name

class Kubernetes(Deploy):

    def __init__(self, deploy_parameters: KubernetesParameters):
        super().__init__(deploy_parameters)
        self.deploy_parameters = deploy_parameters
        if self.deploy_parameters.kubeconfig:
            config.load_kube_config()
        else:
            config.load_incluster_config()

        if not self.deploy_parameters.namespace:
            _, current_context = config.list_kube_config_contexts()
            self.deploy_parameters.namespace = current_context['context']['namespace']
        self.nfs_pvc_name = 'nfs-checkpoint-pvc'

    def setup(self) -> bool:

        if not self.deploy_parameters.redis_ip:
            # Need to spin up a redis service and a deployment.
            if not self.deploy_redis():
                print("Failed to setup redis")
                return False

        if not self.create_nfs_resources():
            return False

        return True

    def create_nfs_resources(self):
        persistent_volume = client.V1PersistentVolume(
            api_version="v1",
            kind="PersistentVolume",
            metadata=client.V1ObjectMeta(
                name='nfs-checkpoint-pv',
                labels={'app': 'nfs-checkpoint-pv'}
            ),
            spec=client.V1PersistentVolumeSpec(
                access_modes=["ReadWriteMany"],
                nfs=client.V1NFSVolumeSource(
                    path=self.deploy_parameters.nfs_path,
                    server=self.deploy_parameters.nfs_server
                ),
                capacity={'storage': '10Gi'},
                storage_class_name=""
            )
        )
        api_client = client.CoreV1Api()
        try:
            api_client.create_persistent_volume(persistent_volume)
        except client.rest.ApiException as e:
            print("Got exception: %s\n while creating the NFS PV", e)
            return False

        persistent_volume_claim = client.V1PersistentVolumeClaim(
            api_version="v1",
            kind="PersistentVolumeClaim",
            metadata=client.V1ObjectMeta(
                name="nfs-checkpoint-pvc"
            ),
            spec=client.V1PersistentVolumeClaimSpec(
                access_modes=["ReadWriteMany"],
                resources=client.V1ResourceRequirements(
                    requests={'storage': '10Gi'}
                ),
                selector=client.V1LabelSelector(
                    match_labels={'app': 'nfs-checkpoint-pv'}
                ),
                storage_class_name=""
            )
        )

        try:
            api_client.create_namespaced_persistent_volume_claim(self.deploy_parameters.namespace, persistent_volume_claim)
        except client.rest.ApiException as e:
            print("Got exception: %s\n while creating the NFS PVC", e)
            return False
        return True

    def deploy_redis(self) -> bool:
        container = client.V1Container(
            name="redis-server",
            image='redis:4-alpine',
        )
        template = client.V1PodTemplateSpec(
            metadata=client.V1ObjectMeta(labels={'app': 'redis-server'}),
            spec=client.V1PodSpec(
                containers=[container]
            )
        )
        deployment_spec = client.V1DeploymentSpec(
            replicas=1,
            template=template,
            selector=client.V1LabelSelector(
                match_labels={'app': 'redis-server'}
            )
        )

        deployment = client.V1Deployment(
            api_version='apps/v1',
            kind='Deployment',
            metadata=client.V1ObjectMeta(name='redis-server', labels={'app': 'redis-server'}),
            spec=deployment_spec
        )

        api_client = client.AppsV1Api()
        try:
            api_client.create_namespaced_deployment(self.deploy_parameters.namespace, deployment)
        except client.rest.ApiException as e:
            print("Got exception: %s\n while creating redis-server", e)
            return False

        core_v1_api = client.CoreV1Api()

        service = client.V1Service(
            api_version='v1',
            kind='Service',
            metadata=client.V1ObjectMeta(
                name='redis-service'
            ),
            spec=client.V1ServiceSpec(
                selector={'app': 'redis-server'},
                ports=[client.V1ServicePort(
                    protocol='TCP',
                    port=6379,
                    target_port=6379
                )]
            )
        )

        try:
            core_v1_api.create_namespaced_service(self.deploy_parameters.namespace, service)
            self.deploy_parameters.redis_ip = 'redis-service.{}.svc'.format(self.deploy_parameters.namespace)
            self.deploy_parameters.redis_port = 6379
            return True
        except client.rest.ApiException as e:
            print("Got exception: %s\n while creating a service for redis-server", e)
            return False

    def deploy(self) -> bool:

        self.deploy_parameters.command += ['--redis_ip', self.deploy_parameters.redis_ip, '--redis_port', '{}'.format(self.deploy_parameters.redis_port)]

        if self.deploy_parameters.synchronized:
            return self.create_k8s_deployment()
        else:
            return self.create_k8s_job()

    def create_k8s_deployment(self) -> bool:
        name = "{}-{}".format(self.deploy_parameters.name, uuid.uuid4())

        container = client.V1Container(
            name=name,
            image=self.deploy_parameters.image,
            command=self.deploy_parameters.command,
            args=self.deploy_parameters.arguments,
            image_pull_policy='Always',
            volume_mounts=[client.V1VolumeMount(
                name='nfs-pvc',
                mount_path=self.deploy_parameters.checkpoint_dir
            )]
        )
        template = client.V1PodTemplateSpec(
            metadata=client.V1ObjectMeta(labels={'app': name}),
            spec=client.V1PodSpec(
                containers=[container],
                volumes=[client.V1Volume(
                    name="nfs-pvc",
                    persistent_volume_claim=client.V1PersistentVolumeClaimVolumeSource(
                        claim_name=self.nfs_pvc_name
                    )
                )]
            ),
        )
        deployment_spec = client.V1DeploymentSpec(
            replicas=self.deploy_parameters.num_workers,
            template=template,
            selector=client.V1LabelSelector(
                match_labels={'app': name}
            )
        )

        deployment = client.V1Deployment(
            api_version='apps/v1',
            kind='Deployment',
            metadata=client.V1ObjectMeta(name=name),
            spec=deployment_spec
        )

        api_client = client.AppsV1Api()
        try:
            api_client.create_namespaced_deployment(self.deploy_parameters.namespace, deployment)
            return True
        except client.rest.ApiException as e:
            print("Got exception: %s\n while creating deployment", e)
            return False

    def create_k8s_job(self):
        name = "{}-{}".format(self.deploy_parameters.name, uuid.uuid4())

        container = client.V1Container(
            name=name,
            image=self.deploy_parameters.image,
            command=self.deploy_parameters.command,
            args=self.deploy_parameters.arguments,
            image_pull_policy='Always',
            volume_mounts=[client.V1VolumeMount(
                name='nfs-pvc',
                mount_path=self.deploy_parameters.checkpoint_dir
            )]
        )
        template = client.V1PodTemplateSpec(
            metadata=client.V1ObjectMeta(labels={'app': name}),
            spec=client.V1PodSpec(
                containers=[container],
                volumes=[client.V1Volume(
                    name="nfs-pvc",
                    persistent_volume_claim=client.V1PersistentVolumeClaimVolumeSource(
                        claim_name=self.nfs_pvc_name
                    )
                )],
                restart_policy='Never'
            ),
        )

        job_spec = client.V1JobSpec(
            parallelism=self.deploy_parameters.num_workers,
            template=template,
            completions=2147483647
        )

        job = client.V1Job(
            api_version='batch/v1',
            kind='Job',
            metadata=client.V1ObjectMeta(name=name),
            spec=job_spec
        )

        api_client = client.BatchV1Api()
        try:
            api_client.create_namespaced_job(self.deploy_parameters.namespace, job)
            return True
        except client.rest.ApiException as e:
            print("Got exception: %s\n while creating deployment", e)
            return False
<commit_msg>add support for running kubernetes orchestrator from behind proxy<commit_after>import os
import uuid
from rl_coach.orchestrators.deploy import Deploy, DeployParameters
from kubernetes import client, config

class KubernetesParameters(DeployParameters):

    def __init__(self, name: str, image: str, command: list(), arguments: list() = list(),  synchronized: bool = False,
                 num_workers: int = 1, kubeconfig: str = None, namespace: str = None, redis_ip: str = None,
                 redis_port: int = None, redis_db: int = 0, nfs_server: str = None, nfs_path: str = None,
                 checkpoint_dir: str = '/checkpoint'):
        self.image = image
        self.synchronized = synchronized
        self.command = command
        self.arguments = arguments
        self.kubeconfig = kubeconfig
        self.num_workers = num_workers
        self.namespace = namespace
        self.redis_ip = redis_ip
        self.redis_port = redis_port
        self.redis_db = redis_db
        self.nfs_server = nfs_server
        self.nfs_path = nfs_path
        self.checkpoint_dir = checkpoint_dir
        self.name = name

class Kubernetes(Deploy):

    def __init__(self, deploy_parameters: KubernetesParameters):
        super().__init__(deploy_parameters)
        self.deploy_parameters = deploy_parameters
        if self.deploy_parameters.kubeconfig:
            config.load_kube_config()
        else:
            config.load_incluster_config()

        if not self.deploy_parameters.namespace:
            _, current_context = config.list_kube_config_contexts()
            self.deploy_parameters.namespace = current_context['context']['namespace']
        self.nfs_pvc_name = 'nfs-checkpoint-pvc'

        if os.environ.get('http_proxy'):
            client.Configuration._default.proxy = os.environ.get('http_proxy')

    def setup(self) -> bool:

        if not self.deploy_parameters.redis_ip:
            # Need to spin up a redis service and a deployment.
            if not self.deploy_redis():
                print("Failed to setup redis")
                return False

        if not self.create_nfs_resources():
            return False

        return True

    def create_nfs_resources(self):
        persistent_volume = client.V1PersistentVolume(
            api_version="v1",
            kind="PersistentVolume",
            metadata=client.V1ObjectMeta(
                name='nfs-checkpoint-pv',
                labels={'app': 'nfs-checkpoint-pv'}
            ),
            spec=client.V1PersistentVolumeSpec(
                access_modes=["ReadWriteMany"],
                nfs=client.V1NFSVolumeSource(
                    path=self.deploy_parameters.nfs_path,
                    server=self.deploy_parameters.nfs_server
                ),
                capacity={'storage': '10Gi'},
                storage_class_name=""
            )
        )
        api_client = client.CoreV1Api()
        try:
            api_client.create_persistent_volume(persistent_volume)
        except client.rest.ApiException as e:
            print("Got exception: %s\n while creating the NFS PV", e)
            return False

        persistent_volume_claim = client.V1PersistentVolumeClaim(
            api_version="v1",
            kind="PersistentVolumeClaim",
            metadata=client.V1ObjectMeta(
                name="nfs-checkpoint-pvc"
            ),
            spec=client.V1PersistentVolumeClaimSpec(
                access_modes=["ReadWriteMany"],
                resources=client.V1ResourceRequirements(
                    requests={'storage': '10Gi'}
                ),
                selector=client.V1LabelSelector(
                    match_labels={'app': 'nfs-checkpoint-pv'}
                ),
                storage_class_name=""
            )
        )

        try:
            api_client.create_namespaced_persistent_volume_claim(self.deploy_parameters.namespace, persistent_volume_claim)
        except client.rest.ApiException as e:
            print("Got exception: %s\n while creating the NFS PVC", e)
            return False
        return True

    def deploy_redis(self) -> bool:
        container = client.V1Container(
            name="redis-server",
            image='redis:4-alpine',
        )
        template = client.V1PodTemplateSpec(
            metadata=client.V1ObjectMeta(labels={'app': 'redis-server'}),
            spec=client.V1PodSpec(
                containers=[container]
            )
        )
        deployment_spec = client.V1DeploymentSpec(
            replicas=1,
            template=template,
            selector=client.V1LabelSelector(
                match_labels={'app': 'redis-server'}
            )
        )

        deployment = client.V1Deployment(
            api_version='apps/v1',
            kind='Deployment',
            metadata=client.V1ObjectMeta(name='redis-server', labels={'app': 'redis-server'}),
            spec=deployment_spec
        )

        api_client = client.AppsV1Api()
        try:
            api_client.create_namespaced_deployment(self.deploy_parameters.namespace, deployment)
        except client.rest.ApiException as e:
            print("Got exception: %s\n while creating redis-server", e)
            return False

        core_v1_api = client.CoreV1Api()

        service = client.V1Service(
            api_version='v1',
            kind='Service',
            metadata=client.V1ObjectMeta(
                name='redis-service'
            ),
            spec=client.V1ServiceSpec(
                selector={'app': 'redis-server'},
                ports=[client.V1ServicePort(
                    protocol='TCP',
                    port=6379,
                    target_port=6379
                )]
            )
        )

        try:
            core_v1_api.create_namespaced_service(self.deploy_parameters.namespace, service)
            self.deploy_parameters.redis_ip = 'redis-service.{}.svc'.format(self.deploy_parameters.namespace)
            self.deploy_parameters.redis_port = 6379
            return True
        except client.rest.ApiException as e:
            print("Got exception: %s\n while creating a service for redis-server", e)
            return False

    def deploy(self) -> bool:

        self.deploy_parameters.command += ['--redis_ip', self.deploy_parameters.redis_ip, '--redis_port', '{}'.format(self.deploy_parameters.redis_port)]

        if self.deploy_parameters.synchronized:
            return self.create_k8s_deployment()
        else:
            return self.create_k8s_job()

    def create_k8s_deployment(self) -> bool:
        name = "{}-{}".format(self.deploy_parameters.name, uuid.uuid4())

        container = client.V1Container(
            name=name,
            image=self.deploy_parameters.image,
            command=self.deploy_parameters.command,
            args=self.deploy_parameters.arguments,
            image_pull_policy='Always',
            volume_mounts=[client.V1VolumeMount(
                name='nfs-pvc',
                mount_path=self.deploy_parameters.checkpoint_dir
            )]
        )
        template = client.V1PodTemplateSpec(
            metadata=client.V1ObjectMeta(labels={'app': name}),
            spec=client.V1PodSpec(
                containers=[container],
                volumes=[client.V1Volume(
                    name="nfs-pvc",
                    persistent_volume_claim=client.V1PersistentVolumeClaimVolumeSource(
                        claim_name=self.nfs_pvc_name
                    )
                )]
            ),
        )
        deployment_spec = client.V1DeploymentSpec(
            replicas=self.deploy_parameters.num_workers,
            template=template,
            selector=client.V1LabelSelector(
                match_labels={'app': name}
            )
        )

        deployment = client.V1Deployment(
            api_version='apps/v1',
            kind='Deployment',
            metadata=client.V1ObjectMeta(name=name),
            spec=deployment_spec
        )

        api_client = client.AppsV1Api()
        try:
            api_client.create_namespaced_deployment(self.deploy_parameters.namespace, deployment)
            return True
        except client.rest.ApiException as e:
            print("Got exception: %s\n while creating deployment", e)
            return False

    def create_k8s_job(self):
        name = "{}-{}".format(self.deploy_parameters.name, uuid.uuid4())

        container = client.V1Container(
            name=name,
            image=self.deploy_parameters.image,
            command=self.deploy_parameters.command,
            args=self.deploy_parameters.arguments,
            image_pull_policy='Always',
            volume_mounts=[client.V1VolumeMount(
                name='nfs-pvc',
                mount_path=self.deploy_parameters.checkpoint_dir
            )]
        )
        template = client.V1PodTemplateSpec(
            metadata=client.V1ObjectMeta(labels={'app': name}),
            spec=client.V1PodSpec(
                containers=[container],
                volumes=[client.V1Volume(
                    name="nfs-pvc",
                    persistent_volume_claim=client.V1PersistentVolumeClaimVolumeSource(
                        claim_name=self.nfs_pvc_name
                    )
                )],
                restart_policy='Never'
            ),
        )

        job_spec = client.V1JobSpec(
            parallelism=self.deploy_parameters.num_workers,
            template=template,
            completions=2147483647
        )

        job = client.V1Job(
            api_version='batch/v1',
            kind='Job',
            metadata=client.V1ObjectMeta(name=name),
            spec=job_spec
        )

        api_client = client.BatchV1Api()
        try:
            api_client.create_namespaced_job(self.deploy_parameters.namespace, job)
            return True
        except client.rest.ApiException as e:
            print("Got exception: %s\n while creating deployment", e)
            return False
<|endoftext|>


<commit_before>import pyrebase
import settings

CONNECTION = None

def connect():
    global CONNECTION

    if CONNECTION is None:
        CONNECTION = pyrebase.initialize_app(settings.FIREBASE)
    return CONNECTION

def get_default_item():
    return {
        'name': '',
        'quantity': '',
        'user': None,
    }

def add(team, listname, item):
    db = connect().database()

    team = db.child('teams').child(team)
    l = db.child('lists').child(listname)
    l.push(item)

def list_(team, listname):
    db = connect().database()

    return db.child('teams').child(team).child('lists').child(listname).get().val()

def item_exists(name, team, listname):
    items = list_(team, listname)

    if items is not None:
        for _, x in items.iteritems():
            if x['name'] == name:
                return x
    return None
>>>>>>> master

def clear(team, list_):
    db = connect().database()
    return db.child('teams').child(team).child('lists').child(list_).remove()
<commit_msg>fix syntax error<commit_after>import pyrebase
import settings

CONNECTION = None

def connect():
    global CONNECTION

    if CONNECTION is None:
        CONNECTION = pyrebase.initialize_app(settings.FIREBASE)
    return CONNECTION

def get_default_item():
    return {
        'name': '',
        'quantity': '',
        'user': None,
    }

def add(team, listname, item):
    db = connect().database()

    team = db.child('teams').child(team)
    l = db.child('lists').child(listname)
    l.push(item)

def list_(team, listname):
    db = connect().database()

    return db.child('teams').child(team).child('lists').child(listname).get().val()

def item_exists(name, team, listname):
    items = list_(team, listname)

    if items is not None:
        for _, x in items.iteritems():
            if x['name'] == name:
                return x
    return None

def clear(team, listname):
    db = connect().database()
    return db.child('teams').child(team).child('lists').child(listname).remove()
<|endoftext|>


<commit_before># Copyright 2009, BlueDynamics Alliance - http://bluedynamics.com
# Python Software Foundation License

import uuid
import inspect
from odict import odict
from odict.pyodict import _nil
from zope.interface import implements
from zope.interface.common.mapping import IReadMapping
try:
    from zope.location import LocationIterator
except ImportError, e:
    from zope.app.location import LocationIterator # BBB
try:
    from zope.component.event import objectEventNotify
except ImportError, e:
    from zope.app.event.objectevent import objectEventNotify # BBB
from zodict import Zodict
from zodict.interfaces import (
    INode,
    INodeAttributes,
    IAttributedNode,
    ILifecycleNode,
)
from zodict.events import (
    NodeCreatedEvent,
    NodeAddedEvent,
    NodeRemovedEvent,
    NodeModifiedEvent,
    NodeDetachedEvent,
)

class NodeIndex(object):
    implements(IReadMapping)

    def __init__(self, index):
        self._index = index

    def __getitem__(self, key):
        return self._index[int(key)]

    def get(self, key, default=None):
        return self._index.get(int(key), default)

    def __contains__(self, key):
        return int(key) in self._index

class _Node(object):
    """Abstract node implementation. Subclass must mixin ``_node_impl()``.
    """
    implements(INode)

    def _node_impl(self):
        return None

    def __init__(self, name=None, index=True):
        """
        ``name``
            optional name used for ``__name__`` declared by ``ILocation``.
        ``index``
            flag wether node index is enabled or not.
        """
        super(self._node_impl(), self).__init__()
        self.__parent__ = None
        self.__name__ = name
        if index:
            self._index = dict()
        else:
            self._index = None
        self._uuid = None
        self.uuid = uuid.uuid4()
        self.aliases = None

    def __contains__(self, key):
        try:
            return self.aliases.__contains__(key)
        except AttributeError:
            # aliases is None
            return self._node_impl().__contains__(self, key)

    def _aliased(self, key):
        try:
            key = self.aliases.__getitem__(key)
        except AttributeError:
            # aliases is None
            pass
        return key

    def __getitem__(self, key):
        key = self._aliased(key)
        return self._node_impl().__getitem__(self, key)

    def __iter__(self):
        try:
            return self.aliases.__iter__()
        except AttributeError:
            # aliases is None
            return self._node_impl().__iter__(self)

    def keys(self):
        try:
            return self.aliases.keys()
        except AttributeError:
            # aliases is None
            return self._node_impl().keys(self)

    def __setitem__(self, key, val):
        if inspect.isclass(val):
            raise ValueError, u"It isn't allowed to use classes as values."
        key = self._aliased(key)
        val.__name__ = key
        val.__parent__ = self
        has_children = False
        for valkey in val.iterkeys():
            has_children = True
            break
        if has_children and self._index is not None:
            keys = set(self._index.keys())
            if keys.intersection(val._index.keys()):
                raise ValueError, u"Node with uuid already exists"
        if self._index is not None:
            self._index.update(val._index)
            val._index = self._index
        self._node_impl().__setitem__(self, key, val)

    def _to_delete(self):
        todel = [int(self.uuid)]
        for childkey in self:
            todel += self[childkey]._to_delete()
        return todel

    def __delitem__(self, key):
        key = self._aliased(key)
        val = self[key]
        if self._index is not None:
            for iuuid in self[key]._to_delete():
                del self._index[iuuid]
        self._node_impl().__delitem__(self, key)

    def _get_uuid(self):
        return self._uuid

    def _set_uuid(self, uuid):
        iuuid = uuid is not None and int(uuid) or None
        if self._index is not None \
          and iuuid in self._index \
          and self._index[iuuid] is not self:
            raise ValueError, u"Given uuid was already used for another Node"
        siuuid = self._uuid is not None and int(self._uuid) or None
        if self._index is not None and siuuid in self._index:
            del self._index[siuuid]
        if self._index is not None:
            self._index[iuuid] = self
        self._uuid = uuid

    uuid = property(_get_uuid, _set_uuid)

    @property
    def path(self):
        path = list()
        for parent in LocationIterator(self):
            path.append(parent.__name__)
        path.reverse()
        return path

    @property
    def root(self):
        root = None
        for parent in LocationIterator(self):
            root = parent
        return root

    @property
    def index(self):
        if self._index is None:
            raise AttributeError(u"No index support configured on this Node.")
        return NodeIndex(self._index)

    def node(self, uuid):
        if self._index is None:
            raise ValueError(u"No index support configured on this Node.")
        return self._index.get(int(uuid))

    def filtereditems(self, interface):
        for node in self.values():
            if interface.providedBy(node):
                yield node

    def _validateinsertion(self, newnode, refnode):
        nodekey = newnode.__name__
        if nodekey is None:
            raise ValueError, u"Given node has no __name__ set."
        if self.node(newnode.uuid) is not None:
            raise KeyError, u"Given node already contained in tree."
        index = self._nodeindex(refnode)
        if index is None:
            raise ValueError, u"Given reference node not child of self."

    def _nodeindex(self, node):
        index = 0
        for key in self.keys():
            if key == node.__name__:
                return index
            index += 1
        return None

    def insertbefore(self, newnode, refnode):
        self._validateinsertion(newnode, refnode)
        nodekey = newnode.__name__
        refkey = refnode.__name__
        index = self._nodeindex(refnode)
        prevnode = None
        prevkey = None
        if index > 0:
            prevkey = self.keys()[index - 1]
            prevnode = dict.__getitem__(self, prevkey)
        if prevnode is not None:
            dict.__getitem__(self, prevkey)[2] = nodekey
            newnode = [prevkey, newnode, refkey]
        else:
            self.lh = nodekey
            newnode = [_nil, newnode, refkey]
        dict.__getitem__(self, refkey)[0] = nodekey
        dict.__setitem__(self, nodekey, newnode)
        self[nodekey] = newnode[1]

    def insertafter(self, newnode, refnode):
        self._validateinsertion(newnode, refnode)
        nodekey = newnode.__name__
        refkey = refnode.__name__
        index = self._nodeindex(refnode)
        nextnode = None
        nextkey = None
        keys = self.keys()
        if index < len(keys) - 1:
            nextkey = self.keys()[index + 1]
            nextnode = dict.__getitem__(self, nextkey)
        if nextnode is not None:
            dict.__getitem__(self, nextkey)[0] = nodekey
            newnode = [refkey, newnode, nextkey]
        else:
            self.lt = nodekey
            newnode = [refkey, newnode, _nil]
        dict.__getitem__(self, refkey)[2] = nodekey
        dict.__setitem__(self, nodekey, newnode)
        self[nodekey] = newnode[1]

    def _index_nodes(self):
        for node in self.values():
            self._index[int(node.uuid)] = node
            node._index = self._index
            node._index_nodes()

    def detach(self, key):
        node = self[key]
        del self[key]
        if self._index is not None:
            node._index = { int(node.uuid): node }
            node._index_nodes()
        return node

    @property
    def noderepr(self):
        name = str(self.__name__)
        return str(self.__class__) + ': ' + name[name.find(':') + 1:]

    def printtree(self, indent=0):
        print "%s%s" % (indent * ' ', self.noderepr)
        for node in self.values():
            node.printtree(indent+2)

    def __repr__(self):
        return "<%s object '%s' at %s>" % (self.__class__.__name__,
                                           str(self.__name__),
                                           hex(id(self))[:-1])

    __str__ = __repr__

class Node(_Node, Zodict):
    """Inherit from _Node and mixin Zodict.
    """

    def _node_impl(self):
        return Zodict

class NodeAttributes(dict):
    implements(INodeAttributes)

    def __init__(self, node):
        super(NodeAttributes, self).__init__()
        object.__setattr__(self, '_node', node)
        object.__setattr__(self, 'changed', False)

    def __getattr__(self, name):
        try:
            return self[name]
        except KeyError:
            raise AttributeError(name)

    def __setattr__(self, name, value):
        self[name] = value

    def __setitem__(self, key, val):
        dict.__setitem__(self, key, val)
        object.__setattr__(self, 'changed', True)

    def __delitem__(self, key):
        dict.__delitem__(self, key)
        object.__setattr__(self, 'changed', True)

    def __copy__(self):
        _new = NodeAttributes(object.__getattribute__(self, '_node'))
        for key, value in self.items():
            _new[key] = value
        _new.changed = object.__getattribute__(self, 'changed')
        return _new

class AttributedNode(Node):
    implements(IAttributedNode)

    attributes_factory = NodeAttributes

    # enable subclasses to override this
    _attrmap = None

    def __init__(self, name=None, attrmap=None):
        super(AttributedNode, self).__init__(name)
        if attrmap is not None:
            self._attrmap = attrmap

    @property
    def attrs(self):
        if not hasattr(self, '_attributes'):
            self._attributes = self.attributes_factory(self)
        return self._attributes

    # BBB
    attributes = attrs

class LifecycleNodeAttributes(NodeAttributes):

    def __setitem__(self, key, val):
        NodeAttributes.__setitem__(self, key, val)
        node = object.__getattribute__(self, '_node')
        if node._notify_suppress:
            return
        objectEventNotify(node.events['modified'](node))

    def __delitem__(self, key):
        NodeAttributes.__delitem__(self, key)
        node = object.__getattribute__(self, '_node')
        if self._node._notify_suppress:
            return
        if node._notify_suppress:
            return
        objectEventNotify(node.events['modified'](node))

class LifecycleNode(AttributedNode):
    implements(ILifecycleNode)

    events = {
        'created': NodeCreatedEvent,
        'added': NodeAddedEvent,
        'modified': NodeModifiedEvent,
        'removed': NodeRemovedEvent,
        'detached': NodeDetachedEvent,
    }

    attributes_factory = LifecycleNodeAttributes

    def __init__(self, name=None, attrmap=None):
        super(LifecycleNode, self).__init__(name=name, attrmap=attrmap)
        self._notify_suppress = False
        objectEventNotify(self.events['created'](self))

    def __setitem__(self, key, val):
        super(LifecycleNode, self).__setitem__(key, val)
        if self._notify_suppress:
            return
        objectEventNotify(self.events['added'](val, newParent=self,
                                               newName=key))

    def __delitem__(self, key):
        delnode = self[key]
        super(LifecycleNode, self).__delitem__(key)
        if self._notify_suppress:
            return
        objectEventNotify(self.events['removed'](delnode, oldParent=self,
                                                 oldName=key))

    def detach(self, key):
        notify_before = self._notify_suppress
        self._notify_suppress = True
        node = super(LifecycleNode, self).detach(key)
        self._notify_suppress = False
        objectEventNotify(self.events['detached'](node, oldParent=self,
                                                  oldName=key))
        return node
<commit_msg>uuid only needed if index<commit_after># Copyright 2009, BlueDynamics Alliance - http://bluedynamics.com
# Python Software Foundation License

import uuid
import inspect
from odict import odict
from odict.pyodict import _nil
from zope.interface import implements
from zope.interface.common.mapping import IReadMapping
try:
    from zope.location import LocationIterator
except ImportError, e:
    from zope.app.location import LocationIterator # BBB
try:
    from zope.component.event import objectEventNotify
except ImportError, e:
    from zope.app.event.objectevent import objectEventNotify # BBB
from zodict import Zodict
from zodict.interfaces import (
    INode,
    INodeAttributes,
    IAttributedNode,
    ILifecycleNode,
)
from zodict.events import (
    NodeCreatedEvent,
    NodeAddedEvent,
    NodeRemovedEvent,
    NodeModifiedEvent,
    NodeDetachedEvent,
)

class NodeIndex(object):
    implements(IReadMapping)

    def __init__(self, index):
        self._index = index

    def __getitem__(self, key):
        return self._index[int(key)]

    def get(self, key, default=None):
        return self._index.get(int(key), default)

    def __contains__(self, key):
        return int(key) in self._index

class _Node(object):
    """Abstract node implementation. Subclass must mixin ``_node_impl()``.
    """
    implements(INode)

    def _node_impl(self):
        return None

    def __init__(self, name=None, index=True):
        """
        ``name``
            optional name used for ``__name__`` declared by ``ILocation``.
        ``index``
            flag wether node index is enabled or not.
        """
        super(self._node_impl(), self).__init__()
        self.__parent__ = None
        self.__name__ = name
        if index:
            self._index = dict()
            self._uuid = None
            self.uuid = uuid.uuid4()
        else:
            self._index = None
        self.aliases = None

    def __contains__(self, key):
        try:
            return self.aliases.__contains__(key)
        except AttributeError:
            # aliases is None
            return self._node_impl().__contains__(self, key)

    def _aliased(self, key):
        try:
            key = self.aliases.__getitem__(key)
        except AttributeError:
            # aliases is None
            pass
        return key

    def __getitem__(self, key):
        key = self._aliased(key)
        return self._node_impl().__getitem__(self, key)

    def __iter__(self):
        try:
            return self.aliases.__iter__()
        except AttributeError:
            # aliases is None
            return self._node_impl().__iter__(self)

    def keys(self):
        try:
            return self.aliases.keys()
        except AttributeError:
            # aliases is None
            return self._node_impl().keys(self)

    def __setitem__(self, key, val):
        if inspect.isclass(val):
            raise ValueError, u"It isn't allowed to use classes as values."
        key = self._aliased(key)
        val.__name__ = key
        val.__parent__ = self
        has_children = False
        for valkey in val.iterkeys():
            has_children = True
            break
        if has_children and self._index is not None:
            keys = set(self._index.keys())
            if keys.intersection(val._index.keys()):
                raise ValueError, u"Node with uuid already exists"
        if self._index is not None:
            self._index.update(val._index)
            val._index = self._index
        self._node_impl().__setitem__(self, key, val)

    def _to_delete(self):
        todel = [int(self.uuid)]
        for childkey in self:
            todel += self[childkey]._to_delete()
        return todel

    def __delitem__(self, key):
        key = self._aliased(key)
        val = self[key]
        if self._index is not None:
            for iuuid in self[key]._to_delete():
                del self._index[iuuid]
        self._node_impl().__delitem__(self, key)

    def _get_uuid(self):
        return self._uuid

    def _set_uuid(self, uuid):
        iuuid = uuid is not None and int(uuid) or None
        if self._index is not None \
          and iuuid in self._index \
          and self._index[iuuid] is not self:
            raise ValueError, u"Given uuid was already used for another Node"
        siuuid = self._uuid is not None and int(self._uuid) or None
        if self._index is not None and siuuid in self._index:
            del self._index[siuuid]
        if self._index is not None:
            self._index[iuuid] = self
        self._uuid = uuid

    uuid = property(_get_uuid, _set_uuid)

    @property
    def path(self):
        path = list()
        for parent in LocationIterator(self):
            path.append(parent.__name__)
        path.reverse()
        return path

    @property
    def root(self):
        root = None
        for parent in LocationIterator(self):
            root = parent
        return root

    @property
    def index(self):
        if self._index is None:
            raise AttributeError(u"No index support configured on this Node.")
        return NodeIndex(self._index)

    def node(self, uuid):
        if self._index is None:
            raise ValueError(u"No index support configured on this Node.")
        return self._index.get(int(uuid))

    def filtereditems(self, interface):
        for node in self.values():
            if interface.providedBy(node):
                yield node

    def _validateinsertion(self, newnode, refnode):
        nodekey = newnode.__name__
        if nodekey is None:
            raise ValueError, u"Given node has no __name__ set."
        if self.node(newnode.uuid) is not None:
            raise KeyError, u"Given node already contained in tree."
        index = self._nodeindex(refnode)
        if index is None:
            raise ValueError, u"Given reference node not child of self."

    def _nodeindex(self, node):
        index = 0
        for key in self.keys():
            if key == node.__name__:
                return index
            index += 1
        return None

    def insertbefore(self, newnode, refnode):
        self._validateinsertion(newnode, refnode)
        nodekey = newnode.__name__
        refkey = refnode.__name__
        index = self._nodeindex(refnode)
        prevnode = None
        prevkey = None
        if index > 0:
            prevkey = self.keys()[index - 1]
            prevnode = dict.__getitem__(self, prevkey)
        if prevnode is not None:
            dict.__getitem__(self, prevkey)[2] = nodekey
            newnode = [prevkey, newnode, refkey]
        else:
            self.lh = nodekey
            newnode = [_nil, newnode, refkey]
        dict.__getitem__(self, refkey)[0] = nodekey
        dict.__setitem__(self, nodekey, newnode)
        self[nodekey] = newnode[1]

    def insertafter(self, newnode, refnode):
        self._validateinsertion(newnode, refnode)
        nodekey = newnode.__name__
        refkey = refnode.__name__
        index = self._nodeindex(refnode)
        nextnode = None
        nextkey = None
        keys = self.keys()
        if index < len(keys) - 1:
            nextkey = self.keys()[index + 1]
            nextnode = dict.__getitem__(self, nextkey)
        if nextnode is not None:
            dict.__getitem__(self, nextkey)[0] = nodekey
            newnode = [refkey, newnode, nextkey]
        else:
            self.lt = nodekey
            newnode = [refkey, newnode, _nil]
        dict.__getitem__(self, refkey)[2] = nodekey
        dict.__setitem__(self, nodekey, newnode)
        self[nodekey] = newnode[1]

    def _index_nodes(self):
        for node in self.values():
            self._index[int(node.uuid)] = node
            node._index = self._index
            node._index_nodes()

    def detach(self, key):
        node = self[key]
        del self[key]
        if self._index is not None:
            node._index = { int(node.uuid): node }
            node._index_nodes()
        return node

    @property
    def noderepr(self):
        name = str(self.__name__)
        return str(self.__class__) + ': ' + name[name.find(':') + 1:]

    def printtree(self, indent=0):
        print "%s%s" % (indent * ' ', self.noderepr)
        for node in self.values():
            node.printtree(indent+2)

    def __repr__(self):
        return "<%s object '%s' at %s>" % (self.__class__.__name__,
                                           str(self.__name__),
                                           hex(id(self))[:-1])

    __str__ = __repr__

class Node(_Node, Zodict):
    """Inherit from _Node and mixin Zodict.
    """

    def _node_impl(self):
        return Zodict

class NodeAttributes(dict):
    implements(INodeAttributes)

    def __init__(self, node):
        super(NodeAttributes, self).__init__()
        object.__setattr__(self, '_node', node)
        object.__setattr__(self, 'changed', False)

    def __getattr__(self, name):
        try:
            return self[name]
        except KeyError:
            raise AttributeError(name)

    def __setattr__(self, name, value):
        self[name] = value

    def __setitem__(self, key, val):
        dict.__setitem__(self, key, val)
        object.__setattr__(self, 'changed', True)

    def __delitem__(self, key):
        dict.__delitem__(self, key)
        object.__setattr__(self, 'changed', True)

    def __copy__(self):
        _new = NodeAttributes(object.__getattribute__(self, '_node'))
        for key, value in self.items():
            _new[key] = value
        _new.changed = object.__getattribute__(self, 'changed')
        return _new

class AttributedNode(Node):
    implements(IAttributedNode)

    attributes_factory = NodeAttributes

    # enable subclasses to override this
    _attrmap = None

    def __init__(self, name=None, attrmap=None):
        super(AttributedNode, self).__init__(name)
        if attrmap is not None:
            self._attrmap = attrmap

    @property
    def attrs(self):
        if not hasattr(self, '_attributes'):
            self._attributes = self.attributes_factory(self)
        return self._attributes

    # BBB
    attributes = attrs

class LifecycleNodeAttributes(NodeAttributes):

    def __setitem__(self, key, val):
        NodeAttributes.__setitem__(self, key, val)
        node = object.__getattribute__(self, '_node')
        if node._notify_suppress:
            return
        objectEventNotify(node.events['modified'](node))

    def __delitem__(self, key):
        NodeAttributes.__delitem__(self, key)
        node = object.__getattribute__(self, '_node')
        if self._node._notify_suppress:
            return
        if node._notify_suppress:
            return
        objectEventNotify(node.events['modified'](node))

class LifecycleNode(AttributedNode):
    implements(ILifecycleNode)

    events = {
        'created': NodeCreatedEvent,
        'added': NodeAddedEvent,
        'modified': NodeModifiedEvent,
        'removed': NodeRemovedEvent,
        'detached': NodeDetachedEvent,
    }

    attributes_factory = LifecycleNodeAttributes

    def __init__(self, name=None, attrmap=None):
        super(LifecycleNode, self).__init__(name=name, attrmap=attrmap)
        self._notify_suppress = False
        objectEventNotify(self.events['created'](self))

    def __setitem__(self, key, val):
        super(LifecycleNode, self).__setitem__(key, val)
        if self._notify_suppress:
            return
        objectEventNotify(self.events['added'](val, newParent=self,
                                               newName=key))

    def __delitem__(self, key):
        delnode = self[key]
        super(LifecycleNode, self).__delitem__(key)
        if self._notify_suppress:
            return
        objectEventNotify(self.events['removed'](delnode, oldParent=self,
                                                 oldName=key))

    def detach(self, key):
        notify_before = self._notify_suppress
        self._notify_suppress = True
        node = super(LifecycleNode, self).detach(key)
        self._notify_suppress = False
        objectEventNotify(self.events['detached'](node, oldParent=self,
                                                  oldName=key))
        return node
<|endoftext|>


<commit_before># Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.

import sys
from zohmg.setup import Setup
from zohmg.config import Config
from zohmg.utils import setup_transport, disable, drop
from hbase.ttypes import IOError

class Reset(object):
    def please(self):
        host = 'localhost'
        table = Config().dataset()

        # confirm.
        print "reset will *wipe all data* in dataset '%s'." % table
        print "ARE YOU QUITE SURE? ('yes' to confirm.)"

        response = sys.stdin.readline().strip()
        if response.lower() not in ["yes", "yes!"]:
            print 'phew!'
            sys.exit(0)

        # disable+drop.
        print "ok, wiping!"
        try:
            c = setup_transport(host)
            disable(c, table)
            drop(c, table)
        except Exception, e:
            print 'reset failed :-('
            print 'error: ' + str(e)
        except IOError:
            # problem is, hbase.ttypes.IOError isn't very expressive.
            print 'reset failed :-('
            print '(does the table perhaps not exist?)'
            sys.exit(1)

        # recreate (with the help of our dear friend setup).
        print
        print "recreating."
        Setup().go()

        print
        print "%s reset'd" % table

<commit_msg>quit on error!<commit_after># Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.

import sys
from zohmg.setup import Setup
from zohmg.config import Config
from zohmg.utils import setup_transport, disable, drop
from hbase.ttypes import IOError

class Reset(object):
    def please(self):
        host = 'localhost'
        table = Config().dataset()

        # confirm.
        print "reset will *wipe all data* in dataset '%s'." % table
        print "ARE YOU QUITE SURE? ('yes' to confirm.)"

        response = sys.stdin.readline().strip()
        if response.lower() not in ["yes", "yes!"]:
            print 'phew!'
            sys.exit(0)

        # disable+drop.
        print "ok, wiping!"
        try:
            c = setup_transport(host)
            disable(c, table)
            drop(c, table)
        except Exception, e:
            print 'reset failed :-('
            print 'error: ' + str(e)
            sys.exit(1)
        except IOError:
            # problem is, hbase.ttypes.IOError isn't very expressive.
            print 'reset failed :-('
            print '(does the table perhaps not exist?)'
            sys.exit(1)

        # recreate (with the help of our dear friend setup).
        print
        print "recreating."
        Setup().go()

        print
        print "%s reset'd" % table

<|endoftext|>


<commit_before>from flask import Flask, url_for, request, redirect, render_template, send_file
from srs.sentiment_plot import sentimentBoxPlot
from srs.srs_local import fill_in_db
from srs.utilities import loadScraperDataFromDB
from srs.scraper import createAmazonScraper
import json
import numpy as np

from bokeh.embed import file_html, components

app = Flask(__name__)

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

@app.route('/scrape_reviews', methods=['GET', 'POST'])
def scrape_reviews():
	if request.method == 'POST':
		product_id = request.form["product_id"]
		print 'product_id is ' + product_id

		a = createAmazonScraper()
		fill_in_db(a, product_id)

		return product_id
	else:
		return render_template('home.html')

@app.route('/srs_result/<product_id>')
def showResultWithProductId(product_id): #B00HZE2PYI

	_, ft_score_dict, _ = loadScraperDataFromDB(product_id)
	ft_score_ave = []
	for ft in ft_score_dict:
		average_score = np.mean(np.array(ft_score_dict[ft]))
		ft_score_ave.append({"feature": ft, "score":average_score})

	return render_template('srs_result_bar.html', ft_score_ave=json.dumps(ft_score_ave))

@app.route('/srs_result_box/<product_id>')
def showBoxResultWithProductId(product_id): #B00HZE2PYI

	_, ft_score_dict, _ = loadScraperDataFromDB(product_id)
	ft_scorelist = []
	for ft in ft_score_dict:
		ft_scorelist.append([ft, ft_score_dict[ft]])

	return render_template('srs_result_box.html', ft_scorelist=json.dumps(ft_scorelist))

@app.route('/srs_result_box_bokeh/<product_id>')
def showBokehBoxResultWithProductId(product_id):
	# generate data for plotting
	_, ft_score_dict, _ = loadScraperDataFromDB(product_id)

	# do plotting
	p = sentimentBoxPlot(ft_score_dict)

	# create the HTML elements to pass to template
	figJS,figDiv = components(p)
	return render_template('srs_result_box_bokeh.html', figJS=figJS,figDiv=figDiv)

if __name__ == '__main__':
	app.debug = True
	app.run(host='0.0.0.0', port=80)<commit_msg>Modify scrape_reviews() so that it can configure two products<commit_after>from flask import Flask, url_for, request, redirect, render_template, send_file
from srs.sentiment_plot import sentimentBoxPlot
from srs.srs_local import fill_in_db
from srs.utilities import loadScraperDataFromDB
from srs.scraper import createAmazonScraper
import json
import numpy as np

from bokeh.embed import file_html, components

app = Flask(__name__)

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

@app.route('/scrape_reviews', methods=['GET', 'POST'])
def scrape_reviews():
	if request.method == 'POST':
		product_id = request.form["product_id"]
		product_id2 = request.form["product_id2"]
		if not product_id2:
			print 'product_id is ' + product_id
			a = createAmazonScraper()
			fill_in_db(a, product_id)
		else:
			print 'product_id is ' + product_id	+ ' and '+ product_id2
			a = createAmazonScraper()
			fill_in_db(a, product_id)
			fill_in_db(a, product_id2)

		return product_id
	else:
		return render_template('home.html')

@app.route('/srs_result/<product_id>')
def showResultWithProductId(product_id): #B00HZE2PYI

	_, ft_score_dict, _ = loadScraperDataFromDB(product_id)
	ft_score_ave = []
	for ft in ft_score_dict:
		average_score = np.mean(np.array(ft_score_dict[ft]))
		ft_score_ave.append({"feature": ft, "score":average_score})

	return render_template('srs_result_bar.html', ft_score_ave=json.dumps(ft_score_ave))

@app.route('/srs_result_box/<product_id>')
def showBoxResultWithProductId(product_id): #B00HZE2PYI

	_, ft_score_dict, _ = loadScraperDataFromDB(product_id)
	ft_scorelist = []
	for ft in ft_score_dict:
		ft_scorelist.append([ft, ft_score_dict[ft]])

	return render_template('srs_result_box.html', ft_scorelist=json.dumps(ft_scorelist))

@app.route('/srs_result_box_bokeh/<product_id>')
def showBokehBoxResultWithProductId(product_id):
	# generate data for plotting
	_, ft_score_dict, _ = loadScraperDataFromDB(product_id)

	# do plotting
	p = sentimentBoxPlot(ft_score_dict)

	# create the HTML elements to pass to template
	figJS,figDiv = components(p)
	return render_template('srs_result_box_bokeh.html', figJS=figJS,figDiv=figDiv)

if __name__ == '__main__':
	app.debug = True
	app.run(host='0.0.0.0', port=80)<|endoftext|>


<commit_before>from datetime import timedelta

import t

try:
    from cStringIO import StringIO
except ImportError:
    from io import StringIO

import logging

from gunicorn.config import Config
from gunicorn.instrument.statsd import Statsd

class TestException(Exception): pass

class MockSocket(object):
    "Pretend to be a UDP socket"
    def __init__(self, failp):
        self.failp = failp
        self.msgs = []  # accumulate messages for later inspection

    def send(self, msg):
        if self.failp:
            raise TestException("Should not interrupt the logger")
        self.msgs.append(msg)

    def reset(self):
        self.msgs = []

class MockResponse(object):
    def __init__(self, status):
        self.status = status

def test_statsd_fail():
    "UDP socket fails"
    logger = Statsd(Config())
    logger.sock = MockSocket(True)
    logger.info("No impact on logging")
    logger.debug("No impact on logging")
    logger.critical("No impact on logging")
    logger.error("No impact on logging")
    logger.warning("No impact on logging")
    logger.exception("No impact on logging")

def test_instrument():
    logger = Statsd(Config())
    # Capture logged messages
    sio = StringIO()
    logger.error_log.addHandler(logging.StreamHandler(sio))
    logger.sock = MockSocket(False)

    # Regular message
    logger.info("Blah", extra={"mtype": "gauge", "metric": "gunicorn.test", "value": 666})
    t.eq(logger.sock.msgs[0], "gunicorn.test:666|g")
    t.eq(sio.getvalue(), "Blah\n")
    logger.sock.reset()

    # Only metrics, no logging
    logger.info("", extra={"mtype": "gauge", "metric": "gunicorn.test", "value": 666})
    t.eq(logger.sock.msgs[0], "gunicorn.test:666|g")
    t.eq(sio.getvalue(), "Blah\n")  # log is unchanged
    logger.sock.reset()

    # Debug logging also supports metrics
    logger.debug("", extra={"mtype": "gauge", "metric": "gunicorn.debug", "value": 667})
    t.eq(logger.sock.msgs[0], "gunicorn.debug:667|g")
    t.eq(sio.getvalue(), "Blah\n")  # log is unchanged
    logger.sock.reset()

    logger.critical("Boom")
    t.eq(logger.sock.msgs[0], "gunicorn.log.critical:1|c|@1.0")
    logger.sock.reset()

    logger.access(MockResponse("200 OK"), None, {}, timedelta(seconds=7))
    t.eq(logger.sock.msgs[0], "gunicorn.request.duration:7000.0|ms")
    t.eq(logger.sock.msgs[1], "gunicorn.requests:1|c|@1.0")
    t.eq(logger.sock.msgs[2], "gunicorn.request.status.200:1|c|@1.0")
<commit_msg>Add tests for statsd prefix<commit_after>from datetime import timedelta

import t

try:
    from cStringIO import StringIO
except ImportError:
    from io import StringIO

import logging

from gunicorn.config import Config
from gunicorn.instrument.statsd import Statsd

class TestException(Exception): pass

class MockSocket(object):
    "Pretend to be a UDP socket"
    def __init__(self, failp):
        self.failp = failp
        self.msgs = []  # accumulate messages for later inspection

    def send(self, msg):
        if self.failp:
            raise TestException("Should not interrupt the logger")
        self.msgs.append(msg)

    def reset(self):
        self.msgs = []

class MockResponse(object):
    def __init__(self, status):
        self.status = status

def test_statsd_fail():
    "UDP socket fails"
    logger = Statsd(Config())
    logger.sock = MockSocket(True)
    logger.info("No impact on logging")
    logger.debug("No impact on logging")
    logger.critical("No impact on logging")
    logger.error("No impact on logging")
    logger.warning("No impact on logging")
    logger.exception("No impact on logging")

def test_instrument():
    logger = Statsd(Config())
    # Capture logged messages
    sio = StringIO()
    logger.error_log.addHandler(logging.StreamHandler(sio))
    logger.sock = MockSocket(False)

    # Regular message
    logger.info("Blah", extra={"mtype": "gauge", "metric": "gunicorn.test", "value": 666})
    t.eq(logger.sock.msgs[0], "gunicorn.test:666|g")
    t.eq(sio.getvalue(), "Blah\n")
    logger.sock.reset()

    # Only metrics, no logging
    logger.info("", extra={"mtype": "gauge", "metric": "gunicorn.test", "value": 666})
    t.eq(logger.sock.msgs[0], "gunicorn.test:666|g")
    t.eq(sio.getvalue(), "Blah\n")  # log is unchanged
    logger.sock.reset()

    # Debug logging also supports metrics
    logger.debug("", extra={"mtype": "gauge", "metric": "gunicorn.debug", "value": 667})
    t.eq(logger.sock.msgs[0], "gunicorn.debug:667|g")
    t.eq(sio.getvalue(), "Blah\n")  # log is unchanged
    logger.sock.reset()

    logger.critical("Boom")
    t.eq(logger.sock.msgs[0], "gunicorn.log.critical:1|c|@1.0")
    logger.sock.reset()

    logger.access(MockResponse("200 OK"), None, {}, timedelta(seconds=7))
    t.eq(logger.sock.msgs[0], "gunicorn.request.duration:7000.0|ms")
    t.eq(logger.sock.msgs[1], "gunicorn.requests:1|c|@1.0")
    t.eq(logger.sock.msgs[2], "gunicorn.request.status.200:1|c|@1.0")

def test_prefix():
    c = Config()
    c.set("statsd_prefix", "test.")
    logger = Statsd(c)
    logger.sock = MockSocket(False)

    logger.info("Blah", extra={"mtype": "gauge", "metric": "gunicorn.test", "value": 666})
    t.eq(logger.sock.msgs[0], "test.gunicorn.test:666|g")
<|endoftext|>