<s> '''<STR_LIT>''' <EOL> from urlparse import * <EOL> import re <EOL> from redis import Redis <EOL> from pymongo import MongoClient <EOL> from kairos import Timeseries <EOL> from sqlalchemy import create_engine <EOL> import cql <EOL> import time <EOL> def long_or_float ( v ) : <EOL> try : <EOL> return long ( v ) <EOL> except ValueError : <EOL> return float ( v ) <EOL> class Schema ( object ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , name , config ) : <EOL> self . _count = <NUM_LIT:0> <EOL> self . _name = name <EOL> self . _host = config . pop ( '<STR_LIT:host>' , '<STR_LIT>' ) <EOL> self . _rolling = config . pop ( '<STR_LIT>' , <NUM_LIT:0> ) <EOL> self . _generator = config . pop ( '<STR_LIT>' , None ) <EOL> config . setdefault ( '<STR_LIT:type>' , '<STR_LIT:count>' ) <EOL> config . setdefault ( '<STR_LIT>' , long_or_float ) <EOL> config . setdefault ( '<STR_LIT>' , long_or_float ) <EOL> self . _transform = config . get ( '<STR_LIT>' ) <EOL> self . _patterns = config . pop ( '<STR_LIT>' , [ ] ) <EOL> if isinstance ( self . _patterns , ( tuple , list ) ) : <EOL> if len ( self . _patterns ) != <NUM_LIT:1> : <EOL> self . _patterns = [ re . compile ( x ) for x in self . _patterns ] <EOL> self . match = self . _match_list <EOL> else : <EOL> self . _patterns = re . compile ( self . _patterns [ <NUM_LIT:0> ] ) <EOL> self . match = self . _match_single <EOL> else : <EOL> self . _patterns = re . compile ( self . _patterns ) <EOL> self . match = self . _match_single <EOL> self . config = config <EOL> self . timeseries = Timeseries ( self . _host , ** config ) <EOL> self . list = self . timeseries . list <EOL> self . properties = self . timeseries . properties <EOL> self . iterate = self . timeseries . iterate <EOL> @ property <EOL> def name ( self ) : <EOL> return self . _name <EOL> @ property <EOL> def host ( self ) : <EOL> return self . _host <EOL> @ property <EOL> def count ( self ) : <EOL> return self . _count <EOL> def generate ( self ) : <EOL> if self . _generator : <EOL> stat , value = self . _generator ( ) <EOL> return stat , value , time . time ( ) <EOL> return None <EOL> def store ( self , stat , val , timestamp = None ) : <EOL> '''<STR_LIT>''' <EOL> if self . match ( stat ) : <EOL> if self . _transform : <EOL> stat , val = self . _transform ( stat , val ) <EOL> if stat is None : <EOL> return False <EOL> self . _count += <NUM_LIT:1> <EOL> self . timeseries . insert ( stat , val , timestamp , intervals = self . _rolling ) <EOL> return True <EOL> return False <EOL> def _match_single ( self , stat ) : <EOL> '''<STR_LIT>''' <EOL> if isinstance ( stat , ( list , tuple ) ) : <EOL> matches = filter ( None , [ self . _patterns . search ( s ) for s in stat ] ) <EOL> return len ( matches ) == len ( stat ) <EOL> return self . _patterns . search ( stat ) is not None <EOL> def _match_list ( self , stat ) : <EOL> '''<STR_LIT>''' <EOL> matches = set ( ) <EOL> for pattern in self . _patterns : <EOL> if isinstance ( stat , ( list , tuple ) ) : <EOL> for s in stat : <EOL> if pattern . search ( s ) : <EOL> matches . add ( s ) <EOL> if len ( matches ) == len ( stat ) : <EOL> return True <EOL> elif pattern . search ( stat ) : <EOL> return True <EOL> return False </s>
