<s> import urllib2 <EOL> import logging <EOL> import pprint <EOL> from jenkinsapi import config <EOL> from jenkinsapi . utils . retry import retry_function <EOL> log = logging . getLogger ( __name__ ) <EOL> class JenkinsBase ( object ) : <EOL> """<STR_LIT>""" <EOL> RETRY_ATTEMPTS = <NUM_LIT:5> <EOL> def __repr__ ( self ) : <EOL> return """<STR_LIT>""" % ( self . __class__ . __module__ , <EOL> self . __class__ . __name__ , <EOL> str ( self ) ) <EOL> def print_data ( self ) : <EOL> pprint . pprint ( self . _data ) <EOL> def __str__ ( self ) : <EOL> raise NotImplemented <EOL> def __init__ ( self , baseurl , poll = True , formauth = False ) : <EOL> """<STR_LIT>""" <EOL> self . baseurl = baseurl <EOL> self . formauth = formauth <EOL> if poll and not self . formauth : <EOL> try : <EOL> self . poll ( ) <EOL> except urllib2 . HTTPError , hte : <EOL> log . exception ( hte ) <EOL> log . warn ( "<STR_LIT>" % baseurl ) <EOL> raise <EOL> def poll ( self ) : <EOL> self . _data = self . _poll ( ) <EOL> def _poll ( self ) : <EOL> url = self . python_api_url ( self . baseurl ) <EOL> return retry_function ( self . RETRY_ATTEMPTS , self . get_data , url ) <EOL> def get_jenkins_obj ( self ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplemented ( "<STR_LIT>" ) <EOL> @ classmethod <EOL> def python_api_url ( cls , url ) : <EOL> if url . endswith ( config . JENKINS_API ) : <EOL> return url <EOL> else : <EOL> if url . endswith ( r"<STR_LIT:/>" ) : <EOL> fmt = "<STR_LIT>" <EOL> else : <EOL> fmt = "<STR_LIT>" <EOL> return fmt % ( url , config . JENKINS_API ) <EOL> def get_data ( self , url ) : <EOL> """<STR_LIT>""" <EOL> fn_urlopen = self . get_jenkins_obj ( ) . get_opener ( ) <EOL> try : <EOL> stream = fn_urlopen ( url ) <EOL> result = eval ( stream . read ( ) ) <EOL> except urllib2 . HTTPError , e : <EOL> log . warn ( "<STR_LIT>" % url ) <EOL> log . exception ( e ) <EOL> raise <EOL> return result <EOL> def post_data ( self , url , content ) : <EOL> try : <EOL> urlopen = self . get_jenkins_obj ( ) . get_opener ( ) <EOL> result = urlopen ( url , data = content ) . read ( ) . strip ( ) <EOL> except urllib2 . HTTPError , e : <EOL> log . warn ( "<STR_LIT>" % url ) <EOL> log . exception ( e ) <EOL> raise <EOL> return result <EOL> def hit_url ( self , url ) : <EOL> fn_urlopen = self . get_jenkins_obj ( ) . get_opener ( ) <EOL> try : <EOL> stream = fn_urlopen ( url ) <EOL> html_result = stream . read ( ) <EOL> except urllib2 . HTTPError , e : <EOL> log . debug ( "<STR_LIT>" % url ) <EOL> log . exception ( e ) <EOL> raise <EOL> return html_result </s>
