From: lechat Date: Tue, 9 Jul 2013 17:12:30 +0000 (+0800) Subject: Added another way of adding a view and example for it X-Git-Tag: v0.2.23~109^2~3 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=e398c04c0c4d7ac73d9ebb04617b39d6f91416e8;p=tools%2Fpython-jenkinsapi.git Added another way of adding a view and example for it --- diff --git a/examples/how_to/create_nested_views.py b/examples/how_to/create_nested_views.py index 53ad9cb..fab8892 100644 --- a/examples/how_to/create_nested_views.py +++ b/examples/how_to/create_nested_views.py @@ -1,6 +1,7 @@ # This example requires NestedViews plugin to be installed in Jenkins # You need to have at least one job in your Jenkins to see views import logging +from pkg_resources import resource_string from jenkinsapi.view import View from jenkinsapi.views import Views @@ -13,6 +14,10 @@ logger = logging.getLogger() jenkins_url = "http://127.0.0.1:8080/" api = Jenkins(jenkins_url) +jobName = 'foo_job2' +xml = resource_string('examples', 'addjob.xml') +j = api.create_job(jobname=jobName, config=xml) + # Create ListView in main view logger.info('Attempting to create new nested view') top_view = api.views().create('TopView', Views.NESTED_VIEW) @@ -37,9 +42,28 @@ if 'SubView' in top_view.views(): else: logger.info('SubView has been deleted') +# Another way of creating sub view +# This way sub view will have jobs in it +logger.info('Attempting to create view with jobs inside nested view') +top_view.views()['SubView'] = jobName +if 'SubView' not in top_view.views(): + logger.error('View was not created') +else: + logger.info('View has been created') + +logger.info('Attempting to delete sub_view') +del top_view.views()['SubView'] +if 'SubView' in top_view.views(): + logger.error('SubView was not deleted') +else: + logger.info('SubView has been deleted') + logger.info('Attempting to delete top view') del api.views()['TopView'] if 'TopView' not in api.views(): logger.info('View has been deleted') else: logger.error('View was not deleted') + +# Delete job that we created +api.delete_job(jobName) diff --git a/jenkinsapi/views.py b/jenkinsapi/views.py index 9775419..db65d03 100644 --- a/jenkinsapi/views.py +++ b/jenkinsapi/views.py @@ -32,8 +32,15 @@ class Views(object): self[view_name].delete() self.jenkins.poll() - def __setitem__(self, name, value): - raise NotImplementedError() + def __setitem__(self, view_name, job_names_list): + new_view = self.create(view_name) + if isinstance(job_names_list, str): + job_names_list = [job_names_list] + for job_name in job_names_list: + if not new_view.add_job(job_name): + # Something wrong - delete view + del self[new_view] + raise TypeError('Job %s does not exist in Jenkins') def __getitem__(self, view_name): for row in self.jenkins._data['views']: @@ -66,7 +73,7 @@ class Views(object): """ Get the names of all available views """ - for row in self.jenkins._data['views']: + for row in self.jenkins._data.get('views', []): yield row['name'] def keys(self):