<commit_before>from ggame import App, Sprite, ImageAsset, Frame, SoundAsset, Sound
import math
from time import time

class Stars(Sprite):

    asset = ImageAsset("images/starfield.jpg")
    width = 512
    height = 512

    def __init__(self, position):
        super().__init__(Stars.asset, position)

class Sun(Sprite):

    asset = ImageAsset("images/sun.png")
    width = 80
    height = 76

    def __init__(self, position):
        super().__init__(Sun.asset, position)
        self.mass = 30*1000
        self.fxcenter = 0.5
        self.fycenter = 0.5
        self.circularCollisionModel()

class Vector(object):

    def __init__(self, x, y):
        self.x = x
        self.y = y

    def mag(self):
        return math.sqrt(self.x*self.x + self.y*self.y)

    def unit(self):
        r = self.mag()
        if r == 0:
            return Vector(0,0)
        else:
            return Vector(self.x/r, self.y/r)

class GravitySprite(Sprite):

    G = 50.0

    def __init__(self, asset, position, velocity, sun):
        super().__init__(asset, position)
        self.vx = velocity[0]
        self.vy = velocity[1]
        self.sun = sun
        self.fxcenter = 0.5
        self.fycenter = 0.5
        self.rrate = 0.0
        self.thrust = 0.0
        self.mass = 1.0

    def step(self, T, dT):
        #dt = 0.033
        R = Vector(self.sun.x-self.x, self.sun.y-self.y)
        #Ur = R.unit()
        r = R.mag()
        Ux, Uy = R.x/r, R.y/r
        ag = GravitySprite.G*self.sun.mass/R.mag()**2
        Agx, Agy = Ux*ag, Uy*ag
        vx, vy = self.vx, self.vy
        At = self.thrust/self.mass
        dt2o2 = dT*dT*0.5
        self.vx += (Agx + At*math.sin(self.rotation))* dT
        self.vy += (Agy - At*math.cos(self.rotation))* dT
        self.x += self.vx * dT + Agx*dt2o2
        self.y += self.vy * dT + Agy*dt2o2

class Bullet(GravitySprite):

    asset = ImageAsset("images/blast.png", Frame(0,0,8,8), 8)
    pewasset = SoundAsset("sounds/pew1.mp3")

    def __init__(self, app, sun):
        super().__init__(Bullet.asset, (0,0), (0,0), sun)
        self.visible = False
        self.firing = False
        self.time = 0
        self.circularCollisionModel()
        self.pew = Sound(Bullet.pewasset)
        self.pew.volume = 0.3

    def shoot(self, position, velocity, time):
        self.position = position
        self.vx = velocity[0]
        self.vy = velocity[1]
        self.time = time
        self.visible = True
        self.firing = True
        self.pew.play()

    def step(self, T, dT):
        if self.time > 0:
            self.time -= dT
            if self.visible:
                self.nextImage(True)
                super().step(T, dT)
                if self.collidingWith(self.sun):
                    self.visible = False
                ships = []
                ships = self.collidingWithSprites(Ship1)
                ships.extend(self.collidingWithSprites(Ship2))
                if len(ships):
                    if not self.firing:
                        ships[0].explode()
                        self.visible = False
                elif self.firing:
                    self.firing = False

        else:
            if self.visible:
                self.visible = False
            self.time = 0

class Ship(GravitySprite):

    R = 2.0
    bullets = 2

    def __init__(self, asset, app, position, velocity, sun):
        self.bullets = []
        for i in range(Ship.bullets):
            self.bullets.append(Bullet(app, sun))
        super().__init__(asset, position, velocity, sun)
        self.initposition = position
        self.initvelocity = self.vx, self.vy
        self.initrotation = self.rotation
        self.app = app
        self.mass = 1.0
        self.circularCollisionModel()
        self.imagex = 0

    def registerKeys(self, keys):
        commands = ["left", "right", "forward", "fire"]
        self.keymap = dict(zip(keys, commands))
        [self.app.listenKeyEvent("keydown", k, self.controldown) for k in keys]
        [self.app.listenKeyEvent("keyup", k, self.controlup) for k in keys]

    def shootvector(self):
        vel = 150
        xv = vel*math.sin(self.rotation)
        yv = vel*(-math.cos(self.rotation))
        return xv + self.vx, yv + self.vy

    def controldown(self, event):
        command = self.keymap[event.key]
        if command == "left":
            self.rrate = -Ship.R
        elif command == "right":
            self.rrate = Ship.R
        elif command == "forward":
            self.thrust = 40.0
            self.imagex = 1 # start the animated rockets
            self.setImage(self.imagex)
        elif command == "fire":
            for bullet in self.bullets:
                if bullet.time == 0:
                    bullet.shoot(self.position, self.shootvector(), 10)
                    break

    def controlup(self, event):
        command = self.keymap[event.key]
        if command in ["left", "right"]:
            self.rrate = 0.0
        elif command == "forward":
            self.thrust = 0.0
            self.imagex = 0 # stop the animated rockets
            self.setImage(self.imagex)

    def step(self, T, dT):
        super().step(T, dT)
        self.rotation += self.rrate * dT
        for bullet in self.bullets:
            bullet.step(T, dT)
        if self.collidingWith(self.sun):
            self.explode()
        if self.thrust != 0.0:
            self.imagex += 1    # animate the rockets
            if self.imagex == 4:
                self.imagex = 1
            self.setImage(self.imagex)
        if (self.x < -100 or self.x > self.app.width + 100 or
            self.y < -100 or self.y > self.app.height + 100):
            self.reset()

    def explode(self):
        self.reset()

    def reset(self):
        self.position = self.initposition
        self.vx, self.vy = self.initvelocity
        self.rotation = self.initrotation

class Ship1(Ship):

    asset = ImageAsset("images/four_spaceship_by_albertov_with_thrust.png",
        Frame(227,0,292-227,125), 4, 'vertical')

    def __init__(self, app, position, velocity, sun):
        super().__init__(Ship1.asset, app, position, velocity, sun)
        self.registerKeys(["left arrow", "right arrow", "up arrow", "enter"])

    def step(self, T, dT):
        super().step(T, dT)
        collides = self.collidingWithSprites(Ship2)
        if len(collides):
            collides[0].explode()
            self.explode()

class Ship2(Ship):

    asset = ImageAsset("images/four_spaceship_by_albertov_with_thrust.png",
        Frame(0,0,86,125), 4, 'vertical')

    def __init__(self, app, position, velocity, sun):
        super().__init__(Ship2.asset, app, position, velocity, sun)
        self.registerKeys(["a", "d", "w", "space"])

    def step(self, T, dT):
        super().step(T, dT)
        collides = self.collidingWithSprites(Ship1)
        if len(collides):
            collides[0].explode()
            self.explode()

