Changeset - 431ddd043b47
[Not reviewed]
default
0 3 0
drewp@bigasterisk.com - 12 years ago 2013-03-26 08:43:37
drewp@bigasterisk.com
started porting curvecalc to SyncedGraph
Ignore-this: bd6b099c6d5d84d56c9320a0fb742de7
3 files changed with 24 insertions and 51 deletions:
0 comments (0 inline, 0 general)
bin/curvecalc
Show inline comments
 
@@ -26,25 +26,17 @@ import run_local
 
from light9 import showconfig, prof, networking
 
from light9.curvecalc.curve import Curveset
 
from light9.curvecalc import curveview 
 
from light9.curvecalc.musicaccess import Music, currentlyPlayingSong
 
from light9.wavelength import wavelength
 
from light9.namespaces import L9
 
from light9.curvecalc.subterm import read_all_subs, savekey, graphPathForSubterms
 
from light9.curvecalc.subterm import savekey, graphPathForSubterms
 
from light9.curvecalc.subtermview import add_one_subterm
 
from light9.curvecalc.output import Output
 
from light9.gtkpyconsole import togglePyConsole
 

	
 
@prof.logTime
 
def makeGraph():
 
    graphOrig = showconfig.getGraph()
 
    graph = Graph() # a copy, since we're going to add subs into it
 
    for s in graphOrig:
 
        graph.add(s)
 
    read_all_subs(graph)
 
    return graph
 
from light9.rdfdb.syncedgraph import SyncedGraph
 

	
 
class SubtermExists(ValueError):
 
    pass
 

	
 
class Main(object):
 
    def __init__(self, graph, opts, song, curveset, subterms, music):
 
