示例#1
0
 def __enter__(self):
     """
     Open the output database, in preparation to store recipe data.
     :return: self, if the file was opened successfully, None if not.
     """
     self.__database = RecipeDB()
     return self
示例#2
0
class SqlRecipeStorage(RecipeStorage):
    """
    A SqlRecipeStorage object will save storage_item.RecipeStorage objects
    the custom food storage database.
    """
    def __init__(self, dbpath):
        """
        Initialize this object to store recipe data in a database file at a
        location, given a file/path name.

        :param dbpath: A string object, containing the pathname of the database
        where all serialization routines will be performed during a call to
        "self.write()".
        """
        dbpath = RecipeDB.DATABASE_PATH  # because you just can't trust people.
        RecipeStorage.__init__(self, dbpath)
        self.__database = None

    def __enter__(self):
        """
        Open the output database, in preparation to store recipe data.
        :return: self, if the file was opened successfully, None if not.
        """
        self.__database = RecipeDB()
        return self

    def __exit__(self, exc_type, exc_val, exc_tb):
        """
        Close the output, preventing any more data writes.
        """
        self.__database.close()

    def get_storage_name(self):
        return RecipeDB.DATABASE_PATH

    def write(self, recipeitem):
        """
        Write out the data from a storage_items.RecipeStorage to a file.

        :param recipeitem: storage_items.RecipeStorage
        """
        self.__database.insert_recipe(recipeitem)