<s> import json <EOL> import os <EOL> import logging <EOL> import urllib2 <EOL> from urlparse import urljoin <EOL> class MetricsError ( Exception ) : <EOL> def __init__ ( self , msg , underlying_error ) : <EOL> Exception . __init__ ( self , msg , underlying_error ) <EOL> self . msg = str ( msg ) <EOL> self . underlying_error = str ( underlying_error ) <EOL> def __str__ ( self ) : <EOL> return "<STR_LIT>" % ( self . msg , self . underlying_error ) <EOL> class Backend ( object ) : <EOL> def __init__ ( self , config , logger = logging ) : <EOL> self . config = config <EOL> self . logger = logger <EOL> def download_metrics_json ( self ) : <EOL> url = urljoin ( self . config . graphite_url_server , "<STR_LIT>" ) <EOL> if self . config . graphite_username is not None and self . config . graphite_password is not None : <EOL> username = self . config . graphite_username <EOL> password = self . config . graphite_password <EOL> passmanager = urllib2 . HTTPPasswordMgrWithDefaultRealm ( ) <EOL> passmanager . add_password ( None , url , username , password ) <EOL> authhandler = urllib2 . HTTPBasicAuthHandler ( passmanager ) <EOL> opener = urllib2 . build_opener ( authhandler ) <EOL> urllib2 . install_opener ( opener ) <EOL> response = urllib2 . urlopen ( url ) <EOL> with open ( '<STR_LIT>' % self . config . filename_metrics , '<STR_LIT:w>' ) as m : <EOL> m . write ( response . read ( ) ) <EOL> try : <EOL> os . unlink ( self . config . filename_metrics ) <EOL> except OSError : <EOL> pass <EOL> os . rename ( '<STR_LIT>' % self . config . filename_metrics , self . config . filename_metrics ) <EOL> def load_metrics ( self ) : <EOL> try : <EOL> with open ( self . config . filename_metrics , '<STR_LIT:r>' ) as f : <EOL> metrics = json . load ( f ) <EOL> if len ( metrics ) and metrics [ <NUM_LIT:0> ] [ <NUM_LIT:0> ] == '<STR_LIT:.>' : <EOL> metrics = [ m . lstrip ( '<STR_LIT:.>' ) for m in metrics ] <EOL> return metrics <EOL> except IOError , e : <EOL> raise MetricsError ( "<STR_LIT>" , e ) <EOL> except ValueError , e : <EOL> raise MetricsError ( "<STR_LIT>" , e ) <EOL> def stat_metrics ( self ) : <EOL> try : <EOL> return os . stat ( self . config . filename_metrics ) <EOL> except OSError , e : <EOL> raise MetricsError ( "<STR_LIT>" , e ) <EOL> def update_data ( self , s_metrics ) : <EOL> self . logger . debug ( "<STR_LIT>" ) <EOL> metrics = self . load_metrics ( ) <EOL> self . logger . debug ( "<STR_LIT>" ) <EOL> s_metrics . remove_metrics_not_in ( metrics ) <EOL> self . logger . debug ( "<STR_LIT>" ) <EOL> s_metrics . update_metrics ( metrics ) <EOL> def make_config ( config ) : <EOL> if hasattr ( config , '<STR_LIT>' ) : <EOL> if not hasattr ( config , '<STR_LIT>' ) : <EOL> config . graphite_url_server = config . graphite_url <EOL> if not hasattr ( config , '<STR_LIT>' ) : <EOL> config . graphite_url_client = config . graphite_url <EOL> return config <EOL> def get_action_on_rules_match ( rules , subject ) : <EOL> '''<STR_LIT>''' <EOL> for ( match_rules , action ) in rules : <EOL> rule_match = True <EOL> for ( tag_k , tag_v ) in match_rules . items ( ) : <EOL> if tag_k not in subject : <EOL> rule_match = False <EOL> break <EOL> if isinstance ( tag_v , basestring ) : <EOL> if subject [ tag_k ] != tag_v : <EOL> rule_match = False <EOL> break <EOL> else : <EOL> tag_match = False <EOL> for option in tag_v : <EOL> if subject [ tag_k ] == option : <EOL> tag_match = True <EOL> if not tag_match : <EOL> rule_match = False <EOL> break <EOL> if rule_match : <EOL> yield action </s>
