<commit_before>import unittest
import numpy as np

from pgmpy.models import ClusterGraph
from pgmpy.tests import help_functions as hf
from pgmpy.factors import Factor
from pgmpy.extern.six.moves import range

class TestClusterGraphCreation(unittest.TestCase):
    def setUp(self):
        self.graph = ClusterGraph()

    def test_add_single_node(self):
        self.graph.add_node(('a', 'b'))
        self.assertListEqual(self.graph.nodes(), [('a', 'b')])

    def test_add_single_node_raises_error(self):
        self.assertRaises(TypeError, self.graph.add_node, 'a')

    def test_add_multiple_nodes(self):
        self.graph.add_nodes_from([('a', 'b'), ('b', 'c')])
        self.assertListEqual(hf.recursive_sorted(self.graph.nodes()), [['a', 'b'], ['b', 'c']])

    def test_add_single_edge(self):
        self.graph.add_edge(('a', 'b'), ('b', 'c'))
        self.assertListEqual(hf.recursive_sorted(self.graph.nodes()), [['a', 'b'], ['b', 'c']])
        self.assertListEqual(sorted([node for edge in self.graph.edges() for node in edge]),
                             [('a', 'b'), ('b', 'c')])

    def test_add_single_edge_raises_error(self):
        self.assertRaises(ValueError, self.graph.add_edge, ('a', 'b'), ('c', 'd'))

    def tearDown(self):
        del self.graph

class TestClusterGraphFactorOperations(unittest.TestCase):
    def setUp(self):
        self.graph = ClusterGraph()

    def test_add_single_factor(self):
        self.graph.add_node(('a', 'b'))
        phi1 = Factor(['a', 'b'], [2, 2], np.random.rand(4))
        self.graph.add_factors(phi1)
        self.assertListEqual(self.graph.get_factors(), [phi1])

    def test_add_single_factor_raises_error(self):
        self.graph.add_node(('a', 'b'))
        phi1 = Factor(['b', 'c'], [2, 2], np.random.rand(4))
        self.assertRaises(ValueError, self.graph.add_factors, phi1)

    def test_add_multiple_factors(self):
        self.graph.add_edges_from([[('a', 'b'), ('b', 'c')]])
        phi1 = Factor(['a', 'b'], [2, 2], np.random.rand(4))
        phi2 = Factor(['b', 'c'], [2, 2], np.random.rand(4))
        self.graph.add_factors(phi1, phi2)
        self.assertEqual(self.graph.get_factors(node=('b', 'a')), phi1)
        self.assertEqual(self.graph.get_factors(node=('b', 'c')), phi2)

    def test_remove_factors(self):
        self.graph.add_edges_from([[('a', 'b'), ('b', 'c')]])
        phi1 = Factor(['a', 'b'], [2, 2], np.random.rand(4))
        phi2 = Factor(['b', 'c'], [2, 2], np.random.rand(4))
        self.graph.add_factors(phi1, phi2)
        self.graph.remove_factors(phi1)
        self.assertListEqual(self.graph.get_factors(), [phi2])

    def test_get_partition_function(self):
        self.graph.add_edges_from([[('a', 'b'), ('b', 'c')]])
        phi1 = Factor(['a', 'b'], [2, 2], range(4))
        phi2 = Factor(['b', 'c'], [2, 2], range(4))
        self.graph.add_factors(phi1, phi2)
        self.assertEqual(self.graph.get_partition_function(), 22.0)

class TestClusterGraphMethods(unittest.TestCase):
    def setUp(self):
        self.graph = ClusterGraph()

    def test_get_cardinality(self):

        self.graph.add_edges_from([(('a', 'b', 'c'), ('a', 'b')),
                                   (('a', 'b', 'c'), ('a', 'c'))])

        self.assertDictEqual(self.graph.get_cardinality(), {})

        phi1 = Factor(['a', 'b', 'c'], [1, 2, 2], np.random.rand(4))
        self.graph.add_factors(phi1)
        self.assertDictEqual(self.graph.get_cardinality(), {'a': 1, 'b': 2, 'c':2})
        self.graph.remove_factors(phi1)
        self.assertDictEqual(self.graph.get_cardinality(), {})

        phi1 = Factor(['a', 'b'], [1, 2], np.random.rand(2))
        phi2 = Factor(['a', 'c'], [1, 2], np.random.rand(2))
        self.graph.add_factors(phi1, phi2)
        self.assertDictEqual(self.graph.get_cardinality(), {'a': 1, 'b': 2, 'c': 2})

        phi3 = Factor(['a', 'c'], [1, 1], np.random.rand(1))
        self.graph.add_factors(phi3)
        self.assertDictEqual(self.graph.get_cardinality(), {'c': 1, 'b': 2, 'a': 1})

        self.graph.remove_factors(phi1, phi2, phi3)
        self.assertDictEqual(self.graph.get_cardinality(), {})

    def tearDown(self):
        del self.graph
<commit_msg>added tests for check_model in ClusterGraph<commit_after>import unittest
import numpy as np

from pgmpy.models import ClusterGraph
from pgmpy.tests import help_functions as hf
from pgmpy.factors import Factor
from pgmpy.exceptions import CardinalityError
from pgmpy.extern.six.moves import range

class TestClusterGraphCreation(unittest.TestCase):
    def setUp(self):
        self.graph = ClusterGraph()

    def test_add_single_node(self):
        self.graph.add_node(('a', 'b'))
        self.assertListEqual(self.graph.nodes(), [('a', 'b')])

    def test_add_single_node_raises_error(self):
        self.assertRaises(TypeError, self.graph.add_node, 'a')

    def test_add_multiple_nodes(self):
        self.graph.add_nodes_from([('a', 'b'), ('b', 'c')])
        self.assertListEqual(hf.recursive_sorted(self.graph.nodes()), [['a', 'b'], ['b', 'c']])

    def test_add_single_edge(self):
        self.graph.add_edge(('a', 'b'), ('b', 'c'))
        self.assertListEqual(hf.recursive_sorted(self.graph.nodes()), [['a', 'b'], ['b', 'c']])
        self.assertListEqual(sorted([node for edge in self.graph.edges() for node in edge]),
                             [('a', 'b'), ('b', 'c')])

    def test_add_single_edge_raises_error(self):
        self.assertRaises(ValueError, self.graph.add_edge, ('a', 'b'), ('c', 'd'))

    def tearDown(self):
        del self.graph

class TestClusterGraphFactorOperations(unittest.TestCase):
    def setUp(self):
        self.graph = ClusterGraph()

    def test_add_single_factor(self):
        self.graph.add_node(('a', 'b'))
        phi1 = Factor(['a', 'b'], [2, 2], np.random.rand(4))
        self.graph.add_factors(phi1)
        self.assertListEqual(self.graph.get_factors(), [phi1])

    def test_add_single_factor_raises_error(self):
        self.graph.add_node(('a', 'b'))
        phi1 = Factor(['b', 'c'], [2, 2], np.random.rand(4))
        self.assertRaises(ValueError, self.graph.add_factors, phi1)

    def test_add_multiple_factors(self):
        self.graph.add_edges_from([[('a', 'b'), ('b', 'c')]])
        phi1 = Factor(['a', 'b'], [2, 2], np.random.rand(4))
        phi2 = Factor(['b', 'c'], [2, 2], np.random.rand(4))
        self.graph.add_factors(phi1, phi2)
        self.assertEqual(self.graph.get_factors(node=('b', 'a')), phi1)
        self.assertEqual(self.graph.get_factors(node=('b', 'c')), phi2)

    def test_remove_factors(self):
        self.graph.add_edges_from([[('a', 'b'), ('b', 'c')]])
        phi1 = Factor(['a', 'b'], [2, 2], np.random.rand(4))
        phi2 = Factor(['b', 'c'], [2, 2], np.random.rand(4))
        self.graph.add_factors(phi1, phi2)
        self.graph.remove_factors(phi1)
        self.assertListEqual(self.graph.get_factors(), [phi2])

    def test_get_partition_function(self):
        self.graph.add_edges_from([[('a', 'b'), ('b', 'c')]])
        phi1 = Factor(['a', 'b'], [2, 2], range(4))
        phi2 = Factor(['b', 'c'], [2, 2], range(4))
        self.graph.add_factors(phi1, phi2)
        self.assertEqual(self.graph.get_partition_function(), 22.0)

class TestClusterGraphMethods(unittest.TestCase):
    def setUp(self):
        self.graph = ClusterGraph()

    def test_get_cardinality(self):

        self.graph.add_edges_from([(('a', 'b', 'c'), ('a', 'b')),
                                   (('a', 'b', 'c'), ('a', 'c'))])

        self.assertDictEqual(self.graph.get_cardinality(), {})

        phi1 = Factor(['a', 'b', 'c'], [1, 2, 2], np.random.rand(4))
        self.graph.add_factors(phi1)
        self.assertDictEqual(self.graph.get_cardinality(), {'a': 1, 'b': 2, 'c':2})
        self.graph.remove_factors(phi1)
        self.assertDictEqual(self.graph.get_cardinality(), {})

        phi1 = Factor(['a', 'b'], [1, 2], np.random.rand(2))
        phi2 = Factor(['a', 'c'], [1, 2], np.random.rand(2))
        self.graph.add_factors(phi1, phi2)
        self.assertDictEqual(self.graph.get_cardinality(), {'a': 1, 'b': 2, 'c': 2})

        phi3 = Factor(['a', 'c'], [1, 1], np.random.rand(1))
        self.graph.add_factors(phi3)
        self.assertDictEqual(self.graph.get_cardinality(), {'c': 1, 'b': 2, 'a': 1})

        self.graph.remove_factors(phi1, phi2, phi3)
        self.assertDictEqual(self.graph.get_cardinality(), {})

    def test_check_model(self):
        self.graph.add_edges_from([(('a', 'b'), ('a', 'c'))])
        phi1 = Factor(['a', 'b'], [1, 2], np.random.rand(2))
        phi2 = Factor(['a', 'c'], [1, 2], np.random.rand(2))
        self.graph.add_factors(phi1, phi2)
        self.assertTrue(self.graph.check_model())

        self.graph.remove_factors(phi2)
        phi2 = Factor(['a', 'c'], [1, 2], np.random.rand(2))
        self.graph.add_factors(phi2)
        self.assertTrue(self.graph.check_model())

    def test_check_model1(self):
        self.graph.add_edges_from([(('a', 'b'), ('a', 'c')),
                                   (('a', 'c'), ('a', 'd'))])
        phi1 = Factor(['a', 'b'], [1, 2], np.random.rand(2))
        self.graph.add_factors(phi1)
        self.assertRaises(ValueError, self.graph.check_model)
        phi2 = Factor(['a', 'c'], [1, 2], np.random.rand(2))
        self.graph.add_factors(phi2)
        self.assertRaises(ValueError, self.graph.check_model)

    def test_check_model2(self):
        self.graph.add_edges_from([(('a', 'b'), ('a', 'c')),
                                   (('a', 'c'), ('a', 'd'))])

        phi1 = Factor(['a', 'b'], [1, 2], np.random.rand(2))
        phi2 = Factor(['a', 'c'], [3, 3], np.random.rand(9))
        phi3 = Factor(['a', 'd'], [4, 4], np.random.rand(16))
        self.graph.add_factors(phi1, phi2, phi3)
        self.assertRaises(CardinalityError, self.graph.check_model)
        self.graph.remove_factors(phi2)

        phi2 = Factor(['a', 'c'], [1, 3], np.random.rand(3))
        self.graph.add_factors(phi2)
        self.assertRaises(CardinalityError, self.graph.check_model)
        self.graph.remove_factors(phi3)

        phi3 = Factor(['a', 'd'], [1, 4], np.random.rand(4))
        self.graph.add_factors(phi3)
        self.assertTrue(self.graph.check_model())

    def tearDown(self):
        del self.graph
