diff --git a/libsaria/__init__.py b/libsaria/__init__.py new file mode 100644 index 00000000..39a623eb --- /dev/null +++ b/libsaria/__init__.py @@ -0,0 +1,9 @@ +# Bryan Schumaker (8/7/2010) + +__all__ = ["data", "map", "path"] + + +import map + +# Runtime variables are not saved +vars = map.Map() diff --git a/libsaria/data.py b/libsaria/data.py new file mode 100644 index 00000000..0d11e19b --- /dev/null +++ b/libsaria/data.py @@ -0,0 +1,34 @@ +# Bryan Schumaker (8/7/2010) + +from libsaria import path +import cPickle as pickle + +PROTO = pickle.HIGHEST_PROTOCOL + +def save(item, file, ext="pickle"): + file = "%s%s" % (path.join(path.sariadir(),file), 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" % (path.join(path.sariadir(),file),ext) + return loadfile(file) + + +def loadfile(file): + if path.exists(file) == False: + return + f = open(file) + p = pickle.Unpickler(f) + item = p.load() + f.close() + return item + + diff --git a/libsaria/map.py b/libsaria/map.py new file mode 100644 index 00000000..a84884cc --- /dev/null +++ b/libsaria/map.py @@ -0,0 +1,40 @@ +# Bryan Schumaker (8/7/2010) + +data = None + +class Map(dict): + savefile = None + def __init__(self, file=None): + dict.__init__(self) + self.savefile = file + self.reload() + + def __setitem__(self, key, value): + dict.__setitem__(self, key, value) + self.save() + + def __getitem__(self, key): + return dict.get(self, key, None) + + def has(self,key): + return key in self + + def save(self): + if self.savefile == None: + return + global data + if data == None: + import data + data.save(self, self.savefile) + + def reload(self): + if self.savefile == None: + return + global data + if data == None: + import data + item = data.load(self.savefile) + if item == None: + return + for key in item: + dict.__setitem__(self, key, item[key]) diff --git a/libsaria/path.py b/libsaria/path.py new file mode 100644 index 00000000..9ac01828 --- /dev/null +++ b/libsaria/path.py @@ -0,0 +1,24 @@ +# Bryan Schumaker (8/7/2010) + +import os + +exists = os.path.exists +expand = os.path.expanduser +join = os.path.join +mkdir = os.mkdir +rm = os.remove + +dir +def sariadir(): + global dir + dir = join(expand("~"), ".saria") + if exists(dir) == False: + mkdir(dir) + sariadir = get_sariadir() + return get_sariadir() + + +def get_sariadir(): + global dir + return dir + diff --git a/ocarina/.__init__.py b/ocarina/.__init__.py deleted file mode 100644 index e69de29b..00000000 diff --git a/libsaria/.__init__.py b/ocarina/__init__.py similarity index 100% rename from libsaria/.__init__.py rename to ocarina/__init__.py diff --git a/saria-test.py b/saria-test.py new file mode 100644 index 00000000..7dbfbfb5 --- /dev/null +++ b/saria-test.py @@ -0,0 +1,36 @@ +# Bryan Schumaker (8/7/2010) + +import os +import sys +import datetime +now = datetime.datetime.now + +print "Loading libsaria" + +before = now() +import libsaria +after = now() + +print "libsaria loaded (", after-before, ")" + +if len(sys.argv) == 1: + print + print "Available tests:" + for file in os.listdir("tests"): + split = file.rsplit(".") + if split[1] == "pyc": + continue + if split[0] == "__init__": + continue + print " ", file + + +for file in sys.argv[1:]: + print + print "=====", file, "=====" + print + begin = now() + __import__("tests."+file.rsplit(".")[0]) + end = now() + print + print "=====", file, "(", after-before,") =====" diff --git a/tests/__init__.py b/tests/__init__.py new file mode 100644 index 00000000..8b137891 --- /dev/null +++ b/tests/__init__.py @@ -0,0 +1 @@ + diff --git a/tests/map.py b/tests/map.py new file mode 100644 index 00000000..c539de55 --- /dev/null +++ b/tests/map.py @@ -0,0 +1,47 @@ + +from libsaria import path +from libsaria.map import Map +key = "key" + +# __init__ +test = Map() +print "Testing basic functions (no persistance):" +# has +print " Has key ?", test.has(key) +# __getitem__ +print " test[key] ?", test[key] + +# __setitem__ +print " Setting test[key] = True" +test[key] = True + +# has +print " Has key ?", test.has(key) +# __getitem__ +print " test[key] ?", test['key'] + + +print +print "Testing persistance:" +file = path.join(path.join(path.sariadir(),".saria"),"tests.map.pickle") +try: + path.rm(file) +except: + pass +del test + +test = Map("tests.map") +test["a"] = 0 +test["b"] = 0 +print " print test" +print test +print " del test" +del test +print " test = Map(\"tests.map\")" +test = Map("tests.map") +print " print test" +print test +try: + path.rm(file) +except: + pass