diff --git a/tconfpy.py b/tconfpy.py index d35ca9a..e90aa33 100755 --- a/tconfpy.py +++ b/tconfpy.py @@ -6,7 +6,7 @@ # 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 @@ -159,8 +159,10 @@ ALLOWNEWVAR = True # Allow new variable creation in cfg file -DEBUG = False # Control Debug output +TEMPLATES = {} # Place to hold variable templates +TEMPONLY = False # Allow only template variable creation LITERALVARS = False +DEBUG = False # Control Debug output INLITERAL = False # Indicates we are currently in a literal block DebugMsg = [] # Place to store and return debug info @@ -318,6 +320,7 @@ 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" @@ -416,27 +419,40 @@ # 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 @@ -1128,15 +1144,16 @@ 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 @@ -1154,12 +1171,49 @@ # Only do this if new variable creation allowed if ALLOWNEWVAR: - # Build a new variable descriptor + # 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 - d = VarDescriptor() - d.Default = r - d.Value = r - SymTable[l] = d + # 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: