Exemplo n.º 1
0
 def test_ensure_endswith(self):
     self.assertEqual(utils.ensure_endswith('ala', 'a'), 'ala')
Exemplo n.º 2
0
    def process(self, input, output=None):
        """Processes input file and export output file.

        Utilizes input classes like Parser and output like Exporter
        Uses OutputItems objects to build OutputItems
        """

        # parse tree
        finput = open(input, 'rt')
        fcontent = finput.read()
        finput.close()
        parser = ParseFile()
        ast_tree = parser.parse(fcontent)

        # process parse tree to create OutputItems structure
        # is this step necessary ??

        # items
        # initialize variables
        title         = os.path.basename(os.path.splitext(input)[0])
        section       = ''
        subsection    = ''
        subsubsection = ''
        prefix        = '' # prefix used
        items         = OutputItems()

        if config.DEBUG:
            print str(ast_tree)

        for obj in ast_tree.children:
            prefix = self.build_prefix(title, section, subsection, subsubsection)
            if type(obj) is ASTTitle:
                title = obj.content
            elif type(obj) is ASTSection:
                section = obj.content
            elif type(obj) is ASTSubsection:
                subsection = obj.content
            elif type(obj) is ASTSubsubsection:
                subsubsection = obj.content
            # process each class command
            # by generating output items from its ast child items
            elif isinstance(obj, ASTClassCommand):
                for block in obj.children:
                    words = block.children
                    # which words it should ask for?
                    if obj.get_option('ask') == 'marked' or obj.get_option('ask') == '':
                        hide_words_idx = [i for i in range(len(block.children)) \
                                            if words[i].get_option('marked')]
                    else: # default option is set asking for all words
                        hide_words_idx =  [i for i in range(len(block.children)) \
                                            if not words[i].get_option('ignored') and \
                                                type(words[i]) != ASTSeparatorWord]

                    # build question (output item) for each hidden words
                    for hide_idx in hide_words_idx:
                        question = self.build_question(words, hide_idx, isinstance(obj, ASTVerbatim))
                        answer = words[hide_idx].content.strip()

                        question_hint = words[hide_idx].get_option('question_hint')
                        answer_hint = words[hide_idx].get_option('answer_hint')

                        item = OutputItem(ensure_endswith(prefix, ': '), question, answer, question_hint, answer_hint)
                        items.add_item(item)


        # now export items using exporter
        exporter = QAExporter()
        exporter.export_file(items, output=None)


        # now export items using exporter
        exporter = QAExporter()
Exemplo n.º 3
0
 def test_ensure_endswith(self):
     self.assertEqual(utils.ensure_endswith('ala', 'a'), 'ala')
Exemplo n.º 4
0
    def process(self, input, output=None):
        """Processes input file and export output file.

        Utilizes input classes like Parser and output like Exporter
        Uses OutputItems objects to build OutputItems
        """

        # parse tree
        finput = open(input, 'rt')
        fcontent = finput.read()
        finput.close()
        parser = ParseFile()
        ast_tree = parser.parse(fcontent)

        # process parse tree to create OutputItems structure
        # is this step necessary ??

        # items
        # initialize variables
        title         = os.path.basename(os.path.splitext(input)[0])
        section       = ''
        subsection    = ''
        subsubsection = ''
        prefix        = '' # prefix used
        items         = OutputItems()

        if config.DEBUG:
            print str(ast_tree)

        for obj in ast_tree.children:
            prefix = self.buildPrefix(title, section, subsection, subsubsection)
            if type(obj) is ASTTitle:
                title = obj.content
            elif type(obj) is ASTSection:
                section = obj.content
            elif type(obj) is ASTSubsection:
                subsection = obj.content
            elif type(obj) is ASTSubsubsection:
                subsubsection = obj.content
            # process each class command
            # by generating output items from its ast child items
            elif isinstance(obj, ASTClassCommand):
                for block in obj.children:
                    words = block.children
                    # which words it should ask for?
                    if obj.getOption('ask') == 'marked' or obj.getOption('ask') == '':
                        hide_words_idx = [i for i in range(len(block.children)) \
                                            if words[i].getOption('marked')]
                    else: # default option is set asking for all words
                        hide_words_idx =  [i for i in range(len(block.children)) \
                                            if not words[i].getOption('ignored') and \
                                                type(words[i]) != ASTSeparatorWord]

                    # build question (output item) for each hidden words
                    for hide_idx in hide_words_idx:
                        question = self.buildQuestion(words, hide_idx, isinstance(obj, ASTVerbatim))
                        answer = words[hide_idx].content.strip()

                        question_hint = words[hide_idx].getOption('question_hint')
                        answer_hint = words[hide_idx].getOption('answer_hint')

                        item = OutputItem(ensure_endswith(prefix, ': '), question, answer, question_hint, answer_hint)
                        items.addItem(item)


        # now export items using exporter
        exporter = QAExporter()
        exporter.exportFile(items, output=None)


        # now export items using exporter
        exporter = QAExporter()