thankyou
Donate to the B3 fund!

"even a small donation helps!"
Donate with PayPal!
Most of the plugins here are made by B3 users and the authors may not visit frequently. If you need support for plugins or if questions remain unanswered, you will have to contact the author directly. Read the full Support Disclaimer here

NOTE: Do not attach plugins to your forumtopics! Attachements are periodically removed by maintenance tasks. Upload your plugins to our Downloads section instead!

Author Topic: Preserve case when reading config  (Read 1456 times)

Offline ph03n1x

  • Sr. Member
  • ****
  • Posts: 296
    • Lovers and Haters Urban Terror
Preserve case when reading config
« on: November 20, 2015, 11:54:09 PM »
Hey all,


Was wondering how to get my config to be read as string. B3's core already loads the configs automatically, where it uses config.read before I can specify options.
I know that "config.optionxform = str" needs to be before config.read, but it is happening outside of my plugin's code. Can I do something to my plugin's code to have it load properly?

Example:
My option in myconfig.ini:
Code: [Select]
BliTz: python 5 red 0
ShaMe: boa 5 blue 0


My code:
Code: [Select]
self.botlist = {}
for (name, config) in self.config.items('bots'):
    self.botlist[name] = config



During execution the names come out as lower due to configparser behavior.  Any way I can fix that?

Thanks in advance for any help.

cu :)

Offline PtitBigorneau

  • B3 Contrib/Support
  • Sr. Member
  • *
  • Posts: 194
    • www.ptitbigorneau.fr
Re: Preserve case when reading config
« Reply #1 on: November 21, 2015, 11:43:38 AM »
hi :)

something like that ?

Code: [Select]
self.botlist = {}
for (name, config) in self.config.items('bots'):
    self.botlist.update({name:config})
or me it that i would use
Code: [Select]
self.botlist = {} 
for name in self.config.options('bots'):
    self.botlist.update({name:self.config.get('bots', name)})
« Last Edit: November 21, 2015, 12:03:20 PM by PtitBigorneau »
PtitBigorneau

Offline ph03n1x

  • Sr. Member
  • ****
  • Posts: 296
    • Lovers and Haters Urban Terror
Re: Preserve case when reading config
« Reply #2 on: November 21, 2015, 02:25:13 PM »
Nope, it is still coming out as lower case.

Sorry for bad explanation..I think it is not understood well.
In config.ini:
Code: [Select]
BliTz: python 5 red 0
ShaMe: boa 5 blue 0
[/pre]
But when it goes through my code,  it comes out as:
Code: [Select]
blitz: python 5 red 0
shame: boa 5 blue 0
[/pre]
Everything works as wanted except for that. I don't want it turned to lower case.  The same thing happens with your code as well, PtitBigorneau.
I thought there was a function built in to B3 to activate "config.optionxform = str" if needed. I'm still too noob to mess with the main B3 files to add it myself.
I guess in this case it's best to use .xml format for config?

cu :)

Offline PtitBigorneau

  • B3 Contrib/Support
  • Sr. Member
  • *
  • Posts: 194
    • www.ptitbigorneau.fr
Re: Preserve case when reading config
« Reply #3 on: November 22, 2015, 10:39:04 PM »
sorry, me with the english language :(

a solution is to not use the function b3

Code: [Select]
from ConfigParser import ConfigParser
......
    def onLoadConfig(self):

        config = ConfigParser()
        config.optionxform = str
        config.read(self.config.fileName)

        self.botlist = {}

        for (name, config) in config.items('bots'):
            self.botlist.update({name:config})
« Last Edit: November 23, 2015, 09:43:29 AM by PtitBigorneau »
PtitBigorneau

Offline ph03n1x

  • Sr. Member
  • ****
  • Posts: 296
    • Lovers and Haters Urban Terror
Re: Preserve case when reading config
« Reply #4 on: November 25, 2015, 01:52:24 PM »
Hey,
Sorry for late reply... was helping a friend with another project.

That code is raising a TypeError...I seem to be too noob to figure it out... I tried a bunch of weird stuff...one time it worked but it loaded the config twice.
Google is bringing up nothing useful for me.

Code: [Select]
151125 12:47:01 INFO     "STDOUT 'ERROR: unbound method read() must be called with ConfigParser instance as first argument (got unicode instance instead)'"
151125 12:47:01 INFO     "STDOUT '\\n'"
151125 12:47:01 ERROR    "STDERR 'Traceback (most recent call last):\\n'"
151125 12:47:01 ERROR    'STDERR \'  File "/home/UrbanTerror/b3/b3/__init__.py", line 350, in start\\n\''
151125 12:47:01 ERROR    "STDERR '    console.start()\\n'"
151125 12:47:01 ERROR    'STDERR \'  File "/home/UrbanTerror/b3/b3/parser.py", line 627, in start\\n\''
151125 12:47:01 ERROR    "STDERR '    self.call_plugins_onLoadConfig()\\n'"
151125 12:47:01 ERROR    'STDERR \'  File "/home/UrbanTerror/b3/b3/parser.py", line 998, in call_plugins_onLoadConfig\\n\''
151125 12:47:01 ERROR    "STDERR '    p.onLoadConfig()\\n'"
151125 12:47:01 ERROR    'STDERR \'  File "/home/UrbanTerror/b3/b3/extplugins/bothandler.py", line 78, in onLoadConfig\\n\''
151125 12:47:01 ERROR    "STDERR '    config.read(self.config.fileName)\\n'"
151125 12:47:01 ERROR    "STDERR 'TypeError: unbound method read() must be called with ConfigParser instance as first argument (got unicode instance instead)\\n'"
151125 12:47:01 BOT      'Shutting down...'

cu :)

