# Bryan Schumaker (4 / 28 / 2011)

import os
from os import path
import cPickle as pickle
import libsaria

USER_DIR  = path.expanduser("~")
PROTO     = pickle.HIGHEST_PROTOCOL

saria = ".saria"
if libsaria.__dev__ == True:
	saria = ".saria-dev"
SARIA_DIR = path.join(USER_DIR, saria)
if path.exists(SARIA_DIR) == False:
	os.mkdir(SARIA_DIR)


def full_path(filename):
	return path.join(SARIA_DIR, filename)

def save_obj(filename, object):
	f = open(full_path(filename), 'w')
	p = pickle.Pickler(f, PROTO)
	p.dump(object)
	f.close()

def load_obj(filename, default):
	file = full_path(filename)
	if path.exists(file) == False:
		return default()
	f = open(file)
	try:
		object = pickle.loads(f.read())
	except:
		object = default()
	f.close()
	return object
