# Path: webphamerator/app/sqlalchemy_ext/models.py
# class Database(db.Model):
# class Job(db.Model):
# class JobOrganismToDelete(db.Model):
# class GenbankFile(db.Model):
# class Password(db.Model):
#     def url(self):
#     def mysql_name_for(cls, name):
#     def phamerator_name_for(cls, name):
#     def mysql_name(self):
#     def __repr__(self):
#     def __repr__(self):
#     def __repr__(self):
#     def __repr__(self):
# 
# Path: webphamerator/app/celery_ext/celery_app.py
# def make_celery(app_name=__name__):


import datetime
import os
import pham
from pathlib import Path
from flask import current_app
from pdm_utils import AlchemyHandler
from webphamerator.app.sqlalchemy_ext import models
from webphamerator.app.celery_ext.celery_app import celery as celery_app

def clean_job(job_record):
    if job_record.start_time is None:
        job_record.runtime = (datetime.datetime.utcnow() -
                              job_record.modified)
    else:
        job_record.runtime = (datetime.datetime.utcnow() -
                              job_record.start_time)
    job_record.modified = datetime.datetime.utcnow()
    job_record.seen = False

    # delete genbank files
    for file_record in job_record.genbank_files_to_add.all():
        try:
            os.remove(file_record.filename)
        except IOError:
            pass
        file_record.filename = None

    models.db.session.commit()


def get_alchemist():
    alchemist = AlchemyHandler()
    alchemist.URI = current_app.config["SQLALCHEMY_DATABASE_URI"]

    return alchemist


def get_job(job_id):

# Path: webphamerator/app/sqlalchemy_ext/models.py
# class Database(db.Model):
# class Job(db.Model):
# class JobOrganismToDelete(db.Model):
# class GenbankFile(db.Model):
# class Password(db.Model):
#     def url(self):
#     def mysql_name_for(cls, name):
#     def phamerator_name_for(cls, name):
#     def mysql_name(self):
#     def __repr__(self):
#     def __repr__(self):
#     def __repr__(self):
#     def __repr__(self):
# 
# Path: webphamerator/app/celery_ext/celery_app.py
# def make_celery(app_name=__name__):


# Path: webphamerator/app/celery_ext/tasks.py
import datetime
import os
import pham
from pathlib import Path
from flask import current_app
from pdm_utils import AlchemyHandler
from webphamerator.app.sqlalchemy_ext import models
from webphamerator.app.celery_ext.celery_app import celery as celery_app


def clean_job(job_record):
    if job_record.start_time is None:
        job_record.runtime = (datetime.datetime.utcnow() -
                              job_record.modified)
    else:
        job_record.runtime = (datetime.datetime.utcnow() -
                              job_record.start_time)
    job_record.modified = datetime.datetime.utcnow()
    job_record.seen = False

    # delete genbank files
    for file_record in job_record.genbank_files_to_add.all():
        try:
            os.remove(file_record.filename)
        except IOError:
            pass
        file_record.filename = None

    models.db.session.commit()


def get_alchemist():
    alchemist = AlchemyHandler()
    alchemist.URI = current_app.config["SQLALCHEMY_DATABASE_URI"]

    return alchemist


def get_job(job_id):

    return (models.db.session.query(models.Job)