Skip to content
theepicsnail edited this page Mar 26, 2011 · 5 revisions

SuperBot2 comes with a Configuration module that can be used anywhere. It references configuration files in the Config/ directory. Files must follow the format: "Config/<name>.cfg". A simple example of a config file looks like:

Foo.cfg

[Section1]
arg1=1
arg2=true

[Section2]
arg1=value3
arg2=1 2 3

In the code these values can be accessed as follows

from Configuration import ConfigFile
foo = ConfigFile("Foo")
if foo:
  print foo["Section1"] # [('arg1', 'value1'), ('arg2', 'value2')]
  print foo["Section1","arg1"] # value1
  
  for section in foo:              # This produces the entire 
    print "[%s]"%section           # configuration file without
    for key,value in foo[section]: # any of the white space that
      print "%s=%s"%(key,value)    # may have been in the file.

  print foo["Section1","arg1",int] # 1
  print foo["Section1","arg2",bool] # True
  print foo["Section2","arg1",str] # value3

  def converter(line):             #Custom converters can be used as well.
    return map(int, line.split())  

  print foo["Section2","arg2",converter] # [1, 2, 3]

else:
  print "Failed to open Foo"

TODO

  • Look into writing to config files though this.
  • Come up with some useful converters to package with this.

Clone this wiki locally