| Revision 8,
1.1 KB
checked in by atzm, 19 years ago
(diff) |
|
initial import
|
| Line | |
|---|
| 1 | # -*- coding: utf-8 -*- |
|---|
| 2 | |
|---|
| 3 | __version__ = '$Revision$' |
|---|
| 4 | __author__ = 'Atzm WATANABE <sitosito@p.chan.ne.jp>' |
|---|
| 5 | |
|---|
| 6 | import os, ConfigParser |
|---|
| 7 | |
|---|
| 8 | _userconfig = 'amazonbot.ini' |
|---|
| 9 | _parser = ConfigParser.SafeConfigParser() |
|---|
| 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 | |
|---|
| 32 | def write(filename=''): |
|---|
| 33 | if not filename: |
|---|
| 34 | filename = _userconfig |
|---|
| 35 | _parser.write(file(filename, 'w')) |
|---|
| 36 | |
|---|
| 37 | def read(filename=''): |
|---|
| 38 | if not filename or not os.path.isfile(filename): |
|---|
| 39 | filename = _userconfig |
|---|
| 40 | _parser.read(filename) |
|---|
| 41 | |
|---|
| 42 | def items(sect='DEFAULT'): |
|---|
| 43 | for key, val in _parser.items(str(sect)): |
|---|
| 44 | yield key, val |
|---|
| 45 | |
|---|
| 46 | def init(): |
|---|
| 47 | read() |
|---|
| 48 | |
|---|
| 49 | if __name__ == '__main__': |
|---|
| 50 | import sys |
|---|
| 51 | init() |
|---|
| 52 | _parser.write(sys.stdout) |
|---|
Note: See
TracBrowser
for help on using the repository browser.