ask_wx.py :  » Language-Interface » Python-Knowledge-Engine » pyke-1.1.1 » pyke » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » Language Interface » Python Knowledge Engine 
Python Knowledge Engine » pyke 1.1.1 » pyke » ask_wx.py
# $Id: ask_wx.py 081917d30609 2010-03-05 mtnyogi $
# coding=utf-8
# 
# Copyright  2008 Bruce Frederiksen
# 
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# 
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
# 
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

r'''
    A "match" here is one of:
        - an instance of qa_helpers.regexp
            - msg (for error message)
            - prompt (without the [])
            - match(str) returns converted value (None if no match)
        - an instance of qa_helpers.qmap
            - test (a match)
            - value (value to use)
        - an instance of slice (step must be None)
        - a tuple of matches (implied "or")
        - some other python value (which stands for itself)

    A "review" here is a tuple of (match, review_string)

    "Alternatives" here is a tuple of (tag, label_string)
'''

import sys
if __name__ != "__main__" \
   and ('__main__' not in sys.modules
        or 'doctest' not in sys.modules['__main__'].__file__):
    # So we don't screw up doctest runs on boxes that don't have wxPython
    # installed...
    import wx
import itertools
from pyke import qa_helpers

def review_ans(dlg, ans, review=None):
    if review:

        def matches2(ans, test):
            try:
                qa_helpers.match(ans, test)
                return True
            except ValueError:
                return False

        def matches(ans, test):
            if isinstance(ans, (tuple, list)):
                return any(itertools.imap(lambda elem: matches2(elem, test),
                                          ans))
            return matches2(ans, test)
        msg = u'\n\n'.join(review_str for review_test, review_str in review
                                       if matches(ans, review_test))
        if msg:
            dlg2 = wx.MessageDialog(dlg, msg, 'Answer Information',
                                    wx.OK | wx.ICON_INFORMATION)
            dlg2.ShowModal()
            dlg2.Destroy()

def get_answer(question, title, conv_fn=None, test=None, review=None):
    dlg = wx.TextEntryDialog(None, question, title, u'',
                             wx.CENTRE | wx.RESIZE_BORDER | wx.OK)
    while True:
        status = dlg.ShowModal()
        if status != wx.ID_OK:
            raise AssertionError("dlg.ShowModal failed with %d" % status)
        ans = dlg.GetValue()
        try:
            if conv_fn: ans = conv_fn(ans)
            if test: ans = qa_helpers.match(ans, test)
            break
        except ValueError, e:
            err = wx.MessageDialog(dlg,
                                   u"Answer should be %s\nGot %s" %
                                       (e.message, repr(ans)),
                                   u"Error Notification",
                                   wx.OK | wx.ICON_ERROR)
            err.ShowModal()
            err.Destroy()
            dlg.SetValue(u"")
    review_ans(dlg, ans, review)
    dlg.Destroy()
    return ans

def ask_yn(question, review=None):
    dlg = wx.MessageDialog(None, question, 'User Question',
                           wx.YES_NO | wx.ICON_QUESTION)

    status = dlg.ShowModal()
    if status not in (wx.ID_YES, wx.ID_NO):
        raise AssertionError("dlg.ShowModal failed with %d" % status)
    ans = status == wx.ID_YES
    review_ans(dlg, ans, review)
    dlg.Destroy()
    return ans

def ask_integer(question, match=None, review=None):
    return get_answer(question,
                      qa_helpers.match_prompt(match, int, "Enter Integer %s",
                                              'Enter Integer'),
                      conv_fn=qa_helpers.to_int,
                      test=match,
                      review=review)

def ask_float(question, match=None, review=None):
    return get_answer(question,
                      qa_helpers.match_prompt(match, float, "Enter Number %s",
                                              'Enter Number'),
                      conv_fn=qa_helpers.to_float,
                      test=match,
                      review=review)

def ask_number(question, match=None, review=None):
    return get_answer(question,
                      qa_helpers.match_prompt(match, int, "Enter Number %s",
                                              'Enter Number'),
                      conv_fn=qa_helpers.to_number,
                      test=match,
                      review=review)

def ask_string(question, match=None, review=None):
    return get_answer(question,
                      qa_helpers.match_prompt(match, str, "Enter %s",
                                              'User Question'),
                      test=match,
                      review=review)

def ask_select_1(question, alternatives, review=None):
    dlg = wx.SingleChoiceDialog(None, question, u'User Question',
                                [msg for tag, msg in alternatives],
                                wx.DEFAULT_DIALOG_STYLE | wx.RESIZE_BORDER |
                                wx.OK)
    status = dlg.ShowModal()
    if status != wx.ID_OK:
        raise AssertionError("dlg.ShowModal failed with %d" % status)
    selection = dlg.GetSelection()
    #print 'Got selection: %s' % str(selection)
    ans = alternatives[selection][0]
    review_ans(dlg, ans, review)
    dlg.Destroy()
    return ans

def ask_select_n(question, alternatives, review=None):
    #dlg = wx.lib.dialogs.MultiChoiceDialog(None, question, u'User Question',
    #                                       [msg for tag, msg in alternatives])
    dlg = wx.MultiChoiceDialog(None, question, u'User Question',
                               [msg for tag, msg in alternatives],
                               wx.DEFAULT_DIALOG_STYLE | wx.RESIZE_BORDER |
                               wx.OK)
    status = dlg.ShowModal()
    if status != wx.ID_OK:
        raise AssertionError("dlg.ShowModal failed with %d" % status)
    #selections = dlg.GetValue()
    selections = dlg.GetSelections()
    #print 'Got selections: %s' % str(selections)
    ans = tuple(alternatives[i][0] for i in selections)
    review_ans(dlg, ans, review)
    dlg.Destroy()
    return ans

www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.