<s> class Page ( object ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self , pageref , entry , is_root_doc = True ) : <EOL> '''<STR_LIT>''' <EOL> self . pageref = pageref <EOL> self . referrers = set ( ) <EOL> self . startedDateTime = entry . startedDateTime <EOL> self . last_entry = entry <EOL> self . user_agent = entry . request . msg . headers . get ( '<STR_LIT>' ) <EOL> if is_root_doc : <EOL> self . root_document = entry <EOL> self . url = entry . request . url <EOL> self . title = self . url <EOL> else : <EOL> if '<STR_LIT>' in entry . request . msg . headers : <EOL> self . referrers . add ( entry . request . msg . headers [ '<STR_LIT>' ] ) <EOL> self . url = None <EOL> self . title = '<STR_LIT>' <EOL> def has_referrer ( self , ref ) : <EOL> '''<STR_LIT>''' <EOL> return ref == self . url or ref in self . referrers <EOL> def add ( self , entry ) : <EOL> '''<STR_LIT>''' <EOL> self . last_entry = entry <EOL> self . referrers . add ( entry . request . url ) <EOL> def json_repr ( self ) : <EOL> d = { <EOL> '<STR_LIT:id>' : self . pageref , <EOL> '<STR_LIT:title>' : self . title , <EOL> '<STR_LIT>' : default_page_timings <EOL> } <EOL> if self . startedDateTime : <EOL> d [ '<STR_LIT>' ] = self . startedDateTime . isoformat ( ) + '<STR_LIT>' <EOL> return d <EOL> default_page_timings = { <EOL> '<STR_LIT>' : - <NUM_LIT:1> , <EOL> '<STR_LIT>' : - <NUM_LIT:1> <EOL> } <EOL> def is_root_document ( entry ) : <EOL> '''<STR_LIT>''' <EOL> if entry . response : <EOL> mt = entry . response . mediaType <EOL> if mt . type == '<STR_LIT:text>' : <EOL> if mt . subtype in [ '<STR_LIT:html>' , '<STR_LIT>' , '<STR_LIT>' ] : <EOL> return True <EOL> return False <EOL> class PageTracker ( object ) : <EOL> '''<STR_LIT>''' <EOL> def __init__ ( self ) : <EOL> self . page_number = <NUM_LIT:0> <EOL> self . pages = [ ] <EOL> def getref ( self , entry ) : <EOL> '''<STR_LIT>''' <EOL> req = entry . request <EOL> referrer = req . msg . headers . get ( '<STR_LIT>' ) <EOL> user_agent = req . msg . headers . get ( '<STR_LIT>' ) <EOL> matched_page = None <EOL> for page in self . pages : <EOL> if page . user_agent and user_agent : <EOL> if page . user_agent != user_agent : <EOL> continue <EOL> if referrer and page . has_referrer ( referrer ) : <EOL> matched_page = page <EOL> break <EOL> if matched_page : <EOL> matched_page . add ( entry ) <EOL> return matched_page . pageref <EOL> else : <EOL> return self . new_ref ( entry ) <EOL> def new_ref ( self , entry ) : <EOL> '''<STR_LIT>''' <EOL> new_page = Page ( <EOL> self . new_id ( ) , <EOL> entry , <EOL> is_root_document ( entry ) ) <EOL> self . pages . append ( new_page ) <EOL> return new_page . pageref <EOL> def new_id ( self ) : <EOL> result = '<STR_LIT>' % self . page_number <EOL> self . page_number += <NUM_LIT:1> <EOL> return result <EOL> def json_repr ( self ) : <EOL> return sorted ( self . pages ) </s>
