<s> """<STR_LIT>""" <EOL> import itertools <EOL> import urllib <EOL> import logging <EOL> from grr . gui import renderers <EOL> from grr . gui . plugins import forms <EOL> from grr . lib import aff4 <EOL> from grr . lib import rdfvalue <EOL> from grr . lib import utils <EOL> from grr . lib . aff4_objects import aff4_grr <EOL> from grr . lib . rdfvalues import crypto as rdf_crypto <EOL> from grr . lib . rdfvalues import protodict as rdf_protodict <EOL> from grr . lib . rdfvalues import structs as rdf_structs <EOL> repeated_renderer_cache = { } <EOL> semantic_renderer_cache = { } <EOL> def FindRendererForObject ( rdf_obj ) : <EOL> """<STR_LIT>""" <EOL> if not semantic_renderer_cache : <EOL> for cls in RDFValueRenderer . classes . values ( ) : <EOL> if aff4 . issubclass ( cls , RDFValueArrayRenderer ) : <EOL> repeated_renderer_cache [ cls . classname ] = cls <EOL> elif aff4 . issubclass ( cls , RDFValueRenderer ) : <EOL> semantic_renderer_cache [ cls . classname ] = cls <EOL> rdf_obj_classname = rdf_obj . __class__ . __name__ <EOL> if isinstance ( rdf_obj , rdf_protodict . RDFValueArray ) : <EOL> return repeated_renderer_cache . get ( <EOL> rdf_obj_classname , RDFValueArrayRenderer ) ( rdf_obj ) <EOL> if isinstance ( rdf_obj , rdf_structs . RepeatedFieldHelper ) : <EOL> rdf_obj_classname = rdf_obj . type_descriptor . type . __name__ <EOL> return repeated_renderer_cache . get ( <EOL> rdf_obj_classname , RDFValueArrayRenderer ) ( rdf_obj ) <EOL> if isinstance ( rdf_obj , rdf_structs . RDFProtoStruct ) : <EOL> return semantic_renderer_cache . get ( <EOL> rdf_obj_classname , RDFProtoRenderer ) ( rdf_obj ) <EOL> if isinstance ( rdf_obj , rdfvalue . RDFValue ) : <EOL> return semantic_renderer_cache . get ( <EOL> rdf_obj_classname , RDFValueRenderer ) ( rdf_obj ) <EOL> elif isinstance ( rdf_obj , dict ) : <EOL> return DictRenderer ( rdf_obj ) <EOL> return RDFValueRenderer ( rdf_obj ) <EOL> class RDFValueColumn ( renderers . TableColumn ) : <EOL> """<STR_LIT>""" <EOL> def RenderRow ( self , index , request , row_options = None ) : <EOL> """<STR_LIT>""" <EOL> value = self . rows . get ( index ) <EOL> if value is None : <EOL> return "<STR_LIT>" <EOL> if row_options is not None : <EOL> row_options [ "<STR_LIT>" ] = index <EOL> if self . renderer : <EOL> renderer = self . renderer ( value ) <EOL> else : <EOL> renderer = FindRendererForObject ( value ) <EOL> if renderer : <EOL> result = renderer . RawHTML ( request ) <EOL> else : <EOL> result = utils . SmartStr ( value ) <EOL> return result <EOL> class AttributeColumn ( RDFValueColumn ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , name , ** kwargs ) : <EOL> self . attribute = aff4 . Attribute . GetAttributeByName ( name ) <EOL> super ( AttributeColumn , self ) . __init__ ( name , ** kwargs ) <EOL> def AddRowFromFd ( self , index , fd ) : <EOL> """<STR_LIT>""" <EOL> value = fd . Get ( self . attribute ) <EOL> try : <EOL> value = value . Payload ( ) <EOL> except AttributeError : <EOL> pass <EOL> if value is not None : <EOL> self . rows [ index ] = value <EOL> class RDFValueRenderer ( renderers . TemplateRenderer ) : <EOL> """<STR_LIT>""" <EOL> classname = "<STR_LIT>" <EOL> layout_template = renderers . Template ( """<STR_LIT>""" ) <EOL> def __init__ ( self , proxy = None , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> self . proxy = proxy <EOL> super ( RDFValueRenderer , self ) . __init__ ( ** kwargs ) <EOL> @ classmethod <EOL> def RendererForRDFValue ( cls , rdfvalue_cls_name ) : <EOL> """<STR_LIT>""" <EOL> for candidate in cls . classes . values ( ) : <EOL> if ( aff4 . issubclass ( candidate , RDFValueRenderer ) and <EOL> candidate . classname == rdfvalue_cls_name ) : <EOL> return candidate <EOL> class ValueRenderer ( RDFValueRenderer ) : <EOL> """<STR_LIT>""" <EOL> layout_template = renderers . Template ( """<STR_LIT>""" ) <EOL> def Layout ( self , request , response ) : <EOL> self . rdfvalue_type = self . proxy . __class__ . __name__ <EOL> try : <EOL> self . value = self . proxy . SerializeToString ( ) <EOL> except AttributeError : <EOL> self . value = utils . SmartStr ( self . proxy ) <EOL> renderer = FindRendererForObject ( self . proxy ) <EOL> self . rendered_value = renderer . RawHTML ( request ) <EOL> return super ( ValueRenderer , self ) . Layout ( request , response ) <EOL> class SubjectRenderer ( RDFValueRenderer ) : <EOL> """<STR_LIT>""" <EOL> classname = "<STR_LIT>" <EOL> layout_template = renderers . Template ( """<STR_LIT>""" ) <EOL> def Layout ( self , request , response ) : <EOL> if not self . proxy : <EOL> return <EOL> aff4_path = request . REQ . get ( "<STR_LIT>" , "<STR_LIT>" ) <EOL> aff4_path = rdfvalue . RDFURN ( aff4_path ) <EOL> self . basename = self . proxy . RelativeName ( aff4_path ) or self . proxy <EOL> self . aff4_path = self . proxy <EOL> self . tree_node_id = renderers . DeriveIDFromPath ( <EOL> "<STR_LIT:/>" . join ( self . aff4_path . Split ( ) [ <NUM_LIT:1> : ] ) ) <EOL> return super ( SubjectRenderer , self ) . Layout ( request , response ) <EOL> class RDFBytesRenderer ( RDFValueRenderer ) : <EOL> """<STR_LIT>""" <EOL> classname = "<STR_LIT>" <EOL> def Layout ( self , request , response ) : <EOL> self . proxy = utils . SmartStr ( self . proxy ) . encode ( "<STR_LIT>" ) <EOL> super ( RDFBytesRenderer , self ) . Layout ( request , response ) <EOL> class LiteralExpressionRenderer ( RDFBytesRenderer ) : <EOL> classname = "<STR_LIT>" <EOL> class RDFURNRenderer ( RDFValueRenderer ) : <EOL> """<STR_LIT>""" <EOL> classname = "<STR_LIT>" <EOL> layout_template = renderers . Template ( """<STR_LIT>""" ) <EOL> def Layout ( self , request , response ) : <EOL> client , rest = self . proxy . Split ( <NUM_LIT:2> ) <EOL> if aff4_grr . VFSGRRClient . CLIENT_ID_RE . match ( client ) : <EOL> h = dict ( main = "<STR_LIT>" , <EOL> c = client , <EOL> tab = "<STR_LIT>" , <EOL> t = renderers . DeriveIDFromPath ( rest ) ) <EOL> self . href = urllib . urlencode ( sorted ( h . items ( ) ) ) <EOL> super ( RDFURNRenderer , self ) . Layout ( request , response ) <EOL> class RDFProtoRenderer ( RDFValueRenderer ) : <EOL> """<STR_LIT>""" <EOL> name = "<STR_LIT>" <EOL> proxy_field = "<STR_LIT:data>" <EOL> layout_template = renderers . Template ( """<STR_LIT>""" ) <EOL> translator = { } <EOL> translator_error_template = renderers . Template ( "<STR_LIT>" ) <EOL> def Ignore ( self , unused_descriptor , unused_value ) : <EOL> """<STR_LIT>""" <EOL> return None <EOL> hrb_template = renderers . Template ( "<STR_LIT>" ) <EOL> def HumanReadableBytes ( self , _ , value ) : <EOL> """<STR_LIT>""" <EOL> return self . FormatFromTemplate ( self . hrb_template , value = value ) <EOL> pre_template = renderers . Template ( "<STR_LIT>" ) <EOL> def Pre ( self , _ , value ) : <EOL> return self . FormatFromTemplate ( self . pre_template , value = value ) <EOL> def Layout ( self , request , response ) : <EOL> """<STR_LIT>""" <EOL> self . result = [ ] <EOL> for descriptor , value in self . proxy . ListSetFields ( ) : <EOL> name = descriptor . name <EOL> friendly_name = descriptor . friendly_name or name <EOL> if name in self . translator : <EOL> try : <EOL> value = self . translator [ name ] ( self , request , value ) <EOL> if value is not None : <EOL> self . result . append ( ( friendly_name , descriptor . description , value ) ) <EOL> except KeyError : <EOL> value = self . FormatFromTemplate ( self . translator_error_template , <EOL> value = value ) <EOL> except Exception as e : <EOL> logging . warn ( "<STR_LIT>" . format ( name , e ) ) <EOL> value = "<STR_LIT>" <EOL> else : <EOL> renderer = FindRendererForObject ( value ) <EOL> self . result . append ( ( friendly_name , descriptor . description , <EOL> renderer . RawHTML ( request ) ) ) <EOL> return super ( RDFProtoRenderer , self ) . Layout ( request , response ) <EOL> class RDFValueArrayRenderer ( RDFValueRenderer ) : <EOL> """<STR_LIT>""" <EOL> layout_template = renderers . Template ( """<STR_LIT>""" ) <EOL> def Layout ( self , request , response ) : <EOL> """<STR_LIT>""" <EOL> start = int ( request . REQ . pop ( "<STR_LIT:start>" , <NUM_LIT:0> ) ) <EOL> length = int ( request . REQ . pop ( "<STR_LIT>" , <NUM_LIT:10> ) ) <EOL> cache = request . REQ . pop ( "<STR_LIT>" , None ) <EOL> if cache : <EOL> self . cache = aff4 . FACTORY . Open ( cache , token = request . token ) <EOL> self . proxy = rdf_protodict . RDFValueArray ( self . cache . Read ( <NUM_LIT> ) ) <EOL> else : <EOL> if len ( self . proxy ) > length : <EOL> with aff4 . FACTORY . Create ( None , "<STR_LIT>" , <EOL> token = request . token ) as self . cache : <EOL> data = rdf_protodict . RDFValueArray ( ) <EOL> data . Extend ( self . proxy ) <EOL> self . cache . Write ( data . SerializeToString ( ) ) <EOL> self . data = [ ] <EOL> self . next_start = <NUM_LIT:0> <EOL> for i , element in enumerate ( self . proxy ) : <EOL> if i < start : <EOL> continue <EOL> elif len ( self . data ) > length : <EOL> self . next_start = i <EOL> self . length = <NUM_LIT:100> <EOL> break <EOL> renderer = FindRendererForObject ( element ) <EOL> if renderer : <EOL> try : <EOL> self . data . append ( renderer . RawHTML ( request ) ) <EOL> except Exception as e : <EOL> logging . error ( <EOL> "<STR_LIT>" , type ( element ) , renderer , e ) <EOL> response = super ( RDFValueArrayRenderer , self ) . Layout ( request , response ) <EOL> if self . next_start : <EOL> response = self . CallJavascript ( response , "<STR_LIT>" , <EOL> next_start = self . next_start , <EOL> cache_urn = self . cache . urn , <EOL> array_length = self . length ) <EOL> return response <EOL> class DictRenderer ( RDFValueRenderer ) : <EOL> """<STR_LIT>""" <EOL> classname = "<STR_LIT>" <EOL> filter_keys = None <EOL> layout_template = renderers . Template ( """<STR_LIT>""" ) <EOL> translator_error_template = renderers . Template ( "<STR_LIT>" ) <EOL> def __init__ ( self , proxy = None , filter_keys = None , ** kwargs ) : <EOL> self . filter_keys = filter_keys or [ ] <EOL> super ( DictRenderer , self ) . __init__ ( proxy = proxy , ** kwargs ) <EOL> def Layout ( self , request , response ) : <EOL> """<STR_LIT>""" <EOL> self . data = [ ] <EOL> for key , value in sorted ( self . proxy . items ( ) ) : <EOL> rendered_value = None <EOL> if key in self . filter_keys : <EOL> continue <EOL> try : <EOL> renderer = FindRendererForObject ( value ) <EOL> if renderer : <EOL> rendered_value = renderer . RawHTML ( request ) <EOL> else : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> except TypeError : <EOL> rendered_value = self . FormatFromTemplate ( self . translator_error_template , <EOL> value = value ) <EOL> except Exception as e : <EOL> logging . warn ( "<STR_LIT>" . format ( type ( value ) , e ) ) <EOL> if rendered_value is not None : <EOL> self . data . append ( ( key , rendered_value ) ) <EOL> return super ( DictRenderer , self ) . Layout ( request , response ) <EOL> class ListRenderer ( RDFValueArrayRenderer ) : <EOL> classname = "<STR_LIT:list>" <EOL> class IconRenderer ( RDFValueRenderer ) : <EOL> width = <NUM_LIT:0> <EOL> layout_template = renderers . Template ( """<STR_LIT>""" ) <EOL> class RDFValueCollectionRenderer ( renderers . TableRenderer ) : <EOL> """<STR_LIT>""" <EOL> post_parameters = [ "<STR_LIT>" ] <EOL> size = <NUM_LIT:0> <EOL> show_total_count = True <EOL> layout_template = """<STR_LIT>""" + renderers . TableRenderer . layout_template <EOL> def __init__ ( self , ** kwargs ) : <EOL> super ( RDFValueCollectionRenderer , self ) . __init__ ( ** kwargs ) <EOL> self . AddColumn ( RDFValueColumn ( "<STR_LIT>" , width = "<STR_LIT>" ) ) <EOL> def BuildTable ( self , start_row , end_row , request ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> aff4_path = self . state . get ( "<STR_LIT>" ) or request . REQ . get ( "<STR_LIT>" ) <EOL> collection = aff4 . FACTORY . Open ( aff4_path , <EOL> aff4_type = "<STR_LIT>" , <EOL> token = request . token ) <EOL> except IOError : <EOL> return <EOL> try : <EOL> self . size = len ( collection ) <EOL> except AttributeError : <EOL> self . show_total_count = False <EOL> row_index = start_row <EOL> for value in itertools . islice ( collection , start_row , end_row ) : <EOL> self . AddCell ( row_index , "<STR_LIT>" , value ) <EOL> row_index += <NUM_LIT:1> <EOL> def Layout ( self , request , response , aff4_path = None ) : <EOL> if aff4_path : <EOL> self . state [ "<STR_LIT>" ] = str ( aff4_path ) <EOL> collection = aff4 . FACTORY . Create ( aff4_path , mode = "<STR_LIT:r>" , <EOL> aff4_type = "<STR_LIT>" , <EOL> token = request . token ) <EOL> try : <EOL> self . size = len ( collection ) <EOL> except AttributeError : <EOL> self . show_total_count = False <EOL> return super ( RDFValueCollectionRenderer , self ) . Layout ( <EOL> request , response ) <EOL> class FlowStateRenderer ( DictRenderer ) : <EOL> """<STR_LIT>""" <EOL> classname = "<STR_LIT>" <EOL> class DataObjectRenderer ( DictRenderer ) : <EOL> """<STR_LIT>""" <EOL> classname = "<STR_LIT>" <EOL> class AES128KeyFormRenderer ( forms . StringTypeFormRenderer ) : <EOL> """<STR_LIT>""" <EOL> type = rdf_crypto . AES128Key <EOL> layout_template = """<STR_LIT>""" + forms . TypeDescriptorFormRenderer . default_description_view + """<STR_LIT>""" <EOL> def Layout ( self , request , response ) : <EOL> self . default = str ( self . descriptor . type ( ) . Generate ( ) ) <EOL> response = super ( AES128KeyFormRenderer , self ) . Layout ( request , response ) <EOL> return self . CallJavascript ( response , "<STR_LIT>" , <EOL> prefix = self . prefix ) <EOL> class ClientURNRenderer ( RDFValueRenderer ) : <EOL> """<STR_LIT>""" <EOL> classname = "<STR_LIT>" <EOL> layout_template = renderers . Template ( """<STR_LIT>""" ) <EOL> ajax_template = renderers . Template ( """<STR_LIT>""" ) <EOL> def Layout ( self , request , response ) : <EOL> h = dict ( main = "<STR_LIT>" , c = self . proxy ) <EOL> self . hash = urllib . urlencode ( sorted ( h . items ( ) ) ) <EOL> response = super ( ClientURNRenderer , self ) . Layout ( request , response ) <EOL> return self . CallJavascript ( response , "<STR_LIT>" , <EOL> urn = utils . SmartStr ( self . proxy ) ) <EOL> def RenderAjax ( self , request , response ) : <EOL> self . urn = request . REQ . get ( "<STR_LIT>" ) <EOL> if self . urn : <EOL> fd = aff4 . FACTORY . Open ( self . urn , token = request . token ) <EOL> self . summary = FindRendererForObject ( fd . GetSummary ( ) ) . RawHTML ( request ) <EOL> return super ( ClientURNRenderer , self ) . RenderAjax ( request , response ) <EOL> class KeyValueFormRenderer ( forms . TypeDescriptorFormRenderer ) : <EOL> """<STR_LIT>""" <EOL> type = rdf_protodict . KeyValue <EOL> layout_template = renderers . Template ( """<STR_LIT>""" ) <EOL> def Layout ( self , request , response ) : <EOL> response = super ( KeyValueFormRenderer , self ) . Layout ( request , response ) <EOL> return self . CallJavascript ( response , "<STR_LIT>" ) <EOL> def GuessValueType ( self , value ) : <EOL> """<STR_LIT>""" <EOL> if value in [ "<STR_LIT:None>" , "<STR_LIT:none>" , None ] : <EOL> return None <EOL> if value in [ "<STR_LIT:True>" , "<STR_LIT:true>" , "<STR_LIT:yes>" ] : <EOL> return True <EOL> if value in [ "<STR_LIT:False>" , "<STR_LIT:false>" , "<STR_LIT>" ] : <EOL> return False <EOL> try : <EOL> return int ( value ) <EOL> except ( TypeError , ValueError ) : <EOL> try : <EOL> return float ( value ) <EOL> except ( TypeError , ValueError ) : <EOL> try : <EOL> return value . decode ( "<STR_LIT:utf8>" ) <EOL> except UnicodeDecodeError : <EOL> return value <EOL> def ParseArgs ( self , request ) : <EOL> """<STR_LIT>""" <EOL> key = request . REQ . get ( "<STR_LIT>" % self . prefix ) <EOL> value = request . REQ . get ( "<STR_LIT>" % self . prefix ) <EOL> value_type = request . REQ . get ( "<STR_LIT>" % self . prefix ) <EOL> if key is None : <EOL> return <EOL> result = rdf_protodict . KeyValue ( ) <EOL> result . k . SetValue ( key ) <EOL> if value_type is None : <EOL> value = self . GuessValueType ( value ) <EOL> elif value_type == "<STR_LIT>" : <EOL> value = int ( value ) <EOL> elif value_type == "<STR_LIT>" : <EOL> value = float ( value ) <EOL> elif value_type == "<STR_LIT>" : <EOL> if value in [ "<STR_LIT:True>" , "<STR_LIT:true>" , "<STR_LIT:yes>" , "<STR_LIT:1>" ] : <EOL> value = True <EOL> elif value in [ "<STR_LIT:False>" , "<STR_LIT:false>" , "<STR_LIT>" , "<STR_LIT:0>" ] : <EOL> value = False <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" % value ) <EOL> elif value_type == "<STR_LIT>" : <EOL> value = value . decode ( "<STR_LIT:utf8>" ) <EOL> result . v . SetValue ( value ) <EOL> return result </s>
