# ---------------------------------------------------------------------------------------------------
# MMYW.PY - Make MY Web - yet another Python HTML preprocessor
# version 1.0 (c) August 2000 by F. Pacquier (fredp@mygale.org)
# ---------------------------------------------------------------------------------------------------
# ---------------------------------------------------------------------------------------------------
# ** CUSTOMIZATION **
#
# Change the defaults here if they don't suit you...
sourcedir = 'src' # root of source tree (.inc .src etc.)
htmldir = 'html' # root of destination for generated pages (.html)
# ---------------------------------------------------------------------------------------------------
# imported modules
#
import sys, string, os, os.path
# ---------------------------------------------------------------------------------------------------
# globals
#
include_files = {}
page_vars = {}
global_vars = {}
force = 0
debug = 0
# ---------------------------------------------------------------------------------------------------
# C 'struct' lookalike for include_files dictionary (see function scan_includes below)
#
class storage :
pass
# ---------------------------------------------------------------------------------------------------
def scan_includes(arg, dirname, names) :
destdir = os.path.normpath(os.path.join(htmldir, dirname[len(sourcedir)+1:] ))
if not os.path.exists(destdir) : # check that same directory exists under htmldir, else create it
os.mkdir(destdir)
for fn in names : # look for include files here and add them to dictionary
fn = string.lower(os.path.join(dirname,fn))
if os.path.isdir(fn) : continue
(root, ext) = os.path.splitext(fn)
if not ext == '.inc' : continue
include_files[root] = storage()
include_files[root].inc = ''
include_files[root].mtime = os.path.getmtime(fn)
# ---------------------------------------------------------------------------------------------------
def scan_sources(arg, dirname, names) :
print '----------\nDirectory :', dirname
if os.path.exists(os.path.join(dirname, 'global.vars')) :
mmyw_setglobals(dirname) # set global variables for this directory downwards
print
for fn in names :
fn = string.lower(fn)
if os.path.isdir(os.path.join(dirname, fn)) : continue
(basename, extension) = os.path.splitext(fn)
if not extension == '.src' : continue
print 'Source file :', fn
# process all include directives in page
(target, html) = mmyw_include(dirname, basename, extension)
if target == None : # if no dependencies do not check other directives
print 'No depencies.'
continue
html = mmyw_set(html) # collect local variables defined in page
html = mmyw_var(html, dirname) # substitute local/global variables used in page
print 'Writing :', target
out = open(target , 'w')
out.write(html)
out.close()
if debug : raw_input('Type Enter to continue...') # pause after each written file
# ---------------------------------------------------------------------------------------------------
def mmyw_include(dirname, basename, extension):
print 'Includes :',
rewrite = force
target = os.path.join(htmldir+dirname[3:], basename +'.html')
if os.path.exists(target) :
target_mtime = os.path.getmtime(target)
else :
target_mtime = 0
source = os.path.join(dirname, basename+extension)
if os.path.getmtime(source) > target_mtime : # is source later than target ?
rewrite = 1
buf = open(source).read()
while 1 : # find all include directives
(start, stop, parm) = get_mmyw_token(buf, 'include')
if start == -1 : break # no more includes
found = None
searchpath = dirname
while 1 : # look for nearest include file, from here upwards
key = os.path.join(searchpath, parm)
if include_files.has_key(key) : # found one
found = 1
break # stop here
if searchpath == sourcedir : break # give up at top level
(searchpath, spam) = os.path.split(searchpath) # else try parent dir
if found : # do the actual substitution
print key,
if include_files[key].inc == '' : # read it only first time and store it
include_files[key].inc = open(key + '.inc').read()
value = include_files[key].inc
if os.path.getmtime(key + '.inc') > target_mtime :
rewrite = 1 # is include later than target ?
else : # ignore directive (delete)
print parm,'(not found)',
value = ''
buf = string.replace(buf, buf[start:stop+1], value) # replace all instances of include
print
if rewrite :
return (target, buf)
else :
return (None, None)
# ---------------------------------------------------------------------------------------------------
def mmyw_setglobals(dirname) :
print 'Globals :',
buf = open(os.path.join(dirname, 'global.vars')).read()
while 1 : # find all set directives
(start, stop, parm) = get_mmyw_token(buf, 'set')
if start == -1 : break # no more sets
words = string.split(parm)
var = words[0]
value = string.strip(parm[len(var):])
global_vars[os.path.join(dirname, var)] = value # store global value
buf = buf[:start] + buf[stop+1:] # remove directive
print var,
print
return buf
# ---------------------------------------------------------------------------------------------------
def mmyw_set(buf) :
print 'Sets :',
page_vars.clear()
while 1 : # find all set directives
(start, stop, parm) = get_mmyw_token(buf, 'set')
if start == -1 : break # no more sets
words = string.split(parm)
var = words[0]
page_vars[var] = string.strip(parm[len(var):]) # store local value
buf = buf[:start] + buf[stop+1:] # remove directive
print var,
print
return buf
# ---------------------------------------------------------------------------------------------------
def mmyw_var(buf, dirname) :
print 'Uses :',
while 1 : # find all var directives
(start, stop, parm) = get_mmyw_token(buf, 'var')
if start == -1 : break # no more vars
words = string.split(parm)
var = words[0]
if page_vars.has_key(var) : # substitute with local value if found
print var,
value = page_vars[var]
else : # look for nearest global value, from here upwards
found = None
searchpath = dirname
while 1 :
key = os.path.join(searchpath, parm)
if global_vars.has_key(key) : # found one
found = 1
break # stop here
if searchpath == sourcedir : break # give up at top level
(searchpath, spam) = os.path.split(searchpath) # else try parent dir
if found : # substitute with global value if found
print key,
value = global_vars[key]
else : # ignore directive
print var,'(not found)',
value = ''
buf = string.replace(buf, buf[start:stop+1], value) # replace all instances of var
print
return buf
# ---------------------------------------------------------------------------------------------------
def get_mmyw_token(buf, token) :
'''
Find the first occurrence in buf of a pair of tags matching :
...
Return start and end positions in buf, and whatever string was in between
NO NESTING !
'''
offset = 0
opentag = ''
closetag = ''
taglen = len(opentag)
start = string.find(buf, opentag, offset)
if start == -1 : return (-1, -1, None)
stop = string.find(buf, closetag, start + taglen)
if stop == -1 : return (-1, -1, None)
return (start, stop+taglen, string.strip(buf[start+taglen:stop]))
return (-1, -1, None)
# ---------------------------------------------------------------------------------------------------
# Make My Web...
if not os.path.exists(sourcedir) :
print 'No source directory :', sourcedir
sys.exit()
if '--force' in sys.argv : force = 1
if '--debug' in sys.argv : debug = 1
os.path.walk(sourcedir, scan_includes, '')
os.path.walk(sourcedir, scan_sources, '')