8 from i18n import gettext as _ |
8 from i18n import gettext as _ |
9 from demandload import * |
9 from demandload import * |
10 demandload(globals(), "errno getpass os re socket sys tempfile") |
10 demandload(globals(), "errno getpass os re socket sys tempfile") |
11 demandload(globals(), "ConfigParser mdiff templater traceback util") |
11 demandload(globals(), "ConfigParser mdiff templater traceback util") |
12 |
12 |
|
13 def dupconfig(orig): |
|
14 new = ConfigParser.SafeConfigParser(orig.defaults()) |
|
15 updateconfig(orig, new) |
|
16 return new |
|
17 |
|
18 def updateconfig(source, dest): |
|
19 for section in source.sections(): |
|
20 if not dest.has_section(section): |
|
21 dest.add_section(section) |
|
22 for name, value in source.items(section, raw=True): |
|
23 dest.set(section, name, value) |
|
24 |
13 class ui(object): |
25 class ui(object): |
14 def __init__(self, verbose=False, debug=False, quiet=False, |
26 def __init__(self, verbose=False, debug=False, quiet=False, |
15 interactive=True, traceback=False, parentui=None): |
27 interactive=True, traceback=False, parentui=None): |
16 self.overlay = {} |
28 self.overlay = None |
17 self.header = [] |
29 self.header = [] |
18 self.prev_header = [] |
30 self.prev_header = [] |
19 if parentui is None: |
31 if parentui is None: |
20 # this is the parent of all ui children |
32 # this is the parent of all ui children |
21 self.parentui = None |
33 self.parentui = None |
32 self.updateopts(verbose, debug, quiet, interactive) |
44 self.updateopts(verbose, debug, quiet, interactive) |
33 else: |
45 else: |
34 # parentui may point to an ui object which is already a child |
46 # parentui may point to an ui object which is already a child |
35 self.parentui = parentui.parentui or parentui |
47 self.parentui = parentui.parentui or parentui |
36 self.readhooks = self.parentui.readhooks[:] |
48 self.readhooks = self.parentui.readhooks[:] |
37 parent_cdata = self.parentui.cdata |
49 self.cdata = dupconfig(self.parentui.cdata) |
38 self.cdata = ConfigParser.SafeConfigParser(parent_cdata.defaults()) |
50 if self.parentui.overlay: |
39 # make interpolation work |
51 self.overlay = dupconfig(self.parentui.overlay) |
40 for section in parent_cdata.sections(): |
|
41 self.cdata.add_section(section) |
|
42 for name, value in parent_cdata.items(section, raw=True): |
|
43 self.cdata.set(section, name, value) |
|
44 |
52 |
45 def __getattr__(self, key): |
53 def __getattr__(self, key): |
46 return getattr(self.parentui, key) |
54 return getattr(self.parentui, key) |
47 |
55 |
48 def updateopts(self, verbose=False, debug=False, quiet=False, |
56 def updateopts(self, verbose=False, debug=False, quiet=False, |
76 if root is None: |
84 if root is None: |
77 root = os.path.expanduser('~') |
85 root = os.path.expanduser('~') |
78 for name, path in self.configitems("paths"): |
86 for name, path in self.configitems("paths"): |
79 if path and "://" not in path and not os.path.isabs(path): |
87 if path and "://" not in path and not os.path.isabs(path): |
80 self.cdata.set("paths", name, os.path.join(root, path)) |
88 self.cdata.set("paths", name, os.path.join(root, path)) |
|
89 # override data from config files with data set with ui.setconfig |
|
90 if self.overlay: |
|
91 updateconfig(self.overlay, self.cdata) |
81 for hook in self.readhooks: |
92 for hook in self.readhooks: |
82 hook(self) |
93 hook(self) |
83 |
94 |
84 def addreadhook(self, hook): |
95 def addreadhook(self, hook): |
85 self.readhooks.append(hook) |
96 self.readhooks.append(hook) |
86 |
97 |
87 def setconfig(self, section, name, val): |
98 def setconfig(self, section, name, value): |
88 self.overlay[(section, name)] = val |
99 if not self.overlay: |
|
100 self.overlay = ConfigParser.SafeConfigParser() |
|
101 for cdata in (self.overlay, self.cdata): |
|
102 if not cdata.has_section(section): |
|
103 cdata.add_section(section) |
|
104 cdata.set(section, name, value) |
89 |
105 |
90 def _config(self, section, name, default, funcname): |
106 def _config(self, section, name, default, funcname): |
91 if self.overlay.has_key((section, name)): |
|
92 return self.overlay[(section, name)] |
|
93 if self.cdata.has_option(section, name): |
107 if self.cdata.has_option(section, name): |
94 try: |
108 try: |
95 func = getattr(self.cdata, funcname) |
109 func = getattr(self.cdata, funcname) |
96 return func(section, name) |
110 return func(section, name) |
97 except ConfigParser.InterpolationError, inst: |
111 except ConfigParser.InterpolationError, inst: |
130 x = items.items() |
144 x = items.items() |
131 x.sort() |
145 x.sort() |
132 return x |
146 return x |
133 |
147 |
134 def walkconfig(self): |
148 def walkconfig(self): |
135 seen = {} |
|
136 for (section, name), value in self.overlay.iteritems(): |
|
137 yield section, name, value |
|
138 seen[section, name] = 1 |
|
139 sections = self.cdata.sections() |
149 sections = self.cdata.sections() |
140 sections.sort() |
150 sections.sort() |
141 for section in sections: |
151 for section in sections: |
142 for name, value in self.configitems(section): |
152 for name, value in self.configitems(section): |
143 if (section, name) in seen: continue |
|
144 yield section, name, value.replace('\n', '\\n') |
153 yield section, name, value.replace('\n', '\\n') |
145 seen[section, name] = 1 |
|
146 |
154 |
147 def extensions(self): |
155 def extensions(self): |
148 result = self.configitems("extensions") |
156 result = self.configitems("extensions") |
149 for i, (key, value) in enumerate(result): |
157 for i, (key, value) in enumerate(result): |
150 if value: |
158 if value: |