pycontribs / jenkinsapi

A Python API for accessing resources and configuring Hudson & Jenkins continuous-integration servers
http://pypi.python.org/pypi/jenkinsapi
MIT License
859 stars 485 forks source link

Provide "public" getter-methods #665

Closed Klaus-Valse closed 6 years ago

Klaus-Valse commented 6 years ago
ISSUE TYPE
Jenkinsapi 0.3.6
Jenkins 1.658
SUMMARY

Hey!

Disclaimer: I'm far from being a Python expert, so chances are I didn't get something important. Here's what I wanna do: I wanna select a view, iterate through all the jobs therein and get the console output of each job's last build for later parsing purposes. In views.py there is a method that does just what I need:

 def __getitem__(self, view_name):

The double-leading-underscores tell me that this method is "private", and from what I know it would be very bad practice to get a view via this method, for example it could be gone in a future version and my code wouldn't work anymore... So right now, in order to avoid using this "private" function, I came up with a quite akward solution: projectSelection contains a list of all views

        for s in projectSelection:
            for viewKey, view in jenkins.views.iteritems():
                if viewKey == s:
                    for jobKey, job in view.iteritems():
                        # Further processing...
                        pass 

                else:
                    continue

Thanks for this nice API!

Best regards, Klaus

lechat commented 6 years ago

This method is "magic" one. Read this: https://rszalski.github.io/magicmethods/

As views are acting like dictionaries, your code could look like this:

for s in projectSelection:
    for jobKey, job in jenkins.views[s]:
        # Further processing
        pass
Klaus-Valse commented 6 years ago

Wow, thanks for the quick reply and that cool article, I'll printed it out and will study it :)

Best regards!

Edit: For any future greenhorn running into this problem: @lechat's example does not work like this (assertion error): You have to use the iteritems() method to get the jobs out of the view. So in respect to the snippets above, I got it working for me like this:

            for jobKey, job in jenkins.views[s].iteritems():
                # Further processing
                   pass