5
from debconf import Debconf, DebconfCommunicator, DebconfError, MEDIUM
11
config_base = "/etc/hwtest.d/%(name)s.conf"
12
example_base = "/usr/share/doc/%(name)s/examples/%(name)s.conf"
14
def __init__(self, name, config_path=None, example_path=None, variables=[]):
16
self._variables = variables
17
self._config_path = config_path or self.config_base % {"name": name}
18
self._example_path = example_path or self.example_base % {"name": name}
20
self._config = ConfigParser.ConfigParser()
21
if os.environ.get("DEBIAN_HAS_FRONTEND"):
22
if os.environ.get("DEBCONF_REDIR"):
23
write = os.fdopen(3, "w")
26
self._debconf = Debconf(write=write)
28
self._debconf = DebconfCommunicator(self.name)
30
def set_example(self, path):
31
self._example_path = path
33
def set_config(self, path):
34
self._config_path = path
36
def set_variables(self, variables):
37
self._variables = variables
39
def write(self, output):
40
for path in [self._example_path, self._config_path]:
41
if path and os.path.isfile(path):
42
self._config.read(path)
44
# Set configuration variables
45
for variable in self._variables:
46
section, name = variable.rsplit(self.separator, 1)
47
value = self._debconf.get(variable)
48
self._config.set(section, name, value)
55
def configure(self, priority=MEDIUM):
56
path = self._config_path
57
if path and os.path.isfile(path):
58
self._config.read(path)
60
# Set debconf variables
61
for variable in self._variables:
62
section, name = variable.rsplit(self.separator, 1)
63
if self._config.has_option(section, name):
64
self._debconf.set(variable, self._config.get(section, name))
66
# Ask questions and set new values, if needed.
68
while step < len(self._variables):
70
raise Exception, "Stepped too far back."
71
variable = self._variables[step]
73
self._debconf.input(priority, variable)
74
except DebconfError, e:
77
# Question preivously answered and skipped.
82
except DebconfError, e:
85
# User requested to go back.