Changeset - 415c206f7534
[Not reviewed]
default
0 2 0
Drew Perttula - 18 years ago 2007-06-13 07:23:54
drewp@bigasterisk.com
fix sub loading and reloading in CC
2 files changed with 37 insertions and 18 deletions:
0 comments (0 inline, 0 general)
bin/curvecalc
Show inline comments
 
@@ -167,28 +167,29 @@ class Subterm:
 
            if isinstance(subexpr_eval, Submaster.Submaster):
 
                # if the expression returns a submaster, just return it
 
                return subexpr_eval
 
            else:
 
                # otherwise, return our submaster multiplied by the value 
 
                # returned
 
                return self.submaster * subexpr_eval
 
        except Exception, e:
 
            dispatcher.send("expr_error", sender=self.subexpr, exc=str(e))
 
            return Submaster.Submaster('Error: %s' % str(e), temporary=True)
 

	
 
class Subtermview(tk.Frame):
 
    def __init__(self,master,st,**kw):
 
    def __init__(self, master, graph, st, **kw):
 
        self.subterm = st
 
        tk.Frame.__init__(self,master,bd=1,relief='raised',**kw)
 
        l = tk.Label(self,text="sub %r" % self.subterm.submaster.name)
 
        l = tk.Label(self,
 
                     text="sub %s" % graph.label(self.subterm.submaster.uri))
 
        l.pack(side='left')
 
        sev=Subexprview(self,self.subterm.subexpr)
 
        sev.pack(side='left',fill='both',exp=1)
 

	
 
class Output:
 
    lastsendtime=0
 
    lastsendlevs=None
 
    def __init__(self,subterms,music):
 
        make_attributes_from_args('subterms','music')
 

	
 
        self.recent_t=[]
 
        self.later = None
 
@@ -264,45 +265,50 @@ class SubtermSetView(tk.Frame):
 
        self.cur_row = 0
 
        self.cur_col = 0
 
        self.ncols = 2
 
    def add_subtermview(self, stv):
 
        stv.grid(row=self.cur_row, column=self.cur_col, sticky='news')
 
        self.columnconfigure(self.cur_col, weight=1)
 

	
 
        self.cur_col += 1
 
        self.cur_col %= self.ncols
 
        if self.cur_col == 0:
 
            self.cur_row += 1
 

	
 
def add_one_subterm(graph, sub, curveset, subterms, root, ssv, expr=None):
 
    subname = graph.label(sub)
 
def add_one_subterm(graph, subUri, curveset, subterms, root, ssv, expr=None):
 
    subname = graph.label(subUri)
 
    if expr is None:
 
        expr = '%s(t)' % subname
 

	
 
    term = Subterm(Submaster.Submaster(graph=graph, sub=sub), Subexpr(curveset,expr))
 
    print "req to add %r" % subUri
 
    for s in graph.triples((subUri, None, None)):
 
        print s
 
    term = Subterm(Submaster.Submaster(graph=graph, sub=subUri),
 
                   Subexpr(curveset,expr))
 
    subterms.append(term)
 

	
 
    stv=Subtermview(ssv,term)
 
    stv=Subtermview(ssv, graph, term)
 
    # stv.pack(side='top',fill='x')
 

	
 
    ssv.add_subtermview(stv)
 

	
 
    return term
 

	
 
def sub_commands_tk(master, curveset, subterms, root, ssv, graph):
 
    f=tk.Frame(master,relief='raised',bd=1)
 
    newname = tk.StringVar()
 

	
 
    def add_cmd():
 
        add_one_subterm(graph, newname.get(), curveset, subterms, root, ssv, '')
 
        add_one_subterm(graph, L9['sub/%s' % newname.get()],
 
                        curveset, subterms, root, ssv, '')
 
        newname.set('')
 

	
 
    def reload_subs():
 
        dispatcher.send('reload all subs')
 

	
 
    tk.Button(f, text="reload subs (C-r)", 
 
        command=reload_subs).pack(side='left')
 
    tk.Label(f, text="new subterm named:").pack(side='left')
 
    entry = tk.Entry(f, textvariable=newname)
 
    entry.pack(side='left', fill='x', exp=1)
 
    entry.bind("<Key-Return>", lambda evt: add_cmd())
 

	
 
@@ -326,24 +332,31 @@ def createSubtermGraph(song, subterms):
 
        graph.add((uri, L9['sub'], L9['sub/%s' % subterm.submaster.name]))
 
        graph.add((uri, L9['expression'], Literal(subterm.subexpr.expr)))
 
    return graph
 

	
 
def add_subterms_for_song(graph, song, curveset, subterms, root, ssv):
 
    for st in graph.objects(song, L9['subterm']):
 
        add_one_subterm(graph, graph.value(st, L9['sub']), curveset, subterms,
 
                        root, ssv, graph.value(st, L9['expression']))
 

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

	
 
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):
 
        graph.parse(os.path.join(subsDir, filename), format="n3")
 

	
 
