Clean up universal_open()

The cleaned up version chooses what function to call based on file
extention.  I use map.get() to pick a function, and use the open_music()
function as the default.
This commit is contained in:
Bryan Schumaker 2010-12-27 12:06:22 -05:00
parent 961eacf3c4
commit 0c636bfd56

View File

@ -4,6 +4,7 @@ import libsaria
from libsaria import path
import cPickle as pickle
plugin = None
export = None
dev = ""
if libsaria.__dev__ == True:
@ -15,19 +16,16 @@ def save(item, file, ext=".pickle"):
file = "%s%s%s" % (path.join(path.saria_dir(),file), dev, ext)
savefile(item, file)
def savefile(item, file):
f = open(file, 'w')
p = pickle.Pickler(f, PROTO)
p.dump(item)
f.close()
def load(file, ext=".pickle"):
file = "%s%s%s" % (path.join(path.saria_dir(),file), dev, ext)
return loadfile(file)
def loadfile(file):
if path.exists(file) == False:
return
@ -37,29 +35,32 @@ def loadfile(file):
f.close()
return item
def universal_open(file):
def open_plugin(file):
global plugin
if plugin == None:
from libsaria import plugin
plugin.install(file)
def open_xml(file):
global export
if export == None:
import export
export.import_xml(file)
def open_music(file):
try:
from libsaria.sources import file as file_source
file_source.load_file(file)
except Exception, e:
print e
mapping = {".py":open_plugin, ".xml":open_xml}
def universal_open(file):
file = file.replace("%20", " ")
file = file.replace("%22", "\"")
if path.is_dir(file):
libsaria.sources.new_source(file)
return
split = path.splitext(file)
ext = split[1]
# Install and start a plugin
if ext == ".py":
if plugin == None:
from libsaria import plugin
plugin.install(file)
return
if ext == ".xml":
import export
export.import_xml(file)
return
try:
from libsaria.sources import file as file_source
file_source.load_file(file)
except Exception,e:
print e
pass
base, ext = path.splitext(file)
func = mapping.get(ext, open_music)
func(file)