ConfigParser, no attribute

N

Nexu

Hello,

I'm not sure exactly what i'm doing wrong here. I asked around on IRC
and i was told the code is correct.
The purpose of Settings() is that whenever Settings() or any of its
methods are called. It should pick up the latest settings from file
instead of returning what was in the buffer. This allow other scripts to
change the value and my program and pick these changes up.
Everything else should work exact same as ConfigParser().
-----------------------------------------------------------------------------
class Settings(ConfigParser.SafeConfigParser):
def __init__(self):
self.filename = os.path.join(xchat.get_info('xchatdir'), 'nxscript',
'nxscript.conf')
try:
config_file = file(self.filename, 'r')
self.readfp(config_file, self.filename)
if self.sections() == []:
self.add_section('Global')
if self.has_section('Global'):
self.set('Global', 'ProtectME', 'false')
config_file.close()
except IOError:
nx.sysmsg('Configuration file not found')

def update_file(self):
try:
config_file = file(self.filename, 'w')
self.write(config_file)
except IOError:
nx.sysmsg('Could not write to configuration file')
-----------------------------------------------------------------------------
SAMPLE CODE (what i want to able to do):
setting = Settings()
if setting.get('Global', 'ProtectME'):
print 'protection enabled'
-----------------------------------------------------------------------------
ERRORS:
File "/home/nexu/.xchat2/nxscript/nx.py", line 43, in ?
setting = Settings()
File "/home/nexu/.xchat2/nxscript/nx.py", line 24, in __init__
self.readfp(config_file, self.filename)
File "/usr/lib/python2.4/ConfigParser.py", line 286, in readfp
self._read(fp, filename)
File "/usr/lib/python2.4/ConfigParser.py", line 451, in _read
if sectname in self._sections:
AttributeError: Settings instance has no attribute '_sections'
 
F

faulkner

Settings.__init__ needs to call ConfigParser.SafeConfigParser.__init__
before it calls self.readfp.
 

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

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,072
Latest member
trafficcone

Latest Threads

Top