<commit_before># Copyright (C) 2010 Linaro Limited
#
# Author: Zygmunt Krynicki <zygmunt.krynicki@linaro.org>
#
# This file is part of Launch Control.
#
# Launch Control is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License version 3
# as published by the Free Software Foundation
#
# Launch Control 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 General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with Launch Control.  If not, see <http://www.gnu.org/licenses/>.

"""
XMP-RPC API
"""

import xmlrpclib

from django.core.files.base import ContentFile
from django.db import IntegrityError
from django.db.models import Q

from dashboard_app import get_version
from dashboard_app.dispatcher import xml_rpc_signature

from dashboard_app.models import (
        Bundle,
        BundleStream,
        )

class errors:
    """
    A namespace for error codes that may be returned by various XML-RPC
    methods. Where applicable existing status codes from HTTP protocol
    are reused
    """
    AUTH_FAILED = 100
    AUTH_BLOCKED = 101
    FORBIDDEN = 403
    NOT_FOUND = 404
    CONFLICT = 409
    INTERNAL_SERVER_ERROR = 500
    NOT_IMPLEMENTED = 501

class DashboardAPI(object):
    """
    Dashboard API object.

    All public methods are automatically exposed as XML-RPC methods
    """

    @xml_rpc_signature('str')
    def version(self):
        """
        Name
        ----
        `version` ()

        Description
        -----------
        Return dashboard server version. The version is a string with
        dots separating five components.

        The components are:
            1. major version
            2. minor version
            3. micro version
            4. release level
            5. serial

        See: http://docs.python.org/library/sys.html#sys.version_info

        Return value
        -------------
        Server version string
        """
        return get_version()

    @xml_rpc_signature('str', 'str', 'str', 'str')
    def put(self, content, content_filename, pathname):
        """
        Name
        ----
        `put` (`content`, `content_filename`, `pathname`)

        Description
        -----------
        Upload a bundle to the server.

        Arguments
        ---------
        `content`: string
            Full text of the bundle. This *SHOULD* be a valid JSON
            document and it *SHOULD* match the "Dashboard Bundle Format
            1.0" schema. The SHA1 of the content *MUST* be unique or a
            ``Fault(409, "...")`` is raised. This is used to protect
            from simple duplicate submissions.
        `content_filename`: string
            Name of the file that contained the text of the bundle. The
            `content_filename` can be an arbitrary string and will be
            stored along with the content for reference.
        `pathname`: string
            Pathname of the bundle stream where a new bundle should
            be created and stored. This argument *MUST* designate a
            pre-existing bundle stream or a ``Fault(404, "...")`` exception
            is raised. In addition the user *MUST* have access
            permission to upload bundles there or a ``Fault(403, "...")``
            exception is raised. See below for access rules.

        Return value
        ------------
        If all goes well this function returns the SHA1 of the content.

        Exceptions raised
        -----------------
        404
            Bundle stream not found
        409
            Duplicate bundle content
        403
            Uploading to specified stream is not permitted

        Rules for bundle stream access
        ------------------------------
        The following rules govern bundle stream upload access rights:
            - all anonymous streams are accessible
            - personal streams are accessible by owners
            - team streams are accessible by team members

        """
        user = None
        try:
            bundle_stream = BundleStream.objects.get(pathname=pathname)
        except BundleStream.DoesNotExist:
            raise xmlrpclib.Fault(errors.NOT_FOUND,
                    "Bundle stream not found")
        if not bundle_stream.can_access(user):
            raise xmlrpclib.Fault(errors.FORBIDDEN,
                    "Uploading to specified stream is not permitted")
        try:
            bundle = Bundle.objects.create(
                    bundle_stream=bundle_stream,
                    uploaded_by=user,
                    content_filename=content_filename)
            bundle.save()
            bundle.content.save("bundle-{0}".format(bundle.pk),
                    ContentFile(content))
            bundle.deserialize()
        except IntegrityError:
            bundle.delete()
            raise xmlrpclib.Fault(errors.CONFLICT,
                    "Duplicate bundle content")
        return bundle.content_sha1

    def get(self, content_sha1):
        """
        Name
        ----
        `get` (`content_sha1`)

        Description
        -----------
        Download a bundle from the server.

        Arguments
        ---------
        `content_sha1`: string
            SHA1 hash of the content of the bundle to download. This
            *MUST* designate an bundle or ``Fault(404, "...")`` is raised.

        Return value
        ------------
        This function returns an XML-RPC struct with the following fields:

        `content_filename`: string
            The value that was stored on a previous call to put()
        `content`: string
            The full text of the bundle

        Exceptions raised
        -----------------
        404
            Bundle not found
        403
            Downloading from the stream that contains this bundle is
            not permitted

        Rules for bundle stream access
        ------------------------------
        The following rules govern bundle stream download access rights:
            - all anonymous streams are accessible
            - personal streams are accessible by owners
            - team streams are accessible by team members
        """
        user = None
        try:
            bundle = Bundle.objects.get(content_sha1=content_sha1)
        except Bundle.DoesNotExist:
            raise xmlrpclib.Fault(errors.NOT_FOUND,
                    "Bundle not found")
        if not bundle.bundle_stream.can_access(user):
            raise xmlrpclib.Fault(errors.FORBIDDEN,
                    "Downloading from specified stream is not permitted")
        else:
            return {"content": bundle.content.read(),
                    "content_filename": bundle.content_filename}

    @xml_rpc_signature('struct')
    def streams(self):
        """
        Name
        ----
        `streams` ()

        Description
        -----------
        List all bundle streams that the user has access to

        Arguments
        ---------
        None

        Return value
        ------------
        This function returns an XML-RPC array of XML-RPC structs with
        the following fields:

        `pathname`: string
            The pathname of the bundle stream
        `name`: string
            The user-configurable name of the bundle stream
        `user`: string
            The username of the owner of the bundle stream for personal
            streams or an empty string for public and team streams.
        `group`: string
            The name of the team that owsn the bundle stream for team
            streams or an empty string for public and personal streams.
        `bundle_count`: int
            Number of bundles that are in this stream

        Exceptions raised
        -----------------
        None

        Rules for bundle stream access
        ------------------------------
        The following rules govern bundle stream download access rights:
            - all anonymous streams are accessible
            - personal streams are accessible by owners
            - team streams are accessible by team members
        """
        user = None
        bundle_streams = BundleStream.objects.allowed_for_user(user)
        return [{
            'pathname': bundle_stream.pathname,
            'name': bundle_stream.name,
            'user': bundle_stream.user.username if bundle_stream.user else "",
            'group': bundle_stream.group.name if bundle_stream.group else "",
            'bundle_count': bundle_stream.bundles.count(),
            } for bundle_stream in bundle_streams]

    def bundles(self, pathname):
        """
        Name
        ----
        `bundles` (`pathname`)

        Description
        -----------
        List all bundles in a specified bundle stream

        Arguments
        ---------
        `pathname`: string
            The pathname of the bundle stream to query. This argument
            *MUST* designate an existing stream or Fault(404, "...") is
            raised. The user *MUST* have access to this stream or
            Fault(403, "...") is raised.

        Return value
        ------------
        This function returns an XML-RPC array of XML-RPC structs with
        the following fields:

        `uploaded_by`: string
            The username of the user that uploaded this bundle or
            empty string if this bundle was uploaded anonymously.
        `uploaded_on`: datetime
            The timestamp when the bundle was uploaded
        `content_filename`: string
            The filename of the original bundle file
        `content_sha1`: string
            The SHA1 hash if the content of the bundle
        `is_deserialized`: bool
            True if the bundle was de-serialized successfully, false otherwise

        Exceptions raised
        -----------------
        404
            Bundle stream not found
        403
            Listing bundles in this bundle stream is not permitted

        Rules for bundle stream access
        ------------------------------
        The following rules govern bundle stream download access rights:
            - all anonymous streams are accessible
            - personal streams are accessible by owners
            - team streams are accessible by team members
        """
        user = None
        try:
            bundle_stream = BundleStream.objects.get(pathname=pathname)
        except BundleStream.DoesNotExist:
            raise xmlrpclib.Fault(errors.NOT_FOUND,
                    "Bundle stream not found")
        if not bundle_stream.can_access(user):
            raise xmlrpclib.Fault(errors.FORBIDDEN,
                    "Downloading from specified stream is not permitted")
        return [{
            'uploaded_by': bundle.uploaded_by.username if bundle.uploaded_by else "",
            'uploaded_on': bundle.uploaded_on,
            'content_filename': bundle.content_filename,
            'content_sha1': bundle.content_sha1,
            'is_deserialized': bundle.is_deserialized
            } for bundle in bundle_stream.bundles.all()]

    def deserialize(self, content_sha1):
        """
        Name
        ----
        `deserialize` (`content_sha1`)

        Description
        -----------
        Deserialize bundle on the server

        Arguments
        ---------
        `content_sha1`: string
            SHA1 hash of the content of the bundle to download. This
            *MUST* designate an bundle or ``Fault(404, "...")`` is raised.

        Return value
        ------------
        True - deserialization okay
        False - deserialization not needed

        Exceptions raised
        -----------------
        404
            Bundle not found
        409
            Bundle import failed
        """
        user = None
        try:
            bundle = Bundle.objects.get(content_sha1=content_sha1)
        except Bundle.DoesNotExist:
            raise xmlrpclib.Fault(errors.NOT_FOUND,
                    "Bundle not found")
        if bundle.is_deserialized:
            return False
        bundle.deserialize()
        if bundle.is_deserialized is False:
            raise xmlrpclib.Fault(
                errors.CONFLICT,
                bundle.deserialization_error.error_message)
        return True
<commit_msg>Make sure bundles() xml-rpc API returns bundles in the same order as before.<commit_after># Copyright (C) 2010 Linaro Limited
#
# Author: Zygmunt Krynicki <zygmunt.krynicki@linaro.org>
#
# This file is part of Launch Control.
#
# Launch Control is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License version 3
# as published by the Free Software Foundation
#
# Launch Control 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 General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with Launch Control.  If not, see <http://www.gnu.org/licenses/>.

"""
XMP-RPC API
"""

import xmlrpclib

from django.core.files.base import ContentFile
from django.db import IntegrityError
from django.db.models import Q

from dashboard_app import get_version
from dashboard_app.dispatcher import xml_rpc_signature

from dashboard_app.models import (
        Bundle,
        BundleStream,
        )

