Example #1
0
    def userchoice(prompt, choices, help):
        """
        Choices menu for user input.

        The user get a menu with help content for choices index followed by
        a numeric index value. The user just choose a numeric index for his
        choice and this choice is returned back

        :param prompt: the prompt to be shown to the user
        :type prompt: str
        :param choices: the choices to be shown
        :type choices: tuple or list
        :param help: the help to be shown
        :type help: tuple or list
        """

        if help is None:
            raise RuntimeError('Help can not be None')

        if len(choices) != len(help):
            raise RuntimeError(
                'Choices and Help should has the same number of items'
            )

        if len(choices) == 0 or len(help) == 0:
            raise RuntimeError('Nor choices or help can be empty')

        color = create_color_func('blue')
        print('\n{}'.format(prompt))
        try:
            while True:
                for choice in choices:
                    print('{} [{}] '.format(
                        color(help[choices.index(choice)].ljust(30)),
                        bold(str(choices.index(choice)))
                    ))

                response = raw_input('[{}] '.format(
                    '/'.join([str(choices.index(choices[i])) for i in range(
                        len(choices))])
                ))

                if response:
                    for key in choices:
                        # an empty response will match the first
                        # value in responses
                        if response == str(choices.index(key))[:len(response)]:
                            return key

                print('Sorry, response "{}" not understood.'.format(response))
                print('\n{}'.format(prompt))
        except (EOFError, KeyboardInterrupt):
            print('Interrupted')
            sys.exit(1)
Example #2
0
    def getChild(self, name, request):
        """
        This method is not supposed to be called because we are overriden
        the full route dispatching mechanism already built in Twisted.

        Class variable level :attr:`isLeaf` is supposed to be always
        :keyword:`True` but any users can override it in their
        :class:`~mamba.Controller` implementation so we make sure that the
        native twisted behavior is never executed.

        If you need Twisted native url dispatching in your site you should
        use :class:`~twisted.web.resource.Resource` class directly in your
        code.

        :param name: ignored
        :type name: string
        :param request: a :class:`twisted.web.server.Request` specifying
                        meta-information about the request that is being made
                        for this child that is ignored at all.
        :type request: :class:`~twisted.web.server.Request`
        :rtype: :class:`~mamba.Controller`
        """

        # someone is using Controller wrong
        msg = (
            '\n\n'
            '===============================================================\n'
            '                             WARNING\n'
            '===============================================================\n'
            'getChild method has been called by Twisted in the {controller}\n'
            'Controller. That means that you are defining the class level \n'
            'variable `isLeaf` as False, this cause Twisted try to dispatch \n'
            'routes with its own built mechanism instead of mamba routing.\n\n'
            'This is not a fatal error but if you are not using controllers\n'
            'containers you should revise your Controller becaue should be \n'
            'probable that your routing dispatching does not work at all \n'
            '(even the Twisted ones)\n\n'
            'If you are using controllers containers just overwrite the \n'
            'getChild method in your container controller to disable this \n'
            'warning (your getChild can just return self)\n'
            '===============================================================\n'
            '                             WARNING\n'
            '===============================================================\n'
            ''.format(controller=self.__class__.__name__)
        )
        log.msg(bold(msg))

        return self
Example #3
0
    def userinput(prompt):
        """
        Process user input

        :param prompt: the prompt to be shown
        :type prompt: str
        """

        print('\n{}'.format(bold(prompt)))
        try:
            response = raw_input('$ ')
            if response:
                return response
        except (EOFError, KeyboardInterrupt):
            print('Interrupted')
            sys.exit(1)
Example #4
0
    def getChild(self, name, request):
        """
        This method is not supposed to be called because we are overriden
        the full route dispatching mechanism already built in Twisted.

        Class variable level :attr:`isLeaf` is supposed to be always
        :keyword:`True` but any users can override it in their
        :class:`~mamba.Controller` implementation so we make sure that the
        native twisted behavior is never executed.

        If you need Twisted native url dispatching in your site you should
        use :class:`~twisted.web.resource.Resource` class directly in your
        code.

        :param name: ignored
        :type name: string
        :param request: a :class:`twisted.web.server.Request` specifying
                        meta-information about the request that is being made
                        for this child that is ignored at all.
        :type request: :class:`~twisted.web.server.Request`
        :rtype: :class:`~mamba.Controller`
        """

        # someone is using Controller wrong
        msg = (
            '\n\n'
            '===============================================================\n'
            '                             WARNING\n'
            '===============================================================\n'
            'getChild method has been called by Twisted in the {controller}\n'
            'Controller. That means that you are defining the class level \n'
            'variable `isLeaf` as False, this cause Twisted try to dispatch \n'
            'routes with its own built mechanism instead of mamba routing.\n\n'
            'This is not a fatal error but you should revise your Controller\n'
            'because should be probable that your routing dispatching does\n'
            'not work at all (even the Twisted ones)\n'
            '===============================================================\n'
            '                             WARNING\n'
            '===============================================================\n'
            ''.format(controller=self.__class__.__name__))
        log.msg(bold(msg))

        return self