class Spacewar(App):

    def __init__(self, width, height):
        super().__init__(width, height)
        for x in range(self.width//Stars.width + 1):
            for y in range(self.height//Stars.height + 1):
                Stars((x*Stars.width, y*Stars.height))
        self.sun = Sun((self.width/2, self.height/2))
        self.ship1 = Ship1(self, (self.width/2+100,self.height/2), (0,-140), self.sun)
        self.ship2 = Ship2(self, (self.width/2-100,self.height/2), (0,140), self.sun)
        self.Tlast = time()

    def step(self):
        T = time()
        dT = T-self.Tlast
        self.Tlast = T
        self.ship1.step(T, dT)
        self.ship2.step(T, dT)

app = Spacewar(0,0)
app.run()

<commit_msg>Updated from Brython Server: 8/11/2015 4:36:59 PM<commit_after>from ggame import App, Sprite, ImageAsset, Frame, SoundAsset, Sound
import math
from time import time

class Stars(Sprite):

    asset = ImageAsset("images/starfield.jpg")
    width = 512
    height = 512

    def __init__(self, position):
        super().__init__(Stars.asset, position)

class Sun(Sprite):

    asset = ImageAsset("images/sun.png")
    width = 80
    height = 76

    def __init__(self, position):
        super().__init__(Sun.asset, position)
        self.mass = 30*1000
        self.fxcenter = 0.5
        self.fycenter = 0.5
        self.circularCollisionModel()

class Vector(object):

    def __init__(self, x, y):
        self.x = x
        self.y = y

    def mag(self):
        return math.sqrt(self.x*self.x + self.y*self.y)

    def unit(self):
        r = self.mag()
        if r == 0:
            return Vector(0,0)
        else:
            return Vector(self.x/r, self.y/r)

class GravitySprite(Sprite):

    G = 50.0

    def __init__(self, asset, position, velocity, sun):
        super().__init__(asset, position)
        self.vx = velocity[0]
        self.vy = velocity[1]
        self.sun = sun
        self.fxcenter = 0.5
        self.fycenter = 0.5
        self.rrate = 0.0
        self.thrust = 0.0
        self.mass = 1.0

    def step(self, T, dT):
        #dt = 0.033
        R = Vector(self.sun.x-self.x, self.sun.y-self.y)
        #Ur = R.unit()
        r = R.mag()
        Ux, Uy = R.x/r, R.y/r
        ag = GravitySprite.G*self.sun.mass/R.mag()**2
        Agx, Agy = Ux*ag, Uy*ag
        vx, vy = self.vx, self.vy
        At = self.thrust/self.mass
        dt2o2 = dT*dT*0.5
        self.vx += (Agx + At*math.sin(self.rotation))* dT
        self.vy += (Agy - At*math.cos(self.rotation))* dT
        self.x += self.vx * dT + Agx*dt2o2
        self.y += self.vy * dT + Agy*dt2o2

class Bullet(GravitySprite):

    asset = ImageAsset("images/blast.png", Frame(0,0,8,8), 8)
    pewasset = SoundAsset("sounds/pew1.mp3")

    def __init__(self, app, sun):
        super().__init__(Bullet.asset, (0,0), (0,0), sun)
        self.visible = False
        self.firing = False
        self.time = 0
        self.circularCollisionModel()
        self.pew = Sound(Bullet.pewasset)
        self.pew.volume = 0.7

    def shoot(self, position, velocity, time):
        self.position = position
        self.vx = velocity[0]
        self.vy = velocity[1]
        self.time = time
        self.visible = True
        self.firing = True
        self.pew.play()

    def step(self, T, dT):
        if self.time > 0:
            self.time -= dT
            if self.visible:
                self.nextImage(True)
                super().step(T, dT)
                if self.collidingWith(self.sun):
                    self.visible = False
                ships = []
                ships = self.collidingWithSprites(Ship1)
                ships.extend(self.collidingWithSprites(Ship2))
                if len(ships):
                    if not self.firing:
                        ships[0].explode()
                        self.visible = False
                elif self.firing:
                    self.firing = False

        else:
            if self.visible:
                self.visible = False
            self.time = 0

class Ship(GravitySprite):

    R = 2.0
    bullets = 2

    def __init__(self, asset, app, position, velocity, sun):
        self.bullets = []
        for i in range(Ship.bullets):
            self.bullets.append(Bullet(app, sun))
        super().__init__(asset, position, velocity, sun)
        self.initposition = position
        self.initvelocity = self.vx, self.vy
        self.initrotation = self.rotation
        self.app = app
        self.mass = 1.0
        self.circularCollisionModel()
        self.imagex = 0

    def registerKeys(self, keys):
        commands = ["left", "right", "forward", "fire"]
        self.keymap = dict(zip(keys, commands))
        [self.app.listenKeyEvent("keydown", k, self.controldown) for k in keys]
        [self.app.listenKeyEvent("keyup", k, self.controlup) for k in keys]

    def shootvector(self):
        vel = 150
        xv = vel*math.sin(self.rotation)
        yv = vel*(-math.cos(self.rotation))
        return xv + self.vx, yv + self.vy

    def controldown(self, event):
        command = self.keymap[event.key]
        if command == "left":
            self.rrate = -Ship.R
        elif command == "right":
            self.rrate = Ship.R
        elif command == "forward":
            self.thrust = 40.0
            self.imagex = 1 # start the animated rockets
            self.setImage(self.imagex)
        elif command == "fire":
            for bullet in self.bullets:
                if bullet.time == 0:
                    bullet.shoot(self.position, self.shootvector(), 10)
                    break

    def controlup(self, event):
        command = self.keymap[event.key]
        if command in ["left", "right"]:
            self.rrate = 0.0
        elif command == "forward":
            self.thrust = 0.0
            self.imagex = 0 # stop the animated rockets
            self.setImage(self.imagex)

    def step(self, T, dT):
        super().step(T, dT)
        self.rotation += self.rrate * dT
        for bullet in self.bullets:
            bullet.step(T, dT)
        if self.collidingWith(self.sun):
            self.explode()
        if self.thrust != 0.0:
            self.imagex += 1    # animate the rockets
            if self.imagex == 4:
                self.imagex = 1
            self.setImage(self.imagex)
        if (self.x < -100 or self.x > self.app.width + 100 or
            self.y < -100 or self.y > self.app.height + 100):
            self.reset()

    def explode(self):
        self.reset()

    def reset(self):
        self.position = self.initposition
        self.vx, self.vy = self.initvelocity
        self.rotation = self.initrotation

class Ship1(Ship):

    asset = ImageAsset("images/four_spaceship_by_albertov_with_thrust.png",
        Frame(227,0,292-227,125), 4, 'vertical')

    def __init__(self, app, position, velocity, sun):
        super().__init__(Ship1.asset, app, position, velocity, sun)
        self.registerKeys(["left arrow", "right arrow", "up arrow", "enter"])

    def step(self, T, dT):
        super().step(T, dT)
        collides = self.collidingWithSprites(Ship2)
        if len(collides):
            collides[0].explode()
            self.explode()

class Ship2(Ship):

    asset = ImageAsset("images/four_spaceship_by_albertov_with_thrust.png",
        Frame(0,0,86,125), 4, 'vertical')

    def __init__(self, app, position, velocity, sun):
        super().__init__(Ship2.asset, app, position, velocity, sun)
        self.registerKeys(["a", "d", "w", "space"])

    def step(self, T, dT):
        super().step(T, dT)
        collides = self.collidingWithSprites(Ship1)
        if len(collides):
            collides[0].explode()
            self.explode()

class Spacewar(App):

    def __init__(self, width, height):
        super().__init__(width, height)
        for x in range(self.width//Stars.width + 1):
            for y in range(self.height//Stars.height + 1):
                Stars((x*Stars.width, y*Stars.height))
        self.sun = Sun((self.width/2, self.height/2))
        self.ship1 = Ship1(self, (self.width/2+100,self.height/2), (0,-140), self.sun)
        self.ship2 = Ship2(self, (self.width/2-100,self.height/2), (0,140), self.sun)
        self.Tlast = time()

    def step(self):
        T = time()
        dT = T-self.Tlast
        self.Tlast = T
        self.ship1.step(T, dT)
        self.ship2.step(T, dT)

app = Spacewar(0,0)
app.run()

<|endoftext|>


<commit_before>"""Helpers for sparkle packaging."""

load("@io_tweag_clodl//clodl:clodl.bzl", "library_closure")
load("@rules_haskell//haskell:defs.bzl", "haskell_library")

def _mangle_dir(name):
    """Creates a unique directory name from the repo name and package
      name of the package being evaluated, and a given name.
    """
    components = [native.repository_name(), native.package_name(), name]
    components = [c.replace("@", "") for c in components]
    components = [c for c in components if c]
    return "/".join(components).replace("_", "_U").replace("/", "_S")

def sparkle_package(name, src, resource_jars=[], **kwargs):
  libclosure = "libclosure-%s" % name

  haskell_library(
	name = libclosure + "-hs-wrapper",
	srcs = ["@io_tweag_sparkle//:hs-wrapper/FFIWrapper.hs"],
	deps = [src, "@io_tweag_sparkle//:base"],
    compiler_flags = ["-threaded", "-flink-rts"],
  )

  library_closure(
    name = libclosure,
    srcs = [libclosure + "-hs-wrapper"],
    excludes = [
      "^/System/",
      "^/usr/lib/",
      "ld\.so.*",
      "ld-linux\.so.*",
      "ld-linux-x86-64\.so.*",
      "libgcc_s\.so.*",
      "libc\.so.*",
      "libcrypt\.so.*",
      "libdl\.so.*",
      "libjava\.so.*",
      "libjli\.so.*",
      "libjvm\.so.*",
      "libm\.so.*",
      "libpthread\.so.*",
      "librt\.so.*",
      "libresolv\.so.*",
      "libstdc++\.so.*",
      "libutil\.so.*",
      "libz\.so.*",
    ],
    **kwargs
  )

  libclosure_renamed = "libclosure-%s-renamed"
  native.genrule(
    name = libclosure_renamed,
    srcs = [libclosure],
    outs = [_mangle_dir(libclosure) + "/sparkle-app.zip"],
    cmd = "cp $< $@",
  )

  native.java_binary(
    name = name,
    create_executable = False,
    classpath_resources = [libclosure_renamed],
    resource_jars = ["@io_tweag_sparkle//:sparkle-jar"] + resource_jars,
    deploy_manifest_lines = ["Main-Class: io.tweag.sparkle.SparkMain"]
  )
<commit_msg>Fix name of closure targets in sparkle.bzl<commit_after>"""Helpers for sparkle packaging."""

load("@io_tweag_clodl//clodl:clodl.bzl", "library_closure")
load("@rules_haskell//haskell:defs.bzl", "haskell_library")

def _mangle_dir(name):
    """Creates a unique directory name from the repo name and package
      name of the package being evaluated, and a given name.
    """
    components = [native.repository_name(), native.package_name(), name]
    components = [c.replace("@", "") for c in components]
    components = [c for c in components if c]
    return "/".join(components).replace("_", "_U").replace("/", "_S")

def sparkle_package(name, src, resource_jars=[], **kwargs):
  libclosure = "libclosure-%s" % name

  libclosure_hs_wrapper = libclosure + "-hs-wrapper"
  haskell_library(
	name = libclosure_hs_wrapper,
	srcs = ["@io_tweag_sparkle//:hs-wrapper/FFIWrapper.hs"],
	deps = [src, "@io_tweag_sparkle//:base"],
    compiler_flags = ["-threaded", "-flink-rts"],
  )

  library_closure(
    name = libclosure,
    srcs = [libclosure_hs_wrapper],
    excludes = [
      "^/System/",
      "^/usr/lib/",
      "ld\.so.*",
      "ld-linux\.so.*",
      "ld-linux-x86-64\.so.*",
      "libgcc_s\.so.*",
      "libc\.so.*",
      "libcrypt\.so.*",
      "libdl\.so.*",
      "libjava\.so.*",
      "libjli\.so.*",
      "libjvm\.so.*",
      "libm\.so.*",
      "libpthread\.so.*",
      "librt\.so.*",
      "libresolv\.so.*",
      "libstdc++\.so.*",
      "libutil\.so.*",
      "libz\.so.*",
    ],
    **kwargs
  )

  libclosure_renamed = libclosure + "-renamed"
  native.genrule(
    name = libclosure_renamed,
    srcs = [libclosure],
    outs = [_mangle_dir(libclosure) + "/sparkle-app.zip"],
    cmd = "cp $< $@",
  )

  native.java_binary(
    name = name,
    create_executable = False,
    classpath_resources = [libclosure_renamed],
    resource_jars = ["@io_tweag_sparkle//:sparkle-jar"] + resource_jars,
    deploy_manifest_lines = ["Main-Class: io.tweag.sparkle.SparkMain"]
  )
<|endoftext|>


<commit_before>import tornado.ioloop
import tornado.web
import tornado.websocket
import rethinkdb as r
from tornado.options import define, options, parse_command_line

define("port", default=8888, type=int)

class IndexHandler(tornado.web.RequestHandler):
    def get(self):
        print 'rendering'
        self.render("ws_index.html")

class WebSocketHandler(tornado.websocket.WebSocketHandler):

    def check_origin(self, origin):
        return True

    def open(self, *args):
        print "New connection"
        self.write_message("Welcome!")

    def on_message(self, message):
        conn = r.connect( "localhost", 28015).repl()
        print "New message {}".format(message)
        print 'a'
        response = r.db('public').table(message).limit(10).run(conn, time_format="raw")
        print 'b'
        response = list(response)
        print 'c'

        response = {'table': message, 'rows': response}
        self.write_message(response)

    def on_close(self):
        print "Connection closed"

app = tornado.web.Application([
    (r'/', IndexHandler),
    (r'/ws/', WebSocketHandler),
])

if __name__ == '__main__':
    app.listen(options.port)
    tornado.ioloop.IOLoop.instance().start()<commit_msg>auto commit<commit_after>import tornado.ioloop
import tornado.web
import tornado.websocket
import rethinkdb as r
from tornado.options import define, options, parse_command_line

define("port", default=8888, type=int)

class IndexHandler(tornado.web.RequestHandler):
    def get(self):
        print 'rendering'
        self.render("ws_index.html")

class WebSocketHandler(tornado.websocket.WebSocketHandler):

    def check_origin(self, origin):
        return True

    def open(self, *args):
        print "New connection"
        self.write_message("Welcome!")

    def on_message(self, message):
        conn = r.connect( "localhost", 28015).repl()
        print "New message {}".format(message)
        print 'a'
        response = r.db('public').table(message).limit(10).run(conn, time_format="raw")
        print 'b'
        response = list(response)
        print 'c'

        response = {'table': message, 'rows': response}
        self.write_message(response)

    def on_close(self):
        print "Connection closed"

app = tornado.web.Application([
    (r'/', IndexHandler),
    (r'/ws/', WebSocketHandler),
])

if __name__ == '__main__':
    app.listen(options.port)
    tornado.ioloop.IOLoop.instance().start()<|endoftext|>


<commit_before>#   Eve W-Space
#   Copyright 2014 Andrew Austin and contributors
#
#   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 datetime import datetime
from API.models import SSORefreshToken
from django.conf import settings

import pytz
import urllib2
import json
import base64
import pycurl
from io import BytesIO
data = BytesIO()

def timestamp_to_datetime(timestamp):
    """Converts a UNIX Timestamp (in UTC) to a python DateTime"""
    result = datetime.fromtimestamp(timestamp).replace(tzinfo=pytz.utc)
    return result

def sso_refresh_access_token(char_id):
    refresh_token = SSORefreshToken.objects.get(
    		char_id=char_id)

    if refresh_token.valid_until < datetime.now(pytz.utc):
	    #use code to get access & refresh token
	    authorization = base64.urlsafe_b64encode(settings.SSO_CLIENT_ID + ':' + settings.SSO_SECRET_KEY)
	    data = 'grant_type=refresh_token&refresh_token=' + refresh_token.refresh_token
	    url = 'https://'+settings.SSO_LOGIN_SERVER+'/oauth/token'
	    headers = {'Content-Type': 'application/x-www-form-urlencoded',
	        'Host': settings.SSO_LOGIN_SERVER,
	        'Authorization': 'Basic '+ authorization,}
	    opener = urllib2.build_opener(urllib2.HTTPHandler)
	    requested = urllib2.Request(url, data, headers)
	    json_response = opener.open(requested).read()
	    access_response = json.loads(json_response)

	    #verify validity by requesting char info
	    char_authorization = access_response['access_token']
	    char_url = 'https://'+settings.SSO_LOGIN_SERVER+'/oauth/verify'
	    char_headers = {'User-Agent': settings.SSO_USER_AGENT,
	        'Host': settings.SSO_LOGIN_SERVER,
	        'Authorization': 'Bearer '+ char_authorization,}
	    char_requested = urllib2.Request(char_url, None, char_headers)
	    char_json_response = opener.open(char_requested).read()

	    char_response = json.loads(char_json_response)

	    refresh_token.access_token = access_response['access_token']
	    refresh_token.valid_until = char_response["ExpiresOn"]
	    refresh_token.save()

	    refresh_token = SSORefreshToken.objects.get(
    		char_id=char_response['CharacterID'])

    return refresh_token

def crest_access_data(token, requested_url, post_data = None):
    if token.valid_until < datetime.now(pytz.utc):
        token = sso_refresh_access_token(token.char_id)
    if settings.SSO_ENABLED == False:
	    return None
    authorization = token.access_token
    url = 'https://'+settings.CREST_SERVER+requested_url
    headers = {'User-Agent': settings.SSO_USER_AGENT,
        'Host': settings.CREST_SERVER,
        'Authorization': 'Bearer '+ authorization,
        'Accept': 'application/json',}
    opener = urllib2.build_opener(urllib2.HTTPHandler)
    requested = urllib2.Request(url, post_data, headers)
    open_page = opener.open(requested)
    header_response = open_page.info()
    json_response = open_page.read()
    response = json.loads(json_response)

    if response:
       return response
    return None

def sso_verify(token):
    if token.valid_until < datetime.now(pytz.utc):
        token = sso_refresh_access_token(token.char_id)

    opener = urllib2.build_opener(urllib2.HTTPHandler)
    authorization = token.access_token
    url = 'https://'+settings.SSO_LOGIN_SERVER+'/oauth/verify'
    headers = {'User-Agent': settings.SSO_USER_AGENT,
        'Host': settings.SSO_LOGIN_SERVER,
        'Authorization': 'Bearer '+ authorization,}
    requested = urllib2.Request(url, None, headers)
    json_response = opener.open(requested).read()

    response = json.loads(json_response)

    if response:
       return response
    return None

def esi_access_data(token, requested_url, call_type = None, post_data = None):
    authorization = token.access_token
    url = 'https://'+ settings.ESI_SERVER + '/' + requested_url + '?datasource=' + settings.ESI_SOURCE
    curl = pycurl.Curl()
    curl.setopt(curl.URL, url)
    curl.setopt(curl.HTTPHEADER,['Content-Type: application/json','Authorization:' 'Bearer '+ authorization])
    curl.setopt(curl.READFUNCTION, data.write)
    curl.setopt(curl.TIMEOUT, 5)
    curl.setopt(curl.VERBOSE, True)
    curl.perform()

    return data

<commit_msg>ESI support<commit_after>#   Eve W-Space
#   Copyright 2014 Andrew Austin and contributors
#
#   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 datetime import datetime
from API.models import SSORefreshToken
from django.conf import settings

import pytz
import urllib2
import json
import base64
import pycurl
import StringIO

def timestamp_to_datetime(timestamp):
    """Converts a UNIX Timestamp (in UTC) to a python DateTime"""
    result = datetime.fromtimestamp(timestamp).replace(tzinfo=pytz.utc)
    return result

def sso_refresh_access_token(char_id):
    refresh_token = SSORefreshToken.objects.get(
    		char_id=char_id)

    if refresh_token.valid_until < datetime.now(pytz.utc):
	    #use code to get access & refresh token
	    authorization = base64.urlsafe_b64encode(settings.SSO_CLIENT_ID + ':' + settings.SSO_SECRET_KEY)
	    data = 'grant_type=refresh_token&refresh_token=' + refresh_token.refresh_token
	    url = 'https://'+settings.SSO_LOGIN_SERVER+'/oauth/token'
	    headers = {'Content-Type': 'application/x-www-form-urlencoded',
	        'Host': settings.SSO_LOGIN_SERVER,
	        'Authorization': 'Basic '+ authorization,}
	    opener = urllib2.build_opener(urllib2.HTTPHandler)
	    requested = urllib2.Request(url, data, headers)
	    json_response = opener.open(requested).read()
	    access_response = json.loads(json_response)

	    #verify validity by requesting char info
	    char_authorization = access_response['access_token']
	    char_url = 'https://'+settings.SSO_LOGIN_SERVER+'/oauth/verify'
	    char_headers = {'User-Agent': settings.SSO_USER_AGENT,
	        'Host': settings.SSO_LOGIN_SERVER,
	        'Authorization': 'Bearer '+ char_authorization,}
	    char_requested = urllib2.Request(char_url, None, char_headers)
	    char_json_response = opener.open(char_requested).read()

	    char_response = json.loads(char_json_response)

	    refresh_token.access_token = access_response['access_token']
	    refresh_token.valid_until = char_response["ExpiresOn"]
	    refresh_token.save()

	    refresh_token = SSORefreshToken.objects.get(
    		char_id=char_response['CharacterID'])

    return refresh_token

def crest_access_data(token, requested_url, post_data = None):
    if token.valid_until < datetime.now(pytz.utc):
        token = sso_refresh_access_token(token.char_id)
    if settings.SSO_ENABLED == False:
	    return None
    authorization = token.access_token
    url = 'https://'+settings.CREST_SERVER+requested_url
    headers = {'User-Agent': settings.SSO_USER_AGENT,
        'Host': settings.CREST_SERVER,
        'Authorization': 'Bearer '+ authorization,
        'Accept': 'application/json',}
    opener = urllib2.build_opener(urllib2.HTTPHandler)
    requested = urllib2.Request(url, post_data, headers)
    open_page = opener.open(requested)
    header_response = open_page.info()
    json_response = open_page.read()
    response = json.loads(json_response)

    if response:
       return response
    return None

def sso_verify(token):
    if token.valid_until < datetime.now(pytz.utc):
        token = sso_refresh_access_token(token.char_id)

    opener = urllib2.build_opener(urllib2.HTTPHandler)
    authorization = token.access_token
    url = 'https://'+settings.SSO_LOGIN_SERVER+'/oauth/verify'
    headers = {'User-Agent': settings.SSO_USER_AGENT,
        'Host': settings.SSO_LOGIN_SERVER,
        'Authorization': 'Bearer '+ authorization,}
    requested = urllib2.Request(url, None, headers)
    json_response = opener.open(requested).read()

    response = json.loads(json_response)

    if response:
       return response
    return None

def esi_access_data(token, requested_url, call_type = None, post_data = None):
    if token.valid_until < datetime.now(pytz.utc):
        token = sso_refresh_access_token(token.char_id)

    data = StringIO.StringIO()

    authorization = token.access_token
    url = 'https://'+ settings.ESI_SERVER + '/' + requested_url + '?datasource=' + settings.ESI_SOURCE
    curl = pycurl.Curl()
    curl.setopt(curl.URL, url)
    curl.setopt(curl.ENCODING, 'gzip')
    curl.setopt(curl.HTTPHEADER,['Accept: application/json','Authorization: Bearer '+ authorization])
    curl.setopt(curl.WRITEFUNCTION, data.write)
    curl.setopt(curl.TIMEOUT, 5)
    curl.perform()

    response = json.loads(data.getvalue())

    if response:
       return response
    return None

<|endoftext|>


<commit_before>import numpy as np
from scipy.optimize import fsolve
import matplotlib.pyplot as plt

conserved_variables = ('Density', 'Momentum', 'Energy')
primitive_variables = ('Density', 'Velocity', 'Pressure')

def primitive_to_conservative(rho, u, p, gamma=1.4):
    mom = rho*u
    E   = p/(gamma-1.) + 0.5*rho*u**2
    return rho, mom, E

def conservative_to_primitive(rho, mom, E, gamma=1.4):
    u = mom/rho
    p = (gamma-1.)*(E - 0.5*rho*u**2)
    return rho, u, p

def exact_riemann_solution(q_l, q_r, gamma=1.4, phase_plane_curves=False):
    """Return the exact solution to the Riemann problem with initial states
       q_l, q_r.  The solution is given in terms of a list of states, a list of
       speeds (each of which may be a pair in case of a rarefaction fan), and a
       function reval(xi) that gives the solution at a point xi=x/t.

       The input and output vectors are the conserved quantities.

       If phase_plane_curves==True, then the appropriate Hugoniot Locus and/or
       integral curve is returned for the 1- and 3-waves.
    """

    rho_l, u_l, p_l = conservative_to_primitive(*q_l)
    rho_r, u_r, p_r = conservative_to_primitive(*q_r)

    # Compute left and right state sound speeds
    c_l = np.sqrt(gamma*p_l/rho_l)
    c_r = np.sqrt(gamma*p_r/rho_r)

    beta = (gamma+1.)/(gamma-1.)

    # Check for cavitation
    if u_l - u_r + 2*(c_l+c_r)/(gamma-1.) < 0:
        print('Cavitation detected!  Exiting.')
        return None

    # Define the integral curves and hugoniot loci
    integral_curve_1 = lambda p: u_l + 2*c_l/(gamma-1.)* \
                (1.-(max(p,0)/p_l)**((gamma-1.)/(2.*gamma)))
    integral_curve_3 = lambda p: u_r - 2*c_r/(gamma-1.)* \
                (1.-(max(p,0)/p_r)**((gamma-1.)/(2.*gamma)))
    hugoniot_locus_1 = lambda p: u_l + 2*c_l/np.sqrt(2*gamma*(gamma-1.)) \
                * ((1-p/p_l)/np.sqrt(1+beta*p/p_l))
    hugoniot_locus_3 = lambda p: u_r - 2*c_r/np.sqrt(2*gamma*(gamma-1.)) \
                * ((1-p/p_r)/np.sqrt(1+beta*p/p_r))

    # Check whether the 1-wave is a shock or rarefaction
    def phi_l(p):
        if p >= p_l: return hugoniot_locus_1(p)
        else: return integral_curve_1(p)

    # Check whether the 1-wave is a shock or rarefaction
    def phi_r(p):
        if p >= p_r: return hugoniot_locus_3(p)
        else: return integral_curve_3(p)

    phi = lambda p: phi_l(p)-phi_r(p)

    # Compute middle state p, u by finding curve intersection
    p, info, ier, msg = fsolve(phi, (p_l+p_r)/2., full_output=True, xtol=1.e-14)
    # For strong rarefactions, sometimes fsolve needs help
    if ier != 1:
        p, info, ier, msg = fsolve(phi, (p_l+p_r)/2., full_output=True, factor=0.1, xtol=1.e-10)
        # This should not happen:
        if ier != 1:
            print('Warning: fsolve did not converge.')
            print(msg)

    u = phi_l(p)

    # Find middle state densities
    rho_l_star = (p/p_l)**(1./gamma) * rho_l
    rho_r_star = (p/p_r)**(1./gamma) * rho_r

    # compute the wave speeds
    ws = np.zeros(5)
    wave_types = ['', 'contact', '']
    # The contact speed:
    ws[2] = u

    # Find shock and rarefaction speeds
    if p > p_l:
        wave_types[0] = 'shock'
        ws[0] = (rho_l*u_l - rho_l_star*u)/(rho_l - rho_l_star)
        ws[1] = ws[0]
    else:
        wave_types[0] = 'raref'
        c_l_star = np.sqrt(gamma*p/rho_l_star)
        ws[0] = u_l - c_l
        ws[1] = u - c_l_star

    if p > p_r:
        wave_types[2] = 'shock'
        ws[4] = (rho_r*u_r - rho_r_star*u)/(rho_r - rho_r_star)
        ws[3] = ws[4]
    else:
        wave_types[2] = 'raref'
        c_r_star = np.sqrt(gamma*p/rho_r_star)
        ws[3] = u+c_r_star
        ws[4] = u_r + c_r

    # Find solution inside rarefaction fans (in primitive variables)
    def raref1(xi):
        u1 = ((gamma-1.)*u_l + 2*(c_l + xi))/(gamma+1.)
        rho1 = (rho_l**gamma*(u1-xi)**2/(gamma*p_l))**(1./(gamma-1.))
        p1 = p_l*(rho1/rho_l)**gamma
        return rho1, u1, p1

    def raref3(xi):
        u3 = ((gamma-1.)*u_r - 2*(c_r - xi))/(gamma+1.)
        rho3 = (rho_r**gamma*(xi-u3)**2/(gamma*p_r))**(1./(gamma-1.))
        p3 = p_r*(rho3/rho_r)**gamma
        return rho3, u3, p3

    q_l_star = np.squeeze(np.array(primitive_to_conservative(rho_l_star,u,p)))
    q_r_star = np.squeeze(np.array(primitive_to_conservative(rho_r_star,u,p)))

    states = np.column_stack([q_l,q_l_star,q_r_star,q_r])
    speeds = [[], ws[2], []]
    if wave_types[0] is 'shock':
        speeds[0] = ws[0]
    else:
        speeds[0] = (ws[0],ws[1])
    if wave_types[2] is 'shock':
        speeds[2] = ws[3]
    else:
        speeds[2] = (ws[3],ws[4])

    def reval(xi):
        r"""Returns the Riemann solution in primitive variables for any
            value of xi = x/t.
        """
        rar1 = raref1(xi)
        rar3 = raref3(xi)
        rho_out =  (xi<=ws[0]                  )*rho_l      \
                 + (xi>ws[0])*(xi<=ws[1])*rar1[0]    \
                 + (xi>ws[1])*(xi<=ws[2]   )*rho_l_star \
                 + (xi>ws[2])   *(xi<=ws[3])*rho_r_star \
                 + (xi>ws[3])*(xi<=ws[4])*rar3[0]    \
                 + (xi>ws[4]                   )*rho_r

        u_out   =  (xi<=ws[0]                  )*u_l     \
                 + (xi>ws[0])*(xi<=ws[1])*rar1[1] \
                 + (xi>ws[1])*(xi<=ws[2]   )*u       \
                 + (xi>ws[2]   )*(xi<=ws[3])*u       \
                 + (xi>ws[3])*(xi<=ws[4])*rar3[1] \
                 + (xi>ws[4]                   )*u_r

        p_out   =  (xi<=ws[0]                  )*p_l     \
                 + (xi>ws[0])*(xi<=ws[1])*rar1[2] \
                 + (xi>ws[1])*(xi<=ws[2]   )*p       \
                 + (xi>ws[2]   )*(xi<=ws[3])*p       \
                 + (xi>ws[3])*(xi<=ws[4])*rar3[2] \
                 + (xi>ws[4]                   )*p_r
        return primitive_to_conservative(rho_out,u_out,p_out)

    if phase_plane_curves:
        return states, speeds, reval, wave_types, (p, phi_l, phi_r)
    else:
        return states, speeds, reval, wave_types

def phase_plane_plot(left_state, right_state, gamma=1.4):
    r"""Plot the Hugoniot loci or integral curves in the p-u plane."""
    # Solve Riemann problem
    q_left  = primitive_to_conservative(*left_state)
    q_right = primitive_to_conservative(*right_state)
    ex_states, ex_speeds, reval, wave_types, ppc = \
                        exact_riemann_solution(q_left, q_right, gamma,
                                               phase_plane_curves=True)
    pm, w1, w3 = ppc

    # Set plot bounds
    fig, ax = plt.subplots()
    x = (left_state.Pressure, pm, right_state.Pressure)
    y = (left_state.Velocity, w1(pm), right_state.Velocity)
    xmax, xmin = max(x), min(x)
    ymax, ymin = max(y), min(y)
    dx, dy = xmax - xmin, ymax - ymin
    ax.set_xlim(xmin - 0.1*dx, xmax + 0.1*dx)
    ax.set_ylim(ymin - 0.1*dy, ymax + 0.1*dy)
    ax.set_xlabel('Pressure (p)')
    ax.set_ylabel('Velocity (u)')

    # Plot curves
    w1v, w3v = (np.vectorize(w1), np.vectorize(w3))
    press1 = np.linspace(left_state.Pressure, pm)
    press3 = np.linspace(right_state.Pressure, pm)
    if type(ex_speeds[0]) not in (tuple, list):  # this is a jump
        color = 'r'
    else:
        color = 'b'
    ax.plot(press1,w1v(press1),color,lw=2)
    if type(ex_speeds[2]) not in (tuple, list):  # this is a jump
        color = 'r'
    else:
        color = 'b'
    ax.plot(press3,w3v(press3),color,lw=2)
    for xp,yp in zip(x,y):
        ax.plot(xp,yp,'ok',markersize=10)
    # Label states
    for i,label in enumerate(('Left', 'Middle', 'Right')):
        ax.text(x[i] + 0.025*dx,y[i] + 0.025*dy,label)
<commit_msg>Deal with vacuum in ideal gas Euler Riemann solver.<commit_after>import numpy as np
from scipy.optimize import fsolve
import matplotlib.pyplot as plt

conserved_variables = ('Density', 'Momentum', 'Energy')
primitive_variables = ('Density', 'Velocity', 'Pressure')

def primitive_to_conservative(rho, u, p, gamma=1.4):
    mom = rho*u
    E   = p/(gamma-1.) + 0.5*rho*u**2
    return rho, mom, E

def conservative_to_primitive(rho, mom, E, gamma=1.4):
    u = mom/rho
    p = (gamma-1.)*(E - 0.5*rho*u**2)
    return rho, u, p

def exact_riemann_solution(q_l, q_r, gamma=1.4, phase_plane_curves=False):
    """Return the exact solution to the Riemann problem with initial states
       q_l, q_r.  The solution is given in terms of a list of states, a list of
       speeds (each of which may be a pair in case of a rarefaction fan), and a
       function reval(xi) that gives the solution at a point xi=x/t.

       The input and output vectors are the conserved quantities.

       If phase_plane_curves==True, then the appropriate Hugoniot Locus and/or
       integral curve is returned for the 1- and 3-waves.
    """

    rho_l, u_l, p_l = conservative_to_primitive(*q_l)
    rho_r, u_r, p_r = conservative_to_primitive(*q_r)

    # Compute left and right state sound speeds
    c_l = np.sqrt(gamma*p_l/rho_l)
    c_r = np.sqrt(gamma*p_r/rho_r)

    beta = (gamma+1.)/(gamma-1.)

    ws = np.zeros(5)
    wave_types = ['', 'contact', '']

    if u_l - u_r + 2*(c_l+c_r)/(gamma-1.) < 0:
        # Middle states are vacuum
        p = 0.
        rho_l_star = 0.
        rho_r_star = 0.
        u_vacuum_l = u_l + 2*c_l/(gamma-1.)
        u_vacuum_r = u_r - 2*c_r/(gamma-1.)
        u = 0.5*(u_vacuum_l + u_vacuum_r)
        ws[0] = u_l - c_l
        ws[1] = u_vacuum_l
        ws[2] = u
        ws[3] = u_vacuum_r
        ws[4] = u_r + c_r
        wave_types = ['raref', 'contact', 'raref']

    else:
        # Define the integral curves and hugoniot loci
        integral_curve_1 = lambda p: u_l + 2*c_l/(gamma-1.)* \
                    (1.-(max(p,0)/p_l)**((gamma-1.)/(2.*gamma)))
        integral_curve_3 = lambda p: u_r - 2*c_r/(gamma-1.)* \
                    (1.-(max(p,0)/p_r)**((gamma-1.)/(2.*gamma)))
        hugoniot_locus_1 = lambda p: u_l + 2*c_l/np.sqrt(2*gamma*(gamma-1.)) \
                    * ((1-p/p_l)/np.sqrt(1+beta*p/p_l))
        hugoniot_locus_3 = lambda p: u_r - 2*c_r/np.sqrt(2*gamma*(gamma-1.)) \
                    * ((1-p/p_r)/np.sqrt(1+beta*p/p_r))

        # Check whether the 1-wave is a shock or rarefaction
        def phi_l(p):
            if p >= p_l: return hugoniot_locus_1(p)
            else: return integral_curve_1(p)

        # Check whether the 1-wave is a shock or rarefaction
        def phi_r(p):
            if p >= p_r: return hugoniot_locus_3(p)
            else: return integral_curve_3(p)

        phi = lambda p: phi_l(p)-phi_r(p)

        exp = (1.-gamma)/(2.*gamma)
        guess = ((c_l + c_r - (gamma-1.)*(u_r-u_l)/2.)/(c_l*p_l**exp+c_r*p_r**exp))**(-1./exp)
        # Compute middle state p, u by finding curve intersection
        p, info, ier, msg = fsolve(phi, guess, full_output=True, xtol=1.e-14)
        # For strong rarefactions, sometimes fsolve needs help
        if ier != 1:
            p, info, ier, msg = fsolve(phi, guess, full_output=True, factor=0.1, xtol=1.e-10)
            # This should not happen:
            if ier != 1:
                print('Warning: fsolve did not converge.')
                print(msg)

        u = phi_l(p)

        ws[2] = u

        # Find shock and rarefaction speeds
        if p > p_l:
            wave_types[0] = 'shock'
            rho_l_star = rho_l*(1+beta*p/p_l)/(p/p_l+beta)
            ws[0] = (rho_l*u_l - rho_l_star*u)/(rho_l - rho_l_star)
            ws[1] = ws[0]
        else:
            wave_types[0] = 'raref'
            rho_l_star = (p/p_l)**(1./gamma) * rho_l
            c_l_star = np.sqrt(gamma*p/rho_l_star)
            ws[0] = u_l - c_l
            ws[1] = u - c_l_star

        if p > p_r:
            wave_types[2] = 'shock'
            rho_r_star = rho_r*(1+beta*p/p_r)/(p/p_r+beta)
            ws[4] = (rho_r*u_r - rho_r_star*u)/(rho_r - rho_r_star)
            ws[3] = ws[4]
        else:
            wave_types[2] = 'raref'
            rho_r_star = (p/p_r)**(1./gamma) * rho_r
            c_r_star = np.sqrt(gamma*p/rho_r_star)
            ws[3] = u+c_r_star
            ws[4] = u_r + c_r

    # Find solution inside rarefaction fans (in primitive variables)
    def raref1(xi):
        u1 = ((gamma-1.)*u_l + 2*(c_l + xi))/(gamma+1.)
        rho1 = (rho_l**gamma*(u1-xi)**2/(gamma*p_l))**(1./(gamma-1.))
        p1 = p_l*(rho1/rho_l)**gamma
        return rho1, u1, p1

    def raref3(xi):
        u3 = ((gamma-1.)*u_r - 2*(c_r - xi))/(gamma+1.)
        rho3 = (rho_r**gamma*(xi-u3)**2/(gamma*p_r))**(1./(gamma-1.))
        p3 = p_r*(rho3/rho_r)**gamma
        return rho3, u3, p3

    q_l_star = np.squeeze(np.array(primitive_to_conservative(rho_l_star,u,p)))
    q_r_star = np.squeeze(np.array(primitive_to_conservative(rho_r_star,u,p)))

    states = np.column_stack([q_l,q_l_star,q_r_star,q_r])
    speeds = [[], ws[2], []]
    if wave_types[0] is 'shock':
        speeds[0] = ws[0]
    else:
        speeds[0] = (ws[0],ws[1])
    if wave_types[2] is 'shock':
        speeds[2] = ws[3]
    else:
        speeds[2] = (ws[3],ws[4])

    def reval(xi):
        r"""Returns the Riemann solution in primitive variables for any
            value of xi = x/t.
        """
        rar1 = raref1(xi)
        rar3 = raref3(xi)
        rho_out =  (xi<=ws[0]                  )*rho_l      \
                 + (xi>ws[0])*(xi<=ws[1])*rar1[0]    \
                 + (xi>ws[1])*(xi<=ws[2]   )*rho_l_star \
                 + (xi>ws[2])   *(xi<=ws[3])*rho_r_star \
                 + (xi>ws[3])*(xi<=ws[4])*rar3[0]    \
                 + (xi>ws[4]                   )*rho_r

        u_out   =  (xi<=ws[0]                  )*u_l     \
                 + (xi>ws[0])*(xi<=ws[1])*rar1[1] \
                 + (xi>ws[1])*(xi<=ws[2]   )*u       \
                 + (xi>ws[2]   )*(xi<=ws[3])*u       \
                 + (xi>ws[3])*(xi<=ws[4])*rar3[1] \
                 + (xi>ws[4]                   )*u_r

        p_out   =  (xi<=ws[0]                  )*p_l     \
                 + (xi>ws[0])*(xi<=ws[1])*rar1[2] \
                 + (xi>ws[1])*(xi<=ws[2]   )*p       \
                 + (xi>ws[2]   )*(xi<=ws[3])*p       \
                 + (xi>ws[3])*(xi<=ws[4])*rar3[2] \
                 + (xi>ws[4]                   )*p_r
        return primitive_to_conservative(rho_out,u_out,p_out)

    if phase_plane_curves:
        return states, speeds, reval, wave_types, (p, phi_l, phi_r)
    else:
        return states, speeds, reval, wave_types

def phase_plane_plot(left_state, right_state, gamma=1.4):
    r"""Plot the Hugoniot loci or integral curves in the p-u plane."""
    # Solve Riemann problem
    q_left  = primitive_to_conservative(*left_state)
    q_right = primitive_to_conservative(*right_state)
    ex_states, ex_speeds, reval, wave_types, ppc = \
                        exact_riemann_solution(q_left, q_right, gamma,
                                               phase_plane_curves=True)
    pm, w1, w3 = ppc

    # Set plot bounds
    fig, ax = plt.subplots()
    x = (left_state.Pressure, pm, right_state.Pressure)
    y = (left_state.Velocity, w1(pm), right_state.Velocity)
    xmax, xmin = max(x), min(x)
    ymax, ymin = max(y), min(y)
    dx, dy = xmax - xmin, ymax - ymin
    ax.set_xlim(xmin - 0.1*dx, xmax + 0.1*dx)
    ax.set_ylim(ymin - 0.1*dy, ymax + 0.1*dy)
    ax.set_xlabel('Pressure (p)')
    ax.set_ylabel('Velocity (u)')

    # Plot curves
    w1v, w3v = (np.vectorize(w1), np.vectorize(w3))
    press1 = np.linspace(left_state.Pressure, pm)
    press3 = np.linspace(right_state.Pressure, pm)
    if type(ex_speeds[0]) not in (tuple, list):  # this is a jump
        color = 'r'
    else:
        color = 'b'
    ax.plot(press1,w1v(press1),color,lw=2)
    if type(ex_speeds[2]) not in (tuple, list):  # this is a jump
        color = 'r'
    else:
        color = 'b'
    ax.plot(press3,w3v(press3),color,lw=2)
    for xp,yp in zip(x,y):
        ax.plot(xp,yp,'ok',markersize=10)
    # Label states
    for i,label in enumerate(('Left', 'Middle', 'Right')):
        ax.text(x[i] + 0.025*dx,y[i] + 0.025*dy,label)
<|endoftext|>


<commit_before>#!/usr/bin/python -u
#
# yamond.py, Copyright 2010, The Beanstalks Project ehf.
#                            http://beanstalks-project.net/
#
# This is a class implementing a flexible metric-store and an HTTP
# thread for browsing the numbers.
#
#############################################################################
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
#############################################################################
#
import getopt
import os
import random
import re
import select
import socket
import struct
import sys
import threading
import time
import traceback
import urllib

import BaseHTTPServer
try:
  from urlparse import parse_qs, urlparse
except Exception, e:
  from cgi import parse_qs
  from urlparse import urlparse

class YamonRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
  def do_yamon_vars(self):
    self.send_response(200)
    self.send_header('Content-Type', 'text/plain')
    self.send_header('Cache-Control', 'no-cache')
    self.end_headers()
    self.wfile.write(self.server.yamond.render_vars_text())

  def do_404(self):
    self.send_response(404)
    self.send_header('Content-Type', 'text/html')
    self.end_headers()
    self.wfile.write('
404: What? Where? Cannot find it!
')

  def do_root(self):
    self.send_response(200)
    self.send_header('Content-Type', 'text/html')
    self.end_headers()
    self.wfile.write('
Hello!
')

  def handle_path(self, path, query):
    if path == '/vars.txt':
      self.do_yamon_vars()
    elif path == '/':
      self.do_root()
    else:
      self.do_404()

  def do_GET(self):
    self.server.yamond.ladd('yamond_hits', time.time()*1000)
    self.server.yamond.vscale('yamond_hits', 1, 1)

    (scheme, netloc, path, params, query, frag) = urlparse(self.path)
    qs = parse_qs(query)
    return self.handle_path(path, query)

class YamonHttpServer(BaseHTTPServer.HTTPServer):
  def __init__(self, yamond, handler):
    BaseHTTPServer.HTTPServer.__init__(self, yamond.sspec, handler)
    self.yamond = yamond

class YamonD(threading.Thread):
  """Handle HTTP in a separate thread."""

  def __init__(self, sspec,
               server=YamonHttpServer,
               handler=YamonRequestHandler):
    threading.Thread.__init__(self)
    self.sspec = sspec
    self.httpd = None
    self.serve = True
    self.values = {}
    self.lists = {}

    self.vset('yamond_hits', 0)
    self.lcreate('yamond_hits', 50)

  def vmax(self, var, value):
    if value > self.values[var]: self.values[var] = value

  def vscale(self, var, ratio, add=0):
    if var in self.values:
      self.values[var] *= ratio
      self.values[var] += add

  def vset(self, var, value):
    self.values[var] = value

  def vmin(self, var, value):
    if value < self.values[var]: self.values[var] = value

  def lcreate(self, listn, elems):
    self.lists[listn] = [elems, 0, ['' for x in xrange(0, elems)]]

  def ladd(self, listn, value):
    list = self.lists[listn]
    list[2][list[1]] = value
    list[1] += 1
    list[1] %= list[0]

  def render_vars_text(self):
    data = []
    for var in self.values:
      data.append('%s: %s\n' % (var, self.values[var]))

    for lname in self.lists:
      (elems, offset, list) = self.lists[lname]
      l = list[offset:]
      l.extend(list[:offset])
      data.append('%s: %s\n' % (lname, ' '.join(['%s' % x for x in l])))

    return ''.join(data)

  def quit(self):
    if self.httpd:
      self.serve = False
      urllib.urlopen('http://%s:%s/exiting/' % self.sspec,
                     proxies={}).readlines()

  def run(self):
    self.httpd = server(self, handler)
    while self.serve: self.httpd.handle_request()

if __name__ == '__main__':
  yd = YamonD(('', 7999))
  yd.vset('bjarni', 100)
  yd.lcreate('foo', 2)
  yd.ladd('foo', 1)
  yd.ladd('foo', 2)
  yd.ladd('foo', 3)
  yd.run()

<commit_msg>Changed vscale, aded vdel and vadd, removed yamond_hits variables<commit_after>#!/usr/bin/python -u
#
# yamond.py, Copyright 2010, The Beanstalks Project ehf.
#                            http://beanstalks-project.net/
#
# This is a class implementing a flexible metric-store and an HTTP
# thread for browsing the numbers.
#
#############################################################################
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
#############################################################################
#
import getopt
import os
import random
import re
import select
import socket
import struct
import sys
import threading
import time
import traceback
import urllib

import BaseHTTPServer
try:
  from urlparse import parse_qs, urlparse
except Exception, e:
  from cgi import parse_qs
  from urlparse import urlparse

class YamonRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
  def do_yamon_vars(self):
    self.send_response(200)
    self.send_header('Content-Type', 'text/plain')
    self.send_header('Cache-Control', 'no-cache')
    self.end_headers()
    self.wfile.write(self.server.yamond.render_vars_text())

  def do_404(self):
    self.send_response(404)
    self.send_header('Content-Type', 'text/html')
    self.end_headers()
    self.wfile.write('
404: What? Where? Cannot find it!
')

  def do_root(self):
    self.send_response(200)
    self.send_header('Content-Type', 'text/html')
    self.end_headers()
    self.wfile.write('
Hello!
')

  def handle_path(self, path, query):
    if path == '/vars.txt':
      self.do_yamon_vars()
    elif path == '/':
      self.do_root()
    else:
      self.do_404()

  def do_GET(self):
    (scheme, netloc, path, params, query, frag) = urlparse(self.path)
    qs = parse_qs(query)
    return self.handle_path(path, query)

class YamonHttpServer(BaseHTTPServer.HTTPServer):
  def __init__(self, yamond, handler):
    BaseHTTPServer.HTTPServer.__init__(self, yamond.sspec, handler)
    self.yamond = yamond

class YamonD(threading.Thread):
  """Handle HTTP in a separate thread."""

  def __init__(self, sspec,
               server=YamonHttpServer,
               handler=YamonRequestHandler):
    threading.Thread.__init__(self)
    self.sspec = sspec
    self.httpd = None
    self.serve = True
    self.values = {}
    self.lists = {}

  def vmax(self, var, value):
    if value > self.values[var]: self.values[var] = value

  def vscale(self, var, ratio, add=0):
    if var not in self.values: self.values[var] = 0
    self.values[var] *= ratio
    self.values[var] += add

  def vset(self, var, value):
    self.values[var] = value

  def vadd(self, var, value, wrap=None):
    if var not in self.values: self.values[var] = 0
    self.values[var] += value
    if wrap is not None and self.values[var] >= wrap:
      self.values[var] -= wrap

  def vmin(self, var, value):
    if value < self.values[var]: self.values[var] = value

  def vdel(self, var):
    if var in self.values: del self.values[var]

  def lcreate(self, listn, elems):
    self.lists[listn] = [elems, 0, ['' for x in xrange(0, elems)]]

  def ladd(self, listn, value):
    list = self.lists[listn]
    list[2][list[1]] = value
    list[1] += 1
    list[1] %= list[0]

  def render_vars_text(self):
    data = []
    for var in self.values:
      data.append('%s: %s\n' % (var, self.values[var]))

    for lname in self.lists:
      (elems, offset, list) = self.lists[lname]
      l = list[offset:]
      l.extend(list[:offset])
      data.append('%s: %s\n' % (lname, ' '.join(['%s' % x for x in l])))

    return ''.join(data)

  def quit(self):
    if self.httpd:
      self.serve = False
      urllib.urlopen('http://%s:%s/exiting/' % self.sspec,
                     proxies={}).readlines()

  def run(self):
    self.httpd = server(self, handler)
    while self.serve: self.httpd.handle_request()

if __name__ == '__main__':
  yd = YamonD(('', 7999))
  yd.vset('bjarni', 100)
  yd.lcreate('foo', 2)
  yd.ladd('foo', 1)
  yd.ladd('foo', 2)
  yd.ladd('foo', 3)
  yd.run()

<|endoftext|>


<commit_before># encoding: utf-8
#
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
# You can obtain one at http:# mozilla.org/MPL/2.0/.
#
# Contact: Kyle Lahnakoski (kyle@lahnakoski.com)
#
from __future__ import absolute_import, division, unicode_literals

import datetime
from collections import Mapping

from jx_base import Column, TableDesc
from jx_base.schema import Schema
from mo_collections import UniqueIndex
from mo_dots import (
    Data,
    FlatList,
    NullType,
    ROOT_PATH,
    concat_field,
    is_container,
    join_field,
    listwrap,
    split_field,
    unwraplist,
    to_data,
)
from mo_future import binary_type, items, long, none_type, reduce, text
from mo_json import INTEGER, NUMBER, STRING, python_type_to_json_type
from mo_times.dates import Date

DEBUG = False
META_TABLES_NAME = "meta.tables"
META_COLUMNS_NAME = "meta.columns"
META_COLUMNS_TYPE_NAME = "column"
singlton = None

def get_schema_from_list(
    table_name, frum, native_type_to_json_type=python_type_to_json_type
):
    """
    SCAN THE LIST FOR COLUMN TYPES
    """
    columns = UniqueIndex(keys=("name",))
    _get_schema_from_list(
        frum,
        ".",
        parent=".",
        nested_path=ROOT_PATH,
        columns=columns,
        native_type_to_json_type=native_type_to_json_type,
    )
    return Schema(table_name=table_name, columns=list(columns))

def _get_schema_from_list(
    frum,  # The list
    table_name,  # Name of the table this list holds records for
    parent,  # parent path
    nested_path,  # each nested array, in reverse order
    columns,  # map from full name to column definition
    native_type_to_json_type,  # dict from storage type name to json type name
):
    for d in frum:
        row_type = python_type_to_json_type[d.__class__]

        if row_type != "object":
            # EXPECTING PRIMITIVE VALUE
            full_name = parent
            column = columns[full_name]
            if not column:
                column = Column(
                    name=concat_field(table_name, full_name),
                    es_column=full_name,
                    es_index=".",
                    es_type=d.__class__.__name__,
                    jx_type=None,  # WILL BE SET BELOW
                    last_updated=Date.now(),
                    nested_path=nested_path,
                    multi=1,
                )
                columns.add(column)
            column.es_type = _merge_python_type(column.es_type, d.__class__)
            column.jx_type = native_type_to_json_type[column.es_type]
        else:
            for name, value in d.items():
                full_name = concat_field(parent, name)
                column = columns[full_name]
                if not column:
                    column = Column(
                        name=concat_field(table_name, full_name),
                        es_column=full_name,
                        es_index=".",
                        es_type=value.__class__.__name__,
                        jx_type=None,  # WILL BE SET BELOW
                        last_updated=Date.now(),
                        nested_path=nested_path,
                        multi=1,
                    )
                    columns.add(column)
                if is_container(value):  # GET TYPE OF MULTIVALUE
                    v = list(value)
                    if len(v) == 0:
                        this_type_name = none_type.__name__
                    elif len(v) == 1:
                        this_type_name = v[0].__class__.__name__
                    else:
                        this_type_name = reduce(
                            _merge_python_type, (vi.__class__.__name__ for vi in value)
                        )
                else:
                    this_type_name = value.__class__.__name__
                column.es_type = _merge_python_type(column.es_type, this_type_name)
                try:
                    column.jx_type = native_type_to_json_type[column.es_type]
                except Exception as e:
                    raise e

                if this_type_name in {"object", "dict", "Mapping", "Data"}:
                    _get_schema_from_list(
                        [value],
                        table_name,
                        full_name,
                        nested_path,
                        columns,
                        native_type_to_json_type,
                    )
                elif this_type_name in {"list", "FlatList"}:
                    np = listwrap(nested_path)
                    newpath = unwraplist([join_field(split_field(np[0]) + [name])] + np)
                    _get_schema_from_list(
                        value, table_name, full_name, newpath, columns
                    )

def get_id(column):
    """
    :param column:
    :return: Elasticsearch id for column
    """
    return column.es_index + "|" + column.es_column

META_COLUMNS_DESC = TableDesc(
    name=META_COLUMNS_NAME,
    url=None,
    query_path=ROOT_PATH,
    last_updated=Date.now(),
    columns=to_data(
        [
            Column(
                name=c,
                es_index=META_COLUMNS_NAME,
                es_column=c,
                es_type="keyword",
                jx_type=STRING,
                last_updated=Date.now(),
                nested_path=ROOT_PATH,
                multi=1,
            )
            for c in [
                "name",
                "es_type",
                "jx_type",
                "es_column",
                "es_index",
                "partitions",
            ]
        ]
        + [
            Column(
                name=c,
                es_index=META_COLUMNS_NAME,
                es_column=c,
                es_type="integer",
                jx_type=INTEGER,
                last_updated=Date.now(),
                nested_path=ROOT_PATH,
                multi=1
            )
            for c in ["count", "cardinality", "multi"]
        ]
        + [
            Column(
                name="nested_path",
                es_index=META_COLUMNS_NAME,
                es_column="nested_path",
                es_type="keyword",
                jx_type=STRING,
                last_updated=Date.now(),
                nested_path=ROOT_PATH,
                multi=4,
            ),
            Column(
                name="last_updated",
                es_index=META_COLUMNS_NAME,
                es_column="last_updated",
                es_type="double",
                jx_type=NUMBER,
                last_updated=Date.now(),
                nested_path=ROOT_PATH,
                multi=1
            ),
        ]
    ),
)

META_TABLES_DESC = TableDesc(
    name=META_TABLES_NAME,
    url=None,
    query_path=ROOT_PATH,
    last_updated=Date.now(),
    columns=to_data(
        [
            Column(
                name=c,
                es_index=META_TABLES_NAME,
                es_column=c,
                es_type="string",
                jx_type=STRING,
                last_updated=Date.now(),
                nested_path=ROOT_PATH,
                multi=1
            )
            for c in ["name", "url", "query_path"]
        ]
        + [
            Column(
                name=c,
                es_index=META_TABLES_NAME,
                es_column=c,
                es_type="integer",
                jx_type=INTEGER,
                last_updated=Date.now(),
                nested_path=ROOT_PATH,
                multi=1
            )
            for c in ["timestamp"]
        ]
    ),
)

SIMPLE_METADATA_COLUMNS = (  # FOR PURELY INTERNAL PYTHON LISTS, NOT MAPPING TO ANOTHER DATASTORE
    [
        Column(
            name=c,
            es_index=META_COLUMNS_NAME,
            es_column=c,
            es_type="string",
            jx_type=STRING,
            last_updated=Date.now(),
            nested_path=ROOT_PATH,
            multi=1

        )
        for c in ["table", "name", "type"]
    ]
    + [
        Column(
            name=c,
            es_index=META_COLUMNS_NAME,
            es_column=c,
            es_type="long",
            jx_type=INTEGER,
            last_updated=Date.now(),
            nested_path=ROOT_PATH,
            multi=1
        )
        for c in ["count", "cardinality", "multi"]
    ]
    + [
        Column(
            name="last_updated",
            es_index=META_COLUMNS_NAME,
            es_column="last_updated",
            es_type="time",
            jx_type=NUMBER,
            last_updated=Date.now(),
            nested_path=ROOT_PATH,
            multi=1
        ),
        Column(
            name="nested_path",
            es_index=META_COLUMNS_NAME,
            es_column="nested_path",
            es_type="string",
            jx_type=STRING,
            last_updated=Date.now(),
            nested_path=ROOT_PATH,
            multi=4

        )
    ]
)

_merge_order = {
    none_type: 0,
    NullType: 1,
    bool: 2,
    int: 3,
    long: 3,
    Date: 4,
    datetime: 4,
    float: 5,
    text: 6,
    binary_type: 6,
    object: 7,
    dict: 8,
    Mapping: 9,
    Data: 10,
    list: 11,
    FlatList: 12,
}

for k, v in items(_merge_order):
    _merge_order[k.__name__] = v

def _merge_python_type(A, B):
    a = _merge_order[A]
    b = _merge_order[B]

    if a >= b:
        output = A
    else:
        output = B

    if isinstance(output, str):
        return output
    else:
        return output.__name__
<commit_msg>pass test<commit_after># encoding: utf-8
#
#
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this file,
# You can obtain one at http:# mozilla.org/MPL/2.0/.
#
# Contact: Kyle Lahnakoski (kyle@lahnakoski.com)
#
from __future__ import absolute_import, division, unicode_literals

import datetime
from collections import Mapping

from jx_base import Column, TableDesc
from jx_base.schema import Schema
from mo_collections import UniqueIndex
from mo_dots import (
    Data,
    FlatList,
    NullType,
    ROOT_PATH,
    concat_field,
    is_container,
    join_field,
    listwrap,
    split_field,
    unwraplist,
    to_data,
)
from mo_future import binary_type, items, long, none_type, reduce, text
from mo_json import INTEGER, NUMBER, STRING, python_type_to_json_type
from mo_times.dates import Date

DEBUG = False
META_TABLES_NAME = "meta.tables"
META_COLUMNS_NAME = "meta.columns"
META_COLUMNS_TYPE_NAME = "column"
singlton = None

def get_schema_from_list(
    table_name, frum, native_type_to_json_type=python_type_to_json_type
):
    """
    SCAN THE LIST FOR COLUMN TYPES
    """
    columns = UniqueIndex(keys=("name",))
    _get_schema_from_list(
        frum,
        ".",
        parent=".",
        nested_path=ROOT_PATH,
        columns=columns,
        native_type_to_json_type=native_type_to_json_type,
    )
    return Schema(table_name=table_name, columns=list(columns))

def _get_schema_from_list(
    frum,  # The list
    table_name,  # Name of the table this list holds records for
    parent,  # parent path
    nested_path,  # each nested array, in reverse order
    columns,  # map from full name to column definition
    native_type_to_json_type,  # dict from storage type name to json type name
):
    for d in frum:
        row_type = python_type_to_json_type[d.__class__]

        if row_type != "object":
            # EXPECTING PRIMITIVE VALUE
            full_name = parent
            column = columns[full_name]
            if not column:
                es_type = d.__class__

                column = Column(
                    name=concat_field(table_name, full_name),
                    es_column=full_name,
                    es_index=".",
                    es_type=es_type,
                    jx_type=native_type_to_json_type[es_type],
                    last_updated=Date.now(),
                    nested_path=nested_path,
                    multi=1,
                )
                columns.add(column)
            else:
                column.es_type = _merge_python_type(column.es_type, d.__class__)
                column.jx_type = native_type_to_json_type[column.es_type]
        else:
            for name, value in d.items():
                full_name = concat_field(parent, name)
                column = columns[full_name]

                if is_container(value):  # GET TYPE OF MULTIVALUE
                    v = list(value)
                    if len(v) == 0:
                        es_type = none_type.__name__
                    elif len(v) == 1:
                        es_type = v[0].__class__.__name__
                    else:
                        es_type = reduce(
                            _merge_python_type, (vi.__class__.__name__ for vi in value)
                        )
                else:
                    es_type = value.__class__.__name__

                if not column:
                    column = Column(
                        name=concat_field(table_name, full_name),
                        es_column=full_name,
                        es_index=".",
                        es_type=es_type,
                        jx_type=native_type_to_json_type[es_type],
                        last_updated=Date.now(),
                        nested_path=nested_path,
                        multi=1
                    )
                    columns.add(column)
                else:
                    column.es_type = _merge_python_type(column.es_type, es_type)
                    try:
                        column.jx_type = native_type_to_json_type[column.es_type]
                    except Exception as e:
                        raise e

                if es_type in {"object", "dict", "Mapping", "Data"}:
                    _get_schema_from_list(
                        [value],
                        table_name,
                        full_name,
                        nested_path,
                        columns,
                        native_type_to_json_type,
                    )
                elif es_type in {"list", "FlatList"}:
                    np = listwrap(nested_path)
                    newpath = unwraplist([join_field(split_field(np[0]) + [name])] + np)
                    _get_schema_from_list(
                        value, table_name, full_name, newpath, columns
                    )

def get_id(column):
    """
    :param column:
    :return: Elasticsearch id for column
    """
    return column.es_index + "|" + column.es_column

META_COLUMNS_DESC = TableDesc(
    name=META_COLUMNS_NAME,
    url=None,
    query_path=ROOT_PATH,
    last_updated=Date.now(),
    columns=to_data(
        [
            Column(
                name=c,
                es_index=META_COLUMNS_NAME,
                es_column=c,
                es_type="keyword",
                jx_type=STRING,
                last_updated=Date.now(),
                nested_path=ROOT_PATH,
                multi=1,
            )
            for c in [
                "name",
                "es_type",
                "jx_type",
                "es_column",
                "es_index",
                "partitions",
            ]
        ]
        + [
            Column(
                name=c,
                es_index=META_COLUMNS_NAME,
                es_column=c,
                es_type="integer",
                jx_type=INTEGER,
                last_updated=Date.now(),
                nested_path=ROOT_PATH,
                multi=1
            )
            for c in ["count", "cardinality", "multi"]
        ]
        + [
            Column(
                name="nested_path",
                es_index=META_COLUMNS_NAME,
                es_column="nested_path",
                es_type="keyword",
                jx_type=STRING,
                last_updated=Date.now(),
                nested_path=ROOT_PATH,
                multi=4,
            ),
            Column(
                name="last_updated",
                es_index=META_COLUMNS_NAME,
                es_column="last_updated",
                es_type="double",
                jx_type=NUMBER,
                last_updated=Date.now(),
                nested_path=ROOT_PATH,
                multi=1
            ),
        ]
    ),
)

META_TABLES_DESC = TableDesc(
    name=META_TABLES_NAME,
    url=None,
    query_path=ROOT_PATH,
    last_updated=Date.now(),
    columns=to_data(
        [
            Column(
                name=c,
                es_index=META_TABLES_NAME,
                es_column=c,
                es_type="string",
                jx_type=STRING,
                last_updated=Date.now(),
                nested_path=ROOT_PATH,
                multi=1
            )
            for c in ["name", "url", "query_path"]
        ]
        + [
            Column(
                name=c,
                es_index=META_TABLES_NAME,
                es_column=c,
                es_type="integer",
                jx_type=INTEGER,
                last_updated=Date.now(),
                nested_path=ROOT_PATH,
                multi=1
            )
            for c in ["timestamp"]
        ]
    ),
)

SIMPLE_METADATA_COLUMNS = (  # FOR PURELY INTERNAL PYTHON LISTS, NOT MAPPING TO ANOTHER DATASTORE
    [
        Column(
            name=c,
            es_index=META_COLUMNS_NAME,
            es_column=c,
            es_type="string",
            jx_type=STRING,
            last_updated=Date.now(),
            nested_path=ROOT_PATH,
            multi=1

        )
        for c in ["table", "name", "type"]
    ]
    + [
        Column(
            name=c,
            es_index=META_COLUMNS_NAME,
            es_column=c,
            es_type="long",
            jx_type=INTEGER,
            last_updated=Date.now(),
            nested_path=ROOT_PATH,
            multi=1
        )
        for c in ["count", "cardinality", "multi"]
    ]
    + [
        Column(
            name="last_updated",
            es_index=META_COLUMNS_NAME,
            es_column="last_updated",
            es_type="time",
            jx_type=NUMBER,
            last_updated=Date.now(),
            nested_path=ROOT_PATH,
            multi=1
        ),
        Column(
            name="nested_path",
            es_index=META_COLUMNS_NAME,
            es_column="nested_path",
            es_type="string",
            jx_type=STRING,
            last_updated=Date.now(),
            nested_path=ROOT_PATH,
            multi=4

        )
    ]
)

_merge_order = {
    none_type: 0,
    NullType: 1,
    bool: 2,
    int: 3,
    long: 3,
    Date: 4,
    datetime: 4,
    float: 5,
    text: 6,
    binary_type: 6,
    object: 7,
    dict: 8,
    Mapping: 9,
    Data: 10,
    list: 11,
    FlatList: 12,
}

for k, v in items(_merge_order):
    _merge_order[k.__name__] = v

def _merge_python_type(A, B):
    a = _merge_order[A]
    b = _merge_order[B]

    if a >= b:
        output = A
    else:
        output = B

    if isinstance(output, str):
        return output
    else:
        return output.__name__
<|endoftext|>


<commit_before># ome - Object Message Expressions
# Copyright (c) 2015 Luke McCarthy <luke@iogopro.co.uk>. All rights reserved.

import io
import os
import re
import struct
import subprocess
import sys

from . import constants
from .ast import Block, BuiltInBlock, Method, Send, Sequence
from .constants import *
from .dispatcher import generate_dispatcher
from .labels import *
from .parser import Parser
from .target import target_platform_map, default_target_platform

class TraceBackInfo(object):
    def __init__(self, id, file_info, source_line, column, underline):
        self.id = id
        self.file_info = file_info
        self.source_line = source_line
        self.column = column
        self.underline = underline

def encode_string_data(string):
    """Add 32-bit length header and nul termination/alignment padding."""
    string = string.encode('utf8')
    string = struct.pack('I', len(string)) + string
    padding = b'\0' * (8 - (len(string) & 7))
    return string + padding

class DataTable(object):
    def __init__(self):
        self.size = 0
        self.data = []
        self.string_offsets = {}

    def append_data(self, data):
        offset = self.size
        self.data.append(data)
        self.size += len(data)
        return offset

    def allocate_string_offset(self, string):
        if string not in self.string_offsets:
            data = encode_string_data(string)
            self.string_offsets[string] = self.append_data(data)
        return self.string_offsets[string]

    def allocate_string(self, string):
        return '(OME_data+%s)' % self.allocate_string_offset(string)

    def generate_assembly(self, out):
        out.write('\nalign 8\nOME_data:\n')
        for data in self.data:
             out.write('\tdb ' + ','.join('%d' % byte for byte in data) + '\n')
        out.write('.end:\n')

def collect_nodes_of_type(ast, node_type):
    nodes = []
    def append_block(node):
        if isinstance(node, node_type):
            nodes.append(node)
    ast.walk(append_block)
    return nodes

class Program(object):
    def __init__(self, filename, ast, builtin, target_type):
        self.filename = filename
        self.builtin = builtin
        self.toplevel_method = ast
        self.toplevel_block = ast.expr

        if isinstance(self.toplevel_block, Sequence):
            self.toplevel_block = self.toplevel_block.statements[-1]

        if 'main' not in self.toplevel_block.symbols:
            self.error('no main method defined')

        self.target_type = target_type
        self.code_table = []  # list of (symbol, [list of (tag, method)])
        self.data_table = DataTable()

        self.block_list = collect_nodes_of_type(ast, Block)
        self.allocate_tag_ids()
        self.allocate_constant_tag_ids()

        self.send_list = collect_nodes_of_type(ast, Send)
        self.find_used_methods()
        self.compile_traceback_info()

        self.build_code_table()

    def error(self, message):
        raise OmeFileError(self.filename, message)

    def allocate_tag_ids(self):
        tag = Tag_User
        for block in self.block_list:
            if not block.is_constant:
                block.tag = tag
                tag += 1
        if tag > MAX_TAG:
            self.error('exhausted all tag IDs')

    def allocate_constant_tag_ids(self):
        constant_tag = Constant_User
        for block in self.block_list:
            if block.is_constant:
                block.tag = constant_to_tag(constant_tag)
                block.tag_constant = constant_tag
                constant_tag += 1
        if constant_tag > MAX_CONSTANT_TAG:
            self.error('exhausted all constant tag IDs')

    def find_used_methods(self):
        self.sent_messages = set(['string'])
        self.sent_messages.update(
            send.symbol for send in self.send_list if not send.receiver_block)

        self.called_methods = set([
            (self.toplevel_block.tag, 'main'),
        ])
        self.called_methods.update(
            (send.receiver_block.tag, send.symbol) for send in self.send_list
            if send.receiver_block and send.symbol not in self.sent_messages)

        for method in self.target_type.builtin_methods:
            if method.sent_messages and self.should_include_method(method, self.builtin.tag):
                self.sent_messages.update(method.sent_messages)

    def compile_traceback_info(self):
        for send in self.send_list:
            if send.parse_state:
                ps = send.parse_state
                file_info = '\n  File "%s", line %s, in |%s|\n    ' % (
                    ps.stream_name, ps.line_number, send.method.symbol)

                line_unstripped = ps.current_line.rstrip()
                line = line_unstripped.lstrip()
                column = 4 + (ps.column - (len(line_unstripped) - len(line)))

                underline = send.symbol.find(':') + 1
                if underline < 1:
                    underline = len(send.symbol)

                send.traceback_info = TraceBackInfo(
                    id = (ps.stream_name, ps.line_number, ps.column),
                    file_info = self.data_table.allocate_string(file_info),
                    source_line = self.data_table.allocate_string(line),
                    column = column,
                    underline = underline)

    def compile_method_with_label(self, method, label):
        code = method.generate_code(self.data_table)
        code.optimise(self.target_type)
        return code.generate_assembly(label, self.target_type)

    def compile_method(self, method, tag):
        return self.compile_method_with_label(method, make_call_label(tag, method.symbol))

    def should_include_method(self, method, tag):
        return method.symbol in self.sent_messages or (tag, method.symbol) in self.called_methods

    def build_code_table(self):
        methods = {}

        for method in self.target_type.builtin_methods:
            if self.should_include_method(method, self.builtin.tag):
                if method.symbol not in methods:
                    methods[method.symbol] = []
                label = make_call_label(method.tag, method.symbol)
                code = '%s:\n%s' % (label, method.code)
                methods[method.symbol].append((method.tag, code))

        for block in self.block_list:
            for method in block.methods:
                if self.should_include_method(method, block.tag):
                    if method.symbol not in methods:
                        methods[method.symbol] = []
                    code = self.compile_method(method, block.tag)
                    methods[method.symbol].append((block.tag, code))

        for symbol in sorted(methods.keys()):
            self.code_table.append((symbol, methods[symbol]))

        methods.clear()

    def generate_assembly(self, out):
        define_format = self.target_type.define_constant_format
        for name, value in sorted(constants.__dict__.items()):
            if isinstance(value, int):
                out.write(define_format.format(name, value))

        out.write(define_format.format('OME_main', make_call_label(self.toplevel_block.tag, 'main')))
        out.write(self.target_type.builtin_macros)
        out.write(self.target_type.builtin_code)
        out.write('\n')
        out.write(self.compile_method_with_label(self.toplevel_method, 'OME_toplevel'))
        out.write('\n')

        dispatchers = set()
        for symbol, methods in self.code_table:
            if symbol in self.sent_messages:
                tags = [tag for tag, code in methods]
                out.write(generate_dispatcher(symbol, tags, self.target_type))
                out.write('\n')
                dispatchers.add(symbol)
            for tag, code in methods:
                out.write(code)
                out.write('\n')

        for symbol in self.sent_messages:
            if symbol not in dispatchers:
                sys.stderr.write("Warning: No methods defined for message '%s'\n" % symbol)
                out.write(generate_dispatcher(symbol, [], self.target_type))
                out.write('\n')

        out.write(self.target_type.builtin_data)
        self.data_table.generate_assembly(out)

def parse_file(filename):
    try:
        with open(filename) as f:
            source = f.read()
    except FileNotFoundError:
        raise OmeFileError('ome', 'file does not exist: ' + filename)
    except UnicodeDecodeError as e:
        raise OmeFileError(filename, 'utf-8 decoding failed at position {0.start}: {0.reason}'.format(e))
    except Exception as e:
        raise OmeFileError(filename, str(e))
    return Parser(source, filename).toplevel()

def compile_file_to_assembly(filename, target_type):
    builtin = BuiltInBlock(target_type)
    ast = parse_file(filename)
    ast = Method('', [], ast)
    ast = ast.resolve_free_vars(builtin)
    ast = ast.resolve_block_refs(builtin)
    program = Program(filename, ast, builtin, target_type)
    out = io.StringIO()
    program.generate_assembly(out)
    asm = out.getvalue()
    #print(asm)
    return asm.encode('utf8')

def run_assembler(target_type, input, outfile):
    p = subprocess.Popen(target_type.get_assembler_args(outfile), stdin=subprocess.PIPE)
    p.communicate(input)
    if p.returncode != 0:
        sys.exit(p.returncode)

def run_linker(target_type, infile, outfile):
    p = subprocess.Popen(target_type.get_linker_args(infile, outfile))
    p.communicate()
    if p.returncode != 0:
        sys.exit(p.returncode)

def compile_file(filename, target_platform=default_target_platform):
    if target_platform not in target_platform_map:
        raise OmeFileError('ome', 'unsupported target platform: {0}-{1}'.format(*target_platform))
    target_type = target_platform_map[target_platform]
    asm = compile_file_to_assembly(filename, target_type)
    exe_file = os.path.splitext(filename)[0]
    obj_file = exe_file + '.o'
    run_assembler(target_type, asm, obj_file)
    run_linker(target_type, obj_file, exe_file)
<commit_msg>Format warnings in the same way as errors.<commit_after># ome - Object Message Expressions
# Copyright (c) 2015 Luke McCarthy <luke@iogopro.co.uk>. All rights reserved.

import io
import os
import re
import struct
import subprocess
import sys

from . import constants
from .ast import Block, BuiltInBlock, Method, Send, Sequence
from .constants import *
from .dispatcher import generate_dispatcher
from .labels import *
from .parser import Parser
from .target import target_platform_map, default_target_platform

class TraceBackInfo(object):
    def __init__(self, id, file_info, source_line, column, underline):
        self.id = id
        self.file_info = file_info
        self.source_line = source_line
        self.column = column
        self.underline = underline

def encode_string_data(string):
    """Add 32-bit length header and nul termination/alignment padding."""
    string = string.encode('utf8')
    string = struct.pack('I', len(string)) + string
    padding = b'\0' * (8 - (len(string) & 7))
    return string + padding

class DataTable(object):
    def __init__(self):
        self.size = 0
        self.data = []
        self.string_offsets = {}

    def append_data(self, data):
        offset = self.size
        self.data.append(data)
        self.size += len(data)
        return offset

    def allocate_string_offset(self, string):
        if string not in self.string_offsets:
            data = encode_string_data(string)
            self.string_offsets[string] = self.append_data(data)
        return self.string_offsets[string]

    def allocate_string(self, string):
        return '(OME_data+%s)' % self.allocate_string_offset(string)

    def generate_assembly(self, out):
        out.write('\nalign 8\nOME_data:\n')
        for data in self.data:
             out.write('\tdb ' + ','.join('%d' % byte for byte in data) + '\n')
        out.write('.end:\n')

def collect_nodes_of_type(ast, node_type):
    nodes = []
    def append_block(node):
        if isinstance(node, node_type):
            nodes.append(node)
    ast.walk(append_block)
    return nodes

class Program(object):
    def __init__(self, filename, ast, builtin, target_type):
        self.filename = filename
        self.builtin = builtin
        self.toplevel_method = ast
        self.toplevel_block = ast.expr

        if isinstance(self.toplevel_block, Sequence):
            self.toplevel_block = self.toplevel_block.statements[-1]

        if 'main' not in self.toplevel_block.symbols:
            self.error('no main method defined')

        self.target_type = target_type
        self.code_table = []  # list of (symbol, [list of (tag, method)])
        self.data_table = DataTable()

        self.block_list = collect_nodes_of_type(ast, Block)
        self.allocate_tag_ids()
        self.allocate_constant_tag_ids()

        self.send_list = collect_nodes_of_type(ast, Send)
        self.find_used_methods()
        self.compile_traceback_info()

        self.build_code_table()

    def error(self, message):
        raise OmeFileError(self.filename, message)

    def warning(self, message):
        sys.stderr.write('\x1b[1m{0}: \x1b[35mwarning:\x1b[0m {1}\n'.format(self.filename, message))

    def allocate_tag_ids(self):
        tag = Tag_User
        for block in self.block_list:
            if not block.is_constant:
                block.tag = tag
                tag += 1
        if tag > MAX_TAG:
            self.error('exhausted all tag IDs')

    def allocate_constant_tag_ids(self):
        constant_tag = Constant_User
        for block in self.block_list:
            if block.is_constant:
                block.tag = constant_to_tag(constant_tag)
                block.tag_constant = constant_tag
                constant_tag += 1
        if constant_tag > MAX_CONSTANT_TAG:
            self.error('exhausted all constant tag IDs')

    def find_used_methods(self):
        self.sent_messages = set(['string'])
        self.sent_messages.update(
            send.symbol for send in self.send_list if not send.receiver_block)

        self.called_methods = set([
            (self.toplevel_block.tag, 'main'),
        ])
        self.called_methods.update(
            (send.receiver_block.tag, send.symbol) for send in self.send_list
            if send.receiver_block and send.symbol not in self.sent_messages)

        for method in self.target_type.builtin_methods:
            if method.sent_messages and self.should_include_method(method, self.builtin.tag):
                self.sent_messages.update(method.sent_messages)

    def compile_traceback_info(self):
        for send in self.send_list:
            if send.parse_state:
                ps = send.parse_state
                file_info = '\n  File "%s", line %s, in |%s|\n    ' % (
                    ps.stream_name, ps.line_number, send.method.symbol)

                line_unstripped = ps.current_line.rstrip()
                line = line_unstripped.lstrip()
                column = 4 + (ps.column - (len(line_unstripped) - len(line)))

                underline = send.symbol.find(':') + 1
                if underline < 1:
                    underline = len(send.symbol)

                send.traceback_info = TraceBackInfo(
                    id = (ps.stream_name, ps.line_number, ps.column),
                    file_info = self.data_table.allocate_string(file_info),
                    source_line = self.data_table.allocate_string(line),
                    column = column,
                    underline = underline)

    def compile_method_with_label(self, method, label):
        code = method.generate_code(self.data_table)
        code.optimise(self.target_type)
        return code.generate_assembly(label, self.target_type)

    def compile_method(self, method, tag):
        return self.compile_method_with_label(method, make_call_label(tag, method.symbol))

    def should_include_method(self, method, tag):
        return method.symbol in self.sent_messages or (tag, method.symbol) in self.called_methods

    def build_code_table(self):
        methods = {}

        for method in self.target_type.builtin_methods:
            if self.should_include_method(method, self.builtin.tag):
                if method.symbol not in methods:
                    methods[method.symbol] = []
                label = make_call_label(method.tag, method.symbol)
                code = '%s:\n%s' % (label, method.code)
                methods[method.symbol].append((method.tag, code))

        for block in self.block_list:
            for method in block.methods:
                if self.should_include_method(method, block.tag):
                    if method.symbol not in methods:
                        methods[method.symbol] = []
                    code = self.compile_method(method, block.tag)
                    methods[method.symbol].append((block.tag, code))

        for symbol in sorted(methods.keys()):
            self.code_table.append((symbol, methods[symbol]))

        methods.clear()

    def generate_assembly(self, out):
        define_format = self.target_type.define_constant_format
        for name, value in sorted(constants.__dict__.items()):
            if isinstance(value, int):
                out.write(define_format.format(name, value))

        out.write(define_format.format('OME_main', make_call_label(self.toplevel_block.tag, 'main')))
        out.write(self.target_type.builtin_macros)
        out.write(self.target_type.builtin_code)
        out.write('\n')
        out.write(self.compile_method_with_label(self.toplevel_method, 'OME_toplevel'))
        out.write('\n')

        dispatchers = set()
        for symbol, methods in self.code_table:
            if symbol in self.sent_messages:
                tags = [tag for tag, code in methods]
                out.write(generate_dispatcher(symbol, tags, self.target_type))
                out.write('\n')
                dispatchers.add(symbol)
            for tag, code in methods:
                out.write(code)
                out.write('\n')

        for symbol in self.sent_messages:
            if symbol not in dispatchers:
                self.warning("no methods defined for message '%s'" % symbol)
                out.write(generate_dispatcher(symbol, [], self.target_type))
                out.write('\n')

        out.write(self.target_type.builtin_data)
        self.data_table.generate_assembly(out)

def parse_file(filename):
    try:
        with open(filename) as f:
            source = f.read()
    except FileNotFoundError:
        raise OmeFileError('ome', 'file does not exist: ' + filename)
    except UnicodeDecodeError as e:
        raise OmeFileError(filename, 'utf-8 decoding failed at position {0.start}: {0.reason}'.format(e))
    except Exception as e:
        raise OmeFileError(filename, str(e))
    return Parser(source, filename).toplevel()

def compile_file_to_assembly(filename, target_type):
    builtin = BuiltInBlock(target_type)
    ast = parse_file(filename)
    ast = Method('', [], ast)
    ast = ast.resolve_free_vars(builtin)
    ast = ast.resolve_block_refs(builtin)
    program = Program(filename, ast, builtin, target_type)
    out = io.StringIO()
    program.generate_assembly(out)
    asm = out.getvalue()
    #print(asm)
    return asm.encode('utf8')

def run_assembler(target_type, input, outfile):
    p = subprocess.Popen(target_type.get_assembler_args(outfile), stdin=subprocess.PIPE)
    p.communicate(input)
    if p.returncode != 0:
        sys.exit(p.returncode)

def run_linker(target_type, infile, outfile):
    p = subprocess.Popen(target_type.get_linker_args(infile, outfile))
    p.communicate()
    if p.returncode != 0:
        sys.exit(p.returncode)

def compile_file(filename, target_platform=default_target_platform):
    if target_platform not in target_platform_map:
        raise OmeFileError('ome', 'unsupported target platform: {0}-{1}'.format(*target_platform))
    target_type = target_platform_map[target_platform]
    asm = compile_file_to_assembly(filename, target_type)
    exe_file = os.path.splitext(filename)[0]
    obj_file = exe_file + '.o'
    run_assembler(target_type, asm, obj_file)
    run_linker(target_type, obj_file, exe_file)
<|endoftext|>