<s> """<STR_LIT>""" <EOL> import re <EOL> from gcloud . _helpers import _name_from_project_path <EOL> from gcloud . exceptions import NotFound <EOL> _METRIC_TEMPLATE = re . compile ( r"""<STR_LIT>""" , re . VERBOSE ) <EOL> def _metric_name_from_path ( path , project ) : <EOL> """<STR_LIT>""" <EOL> return _name_from_project_path ( path , project , _METRIC_TEMPLATE ) <EOL> class Metric ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , name , filter_ , client , description = '<STR_LIT>' ) : <EOL> self . name = name <EOL> self . _client = client <EOL> self . filter_ = filter_ <EOL> self . description = description <EOL> @ property <EOL> def client ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _client <EOL> @ property <EOL> def project ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _client . project <EOL> @ property <EOL> def full_name ( self ) : <EOL> """<STR_LIT>""" <EOL> return '<STR_LIT>' % ( self . project , self . name ) <EOL> @ property <EOL> def path ( self ) : <EOL> """<STR_LIT>""" <EOL> return '<STR_LIT>' % ( self . full_name , ) <EOL> @ classmethod <EOL> def from_api_repr ( cls , resource , client ) : <EOL> """<STR_LIT>""" <EOL> metric_name = resource [ '<STR_LIT:name>' ] <EOL> filter_ = resource [ '<STR_LIT>' ] <EOL> description = resource . get ( '<STR_LIT:description>' , '<STR_LIT>' ) <EOL> return cls ( metric_name , filter_ , client = client , <EOL> description = description ) <EOL> def _require_client ( self , client ) : <EOL> """<STR_LIT>""" <EOL> if client is None : <EOL> client = self . _client <EOL> return client <EOL> def create ( self , client = None ) : <EOL> """<STR_LIT>""" <EOL> client = self . _require_client ( client ) <EOL> target = '<STR_LIT>' % ( self . project , ) <EOL> data = { <EOL> '<STR_LIT:name>' : self . name , <EOL> '<STR_LIT>' : self . filter_ , <EOL> } <EOL> if self . description : <EOL> data [ '<STR_LIT:description>' ] = self . description <EOL> client . connection . api_request ( method = '<STR_LIT:POST>' , path = target , data = data ) <EOL> def exists ( self , client = None ) : <EOL> """<STR_LIT>""" <EOL> client = self . _require_client ( client ) <EOL> try : <EOL> client . connection . api_request ( method = '<STR_LIT:GET>' , path = self . path ) <EOL> except NotFound : <EOL> return False <EOL> else : <EOL> return True <EOL> def reload ( self , client = None ) : <EOL> """<STR_LIT>""" <EOL> client = self . _require_client ( client ) <EOL> data = client . connection . api_request ( method = '<STR_LIT:GET>' , path = self . path ) <EOL> self . description = data . get ( '<STR_LIT:description>' , '<STR_LIT>' ) <EOL> self . filter_ = data [ '<STR_LIT>' ] <EOL> def update ( self , client = None ) : <EOL> """<STR_LIT>""" <EOL> client = self . _require_client ( client ) <EOL> data = { '<STR_LIT:name>' : self . name , '<STR_LIT>' : self . filter_ } <EOL> if self . description : <EOL> data [ '<STR_LIT:description>' ] = self . description <EOL> client . connection . api_request ( method = '<STR_LIT>' , path = self . path , data = data ) <EOL> def delete ( self , client = None ) : <EOL> """<STR_LIT>""" <EOL> client = self . _require_client ( client ) <EOL> client . connection . api_request ( method = '<STR_LIT>' , path = self . path ) </s>
