<s> __author__ = '<STR_LIT>' <EOL> __credits__ = [ "<STR_LIT>" , "<STR_LIT>" ] <EOL> __version__ = <NUM_LIT> <EOL> __license__ = "<STR_LIT>" <EOL> __maintainer__ = "<STR_LIT>" <EOL> __email__ = "<STR_LIT>" <EOL> import cgi <EOL> import operator <EOL> import re <EOL> import sys <EOL> import time <EOL> import urlparse <EOL> import argparse <EOL> import inspect <EOL> import apachelogs <EOL> class Rule ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , attr , cmp , val ) : <EOL> self . attr = attr <EOL> self . getter = operator . attrgetter ( attr ) <EOL> self . cmp = cmp <EOL> self . val = val <EOL> def __str__ ( self ) : <EOL> return '<STR_LIT:U+002C>' . join ( [ self . attr , self . cmp , self . val ] ) <EOL> class UrlDataRule ( object ) : <EOL> def __init__ ( self , attr , cmp , val ) : <EOL> def getter ( line ) : <EOL> return line . urldata . get ( attr , [ None ] ) [ <NUM_LIT:0> ] <EOL> self . getter = getter <EOL> self . attr = attr <EOL> self . cmp = cmp <EOL> self . val = val <EOL> def __str__ ( self ) : <EOL> return '<STR_LIT:U+002C>' . join ( [ self . attr , self . cmp , self . val ] ) <EOL> class Filter ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , file , ruleset , process_date , process_url , process_qstring ) : <EOL> self . log = file <EOL> self . rules = ruleset <EOL> self . process_date = process_date <EOL> self . process_url = process_url <EOL> self . process_qstring = process_qstring <EOL> self . baserex = re . compile ( "<STR_LIT>" ) <EOL> def strainer ( self ) : <EOL> """<STR_LIT>""" <EOL> for line in self . log : <EOL> if self . process_date : <EOL> line . time = line . time . split ( ) [ <NUM_LIT:0> ] <EOL> line . date = time . strptime ( line . time , '<STR_LIT>' ) <EOL> line . year , line . month , line . day , line . hour , line . minute , line . second = line . date [ <NUM_LIT:0> : <NUM_LIT:6> ] <EOL> if self . process_url : <EOL> line . urlbase = urlparse . urlparse ( line . url ) [ <NUM_LIT:2> ] <EOL> line . base = self . baserex . search ( line . urlbase ) <EOL> if not line . base : <EOL> line . base = line . urlbase . strip ( "<STR_LIT>" ) <EOL> else : <EOL> line . base = line . base . group ( <NUM_LIT:0> ) . strip ( "<STR_LIT>" ) <EOL> if self . process_qstring : <EOL> line . args = urlparse . urlparse ( line . url ) [ <NUM_LIT:4> ] <EOL> line . urldata = cgi . parse_qs ( line . args ) <EOL> show_line = True <EOL> for rule in self . rules : <EOL> show_line &= ( rule . val == rule . getter ( line ) ) <EOL> if not show_line : <EOL> break <EOL> if show_line : <EOL> yield line <EOL> class loghetti ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , args ) : <EOL> self . handler_map = self . opt_method_map ( ) <EOL> self . ruleset = [ ] <EOL> self . count = False <EOL> self . process_date = False <EOL> self . process_url = False <EOL> self . process_qstring = False <EOL> self . fields = False <EOL> self . nolazy = False <EOL> self . customOutput = False <EOL> for opt , val in args . __dict__ . iteritems ( ) : <EOL> """<STR_LIT>""" <EOL> if opt and val : <EOL> if opt in self . handler_map : <EOL> opt_handler = getattr ( self , self . handler_map [ opt ] ) <EOL> optval = getattr ( args , opt ) <EOL> opt_handler ( optval ) <EOL> else : <EOL> "<STR_LIT>" , opt , "<STR_LIT>" , val <EOL> setattr ( self , opt , val ) <EOL> def optionHandler_nolazy ( self , dest = None ) : <EOL> self . nolazy = True <EOL> self . process_date = True <EOL> self . process_url = True <EOL> self . process_qstring = True <EOL> def optionHandler_code ( self , respcode , dest = None ) : <EOL> """<STR_LIT>""" <EOL> coderule = Rule ( "<STR_LIT>" , "<STR_LIT:=>" , respcode ) <EOL> self . ruleset . append ( coderule ) <EOL> return <EOL> def optionHandler_count ( self , val ) : <EOL> """<STR_LIT>""" <EOL> self . count = val <EOL> return <EOL> def optionHandler_ip ( self , ip , dest = None ) : <EOL> """<STR_LIT>""" <EOL> self . iprule = Rule ( "<STR_LIT>" , "<STR_LIT:=>" , ip ) <EOL> self . ruleset . append ( self . iprule ) <EOL> return <EOL> def optionHandler_month ( self , month , dest = None ) : <EOL> """<STR_LIT>""" <EOL> self . monthrule = Rule ( "<STR_LIT>" , "<STR_LIT:=>" , int ( month ) ) <EOL> self . ruleset . append ( self . monthrule ) <EOL> self . process_date = True <EOL> return <EOL> def optionHandler_day ( self , day , dest = None ) : <EOL> """<STR_LIT>""" <EOL> self . dayrule = Rule ( "<STR_LIT>" , "<STR_LIT:=>" , int ( day ) ) <EOL> self . ruleset . append ( self . dayrule ) <EOL> self . process_date = True <EOL> return <EOL> def optionHandler_year ( self , year ) : <EOL> """<STR_LIT>""" <EOL> self . yearrule = Rule ( "<STR_LIT>" , "<STR_LIT:=>" , int ( year ) ) <EOL> self . ruleset . append ( self . yearrule ) <EOL> self . process_date = True <EOL> return <EOL> def optionHandler_hour ( self , hour ) : <EOL> """<STR_LIT>""" <EOL> self . hourrule = Rule ( "<STR_LIT>" , "<STR_LIT:=>" , int ( hour ) ) <EOL> self . ruleset . append ( self . hourrule ) <EOL> self . process_date = True <EOL> return <EOL> def optionHandler_minute ( self , minute ) : <EOL> """<STR_LIT>""" <EOL> self . minuterule = Rule ( "<STR_LIT>" , "<STR_LIT:=>" , int ( minute ) ) <EOL> self . ruleset . append ( self . minuterule ) <EOL> self . process_date = True <EOL> return <EOL> def optionHandler_urlbase ( self , urlbase ) : <EOL> """<STR_LIT>""" <EOL> self . urlrule = Rule ( "<STR_LIT>" , "<STR_LIT:=>" , urlbase ) <EOL> self . ruleset . append ( self . urlrule ) <EOL> self . process_url = True <EOL> return <EOL> def optionHandler_method ( self , method ) : <EOL> """<STR_LIT>""" <EOL> self . methodrule = Rule ( "<STR_LIT>" , "<STR_LIT:=>" , method ) <EOL> self . ruleset . append ( self . methodrule ) <EOL> return <EOL> def optionHandler_urldata ( self , keyval ) : <EOL> """<STR_LIT>""" <EOL> key , val = keyval . split ( '<STR_LIT::>' ) <EOL> self . urldatarule = UrlDataRule ( key , "<STR_LIT:=>" , val ) <EOL> self . ruleset . append ( self . urldatarule ) <EOL> self . process_qstring = True <EOL> return <EOL> def optionHandler_return ( self , fields ) : <EOL> """<STR_LIT>""" <EOL> self . fields = fields . split ( '<STR_LIT:U+002C>' ) <EOL> return <EOL> def optionHandler_output ( self , methodname ) : <EOL> """<STR_LIT>""" <EOL> self . customOutput = True <EOL> self . outmod = __import__ ( methodname ) <EOL> return <EOL> def opt_method_map ( self ) : <EOL> meth_prefix = '<STR_LIT>' <EOL> mapping = { } <EOL> for meth in inspect . getmembers ( self . __class__ , inspect . ismethod ) : <EOL> opt = meth [ <NUM_LIT:0> ] [ len ( meth_prefix ) : ] <EOL> if opt : <EOL> mapping [ opt ] = meth [ <NUM_LIT:0> ] <EOL> return mapping <EOL> def main ( self ) : <EOL> """<STR_LIT>""" <EOL> log = apachelogs . ApacheLogFile ( self . logfile ) <EOL> myfilter = Filter ( log , self . ruleset , self . process_date , self . process_url , self . process_qstring ) <EOL> if self . customOutput : <EOL> for line in myfilter . strainer ( ) : <EOL> self . outmod . munge ( line ) <EOL> else : <EOL> count = <NUM_LIT:0> <EOL> for line in myfilter . strainer ( ) : <EOL> if self . fields : <EOL> flist = [ ] <EOL> for field in self . fields : <EOL> try : <EOL> flist . append ( getattr ( line , field ) ) <EOL> except AttributeError as out : <EOL> print "<STR_LIT>" % str ( field ) <EOL> return <EOL> for item in flist : <EOL> print item , <EOL> print <EOL> continue <EOL> if self . count : <EOL> count += <NUM_LIT:1> <EOL> else : <EOL> print line <EOL> if self . count : <EOL> print "<STR_LIT>" , count <EOL> if __name__ == "<STR_LIT:__main__>" : <EOL> parser = argparse . ArgumentParser ( description = "<STR_LIT>" ) <EOL> parser . add_argument ( '<STR_LIT>' , action = '<STR_LIT:store>' , dest = '<STR_LIT:code>' , help = "<STR_LIT>" ) <EOL> parser . add_argument ( '<STR_LIT>' , action = '<STR_LIT:store_true>' , dest = '<STR_LIT:count>' , help = "<STR_LIT>" ) <EOL> parser . add_argument ( '<STR_LIT>' , action = '<STR_LIT:store>' , dest = '<STR_LIT>' , help = "<STR_LIT>" ) <EOL> parser . add_argument ( '<STR_LIT>' , action = '<STR_LIT:store>' , dest = '<STR_LIT>' , help = "<STR_LIT>" ) <EOL> parser . add_argument ( '<STR_LIT>' , action = '<STR_LIT:store>' , dest = '<STR_LIT>' , help = "<STR_LIT>" ) <EOL> parser . add_argument ( '<STR_LIT>' , action = '<STR_LIT:store>' , dest = '<STR_LIT>' , help = "<STR_LIT>" ) <EOL> parser . add_argument ( '<STR_LIT>' , action = '<STR_LIT:store>' , dest = '<STR_LIT>' , help = "<STR_LIT>" ) <EOL> parser . add_argument ( '<STR_LIT>' , action = '<STR_LIT:store>' , dest = '<STR_LIT>' , help = "<STR_LIT>" ) <EOL> parser . add_argument ( '<STR_LIT>' , action = '<STR_LIT:store>' , dest = '<STR_LIT>' , help = "<STR_LIT>" ) <EOL> parser . add_argument ( '<STR_LIT>' , action = '<STR_LIT:store>' , dest = '<STR_LIT>' , help = "<STR_LIT>" ) <EOL> parser . add_argument ( '<STR_LIT>' , action = '<STR_LIT:store>' , dest = '<STR_LIT>' , <EOL> help = """<STR_LIT>""" ) <EOL> args = parser . parse_args ( ) <EOL> l = loghetti ( args ) <EOL> l . main ( ) </s>
