from jenkinsapi.jenkinsbase import JenkinsBase
from jenkinsapi.job import Job
import urllib

class View(JenkinsBase):

    def __init__(self, url, name, jenkins_obj):
        self.name = name
        self.jenkins_obj = jenkins_obj
        JenkinsBase.__init__(self, url)

    def __str__(self):
        return self.name

    def __getitem__(self, str_job_id ):
        assert isinstance( str_job_id, str )
        api_url = self.python_api_url( self.get_job_url( str_job_id ) )
        return Job( api_url, str_job_id, self.jenkins_obj )

    def keys(self):
        return self.get_job_dict().keys()

    def iteritems(self):
        for name, url in self.get_job_dict().iteritems():
            api_url = self.python_api_url( url )
            yield name, Job( api_url, name, self.jenkins_obj )

    def values(self):
        return [ a[1] for a in self.iteritems() ]

    def items(self):
        return [ a for a in self.iteritems() ]

    def _get_jobs( self ):
        if not self._data.has_key( "jobs" ):
            pass
        else:
            for viewdict in self._data["jobs"]:
                yield viewdict["name"], viewdict["url"]

    def get_job_dict(self):
        return dict( self._get_jobs() )

    def __len__(self):
        return len( self.get_job_dict().keys() )

    def get_job_url( self, str_job_name ):
        try:
            job_dict = self.get_job_dict()
            return job_dict[ str_job_name ]
        except KeyError:
            #noinspection PyUnboundLocalVariable
            all_views = ", ".join( job_dict.keys() )
            raise KeyError("Job %s is not known - available: %s" % ( str_job_name, all_views ) )

    def get_jenkins_obj(self):
        return self.jenkins_obj

    def add_job(self, str_job_name):
        if str_job_name in self.get_job_dict():
            return "Job %s has in View %s" %(str_job_name, self.name)
        elif not self.get_jenkins_obj().has_job(str_job_name):
            return "Job %s is not known - available: %s" % ( str_job_name, ", ".join(self.get_jenkins_obj().get_jobs_list()))
        else:
            data = {
                "description":"",
                "statusFilter":"",
                "useincluderegex":"on",
                "includeRegex":"",
                "columns": [{"stapler-class": "hudson.views.StatusColumn", "kind": "hudson.views.StatusColumn"}, 
                            {"stapler-class": "hudson.views.WeatherColumn", "kind": "hudson.views.WeatherColumn"}, 
                            {"stapler-class": "hudson.views.JobColumn", "kind": "hudson.views.JobColumn"}, 
                            {"stapler-class": "hudson.views.LastSuccessColumn", "kind": "hudson.views.LastSuccessColumn"}, 
                            {"stapler-class": "hudson.views.LastFailureColumn", "kind": "hudson.views.LastFailureColumn"}, 
                            {"stapler-class": "hudson.views.LastDurationColumn", "kind": "hudson.views.LastDurationColumn"}, 
                            {"stapler-class": "hudson.views.BuildButtonColumn", "kind": "hudson.views.BuildButtonColumn"}],
                "Submit":"OK",
                }
            data["name"] = self.name
            for job in self.get_job_dict().keys():
                data[job]='on'
            data[str_job_name] = "on"
            data['json'] = data.copy()
            self.post_data('%sconfigSubmit' % self.baseurl, urllib.urlencode(data))
            return "Job %s is add in View %s successful" % (str_job_name, self.baseurl)

    def id(self):
        """
        Calculate an ID for this object.
        """
        return "%s.%s" % ( self.className, self.name )
@@ -3488,24 +3488,341 @@
 f.baseurl)%0A%0A
+    def _get_nested_views(self):%0A        if not self._data.has_key( %22views%22 ):%0A            pass%0A        else:%0A            for viewdict in self._data%5B%22views%22%5D:%0A                yield viewdict%5B%22name%22%5D, viewdict%5B%22url%22%5D%0A                %0A    def get_nested_view_dict(self):%0A        return dict( self._get_nested_views() )%0A%0A
     def id(s
@@ -3942,12 +3942,13 @@
  self.name )
+%0A

Add support for nested views