Changeset - 6ce00faec207
[Not reviewed]
default
0 2 0
Drew Perttula - 11 years ago 2014-06-01 10:39:17
drewp@bigasterisk.com
move sequentialUri to the graph lib
Ignore-this: 3dc3fb4833a23a46b8cfea90788f25be
2 files changed with 19 insertions and 7 deletions:
0 comments (0 inline, 0 general)
bin/effecteval
Show inline comments
 
#!bin/python
 
from __future__ import division
 
from run_local import log
 
from twisted.internet import reactor
 
from twisted.internet.defer import inlineCallbacks, returnValue
 
import cyclone.web, cyclone.websocket, cyclone.httpclient
 
import sys, optparse, logging, subprocess, json, time, traceback
 
from rdflib import URIRef, Literal
 

	
 
sys.path.append(".")
 
from light9 import networking, showconfig, Submaster, dmxclient
 
from light9.rdfdb.syncedgraph import SyncedGraph
 
from light9.namespaces import L9, RDF, RDFS
 
from light9.rdfdb.patch import Patch
 
from light9.effecteval.effect import EffectNode
 
from light9.greplin_cyclone import StatsForCyclone
 
from greplin import scales
 

	
 
sys.path.append("/my/proj/homeauto/lib")
 
sys.path.append("/home/drewp/projects/homeauto/lib")
 
from cycloneerr import PrettyErrorHandler
 

	
 
class EffectEdit(PrettyErrorHandler, cyclone.web.RequestHandler):
 
    def get(self):
 
        self.write(open("light9/effecteval/effect.html").read())
 
    def delete(self):
 
        graph = self.settings.graph
 
        uri = URIRef(self.get_argument('uri'))
 
        with graph.currentState(tripleFilter=(None, L9['effect'], uri)) as g:
 
            song = ctx = list(g.subjects(L9['effect'], uri))[0]
 
        self.settings.graph.patch(Patch(delQuads=[
 
            (song, L9['effect'], uri, ctx),
 
            ]))
 

	
 
        
 
class SongEffects(PrettyErrorHandler, cyclone.web.RequestHandler):
 
    def post(self):
 
        song = URIRef(self.get_argument('uri'))
 
        drop = URIRef(self.get_argument('drop'))
 
        ctx = song
 
        now = time.time()
 
        effect = song + "/effect/e-%f" % now
 
        curve = song + "/curve/c-%f" % now
 
        graph = self.settings.graph
 
        effect = graph.sequentialUri(song + "/effect/e-")
 
        curve = graph.sequentialUri(song + "/curve/c-")
 

	
 
        with self.settings.graph.currentState(
 
        with graph.currentState(
 
                tripleFilter=(drop, RDFS.label, None)) as g:
 
            dropSubLabel = g.label(drop)
 
        
 
        self.settings.graph.patch(Patch(addQuads=[
 
        graph.patch(Patch(addQuads=[
 
            (song, L9['curve'], curve, ctx),
 
            (song, L9['effect'], effect, ctx),
 
            (effect, RDF.type, L9['Effect'], ctx),
 
            (effect, L9['code'],
 
             Literal('out = sub(%s, intensity=%s)' % (drop.n3(), curve.n3())),
 
             ctx),
 
            (curve, RDF.type, L9['Curve'], ctx),
 
            (curve, RDFS.label, Literal('sub %s' % dropSubLabel), ctx),
 
            (curve, L9['points'], Literal('0 0'), ctx),
 
            ]))
 
        
 
class SongEffectsUpdates(cyclone.websocket.WebSocketHandler):
 
    def connectionMade(self, *args, **kwargs):
 
        self.graph = self.settings.graph
 
        self.graph.addHandler(self.updateClient)
 
        
 
    def updateClient(self):
 
        # todo: abort if client is gone
 
        playlist = self.graph.value(showconfig.showUri(), L9['playList'])
 
        songs = list(self.graph.items(playlist))
 
        out = []
 
        for s in songs:
 
            out.append({'uri': s, 'label': self.graph.label(s)})
 
            out[-1]['effects'] = [{'uri': uri} for uri in sorted(self.graph.objects(s, L9['effect']))]
 
        self.sendMessage({'songs': out})
 
        
 
        
 
class EffectUpdates(cyclone.websocket.WebSocketHandler):
 
    """
 
    stays alive for the life of the effect page
 
    """
 
    def connectionMade(self, *args, **kwargs):
 
        log.info("websocket opened")
 
        self.uri = URIRef(self.get_argument('uri'))
 
        self.sendMessage({'hello': repr(self)})
 

	
 
        self.graph = self.settings.graph
 
        self.graph.addHandler(self.updateClient)
 

	
 
    def updateClient(self):
 
        # todo: if client has dropped, abort and don't get any more
 
        # graph updates
 
        self.sendMessage({'code': self.graph.value(self.uri, L9['code'])})
 
        
 
    def connectionLost(self, reason):
 
        log.info("websocket closed")
 

	
 
    def messageReceived(self, message):
light9/rdfdb/currentstategraphapi.py
Show inline comments
 
import logging, traceback, time
 
import logging, traceback, time, itertools
 
from rdflib import ConjunctiveGraph
 
from light9.rdfdb.rdflibpatch import contextsForStatement as rp_contextsForStatement
 
log = logging.getLogger("currentstate")
 

	
 
class CurrentStateGraphApi(object):
 
    """
 
    mixin for SyncedGraph, separated here because these methods work together
 
    """
 

	
 
    def currentState(self, context=None, tripleFilter=(None, None, None)):
 
        """
 
        a graph you can read without being in an addHandler
 

	
 
        you can save some time by passing a triple filter, and we'll only give you the matching triples
 
        """
 
        if context is not None:
 
            raise NotImplementedError("currentState with context arg")
 

	
 
        class Mgr(object):
 
            def __enter__(self2):
 
                # this should be a readonly view of the existing
 
                # graph, maybe with something to guard against
 
                # writes/patches happening while reads are being
 
                # done. Typical usage will do some reads on this graph
 
                # before moving on to writes.
 

	
 
                t1 = time.time()
 
                g = ConjunctiveGraph()
 
                for s,p,o,c in self._graph.quads(tripleFilter):
 
                    g.store.add((s,p,o), c)
 

	
 
                if tripleFilter == (None, None, None):
 
                    self2.logThisCopy(g, time.time() - t1)
 
                    
 
                g.contextsForStatement = lambda t: contextsForStatementNoWildcards(g, t)
 
                return g
 

	
 
            def logThisCopy(self, g, sec):
 
                log.info("copied graph %s statements (%.1f ms) "
 
                         "because of this:" % (len(g), sec * 1000))
 
                for frame in traceback.format_stack(limit=4)[:-2]:
 
                    for line in frame.splitlines():
 
                        log.info("  "+line)
 

	
 
            def __exit__(self, type, val, tb):
 
                return
 

	
 
        return Mgr()
 

	
 
    def sequentialUri(self, prefix):
 
        """
 
        Prefix URIRef like http://example.com/r- will return
 
        http://example.com/r-1 if that uri is not a subject in the graph,
 
        or else http://example.com/r-2, etc
 
        """
 
        for i in itertools.count(1):
 
            newUri = prefix + str(i)
 
            if not list(self._grap.triples((newUri, None, None))):
 
                return newUri
 

	
 
        
 
def contextsForStatementNoWildcards(g, triple):
 
    if None in triple:
 
        raise NotImplementedError("no wildcards")
 
    return rp_contextsForStatement(g, triple)
0 comments (0 inline, 0 general)