<|endoftext|>


<commit_before>from django.shortcuts import render, redirect
from django.db.models import Q
import re

from retailers.models import Retailer

def home(request):
    all = Retailer.objects.all()
    matches = all
    showing_all = True
    query = ""

    if 'query' in request.GET.keys():
        showing_all = False
        query = request.GET['query']
        split = re.findall(r'[,\w]+', query)
        array = []
        for s in split:
            s = s.replace(',', '').strip()
            if len(s) > 2 and not s.isdigit():
                array.append(s)

        if array and len(array) > 1:
            regex = '^.*(%s).*$' % '|'.join(array)

            matches = Retailer.objects.filter(Q(partner__name__contains=query)
                                          | Q(address__contains=query)
                                          | Q(city__contains=query)
                                          | Q(phone__contains=query)
                                          | Q(state__contains=query)
                                          | Q(zip_code__contains=query)
                                          | Q(partner__name__iregex=regex)
                                          | Q(address__iregex=regex)
                                          | Q(city__iregex=regex)
                                          | Q(phone__iregex=regex)
                                          | Q(state__iregex=regex)
                                          | Q(zip_code__iregex=regex)
                                          )
        else:
            matches = Retailer.objects.filter(Q(partner__name__contains=query)
                                          | Q(address__contains=query)
                                          | Q(city__contains=query)
                                          | Q(phone__contains=query)
                                          | Q(state__contains=query)
                                          | Q(zip_code__contains=query)
                                          )

    return render(request, 'retailers/home.html', {
        'matches': matches,
        'retailers': matches,
        'search_query': query,
        'showing_all': showing_all
    })
<commit_msg>Only do the more greedy search when there are no matches<commit_after>from django.shortcuts import render, redirect
from django.db.models import Q
import re

from retailers.models import Retailer

def home(request):
    all = Retailer.objects.all()
    matches = all
    showing_all = True
    query = ""

    if 'query' in request.GET.keys():
        showing_all = False
        query = request.GET['query']
        split = query.split(',')
        array = []
        for s in split:
            s = s.strip()
            if len(s) > 2 and not s.isdigit():
                array.append(s)

        if array and len(array) > 1:
            regex = '^.*(%s).*$' % '|'.join(array)

            matches = Retailer.objects.filter(Q(partner__name__contains=query)
                                          | Q(address__contains=query)
                                          | Q(city__contains=query)
                                          | Q(phone__contains=query)
                                          | Q(state__contains=query)
                                          | Q(zip_code__contains=query)
                                          | Q(partner__name__iregex=regex)
                                          | Q(address__iregex=regex)
                                          | Q(city__iregex=regex)
                                          | Q(phone__iregex=regex)
                                          | Q(state__iregex=regex)
                                          | Q(zip_code__iregex=regex)
                                          )
        else:
            matches = Retailer.objects.filter(Q(partner__name__contains=query)
                                          | Q(address__contains=query)
                                          | Q(city__contains=query)
                                          | Q(phone__contains=query)
                                          | Q(state__contains=query)
                                          | Q(zip_code__contains=query)
                                          )

        # If it is the case there are no matches we try to do a more greedy search by splitting the query even more
        if len(matches) < 1:
            split = re.findall(r'[,\w]+', query)
            array = []
            for s in split:
                s = s.replace(',', '').strip()
                if len(s) > 2 and not s.isdigit():
                    array.append(s)
            if array and len(array) > 1:
                regex = '^.*(%s).*$' % '|'.join(array)
                matches = Retailer.objects.filter(Q(partner__name__iregex=regex)
                                          | Q(address__iregex=regex)
                                          | Q(city__iregex=regex)
                                          | Q(phone__iregex=regex)
                                          | Q(state__iregex=regex)
                                          | Q(zip_code__iregex=regex)
                                          )

    return render(request, 'retailers/home.html', {
        'matches': matches,
        'retailers': matches,
        'search_query': query,
        'showing_all': showing_all
    })
<|endoftext|>


<commit_before>#!/usr/bin/env python
#
# (C) 2005 British Broadcasting Corporation and Kamaelia Contributors(1)
#     All Rights Reserved.
#
# You may only modify and redistribute this under the terms of any of the
# following licenses(2): Mozilla Public License, V1.1, GNU General
# Public License, V2.0, GNU Lesser General Public License, V2.1
#
# (1) Kamaelia Contributors are listed in the AUTHORS file and at
#     http://kamaelia.sourceforge.net/AUTHORS - please extend this file,
#     not this notice.
# (2) Reproduced in the COPYING file, and at:
#     http://kamaelia.sourceforge.net/COPYING
# Under section 3.5 of the MPL, we are using this text since we deem the MPL
# notice inappropriate for this file. As per MPL/GPL/LGPL removal of this
# notice is prohibited.
#
# Please contact us via: kamaelia-list-owner@lists.sourceforge.net
# to discuss alternative licensing.
# -------------------------------------------------------------------------
"""\
=====================
General 3D Object
=====================
TODO
"""

import pygame
from pygame.locals import *
from OpenGL.GL import *
from OpenGL.GLU import *
from Display3D import Display3D
from Util3D import *
import Axon

class Object3D(Axon.Component.component):
    Inboxes = {
       "inbox": "not used",
       "control": "ignored",
    }

    Outboxes = {
        "outbox": "used for sending position information",
        "display_signal" : "Outbox used for communicating to the display surface"
    }

    def __init__(self, **argd):
        super(Object3D, self).__init__()
        # not sure about the needed data yet, just for testing
#       self.rotspeed = Vector(0.1, 0.0, 0.0)
        self.size = argd.get("size", Vector(2,2,2))
        self.pos = argd.get("pos",Vector(0,0,-15))
        self.rot = Vector(0.0,0.0,0.0)
        self.transform = Transform()

        self.name = argd.get("name", "nameless")
        self.grabbed = 0
        self.oldrot = self.rot.copy()
        self.oldpos = self.pos.copy()

        # prepare vertices for intersection test
        x = float(self.size.x/2)
        y = float(self.size.y/2)
        z = float(self.size.z/2)
        self.vertices = [ Vector(x, 0.0, 0.0), Vector(0.0, y, 0.0), Vector(0.0, 0.0, z) ]

        # similar to Pygame component registration
        self.disprequest = { "3DDISPLAYREQUEST" : True,
#                                          "callback" : (self,"callback"),
                                          "events" : (self, "inbox"),
#                                          "size": self.size,
#                                          "pos": self.pos,
                                          "object": self }

    # Ray intersection test
    # returns the distance of the origin o to the point of intersection
    # if no intersection occurs, 0 is returned
    # Algorithm from "Realtime Rendering"
    def intersectRay(self, o, d):
        transformed = [self.transform.transformVector(v) for v in self.vertices]
        tmin = -10000
        tmax = 10000

        p = self.pos-o
        halfwidths = [self.size.x/2, self.size.y/2, self.size.z/2]
        for i in range(3):
            a = transformed[i]-p
            h = halfwidths[i]
            e = a.dot(p)
            f = a.dot(d)
            if abs(f)>0.0001:
                t1 = (e+h)/f
                t2 = (e-h)/f
                if t1 > t2:
                    x = t1
                    t1 = t2
                    t2 = x
                if t1 > tmin: tmin = t1
                if t2 < tmax: tmax = t2
                if tmin > tmax: return 0
                if tmax < 0: return 0
            elif -e-h > 0 or -e+h < 0: return 0
        if tmin > 0: return tmin
        else: return tmax

    def applyTransforms(self):
        # generate transformation matrix
        self.transform.reset()
        self.transform.applyRotation(self.rot)
        self.transform.applyTranslation(self.pos)

    def draw(self):
        glMatrixMode(GL_MODELVIEW)

        # set generated matrix
        glPushMatrix()
        glLoadMatrixf(self.transform.getMatrix())

        # draw faces
        glBegin(GL_QUADS)
        glColor3f(1.0,0.0,0.0)
        glVertex3f(1.0,1.0,1.0)
        glVertex3f(1.0,-1.0,1.0)
        glVertex3f(-1.0,-1.0,1.0)
        glVertex3f(-1.0,1.0,1.0)

        glColor3f(0.0,1.0,0.0)
        glVertex3f(1.0,1.0,-1.0)
        glVertex3f(1.0,-1.0,-1.0)
        glVertex3f(-1.0,-1.0,-1.0)
        glVertex3f(-1.0,1.0,-1.0)

        glColor3f(0.0,0.0,1.0)
        glVertex3f(1.0,1.0,1.0)
        glVertex3f(1.0,-1.0,1.0)
        glVertex3f(1.0,-1.0,-1.0)
        glVertex3f(1.0,1.0,-1.0)

        glColor3f(1.0,0.0,1.0)
        glVertex3f(-1.0,1.0,1.0)
        glVertex3f(-1.0,-1.0,1.0)
        glVertex3f(-1.0,-1.0,-1.0)
        glVertex3f(-1.0,1.0,-1.0)

        glColor3f(0.0,1.0,1.0)
        glVertex3f(1.0,1.0,1.0)
        glVertex3f(-1.0,1.0,1.0)
        glVertex3f(-1.0,1.0,-1.0)
        glVertex3f(1.0,1.0,-1.0)

        glColor3f(1.0,1.0,0.0)
        glVertex3f(1.0,-1.0,1.0)
        glVertex3f(-1.0,-1.0,1.0)
        glVertex3f(-1.0,-1.0,-1.0)
        glVertex3f(1.0,-1.0,-1.0)
        glEnd()

        glPopMatrix()

    def handleEvents(self):
        while self.dataReady("inbox"):
            for event in self.recv("inbox"):
                if event.type == pygame.MOUSEBUTTONDOWN:
                    if event.button in [1,3] and self.intersectRay(Vector(0,0,0), event.dir) > 0:
                        self.grabbed = event.button
                    if event.button == 4 and self.intersectRay(Vector(0,0,0), event.dir) > 0:
                        self.pos.z -= 1
                    if event.button == 5 and self.intersectRay(Vector(0,0,0), event.dir) > 0:
                        self.pos.z += 1
                if event.type == pygame.MOUSEBUTTONUP:
                    if event.button in [1,3]:
                        self.grabbed = 0
                if event.type == pygame.MOUSEMOTION:
                    if self.grabbed == 1:
                        self.rot.y += float(event.rel[0])
                        self.rot.x += float(event.rel[1])
                        self.rot %= 360
                    if self.grabbed == 3:
                        self.pos.x += float(event.rel[0])/10.0
                        self.pos.y -= float(event.rel[1])/10.0

        if self.oldpos != self.pos:
            diff = self.pos-self.oldpos
            self.send("%s was moved by %s units\n" % (self.name, str(diff)) , "outbox",)
            self.oldpos = self.pos.copy()

        if self.oldrot != self.rot:
            diff = self.rot-self.oldrot
            self.send("%s was rotated by (%d, %d) degrees\n" % (self.name, diff.x, diff.y), "outbox",)
            self.oldrot = self.rot.copy()

    def main(self):
        displayservice = Display3D.getDisplayService()
        self.link((self,"display_signal"), displayservice)
        self.send(self.disprequest, "display_signal");

        while 1:

