changeset 847:431ddd043b47

started porting curvecalc to SyncedGraph Ignore-this: bd6b099c6d5d84d56c9320a0fb742de7
author drewp@bigasterisk.com
date Tue, 26 Mar 2013 08:43:37 +0000
parents 89afd38433e8
children dacbb278d91d
files bin/curvecalc light9/curvecalc/subterm.py light9/curvecalc/subtermview.py
diffstat 3 files changed, 26 insertions(+), 53 deletions(-) [+]
line wrap: on
line diff
--- a/bin/curvecalc	Tue Mar 26 08:24:06 2013 +0000
+++ b/bin/curvecalc	Tue Mar 26 08:43:37 2013 +0000
@@ -29,19 +29,11 @@
 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
@@ -65,7 +57,10 @@
         mainwin.show_all()
 
         mainwin.connect("delete-event", lambda *args: reactor.crash())
-        mainwin.set_title("curvecalc - %s" % graph.label(song))
+        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,
@@ -74,7 +69,7 @@
 
 
         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"))
@@ -155,13 +150,24 @@
             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)
@@ -211,18 +217,6 @@
     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([
@@ -289,11 +283,6 @@
         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()
@@ -321,7 +310,7 @@
         song = currentlyPlayingSong()
 
     music = Music()
-    graph = makeGraph()
+    graph = SyncedGraph("curvecalc")
 
     curveset = Curveset(sliders=opts.sliders)
     subterms = []
@@ -331,14 +320,6 @@
         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)
 
--- a/light9/curvecalc/subterm.py	Tue Mar 26 08:24:06 2013 +0000
+++ b/light9/curvecalc/subterm.py	Tue Mar 26 08:43:37 2013 +0000
@@ -135,15 +135,6 @@
 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()
--- a/light9/curvecalc/subtermview.py	Tue Mar 26 08:24:06 2013 +0000
+++ b/light9/curvecalc/subtermview.py	Tue Mar 26 08:43:37 2013 +0000
@@ -71,8 +71,9 @@
     # 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)