does this exception mean something else?

A

Alex Hall

Hi all,
I have a file, pasted below for what good it will do, which makes a
couple conditional calls to a function called writeDefaults. However,
when I manually trigger a condition that causes the function to be
called, Python gives me a name error and says that my writeDefaults
does not exist. I am sure that it does, as I copied the text from the
call and pasted it into the text search of my editor, and it found the
line that defines the function, so the text in the call is obviously
correct. Sometimes, especially in a language like Javascript, one
error can mean something else or point to a problem other than the one
it claims to be about, so I wondered if the same thing could be
happening here? The error is on line 55, just after the if statement
that uses os.path.exists to ensure my ini file is really there. TIA!
BTW, speech is just a file that will output a given message through an
active screen reader; changing all calls to print will work just as
well.

import os, sys
import helpers
from configobj import ConfigObj #for reading ini files
from string import Template #for interpreting the template strings in
the ini file
from speech import speak

#set up a default dict of options to be used
general={
"useSpeech":"True",
"printMessages":"False",
"roundTo":1,
"defaultStartMode":1
}

enableModes={
"resourceMonitor":"True",
"weather":"True",
"network":"True"
}

weatherOptions={
"location":"04938"
}

armOptions={
"primaryDrive":"c:",
"secondaryDrive":"j:",
"currentIsPrimary":"True",
"CIsSecondary":"True",
"ramInfo":"used",
"driveInfo":"Used"
}

templates={
"sayCorePercentage":"$percent% for core $core",
"sayProcessorAverage":"$percent% average load",
"sayRamFree":"$percent% ram free ($free of $total remaining)",
"sayRamUsed":"$percent% ram used ($used of $total used)",
"sayDriveFree":"$percent% free on $drive ($free of $total remaining)",
"sayDriveUsed":"$percent% used on $drive ($used of $total used)",
"currentWeather":"$condition, $temp degrees. $wind. $humidity.",
"forecast":"$day: $condition, $low to $high."
}


#first, load the ini file setting
#so where is the ini? script_path/config.ini, of course!
iniLoc=helpers.progdir+'\\config.ini'
iniLoc=r'c:\arm\config.ni'
#set up the ini object to read and write settings
if(os.path.exists(iniLoc)):
ini=ConfigObj(iniLoc)
else:
speak("The i n i file was not found. Now generating a default file.")
writeDefaults()
#end except

#now get the settings
general=ini["general"]
printMessages=general["printMessages"]
speech=general["useSpeech"]
rnd=int(general["roundTo"])
mode=int(general["defaultStartMode"])

enable=ini['enableModes']
rmOn=enable["resourceMonitor"]
weatherOn=enable["weather"]
networkOn=enable["network"]

aop=ini["armOptions"] #the options section
drive1=aop["primaryDrive"]
drive2=aop["secondaryDrive"]
ramInfo=aop["ramInfo"]
driveInfo=aop["driveInfo"]
useCurrentDrive=aop["currentIsPrimary"]
CIsSecondary=aop["CIsSecondary"]

wop=ini["weatherOptions"]
zip=str(wop["location"])

subs=ini["templates"] #short for substitute
corePercent=Template(subs["sayCorePercentage"])
avg=Template(subs["sayProcessorAverage"])
sayRamFree=Template(subs["sayRamFree"])
sayRamUsed=Template(subs["sayRamUsed"])
sayDriveFree=Template(subs["sayDriveFree"])
sayDriveUsed=Template(subs["sayDriveUsed"])
currentWeather=Template(subs["currentWeather"])
forecast=Template(subs["forecast"])

def setOptions():
global ini
try:
ini.reload()
except:
speak("The i n i file was not found. Now generating a default file.")
writeDefaults()
#end except
#now just a few settings that depend on the options
#if useCurrentDrive is true, get that drive and set drive1 to it
if(useCurrentDrive=="True"):
#get current drive and assign it to primary
currentDrive=os.path.splitdrive(sys.argv[0])[0]
drive1=currentDrive
#furthermore, if CIsSecondary is true, set that here,
#since useCurrentDrive must be true for this one to take effect
if(CIsSecondary=="True"):
drive2="c:"
#endif
#endif
#end def

def writeDefaults():
global ini
global iniLoc
ini.filename=iniLoc
ini["general"]=general
ini["enableModes"]=enableModes
ini["weatherOptions"]=weatherOptions
ini["armOptions"]=armOptions
ini["templates"]=templates
#create the new file
ini.write()
#end def

def speakSetOptions():
#just calls setOptions, but I use this separate def so I can speak a
"reloaded" msg
setOptions()
speak("Settings reloaded.")
#end def
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,740
Latest member
JudsonFrie

Latest Threads

Top