Rev | Line | |
---|
[8] | 1 | # -*- coding: utf-8 -*- |
---|
| 2 | |
---|
| 3 | __version__ = '$Revision$' |
---|
[28] | 4 | __author__ = 'Atzm WATANABE <atzm@atzm.org>' |
---|
[8] | 5 | |
---|
| 6 | import os, ConfigParser |
---|
| 7 | |
---|
| 8 | _userconfig = 'amazonbot.ini' |
---|
[22] | 9 | _parser = ConfigParser.RawConfigParser() |
---|
[8] | 10 | |
---|
| 11 | def set(sect, key, val): |
---|
| 12 | try: |
---|
| 13 | _parser.set(sect, key, str(val)) |
---|
| 14 | except ConfigParser.NoSectionError: |
---|
| 15 | _parser.add_section(sect) |
---|
| 16 | set(sect, key, val) |
---|
| 17 | |
---|
| 18 | def get(sect, key, valuetype=''): # valuetype in ['', 'int', 'float', 'boolean'] |
---|
| 19 | try: |
---|
| 20 | method = getattr(_parser, 'get%s' % valuetype) |
---|
| 21 | return method(sect, key) |
---|
| 22 | except (ConfigParser.NoSectionError, ConfigParser.NoOptionError): |
---|
| 23 | if valuetype == 'int': |
---|
| 24 | return 0 |
---|
| 25 | elif valuetype == 'float': |
---|
| 26 | return 0.0 |
---|
| 27 | elif valuetype == 'boolean': |
---|
| 28 | return False |
---|
| 29 | else: |
---|
| 30 | return '' |
---|
| 31 | |
---|
[22] | 32 | def has_section(sect): |
---|
| 33 | return _parser.has_section(sect) |
---|
| 34 | |
---|
[8] | 35 | def write(filename=''): |
---|
| 36 | if not filename: |
---|
| 37 | filename = _userconfig |
---|
| 38 | _parser.write(file(filename, 'w')) |
---|
| 39 | |
---|
| 40 | def read(filename=''): |
---|
| 41 | if not filename or not os.path.isfile(filename): |
---|
| 42 | filename = _userconfig |
---|
| 43 | _parser.read(filename) |
---|
| 44 | |
---|
| 45 | def items(sect='DEFAULT'): |
---|
| 46 | for key, val in _parser.items(str(sect)): |
---|
| 47 | yield key, val |
---|
| 48 | |
---|
| 49 | def init(): |
---|
| 50 | read() |
---|
| 51 | |
---|
| 52 | if __name__ == '__main__': |
---|
| 53 | import sys |
---|
| 54 | init() |
---|
| 55 | _parser.write(sys.stdout) |
---|
Note: See
TracBrowser
for help on using the repository browser.