tmplt2html.py

'''Template to HTML.

(See lib/template for details.)

Developer@Sonnack.com
August 2017
'''
####################################################################################################
from __future__ import print_function
from sys import argvexc_info
from traceback import extract_tb
from os import path
from datetime import datetime
from logger import loggerinfodebugtrace
from template import HtmlTemplatetemplate_to_html
####################################################################################################
Log = logger('Template')
Today = datetime.now()

BasePath = r'C:\CJS\prj\Python\app'
HtmlPath = r'C:\cjs\www\root\tmp'

TemplateFilename = path.join(HtmlPath'template-in.html')
OutputFilename   = path.join(HtmlPath'template-out.html')
DataBagFilename  = path.join(BasePath'template-data.xml')

StartMarker = '<!--@'
EndMarker = '-->'


##------------------------------------------------------------------------------------------------##
SampleData = {
    'last-modified':Today.strftime('%a %b %d %H:%M:%S %Y'),
    'DCR':Today.strftime('%m-%d-%Y %H:%M'),
    'level':0,
    'team':'Twins',
    'year':'2014',
    'css-path':'/pub/bb/twins_stats.css',
    'table-class':'Stats',
    'total-cols':'15',
    # Season isn't a list, so can't be iterated, only accessed
    'season':'caption':'Season',
               'w-l':'70-92''wpct':'.432',
               'avg':'.254''obp':'.324''slg':'.389''ops':'.712''iso':'.136',
               'r/g':'4.41''h/g':'8.72',
               'rdiff':'<span style="color:red;">-62</span>',
               'hdiff':'<span style="color:red;">-176</span>'
    },
    # Month has a key member, so can be accessed by it or index; iteration value is the key
    'months':[
        {'key':'apr''caption':'April''w-l':'12-11''wpct':'<span style="color:#007f00;">.522</span>''avg':'.255''ops':'.752'},
        {'key':'may''caption':'May'  , 'w-l':'13-16''wpct':'<span style="color:#ff0000;">.448</span>''avg':'.230''ops':'.639'},
        {'key':'jun''caption':'June' , 'w-l':'12-16''wpct':'<span style="color:#ff0000;">.429</span>''avg':'.246''ops':'.688'},
        {'key':'jul''caption':'July' , 'w-l':'11-15''wpct':'<span style="color:#ff0000;">.423</span>''avg':'.247''ops':'.688'},
        {'key':'aug''caption':'Aug'  , 'w-l':'11-18''wpct':'<span style="color:#ff0000;">.379</span>''avg':'.277''ops':'.781'},
        {'key':'sep''caption':'Sept' , 'w-l':'11-15''wpct':'<span style="color:#ff0000;">.423</span>''avg':'.267''ops':'.734'},
    ],
    # Halfs and Thirds have no key member, so are only accessed by index; iteration value is the index
    'halfs':[
        {   'caption':'1<sup>st</sup> half',
            'w-l':'41-40''wpct':'<span style="color:#009f00;">.506</span>',
            'r/g':'4.53''h/g':'8.60',
            'rdiff':'<span style="color:#ff0000;">-59</span>',
            'avg':'.251''obp':'.326''slg':'.410''ops':'.736''iso':'.160',
        },
        {   'caption':'2<sup>nd</sup> half',
            'w-l':'18-17''wpct':'<span style="color:#009f00;">.514</span>',
            'r/g':'5.03''h/g':'8.86',
            'rdiff':'<span style="color:#009f00;">+9</span>',
            'avg':'.262''obp':'.339''slg':'.414''ops':'.753''iso':'.152',
        },
    ],
    'thirds':[
        {   'caption':'1<sup>st</sup> third',
            'w-l':'29-25''wpct':'<span style="color:#009f00;">.537</span>',
            'r/g':'4.61''h/g':'8.70',
            'rdiff':'<span style="color:#ff0000;">-28</span>',
            'avg':'.253''obp':'.334''slg':'.418''ops':'.751''iso':'.165',
        },
        {   'caption':'2<sup>nd</sup> third',
            'w-l':'23-31''wpct':'<span style="color:#ff0000;">.426</span>',
            'r/g':'4.35''h/g':'8.30',
            'rdiff':'<span style="color:#ff0000;">-46</span>',
            'avg':'.245''obp':'.316''slg':'.385''ops':'.702''iso':'.141',
        },
        {   'caption':'3<sup>rd</sup> third',
            'w-l':'7-1',
            'wpct':'<span style="color:#009f00;">.875</span>',
            'r/g':'7.38''h/g':'11.12',
            'rdiff':'<span style="color:#009f00;">+24</span>',
            'avg':'.318''obp':'.391''slg':'.536''ops':'.926''iso':'.218',
        }
    ],
    # Needed to iterate the rows!
    'stats':[
        {'key':'w-l'    , 'caption':'W-L' },
        {'key':'wpct'   , 'caption':'Win%'},
        {'key':'rdiff'  , 'caption':'R&Delta;' },
        {'key':'r/g'    , 'caption':'R/G' },
        {'key':'h/g'    , 'caption':'H/G' },
        {'key':'avg'    , 'caption':'AVG' },
        {'key':'obp'    , 'caption':'OBP' },
        {'key':'slg'    , 'caption':'SLG' },
        {'key':'ops'    , 'caption':'OPS' },
        {'key':'iso'    , 'caption':'ISO' },
    ]
}


