~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/ui/__init__.py

  • Committer: Martin Pool
  • Date: 2010-09-14 06:46:18 UTC
  • mto: This revision was merged to the branch mainline in revision 5426.
  • Revision ID: mbp@sourcefrog.net-20100914064618-1b05bktzv3513mx0
Add ConfirmationUserInterfacePolicy that lets specific confirmations be configured off.

Show diffs side-by-side

added added

removed removed

Lines of Context:
100
100
    return val
101
101
 
102
102
 
 
103
class ConfirmationUserInterfacePolicy(object):
 
104
    """Wrapper for a UIFactory that allows or denies all confirmed actions."""
 
105
 
 
106
    def __init__(self, wrapped_ui, default_answer, specific_answers):
 
107
        """Generate a proxy UI that does no confirmations.
 
108
 
 
109
        :param wrapped_ui: Underlying UIFactory.
 
110
        :param default_answer: Bool for whether requests for
 
111
            confirmation from the user should be noninteractively accepted or
 
112
            denied.
 
113
        :param specific_answers: Map from confirmation_id to bool answer.
 
114
        """
 
115
        self.wrapped_ui = wrapped_ui
 
116
        self.default_answer = default_answer
 
117
        self.specific_answers = specific_answers
 
118
 
 
119
    def __getattr__(self, name):
 
120
        return getattr(self.wrapped_ui, name)
 
121
 
 
122
    def __repr__(self):
 
123
        return '%s(%r, %r, %r)' % (
 
124
            self.__class__.__name__,
 
125
            self.wrapped_ui,
 
126
            self.default_answer, 
 
127
            self.specific_answers)
 
128
 
 
129
    def confirm_action(self, prompt, confirmation_id, prompt_kwargs):
 
130
        if confirmation_id in self.specific_answers:
 
131
            return self.specific_answers[confirmation_id]
 
132
        elif self.default_answer is not None:
 
133
            return self.default_answer
 
134
        else:
 
135
            return self.wrapped_ui.confirm_action(
 
136
                prompt, confirmation_id, prompt_kwargs)
 
137
 
 
138
 
103
139
class UIFactory(object):
104
140
    """UI abstraction.
105
141
 
401
437
    def confirm_action(self, prompt, confirmation_id, prompt_kwargs):
402
438
        return True
403
439
 
 
440
    def __repr__(self):
 
441
        return '%s()' % (self.__class__.__name__, )
 
442
 
404
443
 
405
444
class SilentUIFactory(NoninteractiveUIFactory):
406
445
    """A UI Factory which never prints anything.