@@ -62,22 +54,25 @@ class Main(object):
 
        gtk.rc_parse_string("""style "default" {font_name = "sans 7"}""")
 
        if self.opts.reload:
 
            self.refreshTheme()
 
        mainwin.show_all()
 

	
 
        mainwin.connect("delete-event", lambda *args: reactor.crash())
 
        def updateTitle():
 
            # song will soon be a lookup on this curvecalc session
 
        mainwin.set_title("curvecalc - %s" % graph.label(song))
 
        graph.addHandler(updateTitle)
 
        mainwin.parse_geometry("1x1-0+0")
 

	
 
        # this is the only one i found that would set the size right,
 
        # but it's a minimum size, which i don't really want
 
        mainwin.set_size_request(1678, 922)
 

	
 

	
 
        wtree.get_object("subterms").connect("add", self.onSubtermChildAdded)
 
        self.add_subterms_for_song(song, curveset, subterms)
 
        graph.addHandler(self.add_subterms_for_song)
 
        self.refreshCurveView()       
 
        
 
        self.makeStatusLines(wtree.get_object("status"))
 

	
 
        def connect(w):
 
            w.drag_dest_set(flags=gtk.DEST_DEFAULT_ALL,
 
@@ -152,19 +147,30 @@ class Main(object):
 
    def makeSubterm(self, newname, withCurve=False, expr=None):
 
        uri = L9['sub/%s' % newname]
 
        if (uri, RDF.type, L9.Subterm) in self.graph:
 
            raise SubtermExists("already have a subterm named %r" % newname)
 
        self.graph.add((uri, RDF.type, L9.Subterm))
 
        self.graph.add((uri, RDFS.label, Literal(newname)))
 
        add_one_subterm(self.graph, uri,
 
                        self.curveset, self.subterms,
 
                        self.wtree.get_object("subterms"),
 
                        expr=expr, show=True)
 
        self.graph.add((self.song, L9['subterm'], uri))
 
        if withCurve:
 
            self.curveset.new_curve(newname)
 

	
 
    def add_subterms_for_song(self):
 
        master = self.wtree.get_object("subterms")
 
        [master.remove(c) for c in master.get_children()]
 

	
 
        for st in self.graph.objects(self.song, L9['subterm']):
 
            log.info("song %s has subterm %s", self.song, st)
 
            add_one_subterm(self.graph,
 
                            self.graph.value(st, L9['sub']),
 
                            self.curveset,
 
                            self.subterms,
 
                            master,
 
                            self.graph.value(st, L9['expression']))
 
        master.show_all()
 

	
 
    def refreshTheme(self):
 
        gtk.rc_reparse_all()
 
        reactor.callLater(1, self.refreshTheme)
 

	
 
    def onSubtermChildAdded(self, subtermsTable, *args):
 
        # this would probably work, but isn't getting called
 
@@ -208,24 +214,12 @@ class Main(object):
 
        times = [t for listener, t in results if t is not None]
 
        self.music.playOrPause(t=times[0] if times else None)
 

	
 
    def onSave(self, *args):
 
        savekey(self.song, self.subterms, self.curveset)
 

	
 
    def add_subterms_for_song(self, song, curveset, subterms):
 
        master = self.wtree.get_object("subterms")
 
        for st in self.graph.objects(song, L9['subterm']):
 
            log.info("song %s has subterm %s", song, st)
 
            add_one_subterm(self.graph,
 
                            self.graph.value(st, L9['sub']),
 
                            curveset,
 
                            subterms,
 
                            master,
 
                            self.graph.value(st, L9['expression']))
 
        master.show_all()
 

	
 
    def makeStatusLines(self, master):
 
        """various labels that listen for dispatcher signals"""
 
        for row, (signame, textfilter) in enumerate([
 
            ('input time', lambda t: "%.2fs"%t),
 
            ('output levels',
 
             lambda levels: textwrap.fill("; ".join(["%s:%.2f"%(n,v)
 
@@ -286,17 +280,12 @@ class Main(object):
 
            except Exception:
 
                print "reload failed:"
 
                traceback.print_exc()
 
        if self.opts.reload:
 
            reactor.callLater(1, self.refreshCurveView)
 

	
 
    def onReloadSubs(self, *args): # wants to be ctrl-r  too
 
        read_all_subs(self.graph) # this will discover new subs (additive only)
 
        dispatcher.send('reload all subs') # this rereads each sub from its graph file
 
        dispatcher.send("all curves rebuild")
 

	
 

	
 
def main():
 
    startTime = time.time()
 
    parser = optparse.OptionParser()
 
    parser.set_usage("%prog [opts] [songURI]")
 
    parser.add_option("--sliders", action='store_true',
 
@@ -318,30 +307,22 @@ def main():
 
    try:
 
        song = URIRef(args[0])
 
    except IndexError:
 
        song = currentlyPlayingSong()
 

	
 
    music = Music()
 
    graph = makeGraph()
 
    graph = SyncedGraph("curvecalc")
 

	
 
    curveset = Curveset(sliders=opts.sliders)
 
    subterms = []
 

	
 
    curveset.load(basename=os.path.join(
 
        showconfig.curvesDir(),
 
        showconfig.songFilenameFromURI(song)),
 
                  skipMusic=opts.skip_music)
 

	
 
    subtermPath = graphPathForSubterms(song)
 
    try:
 
        graph.parse(subtermPath, format='n3')
 
    except IOError, e:
 
        if e.errno != 2:
 
            raise
 
        log.info("%s not found, starting with empty graph" % subtermPath)
 
    
 
    log.debug("startup: output %s", time.time() - startTime)
 
    out = Output(subterms, music)
 

	
 
    musicfilename = showconfig.songOnDisk(song)
 
    maxtime = wavelength(musicfilename)
 
    dispatcher.connect(lambda: maxtime, "get max time", weak=False)
light9/curvecalc/subterm.py
Show inline comments
 
@@ -132,21 +132,12 @@ class Subterm:
 
        return "<Subterm %s %s>" % (self.submaster, self.subexpr)
 

	
 

	
 
def graphPathForSubterms(song):
 
    return showconfig.subtermsForSong(showconfig.songFilenameFromURI(song)) + ".n3"
 

	
 
@prof.logTime
 
def read_all_subs(graph):
 
    """read all sub files into this graph so when add_one_subterm tries
 
    to add, the sub will be available"""
 
    subsDir = showconfig.subsDir()
 
    for filename in os.listdir(subsDir):
 
        # parsing nt is faster, but it should try n3 format if the parsing fails
 
        graph.parse(os.path.join(subsDir, filename), format="n3")
 

	
 
def createSubtermGraph(song, subterms):
 
    """rdf graph describing the subterms, readable by add_subterms_for_song"""
 
    graph = Graph()
 
    for subterm in subterms:
 
        assert subterm.submaster.name, "submaster has no name"
 
        uri = URIRef(song + "/subterm/" + subterm.submaster.name)
light9/curvecalc/subtermview.py
Show inline comments
 
@@ -69,13 +69,14 @@ def add_one_subterm(graph, subUri, curve
 
        expr = '%s(t)' % subname
 

	
 
    # this is what I'd like to have, but the name replacement above is
 
    # too unclear for me to make the change now
 
    #get_global_submasters(graph).get_sub_by_name(
 
        
 
    sub = Submaster.Submaster(graph=graph, name=subname, sub=subUri)
 
    # graph.add([(subUri, RDFS.label, Literal(subname))]) # didntknow context yet
 
    sub = Submaster.PersistentSubmaster(graph, subUri)
 
    term = Subterm(sub, Subexpr(curveset, expr, graph))
 
    subterms.append(term)
 

	
 
    stv = Subtermview(graph, term)
 
    y = master.get_property('n-rows')
 
    master.attach(stv.label, 0, 1, y, y + 1, xoptions=0, yoptions=0)
0 comments (0 inline, 0 general)