~ctf/checkbox/bug811177

« back to all changes in this revision

Viewing changes to plugins/manual_question.py

  • Committer: Marc Tardif
  • Date: 2007-10-04 23:12:10 UTC
  • Revision ID: marc.tardif@canonical.com-20071004231210-unxckndkgndxfdp6
Refactored questions to use templates and scripts which fixes bug #149195.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import os
 
2
from commands import getoutput
 
3
 
 
4
from hwtest.lib.template import convert_string
 
5
from hwtest.lib.environ import add_path, remove_path
 
6
 
 
7
from hwtest.plugin import Plugin
 
8
from hwtest.question import Question
 
9
 
 
10
 
 
11
class Manual(Question):
 
12
 
 
13
    required_fields = Question.required_fields + ["path"]
 
14
 
 
15
    def __getattr__(self, attr):
 
16
        if attr is "description":
 
17
            return self.get_description()
 
18
        else:
 
19
            return super(Manual, self).__getattr__(attr)
 
20
 
 
21
    def run_command(self):
 
22
        add_path(self.path)
 
23
        output = getoutput(self.command)
 
24
        remove_path(self.path)
 
25
        return output
 
26
 
 
27
    def get_description(self):
 
28
        if self.command:
 
29
            output = self.run_command()
 
30
            description = convert_string(self.data["description"], {'output': output})
 
31
        else:
 
32
            description = self.data["description"]
 
33
        return description
 
34
 
 
35
 
 
36
class ManualQuestion(Plugin):
 
37
 
 
38
    def __init__(self, config, question_factory=None):
 
39
        super(ManualQuestion, self).__init__(config)
 
40
        self._question_factory = question_factory or Manual
 
41
 
 
42
    def register(self, manager):
 
43
        super(ManualQuestion, self).register(manager)
 
44
        self._manager.reactor.call_on(("manual", "add-question"),
 
45
            self.add_question)
 
46
 
 
47
    def add_question(self, *args, **kwargs):
 
48
        kwargs["path"] = self.config.scripts_path
 
49
        question = self._question_factory(*args, **kwargs)
 
50
        self._manager.reactor.fire(("prompt", "add-question"), question)
 
51
 
 
52
 
 
53
factory = ManualQuestion