# pichtml.py Create html file from txt input file (e.g. 0102.txt)
#
import shutil
import sys
import os
from stat import *
import string
import time

colsize = 3

#hdg1 = '<!doctype html public "-//w3c//dtd html 4.0 transitional//en">\n'
hdg2 = '<html>\n'
hdg3 = '<head>\n'
hdg4 = '<meta name="Author" content="Peter Hollenbeck">\n'
hdg6 = '</head>\n'
hdg7 = '<body>\n'
hdg8 = ''                                 # Title - see below
trlr1 = '</body>\n'
trlr2 = '</html>\n'
nbsp  = '&nbsp;'
nbsp2 = nbsp+nbsp
title = ''

# inputfilename: e.g., 0101.txt
if len(sys.argv) < 2:
    t = time.time()
    localt = time.localtime(t)                # yyyy, mm, dd, hh, mm, ss, ...
    yyyy = localt[0]
    yy = str(yyyy)[-2:]
    mm = localt[1]
    mm = string.zfill(mm, 2)
    fn = yy+mm+'.txt'
else:
    fn = sys.argv[1]+'.txt'                   # Name of txt file to process

txtfile = open(fn, 'r')
txtlist = txtfile.readlines()

# Open locations table
locfn1 = './locations.txt'
locfn2 = '/hdb/central/weather/locations.txt'
locfn3 = '/hdc/central/weather/locations.txt'
loclist = ('', '')
try:
    locfile = open(locfn1, 'r')
    loclist = locfile.readlines()
    print 'locfile opened'
except:
    try:
        locfile = open(locfn2, 'r')
        loclist = locfile.readlines()
        print 'locfile opened'
    except:
        try:
            locfile = open(locfn3, 'r')
            loclist = locfile.readlines()
        except:
            print 'locfile open failed'

htmlfile = ''

currdir = os.getcwd()                     # Get name of current directory
temp = string.split(currdir, '/')
currdir = temp[len(temp) - 1]

for txt in txtlist:
    print txt
    txtline = string.split(txt,None,3)    # Get 4 fields
    print txtline
    if len(txtline) < 1:
        continue
    if txtline[0] == 'htmlstart':
        print 'htmlstart'
        htmlfn    = txtline[1]+'.html'    # html file name
        htmldir   = txtline[2]            # directory      Not used
        htmltitle = txtline[3]            # title
        htmlfile = open(htmlfn, 'w')      # Open html file
        htmlfile.write(hdg2)              # <html>
        htmlfile.write(hdg3)              # <head>
        htmlfile.write(hdg4)              # meta name, etc.
        hdg5 = '<title>'+htmltitle+'</title>\n'
        htmlfile.write(hdg5)              # title
        htmlfile.write(hdg6)              # </head>
        htmlfile.write(hdg7)              # <body>
        htmlfile.write('<TABLE>\n')       # start TABLE
        hdg8 = '<font size=+1>'+htmltitle+'</font><br>'
        htmlfile.write(hdg8)
        col = 0
        continue
    if txtline[0] == 'htmlend':
        print 'htmlend'
        htmlfile.write('</TABLE>\n')
        htmlfile.write(trlr1)             # </body>
        htmlfile.write(trlr2)             # </html>
        htmlfile.close()
        htmlfile = ''
        continue
    if txtline[0] == 'jpg':
        jpgname =''; jpgdir=''; jpgrating=''; jpgloc=''; jpgsubj='';jpgdesc=''
        txtline = string.split(txt,None,6)        # Get 7 fields
        jpgname    = txtline[1]           # e.g. 00121801
        jpgdir     = txtline[2]           # directory for jpg file
        if jpgdir == currdir:
            jpgdir = ''
        else:
            jpgdir = jpgdir+'/'
        jpgrating  = txtline[3]           # 0 = skip
        if jpgrating == '0':
            continue
        jpgloc     = txtline[4]           # e.g. BC
        for l in loclist:
            line = string.split(l,None,1) # Get loc code and loc desc
            if len(line) < 1:
                continue
            if jpgloc == line[0]:
                jpgloc = line[1]
        jpgloc = string.strip(jpgloc)
        if len(txtline) > 5:
            jpgsubj    = txtline[5]           # e.g. IP (Inside Passage)
            location = ''
            for l in loclist:
                line = string.split(l,None,1) # Get loc code and loc desc
                if len(line) < 1:
                    continue
                if jpgsubj == line[0]:
                    jpgsubj = line[1]
        if len(txtline) > 6:
            jpgdesc    = txtline[6]       # e.g. Desolation Sound
            jpgdesc = string.strip(jpgdesc)
        jpgfn      = '"'+jpgdir+jpgname+'.jpg"'        # jpg file name
        tnfn       = '"'+jpgdir+jpgname+'tn.jpg"'      # thumbnail file name

        if col == 0:
            htmlfile.write('<TR>\n')
        col = col + 1
        htmldata = '<TD><br><a href='+jpgfn+'>'+'<img SRC='+tnfn+'>'+  \
                    '<br>'+jpgloc+', '+jpgsubj+'<br>'+jpgdesc+'</a>\n'
        htmlfile.write(htmldata)
        print htmldata

        if col == colsize:
            col = 0
        continue
    #   oops - not a known type
    print 'Not a known type'

if htmlfile != '':
    htmlfile.write(trlr1)             # </body>
    htmlfile.write(trlr2)             # </html>
    htmlfile.close()
    print htmlfile


