# -*- coding: utf-8 -*-
"""Provides the Job class."""
__docformat__ = "restructuredtext en"

__copyright__ = """
Copyright (C) 2008-2015 Hendrikx-ITC B.V.

Distributed under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3, or (at your option) any later
version.  The full license is in the file COPYING, distributed as part of
this software.
"""
import json


class Job():
    """
    Represents a Minerva job for processing by a node.
    """
    def __init__(
            self, id_, type_, description, size, created, started,
            finished, job_source_id, state):
        self.id = id_
        self.type = type_
        self.description = description
        self.size = size
        self.created = created
        self.started = started
        self.finished = finished
        self.job_source_id = job_source_id
        self.state = state

    @staticmethod
    def create(job_type, description, size, job_source_id):
        def f(cursor):
            cursor.callproc(
                "system.create_job",
                (job_type, description, size, job_source_id)
            )

            Job.from_record(cursor.fetchone())

        return f

    @staticmethod
    def from_record(record):
        (
            id_, job_type, description, size, created, started, finished,
            job_source_id, state
        ) = record

        return Job(
            id_, job_type, description, size, created, started, finished,
            job_source_id, state
        )

    def finish(self, cursor):
        cursor.callproc("system.finish_job", (self.id,))

    def fail_job(self, message):
        def f(cursor):
            cursor.callproc("system.fail_job", (self.id, message,))

        return f

    def enqueue(self, conn):
        create_job(
            conn, self.type, json.dumps(self.description), self.size,
            self.job_source_id
        )

@@ -402,20 +402,8 @@
 %22%22%22%0A
-import json%0A
 %0A%0Acl
@@ -1565,32 +1565,8 @@
     
-cursor.callproc(%22system.
 fini
@@ -1571,27 +1571,24 @@
 nish_job
-%22, 
 (self.id
 ,))%0A%0A   
@@ -1579,18 +1579,24 @@
 (self.id
-,
 )
+(cursor
 )%0A%0A    d
@@ -1602,20 +1602,16 @@
 def fail
-_job
 (self, m
@@ -1619,24 +1619,88 @@
 ssage):%0A    
+    return fail_job(self.id, message)%0A%0A%0Adef finish_job(job_id):%0A
     def f(cu
@@ -1698,36 +1698,32 @@
  def f(cursor):%0A
-    
         cursor.c
@@ -1743,19 +1743,21 @@
 em.f
-ail
+inish
 _job%22, (
 self
@@ -1756,203 +1756,157 @@
 %22, (
-self.id, message,))%0A%0A        return f%0A%0A    def enqueue(self, conn
+job_id,))%0A%0A    return f%0A%0A%0Adef fail_job(job_id, message
 ):%0A    
-  
-  create_job(%0A            conn, self.type, json.dumps(self.description), self.size,%0A            self.job_source_id%0A        )
+def f(cursor):%0A        cursor.callproc(%22system.fail_job%22, (job_id, message,))%0A%0A    return f
 %0A

Make finish_job and fail_job available outside the Job class