webcal.py
'''\
Web Calendar.
Exercise the html_cal library class by creating a list of calendars
for the www.sonnack.com website.
commands:
main - Main function (normal use)
test - Test function (development)
usage:
module.dispatch({command}, args={arguments}) -- calling module from code
$PYTHON module_name {command} {arguments} -- executing module externally
Developer@Sonnack.com
April 2016
'''
from __future__ import print_function
from sys import argv, exc_info
from traceback import extract_tb
from os import path
from logger import logger, info, debug, trace
from html_cal import make_web_calendar, DefaultProperties as props
Log = logger('WebCal')
BasePath = r'C:\CJS\prj\Python\app'
WebFileName = r'C:\CJS\www\root\pub\cal\%04d.html'
date_html = '<span title="%s" style="color:%s;">%d</span>'
day_colors = ['#cc0000', '#000000', '#0000cc', '#000000', '#006600', '#999999', '#cc9999']
day_function = lambda x: date_html % (x.dt1.strftime('%B %d, %Y'), day_colors[x.dt1.weekday()], x.dt1.day)
month_function = lambda x: x.dt1.strftime('%B')
prev_html = '<span style="float:left;"><a href="%04d.html" style="color:#7f7f7f;font-size:xx-small;">« %04d</a></span>'
next_html = '<span style="float:right;"><a href="%04d.html" style="color:#7f7f7f;font-size:xx-small;">%04d »</a></span>'
prev_function = lambda y: prev_html % (y,y)
next_function = lambda y: next_html % (y,y)
def do_test (*args):
Log.trace('test/parameters: %s' % str(args))
cyear = args[0] if 0 < len(args) else '1955'
fname = args[1] if 1 < len(args) else WebFileName
props['year'] = int(cyear)
props['filename'] = fname
props['cb:day'] = day_function
props['cb:month'] = month_function
props['cb:prev'] = prev_function
props['cb:next'] = next_function
Log.debug('year=%s' % props['year'])
Log.debug('filename=%s' % props['filename'])
g = make_web_calendar(props)
Log.debug('html-cal: %s' % g[0])
Log.debug('html-filename: %s' % g[1])
return 'Done!'
def do_main (*args):
Log.trace('main/parameters: %s' % str(args))
year0 = int(args[0]) if 0 < len(args) else 2000
year1 = int(args[1]) if 1 < len(args) else 2017
fname = str(args[2]) if 2 < len(args) else WebFileName
props['filename'] = fname
props['cb:day'] = day_function
props['cb:month'] = month_function
props['cb:prev'] = prev_function
props['cb:next'] = next_function
a = []
for yr in range(year0, year1+1):
props['year'] = yr
g = make_web_calendar(props)
a.append(g[1])
Log.debug('html-cal: %s' % g[0])
Log.debug('html-filename: %s' % g[1])
Log.debug('calendars created: %d' % len(a))
return 'Done!'
def dispatch (cmd, *args):
Log.trace('command: %s' % cmd)
Log.trace('arguments: %d' % len(args))
if cmd == 'test': return do_test(*args)
if cmd == 'main': return do_main(*args)
return 'Nothing to do!'
if __name__ == '__main__':
print('autorun: %s' % argv[0])
Log.start(path.join(BasePath,'webcal.log'))
Log.level(info())
cmd = argv[1] if 1 < len(argv) else ''
etc = argv[2:] if 2 < len(argv) else []
try:
obj = dispatch(cmd, *etc)
print(obj)
Log.info(obj)
except:
etype, evalue, tb = 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'''