<commit_before>import os
from jenkinsapi.jenkins import Jenkins
from collections import namedtuple
import yaml

from .utils import generate_valid_keys

CONFIG_FILE_NAME = ".jenksrc"

STATUS_STRING = "{key}: {host}, {name} (build #{number}) {status}"
LIST_TEMPLATE = "{key} {host} {name}"

KEYS = generate_valid_keys()

UNABLE_TO_FIND_JENKS_CONFIG = """
Unable to find {0} file! Maybe you need to add one?

Jenks searches for files from the current working directory up.

Example .jenksrc:

localhost:
  url: 'http://localhost:8080/'
  jobs:
    - foo
    - bar
""".format(CONFIG_FILE_NAME)


class JenksDataException(Exception):
    """ Wrapper for configs """

JenksJob = namedtuple('JenksJob', 'key, host, name, api_instance')


class JenksData(object):

    def __init__(self, config_dict, write_method=None):
        self.config_dict = config_dict
        self._host_cache = {}
        self._job_cache = {}
        self._jobs = {}
        self._empty_key_index = 0
        for host in sorted(self.config_dict.keys()):
            host_dict = self.config_dict[host]
            host_url = host_dict.get('url', None)
            if 'jobs' in host_dict:
                for job in host_dict['jobs']:
                    self._add_job(host, job, host_url=host_url)

    def _get_job_api_instance(self, host, job_name):
        if host not in self._job_cache:
            self._job_cache[host] = {}
        if job_name not in self._job_cache[host]:
            host_api_instance = self._get_host(host)
            self._job_cache[host][job_name] = host_api_instance[job_name]

        return self._job_cache[host][job_name]

    def _get_host(self, host):
        if host not in self._host_cache:
            self._host_cache[host] = Jenkins(host)
        return self._host_cache[host]

    def job_keys(self):
        """ return a list of jobs """
        return sorted((j for j in self._jobs))

    def jobs(self, job_keys):
        return (self._jobs[key] for key in job_keys)

    def get_jobs_from_arguments(self, job_keys=None, job_code=None):
        """ return a generator for jobs """
        jobs = []
        if job_keys:
            job_keys = job_keys.strip(" :")
            jobs.extend([job for job in self.jobs(job_keys)])
        if job_code:
            host, job_name = job_code.rsplit("/", 1)
            host = self.hosts.get(host, Jenkins(host))
            if host.has_job(job_name):
                job = self.hosts[host][job_name]
                jobs.extend([job])
            else:
                raise JenksDataException(
                    "Could not find Job {0}/{1}!".format(host, job_name))
        return jobs

    def add_job(self, host, job_name):
        """ add a job to the config with <host> and <job_name> """
        pass

    def write(self):<commit_msg>get_jobs_from_arguments succesfully checks the cache<commit_after>import os
from jenkinsapi.jenkins import Jenkins
from collections import namedtuple
import yaml

from .utils import generate_valid_keys

CONFIG_FILE_NAME = ".jenksrc"

STATUS_STRING = "{key}: {host}, {name} (build #{number}) {status}"
LIST_TEMPLATE = "{key} {host} {name}"

KEYS = generate_valid_keys()

UNABLE_TO_FIND_JENKS_CONFIG = """
Unable to find {0} file! Maybe you need to add one?

Jenks searches for files from the current working directory up.

Example .jenksrc:

localhost:
  url: 'http://localhost:8080/'
  jobs:
    - foo
    - bar
""".format(CONFIG_FILE_NAME)


class JenksDataException(Exception):
    """ Wrapper for configs """

JenksJob = namedtuple('JenksJob', 'key, host, name, api_instance')


class JenksData(object):

    def __init__(self, config_dict, write_method=None):
        self.config_dict = config_dict
        self._host_cache = {}
        self._job_cache = {}
        self._jobs = {}
        self._empty_key_index = 0
        for host in sorted(self.config_dict.keys()):
            host_dict = self.config_dict[host]
            host_url = host_dict.get('url', None)
            if 'jobs' in host_dict:
                for job in host_dict['jobs']:
                    self._add_job(host, job, host_url=host_url)

    def _get_job_api_instance(self, host, job_name):
        if host not in self._job_cache:
            self._job_cache[host] = {}
        if job_name not in self._job_cache[host]:
            host_api_instance = self._get_host(host)
            self._job_cache[host][job_name] = host_api_instance[job_name]

        return self._job_cache[host][job_name]

    def _get_host(self, host):
        if host not in self._host_cache:
            self._host_cache[host] = Jenkins(host)
        return self._host_cache[host]

    def job_keys(self):
        """ return a list of jobs """
        return sorted((j for j in self._jobs))

    def jobs(self, job_keys):
        return (self._jobs[key] for key in job_keys)

    def get_jobs_from_arguments(self, job_keys=None, job_code=None):
        """ return a generator for jobs """
        jobs = []
        if job_keys:
            job_keys = job_keys.strip(" :")
            jobs.extend([job for job in self.jobs(job_keys)])
        if job_code:
            host, job_name = job_code.rsplit("/", 1)
            host_url = self.config_dict.get(host, {}).get('url', host)
            host = self._get_host(host_url)
            if host.has_job(job_name):
                jobs.append(JenksJob(None, host, job_name,
                                     lambda: self._get_job_api_instance(host_url, job_name)))
            else:
                raise JenksDataException(
                    "Could not find Job {0}/{1}!".format(host, job_name))
        return jobs

    def add_job(self, host, job_name):
        """ add a job to the config with <host> and <job_name> """
        pass

    def write(self):
{'fraction_non_alphanumeric': 0.11141964595626519, 'fraction_numerical': 0.0031239153071850054, 'mean_word_length': 2.4927272727272727, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6907477', 'n_tokens_mistral': 1865, 'n_tokens_neox': 1789, 'n_words': 453}