#            for _ in self.waitBox("callback"): yield 1
#            self.display = self.recv("callback")

# There is no need for a callback yet

            yield 1

            self.handleEvents()
            self.applyTransforms()
            self.draw()

# Later it might be a good idea to provide a set of drawing functions
# so the component developer does not need to know about opengl
# This way opengl could later easily be replaced by an other mechanism
# for drawing
# e.g. TOGRA

if __name__=='__main__':
    from Kamaelia.Util.ConsoleEcho import consoleEchoer
    from Kamaelia.Util.Graphline import Graphline

    Graphline(
        CUBEC = Object3D(pos=Vector(0, 0,-12), name="Center cube"),
        CUBET = Object3D(pos=Vector(0,4,-20), name="Top cube"),
        CUBER = Object3D(pos=Vector(4,0,-22), name="Right cube"),
        CUBEB = Object3D(pos=Vector(0,-4,-18), name="Bottom cube"),
        CUBEL = Object3D(pos=Vector(-4, 0,-15), name="Left cube"),
        ECHO = consoleEchoer(),
        linkages = {
            ("CUBEC", "outbox") : ("ECHO", "inbox"),
            ("CUBET", "outbox") : ("ECHO", "inbox"),
            ("CUBER", "outbox") : ("ECHO", "inbox"),
            ("CUBEB", "outbox") : ("ECHO", "inbox"),
            ("CUBEL", "outbox") : ("ECHO", "inbox"),
        } ).run()

    Axon.Scheduler.scheduler.run.runThreads()
<commit_msg>Position and rotation can now be controlled using 3dcontrol mailbox. Example updated.<commit_after>#!/usr/bin/env python
#
# (C) 2005 British Broadcasting Corporation and Kamaelia Contributors(1)
#     All Rights Reserved.
#
# You may only modify and redistribute this under the terms of any of the
# following licenses(2): Mozilla Public License, V1.1, GNU General
# Public License, V2.0, GNU Lesser General Public License, V2.1
#
# (1) Kamaelia Contributors are listed in the AUTHORS file and at
#     http://kamaelia.sourceforge.net/AUTHORS - please extend this file,
#     not this notice.
# (2) Reproduced in the COPYING file, and at:
#     http://kamaelia.sourceforge.net/COPYING
# Under section 3.5 of the MPL, we are using this text since we deem the MPL
# notice inappropriate for this file. As per MPL/GPL/LGPL removal of this
# notice is prohibited.
#
# Please contact us via: kamaelia-list-owner@lists.sourceforge.net
# to discuss alternative licensing.
# -------------------------------------------------------------------------
"""\
=====================
General 3D Object
=====================
TODO
"""

import pygame
from pygame.locals import *
from OpenGL.GL import *
from OpenGL.GLU import *
from Display3D import Display3D
from Util3D import *
import Axon

class Object3D(Axon.Component.component):
    Inboxes = {
       "inbox": "not used",
       "control": "ignored",
       "3dcontrol":"receive 3d movement commands here (rel_)?[position | rotation]",
    }

    Outboxes = {
        "outbox": "used for sending position information",
        "display_signal" : "Outbox used for communicating to the display surface"
    }

    def __init__(self, **argd):
        super(Object3D, self).__init__()
        # not sure about the needed data yet, just for testing
#       self.rotspeed = Vector(0.1, 0.0, 0.0)
        self.size = argd.get("size", Vector(2,2,2))
        self.pos = argd.get("pos",Vector(0,0,-15))
        self.rot = Vector(0.0,0.0,0.0)
        self.transform = Transform()

        self.name = argd.get("name", "nameless")
        self.grabbed = 0
        self.oldrot = self.rot.copy()
        self.oldpos = self.pos.copy()

        # prepare vertices for intersection test
        x = float(self.size.x/2)
        y = float(self.size.y/2)
        z = float(self.size.z/2)
        self.vertices = [ Vector(x, 0.0, 0.0), Vector(0.0, y, 0.0), Vector(0.0, 0.0, z) ]

        # similar to Pygame component registration
        self.disprequest = { "3DDISPLAYREQUEST" : True,
#                                          "callback" : (self,"callback"),
                                          "events" : (self, "inbox"),
#                                          "size": self.size,
#                                          "pos": self.pos,
                                          "object": self }

    # Ray intersection test
    # returns the distance of the origin o to the point of intersection
    # if no intersection occurs, 0 is returned
    # Algorithm from "Realtime Rendering"
    def intersectRay(self, o, d):
        transformed = [self.transform.transformVector(v) for v in self.vertices]
        tmin = -10000
        tmax = 10000

        p = self.pos-o
        halfwidths = [self.size.x/2, self.size.y/2, self.size.z/2]
        for i in range(3):
            a = transformed[i]-p
            h = halfwidths[i]
            e = a.dot(p)
            f = a.dot(d)
            if abs(f)>0.0001:
                t1 = (e+h)/f
                t2 = (e-h)/f
                if t1 > t2:
                    x = t1
                    t1 = t2
                    t2 = x
                if t1 > tmin: tmin = t1
                if t2 < tmax: tmax = t2
                if tmin > tmax: return 0
                if tmax < 0: return 0
            elif -e-h > 0 or -e+h < 0: return 0
        if tmin > 0: return tmin
        else: return tmax

    def applyTransforms(self):
        # generate transformation matrix
        self.transform.reset()
        self.transform.applyRotation(self.rot)
        self.transform.applyTranslation(self.pos)

    def draw(self):
        glMatrixMode(GL_MODELVIEW)

        # set generated matrix
        glPushMatrix()
        glLoadMatrixf(self.transform.getMatrix())

        # draw faces
        glBegin(GL_QUADS)
        glColor4f(1.0,0.0,0.0,0.5)
        glVertex3f(1.0,1.0,1.0)
        glVertex3f(-1.0,1.0,1.0)
        glVertex3f(-1.0,-1.0,1.0)
        glVertex3f(1.0,-1.0,1.0)

        glColor4f(0.0,1.0,0.0, 0.5)
        glVertex3f(1.0,1.0,-1.0)
        glVertex3f(1.0,-1.0,-1.0)
        glVertex3f(-1.0,-1.0,-1.0)
        glVertex3f(-1.0,1.0,-1.0)

        glColor4f(0.0,0.0,1.0, 0.5)
        glVertex3f(1.0,1.0,1.0)
        glVertex3f(1.0,-1.0,1.0)
        glVertex3f(1.0,-1.0,-1.0)
        glVertex3f(1.0,1.0,-1.0)

        glColor4f(1.0,0.0,1.0, 0.5)
        glVertex3f(-1.0,1.0,1.0)
        glVertex3f(-1.0,-1.0,1.0)
        glVertex3f(-1.0,-1.0,-1.0)
        glVertex3f(-1.0,1.0,-1.0)

        glColor4f(0.0,1.0,1.0, 0.5)
        glVertex3f(1.0,1.0,1.0)
        glVertex3f(-1.0,1.0,1.0)
        glVertex3f(-1.0,1.0,-1.0)
        glVertex3f(1.0,1.0,-1.0)

        glColor4f(1.0,1.0,0.0, 0.5)
        glVertex3f(1.0,-1.0,1.0)
        glVertex3f(-1.0,-1.0,1.0)
        glVertex3f(-1.0,-1.0,-1.0)
        glVertex3f(1.0,-1.0,-1.0)
        glEnd()

        glPopMatrix()

    def handleEvents(self):
        while self.dataReady("inbox"):
            for event in self.recv("inbox"):
                if event.type == pygame.MOUSEBUTTONDOWN:
                    if event.button in [1,3] and self.intersectRay(Vector(0,0,0), event.dir) > 0:
                        self.grabbed = event.button
                    if event.button == 4 and self.intersectRay(Vector(0,0,0), event.dir) > 0:
                        self.pos.z -= 1
                    if event.button == 5 and self.intersectRay(Vector(0,0,0), event.dir) > 0:
                        self.pos.z += 1
                if event.type == pygame.MOUSEBUTTONUP:
                    if event.button in [1,3]:
                        self.grabbed = 0
                if event.type == pygame.MOUSEMOTION:
                    if self.grabbed == 1:
                        self.rot.y += float(event.rel[0])
                        self.rot.x += float(event.rel[1])
                        self.rot %= 360
                    if self.grabbed == 3:
                        self.pos.x += float(event.rel[0])/10.0
                        self.pos.y -= float(event.rel[1])/10.0

        if self.oldpos != self.pos:
            diff = self.pos-self.oldpos
            self.send("%s was moved by %s units\n" % (self.name, str(diff)) , "outbox",)
            self.oldpos = self.pos.copy()

        if self.oldrot != self.rot:
            diff = self.rot-self.oldrot
            self.send("%s was rotated by (%1.2f, %1.2f) degrees\n" % (self.name, diff.x, diff.y), "outbox",)
            self.oldrot = self.rot.copy()

    def handleMovementCommands(self):
        while self.dataReady("3dcontrol"):
            cmd = self.recv("3dcontrol")
            if cmd.type == "rotation":
                self.rot = cmd.value
            if cmd.type == "postition":
                self.pos = cmd.value
            if cmd.type == "rel_rotation":
                self.rot = (self.rot+cmd.value)%360
            if cmd.type == "rel_position":
                self.pos += cmd.value

    def main(self):
        displayservice = Display3D.getDisplayService()
        self.link((self,"display_signal"), displayservice)
        self.send(self.disprequest, "display_signal");

        while 1:

