David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 1 | """Provides access to configuration information""" |
| 2 | |
| 3 | import os |
| 4 | import sys |
| 5 | from ConfigParser import ConfigParser, NoOptionError, NoSectionError |
| 6 | |
| 7 | class IdleConfParser(ConfigParser): |
| 8 | |
| 9 | # these conf sections do not define extensions! |
| 10 | builtin_sections = {} |
| 11 | for section in ('EditorWindow', 'Colors'): |
| 12 | builtin_sections[section] = section |
Kurt B. Kaiser | 7717ad6 | 2001-07-13 17:38:08 +0000 | [diff] [blame] | 13 | |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 14 | def getcolor(self, sec, name): |
| 15 | """Return a dictionary with foreground and background colors |
| 16 | |
| 17 | The return value is appropriate for passing to Tkinter in, e.g., |
| 18 | a tag_config call. |
| 19 | """ |
Kurt B. Kaiser | 7717ad6 | 2001-07-13 17:38:08 +0000 | [diff] [blame] | 20 | fore = self.getdef(sec, name + "-foreground") |
| 21 | back = self.getdef(sec, name + "-background") |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 22 | return {"foreground": fore, |
| 23 | "background": back} |
| 24 | |
| 25 | def getdef(self, sec, options, raw=0, vars=None, default=None): |
| 26 | """Get an option value for given section or return default""" |
Kurt B. Kaiser | 7717ad6 | 2001-07-13 17:38:08 +0000 | [diff] [blame] | 27 | try: |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 28 | return self.get(sec, options, raw, vars) |
Kurt B. Kaiser | 7717ad6 | 2001-07-13 17:38:08 +0000 | [diff] [blame] | 29 | except (NoSectionError, NoOptionError): |
| 30 | return default |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 31 | |
| 32 | def getsection(self, section): |
| 33 | """Return a SectionConfigParser object""" |
| 34 | return SectionConfigParser(section, self) |
| 35 | |
| 36 | def getextensions(self): |
| 37 | exts = [] |
| 38 | for sec in self.sections(): |
| 39 | if self.builtin_sections.has_key(sec): |
Kurt B. Kaiser | 7717ad6 | 2001-07-13 17:38:08 +0000 | [diff] [blame] | 40 | continue |
| 41 | # enable is a bool, but it may not be defined |
| 42 | if self.getdef(sec, 'enable') != '0': |
| 43 | exts.append(sec) |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 44 | return exts |
| 45 | |
| 46 | def reload(self): |
| 47 | global idleconf |
| 48 | idleconf = IdleConfParser() |
| 49 | load(_dir) # _dir is a global holding the last directory loaded |
| 50 | |
| 51 | class SectionConfigParser: |
| 52 | """A ConfigParser object specialized for one section |
| 53 | |
| 54 | This class has all the get methods that a regular ConfigParser does, |
| 55 | but without requiring a section argument. |
| 56 | """ |
| 57 | def __init__(self, section, config): |
| 58 | self.section = section |
| 59 | self.config = config |
| 60 | |
| 61 | def options(self): |
| 62 | return self.config.options(self.section) |
| 63 | |
| 64 | def get(self, options, raw=0, vars=None): |
| 65 | return self.config.get(self.section, options, raw, vars) |
| 66 | |
| 67 | def getdef(self, options, raw=0, vars=None, default=None): |
| 68 | return self.config.getdef(self.section, options, raw, vars, default) |
| 69 | |
| 70 | def getint(self, option): |
| 71 | return self.config.getint(self.section, option) |
Kurt B. Kaiser | 7717ad6 | 2001-07-13 17:38:08 +0000 | [diff] [blame] | 72 | |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 73 | def getfloat(self, option): |
| 74 | return self.config.getint(self.section, option) |
Kurt B. Kaiser | 7717ad6 | 2001-07-13 17:38:08 +0000 | [diff] [blame] | 75 | |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 76 | def getboolean(self, option): |
| 77 | return self.config.getint(self.section, option) |
| 78 | |
| 79 | def getcolor(self, option): |
| 80 | return self.config.getcolor(self.section, option) |
| 81 | |
| 82 | def load(dir): |
| 83 | """Load IDLE configuration files based on IDLE install in dir |
| 84 | |
| 85 | Attempts to load two config files: |
| 86 | dir/config.txt |
| 87 | dir/config-[win/mac/unix].txt |
| 88 | dir/config-%(sys.platform)s.txt |
| 89 | ~/.idle |
| 90 | """ |
| 91 | global _dir |
| 92 | _dir = dir |
| 93 | |
| 94 | if sys.platform[:3] == 'win': |
| 95 | genplatfile = os.path.join(dir, "config-win.txt") |
| 96 | # XXX don't know what the platform string is on a Mac |
| 97 | elif sys.platform[:3] == 'mac': |
| 98 | genplatfile = os.path.join(dir, "config-mac.txt") |
| 99 | else: |
| 100 | genplatfile = os.path.join(dir, "config-unix.txt") |
Kurt B. Kaiser | 7717ad6 | 2001-07-13 17:38:08 +0000 | [diff] [blame] | 101 | |
David Scherer | 7aced17 | 2000-08-15 01:13:23 +0000 | [diff] [blame] | 102 | platfile = os.path.join(dir, "config-%s.txt" % sys.platform) |
| 103 | |
| 104 | try: |
| 105 | homedir = os.environ['HOME'] |
| 106 | except KeyError: |
| 107 | homedir = os.getcwd() |
| 108 | |
| 109 | idleconf.read((os.path.join(dir, "config.txt"), genplatfile, platfile, |
| 110 | os.path.join(homedir, ".idle"))) |
| 111 | |
| 112 | idleconf = IdleConfParser() |
Kurt B. Kaiser | 8bf5b20 | 2001-07-14 04:51:06 +0000 | [diff] [blame] | 113 | load(os.path.dirname(__file__)) |