#SaveHandler.py
#Handles saving and loading of game data
#Created by: Andrew Davis
#Created on: 1/9/2016
#Open source (MIT license)

#import statement
import pickle

#singleton class definition
class SaveHandler(object):

    #private inner class (handles method stuff)
    class __SaveHandler(object):

        #inner class constructor
        def __init__(self):
            pass #no code needed

        #methods

        #save method - saves an object to a file
        def save_int(self, obj, fname):
            pickle.dump(obj, open(fname, "wb")) #save the object to a file

        #load method - loads an object from a file
        def load_int(self, fname):
            return pickle.load(open(fname, "rb")) #read the object from the file

    #private static instance variable
    __instance = None

    #constructor for outer class
    def __init__(self):
        if not SaveHandler.__instance:
            SaveHandler.__instance = SaveHandler.__SaveHandler() #init the instance field
        else:
            pass #no vars to init

    #save method - saves an object to a file
    def save(self, obj, fname):
        self.__instance.save_int(obj, fname) #use the internal save

    #load method - loads an object from a file
    def load(self, fname):
        return self.__instance.load_int(fname) #use the internal load
