<commit_before>"""
    sqmpy.job.saga_helper
    ~~~~~

    Provides ways to interact with saga classes
"""
import os
import time
import base64
import datetime
import hashlib

import saga
import flask
import gevent
from flask.ext.login import current_user

import helpers
from ..database import db
from .constants import FileRelation, ScriptType, HPCBackend
from .exceptions import JobManagerException
from .models import StagingFile, JobStateHistory, Job
from .callback import JobStateChangeCallback

__author__ = '<NAME>'


class SagaJobWrapper(object):
    """
    To wrap, initialize and run a saga job.
    """
    def __init__(self, job):
        """
        Init
        :param job: an instance of job model class
        :return:
        """
        self._job_id = job.id
        self._job = job

        # If job has resource_job_id, connect to it
        if job.remote_job_id:
            self._job_service = self.make_job_service(job.resource_endpoint)
            self._saga_job = self._job_service.get_job(job.remote_job_id)
        else:
            # Creating the job service object i.e. the connection
            job.resource_endpoint = \
                get_resource_endpoint(job.resource.url, job.hpc_backend)
            self._job_service = self.make_job_service(job.resource_endpoint)

    def make_job_service(self, endpoint):
        # Create ssh security context
        ctx = None
        if flask.current_app.config.get('SSH_WITH_LOGIN_INFO'):
            ctx = saga.Context('userpass')
            ctx.user_id = current_user.username
            ctx.user_pass = base64.b64decode(flask.session['password'])
        else:
            ctx = saga.Context('ssh')
        session = saga.Session()
        session.add_context(ctx)

        return saga.job.Service(endpoint,
                                session=session)

    def _register_callbacks(self):
        """<commit_msg>Workaround for saga bug radical-cybertools/saga-python#477<commit_after>"""
    sqmpy.job.saga_helper
    ~~~~~

    Provides ways to interact with saga classes
"""
import os
import time
import base64
import datetime
import hashlib

import saga
import flask
import gevent
from flask.ext.login import current_user

import helpers
from ..database import db
from .constants import FileRelation, ScriptType, HPCBackend
from .exceptions import JobManagerException
from .models import StagingFile, JobStateHistory, Job
from .callback import JobStateChangeCallback

__author__ = '<NAME>'


class SagaJobWrapper(object):
    """
    To wrap, initialize and run a saga job.
    """
    def __init__(self, job):
        """
        Init
        :param job: an instance of job model class
        :return:
        """
        self._job_id = job.id
        self._job = job

        # If job has resource_job_id, connect to it
        if job.remote_job_id:
            self._job_service = self.make_job_service(job.resource_endpoint)
            self._saga_job = self._job_service.get_job(job.remote_job_id)
        else:
            # Creating the job service object i.e. the connection
            job.resource_endpoint = \
                get_resource_endpoint(job.resource.url, job.hpc_backend)
            self._job_service = self.make_job_service(job.resource_endpoint)

    def make_job_service(self, endpoint):
        # Create ssh security context
        ctx = None
        if flask.current_app.config.get('SSH_WITH_LOGIN_INFO'):
            ctx = saga.Context('userpass')
            ctx.user_id = current_user.username
            ctx.user_pass = <PASSWORD>64.b64decode(flask.session['password'])
        else:
            ctx = saga.Context('ssh')
        session = saga.Session()
        session.add_context(ctx)
        js = saga.job.Service(endpoint, session=session)
        # TODO: Fix in upstream. Service does not populate adaptor's session.
        js._adaptor._set_session(session)
        return js

    def _register_callbacks(self):
        """
{'fraction_non_alphanumeric': 0.08388074785245073, 'fraction_numerical': 0.0037897928246589186, 'mean_word_length': 2.515985790408526, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 6, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3481818', 'n_tokens_mistral': 1162, 'n_tokens_neox': 1112, 'n_words': 295}