Changeset - ad17b0a09f5c
[Not reviewed]
default
0 1 0
drewp@bigasterisk.com - 14 years ago 2011-06-21 01:58:49
drewp@bigasterisk.com
CC more startup logging, timing option to quit after init
Ignore-this: 6116d151a3c36a7830740a7407d5665c
1 file changed with 13 insertions and 5 deletions:
0 comments (0 inline, 0 general)
bin/curvecalc
Show inline comments
 
@@ -397,48 +397,49 @@ def createSubtermGraph(song, subterms):
 
        graph.add((song, L9['subterm'], uri))
 
        graph.add((uri, RDF.type, L9['Subterm']))
 
        graph.add((uri, RDFS.label, Literal(subterm.submaster.name)))
 
        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, master):
 
    for st in graph.objects(song, L9['subterm']):
 
        log.info("song %s has subterm %s", song, st)
 
        try:
 
            add_one_subterm(graph, graph.value(st, L9['sub']), curveset,
 
                            subterms, master, graph.value(st, L9['expression']))
 
        except rdflib.exceptions.UniquenessError:
 
            print "working around curvecalc save corruption"
 
            # curvecalc put all the expressions on one subterm, which is wrong
 
            for expr in graph.objects(st, L9['expression']):
 
                add_one_subterm(graph, graph.value(st, L9['sub']),
 
                                curveset, subterms, master, expr)
 
                
 

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

	
 
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
 

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

	
 
    curveset.save(basename=os.path.join(showconfig.curvesDir(),
 
                                        showconfig.songFilenameFromURI(song)))
 
    print "saved"
 

	
 
@@ -483,80 +484,84 @@ def setupMenubar(barFrame, root, song, s
 

	
 
    with newMenu("Playback") as m:
 
        m.add_command(label="Play/pause at mouse", accelerator="Ctrl+P", command=notImpl)
 

	
 
    with newMenu("Points") as m:
 
        m.add_command(label="Delete", accelerator="Del", command=notImpl)
 

	
 

	
 
def createHelpLines(root):
 
    for helpline in ["Mousewheel zoom; C-p play/pause music at mouse",
 
                     "Curve point bindings: B1 drag point; C-B1 curve add point; S-B1 sketch points; 1..5 add point at time; B1 drag select points",
 
                     "Available in functions: nsin/ncos period=amp=1; within(a,b) bef(x) aft(x) compare to time; smoove(x) cubic smoothstep; chan(name); curvename(t) eval curve"]:
 
        line = tk.Label(root, text=helpline, font="Helvetica -12 italic",
 
                        anchor='w')
 
        line.pack(side='top',fill='x')
 

	
 
def currentlyPlayingSong():
 
    """ask the music player what song it's on"""
 
    t = jsonlib.read(restkit.Resource(networking.musicUrl()).get("time").body)
 
    if t['song'] is None:
 
        raise ValueError("music player is not playing any song")
 
    return URIRef(t['song'])
 

	
 
def main():
 
    startTime = time.time()
 
    parser = optparse.OptionParser()
 
    parser.set_usage("%prog [opts] [songURI]")
 
    parser.add_option("--sliders", action='store_true',
 
                      help='use hardware sliders')
 
    parser.add_option("--skip-music", action='store_true',
 
                      help="ignore music and smooth_music curve files")
 
    parser.add_option("--debug", action="store_true",
 
                      help="log at DEBUG")
 
    parser.add_option("--startup-only", action='store_true',
 
                      help="quit after loading everything (for timing tests)")
 
    opts, args = parser.parse_args()
 

	
 
    logging.basicConfig(format="%(asctime)s %(levelname)-5s %(name)s %(filename)s:%(lineno)d: %(message)s")
 
    log.setLevel(logging.INFO)
 
    log.setLevel(logging.DEBUG if opts.debug else logging.INFO)
 

	
 

	
 
    log.debug("startup: music %s", time.time() - startTime)
 
    try:
 
        song = URIRef(args[0])
 
    except IndexError:
 
        song = currentlyPlayingSong()
 

	
 
    log.debug("music")
 
    music=Music()
 
    graph = makeGraph()
 
    curveset = Curveset(sliders=opts.sliders)
 
    subterms = []
 

	
 
    subtermPath = graphPathForSubterms(song)
 
    try:
 
        graph.parse(subtermPath, format='n3')
 
    except urllib2.URLError, e:
 
        if e.reason.errno != 2:
 
            raise
 
        log.info("%s not found, starting with empty graph" % subtermPath)
 
    
 
    log.debug("output")
 
    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)
 

	
 

	
 
    root=tk.Tk()
 
    root.tk_setPalette("gray50")
 
    toplevelat("curvecalc",root)
 
    root.tk_focusFollowsMouse()
 
    root.title("curvecalc - %s" % graph.label(song))
 

	
 
    menubar = tk.Frame(root)
 
    menubar.pack(side='top', fill='x')
 

	
 
    if 'fixed top rows':
 
        zc = Zoomcontrol(root)
 
        zc.pack(side='top', fill='x')
 

	
 
    if 'panes':
 
        panes = tk.PanedWindow(root, height=1)
 
        panes.add('curvesetView')
 
        panes.add('subterms')
 
@@ -580,28 +585,31 @@ def main():
 
        helpBox = tk.Frame(root)
 
        createHelpLines(helpBox)
 
        helpBox.pack(side='top', fill='x')
 

	
 
    add_subterms_for_song(graph, song, curveset, subterms,
 
                          subtermScroll.subwidget('window'))
 
    setupKeyBindings(root, song, subterms, curveset)
 
    setupMenubar(menubar, root, song, subterms, curveset)
 

	
 
    # curvesetview must already exist, since this makes 'add_curve'
 
    # signals for all the initial curves
 
    curveset.load(basename=os.path.join(showconfig.curvesDir(),
 
                                        showconfig.songFilenameFromURI(song)),
 
                  skipMusic=opts.skip_music)
 
    
 
    dispatcher.send("max time",maxtime=maxtime)
 
    dispatcher.send("show all")
 
    
 
    # this is scheduled after some tk shuffling, to try to minimize
 
    # the number of times we redraw the curve at startup. If tk is
 
    # very slow, it's ok. You'll just get some wasted redraws.
 
    reactor.callLater(.1, curvesetView.goLive)
 

	
 
    tksupport.install(root, ms=10)
 
    log.debug("run")
 
    log.debug("startup: run %s", time.time() - startTime)
 
    if opts.startup_only:
 
        log.debug("quitting now because of --startup-only")
 
        return
 
    prof.run(reactor.run, profile=False)
 

	
 
main()
0 comments (0 inline, 0 general)