##================================================================================================##
def do_tokenize (*args):
    Log.info('test/parameters: %s' % str(args))
    iname = args[0if 0 < len(argselse TemplateFilename
    tmplt = HtmlTemplate(inamelog=Log)
    for tok in tmplt.tokens:
        Log.info('tok: %s' % str(tok))
    return 'Done!'
##================================================================================================##
def do_demo (*args):
    Log.info('demo/parameters: %s' % str(args))
    template_to_html(TemplateFilenameNoneOutputFilenamedbag=SampleDatalog=Log)
    return 'Done!'
##================================================================================================##
def do_test (*args):
    Log.info('test/parameters: %s' % str(args))
    iname = args[0if 0 < len(argselse TemplateFilename
    dname = args[1if 1 < len(argselse DataBagFilename
    oname = args[2if 2 < len(argselse OutputFilename
    #
    template_to_html(inamednameonamelog=Log)
    return 'Done!'
##================================================================================================##
def do_main (*args):
    Log.info('main/parameters: %s' % str(args))
    if len(args) < 3: return ('Not enough arguments! Three required; got %d' % len(args))
    iname = args[0]
    dname = args[1]
    oname = args[2]
    #
    template_to_html(inamednameonamelog=Log)
    return 'Done!'
####################################################################################################
def dispatch (cmd, *args):
    Log.info('command: %s' % cmd)
    Log.info('arguments: %d' % len(args))
    if cmd == 'main': return do_main(*args)
    if cmd == 'test': return do_test(*args)
    if cmd == 'demo': return do_demo(*args)
    if cmd == 'toks': return do_tokenize(*args)
    return 'Nothing to do!'
####################################################################################################
if __name__ == '__main__':
    print('autorun: %s' % argv[0])
    Log.start(path.join(BasePath,'tmplt2html.log'))
    Log.level(info())
    cmd = argv[1if 1 < len(argvelse ''
    etc = argv[2:if 2 < len(argvelse []
    try:
        obj = dispatch(cmd, *etc)
        print(obj)
        Log.info(obj)
    except:
        etypeevaluetb = exc_info()
        ts = extract_tb(tb)
        Log.error('%s: %s' % (etype.__name__,str(evalue)))
        for t in ts[-3:]:
            Log.error('[%d] %s  (%s)' % (t[1], t[2], t[0]))
            Log.error('    %s' % t[3])
        raise
    finally:
        Log.end()
####################################################################################################
'''eof'''