ConfigParser, no attribute

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Nexu

    #1

    ConfigParser, no attribute

    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(Config Parser.SafeConf igParser):
    def __init__(self):
    self.filename = os.path.join(xc hat.get_info('x chatdir'), 'nxscript',
    'nxscript.conf' )
    try:
    config_file = file(self.filen ame, 'r')
    self.readfp(con fig_file, self.filename)
    if self.sections() == []:
    self.add_sectio n('Global')
    if self.has_sectio n('Global'):
    self.set('Globa l', 'ProtectME', 'false')
    config_file.clo se()
    except IOError:
    nx.sysmsg('Conf iguration file not found')

    def update_file(sel f):
    try:
    config_file = file(self.filen ame, 'w')
    self.write(conf ig_file)
    except IOError:
    nx.sysmsg('Coul d not write to configuration file')
    -----------------------------------------------------------------------------
    SAMPLE CODE (what i want to able to do):
    setting = Settings()
    if setting.get('Gl obal', '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(con fig_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'

  • faulkner

    #2
    Re: ConfigParser, no attribute

    Settings.__init __ needs to call ConfigParser.Sa feConfigParser. __init__
    before it calls self.readfp.
    Nexu wrote:[color=blue]
    > 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(Config Parser.SafeConf igParser):
    > def __init__(self):
    > self.filename = os.path.join(xc hat.get_info('x chatdir'), 'nxscript',
    > 'nxscript.conf' )
    > try:
    > config_file = file(self.filen ame, 'r')
    > self.readfp(con fig_file, self.filename)
    > if self.sections() == []:
    > self.add_sectio n('Global')
    > if self.has_sectio n('Global'):
    > self.set('Globa l', 'ProtectME', 'false')
    > config_file.clo se()
    > except IOError:
    > nx.sysmsg('Conf iguration file not found')
    >
    > def update_file(sel f):
    > try:
    > config_file = file(self.filen ame, 'w')
    > self.write(conf ig_file)
    > except IOError:
    > nx.sysmsg('Coul d not write to configuration file')
    > -----------------------------------------------------------------------------
    > SAMPLE CODE (what i want to able to do):
    > setting = Settings()
    > if setting.get('Gl obal', '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(con fig_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'[/color]

    Comment

    Working...