<s> """<STR_LIT>""" <EOL> import abc <EOL> import calendar <EOL> import collections <EOL> import datetime <EOL> import functools <EOL> import posixpath <EOL> import re <EOL> import time <EOL> import zlib <EOL> import dateutil <EOL> from dateutil import parser <EOL> import logging <EOL> from grr . lib import registry <EOL> from grr . lib import utils <EOL> from grr . proto import jobs_pb2 <EOL> MICROSECONDS = <NUM_LIT> <EOL> _LATE_BINDING_STORE = { } <EOL> def RegisterLateBindingCallback ( target_name , callback , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> _LATE_BINDING_STORE . setdefault ( target_name , [ ] ) . append ( ( callback , kwargs ) ) <EOL> class Error ( Exception ) : <EOL> """<STR_LIT>""" <EOL> class InitializeError ( Error ) : <EOL> """<STR_LIT>""" <EOL> class DecodeError ( InitializeError , ValueError ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , msg ) : <EOL> logging . debug ( msg ) <EOL> super ( DecodeError , self ) . __init__ ( msg ) <EOL> class RDFValueMetaclass ( registry . MetaclassRegistry ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( cls , name , bases , env_dict ) : <EOL> super ( RDFValueMetaclass , cls ) . __init__ ( name , bases , env_dict ) <EOL> for callback , kwargs in _LATE_BINDING_STORE . pop ( name , [ ] ) : <EOL> callback ( target = cls , ** kwargs ) <EOL> class RDFValue ( object ) : <EOL> """<STR_LIT>""" <EOL> __metaclass__ = RDFValueMetaclass <EOL> data_store_type = "<STR_LIT>" <EOL> context_help_url = None <EOL> _age = <NUM_LIT:0> <EOL> dirty = False <EOL> attribute_instance = None <EOL> def __init__ ( self , initializer = None , age = None ) : <EOL> """<STR_LIT>""" <EOL> if age is None : <EOL> age = RDFDatetime ( age = <NUM_LIT:0> ) <EOL> self . _age = age <EOL> if initializer . __class__ == self . __class__ : <EOL> self . ParseFromString ( initializer . SerializeToString ( ) ) <EOL> elif initializer is not None : <EOL> self . ParseFromString ( initializer ) <EOL> def Copy ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . __class__ ( initializer = self . SerializeToString ( ) ) <EOL> def __copy__ ( self ) : <EOL> return self . Copy ( ) <EOL> @ property <EOL> def age ( self ) : <EOL> if self . _age . __class__ is not RDFDatetime : <EOL> self . _age = RDFDatetime ( self . _age , age = <NUM_LIT:0> ) <EOL> return self . _age <EOL> @ age . setter <EOL> def age ( self , value ) : <EOL> """<STR_LIT>""" <EOL> self . _age = RDFDatetime ( value , age = <NUM_LIT:0> ) <EOL> def ParseFromDataStore ( self , data_store_obj ) : <EOL> """<STR_LIT>""" <EOL> return self . ParseFromString ( data_store_obj ) <EOL> @ abc . abstractmethod <EOL> def ParseFromString ( self , string ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def SerializeToDataStore ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . SerializeToString ( ) <EOL> @ abc . abstractmethod <EOL> def SerializeToString ( self ) : <EOL> """<STR_LIT>""" <EOL> def __getstate__ ( self ) : <EOL> """<STR_LIT>""" <EOL> return dict ( __pickled_rdfvalue = True , <EOL> age = int ( self . age ) , <EOL> data = self . SerializeToString ( ) ) <EOL> def __setstate__ ( self , data ) : <EOL> """<STR_LIT>""" <EOL> if "<STR_LIT>" in data : <EOL> self . ParseFromString ( data [ "<STR_LIT:data>" ] ) <EOL> self . age = RDFDatetime ( data [ "<STR_LIT>" ] ) <EOL> else : <EOL> self . __dict__ = data <EOL> def AsProto ( self ) : <EOL> """<STR_LIT>""" <EOL> return jobs_pb2 . EmbeddedRDFValue ( age = int ( self . age ) , <EOL> name = self . __class__ . __name__ , <EOL> data = self . SerializeToString ( ) ) <EOL> def __iter__ ( self ) : <EOL> """<STR_LIT>""" <EOL> yield self <EOL> def __hash__ ( self ) : <EOL> return hash ( self . SerializeToString ( ) ) <EOL> def Summary ( self ) : <EOL> """<STR_LIT>""" <EOL> return str ( self ) <EOL> @ classmethod <EOL> def Fields ( cls ) : <EOL> """<STR_LIT>""" <EOL> return [ ] <EOL> @ staticmethod <EOL> def ContainsMatch ( attribute , filter_implemention , regex ) : <EOL> return filter_implemention . PredicateContainsFilter ( attribute , regex ) <EOL> operators = dict ( contains = ( <NUM_LIT:1> , "<STR_LIT>" ) ) <EOL> class RDFBytes ( RDFValue ) : <EOL> """<STR_LIT>""" <EOL> data_store_type = "<STR_LIT>" <EOL> _value = "<STR_LIT>" <EOL> def ParseFromString ( self , string ) : <EOL> if isinstance ( string , unicode ) : <EOL> self . _value = utils . SmartStr ( string ) <EOL> else : <EOL> self . _value = string <EOL> def SerializeToString ( self ) : <EOL> return self . _value <EOL> def __str__ ( self ) : <EOL> return utils . SmartStr ( self . _value ) <EOL> def __lt__ ( self , other ) : <EOL> if isinstance ( other , self . __class__ ) : <EOL> return self . _value < other . _value <EOL> else : <EOL> return self . _value < other <EOL> def __gt__ ( self , other ) : <EOL> if isinstance ( other , self . __class__ ) : <EOL> return self . _value > other . _value <EOL> else : <EOL> return self . _value > other <EOL> def __eq__ ( self , other ) : <EOL> if isinstance ( other , self . __class__ ) : <EOL> return self . _value == other . _value <EOL> else : <EOL> return self . _value == other <EOL> def __ne__ ( self , other ) : <EOL> return not self . __eq__ ( other ) <EOL> def __hash__ ( self ) : <EOL> return hash ( self . _value ) <EOL> def __bool__ ( self ) : <EOL> return bool ( self . _value ) <EOL> def __nonzero__ ( self ) : <EOL> return bool ( self . _value ) <EOL> def __len__ ( self ) : <EOL> return len ( self . _value ) <EOL> class RDFZippedBytes ( RDFBytes ) : <EOL> """<STR_LIT>""" <EOL> def Uncompress ( self ) : <EOL> if self : <EOL> return zlib . decompress ( self . _value ) <EOL> else : <EOL> return "<STR_LIT>" <EOL> class RDFString ( RDFBytes ) : <EOL> """<STR_LIT>""" <EOL> data_store_type = "<STR_LIT:string>" <EOL> _value = u"<STR_LIT>" <EOL> @ staticmethod <EOL> def Startswith ( attribute , filter_implemention , string ) : <EOL> return filter_implemention . PredicateContainsFilter ( <EOL> attribute , "<STR_LIT>" + utils . EscapeRegex ( string ) ) <EOL> operators = RDFValue . operators . copy ( ) <EOL> operators [ "<STR_LIT>" ] = ( <NUM_LIT:1> , "<STR_LIT>" ) <EOL> operators [ "<STR_LIT:=>" ] = ( <NUM_LIT:1> , "<STR_LIT>" ) <EOL> operators [ "<STR_LIT>" ] = ( <NUM_LIT:1> , "<STR_LIT>" ) <EOL> def format ( self , * args , ** kwargs ) : <EOL> return self . _value . format ( * args , ** kwargs ) <EOL> def split ( self , * args , ** kwargs ) : <EOL> return self . _value . split ( * args , ** kwargs ) <EOL> def __unicode__ ( self ) : <EOL> return utils . SmartUnicode ( self . _value ) <EOL> def __getitem__ ( self , value ) : <EOL> return self . _value . __getitem__ ( value ) <EOL> def ParseFromString ( self , string ) : <EOL> self . _value = utils . SmartStr ( string ) <EOL> def SerializeToString ( self ) : <EOL> return utils . SmartStr ( self . _value ) <EOL> def SerializeToDataStore ( self ) : <EOL> return utils . SmartUnicode ( self . _value ) <EOL> class HashDigest ( RDFBytes ) : <EOL> """<STR_LIT>""" <EOL> data_store_type = "<STR_LIT>" <EOL> def __str__ ( self ) : <EOL> return self . _value . encode ( "<STR_LIT>" ) <EOL> def __eq__ ( self , other ) : <EOL> return ( self . _value == utils . SmartStr ( other ) or <EOL> self . _value . encode ( "<STR_LIT>" ) == other ) <EOL> def __ne__ ( self , other ) : <EOL> return not self . __eq__ ( other ) <EOL> @ functools . total_ordering <EOL> class RDFInteger ( RDFString ) : <EOL> """<STR_LIT>""" <EOL> data_store_type = "<STR_LIT>" <EOL> @ staticmethod <EOL> def IsNumeric ( value ) : <EOL> return isinstance ( value , ( int , long , float , RDFInteger ) ) <EOL> def __init__ ( self , initializer = None , age = None ) : <EOL> super ( RDFInteger , self ) . __init__ ( initializer = initializer , age = age ) <EOL> if initializer is None : <EOL> self . _value = <NUM_LIT:0> <EOL> else : <EOL> self . ParseFromString ( initializer ) <EOL> def ParseFromString ( self , string ) : <EOL> self . _value = <NUM_LIT:0> <EOL> if string : <EOL> try : <EOL> self . _value = int ( string ) <EOL> except TypeError as e : <EOL> raise DecodeError ( e ) <EOL> def SerializeToDataStore ( self ) : <EOL> """<STR_LIT>""" <EOL> return int ( self . _value ) <EOL> def Set ( self , value ) : <EOL> if isinstance ( value , ( long , int ) ) : <EOL> self . _value = value <EOL> else : <EOL> self . ParseFromString ( value ) <EOL> def __long__ ( self ) : <EOL> return long ( self . _value ) <EOL> def __int__ ( self ) : <EOL> return int ( self . _value ) <EOL> def __float__ ( self ) : <EOL> return float ( self . _value ) <EOL> def __index__ ( self ) : <EOL> return self . _value <EOL> def __eq__ ( self , other ) : <EOL> return self . _value == other <EOL> def __lt__ ( self , other ) : <EOL> return self . _value < other <EOL> def __and__ ( self , other ) : <EOL> return self . _value & other <EOL> def __rand__ ( self , other ) : <EOL> return self . _value & other <EOL> def __iand__ ( self , other ) : <EOL> self . _value &= other <EOL> return self <EOL> def __or__ ( self , other ) : <EOL> return self . _value | other <EOL> def __ror__ ( self , other ) : <EOL> return self . _value | other <EOL> def __ior__ ( self , other ) : <EOL> self . _value |= other <EOL> return self <EOL> def __add__ ( self , other ) : <EOL> return self . _value + other <EOL> def __radd__ ( self , other ) : <EOL> return self . _value + other <EOL> def __iadd__ ( self , other ) : <EOL> self . _value += other <EOL> return self <EOL> def __sub__ ( self , other ) : <EOL> return self . _value - other <EOL> def __rsub__ ( self , other ) : <EOL> return other - self . _value <EOL> def __isub__ ( self , other ) : <EOL> self . _value -= other <EOL> return self <EOL> def __mul__ ( self , other ) : <EOL> return self . _value * other <EOL> def __rmul__ ( self , other ) : <EOL> return self . _value * other <EOL> def __div__ ( self , other ) : <EOL> return self . _value / other <EOL> def __rdiv__ ( self , other ) : <EOL> return other / self . _value <EOL> @ staticmethod <EOL> def LessThan ( attribute , filter_implemention , value ) : <EOL> return filter_implemention . PredicateLessThanFilter ( attribute , long ( value ) ) <EOL> @ staticmethod <EOL> def GreaterThan ( attribute , filter_implemention , value ) : <EOL> return filter_implemention . PredicateGreaterThanFilter ( <EOL> attribute , long ( value ) ) <EOL> @ staticmethod <EOL> def Equal ( attribute , filter_implemention , value ) : <EOL> return filter_implemention . PredicateNumericEqualFilter ( <EOL> attribute , long ( value ) ) <EOL> operators = { "<STR_LIT:<>" : ( <NUM_LIT:1> , "<STR_LIT>" ) , <EOL> "<STR_LIT:>>" : ( <NUM_LIT:1> , "<STR_LIT>" ) , <EOL> "<STR_LIT:=>" : ( <NUM_LIT:1> , "<STR_LIT>" ) } <EOL> class RDFBool ( RDFInteger ) : <EOL> """<STR_LIT>""" <EOL> data_store_type = "<STR_LIT>" <EOL> class RDFDatetime ( RDFInteger ) : <EOL> """<STR_LIT>""" <EOL> converter = MICROSECONDS <EOL> data_store_type = "<STR_LIT>" <EOL> _value = <NUM_LIT:0> <EOL> def __init__ ( self , initializer = None , age = None ) : <EOL> super ( RDFDatetime , self ) . __init__ ( None , age ) <EOL> if isinstance ( initializer , RDFInteger ) : <EOL> self . _value = initializer . _value <EOL> elif isinstance ( initializer , ( int , long , float ) ) : <EOL> self . _value = int ( initializer ) <EOL> elif isinstance ( initializer , datetime . datetime ) : <EOL> seconds = calendar . timegm ( initializer . utctimetuple ( ) ) <EOL> self . _value = ( seconds * self . converter ) + initializer . microsecond <EOL> elif isinstance ( initializer , basestring ) : <EOL> try : <EOL> self . _value = int ( initializer ) <EOL> except ValueError : <EOL> if initializer : <EOL> self . ParseFromHumanReadable ( initializer ) <EOL> elif initializer is not None : <EOL> raise InitializeError ( "<STR_LIT>" % <EOL> type ( initializer ) ) <EOL> def Now ( self ) : <EOL> self . _value = int ( time . time ( ) * self . converter ) <EOL> return self <EOL> def Format ( self , fmt ) : <EOL> """<STR_LIT>""" <EOL> return time . strftime ( fmt , time . gmtime ( self . _value / self . converter ) ) <EOL> def __str__ ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . Format ( "<STR_LIT>" ) <EOL> def __unicode__ ( self ) : <EOL> return utils . SmartUnicode ( str ( self ) ) <EOL> def AsDatetime ( self ) : <EOL> """<STR_LIT>""" <EOL> return datetime . datetime . utcfromtimestamp ( self . _value / self . converter ) <EOL> def AsSecondsFromEpoch ( self ) : <EOL> return self . _value / self . converter <EOL> def AsMicroSecondsFromEpoch ( self ) : <EOL> return self . _value <EOL> def FromSecondsFromEpoch ( self , value ) : <EOL> self . _value = value * self . converter <EOL> return self <EOL> def ParseFromHumanReadable ( self , string , eoy = False ) : <EOL> self . _value = self . _ParseFromHumanReadable ( string , eoy = eoy ) <EOL> return self <EOL> def __add__ ( self , other ) : <EOL> if isinstance ( other , ( int , long , float , Duration ) ) : <EOL> return self . __class__ ( self . _value + other * self . converter ) <EOL> return NotImplemented <EOL> def __iadd__ ( self , other ) : <EOL> if isinstance ( other , ( int , long , float , Duration ) ) : <EOL> self . _value += other * self . converter <EOL> return self <EOL> return NotImplemented <EOL> def __mul__ ( self , other ) : <EOL> if isinstance ( other , ( int , long , float , Duration ) ) : <EOL> return self . __class__ ( self . _value * other ) <EOL> return NotImplemented <EOL> def __rmul__ ( self , other ) : <EOL> return self . __mul__ ( other ) <EOL> def __sub__ ( self , other ) : <EOL> if isinstance ( other , ( int , long , float , Duration ) ) : <EOL> return self . __class__ ( self . _value - other * self . converter ) <EOL> if isinstance ( other , RDFDatetime ) : <EOL> return Duration ( self . AsSecondsFromEpoch ( ) - other . AsSecondsFromEpoch ( ) ) <EOL> return NotImplemented <EOL> def __isub__ ( self , other ) : <EOL> if isinstance ( other , ( int , long , float , Duration ) ) : <EOL> self . _value -= other * self . converter <EOL> return self <EOL> return NotImplemented <EOL> @ classmethod <EOL> def _ParseFromHumanReadable ( cls , string , eoy = False ) : <EOL> """<STR_LIT>""" <EOL> if eoy : <EOL> default = datetime . datetime ( time . gmtime ( ) . tm_year , <NUM_LIT:12> , <NUM_LIT> , <NUM_LIT> , <NUM_LIT> , <EOL> tzinfo = dateutil . tz . tzutc ( ) ) <EOL> else : <EOL> default = datetime . datetime ( time . gmtime ( ) . tm_year , <NUM_LIT:1> , <NUM_LIT:1> , <NUM_LIT:0> , <NUM_LIT:0> , <EOL> tzinfo = dateutil . tz . tzutc ( ) ) <EOL> timestamp = parser . parse ( string , default = default ) <EOL> return calendar . timegm ( timestamp . utctimetuple ( ) ) * cls . converter <EOL> @ classmethod <EOL> def LessThanEq ( cls , attribute , filter_implemention , value ) : <EOL> return filter_implemention . PredicateLesserEqualFilter ( <EOL> attribute , cls . _ParseFromHumanReadable ( value , eoy = True ) ) <EOL> @ classmethod <EOL> def LessThan ( cls , attribute , filter_implemention , value ) : <EOL> """<STR_LIT>""" <EOL> return filter_implemention . PredicateLesserEqualFilter ( <EOL> attribute , cls . _ParseFromHumanReadable ( value ) ) <EOL> @ classmethod <EOL> def GreaterThanEq ( cls , attribute , filter_implemention , value ) : <EOL> return filter_implemention . PredicateGreaterEqualFilter ( <EOL> attribute , cls . _ParseFromHumanReadable ( value ) ) <EOL> @ classmethod <EOL> def GreaterThan ( cls , attribute , filter_implemention , value ) : <EOL> return filter_implemention . PredicateGreaterEqualFilter ( <EOL> attribute , cls . _ParseFromHumanReadable ( value , eoy = True ) ) <EOL> operators = { "<STR_LIT:<>" : ( <NUM_LIT:1> , "<STR_LIT>" ) , <EOL> "<STR_LIT:>>" : ( <NUM_LIT:1> , "<STR_LIT>" ) , <EOL> "<STR_LIT>" : ( <NUM_LIT:1> , "<STR_LIT>" ) , <EOL> "<STR_LIT>" : ( <NUM_LIT:1> , "<STR_LIT>" ) } <EOL> class RDFDatetimeSeconds ( RDFDatetime ) : <EOL> """<STR_LIT>""" <EOL> converter = <NUM_LIT:1> <EOL> class Duration ( RDFInteger ) : <EOL> """<STR_LIT>""" <EOL> data_store_type = "<STR_LIT>" <EOL> DIVIDERS = collections . OrderedDict ( ( <EOL> ( "<STR_LIT:w>" , <NUM_LIT> * <NUM_LIT> * <NUM_LIT> * <NUM_LIT:7> ) , <EOL> ( "<STR_LIT:d>" , <NUM_LIT> * <NUM_LIT> * <NUM_LIT> ) , <EOL> ( "<STR_LIT:h>" , <NUM_LIT> * <NUM_LIT> ) , <EOL> ( "<STR_LIT:m>" , <NUM_LIT> ) , <EOL> ( "<STR_LIT:s>" , <NUM_LIT:1> ) ) ) <EOL> def __init__ ( self , initializer = None , age = None ) : <EOL> super ( Duration , self ) . __init__ ( None , age ) <EOL> if isinstance ( initializer , Duration ) : <EOL> self . _value = initializer . _value <EOL> elif isinstance ( initializer , basestring ) : <EOL> self . ParseFromHumanReadable ( initializer ) <EOL> elif isinstance ( initializer , ( int , long , float ) ) : <EOL> self . _value = initializer <EOL> elif isinstance ( initializer , RDFInteger ) : <EOL> self . _value = int ( initializer ) <EOL> elif initializer is None : <EOL> self . _value = <NUM_LIT:0> <EOL> else : <EOL> raise InitializeError ( "<STR_LIT>" % <EOL> type ( initializer ) ) <EOL> def Validate ( self , value , ** _ ) : <EOL> self . ParseFromString ( value ) <EOL> def ParseFromString ( self , string ) : <EOL> self . ParseFromHumanReadable ( string ) <EOL> def SerializeToString ( self ) : <EOL> return str ( self ) <EOL> @ property <EOL> def seconds ( self ) : <EOL> return self . _value <EOL> @ property <EOL> def microseconds ( self ) : <EOL> return self . _value * <NUM_LIT> <EOL> def __str__ ( self ) : <EOL> time_secs = self . _value <EOL> for label , divider in self . DIVIDERS . items ( ) : <EOL> if time_secs % divider == <NUM_LIT:0> : <EOL> return "<STR_LIT>" % ( time_secs / divider , label ) <EOL> def __unicode__ ( self ) : <EOL> return utils . SmartUnicode ( str ( self ) ) <EOL> def __add__ ( self , other ) : <EOL> if isinstance ( other , ( int , long , float , Duration ) ) : <EOL> return self . __class__ ( self . _value + other ) <EOL> return NotImplemented <EOL> def __iadd__ ( self , other ) : <EOL> if isinstance ( other , ( int , long , float , Duration ) ) : <EOL> self . _value += other <EOL> return self <EOL> return NotImplemented <EOL> def __mul__ ( self , other ) : <EOL> if isinstance ( other , ( int , long , float , Duration ) ) : <EOL> return self . __class__ ( self . _value * other ) <EOL> return NotImplemented <EOL> def __rmul__ ( self , other ) : <EOL> return self . __mul__ ( other ) <EOL> def __sub__ ( self , other ) : <EOL> if isinstance ( other , ( int , long , float , Duration ) ) : <EOL> return self . __class__ ( self . _value - other ) <EOL> return NotImplemented <EOL> def __isub__ ( self , other ) : <EOL> if isinstance ( other , ( int , long , float , Duration ) ) : <EOL> self . _value -= other <EOL> return self <EOL> return NotImplemented <EOL> def __abs__ ( self ) : <EOL> return Duration ( abs ( self . _value ) ) <EOL> def Expiry ( self , base_time = None ) : <EOL> if base_time is None : <EOL> base_time = RDFDatetime ( ) . Now ( ) <EOL> else : <EOL> base_time = base_time . Copy ( ) <EOL> base_time_sec = base_time . AsSecondsFromEpoch ( ) <EOL> return base_time . FromSecondsFromEpoch ( base_time_sec + self . _value ) <EOL> def ParseFromHumanReadable ( self , timestring ) : <EOL> """<STR_LIT>""" <EOL> if not timestring : <EOL> return <EOL> orig_string = timestring <EOL> multiplicator = <NUM_LIT:1> <EOL> if timestring [ - <NUM_LIT:1> ] . isdigit ( ) : <EOL> pass <EOL> else : <EOL> try : <EOL> multiplicator = self . DIVIDERS [ timestring [ - <NUM_LIT:1> ] ] <EOL> except KeyError : <EOL> raise RuntimeError ( "<STR_LIT>" % <EOL> ( timestring [ - <NUM_LIT:1> ] , orig_string ) ) <EOL> timestring = timestring [ : - <NUM_LIT:1> ] <EOL> try : <EOL> self . _value = int ( timestring ) * multiplicator <EOL> except ValueError : <EOL> raise InitializeError ( "<STR_LIT>" % <EOL> orig_string ) <EOL> class ByteSize ( RDFInteger ) : <EOL> """<STR_LIT>""" <EOL> data_store_type = "<STR_LIT>" <EOL> DIVIDERS = dict ( ( <EOL> ( "<STR_LIT>" , <NUM_LIT:1> ) , <EOL> ( "<STR_LIT:k>" , <NUM_LIT:1000> ) , <EOL> ( "<STR_LIT:m>" , <NUM_LIT:1000> ** <NUM_LIT:2> ) , <EOL> ( "<STR_LIT:g>" , <NUM_LIT:1000> ** <NUM_LIT:3> ) , <EOL> ( "<STR_LIT>" , <NUM_LIT> ) , <EOL> ( "<STR_LIT>" , <NUM_LIT> ** <NUM_LIT:2> ) , <EOL> ( "<STR_LIT>" , <NUM_LIT> ** <NUM_LIT:3> ) , <EOL> ) ) <EOL> REGEX = re . compile ( "<STR_LIT>" ) <EOL> def __init__ ( self , initializer = None , age = None ) : <EOL> super ( ByteSize , self ) . __init__ ( None , age ) <EOL> if isinstance ( initializer , ByteSize ) : <EOL> self . _value = initializer . _value <EOL> elif isinstance ( initializer , basestring ) : <EOL> self . ParseFromHumanReadable ( initializer ) <EOL> elif isinstance ( initializer , ( int , long , float ) ) : <EOL> self . _value = initializer <EOL> elif isinstance ( initializer , RDFInteger ) : <EOL> self . _value = int ( initializer ) <EOL> elif initializer is None : <EOL> self . _value = <NUM_LIT:0> <EOL> else : <EOL> raise InitializeError ( "<STR_LIT>" % <EOL> type ( initializer ) ) <EOL> def __str__ ( self ) : <EOL> size_token = "<STR_LIT>" <EOL> if self . _value > <NUM_LIT> ** <NUM_LIT:3> : <EOL> size_token = "<STR_LIT>" <EOL> value = float ( self . _value ) / <NUM_LIT> ** <NUM_LIT:3> <EOL> elif self . _value > <NUM_LIT> ** <NUM_LIT:2> : <EOL> size_token = "<STR_LIT>" <EOL> value = float ( self . _value ) / <NUM_LIT> ** <NUM_LIT:2> <EOL> elif self . _value > <NUM_LIT> : <EOL> size_token = "<STR_LIT>" <EOL> value = float ( self . _value ) / <NUM_LIT> <EOL> else : <EOL> return utils . SmartStr ( self . _value ) + "<STR_LIT:b>" <EOL> return "<STR_LIT>" % ( value , size_token ) <EOL> def ParseFromHumanReadable ( self , string ) : <EOL> """<STR_LIT>""" <EOL> if not string : <EOL> return None <EOL> match = self . REGEX . match ( string . strip ( ) . lower ( ) ) <EOL> if not match : <EOL> raise DecodeError ( "<STR_LIT>" % string ) <EOL> multiplier = self . DIVIDERS . get ( match . group ( <NUM_LIT:2> ) ) <EOL> if not multiplier : <EOL> raise DecodeError ( "<STR_LIT>" % match . group ( <NUM_LIT:2> ) ) <EOL> value = match . group ( <NUM_LIT:1> ) <EOL> if "<STR_LIT:.>" in value : <EOL> value = float ( value ) <EOL> else : <EOL> value = long ( value ) <EOL> self . _value = int ( value * multiplier ) <EOL> @ functools . total_ordering <EOL> class RDFURN ( RDFValue ) : <EOL> """<STR_LIT>""" <EOL> data_store_type = "<STR_LIT:string>" <EOL> scheme = "<STR_LIT>" <EOL> def __init__ ( self , initializer = None , age = None ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( initializer , RDFURN ) : <EOL> self . _string_urn = initializer . Path ( ) <EOL> super ( RDFURN , self ) . __init__ ( None , age ) <EOL> return <EOL> if initializer is None : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> super ( RDFURN , self ) . __init__ ( initializer = initializer , age = age ) <EOL> def ParseFromString ( self , initializer = None ) : <EOL> """<STR_LIT>""" <EOL> if initializer . startswith ( "<STR_LIT>" ) : <EOL> initializer = initializer [ <NUM_LIT:5> : ] <EOL> self . _string_urn = utils . NormalizePath ( initializer ) <EOL> def SerializeToString ( self ) : <EOL> return str ( self ) <EOL> def SerializeToDataStore ( self ) : <EOL> return unicode ( self ) <EOL> def __setstate__ ( self , data ) : <EOL> """<STR_LIT>""" <EOL> RDFValue . __setstate__ ( self , data ) <EOL> if self . _string_urn . startswith ( "<STR_LIT>" ) : <EOL> self . _string_urn = self . _string_urn [ <NUM_LIT:5> : ] <EOL> def Dirname ( self ) : <EOL> return posixpath . dirname ( self . _string_urn ) <EOL> def Basename ( self ) : <EOL> return posixpath . basename ( self . Path ( ) ) <EOL> def Add ( self , path , age = None ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( path , basestring ) : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> result = self . Copy ( age ) <EOL> result . Update ( path = utils . JoinPath ( self . _string_urn , path ) ) <EOL> return result <EOL> def Update ( self , url = None , path = None ) : <EOL> """<STR_LIT>""" <EOL> if url : <EOL> self . ParseFromString ( url ) <EOL> if path : <EOL> self . _string_urn = path <EOL> self . dirty = True <EOL> def Copy ( self , age = None ) : <EOL> """<STR_LIT>""" <EOL> if age is None : <EOL> age = int ( time . time ( ) * MICROSECONDS ) <EOL> return self . __class__ ( self , age = age ) <EOL> def __str__ ( self ) : <EOL> return utils . SmartStr ( "<STR_LIT>" % self . _string_urn ) <EOL> def __unicode__ ( self ) : <EOL> return utils . SmartUnicode ( u"<STR_LIT>" % self . _string_urn ) <EOL> def __eq__ ( self , other ) : <EOL> if isinstance ( other , basestring ) : <EOL> other = self . __class__ ( other ) <EOL> elif other is None : <EOL> return False <EOL> elif not isinstance ( other , RDFURN ) : <EOL> return NotImplemented <EOL> return self . _string_urn == other . Path ( ) <EOL> def __ne__ ( self , other ) : <EOL> return not self . __eq__ ( other ) <EOL> def __lt__ ( self , other ) : <EOL> return self . _string_urn < other <EOL> def Path ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . _string_urn <EOL> def Split ( self , count = None ) : <EOL> """<STR_LIT>""" <EOL> if count : <EOL> result = filter ( None , self . _string_urn . split ( "<STR_LIT:/>" , count ) ) <EOL> while len ( result ) < count : <EOL> result . append ( "<STR_LIT>" ) <EOL> return result <EOL> else : <EOL> return filter ( None , self . _string_urn . split ( "<STR_LIT:/>" ) ) <EOL> def RelativeName ( self , volume ) : <EOL> """<STR_LIT>""" <EOL> string_url = utils . SmartUnicode ( self ) <EOL> volume_url = utils . SmartUnicode ( volume ) <EOL> if string_url . startswith ( volume_url ) : <EOL> result = string_url [ len ( volume_url ) : ] <EOL> return result . lstrip ( "<STR_LIT:/>" ) <EOL> return None <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" % ( str ( self ) , self . age ) <EOL> class Subject ( RDFURN ) : <EOL> """<STR_LIT>""" <EOL> @ staticmethod <EOL> def ContainsMatch ( unused_attribute , filter_implemention , regex ) : <EOL> return filter_implemention . SubjectContainsFilter ( regex ) <EOL> @ staticmethod <EOL> def Startswith ( unused_attribute , filter_implemention , string ) : <EOL> return filter_implemention . SubjectContainsFilter ( <EOL> "<STR_LIT>" + utils . EscapeRegex ( string ) ) <EOL> @ staticmethod <EOL> def HasAttribute ( unused_attribute , filter_implemention , string ) : <EOL> return filter_implemention . HasPredicateFilter ( string ) <EOL> operators = dict ( matches = ( <NUM_LIT:1> , "<STR_LIT>" ) , <EOL> contains = ( <NUM_LIT:1> , "<STR_LIT>" ) , <EOL> startswith = ( <NUM_LIT:1> , "<STR_LIT>" ) , <EOL> has = ( <NUM_LIT:1> , "<STR_LIT>" ) ) <EOL> DEFAULT_FLOW_QUEUE = RDFURN ( "<STR_LIT:F>" ) <EOL> class SessionID ( RDFURN ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( <EOL> self , initializer = None , age = None , base = "<STR_LIT>" , <EOL> queue = DEFAULT_FLOW_QUEUE , flow_name = None ) : <EOL> """<STR_LIT>""" <EOL> if initializer is None : <EOL> if flow_name is None : <EOL> flow_name = utils . PRNG . GetULong ( ) <EOL> if isinstance ( flow_name , int ) : <EOL> initializer = RDFURN ( base ) . Add ( "<STR_LIT>" % ( queue . Basename ( ) , flow_name ) ) <EOL> else : <EOL> initializer = RDFURN ( base ) . Add ( "<STR_LIT>" % ( queue . Basename ( ) , flow_name ) ) <EOL> elif isinstance ( initializer , RDFURN ) : <EOL> try : <EOL> self . ValidateID ( initializer . Basename ( ) ) <EOL> except ValueError as e : <EOL> raise InitializeError ( "<STR_LIT>" % <EOL> ( initializer , e . message ) ) <EOL> super ( SessionID , self ) . __init__ ( initializer = initializer , age = age ) <EOL> def Queue ( self ) : <EOL> return RDFURN ( self . Basename ( ) . split ( "<STR_LIT::>" ) [ <NUM_LIT:0> ] ) <EOL> def FlowName ( self ) : <EOL> return self . Basename ( ) . split ( "<STR_LIT::>" , <NUM_LIT:1> ) [ <NUM_LIT:1> ] <EOL> def Add ( self , path , age = None ) : <EOL> return RDFURN ( self ) . Add ( path , age = age ) <EOL> @ classmethod <EOL> def ValidateID ( cls , id_str ) : <EOL> allowed_re = re . compile ( r"<STR_LIT>" ) <EOL> if not allowed_re . match ( id_str ) : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> class FlowSessionID ( SessionID ) : <EOL> def ParseFromString ( self , initializer = None ) : <EOL> if not utils . SmartStr ( initializer ) . startswith ( "<STR_LIT>" ) : <EOL> initializer = "<STR_LIT>" + initializer <EOL> super ( FlowSessionID , self ) . ParseFromString ( initializer ) </s>