Offline PtitBigorneau

  • B3 Contrib/Support
  • Sr. Member
  • *
  • Posts: 194
    • www.ptitbigorneau.fr
Re: Preserve case when reading config
« Reply #5 on: November 25, 2015, 03:04:53 PM »
my test plugin

Code: [Select]
# -*- coding: utf-8 -*-

__author__  = 'PtitBigorneau'
__version__ = '1.0'

import b3
import b3.plugin
from ConfigParser import ConfigParser

class TestpluginPlugin(b3.plugin.Plugin):
    
    _adminPlugin = None
 
    def onLoadConfig(self):

        config = ConfigParser()
        config.optionxform = str
        config.read(self.config.fileName)

        self.botlist = {}

        for (name, config) in config.items('bots'):
            self.botlist.update({name:config})
            self.debug("%s: %s"%(name, config))
        
 
    def onStartup(self):
        
        self._adminPlugin = self.console.getPlugin('admin')
        if not self._adminPlugin:
            raise AttributeError('could not find admin plugin')

it works for me
PtitBigorneau

Offline ph03n1x

  • Sr. Member
  • ****
  • Posts: 296
    • Lovers and Haters Urban Terror
Re: Preserve case when reading config
« Reply #6 on: November 25, 2015, 03:20:55 PM »
Oh I see my problem...doh! Sorry that was my mistake... I had "config = ConfigParser" without the "()".
Anyways, it's still loading config in lower case. The entire code: https://github.com/ph03n1x9000/BotHandler/tree/master/extplugins

What it's loading as:
Code: [Select]
151125 14:10:58 DEBUG    'BothandlerPlugin: Bot loaded: python 5 free 40 chewy'
151125 14:10:58 DEBUG    'BothandlerPlugin: Bot loaded: boa 5 free 0 mittens'
151125 14:10:58 DEBUG    'BothandlerPlugin: Bot loaded: puma 5 free 0 barky'
151125 14:10:58 DEBUG    'BothandlerPlugin: Bot loaded: puma 5 free 0 fido'

Thanks for you time!

cu :)

Offline 82ndAB.Bravo17

  • Dev. Team
  • Hero Member
  • *
  • Posts: 2628
Re: Preserve case when reading config
« Reply #7 on: November 25, 2015, 03:26:08 PM »
You could try something like::


Code: [Select]
BliTz: Blitz##python 5 red 0
ShaMe: Shame##boa 5 blue 0
[/pre]

and split the value on ## to get what you want.

Offline ph03n1x

  • Sr. Member
  • ****
  • Posts: 296
    • Lovers and Haters Urban Terror
Re: Preserve case when reading config
« Reply #8 on: November 25, 2015, 04:54:04 PM »
Hey Bravo17,
Thanks for tip. It actually gave me an idea that made everything work out.
I was thinking that everything would be converted to lower case, you showed me that it was only the parameter name that was converted, not the value.

I used:
Code: [Select]
bot1: python 5 red 0 Blitz
bot2: boa 5 blue 0 ShaMe

together with:
Code: [Select]
for (bname, bconfig) in self.config.items('bots'):
    name = bconfig.rsplit(' ', 1)[1]
    config = bconfig.rsplit(' ', 1)[0]

Thanks guys! Really appreciate it!

cu :)

Offline PtitBigorneau

  • B3 Contrib/Support
  • Sr. Member
  • *
  • Posts: 194
    • www.ptitbigorneau.fr
Re: Preserve case when reading config
« Reply #9 on: November 25, 2015, 05:06:39 PM »
with that
Code: [Select]
# -*- coding: utf-8 -*-

__author__  = 'PtitBigorneau'
__version__ = '1.0'

import b3
import b3.plugin
from ConfigParser import ConfigParser

class TestpluginPlugin(b3.plugin.Plugin):
    
    _adminPlugin = None
    _botlist = {}
 
    def onLoadConfig(self):

        config = ConfigParser()
        config.optionxform = str
        config.read(self.config.fileName)


        for (name, config) in config.items('bots'):
            self._botlist.update({name:config})

        self.loadBotstuff()


    def onStartup(self):
        
        self._adminPlugin = self.console.getPlugin('admin')
        if not self._adminPlugin:
            raise AttributeError('could not find admin plugin')

    def loadBotstuff(self):
 
        for bot in self._botlist:
            self.debug("%s: %s"%(self._botlist[bot], bot))
I have this
Quote
DEBUG    'TestpluginPlugin: python 5 free 40: Chewy'
DEBUG    'TestpluginPlugin: puma 5 free 0: Fido'
DEBUG    'TestpluginPlugin: boa 5 free 0: Mittens'
DEBUG    'TestpluginPlugin: puma 5 free 0: Barky'
be used
Code: [Select]
        config = ConfigParser()
        config.optionxform = str
        config.read(self.config.fileName)
and
Code: [Select]
        for (name, config) in config.items('bots'):
            self._botlist.update({name:config})
in the same function
PtitBigorneau

Offline ph03n1x

  • Sr. Member
  • ****
  • Posts: 296
    • Lovers and Haters Urban Terror
Re: Preserve case when reading config
« Reply #10 on: November 25, 2015, 06:15:20 PM »
Ooooooh that's what I was doing wrong...I was using "self.config.items('bots')" so it was not using the local config read. I needed to change that to "config.items('bots')" .I'm slow sometimes...sorry.

It's working great now..thanks!!!

cu :)

 


Rate this page +1 at Google Search