<s> import json <EOL> from tempodb . temporal . validate import convert_iso_stamp , check_time_param <EOL> from cursor import DataPointCursor , SeriesCursor , SingleValueCursor <EOL> class JSONSerializable ( object ) : <EOL> """<STR_LIT>""" <EOL> properties = [ ] <EOL> def __init__ ( self , json_text , response ) : <EOL> self . from_json ( json_text ) <EOL> self . response = response <EOL> def from_json ( self , json_text ) : <EOL> """<STR_LIT>""" <EOL> if type ( json_text ) in [ str , unicode ] : <EOL> j = json . loads ( json_text ) <EOL> else : <EOL> j = json_text <EOL> try : <EOL> for p in self . properties : <EOL> setattr ( self , p , j [ p ] ) <EOL> except KeyError , e : <EOL> msg = '<STR_LIT>' % str ( e ) <EOL> raise ValueError ( msg ) <EOL> def to_json ( self ) : <EOL> """<STR_LIT>""" <EOL> j = { } <EOL> for p in self . properties : <EOL> j [ p ] = getattr ( self , p ) <EOL> return json . dumps ( j ) <EOL> def to_dictionary ( self ) : <EOL> """<STR_LIT>""" <EOL> j = { } <EOL> for p in self . properties : <EOL> j [ p ] = getattr ( self , p ) <EOL> return j <EOL> class Nothing ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> pass <EOL> class SeriesSet ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , json_text , response ) : <EOL> self . data = json . loads ( json_text ) <EOL> self . cursor = SeriesCursor ( self . data , Series , response ) <EOL> class Series ( JSONSerializable ) : <EOL> """<STR_LIT>""" <EOL> properties = [ '<STR_LIT:key>' , '<STR_LIT:name>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> def __init__ ( self , json_text , response ) : <EOL> if isinstance ( json_text , basestring ) : <EOL> j = json . loads ( json_text ) <EOL> else : <EOL> j = json_text <EOL> if '<STR_LIT>' in j : <EOL> self . from_json ( j [ '<STR_LIT>' ] ) <EOL> else : <EOL> self . from_json ( json_text ) <EOL> self . response = response <EOL> class DataSet ( JSONSerializable ) : <EOL> """<STR_LIT>""" <EOL> properties = [ '<STR_LIT:data>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> def __init__ ( self , json_text , response ) : <EOL> super ( DataSet , self ) . __init__ ( json_text , response ) <EOL> self . cursor = DataPointCursor ( self . data , DataPoint , response ) <EOL> if self . rollup is not None : <EOL> self . rollup = Rollup ( self . rollup ) <EOL> class SingleValue ( JSONSerializable ) : <EOL> """<STR_LIT>""" <EOL> properties = [ '<STR_LIT>' , '<STR_LIT:data>' ] <EOL> def __init__ ( self , json_text , response ) : <EOL> super ( SingleValue , self ) . __init__ ( json_text , response ) <EOL> self . series = Series ( self . series , response ) <EOL> if self . data is not None : <EOL> self . data = DataPoint ( self . data , response , self . data . get ( '<STR_LIT>' ) ) <EOL> def to_dictionary ( self ) : <EOL> """<STR_LIT>""" <EOL> j = { } <EOL> j [ '<STR_LIT>' ] = self . series . to_dictionary ( ) <EOL> j [ '<STR_LIT:data>' ] = self . data . to_dictionary ( ) <EOL> return j <EOL> def to_json ( self ) : <EOL> """<STR_LIT>""" <EOL> return json . dumps ( self . to_dictionary ( ) ) <EOL> class SeriesSummary ( JSONSerializable ) : <EOL> properties = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:start>' , '<STR_LIT:end>' ] <EOL> def __init__ ( self , json_text , response , tz = None ) : <EOL> self . tz = tz <EOL> super ( SeriesSummary , self ) . __init__ ( json_text , response ) <EOL> self . series = Series ( self . series , response ) <EOL> self . summary = Summary ( self . summary , response ) <EOL> def from_json ( self , json_text ) : <EOL> """<STR_LIT>""" <EOL> if type ( json_text ) in [ str , unicode ] : <EOL> j = json . loads ( json_text ) <EOL> else : <EOL> j = json_text <EOL> try : <EOL> for p in self . properties : <EOL> if p in [ '<STR_LIT:start>' , '<STR_LIT:end>' ] : <EOL> val = convert_iso_stamp ( j [ p ] , self . tz ) <EOL> setattr ( self , p , val ) <EOL> else : <EOL> setattr ( self , p , j [ p ] ) <EOL> except KeyError : <EOL> pass <EOL> def to_json ( self ) : <EOL> """<STR_LIT>""" <EOL> return json . dumps ( self . to_dictionary ( ) ) <EOL> def to_dictionary ( self ) : <EOL> """<STR_LIT>""" <EOL> d = { '<STR_LIT:start>' : self . start . isoformat ( ) , <EOL> '<STR_LIT:end>' : self . end . isoformat ( ) , <EOL> '<STR_LIT>' : self . tz , <EOL> '<STR_LIT>' : self . summary . to_dictionary ( ) , <EOL> '<STR_LIT>' : self . series . to_dictionary ( ) <EOL> } <EOL> return d <EOL> class Summary ( JSONSerializable ) : <EOL> """<STR_LIT>""" <EOL> properties = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:count>' ] <EOL> class Rollup ( JSONSerializable ) : <EOL> """<STR_LIT>""" <EOL> properties = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> class DataPoint ( JSONSerializable ) : <EOL> """<STR_LIT>""" <EOL> properties = [ '<STR_LIT:t>' , '<STR_LIT:v>' , '<STR_LIT:key>' , '<STR_LIT:id>' ] <EOL> def __init__ ( self , json_text , response , tz = None ) : <EOL> self . tz = tz <EOL> super ( DataPoint , self ) . __init__ ( json_text , response ) <EOL> @ classmethod <EOL> def from_data ( self , time , value , series_id = None , key = None , tz = None ) : <EOL> """<STR_LIT>""" <EOL> t = check_time_param ( time ) <EOL> if type ( value ) in [ float , int ] : <EOL> v = value <EOL> else : <EOL> raise ValueError ( '<STR_LIT>' % <EOL> str ( value ) ) <EOL> j = { <EOL> '<STR_LIT:t>' : t , <EOL> '<STR_LIT:v>' : v , <EOL> '<STR_LIT:id>' : series_id , <EOL> '<STR_LIT:key>' : key <EOL> } <EOL> return DataPoint ( j , None , tz = tz ) <EOL> def from_json ( self , json_text ) : <EOL> """<STR_LIT>""" <EOL> if type ( json_text ) in [ str , unicode ] : <EOL> j = json . loads ( json_text ) <EOL> else : <EOL> j = json_text <EOL> try : <EOL> for p in self . properties : <EOL> if p == '<STR_LIT:t>' : <EOL> val = convert_iso_stamp ( j [ p ] , self . tz ) <EOL> setattr ( self , p , val ) <EOL> else : <EOL> setattr ( self , p , j [ p ] ) <EOL> except KeyError : <EOL> pass <EOL> def to_json ( self ) : <EOL> """<STR_LIT>""" <EOL> j = { } <EOL> for p in self . properties : <EOL> try : <EOL> v = getattr ( self , p ) <EOL> except AttributeError : <EOL> continue <EOL> if v is not None : <EOL> if p == '<STR_LIT:t>' : <EOL> j [ p ] = getattr ( self , p ) . isoformat ( ) <EOL> else : <EOL> j [ p ] = getattr ( self , p ) <EOL> return json . dumps ( j ) <EOL> def to_dictionary ( self ) : <EOL> """<STR_LIT>""" <EOL> j = { } <EOL> for p in self . properties : <EOL> try : <EOL> v = getattr ( self , p ) <EOL> except AttributeError : <EOL> continue <EOL> if v is not None : <EOL> if p == '<STR_LIT:t>' : <EOL> j [ p ] = getattr ( self , p ) . isoformat ( ) <EOL> else : <EOL> j [ p ] = getattr ( self , p ) <EOL> return j <EOL> class DataPointFound ( JSONSerializable ) : <EOL> """<STR_LIT>""" <EOL> properties = [ '<STR_LIT>' , '<STR_LIT>' ] <EOL> def __init__ ( self , json_text , response , tz = None ) : <EOL> self . tz = tz <EOL> super ( DataPointFound , self ) . __init__ ( json_text , response ) <EOL> def from_json ( self , json_text ) : <EOL> """<STR_LIT>""" <EOL> if type ( json_text ) in [ str , unicode ] : <EOL> j = json . loads ( json_text ) <EOL> else : <EOL> j = json_text <EOL> try : <EOL> for p in self . properties : <EOL> if p == '<STR_LIT>' : <EOL> self . start = convert_iso_stamp ( j [ p ] [ '<STR_LIT:start>' ] , self . tz ) <EOL> self . end = convert_iso_stamp ( j [ p ] [ '<STR_LIT:end>' ] , self . tz ) <EOL> elif p == '<STR_LIT>' : <EOL> t = convert_iso_stamp ( j [ p ] [ '<STR_LIT:t>' ] , self . tz ) <EOL> setattr ( self , '<STR_LIT:t>' , t ) <EOL> v = j [ p ] [ '<STR_LIT:v>' ] <EOL> setattr ( self , '<STR_LIT:v>' , v ) <EOL> except KeyError : <EOL> pass <EOL> def to_dictionary ( self ) : <EOL> """<STR_LIT>""" <EOL> j = { } <EOL> j [ '<STR_LIT>' ] = { '<STR_LIT:start>' : self . start . isoformat ( ) , <EOL> '<STR_LIT:end>' : self . end . isoformat ( ) } <EOL> j [ '<STR_LIT>' ] = { '<STR_LIT:v>' : self . v , '<STR_LIT:t>' : self . t . isoformat ( ) } <EOL> return j <EOL> def to_json ( self ) : <EOL> """<STR_LIT>""" <EOL> return json . dumps ( self . to_dictionary ( ) ) <EOL> class MultiPoint ( JSONSerializable ) : <EOL> """<STR_LIT>""" <EOL> properties = [ '<STR_LIT:t>' , '<STR_LIT:v>' ] <EOL> def __init__ ( self , json_text , response , tz = None ) : <EOL> self . tz = tz <EOL> super ( MultiPoint , self ) . __init__ ( json_text , response ) <EOL> def from_json ( self , json_text ) : <EOL> """<STR_LIT>""" <EOL> if type ( json_text ) in [ str , unicode ] : <EOL> j = json . loads ( json_text ) <EOL> else : <EOL> j = json_text <EOL> try : <EOL> for p in self . properties : <EOL> if p == '<STR_LIT:t>' : <EOL> t = convert_iso_stamp ( j [ p ] , self . tz ) <EOL> setattr ( self , '<STR_LIT:t>' , t ) <EOL> else : <EOL> setattr ( self , p , j [ p ] ) <EOL> except KeyError : <EOL> pass <EOL> def to_json ( self ) : <EOL> """<STR_LIT>""" <EOL> j = { } <EOL> for p in self . properties : <EOL> try : <EOL> v = getattr ( self , p ) <EOL> except AttributeError : <EOL> continue <EOL> if v is not None : <EOL> if p == '<STR_LIT:t>' : <EOL> j [ p ] = getattr ( self , p ) . isoformat ( ) <EOL> else : <EOL> j [ p ] = getattr ( self , p ) <EOL> return json . dumps ( j ) <EOL> def get ( self , k ) : <EOL> """<STR_LIT>""" <EOL> return self . v . get ( k ) </s>
