Added variable templating feature.
1 parent 72ec869 commit 8858d7ce5aa0997fd683dd3d0c77ee19bc65f6e9
@tundra tundra authored on 26 Apr 2004
Showing 1 changed file
View
137
tconfpy.py
 
# Program Information
 
PROGNAME = "tconfpy"
RCSID = "$Id: tconfpy.py,v 1.181 2004/04/24 19:08:28 tundra Exp $"
RCSID = "$Id: tconfpy.py,v 1.182 2004/04/27 01:54:04 tundra Exp $"
VERSION = RCSID.split()[2]
 
# Copyright Information
 
#----------------------------------------------------------#
 
 
ALLOWNEWVAR = True # Allow new variable creation in cfg file
TEMPLATES = {} # Place to hold variable templates
TEMPONLY = False # Allow only template variable creation
LITERALVARS = False
DEBUG = False # Control Debug output
LITERALVARS = False
INLITERAL = False # Indicates we are currently in a literal block
 
DebugMsg = [] # Place to store and return debug info
ErrMsgs = [] # Place to store and return errors
Messages["eSYMBADCHAR"] = FILENUM + "Symbol '%s' Contains Illegal Character '%s'"
Messages["eSYMBADSTART"] = FILENUM + "Symbol '%s' Begins With An Illegal Character"
Messages["eSYMNAMESPC"] = FILENUM + "Symbol Names May Not Contain Whitespace"
Messages["eSYMNONAME"] = FILENUM + "Symbol Name Evaluates To Null String. Not Permitted"
Messages["eTEMPONLY"] = FILENUM + "Cannot Create New Variable. No Template For '%s'"
Messages["eTYPEBAD"] = FILENUM + "Type Mismatch. '%s' Must Be Assigned Values Of Type %s Only"
Messages["eVALLARGE"] = FILENUM + "%s Too Large For Variable '%s'. Maximum Allowed is %s"
Messages["eVALSMALL"] = FILENUM + "%s Too Small For Variable '%s'. Minimum Allowed is %s"
Messages["eVARNEW"] = FILENUM + "New Variable Creation Not Permitted"
#----------------------------------------------------------#
# Public API To Module #
#----------------------------------------------------------#
 
def ParseConfig(cfgfile, InitialSymTable={}, AllowNewVars=True, LiteralVars=False, Debug=False):
def ParseConfig(cfgfile,
InitialSymTable={},
AllowNewVars=True,
Templates={},
TemplatesOnly={},
LiteralVars=False,
Debug=False):
 
global DebugMsgs, ErrMsgs, WarnMsgs, LiteralLines
global CondStack, ALLOWNEWVAR, DEBUG, SymTable, TotalLines, LITERALVARS, INLITERAL
global ALLOWNEWVAR, TEMPLATES, TEMPONLY, LITERALVARS, INLITERAL, DEBUG
global CondStack, SymTable, TotalLines
# Initialize the globals
 
ALLOWNEWVAR = AllowNewVars
DEBUG = Debug
LITERALVARS = LiteralVars
DebugMsgs = []
ErrMsgs = []
WarnMsgs = []
LiteralLines = []
 
retobj = RetObj()
ALLOWNEWVAR = AllowNewVars
TEMPLATES = Templates
TEMPONLY = TemplatesOnly
LITERALVARS = LiteralVars
INLITERAL = False
DEBUG = Debug
 
CondStack = [["", True],] # Always has one entry as a sentinel
TotalLines = 0
 
# Setup container to return parsing results
 
retobj = RetObj()
 
 
# Add any passed symbols to the SymbolTable
 
deserror = False
 
if l in (NAMESPACE, NSSEP+NAMESPACE):
 
# Make sure it is in absolute format
l = NAMESPACE
varname = l = NAMESPACE
# Handle absolute variable references
 
elif l.startswith(NSSEP):
l = l[1:]
varname = l = l[1:]
 
# In all other cases prepend current namespace
else:
varname = l
ns = SymTable[NAMESPACE].Value
 
# Top level namespace variables don't need
# separator.
 
# Only do this if new variable creation allowed
if ALLOWNEWVAR:
 
# Build a new variable descriptor
 
d = VarDescriptor()
d.Default = r
d.Value = r
SymTable[l] = d
# Rules for new variable creation:
#
# 1) If var has a template, use it
# 2) If var has no template, but TemplatesOnly=True -> Error
# 3) If var has no template, and TemplatesOnly=False -> Create new var
 
# Rule 1
if varname in TEMPLATES:
 
# Create the new variable
SymTable[l] = TEMPLATES[varname]
 
# Load the proposed value only if valid
 
if ValidateValue(l, r, cfgfile, linenum):
SymTable[l].Value = r
# If value is invalid, remove this
# variable. We cannot create a new
# variable on an error. Errors are
# actually reported by the validation
# function.
 
else:
del(SymTable[l])
 
# Rule 2
elif TEMPONLY:
ErrorMsg("eTEMPONLY", (cfgfile, linenum, varname))
 
# Rule 3
else:
# Build a new variable descriptor
 
d = VarDescriptor()
d.Default = r
d.Value = r
SymTable[l] = d
 
# New vars not allowed
else:
ErrorMsg("eVARNEW", (cfgfile, linenum))