# Path: sacred/dependencies.py
# def get_digest(filename):
#     """Compute the MD5 hash for a given file."""
#     h = hashlib.md5()
#     with open(filename, "rb") as f:
#         data = f.read(1 * MB)
#         while data:
#             h.update(data)
#             data = f.read(1 * MB)
#         return h.hexdigest()
# 
# Path: sacred/serializer.py
# def restore(flat):
#     return json.decode(_json.dumps(flat), keys=True)


import hashlib
import json
import os
import sqlalchemy as sa
from sqlalchemy.ext.declarative import declarative_base
from sacred.dependencies import get_digest
from sacred.serializer import restore




Base = declarative_base()


class Source(Base):
    __tablename__ = "source"

    @classmethod
    def get_or_create(cls, filename, md5sum, session, basedir):
        instance = (
            session.query(cls).filter_by(filename=filename, md5sum=md5sum).first()
        )
        if instance:
            return instance
        full_path = os.path.join(basedir, filename)

# Path: sacred/dependencies.py
# def get_digest(filename):
#     """Compute the MD5 hash for a given file."""
#     h = hashlib.md5()
#     with open(filename, "rb") as f:
#         data = f.read(1 * MB)
#         while data:
#             h.update(data)
#             data = f.read(1 * MB)
#         return h.hexdigest()
# 
# Path: sacred/serializer.py
# def restore(flat):
#     return json.decode(_json.dumps(flat), keys=True)


# Path: sacred/observers/sql_bases.py
import hashlib
import json
import os
import sqlalchemy as sa
from sqlalchemy.ext.declarative import declarative_base
from sacred.dependencies import get_digest
from sacred.serializer import restore





Base = declarative_base()


class Source(Base):
    __tablename__ = "source"

    @classmethod
    def get_or_create(cls, filename, md5sum, session, basedir):
        instance = (
            session.query(cls).filter_by(filename=filename, md5sum=md5sum).first()
        )
        if instance:
            return instance
        full_path = os.path.join(basedir, filename)

        md5sum_ = get_digest(full_path)