Made views() a property, added safety in case view doesn't have subviews
authorlechat <ctpeko3a-github@gmail.com>
Wed, 10 Jul 2013 15:18:27 +0000 (23:18 +0800)
committerlechat <ctpeko3a-github@gmail.com>
Wed, 10 Jul 2013 15:18:27 +0000 (23:18 +0800)
jenkinsapi/jenkins.py
jenkinsapi/view.py
jenkinsapi/views.py
jenkinsapi_tests/unittests/test_views.py

index 08d917b..8c2b2aa 100644 (file)
@@ -206,6 +206,7 @@ class Jenkins(JenkinsBase):
     def __str__(self):
         return "Jenkins server at %s" % self.baseurl
 
+    @property
     def views(self):
         return Views(self)
 
index 2df75d0..4d96074 100644 (file)
@@ -153,5 +153,6 @@ class View(JenkinsBase):
     def get_nested_view_dict(self):
         return dict(self._get_nested_views())
 
+    @property
     def views(self):
         return self.get_jenkins_obj().get_jenkins_obj_from_url(self.baseurl).views()
index db65d03..b99942e 100644 (file)
@@ -43,7 +43,7 @@ class Views(object):
                 raise TypeError('Job %s does not exist in Jenkins')
 
     def __getitem__(self, view_name):
-        for row in self.jenkins._data['views']:
+        for row in self.jenkins._data.get('views', []):
             if row['name'] == view_name:
                 return View(
                     row['url'],
@@ -57,7 +57,7 @@ class Views(object):
         Get the names & objects for all views
         """
         self.jenkins.poll()
-        for row in self.jenkins._data['views']:
+        for row in self.jenkins._data.get('views', []):
             name = row['name']
             url = row['url']
 
index c2e0528..f7a26d3 100644 (file)
@@ -25,7 +25,7 @@ class TestViews(unittest.TestCase):
                     username='foouser', password='foopassword',
                     requester=mock_requester)
 
-        new_view = J.views().create('NewView')
+        new_view = J.views.create('NewView')
         self.assertTrue(isinstance(new_view, View))
         self.assertEquals(new_view.baseurl,
                 'http://localhost:8080/views/NewView')
@@ -55,7 +55,7 @@ class TestViews(unittest.TestCase):
                     username='foouser',
                     password='foopassword')
 
-            new_view = J.views().create('NewView')
+            new_view = J.views.create('NewView')
 
             self.assertIsInstance(new_view, View)