# Path: magnate/errors.py
# class MagnateNoSaveGame(MagnateSaveError):
#     """Raised when a save game file is missing"""
#     pass
# 
# Path: magnate/logging.py



import os
from alembic.config import Config
from alembic import command
from sqlalchemy import create_engine
from sqlalchemy import Column, Enum, ForeignKey, Integer, String
from sqlalchemy import UniqueConstraint
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.ext.associationproxy import association_proxy
from sqlalchemy.orm import relationship, sessionmaker
from sqlalchemy_repr import RepresentableBase
from ..errors import MagnateNoSaveGame
from ..logging import log
from . import base_types
from . import data_def
    global engine

    savegame_uri = f'sqlite:///{savegame}'
    flog.debug('Associating savegame with global `engine` var')
    engine = create_engine(savegame_uri)

    game_data = data_def.load_data_definitions(datadir)
    init_savegame(engine, game_data)

    flog.debug('Returning engine')
    return engine


def load_savegame(savegame, datadir):
    """
    Load a game from a savegame file

    This also upgrades the savegame file if it is for an older version.

    :arg savegame: savegame filename to attempt to load
    :arg datadir: Directory where Stellar Magnates data files are located.
        The Alembic files used to upgrade savegames to a new version are located here.
    :raises MagnateNoSaveGame: If the savegame does not exist
    :raises MagnateInvalidSaveGame: If the savegame exists but cannot be processed.
    :returns: The SQLAlchemy engine referencing the file
    """
    flog = log.fields(func='load_savegame')
    flog.fields(savegame=savegame, datadir=datadir).debug('Entering load_savegame')

    if not os.path.exists(savegame):

# Path: magnate/errors.py
# class MagnateNoSaveGame(MagnateSaveError):
#     """Raised when a save game file is missing"""
#     pass
# 
# Path: magnate/logging.py



# Path: magnate/savegame/db.py
import os
from alembic.config import Config
from alembic import command
from sqlalchemy import create_engine
from sqlalchemy import Column, Enum, ForeignKey, Integer, String
from sqlalchemy import UniqueConstraint
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.ext.associationproxy import association_proxy
from sqlalchemy.orm import relationship, sessionmaker
from sqlalchemy_repr import RepresentableBase
from ..errors import MagnateNoSaveGame
from ..logging import log
from . import base_types
from . import data_def

    global engine

    savegame_uri = f'sqlite:///{savegame}'
    flog.debug('Associating savegame with global `engine` var')
    engine = create_engine(savegame_uri)

    game_data = data_def.load_data_definitions(datadir)
    init_savegame(engine, game_data)

    flog.debug('Returning engine')
    return engine


def load_savegame(savegame, datadir):
    """
    Load a game from a savegame file

    This also upgrades the savegame file if it is for an older version.

    :arg savegame: savegame filename to attempt to load
    :arg datadir: Directory where Stellar Magnates data files are located.
        The Alembic files used to upgrade savegames to a new version are located here.
    :raises MagnateNoSaveGame: If the savegame does not exist
    :raises MagnateInvalidSaveGame: If the savegame exists but cannot be processed.
    :returns: The SQLAlchemy engine referencing the file
    """
    flog = log.fields(func='load_savegame')
    flog.fields(savegame=savegame, datadir=datadir).debug('Entering load_savegame')

    if not os.path.exists(savegame):

        raise MagnateNoSaveGame(f'{savegame} does not point to a file')