user_question.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 » user_question.py
# $Id: user_question.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'''
    This uses a parser object which is expected to have the following methods:

        - get_token(check_token=None)
        - parse_match()
        - parse_alternatives()
        - parse_review()
'''

from string import Template

class user_question(object):
    match = None
    review = None

    def __init__(self, format):
        self.format = Template(format)

    def __repr__(self):
        head = "<%s" % self.__class__.__name__
        if self.match: head += self.repr_match()
        head += ": %s" % repr(self.format.template)
        if self.review:
            head += " %s" % ' ! '.join(repr(m) for m, text in self.review)
        return head + ">"

    def repr_match(self):
        return "(%s)" % repr(self.match)

    def parse(self, parser):
        self.parse_args(parser)
        self.review = parser.parse_review()

    def parse_args(self, parser): pass

    def set_question_base(self, question_base):
        self.question_base = question_base

    def get_ask_module(self):
        return self.question_base.get_ask_module()

    def ask(self, format_params):
        ask_fn = getattr(self.get_ask_module(),
                         'ask_' + self.__class__.__name__)
        if self.review:
            review = tuple((match, template.substitute(format_params))
                           for match, template in self.review)
        else:
            review = None
        arg2 = self.prepare_arg2(format_params)
        if arg2:
            return ask_fn(self.format.substitute(format_params), arg2,
                          review=review)
        return ask_fn(self.format.substitute(format_params),
                      review=review)

    def prepare_arg2(self, format_params):
        return self.match

class yn(user_question):
    pass

class match_args(user_question):
    def parse_args(self, parser):
        token, value = parser.get_token()
        if token == 'lparen':
            self.match = parser.parse_match()
            parser.get_token('rparen')
        else:
            parser.push_token()

class integer(match_args): pass

class float(match_args): pass

class number(match_args): pass

class string(match_args): pass

class select_1(user_question):
    def repr_match(self):
        return "(%s)" % ' '.join(repr(t) + ':' for t, text in self.match)

    def parse(self, parser):
        self.match, self.review = parser.parse_alternatives()

    def prepare_arg2(self, format_params):
        return tuple((tag, label.substitute(format_params))
                     for tag, label in self.match)

class select_n(select_1): pass

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