From e0f4eb45bb84859c2f066ab901ea88ee406797dd Mon Sep 17 00:00:00 2001 From: bjschuma Date: Wed, 31 Mar 2010 23:03:54 -0400 Subject: [PATCH] Added system calls and documentation. Read a config file on ocarina startup --- nbproject/project.properties | 2 +- src/config.py | 3 ++ src/core/cli.py | 6 +++- src/core/coredefaults.py | 13 ++++++-- src/core/ct/__init__.py | 3 +- src/core/ct/call.py | 61 ++++++++++++++++++++++++++++++++++-- src/core/ct/message.py | 15 ++++++++- src/core/ct/times.py | 41 ++++++++++++++++++++++++ src/core/gstreamer.py | 5 +-- src/core/ocarina-core.py | 2 ++ src/core/ocarina.py | 31 ++++++++---------- 11 files changed, 152 insertions(+), 30 deletions(-) create mode 100644 src/config.py create mode 100644 src/core/ct/times.py diff --git a/nbproject/project.properties b/nbproject/project.properties index 596eddd7..0de694e7 100644 --- a/nbproject/project.properties +++ b/nbproject/project.properties @@ -1,4 +1,4 @@ java.lib.path= platform.active=Python_2.6.4 -python.lib.path= +python.lib.path=/home/bjschuma/Programs/ocarina/src/core|/home/bjschuma/Programs/ocarina/src/core/ct|/home/bjschuma/Programs/ocarina/src/extra| src.dir=src diff --git a/src/config.py b/src/config.py new file mode 100644 index 00000000..5537c2d7 --- /dev/null +++ b/src/config.py @@ -0,0 +1,3 @@ + +from ocarina import vars +vars["$verbose"] = 4 \ No newline at end of file diff --git a/src/core/cli.py b/src/core/cli.py index 03480c64..b0950ef5 100644 --- a/src/core/cli.py +++ b/src/core/cli.py @@ -11,13 +11,17 @@ import rlcompleter import ocarina readline.parse_and_bind("tab:complete") + from ct.call import * +from ct.message import * def loop(): while True: try: + enable() exec raw_input(ocarina.vars["$prompt"] + " ") + restore() # Catch this so that we can use ctrl-d to exit except EOFError: @@ -27,4 +31,4 @@ def loop(): print e -ocarina.events.invite("ocarina-start",loop) \ No newline at end of file +#ocarina.events.invite("ocarina-start",loop) \ No newline at end of file diff --git a/src/core/coredefaults.py b/src/core/coredefaults.py index 383e401b..fd0e7669 100644 --- a/src/core/coredefaults.py +++ b/src/core/coredefaults.py @@ -8,6 +8,7 @@ __date__ ="$Mar 15, 2010 9:53:46 PM$" import ocarina from ct import path +from ct import opts from ocarina import vars @@ -20,7 +21,13 @@ vars["$playonload"] = True vars["$playing"] = False -ocarina.init() -import scripting -import manager +opts.parse() +path.mkdir(vars["$ocarina"]) + +# Set verbose value +if opts.opts.has("v") == True: + vars["$verbose"] += opts.opts["v"] +if opts.opts.has("verbose") == True: + vars["$verbose"] += opts.opts["verbose"] + import gstreamer diff --git a/src/core/ct/__init__.py b/src/core/ct/__init__.py index cb0d289f..63529afb 100644 --- a/src/core/ct/__init__.py +++ b/src/core/ct/__init__.py @@ -2,4 +2,5 @@ __author__="bjschuma" __date__ ="$Mar 13, 2010 4:20:16 PM$" -__all__ = ["call", "cmd", "dict", "message", "opts", "path", "plugin","slist"] \ No newline at end of file +__all__ = ["call", "cmd", "dict", "message", "opts", "path", + "plugin", "slist", "times"] \ No newline at end of file diff --git a/src/core/ct/call.py b/src/core/ct/call.py index 1a480775..fb08d05c 100644 --- a/src/core/ct/call.py +++ b/src/core/ct/call.py @@ -1,33 +1,88 @@ #! /usr/bin/python -# To change this template, choose Tools | Templates -# and open the template in the editor. +'''These are core Ocarina system calls that should help in both development +of future Ocarina features and in usage of ocarina-core''' __author__="bjschuma" __date__ ="$Mar 30, 2010 11:24:43 PM$" import ocarina +from ct import message +from ct import times +from ct import path + def load(path): + '''Load the song located at path''' import gstreamer gstreamer.load(path) def seek(prcnt): + ''' Seek to prcnt% of the song. If 0 < prcnt < 1, then we will seek to + (prcnt * 100)% of song's duration. If prcnt < 100, then we will seek + to prcnt% of the song's duration.''' import gstreamer - gstreamer.seek(prcnt) + good = gstreamer.seek(prcnt) + if good == -1: + message.write("There was an error seeking to: "+str(prcnt)) + + +def progress(): + '''Return the fraction of the song that we have already played''' + import gstreamer + progress = gstreamer.getProgress() + message.write(progress) + return progress + + +def duration(): + '''Return the total duration of the song. If message printing is enabled, + then we will also print out string representing the duration in + hh:mm:ss form''' + import gstreamer + duration = gstreamer.duration() + message.write(times.ms2str(duration)) + return duration + + +def time(): + '''Returns how far into the song gstreamer currently is. If message printing + is enabled, then we will also print out a string representing the duration in + hh:mm:ss form''' + import gstreamer + time = gstreamer.currentpos() + message.write(times.ms2str(time)) + return time + + +def gsstate(): + '''Returns the current gstreamer state''' + import gstreamer + state = gstreamer.getstate() + message.write(state) + return state def play(): + '''Begin playback of the loaded song''' ocarina.events.start("ocarina-play") def pause(): + '''Pause playback of the current song''' ocarina.events.start("ocarina-pause") def stop(): + '''Pause playback of the current song and seek to the beginning''' pause() seek(0) + +def pyfile(file): + '''If file exists, try to execute it as a python script''' + if path.exists(file) == True: + message.write("Running script: "+file,1) + execfile(file) \ No newline at end of file diff --git a/src/core/ct/message.py b/src/core/ct/message.py index 54a2deb4..3ebf5829 100644 --- a/src/core/ct/message.py +++ b/src/core/ct/message.py @@ -4,8 +4,11 @@ __author__="bjschuma" __date__ ="$Mar 13, 2010 4:32:22 PM$" import ocarina + global enabled +global lastState enabled = True +lastState = True def write(s,verbose=0): @@ -17,9 +20,19 @@ def write(s,verbose=0): def disable(): global enabled + global lastState + lastState = enabled enabled = False def enable(): global enabled - enabled = True \ No newline at end of file + global lastState + lastState = enabled + enabled = True + + +def restore(): + global enabled + global lastState + enabled = lastState \ No newline at end of file diff --git a/src/core/ct/times.py b/src/core/ct/times.py new file mode 100644 index 00000000..6a7c186d --- /dev/null +++ b/src/core/ct/times.py @@ -0,0 +1,41 @@ +#! /usr/bin/python + +# To change this template, choose Tools | Templates +# and open the template in the editor. + +__author__="bjschuma" +__date__ ="$Mar 16, 2010 7:36:48 PM$" + + +def ftime(time): + time = int(time) + + #print time + # Find hour + length = "" + if time >= 3600: + hour = time/3600 + time = time - (hour * 3600) + if hour > 0: + length=str(hour)+":" + # Find minute + if time >= 60: + min = time/60 + time = time - (min * 60) + if min < 10: + length+="0" + length+=str(min)+":" + else: + length+="00:" + # Remainder is seconds + sec = time + if sec < 10: + length+="0" + length+=str(sec) + return length + + +def ms2str(ms): + # Convert ms to s + time = int(ms) / 1000000000 + return ftime(time) diff --git a/src/core/gstreamer.py b/src/core/gstreamer.py index 0507ed63..2c00ac37 100644 --- a/src/core/gstreamer.py +++ b/src/core/gstreamer.py @@ -142,12 +142,13 @@ def seek(prcnt,fraction=False): if fraction == False: prcnt = float(prcnt) if prcnt < 0: - return + return -1 elif prcnt > 100: - return + return -1 prcnt = prcnt / 100.0 newTime = duration() * prcnt player.seek_simple(time,gst.SEEK_FLAG_FLUSH,newTime) + return 0 bus.add_signal_watch() diff --git a/src/core/ocarina-core.py b/src/core/ocarina-core.py index 1bde7402..fabdb2db 100644 --- a/src/core/ocarina-core.py +++ b/src/core/ocarina-core.py @@ -17,6 +17,8 @@ def main(): write("Welcome to Ocarina (core)", 1) ocarina.events.start("ocarina-start") + ocarina.config() + cli.loop() if __name__ == "__main__":main() diff --git a/src/core/ocarina.py b/src/core/ocarina.py index 11caa98f..45c0273a 100644 --- a/src/core/ocarina.py +++ b/src/core/ocarina.py @@ -5,32 +5,27 @@ __date__ ="$Mar 13, 2010 4:19:39 PM$" from ct.dict import Dict -from ct import opts from ct import path -from ct import call import event - -global settings global vars global events -global plugins -global alias -settings = Dict() +#settings = Dict() vars = Dict() -alias = Dict() +#alias = Dict() events = event.Event() plugins = None +'''Attempt to read in a configuration file''' +def config(): + global vars + from ct.call import pyfile + config = "config.py" + pyfile(path.join(vars["$ocarina"],config)) + pyfile(config) -# Set default values -def init(): - opts.parse() - path.mkdir(vars["$ocarina"]) - - # Set verbose value - if opts.opts.has("v") == True: - vars["$verbose"] += opts.opts["v"] - if opts.opts.has("verbose") == True: - vars["$verbose"] += opts.opts["verbose"] + #config = "config.py" + #config = path.join(vars["$ocarina"],"config.py") + #from ct.call import pyfile + #pyfile(config) \ No newline at end of file