Further cleanup of API validation logic.
1 parent ecbf1a0 commit f24fb4cd33b377886ff41a19c78a299701cce9b0
@tundra tundra authored on 24 Mar 2004
Showing 1 changed file
View
99
tconfpy.py
 
# Program Information
 
PROGNAME = "tconfpy"
RCSID = "$Id: tconfpy.py,v 1.143 2004/03/25 04:42:50 tundra Exp $"
RCSID = "$Id: tconfpy.py,v 1.144 2004/03/25 05:34:58 tundra Exp $"
VERSION = RCSID.split()[2]
 
# Copyright Information
 
 
eIFBAD = "'%s' Or '%s' Missing" % (EQUIV, NOTEQUIV)
eBADDEFAULT = "Type Of Default Value Does Not Agree With Type Declared"
eBADLEGALVAL = "Type Of One Or More LegalVals Does Not Agree With Type Declared"
eBADMINMAX = "Type Of Min Or Max Value Does Not Agree With Type Declared"
eBADMINMAX = "Type Of Min Or Max Value Not Appropriate For"
eNOTDESCRIPT = "Invalid Descriptor Type"
eNOVARREF = "Must Have At Least One Variable Reference"
eSTARTUP = "<Program Starting>"
 
Messages["eEQUIVEXTRA"] = FILENUM + "Only a single '%s' Or '%s' Operator Permitted" % (EQUIV, NOTEQUIV)
Messages["eIFEXTRATXT"] = FILENUM + "Extra Text On Line. '%s' Only Accepts Variable References As Arguments"
Messages["eVARREADONLY"] = FILENUM + "Variable '%s' Is Read-Only. Cannot Change Its Value"
Messages["eRESERVED"] = FILENUM + "Cannot Modify Value Of Reserved Symbol '%s'"
Messages["eSTRINGLONG"] = FILENUM + "String '%s' Longer Than Max Allowed Length, %d"
Messages["eSTRINGSHORT"] = FILENUM + "String '%s' Shorter Than Min Allowed Length, %d"
Messages["eSTRINGLONG"] = FILENUM + "Right-Hand-Side, '%s' Longer Than Max Allowed Length, %d"
Messages["eSTRINGSHORT"] = FILENUM + "Right-Hand-Side, '%s' Shorter Than Min Allowed Length, %d"
Messages["eTYPEBAD"] = FILENUM + "Type Mismatch. '%s' Must Be Assigned Values Of Type %s Only"
Messages["eVALSMALL"] = FILENUM + "%d Is Smaller Than The Minimum Allowed, %d"
Messages["eVALLARGE"] = FILENUM + "%d Is Larger Than The Maximum Allowed, %d"
Messages["eVALSMALL"] = FILENUM + "%d Is Smaller Than The Minimum Allowed, %f"
Messages["eVALLARGE"] = FILENUM + "%d Is Larger Than The Maximum Allowed, %f"
Messages["eVARUNDEF"] = FILENUM + "Attempt To Reference Undefined Variable '%s'"
 
 
###########
TotalLines = 0
 
# Add any passed symbols to the SymbolTable
 
deserror = False
for sym in InitialSymTbl:
 
des = InitialSymTbl[sym]
 
# Make sure a valid descriptor was passed for each variable
 
desok = True
desok = True
 
# Make sure we got a Var Descriptor Object
 
if not isinstance(des, VarDescriptor):
desok = False
detail = eNOTDESCRIPT
ErrorMsg("eDESCRIPTBAD", (eNOTDESCRIPT, sym))
des = VarDescriptor() # Make a fake one so the following tests don't blow up
 
# Check various entries for type agreement
 
dt = des.Type
 
if des.Default and type(des.Default) != dt:
desok = False
detail = eBADDEFAULT
ErrorMsg("eDESCRIPTBAD", (eBADDEFAULT, sym))
 
for lv in des.LegalVals:
if type(lv) != dt:
desok = False
detail = eBADLEGALVAL
ErrorMsg("eDESCRIPTBAD", (eBADLEGALVAL, sym))
 
for mm in (des.Min, des.Max):
if mm and type(mm) != TYPE_INT:
 
# Floats can accept Float or Int boundaries
# Ints, & Strings can accept Int boundaries
# Boundaries not relevant for Bool and Complex
# Anything else is an error
 
if mm and dt == TYPE_FLOAT and type(mm) != TYPE_FLOAT and type(mm) != TYPE_INT:
desok = False
detail = eBADMINMAX
 
# Update or error based on validity of descriptor
ErrorMsg("eDESCRIPTBAD", (eBADMINMAX, sym))
 
if mm and dt in (TYPE_INT, TYPE_STRING) and type(mm) != TYPE_INT:
desok = False
ErrorMsg("eDESCRIPTBAD", (eBADMINMAX, sym))
 
# Only load the symbol table with valid entries
if desok:
SymTable[sym] = des
 
# Invalid descriptor passed
# Indicate that a problem was encountered
else:
ErrorMsg("eDESCRIPTBAD", (detail, sym))
return (SymTable, ErrMsgs, WarnMsgs, DebugMsgs, LiteralLines)
deserror = True
 
# If any of the passed symbols had bogus descriptor contents, we're done
if deserror:
return (SymTable, ErrMsgs, WarnMsgs, DebugMsgs, LiteralLines)
 
 
 
# Symbol Table passed to API was OK, sokeep going
 
# Make sure the symbol table has a valid namespace
 
if NAMESPACE not in SymTable:
d.Default = r
d.Value = r
SymTable[l] = d
 
# Otherwise, update an existing entry
# Otherwise, update an existing entry.
# For existing entries we have to first
# do the various validation checks specified
# in that variable's descriptor
else:
update = True
des = SymTable[l]
typ = des.Type
lv = des.LegalVals
low = des.Min
up = des.Max
 
#####
# Type Enforcement
#####
 
# We try to coerce the new value into
# the specified type. If it works, we
# update, otherwise we throw an error
# and do nothing.
# the specified type. If this works, we
# go on to the rest of the validation tests,
# otherwise mark the attempt as invalid.
 
try:
# Booleans are a special case - we accept only
# a limited number of strings on the RHS
except:
update = False
ErrorMsg("eTYPEBAD", (cfgfile, linenum, l, str(typ).split()[1][:-1]))
 
 
#####
# Bounds Checks
#####
# Check bounds for interger and floats
 
if typ in (TYPE_FLOAT, TYPE_INT):