Implemented type enforcement and recognition of legal boolean
assignment strings.
1 parent 1a2bdf3 commit 86726c7781cba1ecfa037026a0382e093234d3a3
@tundra tundra authored on 24 Mar 2004
Showing 1 changed file
View
38
tconfpy.py
 
# Program Information
 
PROGNAME = "tconfpy"
RCSID = "$Id: tconfpy.py,v 1.139 2004/03/24 23:34:59 tundra Exp $"
RCSID = "$Id: tconfpy.py,v 1.140 2004/03/25 00:18:44 tundra Exp $"
VERSION = RCSID.split()[2]
 
# Copyright Information
 
###########
 
# String Representations Of Booleans
 
Booleans = (("1", True), ("0", False),
("True", True), ("False", False),
("Yes", True), ("No", False),
("On", True), ("Off", False))
Booleans = {"1" : True, "0" : False,
"True" : True, "False" : False,
"Yes" : True, "No" : False,
"On" : True, "Off" : False
}
 
sFALSE = "False"
sTRUE = "True"
 
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["eTYPEBAD"] = FILENUM + "Type Mismatch. '%s' Must Be Assigned Values Of Type %s Only"
Messages["eVARUNDEF"] = FILENUM + "Attempt To Reference Undefined Variable '%s'"
 
 
###########
SymTable[l] = d
 
# Otherwise, update an existing entry
else:
 
# 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.
 
try:
typ = SymTable[l].Type
 
# Booleans are a special case - we accept only
# a limited number of strings on the RHS
 
if typ == TYPE_BOOL:
r = Booleans[r.capitalize()]
 
# For everything else, try an explicit coercion
else:
r = typ(r)
except:
ErrorMsg("eTYPEBAD", (cfgfile, linenum, l, str(typ).split()[1][:-1]))
 
# Finally, we can save the new variable
SymTable[l].Value = r
 
#####
# Line Format Is Not In One Of The Recognized Forms - Syntax Error
#####