summaryrefslogtreecommitdiffstats
path: root/Tools/idle/IdleConf.py
blob: de208b0c910ff13d3a593c1f984dadd52587360a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
"""Provides access to configuration information"""

import os
import sys
from ConfigParser import ConfigParser, NoOptionError

class IdleConfParser(ConfigParser):

    # these conf sections do not define extensions!
    builtin_sections = {}
    for section in ('EditorWindow', 'Colors'):
        builtin_sections[section] = section
    
    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.
        """
	fore = self.getdef(sec, name + "-foreground")
	back = self.getdef(sec, name + "-background")
        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"""
	try:
            return self.get(sec, options, raw, vars)
	except NoOptionError:
	    return default

    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):
		continue
	    # enable is a bool, but it may not be defined
	    if self.getdef(sec, 'enable') != '0':
		exts.append(sec)
        return exts

    def reload(self):
        global IdleConf
        IdleConf = IdleConfParser()
        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)
    
    def getfloat(self, option):
        return self.config.getint(self.section, option)
    
    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

    if sys.platform.startswith('win'):
        genplatfile = os.path.join(dir, "config-win.txt")
    # XXX don't know what the platform string is on a Mac
    elif sys.platform.startswith('mac'):
        genplatfile = os.path.join(dir, "config-mac.txt")
    else:
        genplatfile = os.path.join(dir, "config-unix.txt")
        
    platfile = os.path.join(dir, "config-%s.txt" % sys.platform)
    
    for file in (os.path.join(dir, "config.txt"),
                 genplatfile,
                 platfile,
                 # XXX watch out for KeyError
                 os.path.join(os.environ['HOME'], ".idle"),
                 ):
        try:
            f = open(file)
        except IOError:
            continue
        IdleConf.readfp(f)
        f.close()

IdleConf = IdleConfParser()