<s> import logging <EOL> import re <EOL> import urllib <EOL> from bagpipe . bgp . common . utils import enum <EOL> log = logging . getLogger ( __name__ ) <EOL> LGMap = enum ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) <EOL> def _splitLGPath ( pathPrefix , path ) : <EOL> if len ( path ) == <NUM_LIT:0> : <EOL> return ( None , None , pathPrefix ) <EOL> else : <EOL> return ( path [ <NUM_LIT:0> ] , path [ <NUM_LIT:1> : ] , <EOL> "<STR_LIT>" % ( pathPrefix , urllib . quote ( path [ <NUM_LIT:0> ] ) ) ) <EOL> def _getLGLocalInfoRecurse ( obj , cls , pathPrefix ) : <EOL> if cls == LookingGlass : <EOL> return { } <EOL> log . debug ( "<STR_LIT>" , cls ) <EOL> result = cls . getLookingGlassLocalInfo ( obj , pathPrefix ) <EOL> assert ( isinstance ( result , dict ) ) <EOL> for base in cls . __bases__ : <EOL> if issubclass ( base , LookingGlass ) : <EOL> result . update ( <EOL> _getLGLocalInfoRecurse ( obj , base , pathPrefix ) ) <EOL> return result <EOL> def _getLGMapRecurse ( obj , cls ) : <EOL> if cls == LookingGlass : <EOL> return { } <EOL> log . debug ( "<STR_LIT>" , cls ) <EOL> result = cls . getLGMap ( obj ) <EOL> for base in cls . __bases__ : <EOL> if issubclass ( base , LookingGlass ) : <EOL> result . update ( _getLGMapRecurse ( obj , base ) ) <EOL> else : <EOL> log . debug ( "<STR_LIT>" , base ) <EOL> return result <EOL> def _lookupPathInDict ( myDict , path ) : <EOL> log . debug ( "<STR_LIT>" , myDict , path ) <EOL> assert ( isinstance ( path , list ) ) <EOL> if len ( path ) == <NUM_LIT:0> : <EOL> log . debug ( "<STR_LIT>" , myDict ) <EOL> return myDict <EOL> if not ( isinstance ( myDict , dict ) ) : <EOL> raise KeyError ( path [ <NUM_LIT:0> ] ) <EOL> else : <EOL> return _lookupPathInDict ( myDict [ path [ <NUM_LIT:0> ] ] , path [ <NUM_LIT:1> : ] ) <EOL> class LookingGlass ( object ) : <EOL> def _getLGMap ( self ) : <EOL> """<STR_LIT>""" <EOL> return _getLGMapRecurse ( self , self . __class__ ) <EOL> def getLGMap ( self ) : <EOL> """<STR_LIT>""" <EOL> return { } <EOL> def _getLookingGlassLocalInfo ( self , pathPrefix ) : <EOL> """<STR_LIT>""" <EOL> return _getLGLocalInfoRecurse ( self , self . __class__ , pathPrefix ) <EOL> def getLookingGlassLocalInfo ( self , pathPrefix ) : <EOL> """<STR_LIT>""" <EOL> return { } <EOL> def getLookingGlassInfo ( self , pathPrefix = "<STR_LIT>" , path = None ) : <EOL> """<STR_LIT>""" <EOL> if path is None : <EOL> path = [ ] <EOL> ( firstSegment , restOfPath , <EOL> newPathPrefix ) = _splitLGPath ( pathPrefix , path ) <EOL> lgMap = self . _getLGMap ( ) <EOL> if ( firstSegment in lgMap ) : <EOL> ( mappingType , mappingTarget ) = lgMap [ firstSegment ] <EOL> log . debug ( "<STR_LIT>" , <EOL> firstSegment , LGMap . reverse [ mappingType ] , mappingTarget ) <EOL> if mappingType == LGMap . VALUE : <EOL> return mappingTarget <EOL> if mappingType == LGMap . FORWARD : <EOL> log . debug ( <EOL> "<STR_LIT>" , path , mappingTarget ) <EOL> if not isinstance ( mappingTarget , LookingGlass ) : <EOL> log . error ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" , firstSegment , newPathPrefix ) <EOL> raise NoSuchLookingGlassObject ( <EOL> newPathPrefix , firstSegment ) <EOL> return mappingTarget . getLookingGlassInfo ( pathPrefix , path ) <EOL> if mappingType == LGMap . FORWARD : <EOL> log . debug ( <EOL> "<STR_LIT>" , path , mappingTarget ) <EOL> if not isinstance ( mappingTarget , LookingGlass ) : <EOL> log . error ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" , firstSegment , newPathPrefix ) <EOL> raise NoSuchLookingGlassObject ( newPathPrefix , firstSegment ) <EOL> return mappingTarget . getLookingGlassInfo ( pathPrefix , path ) <EOL> elif mappingType == LGMap . DELEGATE : <EOL> log . debug ( <EOL> "<STR_LIT>" , <EOL> path , mappingTarget ) <EOL> if not isinstance ( mappingTarget , LookingGlass ) : <EOL> log . error ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" , firstSegment , newPathPrefix ) <EOL> raise NoSuchLookingGlassObject ( newPathPrefix , firstSegment ) <EOL> return mappingTarget . getLookingGlassInfo ( newPathPrefix , <EOL> restOfPath ) <EOL> elif mappingType == LGMap . SUBITEM : <EOL> log . debug ( "<STR_LIT>" , firstSegment ) <EOL> try : <EOL> return _lookupPathInDict ( mappingTarget ( ) , restOfPath ) <EOL> except KeyError as e : <EOL> raise NoSuchLookingGlassObject ( newPathPrefix , str ( e ) ) <EOL> elif mappingType == LGMap . SUBTREE : <EOL> log . debug ( "<STR_LIT>" , firstSegment ) <EOL> try : <EOL> return _lookupPathInDict ( mappingTarget ( newPathPrefix ) , <EOL> restOfPath ) <EOL> except KeyError as e : <EOL> raise NoSuchLookingGlassObject ( newPathPrefix , str ( e ) ) <EOL> elif mappingType == LGMap . COLLECTION : <EOL> log . debug ( "<STR_LIT>" ) <EOL> ( listCallback , targetCallback ) = mappingTarget <EOL> ( secondSegment , restOfPath , newerPathPrefix ) = _splitLGPath ( newPathPrefix , restOfPath ) <EOL> if secondSegment is None : <EOL> log . debug ( "<STR_LIT>" , listCallback ) <EOL> result = [ ] <EOL> for x in listCallback ( ) : <EOL> x [ "<STR_LIT>" ] = LookingGlass . getLGPrefixedPath ( <EOL> pathPrefix , [ firstSegment , x [ "<STR_LIT:id>" ] ] ) <EOL> result . append ( x ) <EOL> return result <EOL> else : <EOL> log . debug ( "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> secondSegment , targetCallback , restOfPath ) <EOL> try : <EOL> target = targetCallback ( secondSegment ) <EOL> if target is None : <EOL> log . error ( "<STR_LIT>" , <EOL> secondSegment , newPathPrefix ) <EOL> raise NoSuchLookingGlassObject ( <EOL> newPathPrefix , secondSegment ) <EOL> if not isinstance ( target , LookingGlass ) : <EOL> log . error ( "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> secondSegment , newPathPrefix , <EOL> type ( target ) ) <EOL> raise NoSuchLookingGlassObject ( <EOL> newPathPrefix , secondSegment ) <EOL> return target . getLookingGlassInfo ( newerPathPrefix , <EOL> restOfPath ) <EOL> except KeyError : <EOL> raise NoSuchLookingGlassObject ( <EOL> newPathPrefix , secondSegment ) <EOL> info = self . _getLookingGlassLocalInfo ( pathPrefix ) <EOL> for ( pathItem , ( mappingType , mappingTarget ) ) in lgMap . iteritems ( ) : <EOL> if pathItem in info : <EOL> log . warning ( "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> pathItem ) <EOL> if mappingType in ( LGMap . FORWARD , LGMap . DELEGATE , LGMap . SUBTREE , <EOL> LGMap . COLLECTION ) : <EOL> info [ pathItem ] = { "<STR_LIT>" : <EOL> LookingGlass . getLGPrefixedPath ( pathPrefix , <EOL> [ pathItem ] ) } <EOL> elif mappingType == LGMap . SUBITEM : <EOL> log . debug ( "<STR_LIT>" , mappingTarget ) <EOL> info [ pathItem ] = mappingTarget ( ) <EOL> elif mappingType == LGMap . VALUE : <EOL> info [ pathItem ] = mappingTarget <EOL> else : <EOL> log . warning ( "<STR_LIT>" , pathItem ) <EOL> if firstSegment is None : <EOL> return info <EOL> else : <EOL> try : <EOL> return _lookupPathInDict ( info , path ) <EOL> except KeyError as e : <EOL> raise NoSuchLookingGlassObject ( newPathPrefix , str ( e ) ) <EOL> log . warning ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> return None <EOL> @ staticmethod <EOL> def getLGPrefixedPath ( pathPrefix , pathItems ) : <EOL> fmt = "<STR_LIT:%s>" + ( '<STR_LIT>' * len ( pathItems ) ) <EOL> quotedPathItems = [ urllib . quote ( pathItem ) for pathItem in pathItems ] <EOL> quotedPathItems . insert ( <NUM_LIT:0> , pathPrefix ) <EOL> return fmt % tuple ( quotedPathItems ) <EOL> class NoSuchLookingGlassObject ( Exception ) : <EOL> def __init__ ( self , pathPrefix , path ) : <EOL> Exception . __init__ ( self ) <EOL> assert ( isinstance ( pathPrefix , str ) ) <EOL> self . pathPrefix = pathPrefix <EOL> assert ( isinstance ( path , str ) ) <EOL> self . path = path <EOL> def __repr__ ( self ) : <EOL> return "<STR_LIT>" % ( self . path , <EOL> self . pathPrefix ) <EOL> class LookingGlassReferences ( object ) : <EOL> root = "<STR_LIT>" <EOL> references = { } <EOL> @ staticmethod <EOL> def setRoot ( urlPrefix ) : <EOL> LookingGlassReferences . root = urlPrefix <EOL> @ staticmethod <EOL> def setReferencePath ( reference , path ) : <EOL> LookingGlassReferences . references [ reference ] = path <EOL> @ staticmethod <EOL> def getAbsolutePath ( reference , pathPrefix , path = None ) : <EOL> if path is None : <EOL> path = [ ] <EOL> index = pathPrefix . find ( LookingGlassReferences . root ) <EOL> absoluteBaseURL = pathPrefix [ : index + len ( LookingGlassReferences . root ) ] <EOL> return LookingGlass . getLGPrefixedPath ( <EOL> absoluteBaseURL , <EOL> LookingGlassReferences . references [ reference ] + path ) <EOL> class LookingGlassLogHandler ( logging . Handler ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , level = logging . WARNING , maxSize = <NUM_LIT:100> ) : <EOL> logging . Handler . __init__ ( self , level ) <EOL> self . records = [ ] <EOL> self . maxSize = maxSize <EOL> self . setFormatter ( <EOL> logging . Formatter ( '<STR_LIT>' ) ) <EOL> def emit ( self , record ) : <EOL> record . msg = record . getMessage ( ) . replace ( '<STR_LIT:">' , "<STR_LIT:'>" ) <EOL> record . args = None <EOL> self . records . insert ( <NUM_LIT:0> , record ) <EOL> del self . records [ self . maxSize : ] <EOL> def __len__ ( self ) : <EOL> return len ( self . records ) <EOL> def getRecords ( self ) : <EOL> return self . records <EOL> def resetLocalLGLogs ( self ) : <EOL> del self . records [ : ] <EOL> class LookingGlassLocalLogger ( LookingGlass ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , appendToName = "<STR_LIT>" ) : <EOL> try : <EOL> self . lgLogHandler <EOL> except AttributeError : <EOL> self . lgLogHandler = LookingGlassLogHandler ( ) <EOL> name = self . __module__ <EOL> if appendToName : <EOL> name += "<STR_LIT:.>" + appendToName <EOL> elif ( hasattr ( self , '<STR_LIT>' ) ) : <EOL> name += "<STR_LIT>" % self . instanceId <EOL> elif ( hasattr ( self , '<STR_LIT:name>' ) ) : <EOL> name += "<STR_LIT>" % re . sub ( "<STR_LIT>" , "<STR_LIT:->" , self . name ) . lower ( ) <EOL> self . log = logging . getLogger ( name ) <EOL> self . log . addHandler ( self . lgLogHandler ) <EOL> def getLGMap ( self ) : <EOL> return { "<STR_LIT>" : ( LGMap . SUBTREE , self . getLogs ) } <EOL> def getLogs ( self , pathPrefix ) : <EOL> return [ { '<STR_LIT>' : record . levelname , <EOL> '<STR_LIT:time>' : self . lgLogHandler . formatter . formatTime ( record ) , <EOL> '<STR_LIT:message>' : record . msg } <EOL> for record in self . lgLogHandler . getRecords ( ) ] <EOL> def _resetLocalLGLogs ( self ) : <EOL> self . lgLogHandler . resetLocalLGLogs ( ) </s>