#            for _ in self.waitBox("callback"): yield 1
#            self.display = self.recv("callback")

# There is no need for a callback yet

            yield 1

            self.handleEvents()
            self.handleMovementCommands()
            self.applyTransforms()
            self.draw()

# Later it might be a good idea to provide a set of drawing functions
# so the component developer does not need to know about opengl
# This way opengl could later easily be replaced by an other mechanism
# for drawing
# e.g. TOGRA

if __name__=='__main__':
    class Bunch: pass

    class CubeRotator(Axon.Component.component):
        def main(self):
            while 1:
                yield 1
                cmd = Bunch()
                cmd.type = "rel_rotation"
                cmd.value = Vector(0.1, 0.1, 0.0)
                self.send(cmd, "outbox")

    from Kamaelia.Util.ConsoleEcho import consoleEchoer
    from Kamaelia.Util.Graphline import Graphline

    Graphline(
        CUBEC = Object3D(pos=Vector(0, 0,-12), name="Center cube"),
        CUBET = Object3D(pos=Vector(0,4,-20), name="Top cube"),
        CUBER = Object3D(pos=Vector(4,0,-22), name="Right cube"),
        CUBEB = Object3D(pos=Vector(0,-4,-18), name="Bottom cube"),
        CUBEL = Object3D(pos=Vector(-4, 0,-15), name="Left cube"),
        ROTATOR = CubeRotator(),
        ECHO = consoleEchoer(),
        linkages = {
            ("CUBEC", "outbox") : ("ECHO", "inbox"),
            ("CUBET", "outbox") : ("ECHO", "inbox"),
            ("CUBER", "outbox") : ("ECHO", "inbox"),
            ("CUBEB", "outbox") : ("ECHO", "inbox"),
            ("CUBEL", "outbox") : ("ECHO", "inbox"),
            ("ROTATOR", "outbox") : ("CUBEC", "3dcontrol"),
        } ).run()

    Axon.Scheduler.scheduler.run.runThreads()
<|endoftext|>


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

"""
 List expired users that have expired or will expire soon, according to the expiry date written in their notes field in PaperCut.
 ...

 USAGE: listExpiredUsers.py [how_soon]
 It will list and identify users that have expired or will expire within the range of days specified.

 In PaperCut admin, please use the user's notes field to add an expiry date in the following format:
 expiry:yyyy-mm-dd
 e.g.
 expiry:2019-07-07
 ...

 PARAM: how_soon (integer)
 how_soon is the N number of days in the future.
 e.g. if today is 2019-07-07 and how_soon is 10, then it will look for records between today and 2019-07-17

 if parameter is not supplied it's defaulted to 0 and return users with expiry date in the past.

"""

import xmlrpc.client
from ssl import create_default_context, Purpose
import sys
import re
import datetime
from datetime import date, timedelta

host="https://localhost:9192/rpc/api/xmlrpc" # If not localhost then the client address will need to be whitelisted in PaperCut

auth_token="testing"  # Value defined in advanced config property "auth.webservices.auth-token". Should be random

proxy = xmlrpc.client.ServerProxy(host, verbose=False,
      context = create_default_context(Purpose.CLIENT_AUTH))

# listUsers method takes in optional n day range
def list_users(how_soon):
    offset = 0
    limit = 100

    counter = 0
    unknown_list = []

    now = datetime.date.today()

    to_date = now + timedelta(days=how_soon)

    print("List of expiry dates until " +  to_date.strftime("%Y-%m-%d")+ ":")

    while True:
        try:
            #calls listUserAccount method of the API
            #return list of users
            user_list = proxy.api.listUserAccounts(auth_token, offset,limit)
        except xmlrpc.client.Fault as error:
            print("\ncalled listUserAccounts(). Return fault is {}".format(error.faultString))
            sys.exit(1)
        except xmlrpc.client.ProtocolError as error:
            print("\nA protocol error occurred\nURL: {}\nHTTP/HTTPS headers: {}\nError code: {}\nError message: {}".format(
                error.url, error.headers, error.errcode, error.errmsg))
            sys.exit(1)

        #return every user in the list
        for user in user_list:
            try:
                notes = proxy.api.getUserProperty(auth_token,user, "notes")
                match = re.search(r'expiry:\d{4}-\d{2}-\d{2}', notes.replace(" " , ""))
                if match is None :
                    unknown_list.append(user)
                    continue

                cleaned_match = match.group().strip("expiry:")
                date = datetime.datetime.strptime(cleaned_match, '%Y-%m-%d').date()
                status = ""

                if date < now:
                    status = "expired"
                    counter +=1
                    #HERE you could add user to delete list, or perform other action
                elif date == now :
                    status = "expires today"
                    counter +=1
                elif date <= to_date:
                    delta = date - now
                    status = "expiring in " + str(delta.days) + " day(s)"

                # print output in pretty table
                if status != "":
                    print ("{:25s} {:13s} {:s}".format(user, str(date), status))

            except xmlrpc.client.Fault as error:
                print("\ncalled getUserProperty(). Return fault is {}".format(error.faultString))
                sys.exit(1)
            except xmlrpc.client.ProtocolError as error:
                print("\nA protocol error occurred\nURL: {}\nHTTP/HTTPS headers: {}\nError code: {}\nError message: {}".format(
                    error.url, error.headers, error.errcode, error.errmsg))
                sys.exit(1)
            except ValueError as error:
                print("Invalid date format or value in notes field. It must contain the word 'expiry:' followed by a date in yyyy-mm-dd format")

        if limit == 0 or len(user_list) < limit:
            break # We have reached the end

        offset += limit # We need to next slice of usersi

    #count how many users are expired until today including today
    plural =""
    are = "is"
    if counter>1:
        plural = "s"
        are = "are"
    print("\nThere " + are + " " + str(counter) +" expired user"+plural)

    print("\nUser(s) without any expiry date:")
    for user in unknown_list:
        print (user)

if __name__=="__main__":

    if len(sys.argv) == 1: #no argument, expired today and in the past
        list_users(0)
    elif len(sys.argv) == 2:
        try:
            offset_days = int(sys.argv[1])
            list_users(offset_days)
        except ValueError:
            print("Invalid number")
            sys.exit(1)
    else:
        print("Usage: ./listExpiredUsers.py [how_soon] or leave it blank to return all past record(s)")

<commit_msg>Added some refinements after internal_review<commit_after>#!/usr/bin/env python3
"""
 List expired users that have expired or will expire soon, according to the expiry date written in their notes field in PaperCut.

 USAGE: listExpiredUsers.py [how_soon]
 It will list and identify users that have expired or will expire within the next "n" days

 In PaperCut admin, please use the user's notes field to add an expiry date in the following format:
 expiry:yyyy-mm-dd
 e.g.
 expiry:2019-07-07

 Users with no "expiry value in the notes field will assume to never expire.
 ...

 PARAM: how_soon (integer)

 Will list users who have already expired, or will expire in the next "how_soon" days
"""

from  xmlrpc.client import ServerProxy, Fault
from ssl import create_default_context, Purpose
from sys import exit, argv
from re import compile
from datetime import date, timedelta, datetime

host="https://localhost:9192/rpc/api/xmlrpc" # If not localhost then the client address will need to be whitelisted in PaperCut

auth_token="token"  # Value defined in advanced config property "auth.webservices.auth-token". Should be random

proxy = ServerProxy(host, verbose=False,
      context = create_default_context(Purpose.CLIENT_AUTH))

expireRE=compile(r'expiry:\d{4}-\d{2}-\d{2}')

# listUsers method takes in optional n day range
def list_users(how_soon):
    offset = 0
    limit = 100

    counter = 0
    unknown_list = []

    today = date.today()

    check_date = today + timedelta(days=how_soon)

    print(f'List of expired users who have or will expire by {check_date.strftime("%Y-%m-%d")}:')

    while True:
        try:
            #calls listUserAccount method of the API
            #return list of users
            user_list = proxy.api.listUserAccounts(auth_token, offset,limit)
        except xmlrpc.client.Fault as error:
            print("\ncalled listUserAccounts(). Return fault is {}".format(error.faultString))
            exit(1)
        except xmlrpc.client.ProtocolError as error:
            print("\nA protocol error occurred\nURL: {}\nHTTP/HTTPS headers: {}\nError code: {}\nError message: {}".format(
                error.url, error.headers, error.errcode, error.errmsg))
            exit(1)

        #return every user in the list
        for user in user_list:
            try:
                notes = proxy.api.getUserProperty(auth_token,user, "notes")
            except xmlrpc.client.Fault as error:
                print("\ncalled getUserProperty(). Return fault is {}".format(error.faultString))
                exit(1)
            except xmlrpc.client.ProtocolError as error:
                print("\nA protocol error occurred\nURL: {}\nHTTP/HTTPS headers: {}\nError code: {}\nError message: {}".format(
                    error.url, error.headers, error.errcode, error.errmsg))
                exit(1)

            matchedNote = expireRE.search(notes)

            if matchedNote is None :
                # User has no expiry date -- no action required
                continue

            cleaned_match = matchedNote.group().strip("expiry:")

            expirtyDate = datetime.strptime(cleaned_match, '%Y-%m-%d').date()

            status = ""

            if expirtyDate < check_date:
                status = "expired"
                counter += 1
                if expirtyDate > today:
                    print (f"{user} will expire on {expirtyDate}")
                else:
                    print (f"{user} has expired {expirtyDate}")

                #HERE you could add user to delete list, or perform other action

        if limit == 0 or len(user_list) < limit:
            break # We have reached the end

        offset += limit # We need to next slice of users

    if counter == 0:
        print(f"\nThere are no expiring users")
    elif counter>1:
        print(f"\nThere are {counter} expiring users")
    else:
        print(f"\nThere is one expiring user")

