<commit_before>import ast
import urllib

def color_to_status(colour):
    status = dict()

    data = colour.split('_')

    status['inprogress'] = not len(data) == 1

    if data[0] == 'red':
        status['result'] = 'failed'
    if data[0] == 'yellow':
        status['result'] = 'unstable'
    if data[0] == 'blue':
        status['result'] = 'passed'

    return status

class JenkinsClient(object):
    def __init__(self, server_url, jobs=None):
        self._url = server_url + '/api/python'

        self._last_jobs = dict()
        self._subscribed_jobs = jobs

        # Default to subscribing to all jobs
        if self._subscribed_jobs is None:
            self._subscribed_jobs = self.get_all_job_names()

    def get_data(self, depth):
        url = self._url + '?depth=' + str(depth)
        return ast.literal_eval(urllib.urlopen(url).read())

    def get_job(self, job_name, data):
        all_jobs = data['jobs']
        try:
            job = (j for j in all_jobs if j['name'] == job_name).next()
        except StopIteration:
            raise RuntimeError

        return job

    def get_all_job_names(self, data=None):
        if data is None:
            data = self.get_data(0)

        return [job['name'] for job in data['jobs']]

    def get_job_status(self, job_name, data=None):
        if data is None:
            data = self.get_data(0)

        job = self.get_job(job_name, data)
        colour = job['color']

        status = color_to_status(colour)

        return status

    def get_job_health(self, job_name, data=None):
        if data is None:
            data = self.get_data(1)

        job = self.get_job(job_name, data)

        return job['healthReport']

    def get_subscribed_jobs(self, data=None):
        if data is None:
            data = self.get_data(1)

        jobs = dict()<commit_msg>Improved Jenkis client speed, added culprit reporting<commit_after>import ast
import urllib

def color_to_status(colour):
    status = dict()

    data = colour.split('_')

    status['inprogress'] = not len(data) == 1

    if data[0] == 'red':
        status['result'] = 'failed'
    if data[0] == 'yellow':
        status['result'] = 'unstable'
    if data[0] == 'blue':
        status['result'] = 'passed'

    return status

class JenkinsClient(object):
    def __init__(self, server_url, jobs=None):
        self._url = server_url + '/api/python'

        self._last_jobs = dict()
        self._subscribed_jobs = jobs

        # Default to subscribing to all jobs
        if self._subscribed_jobs is None:
            self._subscribed_jobs = self.get_all_job_names()

    def get_data(self):
        url = self._url + '?pretty=true&depth=2&tree=jobs[name,color,healthReport[*],lastBuild[culprits[fullName]]]'
        return ast.literal_eval(urllib.urlopen(url).read())

    def get_job(self, job_name, data):
        all_jobs = data['jobs']
        try:
            job = (j for j in all_jobs if j['name'] == job_name).next()
        except StopIteration:
            raise RuntimeError

        return job

    def get_all_job_names(self, data=None):
        if data is None:
            data = self.get_data()

        return [job['name'] for job in data['jobs']]

    def get_job_status(self, job_name, data=None):
        if data is None:
            data = self.get_data()

        job = self.get_job(job_name, data)
        colour = job['color']

        status = color_to_status(colour)

        job_culprits = job['lastBuild']['culprits']
        culprits = None
        if len(job_culprits) > 0:
            culprits = [c['fullName'] for c in job_culprits]

        return (status, culprits)

    def get_job_health(self, job_name, data=None):
        if data is None:
            data = self.get_data()

        job = self.get_job(job_name, data)

        return job['healthReport']

    def get_subscribed_jobs(self, data=None):
        if data is None:
            data = self.get_data()

        jobs = dict()
{'fraction_non_alphanumeric': 0.12006040775232822, 'fraction_numerical': 0.003523785552479235, 'mean_word_length': 2.3310980720871752, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 4, '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': '4492120', 'n_tokens_mistral': 1320, 'n_tokens_neox': 1236, 'n_words': 311}