From df13f22db40e187ad6825d03a14ec1c865da0845 Mon Sep 17 00:00:00 2001 From: Salim Fadhley Date: Sun, 16 Jun 2013 23:25:51 +0100 Subject: [PATCH] initial refactoring of queues --- jenkinsapi/exceptions.py | 5 +++++ jenkinsapi/jenkins.py | 1 - jenkinsapi/queue.py | 33 +++++++++++++++++++++++++++++---- 3 files changed, 34 insertions(+), 5 deletions(-) diff --git a/jenkinsapi/exceptions.py b/jenkinsapi/exceptions.py index 7c681e6..04cf7cb 100644 --- a/jenkinsapi/exceptions.py +++ b/jenkinsapi/exceptions.py @@ -18,6 +18,11 @@ class UnknownView( KeyError, JenkinsAPIException): Jenkins does not recognize the view requested. """ +class UnknownQueueItem( KeyError, JenkinsAPIException): + """ + Jenkins does not recognize the requested queue item + """ + class ArtifactBroken(JenkinsAPIException): """ An artifact is broken, wrong diff --git a/jenkinsapi/jenkins.py b/jenkinsapi/jenkins.py index b9b2cbb..745f828 100644 --- a/jenkinsapi/jenkins.py +++ b/jenkinsapi/jenkins.py @@ -1,7 +1,6 @@ import json import urllib import logging -import urlparse from jenkinsapi import config from jenkinsapi.job import Job diff --git a/jenkinsapi/queue.py b/jenkinsapi/queue.py index 164c81c..baca7e5 100644 --- a/jenkinsapi/queue.py +++ b/jenkinsapi/queue.py @@ -1,4 +1,5 @@ from jenkinsapi.jenkinsbase import JenkinsBase +from jenkinsapi.exceptions import UnknownQueueItem import logging import urlparse import urllib2 @@ -19,10 +20,29 @@ class Queue(JenkinsBase): self.jenkins = jenkins_obj JenkinsBase.__init__(self, baseurl) + def __str__(self): + return self.baseurl + def get_jenkins_obj(self): return self.jenkins - def get_queue_items(self, job_name = None): + def iteritems(self): + for item in self._data['items']: + yield item['id'], QueueItem(self.jenkins, **item) + + def keys(self): + return [i[0] for i in self.iteritems()] + + def __len__(self): + return len(self._data['items']) + + def __getitem__(self, item_id): + for id, item in self.iteritems(): + if id == item_id: + return item + raise UnknownQueueItem(item_id) + + def get_queue_items_for_job(self, job_name): if not job_name: return [QueueItem(**item) for item in self._data['items']] else: @@ -49,7 +69,12 @@ class QueueItem(object): those changes """ - def __init__(self, **kwargs): - for arg in kwargs: - setattr(self, arg, kwargs[arg]) + def __init__(self, jenkins, **kwargs): + self.jenkins = jenkins + self.__dict__.update(kwargs) + def getJob(self): + """ + Return the job associated with this queue item + """ + return self.jenkins[self.task['name']] \ No newline at end of file -- 2.7.4