2000-03-03 18:57:42 -04:00
|
|
|
"""Provides access to configuration information"""
|
|
|
|
|
|
|
|
import os
|
|
|
|
import sys
|
2000-03-06 10:43:20 -04:00
|
|
|
from ConfigParser import ConfigParser, NoOptionError, NoSectionError
|
2000-03-03 18:57:42 -04:00
|
|
|
|
|
|
|
class IdleConfParser(ConfigParser):
|
|
|
|
|
|
|
|
# these conf sections do not define extensions!
|
|
|
|
builtin_sections = {}
|
|
|
|
for section in ('EditorWindow', 'Colors'):
|
|
|
|
builtin_sections[section] = section
|
2001-01-17 04:48:39 -04:00
|
|
|
|
2000-03-03 18:57:42 -04:00
|
|
|
def getcolor(self, sec, name):
|
|
|
|
"""Return a dictionary with foreground and background colors
|
|
|
|
|
|
|
|
The return value is appropriate for passing to Tkinter in, e.g.,
|
|
|
|
a tag_config call.
|
|
|
|
"""
|
2001-01-17 04:48:39 -04:00
|
|
|
fore = self.getdef(sec, name + "-foreground")
|
|
|
|
back = self.getdef(sec, name + "-background")
|
2000-03-03 18:57:42 -04:00
|
|
|
return {"foreground": fore,
|
|
|
|
"background": back}
|
|
|
|
|
|
|
|
def getdef(self, sec, options, raw=0, vars=None, default=None):
|
|
|
|
"""Get an option value for given section or return default"""
|
2001-01-17 04:48:39 -04:00
|
|
|
try:
|
2000-03-03 18:57:42 -04:00
|
|
|
return self.get(sec, options, raw, vars)
|
2001-01-17 04:48:39 -04:00
|
|
|
except (NoSectionError, NoOptionError):
|
|
|
|
return default
|
2000-03-03 18:57:42 -04:00
|
|
|
|
|
|
|
def getsection(self, section):
|
|
|
|
"""Return a SectionConfigParser object"""
|
|
|
|
return SectionConfigParser(section, self)
|
|
|
|
|
|
|
|
def getextensions(self):
|
|
|
|
exts = []
|
|
|
|
for sec in self.sections():
|
|
|
|
if self.builtin_sections.has_key(sec):
|
2001-01-17 04:48:39 -04:00
|
|
|
continue
|
|
|
|
# enable is a bool, but it may not be defined
|
|
|
|
if self.getdef(sec, 'enable') != '0':
|
|
|
|
exts.append(sec)
|
2000-03-03 18:57:42 -04:00
|
|
|
return exts
|
|
|
|
|
|
|
|
def reload(self):
|
2000-03-07 13:55:32 -04:00
|
|
|
global idleconf
|
|
|
|
idleconf = IdleConfParser()
|
2000-03-03 18:57:42 -04:00
|
|
|
load(_dir) # _dir is a global holding the last directory loaded
|
|
|
|
|
|
|
|
class SectionConfigParser:
|
|
|
|
"""A ConfigParser object specialized for one section
|
|
|
|
|
|
|
|
This class has all the get methods that a regular ConfigParser does,
|
|
|
|
but without requiring a section argument.
|
|
|
|
"""
|
|
|
|
def __init__(self, section, config):
|
|
|
|
self.section = section
|
|
|
|
self.config = config
|
|
|
|
|
|
|
|
def options(self):
|
|
|
|
return self.config.options(self.section)
|
|
|
|
|
|
|
|
def get(self, options, raw=0, vars=None):
|
|
|
|
return self.config.get(self.section, options, raw, vars)
|
|
|
|
|
|
|
|
def getdef(self, options, raw=0, vars=None, default=None):
|
|
|
|
return self.config.getdef(self.section, options, raw, vars, default)
|
|
|
|
|
|
|
|
def getint(self, option):
|
|
|
|
return self.config.getint(self.section, option)
|
2001-01-17 04:48:39 -04:00
|
|
|
|
2000-03-03 18:57:42 -04:00
|
|
|
def getfloat(self, option):
|
|
|
|
return self.config.getint(self.section, option)
|
2001-01-17 04:48:39 -04:00
|
|
|
|
2000-03-03 18:57:42 -04:00
|
|
|
def getboolean(self, option):
|
|
|
|
return self.config.getint(self.section, option)
|
|
|
|
|
|
|
|
def getcolor(self, option):
|
|
|
|
return self.config.getcolor(self.section, option)
|
|
|
|
|
|
|
|
def load(dir):
|
|
|
|
"""Load IDLE configuration files based on IDLE install in dir
|
|
|
|
|
|
|
|
Attempts to load two config files:
|
|
|
|
dir/config.txt
|
|
|
|
dir/config-[win/mac/unix].txt
|
|
|
|
dir/config-%(sys.platform)s.txt
|
|
|
|
~/.idle
|
|
|
|
"""
|
|
|
|
global _dir
|
|
|
|
_dir = dir
|
|
|
|
|
2000-03-06 10:16:41 -04:00
|
|
|
if sys.platform[:3] == 'win':
|
2000-03-03 18:57:42 -04:00
|
|
|
genplatfile = os.path.join(dir, "config-win.txt")
|
|
|
|
# XXX don't know what the platform string is on a Mac
|
2000-03-06 10:16:41 -04:00
|
|
|
elif sys.platform[:3] == 'mac':
|
2000-03-03 18:57:42 -04:00
|
|
|
genplatfile = os.path.join(dir, "config-mac.txt")
|
|
|
|
else:
|
|
|
|
genplatfile = os.path.join(dir, "config-unix.txt")
|
2001-01-17 04:48:39 -04:00
|
|
|
|
2000-03-03 18:57:42 -04:00
|
|
|
platfile = os.path.join(dir, "config-%s.txt" % sys.platform)
|
2000-03-03 19:00:41 -04:00
|
|
|
|
|
|
|
try:
|
|
|
|
homedir = os.environ['HOME']
|
|
|
|
except KeyError:
|
|
|
|
homedir = os.getcwd()
|
2000-03-07 13:55:32 -04:00
|
|
|
|
|
|
|
idleconf.read((os.path.join(dir, "config.txt"), genplatfile, platfile,
|
|
|
|
os.path.join(homedir, ".idle")))
|
|
|
|
|
|
|
|
idleconf = IdleConfParser()
|