ConfigParser, no attribute

faulkner faulkner612 at comcast.net
Mon Jun 5 17:07:41 EDT 2006


Settings.__init__ needs to call ConfigParser.SafeConfigParser.__init__
before it calls self.readfp.
Nexu wrote:
> 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'




More information about the Python-list mailing list