2020-04-26 23:34:17 +01:00
#!/usr/bin/env python
2020-04-26 23:41:29 +01:00
import sys , os , operator , urllib , json , re , time
2020-04-26 23:47:04 +01:00
from datetime import datetime
2020-04-26 23:34:17 +01:00
2020-04-26 23:41:29 +01:00
# 2017 originally by Martin Green
# 2018-08-27 edited Philip Sargent
2020-04-26 23:47:04 +01:00
# 2019-03-02 extended to take command line argument of loser_dir and set mod time of index.html to be same as json file
2020-04-26 23:41:29 +01:00
# 2019-12-17 extra output of links to troggle-generated trip data
2020-04-26 23:47:04 +01:00
# 2019-12-31 bits to make website link-checker not barf so much. Added endswith() to .startswith() for notes, elev, plan filenames
# 2020-01-21 Now we are using Windows10-WSL1, +links to expedition logbook on every generated page
# 2020-03-15 Adding timestamp to visible outputs, changing name of produced files to walletindex.html so that contents can be browsed
# 2020-03-15 Added "ignore" to the <year>#00 folder containing scraps - then removed as we do
# want it to appear in the reports under "UNKNOWN"
2020-04-26 23:36:25 +01:00
2020-04-26 23:47:04 +01:00
loser_dir = " /home/expo/loser "
#loser_dir = "/mnt/d/CUCC-Expo/Loser/" # when running on Win10-WSL1
2020-04-26 23:41:29 +01:00
#loser_dir = "/media/philip/SD-huge/CUCC-Expo/loser/" # when running on xubuntu laptop 'barbie'
if len ( sys . argv ) > 1 :
if sys . argv [ 1 ] != " " :
loser_dir = sys . argv [ 1 ]
2020-04-26 23:47:04 +01:00
dateTimeObj = datetime . now ( tz = None )
timestamp = dateTimeObj . strftime ( " %d - % b- % Y ( % H: % M) " )
print " Loser repo (for svx files) is assumed to be in: " + loser_dir + " / "
2020-04-26 23:42:43 +01:00
drawings_dir = loser_dir [ 0 : len ( loser_dir ) - 5 ] + " drawings "
2020-04-26 23:47:04 +01:00
print " Drawings repo (for drawings files) is assumed to be in: " + drawings_dir + " / "
2020-04-26 23:41:29 +01:00
2020-04-26 23:34:17 +01:00
html_base = " <html><body> %(body)s </body></html> "
2020-04-26 23:47:04 +01:00
html_year_index = html_base % { " body " : " <H1> %(year)s surveys: wallets status</H1> \n <p>List of trips: <a href= \" http://expo.survex.com/expedition/ %(year)s \" >expedition/ %(year)s </a> - troggle-processed .svx files and logbook entries on server</p> \n As of %(timestamp)s \n <H2>Persons</H2> \n <UL> \n %(persons)s </UL> \n <H2>Wallets</H2> \n <table> %(wallets)s </table> \n <H2>Needing Scanning</H2> \n <UL> \n % (needing scanning)s</ul> \n <H2>Website (Guidebook description) needing updating \n </H2> \n <UL style= \" column-count: 3; \" > \n % (website needing updating)s</ul> \n " }
2020-04-26 23:41:29 +01:00
html_year_person = " <li><a href= ' %(person)s .html ' > %(person)s </a><UL> \n %(complaints)s </ul></li> \n "
html_year_wallet_entry = " <tr><td><a href= ' %(walletindex)s ' > %(walletname)s %(cave)s %(name)s </a></td> <td> %(complaints)s </td></tr> \n "
html_person_wallet_entry = " <li><a href= ' %(walletindex)s ' > %(walletname)s </a> <UL> \n %(complaints)s </ul></li> \n "
html_year_scanning_entry = " <li><a href= ' %(walletindex)s ' > %(walletname)s %(cave)s %(name)s </a></li> \n "
html_wallet_file_entry = " <li><a href= ' %(fileurl)s ' > %(filename)s </a></li> \n "
2020-04-26 23:47:04 +01:00
html_wallet_index = html_base % { " body " : " <H1> %(title)s : %(cave)s : %(name)s </H1> \n <p>List of trips: <a href= \" http://expo.survex.com/expedition/ %(year)s \" >expedition/ %(year)s </a> - troggle-processed .svx files and logbook entries on server</p> \n <p>Date: %(date)s </p><p>People: %(people)s </p> \n <p>Cave <a href= ' %(description)s ' >Guidebook description</a> - %(description_needed)s \n <p>Survex file:<br> <br> Local: <a href= ' file:/// %(loser_dir)s / %(survex)s ' download>file:/// %(loser_dir)s / %(survex)s </a><br> Server: <a href= ' http://expo.survex.com/survexfile/ %(survex)s ' download>http://expo.survex.com/survexfile/ %(survex)s </a></p><a href= ' ../walletindex.html ' >Wallet index for this year</a><br/>Local location for ::loser:: repo specified on command line is <a href= ' file:/// %(loser_dir)s ' > %(loser_dir)s </a>. </p> \n <H2>Issues</H2> \n %(complaints)s \n <H2>Files</H2> \n <UL> \n %(files)s </UL> \n " }
2020-04-26 23:34:17 +01:00
html_survex_required = { True : " Survex " , False : " " }
html_plan_scanned = { True : " " , False : " Plan " }
html_elev_scanned = { True : " " , False : " Elev " }
html_description_written = { True : " " , False : " Desc " }
html_qms_written = { True : " " , False : " QMs " }
html_status = { True : " Issues: " , False : " " }
2020-04-26 23:47:04 +01:00
html_person = html_base % { " body " : " <H1> %(person)s </H1> \n <p>List of trips: <a href= \" http://expo.survex.com/expedition/ %(year)s \" >expedition/ %(year)s </a> - troggle-processed .svx files and logbook entries on server</p> \n <H2>Outstanding Wallets</H2> \n As of %(timestamp)s \n <UL> \n %(wallets)s </UL> " }
2020-04-26 23:34:17 +01:00
html_complaint_items = " <li> %(count)i %(complaint)s </li> "
html_items = " <li> %s </li> "
2020-04-26 23:47:04 +01:00
2020-04-26 23:41:29 +01:00
blank_json = {
" cave " : " " ,
" date " : " " ,
2020-04-26 23:47:04 +01:00
" description url " : " /caves " ,
2020-04-26 23:41:29 +01:00
" description written " : False ,
" electronic survey " : False ,
" elev drawn " : False ,
" elev not required " : False ,
" name " : " " ,
" people " : [
" Unknown "
] ,
" plan drawn " : False ,
" plan not required " : False ,
" qms written " : False ,
" survex file " : " " ,
" survex not required " : False ,
" website updated " : False }
2020-04-26 23:34:17 +01:00
2020-04-26 23:42:43 +01:00
2020-04-26 23:34:17 +01:00
#need to use wallets as a dict/tuple (id,cave,name) - not sure how.
wallets = [ ]
wallets_needing_scanning = set ( )
website_needing_updating = set ( )
people = { }
2020-04-26 23:36:25 +01:00
#use dir this file is in to get current year
path , year = os . path . split ( os . path . dirname ( os . path . realpath ( __file__ ) ) )
2020-04-26 23:41:29 +01:00
print " Year: " + year
for item in sorted ( os . listdir ( " . " ) ) :
2020-04-26 23:47:04 +01:00
if os . path . isdir ( item ) and item != year + " indexpages " :
2020-04-26 23:34:17 +01:00
files = [ ]
for f in os . listdir ( os . path . join ( " . " , item ) ) :
2020-04-26 23:47:04 +01:00
if f not in [ " contents.json " , " contents.json~ " , " walletindex.html " ] and os . path . isfile ( os . path . join ( " . " , item , f ) ) :
2020-04-26 23:34:17 +01:00
files . append ( f )
contents_path = os . path . join ( " . " , item , " contents.json " )
2020-04-26 23:47:04 +01:00
#print "Trying to read file %s" % (contents_path)
2020-04-26 23:34:17 +01:00
if not os . path . isfile ( contents_path ) :
2020-04-26 23:41:29 +01:00
print " Creating file %s from template " % ( contents_path )
2020-04-26 23:34:17 +01:00
json_file = open ( contents_path , " w " )
2020-04-26 23:41:29 +01:00
json . dump ( blank_json , json_file , sort_keys = True , indent = 1 )
2020-04-26 23:34:17 +01:00
json_file . close ( )
2020-04-26 23:47:04 +01:00
#print "Reading file %s" % (contents_path)
2020-04-26 23:34:17 +01:00
json_file = open ( contents_path )
2020-04-26 23:47:04 +01:00
try :
data = json . load ( json_file )
except :
print " FAILURE parsing JSON file %s " % ( contents_path )
# Python bug: https://github.com/ShinNoNoir/twitterwebsearch/issues/12
raise
if not data [ " people " ] :
data [ " people " ] = [ " NOBODY " ]
2020-04-26 23:34:17 +01:00
json_file . close ( )
write_required = False
try :
wallet , cave , name = re . match ( " ( \ d \ d \ d \ d# \ d \ d)-(.*) (.*) " , item ) . groups ( )
except :
wallet , cave , name = " " , " " , " "
#print data
for k , v in blank_json . items ( ) :
if not data . has_key ( k ) :
if k == " cave " :
data [ k ] = cave
elif k == " name " :
data [ k ] = name
else :
data [ k ] = v
write_required = True
#print write_required
if write_required :
2020-04-26 23:41:29 +01:00
print " Writing file %s " % ( contents_path )
2020-04-26 23:34:17 +01:00
json_file = open ( contents_path , " w " )
json . dump ( data , json_file , indent = 1 )
2020-04-26 23:42:43 +01:00
json_file . close ( )
2020-04-26 23:41:29 +01:00
# Get modification time of contents.json
# print("json last modified: %s" % time.ctime(os.path.getmtime(contents_path)))
json_mtime = os . path . getmtime ( contents_path )
2020-04-26 23:34:17 +01:00
#make wallet descriptions
#Survex
survex_required = ( data [ " survex not required " ] and data [ " survex file " ] == " " ) or \
not ( not data [ " survex not required " ] and os . path . isfile ( os . path . join ( loser_dir , data [ " survex file " ] ) ) )
survex_complaint = " "
if data [ " survex not required " ] and data [ " survex file " ] != " " :
survex_complaint = " Survex is not required and yet there is a survex file! "
if not data [ " survex not required " ] and data [ " survex file " ] == " " :
survex_complaint = " A survex file is required, but has not been specified! "
if not data [ " survex not required " ] and not os . path . isfile ( os . path . join ( loser_dir , data [ " survex file " ] ) ) :
2020-04-26 23:41:29 +01:00
survex_complaint = " The specified survex file ( %s ) does not exist here! " % os . path . join ( loser_dir , data [ " survex file " ] )
2020-04-26 23:34:17 +01:00
complaints = [ ]
person_complaints = [ ]
if survex_required :
complaints . append ( survex_complaint )
person_complaints . append ( survex_complaint )
#Notes
notes_scanned = reduce ( operator . or_ , [ f . startswith ( " note " ) for f in files ] , False )
2020-04-26 23:42:43 +01:00
notes_scanned = reduce ( operator . or_ , [ f . endswith ( " note " ) for f in files ] , notes_scanned )
2020-04-26 23:34:17 +01:00
if not notes_scanned :
2020-04-26 23:42:43 +01:00
complaints . append ( " The notes needs scanning (no noteNN.jpg or XXnote.jpg file found) " )
2020-04-26 23:34:17 +01:00
wallets_needing_scanning . add ( item )
#Plan drawing required
plan_scanned = reduce ( operator . or_ , [ f . startswith ( " plan " ) for f in files ] , False )
2020-04-26 23:42:43 +01:00
plan_scanned = reduce ( operator . or_ , [ f . endswith ( " plan " ) for f in files ] , plan_scanned )
2020-04-26 23:34:17 +01:00
plan_drawing_required = not ( plan_scanned or data [ " plan drawn " ] )
if plan_drawing_required :
2020-04-26 23:42:43 +01:00
complaints . append ( " The plan needs drawing (no planNN.jpg or XXplan.jpg file found) " )
person_complaints . append ( " plan(s) needs drawing (no planNN.jpg or XXplan.jpg file found) " )
2020-04-26 23:34:17 +01:00
if not plan_drawing_required and not plan_scanned :
2020-04-26 23:42:43 +01:00
complaints . append ( " The plan needs <em>scanning</em> (no planNN.jpg or XXplan.jpg file found) " )
2020-04-26 23:34:17 +01:00
wallets_needing_scanning . add ( item )
#Elev drawing required
elev_scanned = reduce ( operator . or_ , [ f . startswith ( " elev " ) for f in files ] , False )
2020-04-26 23:42:43 +01:00
elev_scanned = reduce ( operator . or_ , [ f . endswith ( " elev " ) for f in files ] , elev_scanned )
2020-04-26 23:34:17 +01:00
elev_drawing_required = not ( elev_scanned or data [ " elev drawn " ] )
if elev_drawing_required :
2020-04-26 23:42:43 +01:00
complaints . append ( " The elev needs drawing (no elevNN.jpg or XXelev.jpg file found) " )
person_complaints . append ( " elev(s) needs drawing (no elevNN.jpg or XXelev.jpg file found) " )
2020-04-26 23:34:17 +01:00
if not elev_drawing_required and not elev_scanned :
2020-04-26 23:42:43 +01:00
complaints . append ( " The elev needs <em>scanning</em> (no elevNN.jpg or XXelev.jpg file found) " )
2020-04-26 23:34:17 +01:00
wallets_needing_scanning . add ( item )
#Description
2020-04-26 23:41:29 +01:00
if not data [ " description written " ] :
complaints . append ( " The description needs writing " )
person_complaints . append ( " description(s) needs writing " )
2020-04-26 23:42:43 +01:00
description_needed = " A description is indicated as being needed, so may need adding into this cave page. "
2020-04-26 23:41:29 +01:00
2020-04-26 23:34:17 +01:00
#QMS
if not data [ " qms written " ] :
2020-04-26 23:37:29 +01:00
complaints . append ( " The QMs needs writing " )
person_complaints . append ( " set(s) of QMs needs writing " )
2020-04-26 23:34:17 +01:00
#Website
if not data [ " website updated " ] :
2020-04-26 23:42:43 +01:00
complaints . append ( " The website is marked as needing updating (using the guidebook description) " )
2020-04-26 23:34:17 +01:00
website_needing_updating . add ( item )
#Electronic Surveys
if not data [ " electronic survey " ] :
2020-04-26 23:42:43 +01:00
complaints . append ( " Tunnel / Therion drawing files need drawing " )
2020-04-26 23:34:17 +01:00
if data [ " survex file " ] :
survex_description = data [ " survex file " ]
else :
survex_description = " Not specified "
2020-04-26 23:47:04 +01:00
wallet_index_file = open ( os . path . join ( item , " walletindex.html " ) , " w " )
wallet_index_file . write ( html_wallet_index % { " title " : item , " year " : year ,
2020-04-26 23:34:17 +01:00
" cave " : data [ " cave " ] ,
" name " : data [ " name " ] ,
" date " : data [ " date " ] ,
2020-04-26 23:41:29 +01:00
" people " : reduce ( operator . add , [ " %s , " % person for person in data [ " people " ] ] , " " ) ,
" description " : " http://expo.survex.com " + data [ " description url " ] ,
" description_needed " : description_needed ,
" loser_dir " : loser_dir ,
" loser_dirw " : loser_dir [ 5 ] . upper ( ) + ' :/ ' + loser_dir [ 7 : ] ,
2020-04-26 23:34:17 +01:00
" survex " : survex_description ,
" complaints " : reduce ( operator . add , [ " <p> " + complaint + " </p> " for complaint in complaints ] , " " ) ,
" files " : reduce ( operator . add ,
[ html_wallet_file_entry % { " fileurl " : urllib . quote ( f ) ,
" filename " : f }
for f
in files ] ,
" " ) } )
wallet_index_file . close ( )
wallets . append ( ( item , data [ " cave " ] , data [ " name " ] , survex_required , plan_scanned , elev_scanned , data [ " description written " ] , data [ " qms written " ] ) )
2020-04-26 23:41:29 +01:00
# Set modification time to be the same as that of contents.json
2020-04-26 23:47:04 +01:00
index_file = item + " /walletindex.html "
2020-04-26 23:41:29 +01:00
os . utime ( index_file , ( json_mtime , json_mtime ) )
2020-04-26 23:34:17 +01:00
#People
2020-04-26 23:37:29 +01:00
for person in data [ " people " ] :
# delete all person.html as we are recreating all the ones that matter and old ones have old data
if os . path . isfile ( person + " .html " ) :
os . remove ( person + " .html " )
2020-04-26 23:34:17 +01:00
if person_complaints :
for person in data [ " people " ] :
if not people . has_key ( person ) :
people [ person ] = [ ]
people [ person ] . append ( ( item , person_complaints ) )
wallets . sort ( )
website_needing_updating = list ( website_needing_updating )
website_needing_updating . sort ( )
wallets_needing_scanning = list ( wallets_needing_scanning )
wallets_needing_scanning . sort ( )
person_summary = [ ]
for person , person_wallets in people . items ( ) :
complaints = reduce ( operator . add , [ complaints for wallet , complaints in person_wallets ] , [ ] )
complaints_summary = [ ]
for complaint in set ( complaints ) :
complaints_summary . append ( ( complaint , complaints . count ( complaint ) ) )
person_summary . append ( ( person , complaints_summary ) )
person_summary = dict ( person_summary )
2020-04-26 23:47:04 +01:00
year_index_file = open ( " walletindex.html " , " w " )
year_index_file . write ( html_year_index % { " year " : year , " timestamp " : timestamp , " persons " : reduce ( operator . add , [ html_year_person % { " person " : person ,
2020-04-26 23:34:17 +01:00
" complaints " : reduce ( operator . add ,
[ html_complaint_items % { " complaint " : complaint ,
" count " : count }
for complaint , count
in complaints ] ,
" " ) }
for person , complaints
in person_summary . items ( ) ] , " " ) ,
" needing scanning " : reduce ( operator . add , [ html_year_scanning_entry % { " walletname " : wallet ,
" cave " : cave ,
" name " : name ,
2020-04-26 23:47:04 +01:00
" walletindex " : urllib . quote ( wallet ) + " /walletindex.html " }
2020-04-26 23:34:17 +01:00
for ( wallet )
in wallets_needing_scanning ] , " " ) ,
" website needing updating " : reduce ( operator . add , [ html_year_scanning_entry % { " walletname " : wallet ,
" cave " : cave ,
" name " : name ,
2020-04-26 23:47:04 +01:00
" walletindex " : urllib . quote ( wallet ) + " /walletindex.html " }
2020-04-26 23:34:17 +01:00
for ( wallet )
in website_needing_updating ] , " " ) ,
" wallets " : reduce ( operator . add ,
[ html_year_wallet_entry % { " walletname " : wallet ,
" cave " : cave ,
" name " : name ,
2020-04-26 23:47:04 +01:00
" walletindex " : urllib . quote ( wallet ) + " /walletindex.html " ,
2020-04-26 23:34:17 +01:00
" complaints " : html_status [ survex_required or not plan_scanned or not elev_scanned or description_written ] + html_survex_required [ survex_required ] + html_plan_scanned [ plan_scanned ] + html_elev_scanned [ elev_scanned ] + html_description_written [ description_written ] + html_qms_written [ qms_written ] }
for ( wallet , cave , name , survex_required , plan_scanned , elev_scanned , description_written , qms_written )
in wallets ] ) } )
year_index_file . close ( )
for person , item_complaint_list in people . items ( ) :
person_file = open ( person + " .html " , " w " )
2020-04-26 23:47:04 +01:00
person_file . write ( html_person % { " person " : person , " year " : year , " timestamp " : timestamp ,
2020-04-26 23:34:17 +01:00
" wallets " : reduce ( operator . add , [ html_person_wallet_entry % { " walletname " : wallet ,
2020-04-26 23:47:04 +01:00
" walletindex " : urllib . quote ( wallet ) + " /walletindex.html " ,
2020-04-26 23:34:17 +01:00
" complaints " : reduce ( operator . add ,
[ html_items % complaint
for complaint
in complaints ] ,
" " ) }
for wallet , complaints
in item_complaint_list ] , " " )
} )
person_file . close ( )