2016-12-31 17:56:37 +01:00
import sys , os , json , logging
from optparse import OptionParser
2016-09-09 15:15:10 +02:00
2016-12-31 17:56:37 +01:00
reload ( sys )
sys . setdefaultencoding ( ' utf8 ' )
2016-09-09 15:15:10 +02:00
2016-12-31 17:56:37 +01:00
# matplot view/windows
import matplotlib
matplotlib . interactive ( True )
2016-09-09 15:15:10 +02:00
2016-12-31 17:56:37 +01:00
# pd display
import pandas as pd
pd . set_option ( ' display.max_colwidth ' , 100 )
2016-09-09 15:15:10 +02:00
2016-12-31 17:56:37 +01:00
import nettime . archive
import nettime . query
import nettime . report
2016-09-11 14:24:59 +02:00
2017-01-02 19:10:00 +01:00
class ReportDispatch :
def __init__ ( self , r = None ) :
if not isinstance ( r , nettime . report . Report ) :
logging . error ( " Rep constructor Error: r be of type nettime.report.Report " )
raise Exception ( )
self . r = r
def text ( self , command , params = None ) :
func = {
" tab_msgs_threads_replies " : self . r . tab_msgs_threads_replies ,
" tab_avg_rep_msg_thrd " : self . r . tab_avg_rep_msg_thrd ,
" tab_activity_from_ranking " : self . r . tab_activity_from_ranking ,
" tab_threads_replies_to_ranking " : self . r . tab_threads_replies_to_ranking ,
" tab_threads_initiated_from_ranking " : self . r . tab_threads_initiated_from_ranking ,
" tab_threads_activity_threads_initiated_avg_ranking " : self . r . tab_threads_activity_threads_initiated_avg_ranking ,
" tab_threads_initiated_replies_avg_ranking " : self . r . tab_threads_initiated_replies_avg_ranking ,
" tab_content_length_from_ranking " : self . r . tab_content_length_from_ranking ,
" tab_threads_ranking " : self . r . tab_threads_ranking ,
" tab_threads_ranking_year " : self . r . tab_threads_ranking_year ,
" tab_msgs_threads_replies_avg_rep_msg_thrd " : self . r . tab_msgs_threads_replies_avg_rep_msg_thrd ,
" tab_replies_ranking " : self . r . tab_replies_ranking ,
" tab_replies_avg_ranking " : self . r . tab_replies_avg_ranking
}
return func [ command ] ( )
def html ( self , command , params = None ) :
func = {
" html_msgs_threads_replies " : self . r . html_msgs_threads_replies ,
" html_avg_rep_msg_thrd " : self . r . html_avg_rep_msg_thrd ,
" html_activity_from_ranking " : self . r . html_activity_from_ranking ,
" html_threads_replies_to_ranking " : self . r . html_threads_replies_to_ranking ,
" html_threads_initiated_from_ranking " : self . r . html_threads_initiated_from_ranking ,
" html_threads_activity_threads_initiated_avg_ranking " : self . r . html_threads_activity_threads_initiated_avg_ranking ,
" html_threads_initiated_replies_avg_ranking " : self . r . html_threads_initiated_replies_avg_ranking ,
" html_content_length_from_ranking " : self . r . html_content_length_from_ranking ,
" html_threads_ranking " : self . r . html_threads_ranking ,
" html_threads_ranking_year " : self . r . html_threads_ranking_year ,
" html_msgs_threads_replies_avg_rep_msg_thrd " : self . r . html_msgs_threads_replies_avg_rep_msg_thrd ,
" html_replies_ranking " : self . r . html_replies_ranking ,
" html_replies_avg_ranking " : self . r . html_replies_avg_ranking
}
return func [ command ] ( )
2016-09-11 14:24:59 +02:00
2016-12-31 17:56:37 +01:00
def run ( options ) :
2016-09-11 14:24:59 +02:00
2016-12-31 17:56:37 +01:00
if options . input_script and os . path . isfile ( options . input_script ) :
with open ( options . input_script , ' r ' ) as fp :
input_script = json . load ( fp )
else :
2017-01-02 19:10:00 +01:00
print ' No input script. Nothing to do. '
return
if options . template_file and os . path . isfile ( options . template_file ) :
with open ( options . template_file , ' r ' ) as fp :
out = fp . read ( ) # not optimal but will do
else :
print ' No template file. Nothing to do. '
2016-12-31 17:56:37 +01:00
return
2016-09-11 14:24:59 +02:00
2017-01-02 19:10:00 +01:00
a = nettime . archive . Archive ( options . archive )
q = nettime . query . Query ( a )
r = nettime . report . Report ( q )
rep = ReportDispatch ( r )
2016-12-31 17:56:37 +01:00
for cmd in input_script :
2016-09-11 14:24:59 +02:00
2016-12-31 17:56:37 +01:00
if cmd [ ' format ' ] == ' html ' :
2017-01-02 19:10:00 +01:00
res = rep . html ( cmd [ ' command ' ] )
2016-12-31 17:56:37 +01:00
elif cmd [ ' format ' ] == ' text ' :
2017-01-02 19:10:00 +01:00
res = rep . text ( cmd [ ' command ' ] )
2016-12-31 17:56:37 +01:00
else :
continue
2016-09-11 14:24:59 +02:00
2016-12-31 17:56:37 +01:00
if res is not None :
out = out . replace ( cmd [ ' replace ' ] , res )
2016-09-11 14:24:59 +02:00
2016-12-31 17:56:37 +01:00
with open ( options . output_file , ' w ' ) as fp :
fp . write ( out ) # not optimal but will do
2016-09-11 14:24:59 +02:00
2016-12-31 17:56:37 +01:00
if __name__ == " __main__ " :
2016-09-11 14:24:59 +02:00
2016-12-31 17:56:37 +01:00
p = OptionParser ( ) ;
2017-01-02 19:15:31 +01:00
p . add_option ( ' -i ' , ' --input-script ' , action = " store " , help = " input (json) script mapping commands to text placeholders " )
p . add_option ( ' -o ' , ' --output-file ' , action = " store " , help = " report file to be generated " )
p . add_option ( ' -t ' , ' --template-file ' , action = " store " , help = " template file from which the report is generated " )
p . add_option ( ' -a ' , ' --archive ' , action = " store " , help = " the archive dir or file (.json.gz) to produce the report from (default= ' nettime-l_2016-12-31.json.gz ' ) " , default = " nettime-l_2016-12-31.json.gz " )
2016-09-11 14:24:59 +02:00
2016-12-31 17:56:37 +01:00
options , args = p . parse_args ( )
2016-09-11 14:24:59 +02:00
2017-01-02 19:10:00 +01:00
if options . input_script is None :
p . print_help ( )
p . error ( ' No input file specified. ' )
if options . output_file is None :
p . print_help ( )
p . error ( ' No output file specified. ' )
if options . template_file is None :
p . print_help ( )
p . error ( ' No template file specified. ' )
2016-12-31 17:56:37 +01:00
run ( options )
2016-09-09 15:15:10 +02:00