class errors:
    """
    A namespace for error codes that may be returned by various XML-RPC
    methods. Where applicable existing status codes from HTTP protocol
    are reused
    """
    AUTH_FAILED = 100
    AUTH_BLOCKED = 101
    FORBIDDEN = 403
    NOT_FOUND = 404
    CONFLICT = 409
    INTERNAL_SERVER_ERROR = 500
    NOT_IMPLEMENTED = 501

class DashboardAPI(object):
    """
    Dashboard API object.

    All public methods are automatically exposed as XML-RPC methods
    """

    @xml_rpc_signature('str')
    def version(self):
        """
        Name
        ----
        `version` ()

        Description
        -----------
        Return dashboard server version. The version is a string with
        dots separating five components.

        The components are:
            1. major version
            2. minor version
            3. micro version
            4. release level
            5. serial

        See: http://docs.python.org/library/sys.html#sys.version_info

        Return value
        -------------
        Server version string
        """
        return get_version()

    @xml_rpc_signature('str', 'str', 'str', 'str')
    def put(self, content, content_filename, pathname):
        """
        Name
        ----
        `put` (`content`, `content_filename`, `pathname`)

        Description
        -----------
        Upload a bundle to the server.

        Arguments
        ---------
        `content`: string
            Full text of the bundle. This *SHOULD* be a valid JSON
            document and it *SHOULD* match the "Dashboard Bundle Format
            1.0" schema. The SHA1 of the content *MUST* be unique or a
            ``Fault(409, "...")`` is raised. This is used to protect
            from simple duplicate submissions.
        `content_filename`: string
            Name of the file that contained the text of the bundle. The
            `content_filename` can be an arbitrary string and will be
            stored along with the content for reference.
        `pathname`: string
            Pathname of the bundle stream where a new bundle should
            be created and stored. This argument *MUST* designate a
            pre-existing bundle stream or a ``Fault(404, "...")`` exception
            is raised. In addition the user *MUST* have access
            permission to upload bundles there or a ``Fault(403, "...")``
            exception is raised. See below for access rules.

        Return value
        ------------
        If all goes well this function returns the SHA1 of the content.

        Exceptions raised
        -----------------
        404
            Bundle stream not found
        409
            Duplicate bundle content
        403
            Uploading to specified stream is not permitted

        Rules for bundle stream access
        ------------------------------
        The following rules govern bundle stream upload access rights:
            - all anonymous streams are accessible
            - personal streams are accessible by owners
            - team streams are accessible by team members

        """
        user = None
        try:
            bundle_stream = BundleStream.objects.get(pathname=pathname)
        except BundleStream.DoesNotExist:
            raise xmlrpclib.Fault(errors.NOT_FOUND,
                    "Bundle stream not found")
        if not bundle_stream.can_access(user):
            raise xmlrpclib.Fault(errors.FORBIDDEN,
                    "Uploading to specified stream is not permitted")
        try:
            bundle = Bundle.objects.create(
                    bundle_stream=bundle_stream,
                    uploaded_by=user,
                    content_filename=content_filename)
            bundle.save()
            bundle.content.save("bundle-{0}".format(bundle.pk),
                    ContentFile(content))
            bundle.deserialize()
        except IntegrityError:
            bundle.delete()
            raise xmlrpclib.Fault(errors.CONFLICT,
                    "Duplicate bundle content")
        return bundle.content_sha1

    def get(self, content_sha1):
        """
        Name
        ----
        `get` (`content_sha1`)

        Description
        -----------
        Download a bundle from the server.

        Arguments
        ---------
        `content_sha1`: string
            SHA1 hash of the content of the bundle to download. This
            *MUST* designate an bundle or ``Fault(404, "...")`` is raised.

        Return value
        ------------
        This function returns an XML-RPC struct with the following fields:

        `content_filename`: string
            The value that was stored on a previous call to put()
        `content`: string
            The full text of the bundle

        Exceptions raised
        -----------------
        404
            Bundle not found
        403
            Downloading from the stream that contains this bundle is
            not permitted

        Rules for bundle stream access
        ------------------------------
        The following rules govern bundle stream download access rights:
            - all anonymous streams are accessible
            - personal streams are accessible by owners
            - team streams are accessible by team members
        """
        user = None
        try:
            bundle = Bundle.objects.get(content_sha1=content_sha1)
        except Bundle.DoesNotExist:
            raise xmlrpclib.Fault(errors.NOT_FOUND,
                    "Bundle not found")
        if not bundle.bundle_stream.can_access(user):
            raise xmlrpclib.Fault(errors.FORBIDDEN,
                    "Downloading from specified stream is not permitted")
        else:
            return {"content": bundle.content.read(),
                    "content_filename": bundle.content_filename}

    @xml_rpc_signature('struct')
    def streams(self):
        """
        Name
        ----
        `streams` ()

        Description
        -----------
        List all bundle streams that the user has access to

        Arguments
        ---------
        None

        Return value
        ------------
        This function returns an XML-RPC array of XML-RPC structs with
        the following fields:

        `pathname`: string
            The pathname of the bundle stream
        `name`: string
            The user-configurable name of the bundle stream
        `user`: string
            The username of the owner of the bundle stream for personal
            streams or an empty string for public and team streams.
        `group`: string
            The name of the team that owsn the bundle stream for team
            streams or an empty string for public and personal streams.
        `bundle_count`: int
            Number of bundles that are in this stream

        Exceptions raised
        -----------------
        None

        Rules for bundle stream access
        ------------------------------
        The following rules govern bundle stream download access rights:
            - all anonymous streams are accessible
            - personal streams are accessible by owners
            - team streams are accessible by team members
        """
        user = None
        bundle_streams = BundleStream.objects.allowed_for_user(user)
        return [{
            'pathname': bundle_stream.pathname,
            'name': bundle_stream.name,
            'user': bundle_stream.user.username if bundle_stream.user else "",
            'group': bundle_stream.group.name if bundle_stream.group else "",
            'bundle_count': bundle_stream.bundles.count(),
            } for bundle_stream in bundle_streams]

    def bundles(self, pathname):
        """
        Name
        ----
        `bundles` (`pathname`)

        Description
        -----------
        List all bundles in a specified bundle stream

        Arguments
        ---------
        `pathname`: string
            The pathname of the bundle stream to query. This argument
            *MUST* designate an existing stream or Fault(404, "...") is
            raised. The user *MUST* have access to this stream or
            Fault(403, "...") is raised.

        Return value
        ------------
        This function returns an XML-RPC array of XML-RPC structs with
        the following fields:

        `uploaded_by`: string
            The username of the user that uploaded this bundle or
            empty string if this bundle was uploaded anonymously.
        `uploaded_on`: datetime
            The timestamp when the bundle was uploaded
        `content_filename`: string
            The filename of the original bundle file
        `content_sha1`: string
            The SHA1 hash if the content of the bundle
        `is_deserialized`: bool
            True if the bundle was de-serialized successfully, false otherwise

        Exceptions raised
        -----------------
        404
            Bundle stream not found
        403
            Listing bundles in this bundle stream is not permitted

        Rules for bundle stream access
        ------------------------------
        The following rules govern bundle stream download access rights:
            - all anonymous streams are accessible
            - personal streams are accessible by owners
            - team streams are accessible by team members
        """
        user = None
        try:
            bundle_stream = BundleStream.objects.get(pathname=pathname)
        except BundleStream.DoesNotExist:
            raise xmlrpclib.Fault(errors.NOT_FOUND,
                    "Bundle stream not found")
        if not bundle_stream.can_access(user):
            raise xmlrpclib.Fault(errors.FORBIDDEN,
                    "Downloading from specified stream is not permitted")
        return [{
            'uploaded_by': bundle.uploaded_by.username if bundle.uploaded_by else "",
            'uploaded_on': bundle.uploaded_on,
            'content_filename': bundle.content_filename,
            'content_sha1': bundle.content_sha1,
            'is_deserialized': bundle.is_deserialized
            } for bundle in bundle_stream.bundles.all().order_by("uploaded_on")]

    def deserialize(self, content_sha1):
        """
        Name
        ----
        `deserialize` (`content_sha1`)

        Description
        -----------
        Deserialize bundle on the server

        Arguments
        ---------
        `content_sha1`: string
            SHA1 hash of the content of the bundle to download. This
            *MUST* designate an bundle or ``Fault(404, "...")`` is raised.

        Return value
        ------------
        True - deserialization okay
        False - deserialization not needed

        Exceptions raised
        -----------------
        404
            Bundle not found
        409
            Bundle import failed
        """
        user = None
        try:
            bundle = Bundle.objects.get(content_sha1=content_sha1)
        except Bundle.DoesNotExist:
            raise xmlrpclib.Fault(errors.NOT_FOUND,
                    "Bundle not found")
        if bundle.is_deserialized:
            return False
        bundle.deserialize()
        if bundle.is_deserialized is False:
            raise xmlrpclib.Fault(
                errors.CONFLICT,
                bundle.deserialization_error.error_message)
        return True
<|endoftext|>


<commit_before>import sys

import taichi_lang as ti
import math
import numpy as np
import cv2
import os
import matplotlib.pyplot as plt
import time
from matplotlib.pyplot import cm
import taichi as tc

real = ti.f32
ti.set_default_fp(real)

max_steps = 4096
vis_interval = 256
output_vis_interval = 16
steps = 2048
assert steps * 2 <= max_steps

vis_resolution = 1024

scalar = lambda: ti.var(dt=real)
vec = lambda: ti.Vector(2, dt=real)

loss = scalar()

x = vec()
v = vec()
rotation = scalar()
omega = scalar()

halfsize = vec()

inverse_mass = scalar()
inverse_inertia = scalar()

v_inc = vec()
omega_inc = scalar()

head_id = 3
goal = [0.9, 0.15]

n_objects = 1
elasticity = 1
ground_height = 0.1
gravity = -9.8
friction = 0.0
penalty = 1e4
damping = 0

@ti.layout
def place():
  ti.root.dense(ti.l, max_steps).dense(ti.i, n_objects).place(x, v, rotation,
                                                              omega, v_inc,
                                                              omega_inc)
  ti.root.dense(ti.i, n_objects).place(halfsize, inverse_mass, inverse_inertia)
  ti.root.place(loss)
  ti.root.lazy_grad()

dt = 0.001
learning_rate = 1.0

@ti.func
def rotation_matrix(r):
  return ti.Matrix([[ti.cos(r), -ti.sin(r)], [ti.sin(r), ti.cos(r)]])

