# 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
    version = sa.Column(sa.String(16))

    def to_json(self):
        return "{}=={}".format(self.name, self.version)


class Artifact(Base):
    __tablename__ = "artifact"

    @classmethod
    def create(cls, name, filename):
        with open(filename, "rb") as f:
            return cls(filename=name, content=f.read())

    artifact_id = sa.Column(sa.Integer, primary_key=True)
    filename = sa.Column(sa.String(64))
    content = sa.Column(sa.LargeBinary)

    run_id = sa.Column(sa.String(24), sa.ForeignKey("run.run_id"))
    run = sa.orm.relationship("Run", backref=sa.orm.backref("artifacts"))

    def to_json(self):
        return {"_id": self.artifact_id, "filename": self.filename}


class Resource(Base):
    __tablename__ = "resource"

    @classmethod
    def get_or_create(cls, filename, session):

# 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

    version = sa.Column(sa.String(16))

    def to_json(self):
        return "{}=={}".format(self.name, self.version)


class Artifact(Base):
    __tablename__ = "artifact"

    @classmethod
    def create(cls, name, filename):
        with open(filename, "rb") as f:
            return cls(filename=name, content=f.read())

    artifact_id = sa.Column(sa.Integer, primary_key=True)
    filename = sa.Column(sa.String(64))
    content = sa.Column(sa.LargeBinary)

    run_id = sa.Column(sa.String(24), sa.ForeignKey("run.run_id"))
    run = sa.orm.relationship("Run", backref=sa.orm.backref("artifacts"))

    def to_json(self):
        return {"_id": self.artifact_id, "filename": self.filename}


class Resource(Base):
    __tablename__ = "resource"

    @classmethod
    def get_or_create(cls, filename, session):

        md5sum = get_digest(filename)