if __name__=="__main__":

    if len(argv) == 1: #no argument, expired today and in the past
        list_users(0)
    elif len(argv) == 2:
        try:
            offset_days = int(argv[1])
            list_users(offset_days)
        except ValueError:
            print("Usage: ./listExpiredUsers.py [how_soon] or leave it blank to return all past record(s)")
    else:
        print("Usage: ./listExpiredUsers.py [how_soon] or leave it blank to return all past record(s)")
<|endoftext|>


<commit_before>from aquests.lib.producers import simple_producer
from aquests.lib.athreads import trigger
from skitai.server.http_response import catch

class _Method:
	def __init__(self, send, name):
		self.__send = send
		self.__name = name

	def __getattr__(self, name):
		return _Method(self.__send, "%s.%s" % (self.__name, name))

	def __call__(self, *args, **karg):
		return self.__send(self.__name, args, karg)

class Promise (simple_producer):
	def __init__ (self, was, handler, **karg):
		self.__was = was
		self.was = was._clone ()
		self.handler = handler

		self._data = []
		self._parts = karg
		self._numreq = 0
		self._numres = 0
		self._done = False
		self._rejected = 0

	def __getattr__ (self, name):
		if name.split (".")[0] not in self.__was.VALID_COMMANDS:
			raise AttributeError ('%s is not member' % name)
		self._numreq += 1
		return _Method (self._call, name)

	def _call (self, method, args, karg):
		if not karg.get ('meta'):
			karg ['meta'] = {}
		karg ['meta'] = {'__reqid': args [0]}
		karg ['callback'] = self
		self.__was._call (method, args [1:], karg)

	def __call__ (self, response):
		if self._done:
			return
		self._numres += 1
		response.reqid = response.meta ["__reqid"]
		try:
			self.handler (self, response)
		except:
			self.was.traceback ()
			self.reject ("

Error Occured While Processing
%s
" % (self.was.app.debug and catch (1) or "",))

	def __setitem__ (self, name, data):
		self._parts [name] = data

	def __getitem__ (self, name, default = None):
		return self._parts.get (name, default)

	def set (self, k, v):
		self [k] = v

	def encode (self, d):
		if type (d) is str:
			return d.encode ('utf8')
		return d

	def send (self, data):
		if self._done:
			return
		self._data.append (self.encode (data))
		if self.was.env ['wsgi.multithread']:
			trigger.wakeup ()

	def render (self, template_file, _do_not_use_this_variable_name_ = {}, **karg):
		if not _do_not_use_this_variable_name_ and not karg:
			return self.render (template_file, self._parts)
		return self.was.render (template_file, _do_not_use_this_variable_name_, **karg)

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

	def __len__ (self):
		return self._numreq

	def fulfilled (self):
		return self._numreq == self._numres

	def pending (self):
		return not self._done

	def rejected (self):
		return self._rejected

	def settled (self):
		return self.fulfilled () and self._done

	def settle (self, data = None):
		if data:
			self.send (data)
		self._done = True

	def reject (self, data):
		self._rejected = 1
		self.settle (data)

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

	def ready (self):
		return self._data or self._done

	def more (self):
		if self._done and not self._data:
			return b''
		d, self._data = b''.join (self._data), []
		return d

		<commit_msg>fix promise<commit_after>from aquests.lib.producers import simple_producer
from aquests.lib.athreads import trigger
from skitai.server.http_response import catch

class _Method:
	def __init__(self, send, name):
		self.__send = send
		self.__name = name

	def __getattr__(self, name):
		return _Method(self.__send, "%s.%s" % (self.__name, name))

	def __call__(self, *args, **karg):
		return self.__send(self.__name, args, karg)

class Promise (simple_producer):
	def __init__ (self, was, handler, **karg):
		self.__was = was
		self.was = was._clone ()
		self.handler = handler

		self._data = []
		self._parts = karg
		self._numreq = 0
		self._numres = 0
		self._done = False
		self._rejected = 0

	def __getattr__ (self, name):
		if name.split (".")[0] not in self.__was.VALID_COMMANDS:
			return getattr (self.was, name)
		self._numreq += 1
		return _Method (self._call, name)

	def _call (self, method, args, karg):
		if not karg.get ('meta'):
			karg ['meta'] = {}
		karg ['meta'] = {'__reqid': args [0]}
		karg ['callback'] = self
		return self.__was._call (method, args [1:], karg)

	def __call__ (self, response):
		if self._done:
			return
		self._numres += 1
		response.id = response.meta ["__reqid"]
		try:
			self.handler (self, response)
		except:
			self.was.traceback ()
			self.reject ("

Error Occured While Processing
%s
" % (self.was.app.debug and catch (1) or "",))

	def __setitem__ (self, name, data):
		self._parts [name] = data

	def __getitem__ (self, name, default = None):
		return self._parts.get (name, default)

	def set (self, k, v):
		self [k] = v

	def encode (self, d):
		if type (d) is bytes:
			return d
		return str (d).encode ('utf8')

	def send (self, data):
		if self._done:
			return
		self._data.append (self.encode (data))
		if self.was.env ['wsgi.multithread']:
			trigger.wakeup ()

	def render (self, template_file, _do_not_use_this_variable_name_ = {}, **karg):
		if not _do_not_use_this_variable_name_ and not karg:
			return self.render (template_file, self._parts)
		return self.was.render (template_file, _do_not_use_this_variable_name_, **karg)

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

	def __len__ (self):
		return self._numreq

	def fulfilled (self):
		return self._numreq == self._numres

	def pending (self):
		return not self._done

	def rejected (self):
		return self._rejected

	def settled (self):
		return self.fulfilled () and self._done

	def settle (self, data = None):
		if data:
			self.send (data)
		self._done = True

	def reject (self, data):
		self._rejected = 1
		self.settle (data)

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

	def ready (self):
		return self._data or self._done

	def more (self):
		if self._done and not self._data:
			return b''
		d, self._data = b''.join (self._data), []
		return d

		<|endoftext|>


<commit_before>import sys

from django.conf import settings
from django.views.decorators.csrf import csrf_exempt

from shanghai.http import HttpResponseNotImplemented
from shanghai.utils import is_iterable, setattrs

class DispatcherMixin(object):

    @staticmethod
    def action_not_implemented():
        return HttpResponseNotImplemented()

    @staticmethod
    def resolve_pk(pk):
        if pk is None or not len(pk):
            return None
        return pk

    def resolve_parameters(self):
        pk = self.kwargs.get('pk', None)
        link = self.kwargs.get('link', None)
        related = self.kwargs.get('related', None)

        pk = self.resolve_pk(pk)

        return pk, link, related

    def resolve_action(self):
        method = self.request.method.lower()

        if self.pk is None:
            return method, 'collection'
        elif self.link:
            return method, 'linked'
        elif self.related:
            return method, 'related'
        else:
            return method, 'object'

    def resolve_input(self):
        import json

        method = self.request.method.lower()
        body = self.request.body

        if method in ('post', 'put', 'delete') and body is not None:
            body = body.decode(settings.DEFAULT_CHARSET)

            if body is None or not len(body):
                return None

            return json.loads(body)

    @csrf_exempt
    def dispatch(self, request, *args, **kwargs):
        setattrs(self, request=request, args=args, kwargs=kwargs)

        pk, link, related = self.resolve_parameters()
        setattrs(self, pk=pk, link=link, related=related)

        action = self.resolve_action()
        setattr(self, 'action', action)

        input = self.resolve_input()
        setattr(self, 'input', input)

        callback = getattr(self, '_'.join(action), self.action_not_implemented)

        try:
            response = callback()
        except:
            exc_info = sys.exc_info()

            if settings.DEBUG:
                raise

            return self.response_with_error(exc_info[1])
        else:
            return response
<commit_msg>Deserialize identifier in URL<commit_after>import sys

from django.conf import settings
from django.views.decorators.csrf import csrf_exempt

from shanghai.http import HttpResponseNotImplemented
from shanghai.utils import setattrs

class DispatcherMixin(object):

    def action_not_implemented(self):
        return HttpResponseNotImplemented()

    def resolve_pk(self, pk):
        return self.get_id().transform.deserialize(pk)

    def resolve_parameters(self):
        pk = self.kwargs.get('pk', None)
        link = self.kwargs.get('link', None)
        related = self.kwargs.get('related', None)

        pk = self.resolve_pk(pk)

        return pk, link, related

    def resolve_action(self):
        method = self.request.method.lower()

        if self.pk is None:
            return method, 'collection'
        elif self.link:
            return method, 'linked'
        elif self.related:
            return method, 'related'
        else:
            return method, 'object'

    def resolve_input(self):
        import json

        method = self.request.method.lower()
        body = self.request.body

        if method in ('post', 'put', 'delete') and body is not None:
            body = body.decode(settings.DEFAULT_CHARSET)

            if body is None or not len(body):
                return None

            return json.loads(body)

    @csrf_exempt
    def dispatch(self, request, *args, **kwargs):
        setattrs(self, request=request, args=args, kwargs=kwargs)

        pk, link, related = self.resolve_parameters()
        setattrs(self, pk=pk, link=link, related=related)

        action = self.resolve_action()
        setattr(self, 'action', action)

        input = self.resolve_input()
        setattr(self, 'input', input)

        callback = getattr(self, '_'.join(action), self.action_not_implemented)

        try:
            response = callback()
        except:
            exc_info = sys.exc_info()

            if settings.DEBUG:
                raise

            return self.response_with_error(exc_info[1])
        else:
            return response
<|endoftext|>


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

# Copyright (c) 2015 Ericsson AB
#
# 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 pytest
import sys
import argparse
import traceback
from calvin.actorstore.store import ActorStore
from calvin.runtime.north.calvin_token import Token
from calvin.runtime.north.plugins.port.endpoint import Endpoint
from calvin.runtime.north import metering
from calvin.runtime.north.plugins.port import queue

def fwrite(port, value):
    if isinstance(value, Token):
        port.queue.write(value)
    else:
        port.queue.write(Token(value=value))

def pwrite(actor, portname, value):
    port = actor.inports.get(portname)
    if not port:
        raise Exception("No such port %s" % (portname,))
    try:
        if isinstance(value, list):
            for v in value:
                fwrite(port, v)
        else:
            fwrite(port, value)
    except queue.common.QueueFull:
        # Some tests seems to enter too many tokens but we ignore it
        # TODO make all actors' test compliant and change to raise exception
        pass

def pread(actor, portname, number=1):
    port = actor.outports.get(portname, None)
    assert port
    if number > 0:
        if not pavailable(actor, portname, number):
            raise AssertionError("Too few tokens available, %d, expected %d" % (pavailable(actor, portname), number))
    else:
        if pavailable(actor, portname, number+1):
            raise AssertionError("Too many tokens available, %d, expected %d" % (pavailable(actor, portname), number))

    values = [port.queue.peek(actor.id).value for _ in range(number)]
    port.queue.commit(actor.id), True
    return values

def pavailable(actor, portname, number):
    port = actor.outports.get(portname, None)
    assert port
    print "PORTID", port.id
    return port.queue.tokens_available(number, actor.id)

class DummyInEndpoint(Endpoint):

    """
    Dummy in endpoint for actor test
    """

    def __init__(self, port):
        super(DummyInEndpoint, self).__init__(port)

    def is_connected(self):
        return True

class DummyOutEndpoint(Endpoint):

    """
    Dummy out endpoint for actor test
    """

    def __init__(self, port):
        super(DummyOutEndpoint, self).__init__(port)

    def is_connected(self):
        return True

class FDMock(object):
    def __init__(self, fname, mode):
        self.fp = open(fname, mode)
        if 'r' in mode:
            self.buffer = self.fp.read()
        else:
            self.buffer = ""

    def close(self):
        self.fp.close()

    def eof(self):
        return len(self.buffer) == 0

    def has_data(self):
        return len(self.buffer) > 0

    def read(self):
        data = self.buffer
        self.buffer = ""
        return data

    def write(self, data):
        self.buffer += data
        self.fp.write(data)

    def read_line(self):
        if '\n' in self.buffer:
            line, self.buffer = self.buffer.split("\n", 1)
        else:
            line = self.buffer
            self.buffer = ""
        return line

    def write_line(self, data):
        self.buffer += data + "\n"
        self.fp.write(data + "\n")

class StdInMock(FDMock):
    def __init__(self):
        self.buffer = "stdin\nstdin_second_line"

class TimerMock(object):

    def __init__(self):
        self._triggered = False

    @property
    def triggered(self):
        return self._triggered

    def ack(self):
        assert self._triggered
        self._triggered = False

    def cancel(self):
        del self._triggered

    def trigger(self):
        assert not self._triggered
        self._triggered = True

class CalvinSysTimerMock(object):
    def repeat(self, delay):
        return TimerMock()

    def once(self, delay):
        return TimerMock()

class CalvinSysFileMock(object):
    def open(self, fname, mode):
        return FDMock(fname, mode)

    def open_stdin(self):
        return StdInMock()

    def close(self, fdmock):
        fdmock.close()

def load_python_requirement(req):
    import importlib
    loaded = importlib.import_module("calvin.calvinsys.native." + req)
    return loaded.register

requirements = \
    {
        'calvinsys.io.filehandler': CalvinSysFileMock,
        'calvinsys.events.timer': CalvinSysTimerMock,
        'calvinsys.native.python-os-path': load_python_requirement('python-os-path'),
        'calvinsys.native.python-re': load_python_requirement('python-re'),
        'calvinsys.native.python-json': load_python_requirement('python-json'),
        'calvinsys.native.python-copy': load_python_requirement('python-copy'),
        'calvinsys.native.python-base64': load_python_requirement('python-base64')

    }

class CalvinSysMock(dict):
    def use_requirement(self, actor, requirement):
        return requirements[requirement]()

class ActorTester(object):

    def __init__(self):
        self.store = ActorStore()
        self.actors = {}
        self.illegal_actors = {}
        self.components = {}
        self.id = "ActorTester"
        self.metering = metering.set_metering(metering.Metering(self))

    def collect_actors(self, actor):
        actors = [m + '.' + a for m in self.store.modules() for a in self.store.actors(m)]

        if actor:
            actors = [a for a in actors if actor in a]

        self.actor_names = actors

    def instantiate_actor(self, actorclass, actorname):
        try:
            actor = actorclass(actorname, disable_state_checks=True)
            if not hasattr(actor, 'test_set'):
                self.actors[actorname] = 'no_test'
                return
            actor._calvinsys = CalvinSysMock()
            actor._calvinsys['file'] = CalvinSysFileMock()
            actor._calvinsys['timer'] = CalvinSysTimerMock()
            actor.init(*actorclass.test_args, **actorclass.test_kwargs)
            actor.setup_complete()
        except Exception as e:
            self.illegal_actors[actorname] = "Failed to instantiate"
            sys.stderr.write("Actor %s: %s" % (actorname, e))
            sys.stderr.write(''.join(traceback.format_exc()))
            raise e

        for inport in actor.inports.values():
            inport.set_queue(queue.fanout_fifo.FanoutFIFO(5))
            inport.endpoint = DummyInEndpoint(inport)
            inport.queue.add_reader(inport.id)
        for outport in actor.outports.values():
            outport.set_queue(queue.fanout_fifo.FanoutFIFO(5))
            outport.queue.add_reader(actor.id)
            outport.endpoints.append(DummyOutEndpoint(outport))

        self.actors[actorname] = actor

    def instantiate_actors(self):
        for a in self.actor_names:
            found, primitive, actorclass, signer = self.store.lookup(a)
            if found and primitive:
                self.instantiate_actor(actorclass, a)
            elif found and not primitive:
                self.components[a] = "TODO: Cannot test components (%s)" % (a,)
            else:
                self.illegal_actors[a] = "Unknown actor - probably parsing issues"

    def load_actor(self, path):
        actorclass = self.store.load_from_path(path)
        if actorclass:
            self.instantiate_actor(actorclass, path)
        else:
            self.illegal_actors[path] = "Unknown actor - probably parsing issues"

    def test_actor(self, actor, aut):
        for idx in range(len(aut.test_set)):
            test_index = idx + 1
            test = aut.test_set[idx]

            setups = test.get('setup', [])
            inputs = test.get('in', {})
            outputs = test.get('out', {})
            postconds = test.get('postcond', [])

            for f in setups:
                try:
                    f(aut)
                except Exception as e:
                    print "Actor %s failed during setup of test %d: %s" % (actor, test_index, e.message)
                    raise Exception("Failed during setup of test %d" % (test_index, ))

            for port, values in inputs.iteritems():
                pwrite(aut, port, values)

            aut.fire()

            for port, values in outputs.iteritems():
                try:
                    vals = pread(aut, port, len(values))
                    assert vals == values, "Expected output '%s' does not match '%s'" % (vals, values)
                except AssertionError as e:
                    print "Error:", str(e)
                    raise AssertionError("Failed test %d" % (test_index,))

            if not all(f(aut) for f in postconds):
                raise AssertionError("Failed post condition of test %d" % (test_index, ))

        return True

    def test_actors(self):
        test_pass = []
        test_fail = {}
        no_test = []

        for actor in self.actors:
            aut = self.actors[actor]
            if aut == "no_test":
                no_test.append(actor)
                continue
            try:
                self.test_actor(actor, aut)
                test_pass.append(actor)
            except AssertionError as e:
                test_fail[actor] = e.message
                # raise e
            except Exception as e:
                self.illegal_actors[actor] = str(e) + '\n' + ''.join(
                    traceback.format_exception(sys.exc_type, sys.exc_value, sys.exc_traceback))

        return {'pass': test_pass, 'fail': test_fail, 'skipped': no_test,
                'errors': self.illegal_actors, 'components': self.components}

def show_result(header, result):
    print header
    for actor in result:
        print "  %s" % (actor, )

def show_issue(header, result):
    print header
    for actor, reason in result.iteritems():
        print "  %s: %s" % (actor, reason)

def show_issues(results):
    if results['errors']:
        show_issue("Actors with errors", results['errors'])
    if results['fail']:
        show_issue("Failed actor tests", results['fail'])

def show_results(results):
    if results['pass']:
        show_result("Passed tests", results['pass'])
    if results['skipped']:
        show_result("Skipped tests", results['skipped'])
    if results['components']:
        show_issue("Components", results['components'])
    show_issues(results)

@pytest.mark.essential
def test_actors(actor="", show=False, path=None):
    t = ActorTester()
    if path:
        t.load_actor(path)
    else:
        t.collect_actors(actor)
        t.instantiate_actors()
    results = t.test_actors()

    if not any(results.values()):
        if actor:
            print "No actors matching '%s' found" % (actor,)
        else:
            raise Exception("No actors found")

    if show:
        return results

    if results['fail'] or results['errors']:
        show_issues(results)

    if results['errors']:
        raise Exception("%d actor(s) had errors" % (len(results['errors']), ))
    if results['fail']:
        raise Exception("%d actor(s) failed tests" % (len(results['fail']),))

if __name__ == '__main__':

    argparser = argparse.ArgumentParser(description="Run actor unittests")
    group = argparser.add_mutually_exclusive_group()
    group.add_argument('--path', '-p', type=str,
                       help='path to actor to test')
    group.add_argument('filter', type=str, nargs='?',
                       help='test actors matching filter')
    args = argparser.parse_args()

    if args.path:
        show_results(test_actors(path=args.path, show=True))
    elif args.filter:
        show_results(test_actors(actor=args.filter, show=True))
    else:
        show_results(test_actors(show=True))
<commit_msg>Removed unnecessary print.<commit_after># -*- coding: utf-8 -*-

# Copyright (c) 2015 Ericsson AB
#
# 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 pytest
import sys
import argparse
import traceback
from calvin.actorstore.store import ActorStore
from calvin.runtime.north.calvin_token import Token
from calvin.runtime.north.plugins.port.endpoint import Endpoint
from calvin.runtime.north import metering
from calvin.runtime.north.plugins.port import queue

def fwrite(port, value):
    if isinstance(value, Token):
        port.queue.write(value)
    else:
        port.queue.write(Token(value=value))

def pwrite(actor, portname, value):
    port = actor.inports.get(portname)
    if not port:
        raise Exception("No such port %s" % (portname,))
    try:
        if isinstance(value, list):
            for v in value:
                fwrite(port, v)
        else:
            fwrite(port, value)
    except queue.common.QueueFull:
        # Some tests seems to enter too many tokens but we ignore it
        # TODO make all actors' test compliant and change to raise exception
        pass

def pread(actor, portname, number=1):
    port = actor.outports.get(portname, None)
    assert port
    if number > 0:
        if not pavailable(actor, portname, number):
            raise AssertionError("Too few tokens available, %d, expected %d" % (pavailable(actor, portname), number))
    else:
        if pavailable(actor, portname, number+1):
            raise AssertionError("Too many tokens available, %d, expected %d" % (pavailable(actor, portname), number))

    values = [port.queue.peek(actor.id).value for _ in range(number)]
    port.queue.commit(actor.id), True
    return values

def pavailable(actor, portname, number):
    port = actor.outports.get(portname, None)
    assert port
    return port.queue.tokens_available(number, actor.id)

class DummyInEndpoint(Endpoint):

    """
    Dummy in endpoint for actor test
    """

    def __init__(self, port):
        super(DummyInEndpoint, self).__init__(port)

    def is_connected(self):
        return True

class DummyOutEndpoint(Endpoint):

    """
    Dummy out endpoint for actor test
    """

    def __init__(self, port):
        super(DummyOutEndpoint, self).__init__(port)

    def is_connected(self):
        return True

class FDMock(object):
    def __init__(self, fname, mode):
        self.fp = open(fname, mode)
        if 'r' in mode:
            self.buffer = self.fp.read()
        else:
            self.buffer = ""

    def close(self):
        self.fp.close()

    def eof(self):
        return len(self.buffer) == 0

    def has_data(self):
        return len(self.buffer) > 0

    def read(self):
        data = self.buffer
        self.buffer = ""
        return data

    def write(self, data):
        self.buffer += data
        self.fp.write(data)

    def read_line(self):
        if '\n' in self.buffer:
            line, self.buffer = self.buffer.split("\n", 1)
        else:
            line = self.buffer
            self.buffer = ""
        return line

    def write_line(self, data):
        self.buffer += data + "\n"
        self.fp.write(data + "\n")

class StdInMock(FDMock):
    def __init__(self):
        self.buffer = "stdin\nstdin_second_line"

class TimerMock(object):

    def __init__(self):
        self._triggered = False

    @property
    def triggered(self):
        return self._triggered

    def ack(self):
        assert self._triggered
        self._triggered = False

    def cancel(self):
        del self._triggered

    def trigger(self):
        assert not self._triggered
        self._triggered = True

class CalvinSysTimerMock(object):
    def repeat(self, delay):
        return TimerMock()

    def once(self, delay):
        return TimerMock()

class CalvinSysFileMock(object):
    def open(self, fname, mode):
        return FDMock(fname, mode)

    def open_stdin(self):
        return StdInMock()

    def close(self, fdmock):
        fdmock.close()

def load_python_requirement(req):
    import importlib
    loaded = importlib.import_module("calvin.calvinsys.native." + req)
    return loaded.register

requirements = \
    {
        'calvinsys.io.filehandler': CalvinSysFileMock,
        'calvinsys.events.timer': CalvinSysTimerMock,
        'calvinsys.native.python-os-path': load_python_requirement('python-os-path'),
        'calvinsys.native.python-re': load_python_requirement('python-re'),
        'calvinsys.native.python-json': load_python_requirement('python-json'),
        'calvinsys.native.python-copy': load_python_requirement('python-copy'),
        'calvinsys.native.python-base64': load_python_requirement('python-base64')

    }

class CalvinSysMock(dict):
    def use_requirement(self, actor, requirement):
        return requirements[requirement]()

class ActorTester(object):

    def __init__(self):
        self.store = ActorStore()
        self.actors = {}
        self.illegal_actors = {}
        self.components = {}
        self.id = "ActorTester"
        self.metering = metering.set_metering(metering.Metering(self))

    def collect_actors(self, actor):
        actors = [m + '.' + a for m in self.store.modules() for a in self.store.actors(m)]

        if actor:
            actors = [a for a in actors if actor in a]

        self.actor_names = actors

    def instantiate_actor(self, actorclass, actorname):
        try:
            actor = actorclass(actorname, disable_state_checks=True)
            if not hasattr(actor, 'test_set'):
                self.actors[actorname] = 'no_test'
                return
            actor._calvinsys = CalvinSysMock()
            actor._calvinsys['file'] = CalvinSysFileMock()
            actor._calvinsys['timer'] = CalvinSysTimerMock()
            actor.init(*actorclass.test_args, **actorclass.test_kwargs)
            actor.setup_complete()
        except Exception as e:
            self.illegal_actors[actorname] = "Failed to instantiate"
            sys.stderr.write("Actor %s: %s" % (actorname, e))
            sys.stderr.write(''.join(traceback.format_exc()))
            raise e

        for inport in actor.inports.values():
            inport.set_queue(queue.fanout_fifo.FanoutFIFO(5))
            inport.endpoint = DummyInEndpoint(inport)
            inport.queue.add_reader(inport.id)
        for outport in actor.outports.values():
            outport.set_queue(queue.fanout_fifo.FanoutFIFO(5))
            outport.queue.add_reader(actor.id)
            outport.endpoints.append(DummyOutEndpoint(outport))

        self.actors[actorname] = actor

    def instantiate_actors(self):
        for a in self.actor_names:
            found, primitive, actorclass, signer = self.store.lookup(a)
            if found and primitive:
                self.instantiate_actor(actorclass, a)
            elif found and not primitive:
                self.components[a] = "TODO: Cannot test components (%s)" % (a,)
            else:
                self.illegal_actors[a] = "Unknown actor - probably parsing issues"

    def load_actor(self, path):
        actorclass = self.store.load_from_path(path)
        if actorclass:
            self.instantiate_actor(actorclass, path)
        else:
            self.illegal_actors[path] = "Unknown actor - probably parsing issues"

    def test_actor(self, actor, aut):
        for idx in range(len(aut.test_set)):
            test_index = idx + 1
            test = aut.test_set[idx]

            setups = test.get('setup', [])
            inputs = test.get('in', {})
            outputs = test.get('out', {})
            postconds = test.get('postcond', [])

            for f in setups:
                try:
                    f(aut)
                except Exception as e:
                    print "Actor %s failed during setup of test %d: %s" % (actor, test_index, e.message)
                    raise Exception("Failed during setup of test %d" % (test_index, ))

            for port, values in inputs.iteritems():
                pwrite(aut, port, values)

            aut.fire()

            for port, values in outputs.iteritems():
                try:
                    vals = pread(aut, port, len(values))
                    assert vals == values, "Expected output '%s' does not match '%s'" % (vals, values)
                except AssertionError as e:
                    print "Error:", str(e)
                    raise AssertionError("Failed test %d" % (test_index,))

            if not all(f(aut) for f in postconds):
                raise AssertionError("Failed post condition of test %d" % (test_index, ))

        return True

    def test_actors(self):
        test_pass = []
        test_fail = {}
        no_test = []

        for actor in self.actors:
            aut = self.actors[actor]
            if aut == "no_test":
                no_test.append(actor)
                continue
            try:
                self.test_actor(actor, aut)
                test_pass.append(actor)
            except AssertionError as e:
                test_fail[actor] = e.message
                # raise e
            except Exception as e:
                self.illegal_actors[actor] = str(e) + '\n' + ''.join(
                    traceback.format_exception(sys.exc_type, sys.exc_value, sys.exc_traceback))

        return {'pass': test_pass, 'fail': test_fail, 'skipped': no_test,
                'errors': self.illegal_actors, 'components': self.components}

def show_result(header, result):
    print header
    for actor in result:
        print "  %s" % (actor, )

def show_issue(header, result):
    print header
    for actor, reason in result.iteritems():
        print "  %s: %s" % (actor, reason)

def show_issues(results):
    if results['errors']:
        show_issue("Actors with errors", results['errors'])
    if results['fail']:
        show_issue("Failed actor tests", results['fail'])

def show_results(results):
    if results['pass']:
        show_result("Passed tests", results['pass'])
    if results['skipped']:
        show_result("Skipped tests", results['skipped'])
    if results['components']:
        show_issue("Components", results['components'])
    show_issues(results)

@pytest.mark.essential
def test_actors(actor="", show=False, path=None):
    t = ActorTester()
    if path:
        t.load_actor(path)
    else:
        t.collect_actors(actor)
        t.instantiate_actors()
    results = t.test_actors()

    if not any(results.values()):
        if actor:
            print "No actors matching '%s' found" % (actor,)
        else:
            raise Exception("No actors found")

    if show:
        return results

    if results['fail'] or results['errors']:
        show_issues(results)

    if results['errors']:
        raise Exception("%d actor(s) had errors" % (len(results['errors']), ))
    if results['fail']:
        raise Exception("%d actor(s) failed tests" % (len(results['fail']),))

if __name__ == '__main__':

    argparser = argparse.ArgumentParser(description="Run actor unittests")
    group = argparser.add_mutually_exclusive_group()
    group.add_argument('--path', '-p', type=str,
                       help='path to actor to test')
    group.add_argument('filter', type=str, nargs='?',
                       help='test actors matching filter')
    args = argparser.parse_args()

    if args.path:
        show_results(test_actors(path=args.path, show=True))
    elif args.filter:
        show_results(test_actors(actor=args.filter, show=True))
    else:
        show_results(test_actors(show=True))
<|endoftext|>


<commit_before>import cgi
import re
import urlparse

from lxml import builder
from lxml import etree
from lxml.html import builder as E
from lxml.html import tostring
from lxml.html.clean import clean_html

from vidscraper.decorators import provide_shortmem, parse_url, returns_unicode
from vidscraper import errors, util

EMaker = builder.ElementMaker()
EMBED = EMaker.embed

EMBED_WIDTH = 425
EMBED_HEIGHT = 344

@provide_shortmem
@parse_url
@returns_unicode
def scrape_title(url, shortmem=None):
    try:
        return shortmem['base_etree'].xpath(
            "//div[@id='watch-vid-title']/h1")[0].text
    except IndexError:
        raise errors.FieldNotFound('Could not find the title field')

@provide_shortmem
@parse_url
@returns_unicode
def scrape_description(url, shortmem=None):
    span_elts = shortmem['base_etree'].xpath(
        "id('watch-video-details-inner')/"
        "div[@class='expand-content']/"
        "div[position()=1]/span")
    return util.clean_description_html(
        '\n'.join([etree.tostring(span_elt) for span_elt in span_elts])).strip()

@provide_shortmem
@returns_unicode
def get_embed(url, shortmem=None, width=EMBED_WIDTH, height=EMBED_HEIGHT):
    video_id = cgi.parse_qs(urlparse.urlsplit(url)[3])['v'][0]

    flash_url = 'http://www.youtube.com/v/%s&hl=en&fs=1' % video_id

    object_children = (
        E.PARAM(name="movie", value=flash_url),
        E.PARAM(name="allowFullScreen", value="true"),
        E.PARAM(name="allowscriptaccess", value="always"),
        EMBED(src=flash_url,
              type="application/x-shockwave-flash",
              allowfullscreen="true",
              allowscriptaccess="always",
              width=str(EMBED_WIDTH), height=str(EMBED_HEIGHT)))
    main_object = E.OBJECT(
        width=str(EMBED_WIDTH), height=str(EMBED_HEIGHT), *object_children)

    return tostring(main_object)

@provide_shortmem
@parse_url
@returns_unicode
def get_flash_enclosure_url(url, shortmem=None):
    return url

@provide_shortmem
@returns_unicode
def get_thumbnail_url(url, shortmem=None):
    video_id = cgi.parse_qs(urlparse.urlsplit(url)[3])['v'][0]
    return 'http://img.youtube.com/vi/%s/default.jpg' % video_id

YOUTUBE_REGEX = re.compile(r'https?://([^/]+\.)?youtube.com/(?:watch)?\?v=')
SUITE = {
    'regex': YOUTUBE_REGEX,
    'funcs': {
        'title': scrape_title,
        'description': scrape_description,
        'embed': get_embed,
        'thumbnail_url': get_thumbnail_url,
        'flash_enclosure_url': get_flash_enclosure_url}}
<commit_msg>scrape the published date for YouTube videos<commit_after>import cgi
import datetime
import re
import time
import urlparse

import feedparser

from lxml import builder
from lxml import etree
from lxml.html import builder as E
from lxml.html import tostring

from vidscraper.decorators import provide_shortmem, parse_url, returns_unicode
from vidscraper import errors, util

EMaker = builder.ElementMaker()
EMBED = EMaker.embed

EMBED_WIDTH = 425
EMBED_HEIGHT = 344

@provide_shortmem
@parse_url
@returns_unicode
def scrape_title(url, shortmem=None):
    try:
        return shortmem['base_etree'].xpath(
            "//div[@id='watch-vid-title']/h1")[0].text
    except IndexError:
        raise errors.FieldNotFound('Could not find the title field')

@provide_shortmem
@parse_url
@returns_unicode
def scrape_description(url, shortmem=None):
    span_elts = shortmem['base_etree'].xpath(
        "id('watch-video-details-inner')/"
        "div[@class='expand-content']/"
        "div[position()=1]/span")
    return util.clean_description_html(
        '\n'.join([etree.tostring(span_elt) for span_elt in span_elts])).strip()

@provide_shortmem
@returns_unicode
def get_embed(url, shortmem=None, width=EMBED_WIDTH, height=EMBED_HEIGHT):
    video_id = cgi.parse_qs(urlparse.urlsplit(url)[3])['v'][0]

    flash_url = 'http://www.youtube.com/v/%s&hl=en&fs=1' % video_id

    object_children = (
        E.PARAM(name="movie", value=flash_url),
        E.PARAM(name="allowFullScreen", value="true"),
        E.PARAM(name="allowscriptaccess", value="always"),
        EMBED(src=flash_url,
              type="application/x-shockwave-flash",
              allowfullscreen="true",
              allowscriptaccess="always",
              width=str(EMBED_WIDTH), height=str(EMBED_HEIGHT)))
    main_object = E.OBJECT(
        width=str(EMBED_WIDTH), height=str(EMBED_HEIGHT), *object_children)

    return tostring(main_object)

@provide_shortmem
@parse_url
@returns_unicode
def get_flash_enclosure_url(url, shortmem=None):
    return url

@provide_shortmem
@returns_unicode
def get_thumbnail_url(url, shortmem=None):
    video_id = cgi.parse_qs(urlparse.urlsplit(url)[3])['v'][0]
    return 'http://img.youtube.com/vi/%s/default.jpg' % video_id

@provide_shortmem
def scrape_published_date(url, shortmem=None):
    video_id = cgi.parse_qs(urlparse.urlsplit(url)[3])['v'][0]
    api_url = 'http://gdata.youtube.com/feeds/api/videos/' + video_id
    feed = feedparser.parse(api_url)
    return datetime.datetime.fromtimestamp(time.mktime(
            feed.entries[0].published_parsed))

YOUTUBE_REGEX = re.compile(r'https?://([^/]+\.)?youtube.com/(?:watch)?\?v=')
SUITE = {
    'regex': YOUTUBE_REGEX,
    'funcs': {
        'title': scrape_title,
        'description': scrape_description,
        'embed': get_embed,
        'thumbnail_url': get_thumbnail_url,
        'publish_date': scrape_published_date,
        'flash_enclosure_url': get_flash_enclosure_url}}
<|endoftext|>


<commit_before># -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.

import logging

import openerp

from openerp import tools
from openerp.osv import fields, osv
from openerp.tools.translate import _

class pos_config(osv.osv):
    _inherit = 'pos.config'
    _columns = {
        'iface_discount': fields.boolean('Order Discounts', help='Allow the cashier to give discounts on the whole order.'),
        'discount_pc': fields.float('Discount Percentage', help='The default discount percentage'),
        'discount_product_id': fields.many2one('product.product','Discount Product', help='The product used to model the discount'),
    }
    _defaults = {
        'iface_discount': True,
        'discount_pc': 10,
    }
<commit_msg>[FIX] pos_discount: discount product<commit_after># -*- coding: utf-8 -*-
# Part of Odoo. See LICENSE file for full copyright and licensing details.

import logging

import openerp

from openerp import tools
from openerp.osv import fields, osv
from openerp.tools.translate import _

class pos_config(osv.osv):
    _inherit = 'pos.config'
    _columns = {
        'iface_discount': fields.boolean('Order Discounts', help='Allow the cashier to give discounts on the whole order.'),
        'discount_pc': fields.float('Discount Percentage', help='The default discount percentage'),
        'discount_product_id': fields.many2one('product.product', 'Discount Product', domain="[('available_in_pos', '=', True)]", help='The product used to model the discount'),
    }
    _defaults = {
        'iface_discount': True,
        'discount_pc': 10,
    }
<|endoftext|>


<commit_before>from ..value_function import ValueFunction
from ...utils import check_random_state

_MEAN = 0.0
_STD = 0.3

class TabularVF(ValueFunction):
    '''
    Tabular Value Function.

    Can be used for both state V(s) and state-action Q(s, a) values.

    # Arguments
        init: boolean. Wheter to return a randomly initialized value
            when accessed and does not exist in the table.

    # Example
        ```python
        >>> v = TabularVF()
        >>> v[state] = 3.2
        >>> print(q[state])
        3.2

        >>> q = TabularVF()
        >>> q[state, action] = 1.8
        >>> print(q[state, action])
        1.8
        ```
    '''

    def __init__(self, init=True, random_state=None):
        self.init = init
        self.random_state = check_random_state(random_state)
        self._table = {}

    def __setitem__(self, key, value):
        '''
        Sets the state or state-action value.

        # Arguments
            key: `state` or `(state, action)`
            value: a scalar
        '''
        self._table[key] = value

    #################
    # ValueFunction #
    #################

    def __getitem__(self, key):
        '''
        Returns the state or state-action value.

        # Arguments
            key: `state` or `(state, action)`
        # Returns
            a scalar value.
        '''
        if key not in self._table:
            if self.init:
                self._table[key] = self.random_state.normal(_MEAN, _STD)
            else:
                self._table[key] = 0
        return self._table[key]
<commit_msg>update<commit_after>from ..value_function import ValueFunction
from ...utils import check_random_state

_MEAN = 0.0
_STD = 0.3

class TabularVF(ValueFunction):
    '''
    Tabular Value Function.

    Can be used for both state V(s) and state-action Q(s, a) values.

    # Arguments
        init: boolean. Wheter to return a randomly initialized value
            when accessed and does not exist in the table.

    # Example
        ```python
        >>> v = TabularVF()
        >>> v[state] = 3.2
        >>> print(q[state])
        3.2
        >>> q = TabularVF()
        >>> q[state, action] = 1.8
        >>> print(q[state, action])
        1.8
        ```
    '''

    def __init__(self, init=True, random_state=None):
        self.init = init
        self.random_state = check_random_state(random_state)
        self._table = {}

    def __setitem__(self, key, value):
        '''
        Sets the state or state-action value.

        # Arguments
            key: `state` or `(state, action)`
            value: a scalar
        '''
        self._table[key] = value

    #################
    # ValueFunction #
    #################

    def __getitem__(self, key):
        '''
        Returns the state or state-action value.

        # Arguments
            key: `state` or `(state, action)`
        # Returns
            a scalar value.
        '''
        if key not in self._table:
            if self.init:
                self._table[key] = self.random_state.normal(_MEAN, _STD)
            else:
                self._table[key] = 0
        return self._table[key]
<|endoftext|>