@ti.kernel
def initialize_properties():
  for i in range(n_objects):
    inverse_mass[i] = 1.0 / (4 * halfsize[i][0] * halfsize[i][1])
    inverse_inertia[i] = 1.0 / (4 / 3 * halfsize[i][0] * halfsize[i][1] * (
        halfsize[i][0] * halfsize[i][0] + halfsize[i][1] * halfsize[i][1]))
    # ti.print(inverse_mass[i])
    # ti.print(inverse_inertia[i])

@ti.func
def cross(a, b):
  return a[0] * b[1] - a[1] * b[0]

@ti.func
def to_world(t, i, rela_x):
  rot = rotation[t, i]
  rot_matrix = rotation_matrix(rot)

  rela_pos = rot_matrix @ rela_x
  rela_v = omega[t, i] * ti.Vector([-rela_pos[1], rela_pos[0]])

  world_x = x[t, i] + rela_pos
  world_v = v[t, i] + rela_v

  return world_x, world_v, rela_pos

@ti.func
def apply_impulse(t, i, impulse, location):
  ti.atomic_add(v_inc[t + 1, i], impulse * inverse_mass[i])
  ti.atomic_add(omega_inc[t + 1, i],
                cross(location - x[t, i], impulse) * inverse_inertia[i])

@ti.kernel
def collide(t: ti.i32):
  for i in range(n_objects):
    hs = halfsize[i]
    for k in ti.static(range(4)):
      # the corner for collision detection
      offset_scale = ti.Vector([k % 2 * 2 - 1, k // 2 % 2 * 2 - 1])

      corner_x, corner_v, rela_pos = to_world(t, i, offset_scale * hs)
      corner_v = corner_v + dt * gravity * ti.Vector([0.0, 1.0])

      # Apply impulse so that there's no sinking
      normal = ti.Vector([0.0, 1.0])
      tao = ti.Vector([1.0, 0.0])

      rn = cross(rela_pos, normal)
      rt = cross(rela_pos, tao)
      impulse_contribution = inverse_mass[i] + ti.sqr(rn) * \
                             inverse_inertia[i]
      timpulse_contribution = inverse_mass[i] + ti.sqr(rt) * \
                              inverse_inertia[i]

      rela_v_ground = normal.dot(corner_v)

      impulse = 0.0
      timpulse = 0.0
      if rela_v_ground < 0 and corner_x[1] < ground_height:
        impulse = -(1 + elasticity) * rela_v_ground / impulse_contribution
        if impulse > 0:
          # friction
          timpulse = -corner_v.dot(tao) / timpulse_contribution
          timpulse = ti.min(friction * impulse,
                            ti.max(-friction * impulse, timpulse))

      if corner_x[1] < ground_height:
        # apply penalty
        impulse = impulse - dt * penalty * (
            corner_x[1] - ground_height) / impulse_contribution

      apply_impulse(t, i, impulse * normal + timpulse * tao, corner_x)

@ti.kernel
def advance(t: ti.i32):
  for i in range(n_objects):
    s = math.exp(-dt * damping)
    v[t, i] = s * v[t - 1, i] + v_inc[t, i] + dt * gravity * ti.Vector(
      [0.0, 1.0])
    x[t, i] = x[t - 1, i] + dt * v[t, i]
    omega[t, i] = s * omega[t - 1, i] + omega_inc[t, i]
    rotation[t, i] = rotation[t - 1, i] + dt * omega[t, i]

@ti.kernel
def compute_loss(t: ti.i32):
  loss[None] = (x[t, head_id] - ti.Vector(goal)).norm()

gui = tc.core.GUI("Rigid Body", tc.Vectori(1024, 1024))
canvas = gui.get_canvas()

def forward(output=None):

  initialize_properties()

  interval = vis_interval
  total_steps = steps
  if output:
    interval = output_vis_interval
    os.makedirs('rigid_body/{}/'.format(output), exist_ok=True)
    total_steps *= 2

  for t in range(1, total_steps):
    collide(t - 1)
    advance(t)

    if (t + 1) % interval == 0:
      canvas.clear(0xFFFFFF)
      for i in range(n_objects):
        points = []
        for k in range(4):
          offset_scale = [[-1, -1], [1, -1], [1, 1], [-1, 1]][k]
          rot = rotation[t, i]
          rot_matrix = np.array(
            [[math.cos(rot), -math.sin(rot)], [math.sin(rot), math.cos(rot)]])

          pos = np.array(
            [x[t, i][0], x[t, i][1]]) + offset_scale * rot_matrix @ np.array(
            [halfsize[i][0], halfsize[i][1]])
          points.append((pos[0], pos[1]))

        for k in range(4):
          canvas.path(tc.Vector(*points[k]), tc.Vector(*points[(k + 1) % 4])).radius(2).color(0x0).finish()

      offset = 0.003
      canvas.path(tc.Vector(0.05, ground_height - offset), tc.Vector(0.95, ground_height - offset)).radius(2).color(0xAAAAAA).finish()

      if output:
        gui.screenshot('rigid_body/{}/{:04d}.png'.format(output, t))

      gui.update()

  loss[None] = 0
  compute_loss(steps - 1)

@ti.kernel
def clear_states():
  for t in range(0, max_steps):
    for i in range(0, n_objects):
      v_inc[t, i] = ti.Vector([0.0, 0.0])
      omega_inc[t, i] = 0.0

def main():
  x[0, 0] = [0.5, 0.5]
  halfsize[0] = [0.2, 0.03]
  omega[0, 0] = 1
  # forward('initial')
  for iter in range(50):
    clear_states()

    with ti.Tape(loss):
      forward()

    print('Iter=', iter, 'Loss=', loss[None])
    x[0, 0][0] = x[0, 0][0] - x.grad[0, 0][0] * learning_rate

if __name__ == '__main__':
  main()
<commit_msg>saw-tooth<commit_after>import sys

import taichi_lang as ti
import math
import numpy as np
import cv2
import os
import matplotlib.pyplot as plt
import time
from matplotlib.pyplot import cm
import taichi as tc

real = ti.f32
ti.set_default_fp(real)

max_steps = 4096
vis_interval = 128
output_vis_interval = 16
steps = 2048
assert steps * 2 <= max_steps

vis_resolution = 1024

scalar = lambda: ti.var(dt=real)
vec = lambda: ti.Vector(2, dt=real)

loss = scalar()

x = vec()
v = vec()
rotation = scalar()
omega = scalar()

halfsize = vec()

inverse_mass = scalar()
inverse_inertia = scalar()

v_inc = vec()
omega_inc = scalar()

head_id = 3
goal = [0.9, 0.15]

n_objects = 1
elasticity = 1.0
ground_height = 0.1
gravity = 0# -9.8
friction = 0.0
penalty = 1e4
damping = 0

@ti.layout
def place():
  ti.root.dense(ti.l, max_steps).dense(ti.i, n_objects).place(x, v, rotation,
                                                              omega, v_inc,
                                                              omega_inc)
  ti.root.dense(ti.i, n_objects).place(halfsize, inverse_mass, inverse_inertia)
  ti.root.place(loss)
  ti.root.lazy_grad()

dt = 0.0002
learning_rate = 1.0

@ti.func
def rotation_matrix(r):
  return ti.Matrix([[ti.cos(r), -ti.sin(r)], [ti.sin(r), ti.cos(r)]])

@ti.kernel
def initialize_properties():
  for i in range(n_objects):
    inverse_mass[i] = 1.0 / (4 * halfsize[i][0] * halfsize[i][1])
    inverse_inertia[i] = 1.0 / (4 / 3 * halfsize[i][0] * halfsize[i][1] * (
        halfsize[i][0] * halfsize[i][0] + halfsize[i][1] * halfsize[i][1]))
    # ti.print(inverse_mass[i])
    # ti.print(inverse_inertia[i])

@ti.func
def cross(a, b):
  return a[0] * b[1] - a[1] * b[0]

@ti.func
def to_world(t, i, rela_x):
  rot = rotation[t, i]
  rot_matrix = rotation_matrix(rot)

  rela_pos = rot_matrix @ rela_x
  rela_v = omega[t, i] * ti.Vector([-rela_pos[1], rela_pos[0]])

  world_x = x[t, i] + rela_pos
  world_v = v[t, i] + rela_v

  return world_x, world_v, rela_pos

@ti.func
def apply_impulse(t, i, impulse, location):
  ti.atomic_add(v_inc[t + 1, i], impulse * inverse_mass[i])
  ti.atomic_add(omega_inc[t + 1, i],
                cross(location - x[t, i], impulse) * inverse_inertia[i])

@ti.kernel
def collide(t: ti.i32):
  for i in range(n_objects):
    hs = halfsize[i]
    for k in ti.static(range(4)):
      # the corner for collision detection
      offset_scale = ti.Vector([k % 2 * 2 - 1, k // 2 % 2 * 2 - 1])

      corner_x, corner_v, rela_pos = to_world(t, i, offset_scale * hs)
      corner_v = corner_v + dt * gravity * ti.Vector([0.0, 1.0])

      # Apply impulse so that there's no sinking
      normal = ti.Vector([0.0, 1.0])
      tao = ti.Vector([1.0, 0.0])

      rn = cross(rela_pos, normal)
      rt = cross(rela_pos, tao)
      impulse_contribution = inverse_mass[i] + ti.sqr(rn) * \
                             inverse_inertia[i]
      timpulse_contribution = inverse_mass[i] + ti.sqr(rt) * \
                              inverse_inertia[i]

      rela_v_ground = normal.dot(corner_v)

      impulse = 0.0
      timpulse = 0.0
      if rela_v_ground < 0 and corner_x[1] < ground_height:
        impulse = -(1 + elasticity) * rela_v_ground / impulse_contribution
        if impulse > 0:
          # friction
          timpulse = -corner_v.dot(tao) / timpulse_contribution
          timpulse = ti.min(friction * impulse,
                            ti.max(-friction * impulse, timpulse))

      if corner_x[1] < ground_height:
        # apply penalty
        impulse = impulse - dt * penalty * (
            corner_x[1] - ground_height) / impulse_contribution

      apply_impulse(t, i, impulse * normal + timpulse * tao, corner_x)

@ti.kernel
def advance(t: ti.i32):
  for i in range(n_objects):
    s = math.exp(-dt * damping)
    v[t, i] = s * v[t - 1, i] + v_inc[t, i] + dt * gravity * ti.Vector(
      [0.0, 1.0])
    x[t, i] = x[t - 1, i] + dt * v[t, i]
    omega[t, i] = s * omega[t - 1, i] + omega_inc[t, i]
    rotation[t, i] = rotation[t - 1, i] + dt * omega[t, i]

@ti.kernel
def compute_loss(t: ti.i32):
  loss[None] = (x[t, head_id] - ti.Vector(goal)).norm()

gui = tc.core.GUI("Rigid Body", tc.Vectori(1024, 1024))
canvas = gui.get_canvas()

def forward(output=None, visualize=True):

  initialize_properties()

  interval = vis_interval
  total_steps = steps
  if output:
    interval = output_vis_interval
    os.makedirs('rigid_body/{}/'.format(output), exist_ok=True)
    total_steps *= 2

  for t in range(1, total_steps):
    collide(t - 1)
    advance(t)

    if (t + 1) % interval == 0 and visualize:
      canvas.clear(0xFFFFFF)
      for i in range(n_objects):
        points = []
        for k in range(4):
          offset_scale = [[-1, -1], [1, -1], [1, 1], [-1, 1]][k]
          rot = rotation[t, i]
          rot_matrix = np.array(
            [[math.cos(rot), -math.sin(rot)], [math.sin(rot), math.cos(rot)]])

          pos = np.array(
            [x[t, i][0], x[t, i][1]]) + offset_scale * rot_matrix @ np.array(
            [halfsize[i][0], halfsize[i][1]])
          points.append((pos[0], pos[1]))

        for k in range(4):
          canvas.path(tc.Vector(*points[k]), tc.Vector(*points[(k + 1) % 4])).radius(2).color(0x0).finish()

      offset = 0.003
      canvas.path(tc.Vector(0.05, ground_height - offset), tc.Vector(0.95, ground_height - offset)).radius(2).color(0xAAAAAA).finish()

      if output:
        gui.screenshot('rigid_body/{}/{:04d}.png'.format(output, t))

      gui.update()

  loss[None] = 0
  compute_loss(steps - 1)

@ti.kernel
def clear_states():
  for t in range(0, max_steps):
    for i in range(0, n_objects):
      v_inc[t, i] = ti.Vector([0.0, 0.0])
      omega_inc[t, i] = 0.0

def main():
  losses = []
  grads = []
  for i in range(-100, 100):
    x[0, 0] = [0.7, 0.5]
    v[0, 0] = [-1, -2]
    halfsize[0] = [0.1, 0.1]
    omega[0, 0] = (i + 0.5) * 0.01
    # forward('initial')
    # for iter in range(50):
    clear_states()

    with ti.Tape(loss):
      forward(visualize=False)

    print('Iter=', i, 'Loss=', loss[None])
    print(omega.grad[0, 0])
    losses.append(loss[None])
    grads.append(omega.grad[0, 0] * 20)
    # x[0, 0][0] = x[0, 0][0] - x.grad[0, 0][0] * learning_rate
  plt.plot(losses)
  # plt.plot(grads)
  plt.show()

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


<commit_before>import datetime
import os
import sys

from threading import Timer

from plotly import graph_objs as go
from plotly import plotly

sys.path.append(os.getenv('PEAS', '.'))  # Append the $PEAS dir

from peas import weather

header = "{},{},{},{},{},{},{},{},{},{},{},{},{}\n".format(
    'date',
    'safe',
    'ambient_temp_C',
    'sky_temp_C',
    'rain_sensor_temp_C',
    'rain_frequency',
    'wind_speed_KPH',
    'ldr_resistance_Ohm',
    'pwm_value',
    'gust_condition',
    'wind_condition',
    'sky_condition',
    'rain_condition',
)

stream = None

def get_temp_plot(stream_token):
    trace0 = go.Scatter(x=[], y=[], name='MQ Observatory Temperature', mode='lines',
                        stream={'token': stream_token, 'maxpoints': 200}
                        )
    layout = go.Layout(
        xaxis={
            'title': 'Time'
        },
        yaxis={
            'title': 'Temp [C]'
        })

    fig = go.Figure(data=[trace0], layout=layout)

    url = plotly.plot(fig, filename='MQObs Weather - Temp')
    print("Plot available at {}".format(url))

    stream = plotly.Stream(stream_token)
    stream.open()

def write_header(filename):
    # Write out the header to the CSV file
    with open(filename, 'w') as f:
        f.write(header)

def read_capture(delay=30.0, continuous=True, plotly_stream=False, filename=None, **kwargs):
    """ A function that reads the AAG weather can calls itself on a timer """
    data = aag.capture()

    entry = "{},{},{},{},{},{},{},{:0.5f},{:0.5f},{},{},{},{}\n".format(
        data['date'].strftime('%Y-%m-%d %H:%M:%S'),
        data['safe'],
        data['ambient_temp_C'],
        data['sky_temp_C'],
        data['rain_sensor_temp_C'],
        data['rain_frequency'],
        data['wind_speed_KPH'],
        data['ldr_resistance_Ohm'],
        data['pwm_value'],
        data['gust_condition'],
        data['wind_condition'],
        data['sky_condition'],
        data['rain_condition'],
    )

    with open(filename, 'a') as f:
        f.write(entry)

    if plotly_stream:
        stream.write({'x': datetime.datetime.now(), 'y': data['ambient_temp_C']})

    if continuous:
        Timer(delay, read_capture).start()

if __name__ == '__main__':
    import argparse

    # Weather object
    aag = weather.AAGCloudSensor(use_mongo=False)

    # Get the command line option
    parser = argparse.ArgumentParser(
        description="Make a plot of the weather for a give date.")

    parser.add_argument('--loop', action='store_true', default=True,
                        help="If should keep reading, defaults to True")
    parser.add_argument("-d", "--delay", dest="delay", default=30.0,
                        help="Interval to read weather")
    parser.add_argument("-f", "--file", dest="filename", default='weather_info.csv',
                        help="Where to save results")
    parser.add_argument('--plotly', help="Stream to plotly")
    parser.add_argument('--stream-token', help="Plotly stream token", default=None)
    args = parser.parse_args()

    if args.stream_token is not None:
        args.plotly = True

    if args.plotly:
        assert args.stream_token is not None
        get_temp_plot(args.stream_token)

    # Do the initial call
    read_capture(**vars(args))
<commit_msg>Look for filename properly<commit_after>import datetime
import os
import sys

from threading import Timer

from plotly import graph_objs as go
from plotly import plotly

sys.path.append(os.getenv('PEAS', '.'))  # Append the $PEAS dir

from peas import weather

header = "{},{},{},{},{},{},{},{},{},{},{},{},{}\n".format(
    'date',
    'safe',
    'ambient_temp_C',
    'sky_temp_C',
    'rain_sensor_temp_C',
    'rain_frequency',
    'wind_speed_KPH',
    'ldr_resistance_Ohm',
    'pwm_value',
    'gust_condition',
    'wind_condition',
    'sky_condition',
    'rain_condition',
)

stream = None

def get_temp_plot(stream_token):
    trace0 = go.Scatter(x=[], y=[], name='MQ Observatory Temperature', mode='lines',
                        stream={'token': stream_token, 'maxpoints': 200}
                        )
    layout = go.Layout(
        xaxis={
            'title': 'Time'
        },
        yaxis={
            'title': 'Temp [C]'
        })

    fig = go.Figure(data=[trace0], layout=layout)

    url = plotly.plot(fig, filename='MQObs Weather - Temp')
    print("Plot available at {}".format(url))

    stream = plotly.Stream(stream_token)
    stream.open()

def write_header(filename):
    # Write out the header to the CSV file
    with open(filename, 'w') as f:
        f.write(header)

def read_capture(delay=30.0, continuous=True, plotly_stream=False, filename=None, **kwargs):
    """ A function that reads the AAG weather can calls itself on a timer """
    data = aag.capture()

    entry = "{},{},{},{},{},{},{},{:0.5f},{:0.5f},{},{},{},{}\n".format(
        data['date'].strftime('%Y-%m-%d %H:%M:%S'),
        data['safe'],
        data['ambient_temp_C'],
        data['sky_temp_C'],
        data['rain_sensor_temp_C'],
        data['rain_frequency'],
        data['wind_speed_KPH'],
        data['ldr_resistance_Ohm'],
        data['pwm_value'],
        data['gust_condition'],
        data['wind_condition'],
        data['sky_condition'],
        data['rain_condition'],
    )

    if filename is not None:
        with open(filename, 'a') as f:
            f.write(entry)

    if plotly_stream:
        stream.write({'x': datetime.datetime.now(), 'y': data['ambient_temp_C']})

    if continuous:
        Timer(delay, read_capture).start()

if __name__ == '__main__':
    import argparse

    # Weather object
    aag = weather.AAGCloudSensor(use_mongo=False)

    # Get the command line option
    parser = argparse.ArgumentParser(
        description="Make a plot of the weather for a give date.")

    parser.add_argument('--loop', action='store_true', default=True,
                        help="If should keep reading, defaults to True")
    parser.add_argument("-d", "--delay", dest="delay", default=30.0,
                        help="Interval to read weather")
    parser.add_argument("-f", "--file", dest="filename", default='weather_info.csv',
                        help="Where to save results")
    parser.add_argument('--plotly', help="Stream to plotly")
    parser.add_argument('--stream-token', help="Plotly stream token", default=None)
    args = parser.parse_args()

    if args.stream_token is not None:
        args.plotly = True

    if args.plotly:
        assert args.stream_token is not None
        get_temp_plot(args.stream_token)

    # Do the initial call
    read_capture(**vars(args))
<|endoftext|>


<commit_before>"""
Copyright 2019 Google LLC

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.

Convert spike sim log to standard riscv instruction trace format
"""

import argparse
import os
import re
import sys
import logging

sys.path.insert(0, os.path.dirname(os.path.realpath(__file__)))

from riscv_trace_csv import *

def process_spike_sim_log(spike_log, csv):
  """Process SPIKE simulation log.

  Extract instruction and affected register information from spike simulation
  log and save to a list.
  """
  logging.info("Processing spike log : %s" % spike_log)
  instr_cnt = 0
  spike_instr = ""

  RD_RE    = re.compile(r"(?P<pri>\d) 0x(?P<addr>[a-f0-9]+?) " \
                      "\((?P<bin>.*?)\) x\s*(?P<reg>\d*?) 0x(?P<val>.*)")
  CORE_RE  = re.compile(r"core.*0x(?P<addr>[a-f0-9]+?) \(0x(?P<bin>.*?)\) (?P<instr>.*?)$")
  INSTR_RE = re.compile(r"(?P<instr>[a-z\.]+?)(\s+?)(?P<operand>.*)")
  GPR_RE   = re.compile(r"^[a-z][0-9a-z]$")
  CSR_RE   = re.compile(r"csr")

  # Remove all the init spike boot instructions
  cmd = ("sed -i '/core.*0x0000000000001010/,$!d' %s" % spike_log)
  os.system(cmd)
  # Remove all instructions after ecall (end of program excecution)
  cmd = ("sed -i '/ecall/q' %s" % spike_log)
  os.system(cmd)

  gpr = {}
  gpr["zero"] = 0

  with open(spike_log, "r") as f, open(csv, "w") as csv_fd:
    trace_csv = RiscvInstructiontTraceCsv(csv_fd)
    trace_csv.start_new_trace()
    for line in f:
      # Extract instruction infromation
      m = CORE_RE.search(line)
      if m:
        spike_instr = m.group("instr")
        rv_instr_trace = RiscvInstructiontTraceEntry()
        rv_instr_trace.instr_str = spike_instr
        rv_instr_trace.addr = m.group("addr")
        rv_instr_trace.binary = m.group("bin")
        nextline = f.readline()
        if nextline != "":
          m = RD_RE.search(nextline)
          if m:
            # Extract RD information
            instr_cnt += 1
            rv_instr_trace.rd = gpr_to_abi("x%0s" % m.group("reg"))
            rv_instr_trace.rd_val = m.group("val")
            rv_instr_trace.privileged_mode = m.group("pri")
            gpr[rv_instr_trace.rd] = rv_instr_trace.rd_val
          s = INSTR_RE.search(spike_instr)
          if s:
            rv_instr_trace.instr = s.group("instr")
            operand_str = s.group("operand").replace(" ", "")
            if operand_str != "" :
              operands = operand_str.split(",")
              if CSR_RE.search(s.group("instr")):
                # CSR instruction
                operand = operands[-1]
                if GPR_RE.search(operand) or operand == "zero":
                  rv_instr_trace.rs1 = operand
                  rv_instr_trace.rs1_val = gpr[operand]
                else:
                  rv_instr_trace.imm = operand
              else:
                # Non CSR instruction
                for i in range(1, len(operands)):
                  operand = operands[i]
                  if GPR_RE.search(operand) or operand == "zero":
                    if i == 1:
                      rv_instr_trace.rs1 = operand
                      rv_instr_trace.rs1_val = gpr[operand]
                    else:
                      rv_instr_trace.rs2 = operands[i]
                      rv_instr_trace.rs2_val = gpr[operand]
                  else:
                    rv_instr_trace.imm = operand
          trace_csv.write_trace_entry(rv_instr_trace)
  logging.info("Processed instruction count : %d" % instr_cnt)

def main():
  instr_trace = []
  # Parse input arguments
  parser = argparse.ArgumentParser()
  parser.add_argument("--log", type=str, help="Input spike simulation log")
  parser.add_argument("--csv", type=str, help="Output trace csv_buf file")
  args = parser.parse_args()
  # Process spike log
  process_spike_sim_log(args.log, args.csv)

if __name__ == "__main__":
  main()
<commit_msg>Fix spike log processing issue<commit_after>"""
Copyright 2019 Google LLC

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.

Convert spike sim log to standard riscv instruction trace format
"""

import argparse
import os
import re
import sys
import logging

sys.path.insert(0, os.path.dirname(os.path.realpath(__file__)))

from riscv_trace_csv import *

def process_spike_sim_log(spike_log, csv):
  """Process SPIKE simulation log.

  Extract instruction and affected register information from spike simulation
  log and save to a list.
  """
  logging.info("Processing spike log : %s" % spike_log)
  instr_cnt = 0
  spike_instr = ""

  RD_RE    = re.compile(r"(?P<pri>\d) 0x(?P<addr>[a-f0-9]+?) " \
                      "\((?P<bin>.*?)\) x\s*(?P<reg>\d*?) 0x(?P<val>.*)")
  CORE_RE  = re.compile(r"core.*0x(?P<addr>[a-f0-9]+?) \(0x(?P<bin>.*?)\) (?P<instr>.*?)$")
  INSTR_RE = re.compile(r"(?P<instr>[a-z\.]+?)(\s+?)(?P<operand>.*)")
  GPR_RE   = re.compile(r"^[a-z][0-9a-z]$")
  CSR_RE   = re.compile(r"csr")

  # Remove all the init spike boot instructions
  cmd = ("sed -i '/core.*0x0000000000001010/,$!d' %s" % spike_log)
  os.system(cmd)
  # Remove all instructions after ecall (end of program excecution)
  cmd = ("sed -i '/ecall/q' %s" % spike_log)
  os.system(cmd)

  gpr = {}
  gpr["zero"] = 0

  with open(spike_log, "r") as f, open(csv, "w") as csv_fd:
    trace_csv = RiscvInstructiontTraceCsv(csv_fd)
    trace_csv.start_new_trace()
    for line in f:
      # Extract instruction infromation
      m = CORE_RE.search(line)
      if m:
        spike_instr = m.group("instr")
        rv_instr_trace = RiscvInstructiontTraceEntry()
        rv_instr_trace.instr_str = spike_instr
        rv_instr_trace.addr = m.group("addr")
        rv_instr_trace.binary = m.group("bin")
        if spike_instr == "wfi":
          trace_csv.write_trace_entry(rv_instr_trace)
          continue
        nextline = f.readline()
        if nextline != "":
          m = RD_RE.search(nextline)
          if m:
            # Extract RD information
            instr_cnt += 1
            rv_instr_trace.rd = gpr_to_abi("x%0s" % m.group("reg"))
            rv_instr_trace.rd_val = m.group("val")
            rv_instr_trace.privileged_mode = m.group("pri")
            gpr[rv_instr_trace.rd] = rv_instr_trace.rd_val
          s = INSTR_RE.search(spike_instr)
          if s:
            rv_instr_trace.instr = s.group("instr")
            operand_str = s.group("operand").replace(" ", "")
            if operand_str != "" :
              operands = operand_str.split(",")
              if CSR_RE.search(s.group("instr")):
                # CSR instruction
                operand = operands[-1]
                if GPR_RE.search(operand) or operand == "zero":
                  rv_instr_trace.rs1 = operand
                  rv_instr_trace.rs1_val = gpr[operand]
                else:
                  rv_instr_trace.imm = operand
              else:
                # Non CSR instruction
                for i in range(1, len(operands)):
                  operand = operands[i]
                  if GPR_RE.search(operand) or operand == "zero":
                    if i == 1:
                      rv_instr_trace.rs1 = operand
                      rv_instr_trace.rs1_val = gpr[operand]
                    else:
                      rv_instr_trace.rs2 = operands[i]
                      rv_instr_trace.rs2_val = gpr[operand]
                  else:
                    rv_instr_trace.imm = operand
          trace_csv.write_trace_entry(rv_instr_trace)
  logging.info("Processed instruction count : %d" % instr_cnt)

def main():
  instr_trace = []
  # Parse input arguments
  parser = argparse.ArgumentParser()
  parser.add_argument("--log", type=str, help="Input spike simulation log")
  parser.add_argument("--csv", type=str, help="Output trace csv_buf file")
  args = parser.parse_args()
  # Process spike log
  process_spike_sim_log(args.log, args.csv)

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


<commit_before>#  Copyright 2008-2011 Nokia Siemens Networks Oyj
#
#  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 robot.result.visitor import Visitor, SkipAllVisitor

def KeywordRemover(how):
    how = how and how.upper()
    if how == 'PASSED':
        return PassedKeywordRemover()
    if how == 'FOR':
        return ForLoopItemsRemover()
    if how == 'ALL':
        return AllKeywordsRemover()
    return SkipAllVisitor()

class _KeywordRemover(Visitor):

    def _clear_content(self, keyword):
        keyword.messages = []
        keyword.keywords = []

    def _contains_warning(self, item):
        contains_warning_visitor = ContainsWarningVisitor()
        item.visit(contains_warning_visitor)
        return contains_warning_visitor.result

class AllKeywordsRemover(_KeywordRemover):

    def visit_keyword(self, keyword):
        self._clear_content(keyword)

class PassedKeywordRemover(_KeywordRemover):

    def visit_keyword(self, keyword):
        if keyword.is_passed:
            self._clear_content(keyword)

    def visit_test(self, test):
        if test.is_passed and not self._contains_warning(test):
            for keyword in test.keywords:
                self._clear_content(keyword)

class ForLoopItemsRemover(_KeywordRemover):

    def start_keyword(self, keyword):
        if not keyword.is_passed:
            return False
        if keyword.is_forloop:
            self._clear_content(keyword)
            return False

    def start_test(self, test):
        return test.is_passed and not self._contains_warning(test)

def _stop_if_result(method):
    def wrapped(s, i):
        if s.result:
            return
        method(s, i)
    return wrapped

class ContainsWarningVisitor(Visitor):

    def __init__(self):
        self.result = False

    def visit_message(self, msg):
        self.result |= msg.level == 'WARN'

    visit_keyword = _stop_if_result(Visitor.visit_keyword)
    visit_suite = _stop_if_result(Visitor.visit_suite)
    visit_test = _stop_if_result(Visitor.visit_test)

<commit_msg>Automated merge with https://robotframework.googlecode.com/hg/<commit_after>#  Copyright 2008-2011 Nokia Siemens Networks Oyj
#
#  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 robot.result.visitor import Visitor, SkipAllVisitor

def KeywordRemover(how):
    how = how and how.upper()
    if how == 'PASSED':
        return PassedKeywordRemover()
    if how == 'FOR':
        return ForLoopItemsRemover()
    if how == 'ALL':
        return AllKeywordsRemover()
    return SkipAllVisitor()

class _KeywordRemover(Visitor):

    def _clear_content(self, keyword):
        keyword.messages = []
        keyword.keywords = []

    def _contains_warning(self, item):
        contains_warning_visitor = ContainsWarningVisitor()
        item.visit(contains_warning_visitor)
        return contains_warning_visitor.result

class AllKeywordsRemover(_KeywordRemover):

    def visit_keyword(self, keyword):
        self._clear_content(keyword)

class PassedKeywordRemover(_KeywordRemover):

    def _should_be_cleared(self, item):
        return item.is_passed and not self._contains_warning(item)

    def visit_keyword(self, keyword):
        if self._should_be_cleared(keyword):
            self._clear_content(keyword)

    def visit_test(self, test):
        if self._should_be_cleared(test):
            for keyword in test.keywords:
                self._clear_content(keyword)

class ForLoopItemsRemover(_KeywordRemover):

    def start_keyword(self, keyword):
        if not keyword.is_passed:
            return False
        if keyword.is_forloop:
            self._clear_content(keyword)
            return False

    def start_test(self, test):
        return test.is_passed and not self._contains_warning(test)

def _stop_if_result(method):
    def wrapped(s, i):
        if s.result:
            return
        method(s, i)
    return wrapped

class ContainsWarningVisitor(Visitor):

    def __init__(self):
        self.result = False

    def visit_message(self, msg):
        self.result |= msg.level == 'WARN'

    visit_keyword = _stop_if_result(Visitor.visit_keyword)
    visit_suite = _stop_if_result(Visitor.visit_suite)
    visit_test = _stop_if_result(Visitor.visit_test)

<|endoftext|>


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

import os
import types
import inspect

from robot import utils
from robot.errors import DataError
from robot.common import BaseLibrary
from robot.output import SYSLOG

from handlers import Handler, InitHandler, DynamicHandler

if utils.is_jython:
    from org.python.core import PyReflectedFunction, PyReflectedConstructor
    from java.lang import Object

def TestLibrary(name, args=None):
    libcode, source = utils.import_(name)
    libclass = _get_lib_class(libcode)
    return libclass(libcode, source, name, utils.to_list(args))

def _get_lib_class(libcode):
    if isinstance(libcode, types.ModuleType):
        return _ModuleLibrary
    if _get_dynamic_method(libcode, 'get_keyword_names'):
        if _get_dynamic_method(libcode, 'run_keyword'):
            return _DynamicLibrary
        else:
            return _HybridLibrary
    return _ClassLibrary

def _get_dynamic_method(code, underscore_name):
    tokens = underscore_name.split('_')
    tokens = [tokens[0]] + [ t.capitalize() for t in tokens[1:] ]
    camelCaseName = ''.join(tokens)
    for name in underscore_name, camelCaseName:
        if hasattr(code, name):
            method = getattr(code, name)
            if callable(method):
                return method
    return None

class _BaseTestLibrary(BaseLibrary):

    def __init__(self, libcode, source, name, args):
        if os.path.exists(name):
            name = os.path.splitext(os.path.basename(name))[0]
        self.source = source
        self.version = self._get_version(libcode)
        self.name = name
        self.orig_name = name # Stores original name also after copying
        self.args = args
        self._instance_cache = []
        if libcode is not None:
            self.doc = utils.get_doc(libcode)
            self.scope = self._get_scope(libcode)
            self._libcode = libcode
            self.init =  self._create_init_handler(libcode)
            self._libinst = self.get_instance()
            self.handlers = self._create_handlers(self._libinst)
            self._init_scope_handling()

    def copy(self, name):
        lib = _BaseTestLibrary(None, self.version, name, self.args)
        lib.orig_name = self.name
        lib.doc = self.doc
        lib.scope = self.scope
        lib._libcode = self._libcode
        lib._libinst = self._libinst
        lib.init = self.init
        lib._init_scope_handling()
        lib.handlers = utils.NormalizedDict(ignore=['_'])
        for handler_name, handler in self.handlers.items():
            lib.handlers[handler_name] = handler.copy(lib)
        return lib

    def start_suite(self):
        pass

    def end_suite(self):
        pass

    def start_test(self):
        pass

    def end_test(self):
        pass

    def _get_version(self, code):
        try:
            return str(code.ROBOT_LIBRARY_VERSION)
        except AttributeError:
            try:
                return str(code.__version__)
            except AttributeError:
                return '<unknown>'

    def _create_init_handler(self, libcode):
        init_method =  getattr(libcode, '__init__', None)
        if not self._valid_init(init_method):
            init_method = None
        return InitHandler(self, init_method)

    def _valid_init(self, init_method):
        if utils.is_jython and isinstance(init_method, PyReflectedConstructor):
            return True
        if isinstance(init_method, (types.MethodType, types.FunctionType)):
            return True
        return False

    def _init_scope_handling(self):
        if self.scope == 'GLOBAL':
            return
        self._libinst = None
        self.start_suite = self._caching_start
        self.end_suite = self._restoring_end
        if self.scope == 'TESTCASE':
            self.start_test = self._caching_start
            self.end_test = self._restoring_end

    def _caching_start(self):
        self._instance_cache.append(self._libinst)
        self._libinst = None

    def _restoring_end(self):
        self._libinst = self._instance_cache.pop()

    def _get_scope(self, libcode):
        try:
            scope = libcode.ROBOT_LIBRARY_SCOPE
            scope = utils.normalize(scope, ignore=['_']).upper()
        except:
            scope = 'TESTCASE'
        return scope in ['GLOBAL','TESTSUITE'] and scope or 'TESTCASE'

    def get_instance(self):
        try:
            if self._libinst is None:
                self._libinst = self._get_instance()
        except AttributeError:
            self._libinst = self._get_instance()
        return self._libinst

    def _get_instance(self):
        self.init.check_arg_limits(self.args)
        try:
            return self._libcode(*self.args)
        except:
            self._raise_creating_instance_failed()

    def _create_handlers(self, libinst):
        success, failure, details = self._get_reporting_methods()
        handlers = utils.NormalizedDict(ignore=['_'])
        for name in self._get_handler_names(libinst):
            err_pre = "Adding keyword '%s' to library '%s' failed: " % (name, self.name)
            try:
                method = self._get_handler_method(self._libinst, name)
                success("Got handler method '%s'" % (name))
            except TypeError:
                failure(err_pre + 'Not a method or function')
                continue
            except:
                err_msg, err_details = utils.get_error_details()
                failure(err_pre + 'Getting handler method failed: ' + err_msg)
                details('Details:\n%s' % err_details)
                continue
            try:
                handlers[name] = self._create_handler(name, method)
                success("Created keyword '%s'" % handlers[name].name)
            except:
                err_msg, err_details = utils.get_error_details()
                failure(err_pre + 'Creating keyword failed: ' + err_msg)
                details('Details:\n%s' % err_details)
        return handlers

    def _get_reporting_methods(self):
        # success, failure, details
        return SYSLOG.debug, SYSLOG.info, SYSLOG.debug

    def _get_handler_names(self, libcode):
        return [ name for name in dir(libcode)
                 if not (name.startswith('_') or
                         name.startswith('ROBOT_LIBRARY_')) ]

    def _get_handler_method(self, libcode, name):
        method = getattr(libcode, name)
        if inspect.isroutine(method):
            return method
        raise TypeError('Not a method or function')

    def _create_handler(self, handler_name, handler_method):
        return Handler(self, handler_name, handler_method)

    def _raise_creating_instance_failed(self):
        msg, details = utils.get_error_details()
        if self.args:
            args = "argument%s %s" % (utils.plural_or_not(self.args),
                                      utils.seq2str(self.args))
        else:
            args = "no arguments"
        raise DataError("Creating an instance of the test library '%s' with %s "
                        "failed: %s\n%s" % (self.name, args, msg, details))

class _ClassLibrary(_BaseTestLibrary):

    def _get_handler_method(self, libcode, name):
        # Type is checked before using getattr to avoid calling properties,
        # most importantly bean properties generated by Jython (issue 188).
        for item in (libcode,) + inspect.getmro(libcode.__class__):
            try:
                if self._is_valid_handler(item.__dict__[name]):
                    return getattr(libcode, name)
            except (KeyError, AttributeError):
                pass  # Java classes don't always have __dict__
        raise TypeError('Not a method or function')

    def _is_valid_handler(self, handler):
        if inspect.isroutine(handler):
            return True
        if not (utils.is_jython and isinstance(handler, PyReflectedFunction)):
            return False
        # When we get here, the handler is created in a Java class possibly
        # extended in Python. This code ignores handlers defined only in
        # java.lang.Object or generated by Jython (in org.python.proxies).
        for signature in handler.argslist[:handler.nargs]:
            # 'getName' may raise an exception -- not sure why but that happens
            # at least with handlers declared in class extending JUnit TestCase.
            try:
                cls = signature.declaringClass
                if not (cls is Object or
                        cls.getName().startswith('org.python.proxies.')):
                    return True
            except:
                return True
        return False

class _ModuleLibrary(_BaseTestLibrary):

    def _get_scope(self, libcode):
        return 'GLOBAL'

    def get_instance(self):
        self.init.check_arg_limits(self.args)
        return self._libcode

    def _create_init_handler(self, libcode):
        return InitHandler(self, None)

class _HybridLibrary(_BaseTestLibrary):

    def _get_handler_names(self, instance):
        try:
            return instance.get_keyword_names()
        except AttributeError:
            return instance.getKeywordNames()

    def _get_reporting_methods(self):
        # Use SYSLOG.warn for reporting possible failures when creating kws
        # to make them visible. With hybrid libraries kw names are returned
        # explicitly so creating them should also pass.
        return SYSLOG.debug, SYSLOG.warn, SYSLOG.info

class _DynamicLibrary(_BaseTestLibrary):

    def __init__(self, libcode, source, name, args):
        self._get_kw_doc = _get_dynamic_method(libcode, 'get_keyword_documentation')
        self._get_kw_args = _get_dynamic_method(libcode, 'get_keyword_arguments')
        _BaseTestLibrary.__init__(self, libcode, source, name, args)

    def _get_handler_names(self, instance):
        try:
            return instance.get_keyword_names()
        except AttributeError:
            return instance.getKeywordNames()

    def _get_handler_method(self, instance, name_is_ignored):
        try:
            return instance.run_keyword
        except AttributeError:
            return instance.runKeyword

    def _create_handler(self, handler_name, handler_method):
        if self._get_kw_doc:
            doc = self._get_kw_doc(self._libinst, handler_name)
        else:
            doc = ''
        if self._get_kw_args:
            argspec = self._get_kw_args(self._libinst, handler_name)
        else:
            argspec = None
        return DynamicHandler(self, handler_name, handler_method, doc, argspec)
<commit_msg>something missing from last commit<commit_after>#  Copyright 2008 Nokia Siemens Networks Oyj
#
#  Licensed under the Apache License, Version 2.0 (the "License");
#  you may not use this file except in compliance with the License.
#  You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
#  Unless required by applicable law or agreed to in writing, software
#  distributed under the License is distributed on an "AS IS" BASIS,
#  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#  See the License for the specific language governing permissions and
#  limitations under the License.

import os
import types
import inspect

from robot import utils
from robot.errors import DataError
from robot.common import BaseLibrary
from robot.output import SYSLOG

from handlers import Handler, InitHandler, DynamicHandler

if utils.is_jython:
    from org.python.core import PyReflectedFunction, PyReflectedConstructor
    from java.lang import Object

def TestLibrary(name, args=None):
    libcode, source = utils.import_(name)
    libclass = _get_lib_class(libcode)
    return libclass(libcode, source, name, utils.to_list(args))

def _get_lib_class(libcode):
    if isinstance(libcode, types.ModuleType):
        return _ModuleLibrary
    if _get_dynamic_method(libcode, 'get_keyword_names'):
        if _get_dynamic_method(libcode, 'run_keyword'):
            return _DynamicLibrary
        else:
            return _HybridLibrary
    return _ClassLibrary

def _get_dynamic_method(code, underscore_name):
    tokens = underscore_name.split('_')
    tokens = [tokens[0]] + [ t.capitalize() for t in tokens[1:] ]
    camelCaseName = ''.join(tokens)
    for name in underscore_name, camelCaseName:
        if hasattr(code, name):
            method = getattr(code, name)
            if callable(method):
                return method
    return None

class _BaseTestLibrary(BaseLibrary):

    def __init__(self, libcode, source, name, args):
        if os.path.exists(name):
            name = os.path.splitext(os.path.basename(name))[0]
        self.source = source
        self.version = self._get_version(libcode)
        self.name = name
        self.orig_name = name # Stores original name also after copying
        self.args = args
        self._instance_cache = []
        if libcode is not None:
            self.doc = utils.get_doc(libcode)
            self.scope = self._get_scope(libcode)
            self._libcode = libcode
            self.init =  self._create_init_handler(libcode)
            self._libinst = self.get_instance()
            self.handlers = self._create_handlers(self._libinst)
            self._init_scope_handling()

    def copy(self, name):
        lib = _BaseTestLibrary(None, self.version, name, self.args)
        lib.orig_name = self.name
        lib.doc = self.doc
        lib.scope = self.scope
        lib._libcode = self._libcode
        lib._libinst = self._libinst
        lib.init = self.init
        lib._init_scope_handling()
        lib.handlers = utils.NormalizedDict(ignore=['_'])
        for handler_name, handler in self.handlers.items():
            lib.handlers[handler_name] = handler.copy(lib)
        return lib

    def start_suite(self):
        pass

    def end_suite(self):
        pass

    def start_test(self):
        pass

    def end_test(self):
        pass

    def _get_version(self, code):
        try:
            return str(code.ROBOT_LIBRARY_VERSION)
        except AttributeError:
            try:
                return str(code.__version__)
            except AttributeError:
                return '<unknown>'

    def _create_init_handler(self, libcode):
        init_method =  getattr(libcode, '__init__', None)
        if not self._valid_init(init_method):
            init_method = None
        return InitHandler(self, init_method)

    def _valid_init(self, init_method):
        if utils.is_jython and isinstance(init_method, PyReflectedConstructor):
            return True
        if isinstance(init_method, (types.MethodType, types.FunctionType)):
            return True
        return False

    def _init_scope_handling(self):
        if self.scope == 'GLOBAL':
            return
        self._libinst = None
        self.start_suite = self._caching_start
        self.end_suite = self._restoring_end
        if self.scope == 'TESTCASE':
            self.start_test = self._caching_start
            self.end_test = self._restoring_end

    def _caching_start(self):
        self._instance_cache.append(self._libinst)
        self._libinst = None

    def _restoring_end(self):
        self._libinst = self._instance_cache.pop()

    def _get_scope(self, libcode):
        try:
            scope = libcode.ROBOT_LIBRARY_SCOPE
            scope = utils.normalize(scope, ignore=['_']).upper()
        except (AttributeError, TypeError):
            scope = 'TESTCASE'
        return scope in ['GLOBAL','TESTSUITE'] and scope or 'TESTCASE'

    def get_instance(self):
        try:
            if self._libinst is None:
                self._libinst = self._get_instance()
        except AttributeError:
            self._libinst = self._get_instance()
        return self._libinst

    def _get_instance(self):
        self.init.check_arg_limits(self.args)
        try:
            return self._libcode(*self.args)
        except:
            self._raise_creating_instance_failed()

    def _create_handlers(self, libinst):
        success, failure, details = self._get_reporting_methods()
        handlers = utils.NormalizedDict(ignore=['_'])
        for name in self._get_handler_names(libinst):
            err_pre = "Adding keyword '%s' to library '%s' failed: " % (name, self.name)
            try:
                method = self._get_handler_method(self._libinst, name)
                success("Got handler method '%s'" % (name))
            except TypeError:
                failure(err_pre + 'Not a method or function')
                continue
            except:
                err_msg, err_details = utils.get_error_details()
                failure(err_pre + 'Getting handler method failed: ' + err_msg)
                details('Details:\n%s' % err_details)
                continue
            try:
                handlers[name] = self._create_handler(name, method)
                success("Created keyword '%s'" % handlers[name].name)
            except:
                err_msg, err_details = utils.get_error_details()
                failure(err_pre + 'Creating keyword failed: ' + err_msg)
                details('Details:\n%s' % err_details)
        return handlers

    def _get_reporting_methods(self):
        # success, failure, details
        return SYSLOG.debug, SYSLOG.info, SYSLOG.debug

    def _get_handler_names(self, libcode):
        return [ name for name in dir(libcode)
                 if not (name.startswith('_') or
                         name.startswith('ROBOT_LIBRARY_')) ]

    def _get_handler_method(self, libcode, name):
        method = getattr(libcode, name)
        if inspect.isroutine(method):
            return method
        raise TypeError('Not a method or function')

    def _create_handler(self, handler_name, handler_method):
        return Handler(self, handler_name, handler_method)

    def _raise_creating_instance_failed(self):
        msg, details = utils.get_error_details()
        if self.args:
            args = "argument%s %s" % (utils.plural_or_not(self.args),
                                      utils.seq2str(self.args))
        else:
            args = "no arguments"
        raise DataError("Creating an instance of the test library '%s' with %s "
                        "failed: %s\n%s" % (self.name, args, msg, details))

class _ClassLibrary(_BaseTestLibrary):

    def _get_handler_method(self, libcode, name):
        # Type is checked before using getattr to avoid calling properties,
        # most importantly bean properties generated by Jython (issue 188).
        for item in (libcode,) + inspect.getmro(libcode.__class__):
            try:
                if self._is_valid_handler(item.__dict__[name]):
                    return getattr(libcode, name)
            except (KeyError, AttributeError):
                pass  # Java classes don't always have __dict__
        raise TypeError('Not a method or function')

    def _is_valid_handler(self, handler):
        if inspect.isroutine(handler):
            return True
        if not (utils.is_jython and isinstance(handler, PyReflectedFunction)):
            return False
        # When we get here, the handler is created in a Java class possibly
        # extended in Python. This code ignores handlers defined only in
        # java.lang.Object or generated by Jython (in org.python.proxies).
        for signature in handler.argslist[:handler.nargs]:
            # 'getName' may raise an exception -- not sure why but that happens
            # at least with handlers declared in class extending JUnit TestCase.
            try:
                cls = signature.declaringClass
                if not (cls is Object or
                        cls.getName().startswith('org.python.proxies.')):
                    return True
            except:
                return True
        return False

class _ModuleLibrary(_BaseTestLibrary):

    def _get_scope(self, libcode):
        return 'GLOBAL'

    def get_instance(self):
        self.init.check_arg_limits(self.args)
        return self._libcode

    def _create_init_handler(self, libcode):
        return InitHandler(self, None)

class _HybridLibrary(_BaseTestLibrary):

    def _get_handler_names(self, instance):
        try:
            return instance.get_keyword_names()
        except AttributeError:
            return instance.getKeywordNames()

    def _get_reporting_methods(self):
        # Use SYSLOG.warn for reporting possible failures when creating kws
        # to make them visible. With hybrid libraries kw names are returned
        # explicitly so creating them should also pass.
        return SYSLOG.debug, SYSLOG.warn, SYSLOG.info

class _DynamicLibrary(_BaseTestLibrary):

    def __init__(self, libcode, source, name, args):
        self._get_kw_doc = _get_dynamic_method(libcode, 'get_keyword_documentation')
        self._get_kw_args = _get_dynamic_method(libcode, 'get_keyword_arguments')
        _BaseTestLibrary.__init__(self, libcode, source, name, args)

    def _get_handler_names(self, instance):
        try:
            return instance.get_keyword_names()
        except AttributeError:
            return instance.getKeywordNames()

    def _get_handler_method(self, instance, name_is_ignored):
        try:
            return instance.run_keyword
        except AttributeError:
            return instance.runKeyword

    def _create_handler(self, handler_name, handler_method):
        if self._get_kw_doc:
            doc = self._get_kw_doc(self._libinst, handler_name)
        else:
            doc = ''
        if self._get_kw_args:
            argspec = self._get_kw_args(self._libinst, handler_name)
        else:
            argspec = None
        return DynamicHandler(self, handler_name, handler_method, doc, argspec)
<|endoftext|>


<commit_before>#!/usr/bin/env python
# coding=utf-8
"""Test scriptworker.context
"""
import asyncio
import json
import mock
import os
import pytest
import taskcluster
import scriptworker.context as swcontext
from copy import deepcopy
from . import tmpdir
from . import rw_context as context

assert tmpdir, context  # silence pyflakes

# constants helpers and fixtures {{{1
@pytest.fixture(scope='function')
def claim_task():
    return {
        "credentials": {
            "task_credentials": True,
        },
        "task": {
            "task_defn": True,
        }
    }

@pytest.fixture(scope='function')
def reclaim_task():
    return {
        "credentials": {
            "reclaim_task_credentials": True,
        }
    }

def get_task_file(context):
    temp_dir = context.config['work_dir']
    path = os.path.join(temp_dir, "task.json")
    return path

def get_json(path):
    with open(path, "r") as fh:
        return json.load(fh)

# tests {{{1
def test_empty_context(context):
    assert context.task is None
    assert context.claim_task is None
    assert context.reclaim_task is None
    assert context.temp_credentials is None

@pytest.mark.asyncio
async def test_set_task(context, claim_task):
    context.claim_task = claim_task
    assert context.claim_task == claim_task
    assert context.reclaim_task is None
    assert context.temp_credentials == claim_task['credentials']
    assert get_json(get_task_file(context)) == claim_task['task']

@pytest.mark.asyncio
async def test_set_reclaim_task(context, claim_task, reclaim_task):
    context.claim_task = claim_task
    context.reclaim_task = reclaim_task
    assert context.claim_task == claim_task
    assert context.task == claim_task['task']
    assert context.reclaim_task == reclaim_task
    assert context.temp_credentials == reclaim_task['credentials']
    assert get_json(get_task_file(context)) == claim_task['task']

@pytest.mark.asyncio
async def test_set_reset_task(context, claim_task, reclaim_task):
    context.claim_task = claim_task
    context.reclaim_task = reclaim_task
    context.claim_task = None
    assert context.claim_task is None
    assert context.task is None
    assert context.reclaim_task is None
    assert context.proc is None
    assert context.temp_credentials is None
    assert context.temp_queue is None

def test_temp_queue(context, mocker):
    mocker.patch('taskcluster.aio.Queue')
    context.session = {'c': 'd'}
    context.temp_credentials = {'a': 'b'}
    assert taskcluster.aio.Queue.called_once_with({
        'credentials': context.temp_credentials,
    }, session=context.session)

@pytest.mark.asyncio
async def test_projects(context, mocker):
    fake_projects = {"mozilla-central": "blah", "count": 0}

    async def fake_load(*args):
        fake_projects['count'] += 1
        return deepcopy(fake_projects)

    mocker.patch.object(swcontext, "load_json_or_yaml_from_url", new=fake_load)
    assert context.projects is None
    await context.populate_projects()
    assert context.projects == fake_projects
    assert fake_projects['count'] == 1

    await context.populate_projects(force=True)
    assert context.projects == fake_projects
    assert fake_projects['count'] == 2

    await context.populate_projects()
    assert context.projects == fake_projects
    assert fake_projects['count'] == 2

def test_get_credentials(context):
    expected = {'asdf': 'foobar'}
    context._credentials = expected
    assert context.credentials == expected

def test_new_event_loop(mocker):
    """The default context.event_loop is from `asyncio.get_event_loop`"""
    fake_loop = mock.MagicMock()
    mocker.patch.object(asyncio, 'get_event_loop', return_value=fake_loop)
    context = swcontext.Context()
    assert context.event_loop == fake_loop

def test_set_event_loop(mocker):
    """`context.event_loop` returns the same value once set.

    (This may seem obvious, but this tests the correctness of the property.)

    """
    fake_loop = mock.MagicMock()
    context = swcontext.Context()
    context.event_loop = fake_loop
    assert context.event_loop == fake_loop
<commit_msg>address review comments<commit_after>#!/usr/bin/env python
# coding=utf-8
"""Test scriptworker.context
"""
import asyncio
import json
import mock
import os
import pytest
import taskcluster
import scriptworker.context as swcontext
from copy import deepcopy
from . import tmpdir
from . import rw_context as context

assert tmpdir, context  # silence pyflakes

# constants helpers and fixtures {{{1
@pytest.fixture(scope='function')
def claim_task():
    return {
        "credentials": {
            "task_credentials": True,
        },
        "task": {
            "task_defn": True,
        }
    }

@pytest.fixture(scope='function')
def reclaim_task():
    return {
        "credentials": {
            "reclaim_task_credentials": True,
        }
    }

def get_task_file(context):
    temp_dir = context.config['work_dir']
    path = os.path.join(temp_dir, "task.json")
    return path

def get_json(path):
    with open(path, "r") as fh:
        return json.load(fh)

# tests {{{1
def test_empty_context(context):
    assert context.task is None
    assert context.claim_task is None
    assert context.reclaim_task is None
    assert context.temp_credentials is None

@pytest.mark.asyncio
async def test_set_task(context, claim_task):
    context.claim_task = claim_task
    assert context.claim_task == claim_task
    assert context.reclaim_task is None
    assert context.temp_credentials == claim_task['credentials']
    assert get_json(get_task_file(context)) == claim_task['task']

@pytest.mark.asyncio
async def test_set_reclaim_task(context, claim_task, reclaim_task):
    context.claim_task = claim_task
    context.reclaim_task = reclaim_task
    assert context.claim_task == claim_task
    assert context.task == claim_task['task']
    assert context.reclaim_task == reclaim_task
    assert context.temp_credentials == reclaim_task['credentials']
    assert get_json(get_task_file(context)) == claim_task['task']

@pytest.mark.asyncio
async def test_set_reset_task(context, claim_task, reclaim_task):
    context.claim_task = claim_task
    context.reclaim_task = reclaim_task
    context.claim_task = None
    assert context.claim_task is None
    assert context.task is None
    assert context.reclaim_task is None
    assert context.proc is None
    assert context.temp_credentials is None
    assert context.temp_queue is None

def test_temp_queue(context, mocker):
    mocker.patch('taskcluster.aio.Queue')
    context.session = {'c': 'd'}
    context.temp_credentials = {'a': 'b'}
    assert taskcluster.aio.Queue.called_once_with({
        'credentials': context.temp_credentials,
    }, session=context.session)

@pytest.mark.asyncio
async def test_projects(context, mocker):
    fake_projects = {"mozilla-central": "blah", "count": 0}

    async def fake_load(*args):
        fake_projects['count'] += 1
        return deepcopy(fake_projects)

    mocker.patch.object(swcontext, "load_json_or_yaml_from_url", new=fake_load)
    assert context.projects is None
    await context.populate_projects()
    assert context.projects == fake_projects
    assert fake_projects['count'] == 1

    await context.populate_projects(force=True)
    assert context.projects == fake_projects
    assert fake_projects['count'] == 2

    await context.populate_projects()
    assert context.projects == fake_projects
    assert fake_projects['count'] == 2

def test_get_credentials(context):
    expected = {'asdf': 'foobar'}
    context._credentials = expected
    assert context.credentials == expected

def test_new_event_loop(mocker):
    """The default context.event_loop is from `asyncio.get_event_loop`"""
    fake_loop = mock.MagicMock()
    mocker.patch.object(asyncio, 'get_event_loop', return_value=fake_loop)
    context = swcontext.Context()
    assert context.event_loop is fake_loop

def test_set_event_loop(mocker):
    """`context.event_loop` returns the same value once set.

    (This may seem obvious, but this tests the correctness of the property.)

    """
    fake_loop = mock.MagicMock()
    context = swcontext.Context()
    context.event_loop = fake_loop
    assert context.event_loop is fake_loop
<|endoftext|>


<commit_before>'''
Script used to query Wikipedia for summary of object
'''

import sys
import wikipedia

def main():
	# Check that we have the right number of arguments
	if (len(sys.argv) != 2):
		print 'Incorrect number of arguments; please pass in only one string that contains the subject'
	 	return 'Banana'

	print wikipedia.summary(sys.argv[1]).encode('utf-8')
	#return wikipedia.summary(sys.argv[1])

if __name__ == '__main__':
	main()
<commit_msg>Send query from android device to server<commit_after>'''
Script used to query Wikipedia for summary of object
'''

import sys
import wikipedia
import nltk

def main():
	# Check that we have the right number of arguments
	if (len(sys.argv) != 2):
		print 'Incorrect number of arguments; please pass in only one string that contains the query'
	 	return 'Banana'

	# Get the noun from the query (uses the first noun it finds for now)
	print sys.argv[0]
	tokens = nltk.word_tokenize(sys.argv[1])
	tagged = nltk.pos_tag(tokens)

	# Find first noun in query and provide Wikipedia summary for it
	for tag in tagged:
		if tag[1][0] == 'N':
			print wikipedia.summary(tag[0]).encode('utf-8')
			return

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


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

import datetime
import calendar
import logging
import traceback

from third.sqlalchemy import create_engine
from third.sqlalchemy.orm import sessionmaker

from __reader import Reader
from __message import Message

class DBReader(Reader):
    """Conf:
        - period (required): period of reads in seconds
        - user (required): database username,
        - passwd (required): database password
        - host (required): database hostname
        - database (required): database name
        - connection (required): connection string,
            e.g. 'mysql+mysqldb://%s:%s@%s/%s' (see sqlalchemy doc)
        - query (required): sql statement,
            e.g. select name, description from user
        - columns (required): list of columns in the same order from 'query',
            e.g. [name, description]

        Note: if checkpoint is used, query must ensure sorted data"""

    def start_session(self):
        engine = create_engine(self.connection % (self.user,
                                                  self.passwd,
                                                  self.host,
                                                  self.database),
                                                  echo=False)
        Session = sessionmaker(bind=engine)
        self.session = Session()

    def close_session(self):
        self.session.close()

    def do_query(self):
        try:
            self.start_session()
            self.results = self.session.query(*self.columns).from_statement(self.query).all()
            self.close_session()
            return True
        except Exception, e:
            self.log.error("Error during query execution: %s" % self.query)
            self.log.error(traceback.format_exc())
            return False

    def store_results(self):
        messages = [Message(content={column: result[i] for i, column in enumerate(self.columns)})
                    for result in self.results]

        for message in messages: self.store(message)

    def setup(self):
        self.log = logging.getLogger()

        self.required_confs = ['columns', 'query', 'user',
                               'passwd', 'host', 'database']
        self.check_required_confs()

    def read(self):
        if self.period:
            self.do_query() and self.store_results()
            return

<commit_msg>minor refactoring<commit_after># -*- coding: utf-8 -*-

import datetime
import calendar
import logging
import traceback

from third.sqlalchemy import create_engine
from third.sqlalchemy.orm import sessionmaker

from __reader import Reader
from __message import Message

class DBReader(Reader):
    """Conf:
        - period (required): period of reads in seconds
        - user (required): database username,
        - passwd (required): database password
        - host (required): database hostname
        - database (required): database name
        - connection (required): connection string,
            e.g. 'mysql+mysqldb://%s:%s@%s/%s' (see sqlalchemy doc)
        - query (required): sql statement,
            e.g. select name, description from user
        - columns (required): list of columns in the same order from 'query',
            e.g. [name, description]

        Note: if checkpoint is used, query must ensure sorted data"""

    def start_session(self):
        engine = create_engine(self.connection % (self.user,
                                                  self.passwd,
                                                  self.host,
                                                  self.database),
                                                  echo=False)
        Session = sessionmaker(bind=engine)
        self.session = Session()

    def close_session(self):
        self.session.close()

    def do_query(self):
        try:
            self.start_session()
            self.results = self.session.query(*self.columns).from_statement(self.query).all()
            self.close_session()
            return True
        except Exception, e:
            self.log.error("Error during query execution: %s" % self.query)
            self.log.error(traceback.format_exc())
            return False

    def store_results(self):
        messages = [Message(content=dict(zip(self.columns, result))) for result in self.results]
        for message in messages: self.store(message)

    def setup(self):
        self.log = logging.getLogger()
        self.required_confs = ['columns', 'query', 'user',
                               'passwd', 'host', 'database']
        self.check_required_confs()

    def read(self):
        if self.period:
            self.do_query() and self.store_results()
            return

<|endoftext|>