#######################################################################
 
root=tk.Tk()
 
root.tk_setPalette("gray50")
 
toplevelat("curvecalc",root)
 
root.tk_focusFollowsMouse()
 

	
 
parser = optparse.OptionParser()
 
options,args = parser.parse_args()
 

	
 
try:
 
    song = URIRef(args[0])
 
except IndexError:
 
@@ -353,40 +366,44 @@ log.debug("music")
 
music=Music()
 

	
 
zc = Zoomcontrol(root)
 
zc.pack(side='top',fill='x')
 

	
 
curveset = Curveset()
 
csv = Curvesetview(root,curveset)
 
csv.pack(side='top',fill='both',exp=1)
 

	
 
ssv = SubtermSetView(root)
 
ssv.pack(side='top', fill='x')
 

	
 
graph = showconfig.getGraph()
 
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)
 
root.title("Curvemaster 3000MX - %s" % graph.label(song))
 

	
 
musicfilename = showconfig.songOnDisk(song)
 
maxtime = wavelength(musicfilename)
 
dispatcher.send("max time",maxtime=maxtime)
 
dispatcher.connect(lambda: maxtime, "get max time",weak=0)
 
curveset.load(basename=os.path.join(showconfig.curvesDir(), showconfig.songFilenameFromURI(song)))
 
curveset.load(basename=os.path.join(showconfig.curvesDir(),
 
                                    showconfig.songFilenameFromURI(song)))
 

	
 
subterms = []
 
sub_commands_tk(root, curveset, subterms, root, ssv, graph).pack(side='top',fill='x')
 

	
 
try:
 
    g = Graph()
 
    g.parse(graphPathForSubterms(song), format='n3')
 
    add_subterms_for_song(g, song, curveset, subterms, root, ssv)
 
    graph.parse(graphPathForSubterms(song), format='n3')
 
    add_subterms_for_song(graph, song, curveset, subterms, root, ssv)
 
except OSError, e:
 
    print e
 

	
 
log.debug("output")
 
out = Output(subterms, music)
 

	
 
def savekey(*args):
 
    print "saving",song
 
    g = createSubtermGraph(song, subterms)
 
    g.serialize(graphPathForSubterms(song), format="nt")
 

	
 
    curveset.save(basename=os.path.join(showconfig.curvesDir(), showconfig.songFilenameFromURI(song)))
light9/Submaster.py
Show inline comments
 
@@ -36,53 +36,55 @@ class Submaster:
 
          sub - n
 
          name, sub - new 
 
        
 
        """
 
        if name is sub is leveldict is None:
 
            raise TypeError("more args are needed")
 
        if sub is not None:
 
            name = graph.label(sub)
 
        if graph is not None:
 
            # old code was passing leveldict as second positional arg
 
            assert isinstance(graph, Graph)
 
        self.name = name
 
        self.uri = sub
 
        self.temporary = temporary
 
        if leveldict:
 
            self.levels = leveldict
 
        else:
 
            self.levels = {}
 
            self.reload(quiet=True)
 
            self.reload(quiet=True, graph=graph)
 
        if not self.temporary:
 
            dispatcher.connect(self.reload, 'reload all subs')
 
            
 
    def reload(self, quiet=False):
 
    def reload(self, quiet=False, graph=None):
 
        if self.temporary:
 
            return
 
        try:
 
            oldlevels = self.levels.copy()
 
            self.levels.clear()
 
            patchGraph = showconfig.getGraph()
 
            graph = Graph()
 
            graph.parse(showconfig.subFile(self.name), format="nt")
 
            if graph is None:
 
                graph = Graph()
 
                graph.parse(showconfig.subFile(self.name), format="nt")
 
            subUri = L9['sub/%s' % self.name]
 
            for lev in graph.objects(subUri, L9['lightLevel']):
 
                chan = graph.value(lev, L9['channel'])
 
                val = graph.value(lev, L9['level'])
 
                name = patchGraph.label(chan)
 
                self.levels[name] = float(val)
 

	
 
            if (not quiet) and (oldlevels != self.levels):
 
                print "sub %s changed" % self.name
 
        except IOError:
 
            print "Can't read file for sub: %s" % self.name
 
        except IOError, e:
 
            print "Can't read file for sub: %r (%s)" % (self.name, e)
 
    def save(self):
 
        if self.temporary:
 
            print "not saving temporary sub named",self.name
 
            return
 

	
 
        graph = Graph()
 
        subUri = L9['sub/%s' % self.name]
 
        graph.add((subUri, RDFS.label, Literal(self.name)))
 
        for chan in self.levels.keys():
 
            try:
 
                chanUri = Patch.get_channel_uri(chan)
 
            except KeyError:
0 comments (0 inline, 0 general)